One trade per price crossing EMA 9

 

Hi!

My goal is to build a bot that will buy the market price ONLY when a 5 minute candle opens up above EMA line. After the trade it would wait until it has opened a new candel below EMA line. Only after that it may take another long position.

It is working so far, but now what it does is that it ALWAYS opens a new long position when price is above the EMA line. Can't figure this out. Can you guys help me implement with this?


Pseudocode solution would be something like this:


if (Price has already traded above EMA without moving below the EMA inbetween) {

 --> No trade!

} else {

Look for a next trading opportunity

}


Here is my code so far:

//If p > EMA9 and a trade happens --> No more trades until P < EMA9

void OnTick()
  {
  

   //String variable to the signal
   string signal = "";
   
   //9EMA:
   double movingAverage = iMA(_Symbol, _Period, 9, 0, MODE_EMA, PRICE_CLOSE, 0);
   
   
   //Buy signal
   if (Open[1] < movingAverage && Open[0] > movingAverage) {
   
   signal = "buy";
   }
   
   //Sell signal
   if (movingAverage > Close[0]) {
   
   signal = "sell";
   }
   

   //Buy 0.1 lots
   if (signal == "buy" && OrdersTotal() == 0) {
   
   OrderSend (_Symbol, OP_BUY, 0.10, Ask, 3, Ask-10*_Point, Ask+10*_Point, NULL, 0,0, Green);
   }
   
   
   
   //Sell 0.1 lots
   if (signal == "sell" && OrdersTotal() == 0) {
   
   OrderSend (_Symbol, OP_SELL, 0.10, Bid, 3, Ask+10*_Point, Ask-10*_Point, NULL, 0,0, Red);

   }
  }
 
billishbullish:

Hi!

My goal is to build a bot that will buy the market price ONLY when a 5 minute candle opens up above EMA line. After the trade it would wait until it has opened a new candel below EMA line. Only after that it may take another long position.

It is working so far, but now what it does is that it ALWAYS opens a new long position when price is above the EMA line. Can't figure this out. Can you guys help me implement with this?


Pseudocode solution would be something like this:


if (Price has already traded above EMA without moving below the EMA inbetween) {

 --> No trade!

} else {

Look for a next trading opportunity

}


Here is my code so far:


Try something that.


int SwitchBuySell=0;
void OnTick()
  {
  

   //String variable to the signal
   string signal = "";
   
   //9EMA:
   double movingAverage = iMA(_Symbol, _Period, 9, 0, MODE_EMA, PRICE_CLOSE, 0);
   
   
   //Buy signal
   if((Open[0]>movingAverage)&&((SwitchBuySell==0)||(SwitchBuySell==-1)))
     {
     signal = "buy";
     }
   
   //Sell signal
   if((Open[0]<movingAverage)&&((SwitchBuySell==0)||(SwitchBuySell==1)))
     {
     signal = "sell";
     }
   
   //Buy 0.1 lots
   if(signal == "buy" && OrdersTotal() == 0)
     {
     SwitchBuySell=1;
     OrderSend (_Symbol, OP_BUY, 0.10, Ask, 3, Ask-10*_Point, Ask+10*_Point, NULL, 0,0, Green);
     }
 
   //Sell 0.1 lots
   if(signal == "sell" && OrdersTotal() == 0)
     {
     SwitchBuySell=-1;
     OrderSend (_Symbol, OP_SELL, 0.10, Bid, 3, Ask+10*_Point, Ask-10*_Point, NULL, 0,0, Red);
     }
  }
 
Nikolaos Pantzos:


Try something that.


Solved it. Thank you very much for help!