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 / journaling Print( StringFormat( ... ) )
to document all instructed values used in the actual OrderModify()
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 the for(){...}
code-execution path to a part after newSL == sl
( and all above stated conditions are just by chance met too )
Next, check an assigned value of tp
As stated above,
/*Local Declarations*/
...
tp = OrderTakeProfit();
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 a db.Pool
pointer to decide, from which record from the db.Pool
this veryOrderTakeProfit()
would accidentally read ( if any record is present in db.Pool
already ) inside the whole for(){...}
traversing the db.Pool
records will not meet conditions for setting properly a TakeProfit price in the next series of OrderModify()
calls.
This seems to be the root cause, or a source of unhandled exceptions to the valid, Broker-compliant, OrderModify()
values.
Try this:
if (newSL != sl ) {
ModSell = OrderModify( OrderTicket(),
OrderOpenPrice(),
OrderOpenPrice(),
0,
OrderExpiration(),
clrRed
);
if(ModBuy == false )
Print( "OrderModify failed with error #", GetLastError());
}
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.
来源:https://stackoverflow.com/questions/36742043/why-is-my-ea-not-moving-my-position-to-breakeven