diff --git a/Experts/MAGrid.ex5 b/Experts/MAGrid.ex5 index 1b4cef6..42f8cbf 100644 Binary files a/Experts/MAGrid.ex5 and b/Experts/MAGrid.ex5 differ diff --git a/Experts/MAGrid.mq5 b/Experts/MAGrid.mq5 index 377b27a..6e0b26c 100644 --- a/Experts/MAGrid.mq5 +++ b/Experts/MAGrid.mq5 @@ -9,7 +9,7 @@ // Moving Average grid strategy /* -Set pending orders x poinst above and below price. +Set pending orders x point above and below price. If price above SMA, buy and set buy orders x time the ATR above and below price. If price below SMA, sell and set sell orders x time the ATR above and below price. Close all position at the close of the first candle crossing the moving average. @@ -25,13 +25,13 @@ CiATR* atr; //#include // Error library //#include // Prechecks //#include // -//#include +#include #include // Scan for opened positions //#include //Check transaction history //#include //Manage trade dynamic open and close conditions #include // Check buy and sell entries signals and execute them #include // Lot size calculate -//#include // Close opened positions +#include // Close opened positions //+------------------------------------------------------------------+ @@ -63,37 +63,86 @@ void OnTick() { //--- SymbolInfoTick(_Symbol,last_tick); + +//Get technical indicators values ma.Refresh(-1); gMa = ma.Main(1); atr.Refresh(-1); gAtr = atr.Main(1); + +//Initial position scanning ScanPositions(); - - Print("Total transaction ", gTotalTransactions); - if(gTotalTransactions>0) - return; - CheckSpread(); - EvaluateEntry(); - ExecuteEntry(); - } -//+------------------------------------------------------------------+ + Print("Price is below SMA. Price = ", iClose(gSymbol, PERIOD_CURRENT, 1), " SMA = ", gMa, " Total buy ", gTotalBuyPositions); -//Check and return if the spread is not too high -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 - long SpreadCurr=SymbolInfoInteger(gSymbol, SYMBOL_SPREAD); - Print("Spread ", SpreadCurr); - if(SpreadCurr<=InpMaxSpread) +//Check closing signal + +//Close all buy position and orders if price is below MA + if(iClose(gSymbol, PERIOD_CURRENT, 1) < gMa && gTotalTransactions > 0) { - gIsSpreadOK=true; + Print("Price is below SMA. Price = ", iClose(gSymbol, PERIOD_CURRENT, 1), " SMA = ", gMa); + CloseTransactions(SIGNAL_EXIT_BUY); } else { - gIsSpreadOK=false; + //Close all sell positions and orders if price is above MA + if(iClose(gSymbol, PERIOD_CURRENT, 1) > gMa && gTotalTransactions > 0) + { + Print("Price is above SMA. Price = ", iClose(gSymbol, PERIOD_CURRENT, 1), " SMA = ", gMa); + CloseTransactions(SIGNAL_EXIT_SELL); + } + } + +//Rescan positions + ScanPositions(); + + Print("Total transaction ", gTotalTransactions, " gTotalBuyPositions ", gTotalBuyPositions); +//Do not open positions if there are positions or orders pending + if(gTotalTransactions>0) + { + //If there's no position, close all pending orders + if(gTotalBuyPositions == 0 && gTotalTransactions > 0) + { + Print("Delete all"); + CloseTransactions(SIGNAL_EXIT_ALL); + } + else + { + if(gTotalSellPositions==0 && gTotalTransactions >0) + { + CloseTransactions(SIGNAL_EXIT_ALL); + } + else + { + return; + } + } + } +//+------------------------------------------------------------------+ +//| | +//+------------------------------------------------------------------+ + CheckSpread(); + EvaluateEntry(); + ExecuteEntry(); +} +//+------------------------------------------------------------------+ + +//Check and return if the spread is not too high + 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 + long SpreadCurr=SymbolInfoInteger(gSymbol, SYMBOL_SPREAD); + Print("Spread ", SpreadCurr); + if(SpreadCurr<=InpMaxSpread) + { + gIsSpreadOK=true; + } + else + { + gIsSpreadOK=false; + } } - } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ + diff --git a/Include/Nkanven/MAGrid/ClosePositions.mqh b/Include/Nkanven/MAGrid/ClosePositions.mqh deleted file mode 100644 index 219a413..0000000 --- a/Include/Nkanven/MAGrid/ClosePositions.mqh +++ /dev/null @@ -1,38 +0,0 @@ -//+------------------------------------------------------------------+ -//| ClosePositions.mqh | -//| Copyright 2021, Nkondog Anselme Venceslas | -//| https://www.mql5.com | -//+------------------------------------------------------------------+ -#property copyright "Copyright 2021, Nkondog Anselme Venceslas" -#property link "https://www.mql5.com" - -CTrade trade; - -//+------------------------------------------------------------------+ -//| | -//+------------------------------------------------------------------+ -bool OrderClose() - { - bool result = true; - int cnt = OrdersTotal(); - if(cnt == 1) - { - - - for(int i = cnt-1; i>=0; i--) - { - ulong ticket = OrderGetTicket(i); - if(OrderSelect(ticket)) - { - - result &= trade.OrderDelete(ticket); - } - else - { - result = false; - } - } - } - return(result); - } -//+------------------------------------------------------------------+ diff --git a/Include/Nkanven/MAGrid/EntriesManager.mqh b/Include/Nkanven/MAGrid/EntriesManager.mqh index 52f6a1c..2288fa9 100644 Binary files a/Include/Nkanven/MAGrid/EntriesManager.mqh and b/Include/Nkanven/MAGrid/EntriesManager.mqh differ diff --git a/Include/Nkanven/MAGrid/LotSizeCal.mqh b/Include/Nkanven/MAGrid/LotSizeCal.mqh index 4d4ba0f..5ff8fc3 100644 --- a/Include/Nkanven/MAGrid/LotSizeCal.mqh +++ b/Include/Nkanven/MAGrid/LotSizeCal.mqh @@ -17,16 +17,15 @@ void LotSizeCalculate(double SL=0) if(SL!=0) { double RiskBaseAmount=0; - double RiskBase=0; //TickValue is the value of the individual price increment for 1 lot of the instrument, expressed in the account currenty double TickValue=SymbolInfoDouble(gSymbol,SYMBOL_TRADE_TICK_VALUE); //Define the base for the risk calculation depending on the parameter chosen - if(RiskBase==RISK_BASE_BALANCE) + if(InpRiskBase==RISK_BASE_BALANCE) RiskBaseAmount=AccountInfoDouble(ACCOUNT_BALANCE); - if(RiskBase==RISK_BASE_EQUITY) + if(InpRiskBase==RISK_BASE_EQUITY) RiskBaseAmount=AccountInfoDouble(ACCOUNT_EQUITY); - if(RiskBase==RISK_BASE_FREEMARGIN) + if(InpRiskBase==RISK_BASE_FREEMARGIN) RiskBaseAmount=AccountInfoDouble(ACCOUNT_FREEMARGIN); //Calculate the Position Size diff --git a/Include/Nkanven/MAGrid/Parameters.mqh b/Include/Nkanven/MAGrid/Parameters.mqh index 55a90e6..8c7333d 100644 --- a/Include/Nkanven/MAGrid/Parameters.mqh +++ b/Include/Nkanven/MAGrid/Parameters.mqh @@ -93,15 +93,15 @@ struct LastTransaction // Input Section // // Fast moving average -input int InpFastPeriods = 10; // Fast periods +input int InpFastPeriods = 200; // Fast periods input ENUM_MA_METHOD InpFastMethod = MODE_SMA; // Fast method input ENUM_APPLIED_PRICE InpFastAppliedPrice = PRICE_CLOSE; // Fast price -// Slow moving average +/* Slow moving average 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 diff --git a/Include/Nkanven/MAGrid/ScanPositions.mqh b/Include/Nkanven/MAGrid/ScanPositions.mqh index 20ce060..dd7052e 100644 --- a/Include/Nkanven/MAGrid/ScanPositions.mqh +++ b/Include/Nkanven/MAGrid/ScanPositions.mqh @@ -41,7 +41,7 @@ bool ScanPositions() //If it is a sell order then increment the total count of sell orders if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL) gTotalSellPositions++; - +Print("POSITION_TYPE_BUY ", POSITION_TYPE_BUY, " POSITION_TYPE_SELL ", POSITION_TYPE_SELL, " PositionGetInteger(POSITION_TYPE) ", PositionGetInteger(POSITION_TYPE)); //Find what is the open time of the most recent trade and assign it to LastBarTraded //this is necessary to check if we already traded in the current candle if((datetime)PositionGetInteger(POSITION_TIME)>gLastBarTraded || gLastBarTraded==NULL)