I'm attempting to modify my market orders to breakeven the position when the position get 100 pips to the good. This also accounts for the StopLevels
which are around 20-30 pips for my broker. It checks the param's via a "for(){...}
loop" function
The MagicNumber
is the timeframe number for the chart it is on (i.e. 240
=4H, 60
=1H) I don't set a TakeProfit
price & initially no StopLoss
price.
The EA is not adding a SL
to be equal to the opening price when the trade reaches 100 pip in profit (plus stoplevels). Profit points reaches well over 130 points.
My code is below for a OP_SELL
order - any help would be appreciated. Regards, Todd
/*Global Declarations*/
double pnlPoints;
double price, sl, tp;
double point;
int stopLevel;
int breakeven;
double newSL;
/*Local Declaratons*/
pnlPoints = 0;
point = MarketInfo( Symbol(), MODE_POINT );
stopLevel = int( MarketInfo( Symbol(), MODE_STOPLEVEL )
+ MarketInfo( Symbol(), MODE_SPREAD )
);
sl = NormalizeDouble( OrderStopLoss(), Digits );
tp = OrderTakeProfit();
breakeven = 100;
for( int s = OrdersTotal() - 1; s >= 0; s-- )
{ if ( ( OrderSelect( s, SELECT_BY_POS, MODE_TRADES ) ) == true )
price = MarketInfo( Symbol(), MODE_ASK );
newSL = NormalizeDouble( OrderOpenPrice(), Digits );
pnlPoints = ( OrderOpenPrice() - price ) / point;
if ( OP_SELL == OrderType() )
if ( Period() == OrderMagicNumber() )
if ( stopLevel < ( newSL - price ) / point )
if ( breakeven < pnlPoints )
if ( newSL != sl )
ModSell = OrderModify( OrderTicket(),
OrderOpenPrice(),
newSL,
tp,
buycolor
);
else if ( ModBuy == false )
{ Print( "OrderModify failed with error #",
GetLastError()
);
}
}
For the moment being,
refine the code
and
add self-debuging / tracing code
After
OrderModify()
use a self-debugging / journalingPrint( StringFormat( ... ) )
to document all instructed values used in the actualOrderModify()
call and also the remote-execution ( { server-side | StrategyTester } ) reported issues.The current code does not enter into such self-diagnostics and
ModSell
is not inspected at all,ModBuy
is inspected only at uncertain conditions / by-coincidence at some future visit of thefor(){...}
code-execution path to a part afternewSL == sl
( and all above stated conditions are just by chance met too )Next, check an assigned value of
tp
As stated above,
which introduces a reasonable doubt, that re-using of this ( inherently uncertain value, as no one knows, which
OrderSelect()
was the last one that set adb.Pool
pointer to decide, from which record from thedb.Pool
this veryOrderTakeProfit()
would accidentally read ( if any record is present indb.Pool
already ) inside the wholefor(){...}
traversing thedb.Pool
records will not meet conditions for setting properly a TakeProfit price in the next series ofOrderModify()
calls.This seems to be the root cause, or a source of unhandled exceptions to the valid, Broker-compliant,
OrderModify()
values.Try this:
Then check the Expert-tab for error-message if it fails to set the stop.
Also, you need to take note that StopLoss will ONLY occur if you are on the right chart-timeframe; Otherwise, it won't even get into the if-statements.