pls i will be glad if anyone can help. The EA places a buy order when its suppose to place a sell, and places a sell order when its suppose to place a buy order.
Code:
//+------------------------------------------------------------------+ //| Check for open position conditions | //+------------------------------------------------------------------+ void CheckForOpen(void) { MqlRates rt[2]; ArraySetAsSeries(rt,true); //--- go trading only for the first few ticks of new bar if(CopyRates(_Symbol,PERIOD_CURRENT,0,2,rt)!=2) { Print("CopyRates of ",_Symbol," failed. No history"); return; } if(rt[0].tick_volume>1) return; //--- get fast current moving average double fma[2]; ArraySetAsSeries(fma,true); if(CopyBuffer(FastExtHandle,0,0,2,fma)!=2) { Print("CopyBuffer from fast iMA failed, no data"); return; } //--- get slow current moving average double sma[2]; ArraySetAsSeries(sma,true); if(CopyBuffer(SlowExtHandle,0,0,2,sma)!=2) { Print("CopyBuffer from slow iMA failed, no data"); return; } //--- check for signals double StateMA1=fma[1]-sma[1], StateMA0=fma[0]-sma[0]; ENUM_ORDER_TYPE signal=WRONG_VALUE; if(StateMA1<0.0 && StateMA0>=0.0) signal=ORDER_TYPE_BUY; else { if(StateMA1>0.0 && StateMA0<=0.0) signal=ORDER_TYPE_SELL; } //--- additional checking if(signal!=WRONG_VALUE) { if(TerminalInfoInteger(TERMINAL_TRADE_ALLOWED) && Bars(_Symbol,PERIOD_CURRENT)>100) ExtTrade.PositionOpen(_Symbol,signal,1.0, SymbolInfoDouble(_Symbol,signal==ORDER_TYPE_SELL ? SYMBOL_BID:SYMBOL_ASK) ,0,0); }; }
Code:
ok. thanks vladmir. But the strategy does not place any trades now after correcting my code with yours. I will be glad and appreciate if you help me out
ok. thanks vladmir. But the strategy does not place any trades now after correcting my code with yours. I will be glad and appreciate if you help me out
To determine the event: "a new bar has appeared", you need to remember and compare the bar time
Corrected code
//+------------------------------------------------------------------+ //| NLT2.mq5 | //| Copyright 2020, MetaQuotes Software Corp. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2020, MetaQuotes Software Corp." #property link "https://www.mql5.com" #property version "1.00" #include <Trade\Trade.mqh> //--- input parameters input int fastMAPeriod=10; input int fastMAShift=1; input int slowMAPeriod=21; input int slowMAShift=1; //--- int FastExtHandle=0, SlowExtHandle=0; CTrade ExtTrade; //+------------------------------------------------------------------+ //| Check for open position conditions | //+------------------------------------------------------------------+ void CheckForOpen(void) { MqlRates rt[]; ArraySetAsSeries(rt,true); //--- go trading only for the first few ticks of new bar if(CopyRates(_Symbol,PERIOD_CURRENT,0,2,rt)!=2) { Print("CopyRates of ",_Symbol," failed. No history"); return; } if(rt[0].tick_volume>1) return; //--- get fast current moving average double fma[]; ArraySetAsSeries(fma,true); if(CopyBuffer(FastExtHandle,0,0,2,fma)!=2) { Print("CopyBuffer from fast iMA failed, no data"); return; } //--- get slow current moving average double sma[]; ArraySetAsSeries(sma,true); if(CopyBuffer(SlowExtHandle,0,0,2,sma)!=2) { Print("CopyBuffer from slow iMA failed, no data"); return; } //--- check for signals double StateMA1=fma[1]-sma[1], StateMA0=fma[0]-sma[0]; ENUM_ORDER_TYPE signal=WRONG_VALUE; if(StateMA1<0.0 && StateMA0>=0.0) signal=ORDER_TYPE_BUY; else { if(StateMA1>0.0 && StateMA0<=0.0) signal=ORDER_TYPE_SELL; } //--- additional checking if(signal!=WRONG_VALUE) { if(TerminalInfoInteger(TERMINAL_TRADE_ALLOWED) && Bars(_Symbol,PERIOD_CURRENT)>100) ExtTrade.PositionOpen(_Symbol,signal,1.0, SymbolInfoDouble(_Symbol,signal==ORDER_TYPE_SELL ? SYMBOL_BID:SYMBOL_ASK) ,0,0); }; } //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- Fast Moving Average Indicator FastExtHandle=iMA(_Symbol,PERIOD_CURRENT,fastMAPeriod,fastMAShift,MODE_SMA,PRICE_CLOSE); if(FastExtHandle==INVALID_HANDLE) { printf("Error creating Fast MA indicator handle"); return(INIT_FAILED); } //--- Slow Moving Average Indicator SlowExtHandle=iMA(_Symbol,PERIOD_CURRENT,slowMAPeriod,slowMAShift,MODE_SMA,PRICE_CLOSE); if(SlowExtHandle==INVALID_HANDLE) { printf("Error creating Slow MA indicator handle"); return(INIT_FAILED); } //---ok return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- CheckForOpen(); } //+------------------------------------------------------------------+
and the result is:
Corrected code
and the result is:
thanks ***. The code now works as it is inended to. I appreciate man
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
pls i will be glad if anyone can help. The EA places a buy order when its suppose to place a sell, and places a sell order when its suppose to place a buy order.