changes made to bot execution of mt5
This commit is contained in:
+276
-289
@@ -1,351 +1,338 @@
|
|||||||
import time
|
|
||||||
import pandas as pd
|
|
||||||
import numpy as np
|
|
||||||
from sklearn.preprocessing import MinMaxScaler
|
|
||||||
import talib
|
|
||||||
import matplotlib.pyplot as plt
|
|
||||||
import pymt5 as mt5
|
|
||||||
from keras.models import Sequential
|
|
||||||
from keras.layers import Dense, Dropout
|
|
||||||
from keras.optimizers import Adam
|
from keras.optimizers import Adam
|
||||||
import socket
|
from keras.layers import Dense, Dropout
|
||||||
|
from keras.models import Sequential
|
||||||
|
import pymt5
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
import talib
|
||||||
|
from sklearn.preprocessing import MinMaxScaler
|
||||||
|
import numpy as np
|
||||||
|
import pandas as pd
|
||||||
|
import time
|
||||||
|
import os
|
||||||
|
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
|
||||||
|
|
||||||
|
|
||||||
def start_mt5(username, password, server, path):
|
def connect_to_mt5_container():
|
||||||
# Ensure that all variables are the correct type
|
server = "localhost" # Change to the appropriate IP or hostname if necessary
|
||||||
uname = int(username) # Username must be an int
|
port = 15555 # Change to the appropriate port if necessary
|
||||||
pword = str(password) # Password must be a string
|
login = 123456 # Change to your MetaTrader login number if necessary
|
||||||
trading_server = str(server) # Server must be a string
|
password = "your_password" # Change to your MetaTrader password if necessary
|
||||||
filepath = str(path) # Filepath must be a string
|
|
||||||
# Connect to MetaTrader 5
|
# Connect to MetaTrader 5
|
||||||
if mt5.initialize(login=uname, password=pword, server=trading_server, path=filepath):
|
mt5 = pymt5.PyMT5()
|
||||||
# Login to MT5
|
mt5.onConnected = onConnected
|
||||||
if mt5.login(login=uname, password=pword, server=trading_server):
|
mt5.onDisconnected = onDisconnected
|
||||||
return True
|
mt5.onData = onData
|
||||||
else:
|
|
||||||
print("Login Fail")
|
|
||||||
quit()
|
|
||||||
return PermissionError
|
|
||||||
else:
|
|
||||||
print("MT5 Initialization Failed")
|
|
||||||
quit()
|
|
||||||
return ConnectionAbortedError
|
|
||||||
|
|
||||||
|
# Wait for the connection to be established
|
||||||
|
while not onConnected:
|
||||||
|
time.sleep(0.1)
|
||||||
|
|
||||||
def connect_to_mt5(username, password, server, path):
|
# Send login request
|
||||||
# Start the MetaTrader 5 instance
|
login_request = {
|
||||||
if start_mt5(username, password, server, path):
|
'ver': '3',
|
||||||
print("Connected to MetaTrader 5")
|
'type': '1',
|
||||||
|
'login': str(login),
|
||||||
|
'password': password,
|
||||||
|
'res': '0'
|
||||||
|
}
|
||||||
|
mt5.broadcast(login_request)
|
||||||
|
|
||||||
|
# Wait for the login response
|
||||||
|
while not onConnected:
|
||||||
|
time.sleep(0.1)
|
||||||
|
|
||||||
|
# Check if login was successful
|
||||||
|
if onConnected:
|
||||||
|
print(f"Connected to MetaTrader 5: {onConnected}")
|
||||||
else:
|
else:
|
||||||
print("Failed to connect to MetaTrader 5")
|
print("Failed to connect to MetaTrader 5")
|
||||||
return
|
|
||||||
|
|
||||||
|
|
||||||
# Define the symbols and timeframes
|
def onConnected(client_info):
|
||||||
symbol = 'EURUSD'
|
print(f"Connected: {client_info}")
|
||||||
timeframe = mt5.TIMEFRAME_H1
|
|
||||||
|
|
||||||
# Set up initial variables
|
|
||||||
lot_size = 0.01
|
|
||||||
stop_loss = 100
|
|
||||||
take_profit = 150
|
|
||||||
|
|
||||||
# Define TensorFlow neural network model
|
|
||||||
|
|
||||||
|
|
||||||
def create_neural_network_model(input_shape):
|
def onDisconnected(client_info):
|
||||||
model = Sequential()
|
print(f"Disconnected: {client_info}")
|
||||||
model.add(Dense(64, activation='relu', input_shape=input_shape))
|
|
||||||
model.add(Dropout(0.2))
|
|
||||||
model.add(Dense(64, activation='relu'))
|
|
||||||
model.add(Dropout(0.2))
|
|
||||||
model.add(Dense(1, activation='sigmoid'))
|
|
||||||
return model
|
|
||||||
|
|
||||||
|
|
||||||
# Define input shape for the neural network
|
def onData(data):
|
||||||
input_shape = (10,) # Adjust the input shape based on your features and data
|
print(f"Received data: {data}")
|
||||||
|
|
||||||
# Create the neural network model
|
|
||||||
neural_network_model = create_neural_network_model(input_shape)
|
|
||||||
|
|
||||||
# Compile the model
|
|
||||||
neural_network_model.compile(optimizer=Adam(
|
|
||||||
learning_rate=0.001), loss='binary_crossentropy')
|
|
||||||
|
|
||||||
|
|
||||||
def get_historical_data():
|
def start_mt5_bot():
|
||||||
# Retrieve historical data
|
# Define the symbols and timeframes
|
||||||
rates = mt5.copy_rates_from_pos(symbol, timeframe, 0, 1000)
|
symbol = 'EURUSD'
|
||||||
df = pd.DataFrame(rates)
|
timeframe = 60 # H1 timeframe (1 hour)
|
||||||
df['time'] = pd.to_datetime(df['time'], unit='s')
|
|
||||||
df.set_index('time', inplace=True)
|
|
||||||
return df
|
|
||||||
|
|
||||||
|
# Set up initial variables
|
||||||
|
lot_size = 0.01
|
||||||
|
stop_loss = 100
|
||||||
|
take_profit = 150
|
||||||
|
|
||||||
def calculate_indicators_and_detect_patterns(df):
|
# Define TensorFlow neural network model
|
||||||
# Calculate RSI
|
def create_neural_network_model(input_shape):
|
||||||
rsi_period = 14
|
model = Sequential()
|
||||||
df['rsi'] = talib.RSI(df['close'], rsi_period)
|
model.add(Dense(64, activation='relu', input_shape=input_shape))
|
||||||
|
model.add(Dropout(0.2))
|
||||||
|
model.add(Dense(64, activation='relu'))
|
||||||
|
model.add(Dropout(0.2))
|
||||||
|
model.add(Dense(1, activation='sigmoid'))
|
||||||
|
return model
|
||||||
|
|
||||||
# Calculate MACD
|
# Define input shape for the neural network
|
||||||
macd_fast_period = 12
|
# Adjust the input shape based on your features and data
|
||||||
macd_slow_period = 26
|
input_shape = (10,)
|
||||||
macd_signal_period = 9
|
|
||||||
df['macd'], _, df['macd_signal'] = talib.MACD(df['close'], fastperiod=macd_fast_period,
|
|
||||||
slowperiod=macd_slow_period, signalperiod=macd_signal_period)
|
|
||||||
|
|
||||||
# Detect divergence based on RSI and MACD
|
# Create the neural network model
|
||||||
df['rsi_divergence'] = np.where(
|
neural_network_model = create_neural_network_model(input_shape)
|
||||||
df['rsi'].diff().shift(-1) * df['macd'].diff().shift(-1) < 0, True, False)
|
|
||||||
df['macd_divergence'] = np.where(
|
|
||||||
df['macd'].diff().shift(-1) * df['rsi'].diff().shift(-1) < 0, True, False)
|
|
||||||
|
|
||||||
# Detect support and resistance levels
|
# Compile the model
|
||||||
window = 10
|
neural_network_model.compile(optimizer=Adam(
|
||||||
df['support'] = df['low'].rolling(window).min()
|
learning_rate=0.001), loss='binary_crossentropy')
|
||||||
df['resistance'] = df['high'].rolling(window).max()
|
|
||||||
|
|
||||||
# Determine trend direction
|
def get_historical_data():
|
||||||
df['trend_200'] = df['close'].rolling(window=200).mean()
|
# Retrieve historical data
|
||||||
df['trend_50'] = df['close'].rolling(window=50).mean()
|
rates = pymt5.copy_rates_from_pos(symbol, timeframe, 0, 1000)
|
||||||
|
df = pd.DataFrame(rates)
|
||||||
|
df['time'] = pd.to_datetime(df['time'], unit='s')
|
||||||
|
df.set_index('time', inplace=True)
|
||||||
|
return df
|
||||||
|
|
||||||
# Detect double tops and bottoms
|
def calculate_indicators_and_detect_patterns(df):
|
||||||
df['pattern'] = 'None'
|
# Calculate RSI
|
||||||
df['top_pattern'] = np.where(
|
rsi_period = 14
|
||||||
(df['high'].shift(1) < df['high']) & (df['high'].shift(-1) < df['high']) &
|
df['rsi'] = talib.RSI(df['close'], rsi_period)
|
||||||
(df['high'].shift(2) > df['high']) & (
|
|
||||||
df['high'].shift(-2) > df['high']), 'Double Top', 'None'
|
|
||||||
)
|
|
||||||
df.loc[df['top_pattern'] != 'None', 'pattern'] = df['top_pattern']
|
|
||||||
df['bottom_pattern'] = np.where(
|
|
||||||
(df['low'].shift(1) > df['low']) & (df['low'].shift(-1) > df['low']) &
|
|
||||||
(df['low'].shift(2) < df['low']) & (
|
|
||||||
df['low'].shift(-2) < df['low']), 'Double Bottom', 'None'
|
|
||||||
)
|
|
||||||
df.loc[df['bottom_pattern'] != 'None', 'pattern'] = df['bottom_pattern']
|
|
||||||
|
|
||||||
return df
|
# Calculate MACD
|
||||||
|
macd_fast_period = 12
|
||||||
|
macd_slow_period = 26
|
||||||
|
macd_signal_period = 9
|
||||||
|
df['macd'], _, df['macd_signal'] = talib.MACD(df['close'], fastperiod=macd_fast_period,
|
||||||
|
slowperiod=macd_slow_period, signalperiod=macd_signal_period)
|
||||||
|
|
||||||
|
# Detect divergence based on RSI and MACD
|
||||||
|
df['rsi_divergence'] = np.where(
|
||||||
|
df['rsi'].diff().shift(-1) * df['macd'].diff().shift(-1) < 0, True, False)
|
||||||
|
df['macd_divergence'] = np.where(
|
||||||
|
df['macd'].diff().shift(-1) * df['rsi'].diff().shift(-1) < 0, True, False)
|
||||||
|
|
||||||
def generate_signals(df):
|
# Detect support and resistance levels
|
||||||
# Determine trade signals based on divergences, patterns, and trend direction
|
window = 10
|
||||||
df['signal'] = 'None'
|
df['support'] = df['low'].rolling(window).min()
|
||||||
df['divergence_signal'] = np.where((df['rsi_divergence'] == True) & (df['pattern'] != 'None'), 'Both',
|
df['resistance'] = df['high'].rolling(window).max()
|
||||||
np.where(df['rsi_divergence'] == True, 'RSI', 'Pattern'))
|
|
||||||
df['strongest_divergence_signal'] = df[[
|
|
||||||
'divergence_signal', 'macd_divergence']].max(axis=1)
|
|
||||||
df['support_resistance_signal'] = np.where(df['close'] > df['resistance'], 'Resistance',
|
|
||||||
np.where(df['close'] < df['support'], 'Support', 'None'))
|
|
||||||
df['trend_signal'] = np.where(df['close'] > df['trend_200'], 'Uptrend',
|
|
||||||
np.where(df['close'] < df['trend_200'], 'Downtrend', 'None'))
|
|
||||||
for i in range(1, len(df)):
|
|
||||||
prev_divergence_signal = df['divergence_signal'].iloc[i - 1]
|
|
||||||
curr_divergence_signal = df['divergence_signal'].iloc[i]
|
|
||||||
strongest_divergence_signal = df['strongest_divergence_signal'].iloc[i]
|
|
||||||
support_resistance_signal = df['support_resistance_signal'].iloc[i]
|
|
||||||
trend_signal = df['trend_signal'].iloc[i]
|
|
||||||
|
|
||||||
if strongest_divergence_signal != 'None':
|
# Determine trend direction
|
||||||
df['signal'].iloc[i] = strongest_divergence_signal
|
df['trend_200'] = df['close'].rolling(window=200).mean()
|
||||||
elif prev_divergence_signal == curr_divergence_signal and curr_divergence_signal != 'None':
|
df['trend_50'] = df['close'].rolling(window=50).mean()
|
||||||
df['signal'].iloc[i] = curr_divergence_signal
|
|
||||||
else:
|
|
||||||
df['signal'].iloc[i] = support_resistance_signal
|
|
||||||
|
|
||||||
if trend_signal != 'None' and df['signal'].iloc[i] != 'None':
|
# Detect double tops and bottoms
|
||||||
df['signal'].iloc[i] = trend_signal
|
df['pattern'] = 'None'
|
||||||
|
df['top_pattern'] = np.where(
|
||||||
|
(df['high'].shift(1) < df['high']) & (df['high'].shift(-1) < df['high']) &
|
||||||
|
(df['high'].shift(2) > df['high']) & (
|
||||||
|
df['high'].shift(-2) > df['high']), 'Double Top', 'None'
|
||||||
|
)
|
||||||
|
df.loc[df['top_pattern'] != 'None', 'pattern'] = df['top_pattern']
|
||||||
|
df['bottom_pattern'] = np.where(
|
||||||
|
(df['low'].shift(1) > df['low']) & (df['low'].shift(-1) > df['low']) &
|
||||||
|
(df['low'].shift(2) < df['low']) & (
|
||||||
|
df['low'].shift(-2) < df['low']), 'Double Bottom', 'None'
|
||||||
|
)
|
||||||
|
df.loc[df['bottom_pattern'] != 'None',
|
||||||
|
'pattern'] = df['bottom_pattern']
|
||||||
|
|
||||||
return df
|
return df
|
||||||
|
|
||||||
|
def generate_signals(df):
|
||||||
|
# Determine trade signals based on divergences, patterns, and trend direction
|
||||||
|
df['signal'] = 'None'
|
||||||
|
df['divergence_signal'] = np.where((df['rsi_divergence'] == True) & (df['pattern'] != 'None'), 'Both',
|
||||||
|
np.where(df['rsi_divergence'] == True, 'RSI', 'Pattern'))
|
||||||
|
df['strongest_divergence_signal'] = df[[
|
||||||
|
'divergence_signal', 'macd_divergence']].max(axis=1)
|
||||||
|
df['support_resistance_signal'] = np.where(df['close'] > df['resistance'], 'Resistance',
|
||||||
|
np.where(df['close'] < df['support'], 'Support', 'None'))
|
||||||
|
df['trend_signal'] = np.where(df['close'] > df['trend_200'], 'Uptrend',
|
||||||
|
np.where(df['close'] < df['trend_200'], 'Downtrend', 'None'))
|
||||||
|
for i in range(1, len(df)):
|
||||||
|
prev_divergence_signal = df['divergence_signal'].iloc[i - 1]
|
||||||
|
curr_divergence_signal = df['divergence_signal'].iloc[i]
|
||||||
|
strongest_divergence_signal = df['strongest_divergence_signal'].iloc[i]
|
||||||
|
support_resistance_signal = df['support_resistance_signal'].iloc[i]
|
||||||
|
trend_signal = df['trend_signal'].iloc[i]
|
||||||
|
|
||||||
def execute_trade(signal, df):
|
if strongest_divergence_signal != 'None':
|
||||||
# Implement risk management and trade execution logic based on the signals generated
|
df['signal'].iloc[i] = strongest_divergence_signal
|
||||||
# Update TensorFlow neural network model with trade outcome (loss or win)
|
elif prev_divergence_signal == curr_divergence_signal and curr_divergence_signal != 'None':
|
||||||
|
df['signal'].iloc[i] = curr_divergence_signal
|
||||||
|
else:
|
||||||
|
df['signal'].iloc[i] = support_resistance_signal
|
||||||
|
|
||||||
# Calculate risk and position size based on lot size, stop loss, and take profit
|
if trend_signal != 'None' and df['signal'].iloc[i] != 'None':
|
||||||
risk = lot_size * stop_loss
|
df['signal'].iloc[i] = trend_signal
|
||||||
strongest_divergence_signal = df['strongest_divergence_signal'].iloc[-1]
|
|
||||||
if strongest_divergence_signal == 'RSI':
|
|
||||||
risk *= 1.2 # Increase risk by 20% if RSI divergence is the strongest
|
|
||||||
elif strongest_divergence_signal == 'Pattern':
|
|
||||||
risk *= 1.5 # Increase risk by 50% if pattern divergence is the strongest
|
|
||||||
|
|
||||||
position_size = risk / (take_profit - stop_loss)
|
return df
|
||||||
|
|
||||||
try:
|
def execute_trade(signal, df):
|
||||||
if signal == 'Buy':
|
# Implement risk management and trade execution logic based on the signals generated
|
||||||
# Place a buy trade
|
# Update TensorFlow neural network model with trade outcome (loss or win)
|
||||||
result = mt5.ORDER_RESULT_FAIL
|
|
||||||
request = {
|
# Calculate risk and position size based on lot size, stop loss, and take profit
|
||||||
"action": mt5.TRADE_ACTION_DEAL,
|
risk = lot_size * stop_loss
|
||||||
"symbol": symbol,
|
strongest_divergence_signal = df['strongest_divergence_signal'].iloc[-1]
|
||||||
"volume": lot_size,
|
if strongest_divergence_signal == 'RSI':
|
||||||
"type": mt5.ORDER_TYPE_BUY,
|
risk *= 1.2 # Increase risk by 20% if RSI divergence is the strongest
|
||||||
"price": mt5.symbol_info_tick(symbol).ask,
|
elif strongest_divergence_signal == 'Pattern':
|
||||||
"sl": mt5.symbol_info_tick(symbol).ask - stop_loss * mt5.symbol_info(symbol).point,
|
risk *= 1.5 # Increase risk by 50% if pattern divergence is the strongest
|
||||||
"tp": mt5.symbol_info_tick(symbol).ask + take_profit * mt5.symbol_info(symbol).point,
|
|
||||||
"deviation": 20,
|
position_size = risk / (take_profit - stop_loss)
|
||||||
"magic": 123456,
|
|
||||||
"comment": "Buy trade",
|
try:
|
||||||
"type_time": mt5.ORDER_TIME_GTC,
|
if signal == 'Buy':
|
||||||
"type_filling": mt5.ORDER_FILLING_RETURN,
|
# Place a buy trade
|
||||||
|
result = pymt5.order_send(symbol, pymt5.OP_BUY, lot_size, 0, stop_loss, take_profit,
|
||||||
|
"Buy trade", 123456, pymt5.ORDER_TIME_GTC, 0)
|
||||||
|
outcome = 'Win' if result.retcode == pymt5.TRADE_RETCODE_DONE else 'Loss'
|
||||||
|
|
||||||
|
elif signal == 'Sell':
|
||||||
|
# Place a sell trade
|
||||||
|
result = pymt5.order_send(symbol, pymt5.OP_SELL, lot_size, 0, stop_loss, take_profit,
|
||||||
|
"Sell trade", 123456, pymt5.ORDER_TIME_GTC, 0)
|
||||||
|
outcome = 'Win' if result.retcode == pymt5.TRADE_RETCODE_DONE else 'Loss'
|
||||||
|
|
||||||
|
# Example trade outcome information
|
||||||
|
trade_outcome = {
|
||||||
|
'pattern': df['pattern'].iloc[-1],
|
||||||
|
'divergence_strength': strongest_divergence_signal,
|
||||||
|
'time': df.index[-1],
|
||||||
|
'trend_direction': df['trend_signal'].iloc[-1],
|
||||||
|
'indicator_used': strongest_divergence_signal,
|
||||||
|
'outcome': outcome
|
||||||
}
|
}
|
||||||
result = mt5.order_send(request)
|
|
||||||
outcome = 'Win' if result.retcode == mt5.TRADE_RETCODE_DONE else 'Loss'
|
|
||||||
|
|
||||||
elif signal == 'Sell':
|
# Update TensorFlow neural network model with trade outcome
|
||||||
# Place a sell trade
|
update_neural_network_model(trade_outcome)
|
||||||
result = mt5.ORDER_RESULT_FAIL
|
|
||||||
request = {
|
|
||||||
"action": mt5.TRADE_ACTION_DEAL,
|
|
||||||
"symbol": symbol,
|
|
||||||
"volume": lot_size,
|
|
||||||
"type": mt5.ORDER_TYPE_SELL,
|
|
||||||
"price": mt5.symbol_info_tick(symbol).bid,
|
|
||||||
"sl": mt5.symbol_info_tick(symbol).bid + stop_loss * mt5.symbol_info(symbol).point,
|
|
||||||
"tp": mt5.symbol_info_tick(symbol).bid - take_profit * mt5.symbol_info(symbol).point,
|
|
||||||
"deviation": 20,
|
|
||||||
"magic": 123456,
|
|
||||||
"comment": "Sell trade",
|
|
||||||
"type_time": mt5.ORDER_TIME_GTC,
|
|
||||||
"type_filling": mt5.ORDER_FILLING_RETURN,
|
|
||||||
}
|
|
||||||
result = mt5.order_send(request)
|
|
||||||
outcome = 'Win' if result.retcode == mt5.TRADE_RETCODE_DONE else 'Loss'
|
|
||||||
|
|
||||||
# Example trade outcome information
|
# Example print statements for debugging
|
||||||
trade_outcome = {
|
print(
|
||||||
'pattern': df['pattern'].iloc[-1],
|
f"Executed {signal} trade with position size: {position_size}")
|
||||||
'divergence_strength': strongest_divergence_signal,
|
print(f"Trade outcome: {trade_outcome}")
|
||||||
'time': df.index[-1],
|
|
||||||
'trend_direction': df['trend_signal'].iloc[-1],
|
|
||||||
'indicator_used': strongest_divergence_signal,
|
|
||||||
'outcome': outcome
|
|
||||||
}
|
|
||||||
|
|
||||||
# Update TensorFlow neural network model with trade outcome
|
# Additional logic for trade management, monitoring, etc.
|
||||||
update_neural_network_model(trade_outcome)
|
except Exception as e:
|
||||||
|
print(f"Error executing trade: {str(e)}")
|
||||||
|
|
||||||
# Example print statements for debugging
|
def update_neural_network_model(trade_outcome):
|
||||||
print(f"Executed {signal} trade with position size: {position_size}")
|
# Implement code to update the neural network model based on trade outcome
|
||||||
print(f"Trade outcome: {trade_outcome}")
|
pattern = trade_outcome['pattern']
|
||||||
|
divergence_strength = trade_outcome['divergence_strength']
|
||||||
|
time = trade_outcome['time']
|
||||||
|
trend_direction = trade_outcome['trend_direction']
|
||||||
|
indicator_used = trade_outcome['indicator_used']
|
||||||
|
outcome = trade_outcome['outcome']
|
||||||
|
|
||||||
# Additional logic for trade management, monitoring, etc.
|
# Example update code: Append trade outcome information to a dataset for future training
|
||||||
except Exception as e:
|
trade_data = pd.DataFrame({
|
||||||
print(f"Error executing trade: {str(e)}")
|
'pattern': [pattern],
|
||||||
|
'divergence_strength': [divergence_strength],
|
||||||
|
'time': [time],
|
||||||
|
'trend_direction': [trend_direction],
|
||||||
|
'indicator_used': [indicator_used],
|
||||||
|
'outcome': [outcome]
|
||||||
|
})
|
||||||
|
# Append the trade data to the dataset for future training
|
||||||
|
dataset = pd.read_csv('trade_dataset.csv') # Load existing dataset
|
||||||
|
updated_dataset = pd.concat([dataset, trade_data], ignore_index=True)
|
||||||
|
# Save updated dataset
|
||||||
|
updated_dataset.to_csv('trade_dataset.csv', index=False)
|
||||||
|
|
||||||
|
# Example retraining code: Retrain the neural network model with the updated dataset
|
||||||
|
# Preprocess data as per your requirements
|
||||||
|
X_train, y_train = preprocess_data(updated_dataset)
|
||||||
|
# Example retraining step
|
||||||
|
neural_network_model.fit(X_train, y_train, epochs=10, batch_size=32)
|
||||||
|
|
||||||
def update_neural_network_model(trade_outcome):
|
# Save the updated model weights
|
||||||
# Implement code to update the neural network model based on trade outcome
|
neural_network_model.save_weights('weights/model_weights.h5')
|
||||||
pattern = trade_outcome['pattern']
|
|
||||||
divergence_strength = trade_outcome['divergence_strength']
|
|
||||||
time = trade_outcome['time']
|
|
||||||
trend_direction = trade_outcome['trend_direction']
|
|
||||||
indicator_used = trade_outcome['indicator_used']
|
|
||||||
outcome = trade_outcome['outcome']
|
|
||||||
|
|
||||||
# Example update code: Append trade outcome information to a dataset for future training
|
def preprocess_data(dataset):
|
||||||
trade_data = pd.DataFrame({
|
# Define the numerical features (if any)
|
||||||
'pattern': [pattern],
|
numerical_features = [] # Update with the actual numerical feature column names
|
||||||
'divergence_strength': [divergence_strength],
|
|
||||||
'time': [time],
|
|
||||||
'trend_direction': [trend_direction],
|
|
||||||
'indicator_used': [indicator_used],
|
|
||||||
'outcome': [outcome]
|
|
||||||
})
|
|
||||||
# Append the trade data to the dataset for future training
|
|
||||||
dataset = pd.read_csv('trade_dataset.csv') # Load existing dataset
|
|
||||||
updated_dataset = pd.concat([dataset, trade_data], ignore_index=True)
|
|
||||||
# Save updated dataset
|
|
||||||
updated_dataset.to_csv('trade_dataset.csv', index=False)
|
|
||||||
|
|
||||||
# Example retraining code: Retrain the neural network model with the updated dataset
|
# Define the input features
|
||||||
# Preprocess data as per your requirements
|
input_features = dataset[[
|
||||||
X_train, y_train = preprocess_data(updated_dataset)
|
'pattern', 'divergence_strength', 'trend_direction', 'indicator_used']]
|
||||||
# Example retraining step
|
|
||||||
neural_network_model.fit(X_train, y_train, epochs=10, batch_size=32)
|
|
||||||
|
|
||||||
# Save the updated model weights
|
# Convert categorical features to one-hot encoding
|
||||||
neural_network_model.save_weights('model_weights.h5')
|
input_features = pd.get_dummies(input_features)
|
||||||
|
|
||||||
|
# Normalize numerical features (if any)
|
||||||
|
if numerical_features:
|
||||||
|
scaler = MinMaxScaler()
|
||||||
|
input_features[numerical_features] = scaler.fit_transform(
|
||||||
|
input_features[numerical_features])
|
||||||
|
|
||||||
def preprocess_data(dataset):
|
# Extract target labels from the dataset
|
||||||
# Define the numerical features (if any)
|
target_labels = dataset['outcome']
|
||||||
numerical_features = [] # Update with the actual numerical feature column names
|
|
||||||
|
|
||||||
# Define the input features
|
# Convert target labels to numerical representation (0s and 1s)
|
||||||
input_features = dataset[[
|
target_labels = target_labels.map({'Loss': 0, 'Win': 1})
|
||||||
'pattern', 'divergence_strength', 'trend_direction', 'indicator_used']]
|
|
||||||
|
|
||||||
# Convert categorical features to one-hot encoding
|
# Return the preprocessed input features and target labels
|
||||||
input_features = pd.get_dummies(input_features)
|
return input_features, target_labels
|
||||||
|
|
||||||
# Normalize numerical features (if any)
|
def visualize_data(df):
|
||||||
if numerical_features:
|
plt.figure(figsize=(10, 6))
|
||||||
scaler = MinMaxScaler()
|
plt.plot(df.index, df['close'], label='Close')
|
||||||
input_features[numerical_features] = scaler.fit_transform(
|
# Add visualizations for other indicators, levels, and patterns
|
||||||
input_features[numerical_features])
|
plt.scatter(df[df['pattern'] == 'Double Top'].index, df[df['pattern'] == 'Double Top']['high'],
|
||||||
|
color='red', marker='v', label='Double Top')
|
||||||
|
plt.scatter(df[df['pattern'] == 'Double Bottom'].index, df[df['pattern'] == 'Double Bottom']['low'],
|
||||||
|
color='green', marker='^', label='Double Bottom')
|
||||||
|
plt.legend()
|
||||||
|
plt.show()
|
||||||
|
|
||||||
# Extract target labels from the dataset
|
def run_trading_bot():
|
||||||
target_labels = dataset['outcome']
|
# Connect to MetaTrader 5 container
|
||||||
|
connect_to_mt5_container()
|
||||||
|
|
||||||
# Convert target labels to numerical representation (0s and 1s)
|
while True:
|
||||||
target_labels = target_labels.map({'Loss': 0, 'Win': 1})
|
try:
|
||||||
|
# Get historical data
|
||||||
|
df = get_historical_data()
|
||||||
|
|
||||||
# Return the preprocessed input features and target labels
|
# Calculate indicators and detect patterns
|
||||||
return input_features, target_labels
|
df = calculate_indicators_and_detect_patterns(df)
|
||||||
|
|
||||||
|
# Generate trade signals
|
||||||
|
df = generate_signals(df)
|
||||||
|
|
||||||
def visualize_data(df):
|
# Execute trades
|
||||||
plt.figure(figsize=(10, 6))
|
for i in range(1, len(df)):
|
||||||
plt.plot(df.index, df['close'], label='Close')
|
signal = df['signal'].iloc[i]
|
||||||
# Add visualizations for other indicators, levels, and patterns
|
if signal != 'None':
|
||||||
plt.scatter(df[df['pattern'] == 'Double Top'].index, df[df['pattern'] == 'Double Top']['high'],
|
execute_trade(signal, df)
|
||||||
color='red', marker='v', label='Double Top')
|
|
||||||
plt.scatter(df[df['pattern'] == 'Double Bottom'].index, df[df['pattern'] == 'Double Bottom']['low'],
|
|
||||||
color='green', marker='^', label='Double Bottom')
|
|
||||||
plt.legend()
|
|
||||||
plt.show()
|
|
||||||
|
|
||||||
|
# Visualize data
|
||||||
|
visualize_data(df)
|
||||||
|
|
||||||
def run_trading_bot():
|
except Exception as e:
|
||||||
# Connect to MetaTrader 5
|
print(f"Error running trading bot: {str(e)}")
|
||||||
connect_to_mt5(username='your_username', password='your_password',
|
|
||||||
server='your_server', path='your_mt5_installation_path')
|
# Wait for the next iteration
|
||||||
|
time.sleep(60) # Adjust the time interval as needed
|
||||||
|
|
||||||
|
# Run the trading bot
|
||||||
|
run_trading_bot()
|
||||||
|
|
||||||
# Load TensorFlow neural network model weights
|
# Load TensorFlow neural network model weights
|
||||||
neural_network_model.load_weights('model_weights.h5')
|
neural_network_model.load_weights('weights/model_weights.h5')
|
||||||
|
|
||||||
while True:
|
# Disconnect from MetaTrader 5
|
||||||
try:
|
pymt5.shutdown()
|
||||||
# Get historical data
|
|
||||||
df = get_historical_data()
|
|
||||||
|
|
||||||
# Calculate indicators and detect patterns
|
|
||||||
df = calculate_indicators_and_detect_patterns(df)
|
|
||||||
|
|
||||||
# Generate trade signals
|
|
||||||
df = generate_signals(df)
|
|
||||||
|
|
||||||
# Execute trades
|
|
||||||
for i in range(1, len(df)):
|
|
||||||
signal = df['signal'].iloc[i]
|
|
||||||
if signal != 'None':
|
|
||||||
execute_trade(signal, df)
|
|
||||||
|
|
||||||
# Visualize data
|
|
||||||
visualize_data(df)
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
print(f"Error running trading bot: {str(e)}")
|
|
||||||
|
|
||||||
# Wait for the next iteration
|
|
||||||
time.sleep(60) # Adjust the time interval as needed
|
|
||||||
|
|
||||||
|
|
||||||
# Run the trading bot
|
# Start the MetaTrader 5 bot
|
||||||
run_trading_bot()
|
start_mt5_bot()
|
||||||
|
|
||||||
# Disconnect from MetaTrader 5
|
|
||||||
mt5.shutdown()
|
|
||||||
|
|||||||
+6
-1
@@ -12,6 +12,10 @@ services:
|
|||||||
- "15558:15558"
|
- "15558:15558"
|
||||||
volumes:
|
volumes:
|
||||||
- ejtraderMT:/data
|
- ejtraderMT:/data
|
||||||
|
environment:
|
||||||
|
- DISPLAY=host.docker.internal:0
|
||||||
|
- MT5_PASSWORD=your_password
|
||||||
|
- MT5_SERVER=your_server
|
||||||
|
|
||||||
trading_bot:
|
trading_bot:
|
||||||
container_name: trading_bot
|
container_name: trading_bot
|
||||||
@@ -28,7 +32,8 @@ services:
|
|||||||
build:
|
build:
|
||||||
context: .
|
context: .
|
||||||
dockerfile: docker/DockerFile.mt5_bridge
|
dockerfile: docker/DockerFile.mt5_bridge
|
||||||
volumes: -./bridge:/bridge
|
volumes:
|
||||||
|
- ./bridge:/bridge
|
||||||
depends_on:
|
depends_on:
|
||||||
- metatrader_service
|
- metatrader_service
|
||||||
- trading_bot
|
- trading_bot
|
||||||
|
|||||||
@@ -7,6 +7,6 @@ WORKDIR /app
|
|||||||
COPY bridge/mt5_bridge.py .
|
COPY bridge/mt5_bridge.py .
|
||||||
|
|
||||||
# Install any dependencies required by the bridge script
|
# Install any dependencies required by the bridge script
|
||||||
RUN pip install python-socketio python-engineio
|
RUN pip install python-socketio python-engineio requests
|
||||||
|
|
||||||
CMD ["python", "mt5_bridge.py"]
|
CMD ["python", "mt5_bridge.py"]
|
||||||
|
|||||||
Reference in New Issue
Block a user