if(OrderType()==OP_BUY) { if(Bid>OrderOpenPrice()) { tsbuy=Bid-OrderOpenPrice(); buystop=OrderStopLoss()+tsbuy; if(buystop>OrderStopLoss()) { //--- modify order and exit ticket=OrderModify(OrderTicket(),OrderOpenPrice,buystop,OrderTakeProfit(),0,Green); return; }
You will need to rethink your code.
Imagine a situation where you have a 10 pip SL and Bid reaches OrderOpenPrice() + 1 pip.
tsbuy=Bid-OrderOpenPrice(); //EQUALS 1 PIP buystop=OrderStopLoss()+tsbuy; //EQUALS ORDEROPENPRICE -9 PIPS if(buystop>OrderStopLoss()) //THIS WILL BE TRUE, SO ORDER IS MODIFIED TO -9 PIPS // NEXT TICK---- tsbuy=Bid-OrderOpenPrice(); //EQUALS 1 PIP buystop=OrderStopLoss()+tsbuy; //EQUALS ORDEROPENPRICE -8 PIPS if(buystop>OrderStopLoss()) //THIS WILL BE TRUE, SO ORDER IS MODIFIED TO -8 PIPS // NEXT TICK---- tsbuy=Bid-OrderOpenPrice(); //EQUALS 1 PIP buystop=OrderStopLoss()+tsbuy; //EQUALS ORDEROPENPRICE -7 PIPS if(buystop>OrderStopLoss()) //THIS WILL BE TRUE, SO ORDER IS MODIFIED TO -7 PIPS // SL WILL BE MODIFIED EVERY TICK UNTIL IT IS VERY CLOSE TO THE CURRENT PRICE
If Bid reaches OrderOpenPrice + 10, why does the order close because SL is near OrderOpenPrice? I feel I'm missing something basic here.
spillstuff: I am using OrderModify for trailing stops. I feel I'm missing something basic here.
| Yes you are. A trailing stop is relative to the OrderClosePrice (Bid). It's calculation does not involve the current SL value. |
-
tsbuy=Bid-OrderOpenPrice(); buystop=OrderStopLoss()+tsbuy; if(buystop>OrderStopLoss())
Rename tsbuy as profit. First tick you move stop up by profit., next tick you move (new) stop up by profit again, .... - You want a trailing stop. So you code it that way.
// tsbuy=Bid-OrderOpenPrice(); // buystop=OrderStopLoss()+tsbuy; buystop = Bid - FixedStopLoss * pips2dbl; if(buystop>OrderStopLoss() +_Point) // The == operand. - MQL4 forum
Trailing stops can use a lot of ordermodifys, it might be more efficient to trail the price internally and use OrderClose when it gets there.
SDC: Trailing stops can use a lot of ordermodifys, it might be more efficient to trail the price internally and use OrderClose when it gets there.
| And if you loose connection? |
WHRoeder:
And if you loose connection?
Thanks for the response(s). I am rethinking the code as suggested.
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
I am using OrderModify for trailing stops. The problem is when the stop is => the OrderOpenPrice the order closes at the stop. How do I get the order to stay open past the order open price?
if(OrderType()==OP_BUY)
{
if(Bid>OrderOpenPrice())
{
tsbuy=Bid-OrderOpenPrice();
buystop=OrderStopLoss()+tsbuy;
if(buystop>OrderStopLoss())
{
//--- modify order and exit
ticket=OrderModify(OrderTicket(),OrderOpenPrice,buystop,OrderTakeProfit(),0,Green);
return;
}
Any critique/suggestions appreciated.
Rudy