Hey all newb here again
Using a hedging account, I want to open an order when at an EMA cross with stop loss and take profit. I also however, want the order to close if the opposite cross happens before a stop loss or take profit has been triggered. About 8 hours into this problem and I’m lost as to why the close orders are not executing. My buys and sells seem to be happening in the proper place using the total=0 but closes seem to be completely ignored. Please help.
if(total==0)
{
//--- no opened positions identified
if(buyCondition_1 && buyCondition_2)
{
if(!trade.Buy(Lot, symbol.Name(), symbol.Ask(),(symbol.Bid()-StopLoss*Point()),(symbol.Bid()+TakeProfit*Point()),NULL)) //Request is completed or order placed
{
Print("Buy Order Failed",trade.ResultRetcode(),". Error:",trade.ResultRetcodeDescription());
}
else
{
Print("Buy Order Successful") ;
return;
}
}
if(sellCondition_1 && sellCondition_2)
{
if(!trade.Sell(Lot, symbol.Name(), symbol.Ask(),(symbol.Bid()+StopLoss*Point()),(symbol.Bid()-TakeProfit*Point()),NULL)) //Request is completed or order placed
{
Print("Sell Order Failed ",trade.ResultRetcode(),". Error:",trade.ResultRetcodeDescription());
}
else
{
Print("Sell Order Successful");
return;
}
}
return;
}
for(int i=PositionsTotal()-1;i>=0;i--)
if(position.SelectByIndex(i)) // selects the position by index for further access to its properties
if(position.Symbol()==Symbol() && position.Magic()==Magic_Number)
{
if(!RefreshRates())
{
return;
}
//---
if(position.PositionType()==POSITION_TYPE_BUY)
{
if(sEMA[0]>fEMA[0])
{
trade.PositionClose(position.Ticket()); // close position
continue;
}
}
//---
if(position.PositionType()==POSITION_TYPE_SELL)
{
if(fEMA[0]>sEMA[0])
{
trade.PositionClose(position.Ticket()); // close position
continue;
}
}
}