Automatically New Trade Order Executes.

 

I had created a simple crossover strategy for my expert advisor, the problem I'm facing is that a new trade order executes after fulfiling a take profit or stop loss criteria.

As an example I had created 50EMA and 200 EMA crossover strategy, as soon as the 50 EMA crosses above the 200 EMA placing a buy order with 15 pips take profit with a 15 pip stop loss.

Now the trade executes with the current parameters, after some time take profit gets filled. But 50 EMA is still above 200 EMA and another buy order executes later then after some time

the crossover happens 50 EMA slides below 200 EMA and then my stop loss triggers.

I need some solution to fix this problem, can you assist me with some codes. How to place a Single Buy or Sell order Only on Crossover even if the fast 50 EMA holds up above the slow 200EMA or

vice-versa.

 
wisewk:I had created a simple crossover strategy for my expert advisor, the problem I'm facing is that a new trade order executes after fulfiling a take profit or stop loss criteria. As an example I had created 50EMA and 200 EMA crossover strategy, as soon as the 50 EMA crosses above the 200 EMA placing a buy order with 15 pips take profit with a 15 pip stop loss. Now the trade executes with the current parameters, after some time take profit gets filled. But 50 EMA is still above 200 EMA and another buy order executes later then after some time the crossover happens 50 EMA slides below 200 EMA and then my stop loss triggers. I need some solution to fix this problem, can you assist me with some codes. How to place a Single Buy or Sell order Only on Crossover even if the fast 50 EMA holds up above the slow 200EMA or vice-versa.

Don't look for when the EMA(50) is above the EMA(200). Instead, look for the change in the signal when it takes place — ONLY when the cross-over occurs, when it changes from below to above (and vice-versa).

Also, show your code if you need help.

 

You are looking at a signal. Act on a change of signal.
          Too many orders - MQL4 programming forum #1 (2017)
          MQL4 (in Strategy Tester) - double testing of entry conditions - MQL4 programming forum #1 (2017)

 
//+------------------------------------------------------------------+
//|                                                    MyFirstEA.mq5 |
//|                                  Copyright 2022, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#include<Trade\Trade.mqh>
CTrade         trade;            

int OnInit()
  {

   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {

  }

void OnTick()
  {
   
   
   double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
   MqlRates PriceInfo1[];
   ArraySetAsSeries(PriceInfo1, true);
   int PriceData1 = CopyRates(Symbol(),Period(),0,3,PriceInfo1);
      
   double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
   MqlRates PriceInfo2[];
   ArraySetAsSeries(PriceInfo2, true);
   int PriceData2 = CopyRates(Symbol(),Period(),0,3,PriceInfo2);
     
   double movavg1[], movavg2[];
   int movingAverageDef1 = iMA(_Symbol,_Period,50,0,MODE_EMA,PRICE_CLOSE);
   int movingAverageDef2 = iMA(_Symbol,_Period,200,0,MODE_EMA,PRICE_CLOSE);
   ArraySetAsSeries(movavg1,true);
   ArraySetAsSeries(movavg2,true);
   CopyBuffer(movingAverageDef1,0,0,3,movavg1);
   CopyBuffer(movingAverageDef2,0,0,3,movavg2);
   
   double Ask1=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
   MqlRates PriceInfo3[];
   ArraySetAsSeries(PriceInfo3, true);
   int PriceData3 = CopyRates(Symbol(),Period(),0,3,PriceInfo3);
   
   double Bid1=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
   MqlRates PriceInfo4[];
   ArraySetAsSeries(PriceInfo4,true);
   int PriceData4 = CopyRates(_Symbol,_Period,0,3,PriceInfo4);
   
   if(movavg1[1]>movavg2[1])
     {
     Comment("Buy");
     if (PositionsTotal() == 0)
     trade.Buy(0.10,NULL,Ask,Ask-300*_Point,Ask+150*_Point,NULL);
     }
     
    else if(movavg1[1]<movavg2[1])
     {
     Comment("Sell");
     if (PositionsTotal() == 0)
     trade.Sell(0.10,NULL,Bid,Bid+300*_Point,Bid-150*_Point,NULL);
     }
}     
  
Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • 2022.10.20
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
Files:
test.png  47 kb
 
   int movingAverageDef1 = iMA(_Symbol,_Period,50,0,MODE_EMA,PRICE_CLOSE);
   int movingAverageDef2 = iMA(_Symbol,_Period,200,0,MODE_EMA,PRICE_CLOSE);
   ArraySetAsSeries(movavg1,true);
   ArraySetAsSeries(movavg2,true);
   CopyBuffer(movingAverageDef1,0,0,3,movavg1);
   CopyBuffer(movingAverageDef2,0,0,3,movavg2);

Perhaps you should read the manual, especially the examples.
   How To Ask Questions The Smart Way. (2004)
      How To Interpret Answers.
         RTFM and STFW: How To Tell You've Seriously Screwed Up.

They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
          Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
          Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
          How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020)
          How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020)
          MQL5 for Newbies: Guide to Using Technical Indicators in Expert Advisors - MQL5 Articles (2010)
          How to call indicators in MQL5 - MQL5 Articles (2010)