Moving stop to break even once up 50 pips - page 2

 
n4btb:


I looked at the journal and it has Order Modify Error 1. I saw from a previous post of yours that this likely means I am trying to modify an order but with no changes. But I can make no sense of that.

Why doesn't if (OrderStopLoss() != OrderOpenPrice()) in the code below prevent that?

It seems ot only run on the initial bar the order was placed in? Unless I am making assumptions.


Read this thread: Can price != price ?
 
RaptorUK:
Read this thread: Can price != price ?

FYI, I usually find this is sufficient but also, since the OP is setting break even, why not just check if SL < OrderOpenPrice() ? This allows it to work even when the SL is being trailed by either the EA or external trade manager, etc.. where the SL could already be > open price

Also, you mention it setting BE @ 10 pips - you are checking that value in your calculation by using 100, use 500 for 50 pips.

Regards, Paul.

bool DoublesEqual(double number1, double number2, int Digts)
{
  return(NormalizeDouble(number1-number2,Digts)==0);
}
if ( ! DoublesEqual(OrderOpenPrice(), OrderStopLoss(), Digits-1) ) { do modify order }
 
SDC:

Use select by pos, because you have to already know the ticket number to use select by ticket

Make sure you Normalize the OrderOpenPrice() and OrderTakeProfit(), if you dont you will probably have problems.

and if you do anything like if (OrderStopLoss() != OrderOpenPrice()) Normalize them before you compare

to test for pip movement I do

buys .. if(Bid >= openprice+50*Point)

sells .. if(Ask <= openprice-50*Point)


Will using bid and ask work when backtesting? The data is 1M data and only HLOC
 

I have multiple orders open at a time which was causing issues.

Below now works better as it counts through all open orders to look for orders to modify.

It work for the closing of multiple open orders, but I still cannot get it to correctly move the stop loss the breakeven on a 10 pip move in favour of the trade. I am on a 5 digit broker.


int cnt=TotalOrdersCount();
for(int i=cnt-1;i>=0;i--)
{
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL &&
OrderSymbol()==Symbol() &&
OrderMagicNumber()==MagicNumber)

// this is meant to set stop loss to break even when buy trade moved 10 pips in favour: Any suggestions please why it doesn't work.
{
if(Ask <= OrderOpenPrice()-100*Point)
{
if (OrderStopLoss() != OrderOpenPrice())
{
OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice(), 0, 0);
}
}
}


// this is meant to set stop loss to break even when buy trade moved 10 pips in favour:
{
if(Bid >= OrderOpenPrice()-100*Point)
{
if (OrderStopLoss() != OrderOpenPrice())
{
OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice(), 0, 0);
}
}
}

{


if(OrderType()==OP_SELL)

{
// Here is close sell rule.. this bit works for multipl open orders......
.............................................................

{
OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red);
}
}


if(OrderType()==OP_BUY)
{
// Here is close buy rule
...................................................

{
OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red);
}
}

}
}

 
RaptorUK:

You DO NOT need to Normalize OrderStopLoss() or OrderOpenPrice() . . really, you do not.


So one of your OrderModify() calls fails, why ? what error is generated ? I see no code to check the return value or report any error . . . why ?

Read this: What are Function return values ? How do I use them ? and use the return value from your OrderModify() and report any errors, then you might know what is going wrong.


The reason I said to Normalize them is because I had problems with OrderModify() in the past with un-normalized doubles. Esepcially in calculations using the OrderOpenPrice(), OrderStopLoss(), OrderTakeProfit(), for example I did this:

double newstoploss = OrderOpenPrice()+5*Point:

I sent newstoploss in OrderModify()

Then in my code to test the stoploss

if(OrderStopLoss() != OrderOpenPrice()+5*Point)

Was triggered every time even after the stoploss was modified.

The reason I discovered was, some kind of an error caused the new stoploss to be 0.0000000000000000000000000000000000000000001 different from openprice+5. The modified stoploss and openprice+5 were not truly equal.

I fixed it by doing

double newstoploss = NormalizeDouble(OrderOpenPrice()+5*Point,Digits); 

Then when I do the same test

if(OrderStopLoss() != OrderOpenPrice()+5*Point) only triggers when the stoploss was not yet modified.

So now I always normalize the doubles before using them in OrderModify()

 
n4btb:

It work for the closing of multiple open orders, but I still cannot get it to correctly move the stop loss the breakeven on a 10 pip move in favour of the trade. I am on a 5 digit broker

What is your definition of a pip? A 10 point move in a five decimal place broker would (incorrectly described in my opinion) be a 1 pip move in some people minds and would not overcome the restrictions placed on modifying an order.

 
n4btb:

Will using bid and ask work when backtesting? The data is 1M data and only HLOC


Yes you can use Bid and Ask in strategy tester. It uses an algorithm which attempts to simulate how the price bars were formed from ticks, it uses the HLOC from the lowest available timeframe data, so the more 1 minute bars you have, the more accurate the simulation will be.

 
SDC:


Yes you can use Bid and Ask in strategy tester. It uses an algorithm which attempts to simulate how the price bars were formed from ticks, it uses the HLOC from the lowest available timeframe data, so the more 1 minute bars you have, the more accurate the simulation will be.


In my experience the history data can have Bid and Ask values incorectly saved and may need to be normalized - however you do not need to normalize Bid and Ask with live data.
 
SDC:


The reason I said to Normalize them is because I had problems with OrderModify() in the past with un-normalized doubles. Esepcially in calculations using the OrderOpenPrice(), OrderStopLoss(), OrderTakeProfit(), for example I did this:

I sent newstoploss in OrderModify()

Then in my code to test the stoploss

Was triggered every time even after the stoploss was modified.

The reason I discovered was, some kind of an error caused the new stoploss to be 0.0000000000000000000000000000000000000000001 different from openprice+5. The modified stoploss and openprice+5 were not truly equal.

That isn't "some kind of error" . . . it is explained in this thread: Can price != price ? it's simply a by product of the way that doubles are handled, it is an expected side effect that your code needs to cope with, doing . . . .

if( double != double )

. . . will often show up this by product. But what does that double compare have to do with an OrderModify ? you DO NOT need to Normalize the values that you use with OrderModify especially when those values have come from OrderXxxx() functions such as OrderOpenPrice().


If you are calculating a value that you use in your OrderModify() call, or example using Fibonacci, then you will need to normalize it and you can use NormalizeDouble() to do this . . but it isn't the best way and doesn't do what you really need it to do for all Instruments, for example Indices such as the DAX.

 
RaptorUK:

That isn't "some kind of error" . . . it is explained in this thread: Can price != price ? it's simply a by product of the way that doubles are handled, it is an expected side effect that your code needs to cope with, doing . . . .

. . . will often show up this by product. But what does that double compare have to do with an OrderModify ? you DO NOT need to Normalize the values that you use with OrderModify especially when those values have come from OrderXxxx() functions such as OrderOpenPrice().


If you are calculating a value that you use in your OrderModify() call, or example using Fibonacci, then you will need to normalize it and you can use NormalizeDouble() to do this . . but it isn't the best way and doesn't do what you really need it to do for all Instruments, for example Indices such as the DAX.


Alright, I'll accept you do not NEED to use NormalizeDouble() for the OrderModify() parameters. BUT it makes me feel better to normalize them.

Reason: