Can anyone help me please. Strategy tester says error 130 and 138.

 

can anyone help me with my problem. strategy tester doesnt work due to error 130 and error 138.



// Pivot now; 
     double pivota;
     double s1a;
     double r1a;
     double s2a;
     double r2a;
     
     double higha = iHigh(_Symbol,PERIOD_D1,1);
     double lowa = iLow(_Symbol,PERIOD_D1,1);
     double opena = iOpen(_Symbol,PERIOD_D1,1);
     double closea = iClose(_Symbol,PERIOD_D1,1);
     
     pivota = NormalizeDouble(( (higha+ lowa + closea) / 3), Digits);
     
     r1a= NormalizeDouble(((2*pivota)-lowa),_Digits);
     r2a= NormalizeDouble((pivota + (higha - lowa )),_Digits);
     s1a= NormalizeDouble(((2*pivota)- higha),_Digits);
     s2a= NormalizeDouble((pivota - (higha - lowa)),_Digits);
     
     
     
     //PIvot b
     // Pivot now; 
     double pivotb;
     double s1b;
     double r1b;
     double s2b;
     double r2b;
     
     double highb = iHigh(_Symbol,PERIOD_D1,2);
     double lowb = iLow(_Symbol,PERIOD_D1,2);
     double openb = iOpen(_Symbol,PERIOD_D1,2);
     double closeb = iClose(_Symbol,PERIOD_D1,2);
     
     pivotb = NormalizeDouble(( (highb+ lowb + closeb) / 3), Digits);
     
     r1b= NormalizeDouble(((2*pivotb)-lowb),_Digits);
     r2b= NormalizeDouble((pivotb + (highb - lowb )),_Digits);
     s1b= NormalizeDouble(((2*pivotb)- highb),_Digits);
     s2b= NormalizeDouble((pivotb - (highb - lowb)),_Digits);
     
    
    
     
     
    
     
     
     
     if(!CheckIfOpenOrdersByMagicNB(magicNB))//if no open orders try to enter new position
   { 
   double maperiod = 200;
   double movingAverage = iMA(NULL,0,200,0,MODE_EMA,PRICE_CLOSE,0);
     
   
    if((Ask > movingAverage) && (pivota > pivotb))//buying
      {
         Print("Price is above moving average and pivot is buy "  );
         
         Print("Entry Price = " + Ask);
         Print("stop+" + stopLossbuy);
        
         
         double entry = pivota || s1a;
         double stopLossbuy = entry - (0.0020 + atr);
         double takeProfitbuy = r2a;
         
         
        
         int openOrderID = OrderSend(_Symbol,OP_BUY,0.1,entry,100,stopLossbuy,takeProfitbuy,NULL,magicNB);
         if(openOrderID < 0) Alert("order rejected. Order error: " + GetLastError());
      }
       else if ((Bid < movingAverage) && (pivota < pivotb) )//selling
      {
         Print("Price is below 200 EMA and pivot sell , Sending sell order");
         Print("stop=" + stopLosssell);
         
       
         
         double entry = pivota ||  r1a;
         double stopLosssell =  entry +(0.0020+ atr);
         double takeProfitsell = s2a;
         
         
         int openOrderID = OrderSend(NULL,OP_SELL,0.1,entry,100,stopLosssell,takeProfitsell,NULL,magicNB);
         if(openOrderID < 0) Alert("order rejected. Order error: " + GetLastError());
      }
      }
      
else //else if you already have a position, update orders if need too.
      {
      Print("Order already placed from your strategy");
      }
 
I guess that you could only buy at Ask price and sell at Bid price for OP_BUY and OP_SELL. You may need to use OP_BUYLIMIT/OP_BUYSTOP or OP_SELLLIMIT/OP_SELLSTOP if want to set the entry price.
 
  1.          double entry = pivota || s1a;

    Price or distance is nonsense. You need price ± distance

  2.          double stopLossbuy = entry - (0.0020 + atr);

    You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).

  3. r2a= NormalizeDouble((pivota + (higha - lowa )),_Digits);
         ⋮
        double takeProfitbuy = r2a;

    You used NormalizeDouble, It's use is usually wrong, as it is in your case.

    1. Floating point has a infinite number of decimals, it's your not understanding floating point and that some numbers can't be represented exactly. (like 1/10.)
                Double-precision floating-point format - Wikipedia, the free encyclopedia

      See also The == operand. - MQL4 programming forum (2013)

    2. Print out your values to the precision you want with DoubleToString - Conversion Functions - MQL4 Reference.

    3. SL/TP (stops) need to be normalized to tick size (not Point) — code fails on non-currencies.
                On 5Digit Broker Stops are only allowed to be placed on full pip values. How to find out in mql? - MQL4 programming forum (2011)

      And abide by the limits Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial and that requires understanding floating point equality Can price != price ? - MQL4 programming forum (2012)

    4. Open price for pending orders need to be adjusted. On Currencies, Point == TickSize, so you will get the same answer, but it won't work on non-currencies. So do it right.
                Trailing Bar Entry EA - MQL4 programming forum (2013)
                Bid/Ask: (No Need) to use NormalizeDouble in OrderSend - MQL4 programming forum (2012)

    5. Lot size must also be adjusted to a multiple of LotStep and check against min and max. If that is not a power of 1/10 then NormalizeDouble is wrong. Do it right.
                (MT4 2013)) (MT5 2022))

    6. MathRound() and NormalizeDouble() are rounding in a different way. Make it explicit.
                MT4:NormalizeDouble - MQL5 programming forum (2017)
                How to Normalize - Expert Advisors and Automated Trading - MQL5 programming forum (2017)

    7. Prices you get from the terminal are already correct (normalized).

    8. PIP, Point, or Tick are all different in general.
                What is a TICK? - MQL4 programming forum (2014)

 
William Roeder #:
  1. Price or distance is nonsense. You need price ± distance

  2. You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).

  3. You used NormalizeDouble, It's use is usually wrong, as it is in your case.

    1. Floating point has a infinite number of decimals, it's your not understanding floating point and that some numbers can't be represented exactly. (like 1/10.)
                Double-precision floating-point format - Wikipedia, the free encyclopedia

      See also The == operand. - MQL4 programming forum (2013)

    2. Print out your values to the precision you want with DoubleToString - Conversion Functions - MQL4 Reference.

    3. SL/TP (stops) need to be normalized to tick size (not Point) — code fails on non-currencies.
                On 5Digit Broker Stops are only allowed to be placed on full pip values. How to find out in mql? - MQL4 programming forum (2011)

      And abide by the limits Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial and that requires understanding floating point equality Can price != price ? - MQL4 programming forum (2012)

    4. Open price for pending orders need to be adjusted. On Currencies, Point == TickSize, so you will get the same answer, but it won't work on non-currencies. So do it right.
                Trailing Bar Entry EA - MQL4 programming forum (2013)
                Bid/Ask: (No Need) to use NormalizeDouble in OrderSend - MQL4 programming forum (2012)

    5. Lot size must also be adjusted to a multiple of LotStep and check against min and max. If that is not a power of 1/10 then NormalizeDouble is wrong. Do it right.
                (MT4 2013)) (MT5 2022))

    6. MathRound() and NormalizeDouble() are rounding in a different way. Make it explicit.
                MT4:NormalizeDouble - MQL5 programming forum (2017)
                How to Normalize - Expert Advisors and Automated Trading - MQL5 programming forum (2017)

    7. Prices you get from the terminal are already correct (normalized).

    8. PIP, Point, or Tick are all different in general.
                What is a TICK? - MQL4 programming forum (2014)

Hello! Regarding your function DeltaValuePerLot()… I have checked for many pairs currency or non currency together with MODE_TICKVALUE …. They give the same value… correct … where did you find that MODE_TICKVALUE fails? 
 
DannyBass #: They give the same value… correct … where did you find that MODE_TICKVALUE fails? 

Broker dependent. Reported by some users.