Cross EMA Position to Close

 

Hi all

I have a short simple code here below, if anyone could help please , it would be really appreciated.

Main idea is when the fast ema cross over slow ema then to open a buy position and actually it works quite well with the code below.

However I need your help about how to close the position when the opposite happens; Slow EMA  crossover the Fast EMA?

I don't want to use SL or TP options. As my main idea is keeping the trade open unless the slow ema crossover fast ema.

Many thanks

Regards


#include <Trade\Trade.mqh>

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

//| Input Variables                                 |

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

input int InpFastPeriod = 3; //Fast Period

input int InpSlowPeriod = 5; //Slow Period

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

//| Global Variables                                 |

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

int FastHandle;

int SlowHandle;

double FastBuffer[];

double SlowBuffer[];

datetime openTimeBuy = 0;

datetime opentimeClose = 0;

CTrade trade;

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

//| Expert initialization function                                   |

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

int OnInit() {

 // create handles

 FastHandle = iMA(_Symbol,PERIOD_CURRENT,InpFastPeriod,0,MODE_EMA,PRICE_CLOSE);

 if(FastHandle == INVALID_HANDLE) {

  Alert("Failed to create fast handle");

  return INIT_FAILED;

  }

  SlowHandle = iMA(_Symbol,PERIOD_CURRENT,InpSlowPeriod,0,MODE_EMA,PRICE_CLOSE);

 if(SlowHandle == INVALID_HANDLE) {

  Alert("Failed to create SLOW handle");

  return INIT_FAILED;

  }

  ArraySetAsSeries(FastBuffer, true);

  ArraySetAsSeries(SlowBuffer, true);

   return(INIT_SUCCEEDED);

 }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

   if(FastHandle != INVALID_HANDLE) {IndicatorRelease(FastHandle);}

   if(SlowHandle != INVALID_HANDLE) {IndicatorRelease(SlowHandle);}

  }

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

//| Expert tick function                                             |

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

void OnTick() {

//get indicator values

 int values = CopyBuffer(FastHandle,0,0,2,FastBuffer);

   if(values !=2 ) {

   Print("No enough data for fast moving average");

   return;

  }

  values = CopyBuffer(SlowHandle,0,0,2,SlowBuffer);

   if(values !=2 ) {

   Print("No enough data for slow moving average");

   return;

}

   Comment("Fast[0]:",FastBuffer[0],"\n",

  "Fast[1]:",FastBuffer[1],"\n",

   "Slow[0]:",SlowBuffer[0],"\n",

  "Slow[1]:",SlowBuffer[1]);

  //check for cross to buy

   if(FastBuffer[1] <= SlowBuffer[1] && FastBuffer[0] > SlowBuffer[0] && openTimeBuy != iTime(_Symbol, PERIOD_CURRENT,0)){

   openTimeBuy != iTime(_Symbol, PERIOD_CURRENT,0);

   double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);

   trade.PositionOpen(_Symbol,ORDER_TYPE_BUY,1.0,ask,0,0, "Cross MA");

   }

}
 

Just add a function to close the sells when a buy signal appears and another function to close the buys when a sell signal appears.

 if(FastBuffer[1] >= SlowBuffer[1] && FastBuffer[0] < SlowBuffer[0]){
//close buys
   }
 

I tried this but it didn't work, would you mind if you complete the code above please?

Many thanks

Regards