You reminded me of something I tried to do a while back - I will modify this code, I have not tested this - but will do so this week.
Code:
/////////////////////////////////////////////
void twostepts(string Symbol1, int magicNum, int ts1)
{
/*
So my exit is like this:
1) start trade
2) when profit is 30p move sl to be
3) form there use ts 30p
4) when profit will reach 60p move sl to 55p
5) if price still goes up and is above 60p then use ts 15p
and exit on stop.
*/
int total, cnt, ticket1;
int period2;
double n1, newsl;
total = OrdersTotal();
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && OrderSymbol()==Symbol1 && OrderMagicNumber()== magicNum)
{
//close any other outstanding pending OrderSelect
if (totalstop>0)
{
Print("closing all pending, totalstops = ", totalstop);
CloseAllPendingBUYSELL(Symbol1,magicNum);
}
if(OrderType()==OP_BUY) // long position is opened
{
// check for trailing stop
if(ts1>0)
{
if(Bid-OrderOpenPrice()>Point*62)
{
if(OrderStopLoss()<Bid-Point*15)
{
// Print("stage3 nts, Profit : ",Bid-OrderOpenPrice(),", Current sl : ",OrderStopLoss(),", new sl : ", Bid-Point*15);
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*15,OrderTakeProfit(),0,Green);
return(0);
}
return(0);
}
if(Bid-OrderOpenPrice()>Point*60)
{
if(OrderStopLoss()<OrderOpenPrice()+55*Point)
{
// Print("stage2 nts, Profit : ",Bid-OrderOpenPrice(),", Current sl : ",OrderStopLoss(),", new sl : ", OrderOpenPrice()+55*Point);
OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+55*Point,OrderTakeProfit(),0,Green);
return(0);
}
return(0);
}
if(Bid-OrderOpenPrice()>Point*30)
{
if(OrderStopLoss()<Bid-Point*30)
{
// Print("stage1 nts, Profit : ",Bid-OrderOpenPrice(),", Current sl : ",OrderStopLoss(),", new sl : ", OrderOpenPrice()+2*Point);
OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+2*Point,OrderTakeProfit(),0,Green);
return(0);
}
return(0);
}
}
}
else // go to short position
{
// check for trailing stop
if(ts1>0)
{
if((OrderOpenPrice()-Ask)>(Point*62))
{
if((OrderStopLoss()>(Ask+Point*15)) || (OrderStopLoss()==0))
{
//Print("stage1 nts, Profit : ",Bid-OrderOpenPrice(),", Current sl : ",Ask+Point*15,", new sl : ", OrderOpenPrice()+2*Point);
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*15,OrderTakeProfit(),0,Red);
return(0);
}
return(0);
}
if((OrderOpenPrice()-Ask)>(Point*60))
{
if((OrderStopLoss()>(OrderOpenPrice()-55*Point)) || (OrderStopLoss()==0))
{
//Print("stage1 nts, Profit : ",Bid-OrderOpenPrice(),", Current sl : ",OrderOpenPrice()-55*Point,", new sl : ", OrderOpenPrice()+2*Point);
OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-55*Point,OrderTakeProfit(),0,Red);
return(0);
}
return(0);
}
if((OrderOpenPrice()-Ask)>(Point*30))
{
if((OrderStopLoss()>(OrderOpenPrice()-2*Point)) || (OrderStopLoss()==0))
{
//Print("stage1 nts, Profit : ",Bid-OrderOpenPrice(),", Current sl : ",OrderOpenPrice()-2*Point,", new sl : ", OrderOpenPrice()+2*Point);
OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-2*Point,OrderTakeProfit(),0,Red);
return(0);
}
return(0);
}
}
}
}
//end trailing stop
}
}
///////////////////////////////////