hi, I have been implementing a bunch of example Neural Network codes from this site relataly. I hope @Kuba or @Tomasz can have a look at a small problem I have explained below.

It works but when I initially start up Amibroker and then press the "Start Forecast Calculation" button (see param window). Then Amibroker freezes up giving me the following output window:

But then I go out of Amibroker (I have to kill it to get out) and start Amibroker back up and then it works. Below you see that when I press the button "Start Forecast Calculation" it will show the "in-training predictions" (orange) and the forecast (violet). So it works just fine, only it crashes Amibroker just after I start up Amibroker fresh in the morning and press "Start Forecast Calculation" button.

Thank you

the AFL code:

```
SetBarsRequired( sbrAll, sbrAll );
_N( pythonpath = "C:\\Users\\win 10\\AppData\\Local\\Programs\\Python\\Python38\\mypython\\neural\\relataly\\" );
_N( pythonfile = "UnivariateDates.py" );
PyLoadFromFile( "Excersize2", pythonpath + pythonfile );
calculateforecast = ParamTrigger( "Calculate Forecast", "Start Forecast Calculation" );
trainfactor = Param( "Train Factor", 0.8, 0.1, 1, 0.01 );
nforecast = Param( "Number Forecast Points", 10, 1, 50, 1 );
sequence_length = Param( "Sequence Length", 50, 10, 250, 1 );
_N( startDate = ParamDate( "Start Date", "1-1-2021", 1 ) );
_N( endDate = ParamDate( "End Date", "7-28-2022", 1 ) );
datatype = ParamToggle( "Data Type", "Real|Synthetic", 1 );
resetstaticvariables = ParamTrigger( "Reset Static Variables", "Reset" );
if( resetstaticvariables )
{
StaticVarRemove( "prediction" );
StaticVarRemove( "forecast" );
}
if( datatype )
{
pi = 3.14159265359;
sinusFunctionArray = 0.02 * Cum( 1 ) + sin( 0.125 * pi * Cum( 1 ) );
}
else
{
sinusFunctionArray = C;
}
yearArray = Year();
monthArray = Month();
dayArray = Day();
if( calculateforecast )
{
Say( "calculate forecast" );
PyEvalFunction( "Excersize2", "getDataFromAmibroker", yearArray, monthArray, dayArray, startDate, endDate,
sinusFunctionArray, nforecast, sequence_length, trainfactor );
PyEvalFunction( "Excersize2", "calculateForecastUsingPython" );
forecastFromPython = PyEvalFunction( "Excersize2", "getForecastFromPython" );
StaticVarSet( "forecast", forecastFromPython );
predictionFromPython = PyEvalFunction( "Excersize2", "getPredictionFromPython" );
StaticVarSet( "prediction", predictionFromPython );
Say( "finished" );
}
SetChartOptions( 1, chartShowDates, chartGridMiddle, 0, 0, 0 );
Plot( sinusFunctionArray, "Data", ColorRGB( 3, 157, 252 ), styleDots, Null, Null, 0, 0, 1 );
Plot( IIf( Nz( StaticVarGet( "prediction" ) ) == 0, Null, Nz( StaticVarGet( "prediction" ) ) ), "Prediction", ColorRGB( 249, 160, 72 ), styleDots | styleNoRescale, Null, Null, 0, -1, 3 );
Plot( IIf( Nz( StaticVarGet( "forecast" ) ) == 0, Null, Nz( StaticVarGet( "forecast" ) ) ), "Forecast", ColorRGB( 243, 50, 230 ), styleDots | styleNoRescale, Null, Null, nforecast, -1, 3 );
mask = DateTime() >= StrToDateTime( startDate ) AND DateTime() <= StrToDateTime( endDate );
startmask = mask AND !Ref( mask, -1 );
endmask = !mask AND Ref( mask, -1 );
Plot( mask, "", ColorRGB( 0, 0, 40 ), styleArea | styleOwnScale | styleNoLabel, 0, 1, 0, -3, 1 );
Plot( startMask, "", ColorGold, styleHistogram | styleOwnScale | styleNoLabel, 0, 1, 0, -2, 1 );
Plot( endMask, "", ColorGold, styleHistogram | styleOwnScale | styleNoLabel, 0, 1, 0, -2, 1 );
```

the PYTHON code:

```
'''
https://www.relataly.com/
https://www.relataly.com/multi-step-time-series-forecasting-a-step-by-step-guide/275/
https://www.relataly.com/stock-market-prediction-using-a-recurrent-neural-network/122/
'''
import sys
sys.path.append('C:/Users/win 10/AppData/Roaming/Python/Python38/site-packages')
import math
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from keras.models import Sequential
from sklearn.preprocessing import MinMaxScaler
from keras.layers import LSTM, Dense, TimeDistributed, Dropout, Activation
from sklearn.preprocessing import RobustScaler
from sklearn.metrics import mean_absolute_error, mean_squared_error
import AmiPy
def getDataFromAmibroker(year, month, day, startdate, enddate, data_array, nforecast, sl, tf):
global df
global df1
global rolling_forecast_range
global sequence_length
global trainfactor
global mask
start_date = startdate
end_date = enddate
df = pd.DataFrame()
df['Year'] = year.astype(int)
df['Month'] = month.astype(int)
df['Day'] = day.astype(int)
df['Date'] = pd.to_datetime(df[['Month', 'Day', 'Year']], format='%m%d%Y')
df['Data'] = data_array
df['Predict'] = data_array # fill with data array to overwrite later
df['Forecast'] = data_array # fill with data array to overwrite later
rolling_forecast_range = int(nforecast)
sequence_length = int(sl)
trainfactor = tf
mask = (df['Date'] >= start_date) & (df['Date'] <= end_date)
df1 = df.loc[mask]
df['Predict'] = 0.0;
df1['Predict'] = 0.0;
df['Forecast'] = 0.0;
df1['Forecast'] = 0.0;
def calculateForecastUsingPython():
# Get the number of rows in the data
nrows = df1['Data'].shape[0]
# Convert the data to numpy values
np_data_unscaled = np.array(df1['Data'])
np_data_unscaled = np.reshape(np_data_unscaled, (nrows, -1))
# Transform the data by scaling each feature to a range between 0 and 1
scaler = RobustScaler()
np_data_scaled = scaler.fit_transform(np_data_unscaled)
# Prediction Index
index_Close = 0
# Split the training data into train and train data sets
# As a first step, we get the number of rows to train the model on 80% of the data
train_data_len = math.ceil(np_data_scaled.shape[0] * trainfactor)
# Create the training and test data
train_data = np_data_scaled[0:train_data_len, :]
test_data = np_data_scaled[train_data_len - sequence_length:, :]
# The RNN needs data with the format of [samples, time steps, features]
# Here, we create N samples, sequence_length time steps per sample, and 6 features
def partition_dataset(sequence_length, data):
x, y = [], []
data_len = data.shape[0]
for i in range(sequence_length, data_len):
x.append(data[i-sequence_length:i,:]) #contains sequence_length values 0-sequence_length * columsn
y.append(data[i, index_Close]) #contains the prediction values for validation (3rd column = Close), for single-step prediction
# Convert the x and y to numpy arrays
x = np.array(x)
y = np.array(y)
return x, y
# Generate training data and test data
x_train, y_train = partition_dataset(sequence_length, train_data)
x_test, y_test = partition_dataset(sequence_length, test_data)
# Configure the neural network model
epochs = 25; # An epoch is an iteration over the entire x_train and y_train data provided.
batch_size = 1; # Number of samples per gradient update. If unspecified, batch_size is 32.
# Model with n_neurons = inputshape Timestamps, each with x_train.shape[2] variables
n_neurons = x_train.shape[1] * x_train.shape[2]
model = Sequential()
model.add(LSTM(n_neurons, return_sequences=True, input_shape=(x_train.shape[1], 1)))
model.add(LSTM(n_neurons, return_sequences=False))
model.add(Dense(5))
model.add(Dense(1))
# model.add(Dense(25, activation='exponential'))
# model.add(Dense(1))
model.compile(optimizer="adam", loss="mean_squared_error")
# Train the model
history = model.fit(x_train, y_train, batch_size, epochs,verbose=0)
# Reshape the data, so that we get an array with multiple test datasets
x_test_np = np.array(x_test)
x_test_reshape = np.reshape(x_test_np, (x_test_np.shape[0], x_test_np.shape[1], 1))
# Get the predicted values
y_pred = model.predict(x_test_reshape)
y_pred_unscaled = scaler.inverse_transform(y_pred)
y_test_unscaled = scaler.inverse_transform(y_test.reshape(-1, 1))
offset1 = df1.index[0]
df1.at[train_data_len + offset1:df1['Predict'].size + offset1,'Predict'] = y_pred_unscaled
df['Predict'].loc[mask] = df1['Predict']
# Making a Multi-Step Prediction
new_df = df1.filter(['Data'])
for i in range(0, rolling_forecast_range):
last_values = new_df[-n_neurons:].values
last_values_scaled = scaler.transform(last_values)
X_input = []
X_input.append(last_values_scaled)
X_input = np.array(X_input)
X_test = np.reshape(X_input, (X_input.shape[0], X_input.shape[1], 1))
pred_value = model.predict(X_input)
pred_value_unscaled = scaler.inverse_transform(pred_value)
new_df = new_df.append(pd.DataFrame({'Data': pred_value_unscaled[0, 0]}, index=new_df.iloc[[-1]].index.values + 1))
new_df_length = new_df.size
new_df_shift = new_df.shift(-rolling_forecast_range)
st1 = df1.index[df1['Forecast'].size-1] + 1
df1.loc[st1-rolling_forecast_range:,'Forecast'] = new_df_shift.loc[st1-rolling_forecast_range:,'Data']
df['Forecast'].loc[mask] = df1['Forecast']
def getPredictionFromPython():
return df['Predict'].to_numpy()
def getForecastFromPython():
return df['Forecast'].to_numpy()
```