Trailing stop help

 

Hi everyone,

I (obviously which will become apparant) know nothing about EAs and have an EA coded by a company which I wanted to add a trailing stop too in metatrader4.

I don't want anything exciting or fancy, just a run-of-the-mill trailing stop that kicks in after 25 pips and trails by 25 pips until its closed or I close it.  (25 is just an example figure).

I got this from another thread but getting errors about return value of 'OrderSelect' and 'OrderModify' needing to be checked and not all control paths return a value.

--------------

extern int     TS        =  25;

int start()
{
//----
OrderSelect(0, SELECT_BY_POS);

if (OrderType() == OP_BUY) {
if (OrderStopLoss()< Bid-Point*TS) OrderModify(OrderTicket(),0,Bid-Point*TS,OrderTakeProfit(),0,Red); }

if (OrderType() == OP_SELL) {
if (OrderStopLoss()> Bid+Point*TS) OrderModify(OrderTicket(),0,Bid+Point*TS,OrderTakeProfit(),0,Red); }
//----

}

--------------

Could anyone explain what is wrong and what needs to be fixed?

Thanks, I hope.

 
ujtrader:

Have you tried searching for trailing stop in the codebase?

for example:

https://www.mql5.com/en/code/9530

https://www.mql5.com/en/code/11318

https://www.mql5.com/en/code/1408

 

Hi

The "errors" you're getting are just warnings. Have a look in the errors tab of MetaEditor. If they're errors, you'll see the red stop sign. If they're just warnings, it will be the yellow triangle sign. Warnings won't stop the EA compiling, it just means the code isn't perfect and you could get unexpected results.

You're getting the first warning because you're not checking if the OrderModify was successful. (it's a boolean). eg:

         if(OrderStopLoss()<Bid-Point*TrailingStop)
           {
            bool res=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Bid-Point*TrailingStop,Digits),OrderTakeProfit(),0,Blue);
            if(!res)
               Print("Error in OrderModify. Error code=",GetLastError());
            else
               Print("Order modified successfully.");
           }

 

You're getting the second warning because one of your functions, eg int start(), isn't returning a value: eg return(0)

Also, using that OrderSelect, you're only ever going to be selecting the first order. And if there is no open order, it has nothing to check. You need to put it in a for loop which will cycle through all open orders and if there are none, it won't execute:

for(int j=0; j<OrdersTotal(); j++) 
{
   OrderSelect(j,SELECT_BY_POS);

   etc etc etc
}



Hope that helps

Reason: