How to force ea to wait

SIMONE MARELLI  

Good Morning Guys,

I have searched on forum but i can't find the solution, i am pretty new to coding.

I have made a simple ea, a simple triple ema cross. But there is an important condition that i need to be true, here is how it should work;

Ema 20, 50, 200

When ema 50 cross the 200 (above or belove), when price is at the ema, open the trades. But just one per cross. I really don't know how to code it, i mean, i have coded it but it open when ema 50 cross ema 200 (and not when price is back to the ema).

Could you help me? How can i share my code here?

thanks, Simo

SIMONE MARELLI  
Andres Sigala:
Share your code to help you

Sure, how to share it correctly?


void OnTick()

  {


   double Equity = AccountInfoDouble(ACCOUNT_EQUITY);

   double Balance = AccountInfoDouble(ACCOUNT_BALANCE);


   if(Equity>Balance*1.009)

     {

      CloseAllPositions();

     }


   if(Equity<Balance*0.1)

     {

      CloseAllPositions();

     }


   int PositionOpened=0;

   for(int i=PositionsTotal()-1; i>=0; i--)

     {

      string symbol=PositionGetSymbol(i);

      if(Symbol()==symbol)

        {

         PositionOpened+=1;

        }

     }


   long   Volume10 = iTickVolume(Symbol(),Period(),10);

   long   Volume9 = iTickVolume(Symbol(),Period(),9);

   long   Volume8 = iTickVolume(Symbol(),Period(),8);

   long   Volume7 = iTickVolume(Symbol(),Period(),7);

   long   Volume6 = iTickVolume(Symbol(),Period(),6);

   long   Volume5 = iTickVolume(Symbol(),Period(),5);

   long   Volume4 = iTickVolume(Symbol(),Period(),4);

   long   Volume3 = iTickVolume(Symbol(),Period(),3);

   long   Volume2 = iTickVolume(Symbol(),Period(),2);

   long   Volume1 = iTickVolume(Symbol(),Period(),1);

   long   Volume = iTickVolume(Symbol(),Period(),0);

   long InVolume = (Volume>Volume1);

   long DnVolume = (Volume<Volume1);

   long AVGVL = (Volume9+Volume10+Volume8+Volume7+Volume6+Volume5+Volume4+Volume3+Volume2+Volume1)/10;

   double Ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);

   double Bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);

   double DinamicLots= NormalizeDouble((Equity/1500),2);

   string signal="";


   double iMom[];

   int Momentum = iMomentum(_Symbol,_Period,20,PRICE_CLOSE);

   ArraySetAsSeries(iMom,true);

   CopyBuffer(Momentum,0,0,3,iMom);

   double MomentumValue = NormalizeDouble(iMom[0],2);


   double myPriceArray[];

   int Volumes = iVolumes(_Symbol,_Period,VOLUME_TICK);

   ArraySetAsSeries(myPriceArray,true);

   CopyBuffer(Volumes,0,0,3,myPriceArray);

   float CVolumes = (myPriceArray[0]);

   float LVolumes = (myPriceArray[1]);


   double myPriceArray2[];

   int ATR = iATR(_Symbol,_Period,14);

   ArraySetAsSeries(myPriceArray2,true);

   CopyBuffer(ATR,0,0,3,myPriceArray2);

   double ATRVAlue = NormalizeDouble(myPriceArray2[0],5);


   if(ATRVAlue<0.00024 && InVolume && CVolumes>AVGVL*2.5)

     { signal="buy";}


   if(ATRVAlue>0.00014 && DnVolume && CVolumes>AVGVL*2.5)

     { signal="sell";}


   if(signal =="buy")

      trade.Buy(DinamicLots,NULL,Ask,0,Ask+50*_Point,"Rims Buy Order V2");

   Sleep(300000);



   if(signal =="sell")

      trade.Sell(DinamicLots,NULL,Bid,0,Bid-50*_Point,"Rims Sell Order V2");

   Sleep(300000);


  }

//+------------------------------------------------------------------+


//+------------------------------------------------------------------+

//|                                                                  |

//+------------------------------------------------------------------+

void CloseAllPositions()

  {

   for(int i=PositionsTotal()-1; i>=0; i--)

     {

      int ticket=PositionGetTicket(i);

      trade.PositionClose(ticket);

     }

  }


//+------------------------------------------------------------------+

//|                                                                  |

//+------------------------------------------------------------------+

void Timer()

  {

   for(int i=PositionsTotal()-1; i>=0; i--)

     {

      int ticket=PositionGetTicket(i);

      datetime PositionOpenTime=PositionGetInteger(POSITION_TIME);

      MqlDateTime MyOpenTime;

      TimeToStruct(PositionOpenTime,MyOpenTime);

      int OpeningHour= MyOpenTime.hour;

      datetime LocalTime=TimeLocal();

      MqlDateTime MyLocalTime;

      TimeToStruct(LocalTime,MyLocalTime);

      int CurrentHour=MyLocalTime.hour;

      int Difference = CurrentHour-OpeningHour;


      if(Difference>33)

        {

         trade.PositionClose(ticket);

        }

     }

  }

//+------------------------------------------------------------------+


Chidera Anakpe  
SIMONE MARELLI:

Sure, how to share it correctly?


void OnTick()

  {


   double Equity = AccountInfoDouble(ACCOUNT_EQUITY);

   double Balance = AccountInfoDouble(ACCOUNT_BALANCE);


   if(Equity>Balance*1.009)

     {

      CloseAllPositions();

     }


   if(Equity<Balance*0.1)

     {

      CloseAllPositions();

     }


   int PositionOpened=0;

   for(int i=PositionsTotal()-1; i>=0; i--)

     {

      string symbol=PositionGetSymbol(i);

      if(Symbol()==symbol)

        {

         PositionOpened+=1;

        }

     }


   long   Volume10 = iTickVolume(Symbol(),Period(),10);

   long   Volume9 = iTickVolume(Symbol(),Period(),9);

   long   Volume8 = iTickVolume(Symbol(),Period(),8);

   long   Volume7 = iTickVolume(Symbol(),Period(),7);

   long   Volume6 = iTickVolume(Symbol(),Period(),6);

   long   Volume5 = iTickVolume(Symbol(),Period(),5);

   long   Volume4 = iTickVolume(Symbol(),Period(),4);

   long   Volume3 = iTickVolume(Symbol(),Period(),3);

   long   Volume2 = iTickVolume(Symbol(),Period(),2);

   long   Volume1 = iTickVolume(Symbol(),Period(),1);

   long   Volume = iTickVolume(Symbol(),Period(),0);

   long InVolume = (Volume>Volume1);

   long DnVolume = (Volume<Volume1);

   long AVGVL = (Volume9+Volume10+Volume8+Volume7+Volume6+Volume5+Volume4+Volume3+Volume2+Volume1)/10;

   double Ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);

   double Bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);

   double DinamicLots= NormalizeDouble((Equity/1500),2);

   string signal="";


   double iMom[];

   int Momentum = iMomentum(_Symbol,_Period,20,PRICE_CLOSE);

   ArraySetAsSeries(iMom,true);

   CopyBuffer(Momentum,0,0,3,iMom);

   double MomentumValue = NormalizeDouble(iMom[0],2);


   double myPriceArray[];

   int Volumes = iVolumes(_Symbol,_Period,VOLUME_TICK);

   ArraySetAsSeries(myPriceArray,true);

   CopyBuffer(Volumes,0,0,3,myPriceArray);

   float CVolumes = (myPriceArray[0]);

   float LVolumes = (myPriceArray[1]);


   double myPriceArray2[];

   int ATR = iATR(_Symbol,_Period,14);

   ArraySetAsSeries(myPriceArray2,true);

   CopyBuffer(ATR,0,0,3,myPriceArray2);

   double ATRVAlue = NormalizeDouble(myPriceArray2[0],5);


   if(ATRVAlue<0.00024 && InVolume && CVolumes>AVGVL*2.5)

     { signal="buy";}


   if(ATRVAlue>0.00014 && DnVolume && CVolumes>AVGVL*2.5)

     { signal="sell";}


   if(signal =="buy")

      trade.Buy(DinamicLots,NULL,Ask,0,Ask+50*_Point,"Rims Buy Order V2");

   Sleep(300000);



   if(signal =="sell")

      trade.Sell(DinamicLots,NULL,Bid,0,Bid-50*_Point,"Rims Sell Order V2");

   Sleep(300000);


  }

//+------------------------------------------------------------------+


//+------------------------------------------------------------------+

//|                                                                  |

//+------------------------------------------------------------------+

void CloseAllPositions()

  {

   for(int i=PositionsTotal()-1; i>=0; i--)

     {

      int ticket=PositionGetTicket(i);

      trade.PositionClose(ticket);

     }

  }


//+------------------------------------------------------------------+

//|                                                                  |

//+------------------------------------------------------------------+

void Timer()

  {

   for(int i=PositionsTotal()-1; i>=0; i--)

     {

      int ticket=PositionGetTicket(i);

      datetime PositionOpenTime=PositionGetInteger(POSITION_TIME);

      MqlDateTime MyOpenTime;

      TimeToStruct(PositionOpenTime,MyOpenTime);

      int OpeningHour= MyOpenTime.hour;

      datetime LocalTime=TimeLocal();

      MqlDateTime MyLocalTime;

      TimeToStruct(LocalTime,MyLocalTime);

      int CurrentHour=MyLocalTime.hour;

      int Difference = CurrentHour-OpeningHour;


      if(Difference>33)

        {

         trade.PositionClose(ticket);

        }

     }

  }

//+------------------------------------------------------------------+


Use the code upload option. (Alt + S)

Reason: