Why a MQL4 bot does not detect a 5-EMA & 20-EMA cross-over in a StrategyTester run?

Why a MQL4 bot does not detect a 5-EMA & 20-EMA cross-over in a StrategyTester run?

Manage alerts

Loading saved threads...

Bill Mayheptad Ritchie · External communityPost link
External question — Stack Overflow Stack Exchange Author: Bill Mayheptad Ritchie Original post: https://stackoverflow.com/questions/65201216 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. I am a newbie in coding bot for MetaTrader Terminal 4 platform, but I have a decent experience in coding JavaScript for the web. I just build a simple MQL4 bot, using EMA(5) and EMA(20) to place an order, but when I try to test it via a StrategyTester run, it keeps showing both condition not met, please what am I doing wrong? Below is my code ( https://codeshare.io/5NzvYl ) input int fastEma_Period = 5; input int fastEma_ma_Shift = 0; input int fastEma_ma_Method = 1; // EXPONENTIAL input int fastEma_applied_Price = 0; // CLOSED PRICE input int fastEma_shift_CandleIndex = 0; // CURRENT FORMING CANDLESTICK input int slowEma_Period = 20; input int slowEma_ma_Shift = 0; input int slowEma_ma_Method = 1; // EXPONENTIAL input int slowEma_applied_Price = 0; // CLOSED PRICE input int slowEma_shift_CandleIndex = 0; // CURRENT FORMING CANDLESTICK //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ double fastEmaBar0 = iMA(NULL,0,fastEma_Period,fastEma_ma_Shift,fastEma_ma_Method,fastEma_applied_Price,fastEma_shift_CandleIndex); double slowEmaBar0 = iMA(NULL,0,slowEma_Period,slowEma_ma_Shift,slowEma_ma_Method,slowEma_applied_Price,slowEma_shift_CandleIndex); //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ double fastEmaBar1 = iMA(NULL,0,fastEma_Period,fastEma_ma_Shift,fastEma_ma_Method,fastEma_applied_Price,fastEma_shift_CandleIndex + 1); double slowEmaBar1 = iMA(NULL,0,slowEma_Period,slowEma_ma_Shift,slowEma_ma_Method,slowEma_applied_Price,slowEma_shift_CandleIndex + 1); double lotSize = AccountBalance() / 10000; int maBt; int maSt; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- enterMarket(); // ExpertRemove(); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ string maCrossOverSignal(double fastEmaBar1, double slowEmaBar1, double fastEmaBar0, double slowEmaBar0) { string signal; if((fastEmaBar1 <= slowEmaBar1) && (fastEmaBar0 > slowEmaBar0)) { signal = "Buy"; } else if((fastEmaBar1 >= slowEmaBar1) && (fastEmaBar0 < slowEmaBar0)) { signal = "Sell"; } return signal; } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void enterMarket() { if(/*OrdersTotal() == 0 &&*/ maCrossOverSignal(fastEmaBar1,slowEmaBar1,fastEmaBar0,slowEmaBar0)== "Buy") { //continue inside this condition, bcos its not getting met Print("time to Buy "); maBt = OrderSend(NULL,OP_BUY,lotSize,Ask,100,0,0,"maBt"); if(maBt > 0) { if(OrderSelect(maBt,SELECT_BY_TICKET)) { modify(maBt,OrderOpenPrice(),OrderOpenPrice() - 50 * Point,OrderOpenPrice() + 200 * Point,0); } } } else if(/*OrdersTotal() == 0 && */maCrossOverSignal(fastEmaBar1,slowEmaBar1,fastEmaBar0,slowEmaBar0)== "Sell") { Print("time to Sell "); maSt = OrderSend(NULL,OP_SELL,lotSize,Bid,100,0,0,"maSt"); if(maSt > 0) { if(OrderSelect(maSt,SELECT_BY_TICKET)) { modify(maSt,OrderOpenPrice(),OrderOpenPrice() + 50 * Point,OrderOpenPrice() - 200 * Point,0); } } } else { Print("both condition not met"); } }
Quote
Report
user3666197 · External communityPost link
External answer — Stack Overflow Stack Exchange Author: user3666197 Original post: https://stackoverflow.com/a/65205816 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. Q : "... what am I doing wrong?" Almost there. You've missed a tiny, yet cardinal detail. The both assignments of iMA() -values are fine, but in a wrong place. You need to let re-calculate & re-assign iMA() -values after each QUOTE -message arrival ( per each tick, coming from your FX-Market access mediator ( emulated in StrategyTester runs from past-data, sure ) ). void OnTick() { /*\/\/\/\/\/\/\/\/\ QUOTE-has arrived, i.e. new prices are known here: //------------------------------------------- UPDATE each control-DATA now */ fastEmaBar0 = iMA( NULL, 0, fastEma_Period, fastEma_ma_Shift, fastEma_ma_Method, fastEma_applied_Price, fastEma_shift_CandleIndex ); slowEmaBar0 = iMA( NULL, 0, slowEma_Period, slowEma_ma_Shift, slowEma_ma_Method, slowEma_applied_Price, slowEma_shift_CandleIndex ); fastEmaBar1 = iMA( NULL, 0, fastEma_Period, fastEma_ma_Shift, fastEma_ma_Method, fastEma_applied_Price, fastEma_shift_CandleIndex + 1 ); slowEmaBar1 = iMA( NULL, 0, slowEma_Period, slowEma_ma_Shift, slowEma_ma_Method, slowEma_applied_Price, slowEma_shift_CandleIndex + 1 ); //------------------------------------------- UPDATE //------------------------------------------- DONE, may start making decisions enterMarket(); // ExpertRemove(); }
Quote
Report

Post Reply

Checking account access…