Download MetaTrader 5

Error Modify error 130 have checked all things possible

To add comments, please log in or register
Mary Clark
120
Mary Clark 2016.11.16 05:28 

I have code that work in other strategies, but I had to change this slightly as the order can start off with no StopLoss and no TakeProfit. I think I just need another set of eyes. Thanks in advance. 

 

This is just basic trailing stop code with an initial distance of 8 and a modify increment of say every two pips.  Point has been adjusted and is vPoint = .0001 for GBP/USD. Digits is 5.

Here is what I logged. It is a SELL

0 21:11:48 2016.10.05 18:33  ManageOrder GBPUSD,M15: *** Error Modifying Trailing Stop Ask :1.27541 OpenPrice:1.27659 tsInitial:0.0008 tsSubsequent 0.0002 Order Stop Loss :1.2766 New SL : 1.2764  TP: 0 New TP:1.27121

0 21:11:48 2016.10.05 18:33  ManageOrder GBPUSD,M15: New SL:1.2764 OrderStopLoss:1.2766 true Diff:0.0002

3 21:11:48 2016.10.05 18:33  ManageOrder GBPUSD,M15: OrderModify error 130

 

// check for trailing stop
                  if(TrailingStop>0)
                  {
                    curStop = OrderStopLoss();
                    if (curStop > Ask + tsInitial + tsSubsequent  || (curStop == 0 && (Ask + tsInitial + tsSubsequent) >=OrderOpenPrice() ))
                    {
                        sl = NormalizeDouble(Ask + tsInitial + tsSubsequent, Digits);
                        tp = OrderTakeProfit() == 0 ? (TakeProfit == 0 ? 0 : Ask - TakeProfit*vPoint - tsSubsequent): OrderTakeProfit()-tsSubsequent;
                        tp = NormalizeDouble(tp, Digits);
    
                        if (sl < curStop && MathAbs(NormalizeDouble(sl-curStop,5) != 0))
                        {  
                            
                           Print("New SL:", sl, " OrderStopLoss:", curStop, (sl < curStop ? " true" : " false")," Diff:", MathAbs(NormalizeDouble(sl-curStop,Digits)) );          
                           if(!OrderModify(OrderTicket(),OrderOpenPrice(),sl,tp,0,Green))
                           {
                              Print("Error was : " + GetLastError());
                              Print("*** Error Modifying Trailing Stop Ask :" + Ask +" OpenPrice:" + OrderOpenPrice() + " tsInitial:" + tsInitial + " tsSubsequent " + tsSubsequent + " Order Stop Loss :",curStop," New SL : ",sl,"  TP: ",OrderTakeProfit() + " New TP:" + tp);
                              ResetLastError();
                              errModifyCount++;
                           }
                           else
                              Print("###### Trailing Stop Modified:",TimeToStr(TimeCurrent())," Ask:",Ask," Order Stop Loss :",curStop," New SL : ",sl,"  TP:",OrderTakeProfit());
  
                         }
                     }
                  } // If TrailingStop
Alain Verleyen
Moderator
31085
Alain Verleyen 2016.11.16 08:36  
Mary Clark:

I have code that work in other strategies, but I had to change this slightly as the order can start off with no StopLoss and no TakeProfit. I think I just need another set of eyes. Thanks in advance. 

 

This is just basic trailing stop code with an initial distance of 8 and a modify increment of say every two pips.  Point has been adjusted and is vPoint = .0001 for GBP/USD. Digits is 5.

Here is what I logged. It is a SELL

0 21:11:48 2016.10.05 18:33  ManageOrder GBPUSD,M15: *** Error Modifying Trailing Stop Ask :1.27541 OpenPrice:1.27659 tsInitial:0.0008 tsSubsequent 0.0002 Order Stop Loss :1.2766 New SL : 1.2764  TP: 0 New TP:1.27121

That seems correct while a bit messy.

Are you sure your Ask price is up to date (could need a RefreshRates()) ?

What is the value of STOP_LEVEL ?

Mary Clark
120
Mary Clark 2016.11.16 15:43  

I know I kept adding stuff to deal with the error. Stop Level is 0. Its two pips anyway. Its on a backtest. All my other strategies backtest ok.  

 

I don't usually call Refresh Rates as there are no heavy calculations OnTick. Its check if a new order needs to be opened and then check current orders which there are only one or two.

This is driving me nuts.  But I could try RefreshRates on the error.

 

Thanks. 

Mary Clark
120
Mary Clark 2016.11.16 17:24  
Also. I have real account open on the terminal. So is it using fxcm's server for backtest or metatraders demo server.

I am going to try to use it live and see what happens.
Mladen Rakic
56238
Mladen Rakic 2016.11.16 18:00  

Not sure about fxcm's server, but since it is returning 0 for stop level it might fall in the following description :

A zero value of MODE_STOPLEVEL means either absence of any restrictions on the minimal distance for Stop Loss/Take Profit or the fact that a trade server utilizes some external mechanisms for dynamic level control, which cannot be translated in the client terminal. In the second case, GetLastError() can return error 130, because MODE_STOPLEVEL is actually "floating" here.


Alain Verleyen
Moderator
31085
Alain Verleyen 2016.11.16 18:12  
Mladen Rakic:

Not sure about fxcm's server, but since it is returning 0 for stop level it might fall in the following description :

She said it happens on backtest, so I don't think it's applicable ?
Mladen Rakic
56238
Mladen Rakic 2016.11.16 18:32  
Alain Verleyen:
She said it happens on backtest, so I don't think it's applicable ?
I am not sure, doing some tests, will get back on that
Mladen Rakic
56238
Mladen Rakic 2016.11.16 18:57  
Alain Verleyen:
She said it happens on backtest, so I don't think it's applicable ?

I used this very rough code :

extern double stopLoss=8;
extern double takeProfit=8;

int OnInit() { return(0); }
void OnDeinit(const int reason) { }
void OnTick()
{
   bool dummy;
   if (OrdersTotal()==0)
   {
      int openWhat = MathMod(MathRand(),2);
         if (openWhat==0) dummy = OrderSend(NULL,OP_BUY ,0.1,Ask,10,0,0);
         if (openWhat==1) dummy = OrderSend(NULL,OP_SELL,0.1,Bid,10,0,0);
   }

   //
   //
   //
   //
   //
  
   for (int i=OrdersTotal()-1; i>=0; i--)
      if (OrderSelect(i,SELECT_BY_POS))
      {
         if (iBarShift(NULL,0,OrderOpenTime())!=0)
            {
               ResetLastError();
                  dummy = OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),10);
                  Comment("Closing status : "+(string)GetLastError());
            }                  
         else
         {            
            double pipValue = _Point*MathPow(10,MathMod(_Digits,2));
            ResetLastError();
               if (OrderType()==OP_BUY)  dummy = OrderModify(OrderTicket(),OrderOpenPrice(),Bid-stopLoss*pipValue,Ask+takeProfit*pipValue,0);
               if (OrderType()==OP_SELL) dummy = OrderModify(OrderTicket(),OrderOpenPrice(),Ask+stopLoss*pipValue,Bid-takeProfit*pipValue,0);
               Comment(GetLastError());
         }              
      }
}
But I am not getting any error(s) - neither in back-test nor in live (tested it on FXCM USDDemo01 server) regardless of what (reasonable) settings I use for stop loss and take profit
JC
1455
JC 2016.11.16 19:05  
Mary Clark:

This is driving me nuts. 

With apologies if I'm missing something obvious about your code...

Among the many things which is puzzling me about this is the following:

  • The trailing-stop code which you have posted sets the t/p as well as the s/l
  • In your logging, the existing t/p is reported as zero and therefore the trailing-stop logic has not previously been run
  • Despite this, your log says that the existing s/l is only 0.00001 from the open price:  "OpenPrice:1.27659 ... Order Stop Loss:1.2766"

Your report of the logging says "OrderModify error 130"; it's not clear whether this is verbatim, and whether it is from your code or from MT4. But the code you have posted will write "Error was : 130".

It is possible that you have two blocks of trailing-stop code, and you are looking for a problem in the wrong one? 

Mary Clark
120
Mary Clark 2016.11.17 04:19  

The code above the trailing stop code ( not shown), sets Breakeven. So that is already set but will never get run again. Thats why SL has a value.

 

The whole point of this, is I will open an order by hand and don't set any SL or TP and it uses my settings for StopLoss, TakeProfit (which could be 0) and Trailing Stop. The order for backtesting that gets opened is from a SELL_STOP, that shouldn't matter though.

 

Another strange thing that also happened is when I set breakeven, I usually set it to OpenPrice() +- 1 Point.(works in my other strategies) . It won't let me, I got invalid stop there too. I changed it to just OpenPrice() and it worked, and now the next error is the Trailing Stop Modify invalid stop. Those are the only two modifies that run.. One for breakeven and one for TrailingStop, but I include which one as part of the Error text.

 

El bizarro #3 is I test that the new SL is less than the current StopLoss, otherwise don't do it. But it executes it anyway. I put the errorCount in there, and stop after 3 tries, then waits 60 seconds. Then I get to the point where it is two pips away, and it still won't modify. Is there some algorithm that when an error happens it won't clear on the server or terminal ? 

I have build 1010 Aug 18th 2016. 

Here is that breakeven code. And the log below which shows it tried to set it to the same value, even with the If test.  I am testing it live right now to see if it makes a difference.

                else // go to short position
                {
                   curStop= OrderStopLoss();
  
                   if(SetBreakEvenAt>0 && (curStop>OrderOpenPrice() || curStop == 0) && OrderOpenPrice() - Ask>(vPoint*SetBreakEvenAt)+stopLevel)
                    {
                        double newStop = NormalizeDouble(OrderOpenPrice(), Digits);
                        if(!OrderModify(OrderTicket(),OrderOpenPrice(),newStop,OrderTakeProfit(),0,Green))
                        {
                           Print("*********** Unable to set Breakeven : Open Price:",OrderOpenPrice()," Previous Stop Loss :",OrderStopLoss()," Breakeven :",newStop," Current Ask: ",Ask);
                           errModifyCount++;
                        }
                        else
                        {
                           Print("##### Breakeven Set:",TimeToStr(TimeCurrent())," Ask:",Ask," Order Stop Loss :",OrderStopLoss()," New Stop Loss:",OrderOpenPrice()-stopLevel, " TP:" ,OrderTakeProfit());
                           breakevenSet = true;
                        }
                    
                   }
                  // check for trailing stop
                  if(TrailingStop>0 && !breakevenSet)
                  {

2 20:04:53 2016.10.05 18:30  ManageOrder GBPUSD,M15: modify #4 sell limit 0.01 GBPUSD at 1.27659 sl: 1.27659 tp: 0.00000 ok

0 20:04:53 2016.10.05 18:30  ManageOrder GBPUSD,M15: ##### Breakeven Set:2016.10.05 18:30 Ask:1.2758 Order Stop Loss :0 New Stop Loss:1.2766 TP:0

0 20:04:53 2016.10.05 18:31  ManageOrder GBPUSD,M15: New SL:1.2766 OrderStopLoss:1.2766 true Diff:0

3 20:04:53 2016.10.05 18:31  ManageOrder GBPUSD,M15: OrderModify error 130

0 20:04:53 2016.10.05 18:31  ManageOrder GBPUSD,M15: Error was : 130

0 20:04:53 2016.10.05 18:31  ManageOrder GBPUSD,M15: *** Error Modifying Trailing Stop Ask :1.27558 OpenPrice:1.27659 tsInitial:0.0008 tsSubsequent 0.0002 Order Stop Loss :1.2766 New SL : 1.2766  TP: 0 New TP:1.27138

0 20:04:53 2016.10.05 18:31  ManageOrder GBPUSD,M15: New SL:1.2766 OrderStopLoss:1.2766 true Diff:0

3 20:04:53 2016.10.05 18:31  ManageOrder GBPUSD,M15: OrderModify error 130

0 20:04:53 2016.10.05 18:31  ManageOrder GBPUSD,M15: Error was : 130

0 20:04:53 2016.10.05 18:31  ManageOrder GBPUSD,M15: *** Error Modifying Trailing Stop Ask :1.27557 OpenPrice:1.27659 tsInitial:0.0008 tsSubsequent 0.0002 Order Stop Loss :1.2766 New SL : 1.2766  TP: 0 New TP:1.27137

0 20:04:53 2016.10.05 18:31  ManageOrder GBPUSD,M15: New SL:1.2766 OrderStopLoss:1.2766 true Diff:0

3 20:04:53 2016.10.05 18:31  ManageOrder GBPUSD,M15: OrderModify error 130

0 20:04:53 2016.10.05 18:31  ManageOrder GBPUSD,M15: Error was : 130

0 20:04:53 2016.10.05 18:31  ManageOrder GBPUSD,M15: *** Error Modifying Trailing Stop Ask :1.27556 OpenPrice:1.27659 tsInitial:0.0008 tsSubsequent 0.0002 Order Stop Loss :1.2766 New SL : 1.2766  TP: 0 New TP:1.27136

0 20:04:53 2016.10.05 18:31  ManageOrder GBPUSD,M15: Error Modify Count too high

0 20:04:53 2016.10.05 18:32  ManageOrder GBPUSD,M15: New SL:1.2764 OrderStopLoss:1.2766 true Diff:0.0002

3 20:04:53 2016.10.05 18:32  ManageOrder GBPUSD,M15: OrderModify error 130

0 20:04:53 2016.10.05 18:32  ManageOrder GBPUSD,M15: Error was : 130

0 20:04:53 2016.10.05 18:32  ManageOrder GBPUSD,M15: *** Error Modifying Trailing Stop Ask :1.27537 OpenPrice:1.27659 tsInitial:0.0008 tsSubsequent 0.0002 Order Stop Loss :1.2766 New SL : 1.2764  TP: 0 New TP:1.27117

0 20:04:53 2016.10.05 18:32  ManageOrder GBPUSD,M15: New SL:1.2764 OrderStopLoss:1.2766 true Diff:0.0002

3 20:04:53 2016.10.05 18:32  ManageOrder GBPUSD,M15: OrderModify error 130

0 20:04:53 2016.10.05 18:32  ManageOrder GBPUSD,M15: Error was : 130

0 20:04:53 2016.10.05 18:32  ManageOrder GBPUSD,M15: *** Error Modifying Trailing Stop Ask :1.27538 OpenPrice:1.27659 tsInitial:0.0008 tsSubsequent 0.0002 Order Stop Loss :1.2766 New SL : 1.2764  TP: 0 New TP:1.27118

0 20:04:53 2016.10.05 18:32  ManageOrder GBPUSD,M15: New SL:1.2764 OrderStopLoss:1.2766 true Diff:0.0002


 Thanks for anyones help. I have never seen this before.

Mary Clark
120
Mary Clark 2016.11.17 06:21  
JC:

With apologies if I'm missing something obvious about your code...

Among the many things which is puzzling me about this is the following:

  • The trailing-stop code which you have posted sets the t/p as well as the s/l
  • In your logging, the existing t/p is reported as zero and therefore the trailing-stop logic has not previously been run
  • Despite this, your log says that the existing s/l is only 0.00001 from the open price:  "OpenPrice:1.27659 ... Order Stop Loss:1.2766"

Your report of the logging says "OrderModify error 130"; it's not clear whether this is verbatim, and whether it is from your code or from MT4. But the code you have posted will write "Error was : 130".

It is possible that you have two blocks of trailing-stop code, and you are looking for a problem in the wrong one? 

  • Despite this, your log says that the existing s/l is only 0.00001 from the open price:  "OpenPrice:1.27659 ... Order Stop Loss:1.2766"

Yes look at the Ask at that point in the log. It is 8 pips above open and this is the breakeven set.

 

Thanks. 

12
To add comments, please log in or register