diff --git a/Experts/Nkanven/GDeaLite.ex5 b/Experts/Nkanven/GDeaLite.ex5 index 863557e..c9fd358 100644 Binary files a/Experts/Nkanven/GDeaLite.ex5 and b/Experts/Nkanven/GDeaLite.ex5 differ diff --git a/Experts/Nkanven/GDeaLite.mq5 b/Experts/Nkanven/GDeaLite.mq5 index 4cdf40f..1346f33 100644 --- a/Experts/Nkanven/GDeaLite.mq5 +++ b/Experts/Nkanven/GDeaLite.mq5 @@ -7,6 +7,14 @@ #property link "https://www.mql5.com" #property version "1.00" +#include +#include + +CiMA* fsma; +CiMA* ssma; + +CiATR* atr; + #include // Description of variables #include // Error library #include // Prechecks @@ -24,7 +32,14 @@ int OnInit() { //--- - + fsma = new CiMA(); + ssma = new CiMA(); + + fsma.Create(gSymbol, PERIOD_CURRENT, InpFastPeriods, InpFastAppliedPrice, InpFastMethod, PRICE_CLOSE); + ssma.Create(gSymbol, PERIOD_CURRENT, InpSlowPeriods, InpFastAppliedPrice, InpSlowMethod, PRICE_CLOSE); + + atr = new CiATR(); + atr.Create(gSymbol, PERIOD_CURRENT, InpAtrPeriod); //--- return(INIT_SUCCEEDED); } @@ -34,7 +49,7 @@ int OnInit() void OnDeinit(const int reason) { //--- - + } //+------------------------------------------------------------------+ //| Expert tick function | @@ -42,26 +57,26 @@ void OnDeinit(const int reason) void OnTick() { //--- - + } //+------------------------------------------------------------------+ //Initialize variables void InitializeVariables() { - IsNewCandle=false; - IsTradedThisBar=false; - IsOperatingHours=false; - IsSpreadOK=false; + gIsNewCandle=false; + gIsTradedThisBar=false; + gIsOperatingHours=false; + gIsSpreadOK=false; - LotSize=DefaultLotSize; - TickValue=0; + gLotSize=InpDefaultLotSize; + gTickValue=0; - TotalOpenBuy=0; - TotalOpenSell=0; + gTotalOpenBuy=0; + gTotalOpenSell=0; - SignalEntry=SIGNAL_ENTRY_NEUTRAL; - SignalExit=SIGNAL_EXIT_NEUTRAL; + gSignalEntry=SIGNAL_ENTRY_NEUTRAL; + gSignalExit=SIGNAL_EXIT_NEUTRAL; Print("Variables intialized"); } @@ -69,15 +84,15 @@ void InitializeVariables() void CheckSpread() { //Get the current spread in points, the (int) transforms the double coming from MarketInfo into an integer to avoid a warning when compiling - double SpreadCurr=SymbolInfoInteger(mSymbol, SYMBOL_SPREAD); + long SpreadCurr=SymbolInfoInteger(gSymbol, SYMBOL_SPREAD); Print("Spread ", SpreadCurr); - if(SpreadCurr<=MaxSpread) + if(SpreadCurr<=InpMaxSpread) { - IsSpreadOK=true; + gIsSpreadOK=true; } else { - IsSpreadOK=false; + gIsSpreadOK=false; } } //+------------------------------------------------------------------+ diff --git a/Include/Nkanven/GDea/Parameters.mqh b/Include/Nkanven/GDea/Parameters.mqh index d2b8ef1..c0d3ccc 100644 --- a/Include/Nkanven/GDea/Parameters.mqh +++ b/Include/Nkanven/GDea/Parameters.mqh @@ -103,19 +103,31 @@ input int InpSlowPeriods = 20; // Slow periods input ENUM_MA_METHOD InpSlowMethod = MODE_SMA; // Slow method input ENUM_APPLIED_PRICE InpSlowAppliedPrice = PRICE_CLOSE; // Slow price +input int InpAtrPeriod = 14; // ATR period // Bar numbers for comparison //input int InpBar2 = 2; // Base bar number //input int InpBar1 = 1; // Crossover bar number input string Comment_0="=========="; //Risk Management Settings -input ENUM_RISK_DEFAULT_SIZE RiskDefaultSize=RISK_DEFAULT_AUTO; //Position Size Mode -input double DefaultLotSize=1; //Position Size (if fixed or if no stop loss defined) -input ENUM_RISK_BASE RiskBase=RISK_BASE_BALANCE; //Risk Base -input double MaxRiskPerTrade=0.5; //Percentage To Risk Each Trade -input double MinLotSize=0.01; //Minimum Position Size Allowed -input double MaxLotSize=100; //Maximum Position Size Allowed -input double MaxSpread=10.0; //Maximum Spread Allowed -input int Slippage=5; //Maximum Slippage Allowed in points +input ENUM_RISK_DEFAULT_SIZE InpRiskDefaultSize=RISK_DEFAULT_AUTO; //Position Size Mode +input double InpDefaultLotSize=1; //Position Size (if fixed or if no stop loss defined) +input ENUM_RISK_BASE InpRiskBase=RISK_BASE_BALANCE; //Risk Base +input double InpMaxRiskPerTrade=0.5; //Percentage To Risk Each Trade +input double InpMinLotSize=0.01; //Minimum Position Size Allowed +input double InpMaxLotSize=100; //Maximum Position Size Allowed +input double InpMaxSpread=10.0; //Maximum Spread Allowed +input int InpSlippage=5; //Maximum Slippage Allowed in points +input ENUM_MODE_SL InpStopLossMode=SL_AUTO; //Stop Loss Mode +input int InpDefaultStopLoss=0; //Default Stop Loss In Points (0=No Stop Loss) +input int InpMinStopLoss=0; //Minimum Allowed Stop Loss In Points +input int InpMaxStopLoss=5000; //Maximum Allowed Stop Loss In Points +input bool InpAtrStopLoss=false; //Set Stop loss based on ATR +input int InpAtrStopLossFactor=3; //Multiplicator for ATR stop loss +input ENUM_MODE_TP InpTakeProfitMode=TP_AUTO; //Take Profit Mode +input int InpDefaultTakeProfit=0; //Default Take Profit In Points (0=No Take Profit) +input int InpMinTakeProfit=0; //Minimum Allowed Take Profit In Points +input int InpMaxTakeProfit=5000; //Maximum Allowed Take Profit In Points +input double InpTakeProfitPercent=1.0; //Take Profit percent on risk base // Trading time input int InStartHour = 12; // Trading starting hour @@ -131,20 +143,32 @@ input double InpVolume = 0.01; // Default order size input string InpComment = __FILE__; // Default trade comment input int InpMagicNumber = 198901; // Magic Number +input string Comment_1="=========="; //Trading Hours Settings +input bool InpUseTradingHours=false; //Limit Trading Hours +input string InpTradingHourStart="01"; //Trading Start Hour (Broker Server Hour) +input string InpTradingHourEnd="23"; //Trading End Hour (Broker Server Hour) +input string InpTradingStartMin="30"; //Trading Start minute (Broker Server Hour) +input string InpTradingEndMin="00"; //Trading End minute -bool IsNewCandle=false; -bool IsTradedThisBar=false; bool IsOperatingHours=false; -bool IsSpreadOK=false; +bool gIsNewCandle=false; +bool gIsTradedThisBar=false; +bool gIsOperatingHours=false; +bool gIsPreChecksOk=false; //Indicates if the pre checks are satisfied +bool gIsSpreadOK=false; //Indicates if the spread is low enough to trade -double LotSize=DefaultLotSize; -int TickValue=0; +double gLotSize=InpDefaultLotSize; +int gTickValue=0; -int TotalOpenBuy=0; -int TotalOpenSell=0; -string mSymbol = Symbol(); +int gTotalOpenBuy=0; +int gTotalOpenSell=0; +int gTotalOpenOrders=0; +string gSymbol = Symbol(); + +datetime gLastBarTraded=NULL; MqlTick last_tick; +MqlDateTime dt; -ENUM_SIGNAL_ENTRY SignalEntry=SIGNAL_ENTRY_NEUTRAL; //Entry signal variable -ENUM_SIGNAL_EXIT SignalExit=SIGNAL_EXIT_NEUTRAL; \ No newline at end of file +ENUM_SIGNAL_ENTRY gSignalEntry=SIGNAL_ENTRY_NEUTRAL; //Entry signal variable +ENUM_SIGNAL_EXIT gSignalExit=SIGNAL_EXIT_NEUTRAL; \ No newline at end of file diff --git a/Include/Nkanven/GDea/Prechecks.mqh b/Include/Nkanven/GDea/Prechecks.mqh index aedb074..abe6850 100644 --- a/Include/Nkanven/GDea/Prechecks.mqh +++ b/Include/Nkanven/GDea/Prechecks.mqh @@ -5,23 +5,58 @@ //+------------------------------------------------------------------+ #property copyright "Copyright 2021, Nkondog Anselme Venceslas" #property link "https://www.mql5.com" -//+------------------------------------------------------------------+ -//| defines | -//+------------------------------------------------------------------+ -// #define MacrosHello "Hello, world!" -// #define MacrosYear 2010 -//+------------------------------------------------------------------+ -//| DLL imports | -//+------------------------------------------------------------------+ -// #import "user32.dll" -// int SendMessageA(int hWnd,int Msg,int wParam,int lParam); -// #import "my_expert.dll" -// int ExpertRecalculate(int wParam,int lParam); -// #import -//+------------------------------------------------------------------+ -//| EX5 imports | -//+------------------------------------------------------------------+ -// #import "stdlib.ex5" -// string ErrorDescription(int error_code); -// #import -//+------------------------------------------------------------------+ + +//Perform integrity checks when the EA is loaded +void CheckPreChecks() + { + gIsPreChecksOk=true; +//Check if Live Trading is enabled in MT4 + if(!MQLInfoInteger(MQL_TRADE_ALLOWED)) + { + gIsPreChecksOk=false; + Print("Live Trading is not enabled, please enable it in MT4 and chart settings"); + return; + } +//Check if the default stop loss you are setting in above the minimum and below the maximum + if(InpDefaultStopLossInpMaxStopLoss) + { + gIsPreChecksOk=false; + Print("Default Stop Loss must be between Minimum and Maximum Stop Loss Allowed"); + return; + } +//Check if the default take profit you are setting in above the minimum and below the maximum + if(InpDefaultTakeProfitInpMaxTakeProfit) + { + gIsPreChecksOk=false; + Print("Default Take Profit must be between Minimum and Maximum Take Profit Allowed"); + return; + } +//Check if the Lot Size is between the minimum and maximum + if(InpDefaultLotSizeInpMaxLotSize) + { + gIsPreChecksOk=false; + Print("Default Lot Size must be between Minimum and Maximum Lot Size Allowed"); + return; + } +//Slippage must be >= 0 + if(InpSlippage<0) + { + gIsPreChecksOk=false; + Print("Slippage must be a positive value"); + return; + } +//MaxSpread must be >= 0 + if(InpMaxSpread<0) + { + gIsPreChecksOk=false; + Print("Maximum Spread must be a positive value"); + return; + } +//MaxRiskPerTrade is a % between 0 and 100 + if(InpMaxRiskPerTrade<0 || InpMaxRiskPerTrade>100) + { + gIsPreChecksOk=false; + Print("Maximum Risk Per Trade must be a percentage between 0 and 100"); + return; + } + } diff --git a/Include/Nkanven/GDea/ScanPositions.mqh b/Include/Nkanven/GDea/ScanPositions.mqh index 061ea76..7adfdb0 100644 --- a/Include/Nkanven/GDea/ScanPositions.mqh +++ b/Include/Nkanven/GDea/ScanPositions.mqh @@ -5,23 +5,45 @@ //+------------------------------------------------------------------+ #property copyright "Copyright 2021, Nkondog Anselme Venceslas" #property link "https://www.mql5.com" -//+------------------------------------------------------------------+ -//| defines | -//+------------------------------------------------------------------+ -// #define MacrosHello "Hello, world!" -// #define MacrosYear 2010 -//+------------------------------------------------------------------+ -//| DLL imports | -//+------------------------------------------------------------------+ -// #import "user32.dll" -// int SendMessageA(int hWnd,int Msg,int wParam,int lParam); -// #import "my_expert.dll" -// int ExpertRecalculate(int wParam,int lParam); -// #import -//+------------------------------------------------------------------+ -//| EX5 imports | -//+------------------------------------------------------------------+ -// #import "stdlib.ex5" -// string ErrorDescription(int error_code); -// #import -//+------------------------------------------------------------------+ + +//Scan all positions to find the ones submitted by the EA +//NOTE This function is defined as bool because we want to return true if it is successful and false if it fails +bool ScanPositions() + { + +//Scan all the orders, retrieving some of the details + gTotalOpenOrders = 0; + gTotalOpenBuy = 0; + gTotalOpenSell = 0; + for(int i=0; igLastBarTraded || gLastBarTraded==NULL) + gLastBarTraded=(datetime)PositionGetInteger(POSITION_TIME); + } + Print("Total positions ", gTotalOpenOrders, " - Total buys ", gTotalOpenBuy, " - Total sells ", gTotalOpenSell); + return true; + } \ No newline at end of file diff --git a/Include/Nkanven/GDea/TradingHour.mqh b/Include/Nkanven/GDea/TradingHour.mqh index db311c7..7933ad8 100644 --- a/Include/Nkanven/GDea/TradingHour.mqh +++ b/Include/Nkanven/GDea/TradingHour.mqh @@ -5,23 +5,41 @@ //+------------------------------------------------------------------+ #property copyright "Copyright 2021, Nkondog Anselme Venceslas" #property link "https://www.mql5.com" -//+------------------------------------------------------------------+ -//| defines | -//+------------------------------------------------------------------+ -// #define MacrosHello "Hello, world!" -// #define MacrosYear 2010 -//+------------------------------------------------------------------+ -//| DLL imports | -//+------------------------------------------------------------------+ -// #import "user32.dll" -// int SendMessageA(int hWnd,int Msg,int wParam,int lParam); -// #import "my_expert.dll" -// int ExpertRecalculate(int wParam,int lParam); -// #import -//+------------------------------------------------------------------+ -//| EX5 imports | -//+------------------------------------------------------------------+ -// #import "stdlib.ex5" -// string ErrorDescription(int error_code); -// #import + +//Check and return if it is operation hours or not +void CheckOperationHours() + { +//If we are not using operating hours then IsOperatingHours is true and I skip the other checks + if(!InpUseTradingHours) + { + gIsOperatingHours=true; + return; + } +//Check if the current hour is between the allowed hours of operations, if so IsOperatingHours is set true + Print("1 this is ", (InpTradingHourStart==InpTradingHourEnd && dt.hour==InpTradingHourStart)); + + if(InpTradingHourStart==InpTradingHourEnd && dt.hour==InpTradingHourStart) + { + gIsOperatingHours=true; + return; + } + + if(InpTradingHourStart= InpTradingStartMin) + { + gIsOperatingHours=true; + return; + } + if(dt.hour > InpTradingHourStart) + { + gIsOperatingHours=true; + } + } + + if(InpTradingHourStart>InpTradingHourEnd && ((dt.hour>=InpTradingHourStart && dt.hour<=23) || (dt.hour<=InpTradingHourEnd && dt.hour>=0))) + { + gIsOperatingHours=true; + } + } //+------------------------------------------------------------------+