Watch
1
0
Fork
You've already forked NeuroBook
0
forked from rosh/NeuroBook
NeuroBook/Experts/ea_template.mq5

218 lines
15 KiB
MQL5

2025-05-30 16:12:30 +02:00
<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| ea_template.mq5 |
//| Copyright 2021, MetaQuotes Ltd. |
//| https://www.mql5.com |
//| Example of Expert Advisor to use with neural network models |
//| from book "Neural Networks for algorithmic trading in MQL5" |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.00"
//--- input parameters
sinput string Model = "our_model.net"; //"our_model.net";
sinput int BarsToPattern = 3;
sinput bool Common = true;
input ENUM_TIMEFRAMES TimeFrame = PERIOD_H1;
input double TradeLevel = 1; //0.9;
input double Lot = 0.01;
input int MaxTP = 500;
input double ProfitMultiply = 0.8;
input int MinTarget = 100;
input int StopLoss = 300;
sinput bool UseOpenCL = true;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
#include "..\Include\realization\neuronnet.mqh"
#include <Trade\Trade.mqh>
CNet *net;
CTrade *trade;
datetime lastbar = 0;
int h_RSI;\
int h_MACD;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
if(!(net = new CNet()))
{
PrintFormat("Error of create Net: %d", GetLastError());
return INIT_FAILED;
}
if(!net.Load(Model, Common))
{
PrintFormat("Error of load mode %s: %d", Model, GetLastError());
return INIT_FAILED;
}
net.UseOpenCL(UseOpenCL);
//---
h_RSI = iRSI(_Symbol, TimeFrame, 12, PRICE_TYPICAL);
if(h_RSI == INVALID_HANDLE)
{
PrintFormat("Error of load indicator %s", "RSI");
return INIT_FAILED;
}
h_MACD = iMACD(_Symbol, TimeFrame, 12, 48, 12, PRICE_TYPICAL);
if(h_MACD == INVALID_HANDLE)
{
PrintFormat("Error of load indicator %s", "MACD");
return INIT_FAILED;
}
//---
if(!(trade = new CTrade()))
{
PrintFormat("Error of create CTrade: %d", GetLastError());
return INIT_FAILED;
}
if(!trade.SetTypeFillingBySymbol(_Symbol))
return INIT_FAILED;
//---
lastbar = TimeCurrent();
PrintFormat("khalil OK");
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
if(!!net)
delete net;
if(!!trade)
delete trade;
IndicatorRelease(h_RSI);
IndicatorRelease(h_MACD);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
if(lastbar >= iTime(_Symbol, TimeFrame, 0))
return;
lastbar = iTime(_Symbol, TimeFrame, 0);
//---
double macd_main[], macd_signal[], rsi[];
if(h_RSI == INVALID_HANDLE || CopyBuffer(h_RSI, 0, 1, BarsToPattern, rsi) <= 0)
{
PrintFormat("Error loading indicator %s data", "RSI");
return;
}
if(h_MACD == INVALID_HANDLE || CopyBuffer(h_MACD, MAIN_LINE, 1, BarsToPattern, macd_main) <= 0 ||
CopyBuffer(h_MACD, SIGNAL_LINE, 1, BarsToPattern, macd_signal) <= 0)
{
PrintFormat("Error loading indicator %s data", "MACD");
return;