## By: Amirali R. Davoudpour

Market analyzing expert, programmer at IraPolska Sp. Z. o. o. in Poznan-Poland

Abstract: In this example, we initially extract market history from 5 minute charts in MetaTrader 4 platform in .CSV format, the extracted data will be subject to deep learning using a recurrent neural network model.

**Introduction: **

Choosing between FANN (Fast Artificial Neural Network Library) RNN (Recurrent Neural Network) and ANN (Artificial Neural Network) depends on the specific problem you are trying to solve and the nature of your data. Here are some considerations for each:

FANN RNN:

- Sequential and time-series data: If your data has a sequential or time-series nature, such as stock prices, speech signals, or sensor data, FANN RNN can be a suitable choice. RNNs are designed to handle sequential data by preserving information from previous steps through recurrent connections.
- Temporal dependencies: If your problem involves capturing long-term dependencies or patterns over time, RNNs can be effective. They are known for their ability to model and learn from sequences with temporal dependencies.
- Dynamic behavior: If your data exhibits dynamic behavior, where the current state depends not only on the current input but also on past inputs, RNNs can capture this behavior and make predictions accordingly.

ANN (Feedforward Neural Network):

- Non-sequential data: If your data is not sequential or time-dependent, such as image data, tabular data, or text classification, ANN can be a suitable choice. ANN architectures, such as multilayer perceptrons (MLPs), are commonly used for such tasks.
- Flexibility: ANN architectures offer more flexibility in terms of network structure and connectivity compared to RNNs. You can design various network architectures, choose different activation functions, and customize the number of hidden layers and neurons.
- Feature engineering: If your problem requires extensive feature engineering, such as extracting meaningful features from raw data before feeding them to the model, ANN architectures can be advantageous. They can handle complex relationships between features.

Ultimately, the choice between FANN RNN and ANN depends on the specific requirements and characteristics of your problem. Consider the nature of your data, the temporal or sequential aspects, and the desired behavior of your model. It can also be beneficial to experiment with both approaches and evaluate their performance on your specific task.

Here's a simple example of a deep learning script in Python using the Keras library to train a model based on raw price data:

```
python:
``````
import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.models import Sequential, save_model
from tensorflow.keras.layers import Dense, LSTM
from tensorflow.keras.callbacks import EarlyStopping
import time
# Function to preprocess data
def preprocess_data(data):
scaler = MinMaxScaler(feature_range=(0, 1))
scaled_data = scaler.fit_transform(data)
return scaled_data, scaler
# Function to create dataset for LSTM model
def create_dataset(dataset, lookback):
data_X, data_y = [], []
for i in range(len(dataset) - lookback):
data_X.append(dataset[i:i+lookback])
data_y.append(dataset[i+lookback])
return np.array(data_X), np.array(data_y)
# Function to build and train the LSTM model
def build_model(train_X, train_y, num_units, dropout_rate):
model = Sequential()
model.add(LSTM(num_units, input_shape=(train_X.shape[1], train_X.shape[2]), return_sequences=True))
model.add(LSTM(num_units))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
early_stopping = EarlyStopping(patience=10) # Early stopping to prevent overfitting
model.fit(train_X, train_y, epochs=100, batch_size=32, callbacks=[early_stopping])
return model
# Function to make predictions using the trained model
def make_predictions(model, data):
predictions = model.predict(data)
return predictions
# Function to execute trades based on trading signals
def execute_trades(signal):
# Add your trade execution logic here
if signal == 1:
print("Executing buy order")
# Execute buy order
elif signal == -1:
print("Executing sell order")
# Execute sell order
else:
print("No trade signal")
# Load and preprocess the historical price data
data = pd.read_csv('C:\\ANN\\PriceData.csv') # Replace 'C:\\ANN\\PriceData.csv' with your data file path
prices = data['Close'].values.reshape(-1, 1) # Assuming the 'Close' column contains price data
scaled_prices, scaler = preprocess_data(prices)
# Split the data into training and testing sets
train_size = int(len(scaled_prices) * 0.8)
train_data = scaled_prices[:train_size]
test_data = scaled_prices[train_size:]
# Prepare the data for LSTM model
lookback = 30 # Number of previous prices to consider
train_X, train_y = create_dataset(train_data, lookback)
test_X, test_y = create_dataset(test_data, lookback)
# Define the model architecture and train the LSTM model
num_units = 50 # Number of LSTM units
dropout_rate = 0.2 # Dropout rate for regularization
model = build_model(train_X, train_y, num_units, dropout_rate)
# Save the model as a frozen model
model.save("frozen_model.h5")
# Real-time trading loop
while True:
# Retrieve the latest price data
latest_prices = np.array([data['Close'].iloc[-lookback:]])
scaled_latest_prices = scaler.transform(latest_prices)
# Prepare the latest data for prediction
latest_X = np.reshape(scaled_latest_prices, (1, lookback, 1))
# Make predictions using the trained model
prediction = make_predictions(model, latest_X)
# Inverse transform the prediction to get the actual price
```

this code is an implementation of a basic LSTM (Long Short-Term Memory) model for time series prediction and real-time trading. It preprocesses the data, creates an LSTM model, trains it, saves the model, and then enters a real-time trading loop where it makes predictions based on the latest price data.

Here's a breakdown of the code:

- Data preprocessing:
- The code uses
`MinMaxScaler`

from scikit-learn to scale the price data between 0 and 1, which is a common practice for neural network models.

- The code uses
- Dataset creation:
- The
`create_dataset`

function is defined to create the input-output pairs for the LSTM model. It takes a time series dataset and a lookback window size as input and returns the input sequences (`data_X`

) and corresponding output values (`data_y`

).

- The
- LSTM model building and training:
- The
`build_model`

function is defined to build an LSTM model using the Keras library. - The model consists of an LSTM layer with 50 units, followed by a Dense layer with a single output neuron.
- The model is compiled with the mean squared error loss function and the Adam optimizer.
- The model is trained for a fixed number of epochs (currently set to 1) and a batch size of 32.

- The
- Model saving:
- The trained model is saved as a frozen model using
`model.save()`

.

- The trained model is saved as a frozen model using
- Real-time trading loop:
- The code enters an infinite loop to perform real-time trading.
- Within each iteration of the loop:
- The latest price data is retrieved.
- The data is preprocessed and reshaped to match the input format required by the LSTM model.
- The model makes predictions on the latest data using the
`make_predictions`

function. - The predictions are inverse transformed to obtain the actual predicted prices.
- Based on the difference between the predicted prices and the latest prices, trade execution logic is applied using the
`execute_trades`

function. - The loop waits for a specified interval (currently set to 1 minute) before making the next prediction.

Please note that the trading logic (`execute_trades`

function) and the implementation of a real-time trading system involve additional complexities, such as connecting to a trading platform, managing orders, risk management, etc. The code you provided only includes a placeholder logic for executing trades and may require further customization based on your specific requirements and the trading platform you intend to use.

To decrease the training time in the above code, you can try the following techniques:

**Reduce the number of epochs**: Decreasing the number of epochs will reduce the number of times the model goes through the entire training dataset. However, be cautious not to reduce it too much, as the model may not have sufficient iterations to learn the underlying patterns in the data. Experiment with different epoch values to find a balance between training time and model performance.

**Increase the batch size**: Increasing the batch size means that the model will process more samples before updating the weights. This can lead to faster training as the computations can be parallelized more efficiently. However, larger batch sizes may require more memory, so ensure that your system can handle the increased memory usage.

**Adjust the architecture**: Modifying the architecture of the LSTM model can impact training time. You can experiment with different configurations, such as changing the number of LSTM units, adding more LSTM layers, or introducing dropout regularization to reduce overfitting.

```
```

**Use GPU acceleration**: If you have access to a GPU (Graphics Processing Unit), you can leverage its computational power to speed up the training process. Use frameworks like TensorFlow or PyTorch with GPU support and ensure that your system is properly configured to utilize the GPU for training.

**Preprocess data offline**: If the data preprocessing step is time-consuming and doesn't change frequently, you can perform it offline and save the preprocessed data to disk. This way, you can load the preprocessed data directly during training, eliminating the need to preprocess it every time.

```
```

Remember to adjust the variables `num_epochs`

, `batch_size`

, `num_units`

, `dropout_rate`

, and apply the necessary changes based on your dataset and computational resources.

By applying these techniques, you should be able to reduce the training time for your LSTM model. However, it's important to strike a balance between training time and model performance, as overly aggressive reductions in training time may compromise the accuracy and generalization capability of the model.

Attachments:

1- Project Github repository: https://github.com/Amidav2023/Gold-Modeling

2- Gold price history data