Why is my EA not moving my position to breakeven?

三世轮回 提交于 2019-12-23 03:24:06

问题


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()
                                                   );
                                     }
   }

回答1:


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.




回答2:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!