import MetaTrader5 as mt5
from MetaTrader5 import *
import numpy as np
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score  # Import r2_score
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.regularizers import l2
from sklearn.model_selection import KFold
import time
import random
import pandas as pd
from datetime import datetime, timedelta
import dask.dataframe as dd
# display data on the MetaTrader 5 package

 

# connect to the trade account without specifying a password and a server

account=11111#input("Enter you account number: ")
clave_secreta="xxxx"#input("Enter you password: ")
server_account="xxxxx"#input("Enter the server for the account: ")

delay_1=300#input("Please enter the delay of days from where to get the ticks (better more data than epochs): ")
cerrar1=60
Seleccion_tmf="3d"#input("Please select time frame to forecast: 1 hour (press: 1), 2hours (2), 4 hours (4) or 1 day (1d), (3d), (4d), (1w): ")

epoch =30# input("Please input the number of epoch to use (for example 75,100 or 150): ")
selection="EURUSD"#input("Please enter symbol as in your broker: ")
k_reg=0.001#input("Please input the kernel regulizer you whant to have (for example 0.01): ")

k_reg=float(k_reg)

to_test_days=30
#############################################################
# Obtains actual time and date
now_inicio_script = time.time()
###########################################################
symbols=selection

if Seleccion_tmf== "1":
    tmf = mt5.TIMEFRAME_H1
    seconds = 60*60
    delay=delay_1
    cerrar=cerrar1*60
    temporalidad="1 hora"
if Seleccion_tmf== "2":
    tmf = mt5.TIMEFRAME_H2
    seconds = 60*60*2
    delay=delay_1
    cerrar=cerrar1*60*2
    temporalidad="2 horas"
if Seleccion_tmf== "3":
    tmf = mt5.TIMEFRAME_H3
    seconds = 60*60*3
    delay=delay_1
    cerrar=cerrar1*60*3
    temporalidad="3 horas"
if Seleccion_tmf== "4":
    tmf = mt5.TIMEFRAME_H4
    seconds = 60*60*4
    delay=delay_1
    cerrar=cerrar1*60*4
    temporalidad="4 horas"
if Seleccion_tmf== "1d":
    tmf = mt5.TIMEFRAME_D1
    seconds = 60*60*24
    delay=delay_1
    cerrar=cerrar1*60*24
    temporalidad="1 dia"
if Seleccion_tmf== "3d":
    tmf = mt5.TIMEFRAME_D1
    seconds = 60*60*24*3
    delay=delay_1
    cerrar=cerrar1*60*24*3
    temporalidad="3 dias"
if Seleccion_tmf== "4d":
    tmf = mt5.TIMEFRAME_D1
    seconds = 60*60*24*4
    delay=delay_1
    cerrar=cerrar1*60*24*4
    temporalidad="4 dias"
if Seleccion_tmf== "1w":
    tmf = mt5.TIMEFRAME_W1
    seconds = 60*60*24*7
    delay=delay_1
    cerrar=cerrar1*60*24*7
    temporalidad="1_week"
print(temporalidad)

print(symbols)
# Initialize an empty list to store results
df = pd.DataFrame()

df = df.drop(index=df.index)

# Create an empty dictionary to store DataFrames
dfs = pd.DataFrame()
# Empty the DataFrame
dfs = dfs.drop(index=dfs.index)

simbolito=symbols
print("Symbol: ", simbolito)

symbol = simbolito
timeframe = temporalidad

# import the 'pandas' module for displaying data obtained in the tabular form
import pandas as pd
pd.set_option('display.max_columns', 500) # number of columns to be displayed
pd.set_option('display.width', 1500)      # max table width to display
# import pytz module for working with time zone
import pytz

# set time zone to UTC
timezone = pytz.timezone("Etc/UTC")

# Obtener la fecha y hora actual
now=None
now = datetime.now() - timedelta(days=int(to_test_days))

# Print actual date and time in legible format
print("Fecha y hora actual:", now)


formatted_now=None
formatted_now = now.strftime("%Y-%m-%d %H:%M:%S")
print("Fecha y hora formateadas:", formatted_now)

# Minus n days
date_n_days_ago =None
date_n_days_ago = now - timedelta(days=int(delay))

# Print date from n days ago
print("Fecha y hora hace n días:", date_n_days_ago)

formated_n_time=date_n_days_ago.strftime("%Y-%m-%d %H:%M:%S")
print("Fecha y hora n days ago",formated_n_time)

# También puedes formatear la salida según tus preferencias
formated_date_n_days_ago = date_n_days_ago.strftime("%Y,%m,%d")
formated_date_n_days_ago_y = date_n_days_ago.strftime("%Y")
formated_date_n_days_ago_m = date_n_days_ago.strftime("%m")
formated_date_n_days_ago_d = date_n_days_ago.strftime("%d")
print("Fecha y hora formateadas hace n días:", formated_date_n_days_ago)

# create 'datetime' object in UTC time zone to avoid the implementation of a local time zone offset
utc_from = datetime(int(formated_date_n_days_ago_y),int(formated_date_n_days_ago_m),int(formated_date_n_days_ago_d), tzinfo=timezone)


path_csv = 'C:/xxxxxx/MQL5/Files/'

rates=pd.DataFrame()
# Empty DataFrame
rates = rates.drop(index=rates.index)

import os

file_path = path_csv + 'ticks_data.csv'

date_format = "%Y-%m-%d  %H:%M"

if os.path.exists(file_path):
    chunk_size = 1000
    rates = pd.read_csv(file_path, encoding='utf-16le', dtype={1: 'float32', 2: 'float32'})#,chunksize=chunk_size)

else:
    print(f"Error: File not found - {file_path}")
    quit()

print(rates)

# create DataFrame out of the obtained data
rates_frame=pd.DataFrame()
# Vaciar el DataFrame
rates_frame = rates_frame.drop(index=rates_frame.index)
rates_frame = pd.DataFrame(rates)
rates = rates.drop(index=rates.index)
#print(rates_frame)
#rename columns
rates_frame.rename(columns={0: 'Time', 1: 'bid', 2 : 'ask',3:'spread'}, inplace=True)

# Step 2: Convert the 'Time' column to pandas datetime format
rates_frame['Time'] = pd.to_datetime(rates_frame['Time'], format='%Y.%m.%d %H:%M', errors='coerce')
# Verificar si hay fechas nulas después de la conversión
if rates_frame['Time'].isnull().any():
    print("Hay fechas inválidas en la columna 'timestamp'.")

# Asumiendo que tu DataFrame rates_frame tiene una columna 'timestamp'
# Convierte la columna 'timestamp' a tipo datetime
#rates_frame['timestamp'] = pd.to_datetime(rates_frame['timestamp'])

# Seleccionar el rango de fechas que deseas
fecha_inicio = formated_n_time #'2023-01-01'
fecha_fin =formatted_now #'2023-12-31'
print("start date",fecha_inicio)
print("end date",fecha_fin)

# Filtrar el DataFrame para obtener solo las filas dentro del rango de fechas
rates_frame_filtrado = rates_frame[(rates_frame['Time'] >= fecha_inicio) & (rates_frame['Time'] <= fecha_fin)]

# Step 4: Sort the DataFrame by the 'Time' column
rates_frame_filtrado = rates_frame_filtrado.sort_values(by='Time')

#rates_frame_filtrado['time']=pd.to_datetime(rates_frame_filtrado['Time'], unit='s')
rates_frame_filtrado['close']=(((rates_frame_filtrado['Ask']+rates_frame_filtrado['Bid'])/2))

# display data
print("\nDisplay dataframe with data")
print(rates_frame_filtrado) 

# Tamaño del lote
batch_size = 1000
# Inicializar df2 fuera del bucle
df2 = pd.DataFrame()

# Procesar el DataFrame por lotes
for i in range(0, len(rates_frame_filtrado), batch_size):
    # Seleccionar el lote actual
    current_batch = rates_frame_filtrado.iloc[i:i+batch_size].copy()

    # Realizar alguna operación en el lote actual utilizando NumPy
    current_batch['target'] = current_batch['close']
    current_batch['time'] = current_batch['Time']
    current_batch['time_target'] = current_batch['time'].sub(pd.Timestamp("1970-01-01")) // pd.Timedelta('1s')
    current_batch['time_target_seconds'] = current_batch['time'].sub(pd.Timestamp("1970-01-01")) // pd.Timedelta('1s')

    serie_resultante = current_batch[['close', 'target', 'time']]

    # Almacenar el lote actual en la lista
    df_batch = pd.DataFrame(serie_resultante.values.reshape(-1, 3), columns=['close', 'target', 'time'])

    # Concatenar cada lote a df2
    df2 = pd.concat([df2, df_batch], ignore_index=True,axis=0)

df2['target'] = df2['close'].shift(-seconds)
print("tabla df2", df2)

df2 = df2.dropna()
print("df2 con dropna",df2)

x1=df2[['close']]

# Split the data into features (X) and target variable (y)
X=[]
y=[]
X = x1
y = df2['target']

# Split the data into training and testing sets
X_train=[]
X_test=[]
y_train=[]
y_test=[]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, shuffle=False)

# Standardize the features
X_train_scaled=[]
X_test_scaled=[]
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)

X_train_scaled = np.array(X_train_scaled)
y_train = np.array(y_train)
X_train_scaled = X_train_scaled.astype('float32')
y_train = y_train.astype('float32')
print(type(X_train_scaled), type(y_train))
# Build a neural network model
model=None
model = Sequential()
model.add(Dense(128, activation='relu', input_shape=(X_train.shape[1],), kernel_regularizer=l2(k_reg)))
model.add(Dense(256, activation='relu', kernel_regularizer=l2(k_reg)))
model.add(Dense(128, activation='relu', kernel_regularizer=l2(k_reg)))
model.add(Dense(64, activation='relu', kernel_regularizer=l2(k_reg)))
model.add(Dense(1, activation='linear'))

# Compile the model[]
model.compile(optimizer='adam', loss='mean_squared_error')

#print(type(X_train_scaled), type(y_train))

time_calc_start = time.time()
# Train the model
model.fit(X_train_scaled, y_train, epochs=int(epoch), batch_size=256, validation_split=0.2, verbose=1)

fit_time_seconds = time.time() - time_calc_start
print("fit time =",fit_time_seconds," seconds.")
# Use the model to predict the next n instances
X_predict=[]
X_predict_scaled=[]

predictions = pd.DataFrame()
predictions=[]

X_predict = df2.tail(seconds)[['close']]
X_predict_scaled = scaler.transform(X_predict)
predictions = model.predict(X_predict_scaled)

predictions=pd.DataFrame(predictions)

import tensorflow as tf
import numpy as np
import pandas as pd
import tf2onnx
from sklearn.model_selection import train_test_split
from sys import argv
import matplotlib.pyplot as plt 
import pytz

symbol=symbol
x_train=X_train
x_test=X_test
inp_model_name = "model."+str(symbol)+".onnx"
file_path="C:/xxxxx/MQL5/Files/"
data_path=argv[0]
last_index=data_path.rfind("\\")+1
data_path=data_path[0:last_index]
print("data path to save onnx model",data_path)

print("file path to save onnx model",file_path)

# save model to ONNX
output_path = data_path+inp_model_name
onnx_model = tf2onnx.convert.from_keras(model, output_path=output_path)
print(f"saved model to {output_path}")

#calculate metrics
from sklearn import metrics
from sklearn.metrics import r2_score

# Calculate and print mean squared error
mse = mean_squared_error(y_test, model.predict(X_test_scaled))
print(f"\nMean Squared Error: {mse}")

# Calculate and print mean absolute error
mae = mean_absolute_error(y_test, model.predict(X_test_scaled))
print(f"\nMean Absolute Error: {mae}")

# Calculate and print R2 Score
r2 = r2_score(y_test, model.predict(X_test_scaled))
print(f"\nR2 Score: {r2}")

print("Finished")
#mt5.shutdown
print("##################################################################")

