How to close a position in condition?

 

Hello there!


I'm wondering how to close a position in condition? 

For example  When EMA 20 cross over EMA 50  then start buy position

and when  EMA 20 cross under EMA 50 then close this buy position


I only using trade.buy()


 if( (myMAfast[0] > myMAslow[0]) && (myMAfast[1] < myMAslow[1]) ) //cross over 

  { signal = "buy"; }


  if( (myMAfast[0] < myMAslow[0]) && (myMAfast[1] > myMAslow[1]) ) //cross under

  { signal  = "sell"; }


if(signal =="sell" && PositionsTotal()<1)

  trade.Sell(0.50,NULL,Bid,Ask+100*_Point,(Bid-150*_Point),NULL);


  if(signal =="buy" && PositionsTotal()<1)

  trade.Buy(0.50,NULL,Ask,Bid - 100*_Point,(Ask+150*_Point),NULL);


I'm thinking open another function ?


like


  if((myMAfast[0] < myMAslow[0]) && (myMAfast[1] > myMAslow[1])) //cross under 

  {

  ClosebuyPositions();

  }

 

but I don't know how to define  ClosebuyPositions()


Can anyone give me a hand?


thank a lot!





  


 
You didn't put much efforts to find it. They are hundreds of example on this site.
Reason: