V 1b is in post 1. I have tried to add code that will deal with this situation. It is all a bit messy, so perhaps someone can see a tidier way of doing this?SteveHopwood wrote:cosmo wrote:Sometimes a stop loss mod and a part close in rapid succession causes a trade context error. I will have a look and see if I can add code to force a close if the order lot size = Lot and the stop is at BE.
My EURUSD Buy trade has exceeded the 50 pip target but the EA has not closed the 1/3 ?
But i have now done so manually.
The code is in void CountOpenTrades()
Code: Select all
//Profitable trade management
if (OrderProfit() > 0 && !TradeWasClosed)
{
double PartLot = Lot / 3;
if (OrderLots() > PartLot && EnablePartClosure)
{
TradeManagementModule();//This will move the stop to BE come what may
//pp = pips profit
double pp = CalculateTradeProfitInPips(OrderType() );
if (pp >= BreakEvenPips && pp < (JumpingStopPips * 2) && OrderLots() == Lot) PartCloseThisTrade();
if (pp >= (JumpingStopPips * 2) && OrderLots() > PartLot) PartCloseThisTrade();
//This snippet is to take care of the situation where the stop has been moved to BE but the part-close failed. Then the market retraced, so the part-close was not attempted again.
if (OrderType() == OP_BUY && OrderStopLoss() >= OrderOpenPrice() && OrderLots() == Lot)
{
PartCloseThisTrade();
}//if (OrderType() == OP_BUY)
if (OrderType() == OP_SELL && OrderStopLoss() <= OrderOpenPrice() && OrderLots() == Lot)
{
PartCloseThisTrade();
}//if (OrderType() == OP_SELL)
//This snippet is to take care of the same situation but following the first jumping stop, when the second third of the trade should have closed. This is all a bit messy, so if someone can see a better way of coding all this, then please do sing out.
if (OrderType() == OP_BUY && OrderStopLoss() >= OrderOpenPrice() + (BreakEvenProfit / factor) + JumpingStopPips && OrderLots() > PartLot)
{
PartCloseThisTrade();
}//if (OrderType() == OP_BUY && OrderStopLoss() >= OrderOpenPrice()
if (OrderType() == OP_SELL && OrderStopLoss() >= OrderOpenPrice() - (BreakEvenProfit / factor) - JumpingStopPips && OrderLots() > PartLot)
{
PartCloseThisTrade();
}//if (OrderType() == OP_BUY && OrderStopLoss() >= OrderOpenPrice()
}//if (OrderLots() > PartLot && EnablePartClosure)
else TradeManagementModule();//This deals with subsequent jumping/trailing stops once part-close has already taken place
}//if (OrderProfit() > 0 && !TradeWasClosed)