can I get someone to make some suggestions to fix an OrderSend error please

 
input int TakeProfit = 10;                                                // hard set number (converted to points in EA)
input int pipsOffset = 5;                                              //Number of pips above or below candle for entry and exit
input double Lotsize = 0.01;                                       // lot size to trade when opening an order
input int slippage = 3;                                                 //  as mentioned slippage
input int candleCount = 5;                                              // Number of candles to create swing high and low

double bPoint; 
int ticket;

////////////

// Detect 3/5 digit brokers for Point

if (Point == 0.00001) { 
        bPoint = 0.0001; 
                }
else {
if (Point == 0.001){ 
bPoint = 0.01; 
}

else bPoint = Point; 
}

///////

void OnTick()
{
    
    // Count the number of open orders
         int openOrders = OrdersTotal();

      // Check if the number of open orders is less than the maximum allowed
         if(openOrders >= maxOpenTrades){
         return;
                 
         }
    else {
    
// Check for a Signal Candle to be true from long term chart
if (IsSignalCandle() == true){

    Print("Signal Candle is true");

    // If true, Calculate the highest closing price of the last `candleCount` candles
    double highPrice = NormalizeDouble(High[iHighest(Symbol(), shortTimeFrame, MODE_HIGH, candleCount, 1)]+ pipsOffset * bPoint,Digits);
    Print("highPrice: ", highPrice);

    // Validate stop loss values
    double minStopLoss = NormalizeDouble(MarketInfo(Symbol(), MODE_STOPLEVEL) * bPoint,Digits);
    Print("minStopLoss: ", minStopLoss);

    double stopLossLong = NormalizeDouble(iLow(Symbol(), shortTimeFrame, candleCount) - (pipsOffset * bPoint), Digits);
    Print("stopLossLong: ", stopLossLong);

    if(stopLossLong >= minStopLoss) {
        // The stop loss is valid
        Print("Stop loss is valid");
    } 
    else {
        // The stop loss is not valid, print an error message
        Print("Error: Invalid stop loss value. Number 101");
    }
    
      ticket = OrderSend(Symbol(), OP_BUYSTOP, Lotsize, highPrice, slippage * bPoint, 0, 0, EAname, magicNumber, 0, Green);
    
    
    // Debugging code
Print("Symbol: ", Symbol());
Print("Order type: ", "OP_BUYSTOP");
Print("Lot size: ", Lotsize);
Print("highPrice: ", highPrice);
Print("Slippage: ", slippage * bPoint);
Print("Stop loss: ", 0);
Print("Take profit: ", 0);
Print("EAname: ", EAname);
Print("magicNumber: ", magicNumber);
Print("Order expiration time: ", 0);
Print("Order color: ", Green);
    
    Print("ticket: ", ticket);
    Print("Order open result: ", PrintError(GetLastError()));
    
        // Check the return value
        if(ticket < 0) {
            // Print the error code and message
            Print("Error placing buy stop order: ", GetLastError());

and here is the results of my debugging:   

2023.02.08 17:15:13.844 2023.01.31 03:53:50  sxcalp AUDUSD.a,M1: Error placing sell stop order: Invalid trade parameters are set

2023.02.08 17:15:13.844 2023.01.31 03:53:50  sxcalp AUDUSD.a,M1: OrderSend error 3

2023.02.08 17:15:13.844 2023.01.31 03:53:50  sxcalp AUDUSD.a,M1: Error: Invalid stop loss value. Stop loss must be greater than or equal to 0.0

2023.02.08 17:15:13.844 2023.01.31 03:53:50  sxcalp AUDUSD.a,M1: Error placing buy stop order: 0

2023.02.08 17:15:13.844 2023.01.31 03:53:50  sxcalp AUDUSD.a,M1: Order open result: Invalid trade parameters are set

2023.02.08 17:15:13.844 2023.01.31 03:53:50  sxcalp AUDUSD.a,M1: ticket: -1

2023.02.08 17:15:13.844 2023.01.31 03:53:50  sxcalp AUDUSD.a,M1: Order color: clrGreen

2023.02.08 17:15:13.844 2023.01.31 03:53:50  sxcalp AUDUSD.a,M1: Order expiration time: 3600

2023.02.08 17:15:13.844 2023.01.31 03:53:50  sxcalp AUDUSD.a,M1: magicNumber: 123456

2023.02.08 17:15:13.844 2023.01.31 03:53:50  sxcalp AUDUSD.a,M1: EAname: Zorro V1.0

2023.02.08 17:15:13.844 2023.01.31 03:53:50  sxcalp AUDUSD.a,M1: Take profit: 0

2023.02.08 17:15:13.844 2023.01.31 03:53:50  sxcalp AUDUSD.a,M1: Stop loss: 0

2023.02.08 17:15:13.844 2023.01.31 03:53:50  sxcalp AUDUSD.a,M1: Slippage: 0.0003

2023.02.08 17:15:13.844 2023.01.31 03:53:50  sxcalp AUDUSD.a,M1: highPrice: 0.70678

2023.02.08 17:15:13.844 2023.01.31 03:53:50  sxcalp AUDUSD.a,M1: Lot size: 0.01

2023.02.08 17:15:13.844 2023.01.31 03:53:50  sxcalp AUDUSD.a,M1: Order type: OP_BUYSTOP

2023.02.08 17:15:13.844 2023.01.31 03:53:50  sxcalp AUDUSD.a,M1: OrderSend error 3

2023.02.08 17:15:13.844 2023.01.31 03:53:50  sxcalp AUDUSD.a,M1: Stop loss is valid

2023.02.08 17:15:13.844 2023.01.31 03:53:50  sxcalp AUDUSD.a,M1: stopLossLong: 0.70567

2023.02.08 17:15:13.844 2023.01.31 03:53:50  sxcalp AUDUSD.a,M1: minStopLoss: 0.0

2023.02.08 17:15:13.844 2023.01.31 03:53:50  sxcalp AUDUSD.a,M1: highPrice: 0.70678

2023.02.08 17:15:13.844 2023.01.31 03:53:50  sxcalp AUDUSD.a,M1: Signal Candle is true


I am a total newbie with coding and have been back and forth with Int's and doubles.  I thought it was slippage causing an issue, then I thought it was stoploss.  See additional code in the above that isnt necessary, but there to attempt to ensure a valid stop loss value.

Any assistance is appreciated.  


Regards

GMC

 
Is your entry price higher than Current Price? You should print also current price tougher with those data’s 
 

Thanks for the response Daniel.  I added some debugging code to check your suggestion - shame it didnt print to the journal!!

  // If true, Calculate the highest closing price of the last `candleCount` candles
    double highPrice = NormalizeDouble(High[iHighest(Symbol(), shortTimeFrame, MODE_HIGH, candleCount, 1)]+ pipsOffset * bPoint,Digits);
    Comment("highPrice: ", highPrice);
    Comment("Current price: ", MarketInfo(Symbol(), MODE_ASK));
   
   if(highPrice > MarketInfo(Symbol(), MODE_ASK)){
   //entry price is above current price, which is what is expected
      Comment("Entry price is above current price");
      }
      else {
      // Something is wrong
      Comment("Something is wrong as entry price is not higher than current price!!");
      }

I tried both comment and print.

I am still working on it, as I feel you may be onto something.  I asked my broker if they had minimums for Take Profit and Stop Loss in case that was my issue as well.  They are to get back to me.  I have tried the EA on another broker and the same errors occur (doesnt tell me a lot as they may also have minimums.


I'll report back once I can get some valid error info


G

 
  1.     double highPrice = NormalizeDouble(High[iHighest(Symbol(), shortTimeFrame, MODE_HIGH, candleCount, 1)]+ pipsOffset * bPoint,Digits);
    

    The predefined arrays are the current chart. Your iHighest is using the shortTimeFrame. You are mixing apples and oranges.

  2. ticket = OrderSend(Symbol(), OP_BUYSTOP, Lotsize, highPrice, slippage * bPoint, 0, 0, EAname, magicNumber, 0, Green);

    Perhaps you should read the manual. The slippage parameter is specified in points, not in PIP distance.
       How To Ask Questions The Smart Way. (2004)
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

 
William Roeder #:
  1. The predefined arrays are the current chart. Your iHighest is using the shortTimeFrame. You are mixing apples and oranges.

  2. Perhaps you should read the manual. The slippage parameter is specified in points, not in PIP distance.
       How To Ask Questions The Smart Way. (2004)
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

Thank you for the feedback William Roeder.  I have changed the iHighest array to set timeframe to 0 and the slippage value has been changed as well.   I am still getting an Error:  Invalid Trade Paramenters are set.  OrderSend error 3.  


Updated code:  

// Check for a Signal Candle to be true from long term chart
if (IsSignalCandle() == true){

    Print("Signal Candle is true");

    // If true, Calculate the highest closing price of the last `candleCount` candles
    double highPrice = NormalizeDouble(High[iHighest(Symbol(),0, MODE_HIGH, candleCount, 1)]+ pipsOffset * bPoint,Digits);
    Print("highPrice: ", highPrice);

    // Validate stop loss values
    double minStopLoss = NormalizeDouble(MarketInfo(Symbol(), MODE_STOPLEVEL) * bPoint,Digits);
    Print("minStopLoss: ", minStopLoss);
    double stopLossLong = NormalizeDouble(iLow(Symbol(), 0, candleCount) - pipsOffset * bPoint, Digits);
    Print("stopLossLong: ", stopLossLong);

    if(stopLossLong >= minStopLoss) {
        // The stop loss is valid
        Print("Stop loss is valid");
    } 
    else {
        // The stop loss is not valid, print an error message
        Print("Error: Invalid stop loss value. Stop loss must be greater than or equal to ", minStopLoss);
    }
    
    int ticketLong = OrderSend(NULL, OP_BUYSTOP, Lotsize, highPrice, slippage, 0, 0, EAname, magicNumber, 3600, clrLimeGreen);
    
    
    // Debugging code
Comment("Symbol: ", Symbol());
Print("Order type: ", "OP_BUYSTOP");
Print("Lot size: ", Lotsize);
Print("highPrice: ", highPrice);
Print("Slippage: ", slippage);
Print("Stop loss: ", 0);
Print("Take profit: ", 0);
Print("EAname: ", EAname);
Print("magicNumber: ", magicNumber);
Print("Order expiration time: ", 3600);
Print("Order color: ", "clrGreen");
    
    Print("ticket: ", ticketLong);
    Print("Order open result: ", PrintError(GetLastError()));
    
        // Check the return value
        if(ticketLong < 0) {
            // Print the error code and message
            Print("Error placing buy stop order: ", GetLastError());
 
Gavin McLeod #:

I am still working on it, as I feel you may be onto something.  I asked my broker if they had minimums for Take Profit and Stop Loss in case that was my issue as well.  They are to get back to me.  I have tried the EA on another broker and the same errors occur (doesnt tell me a lot as they may also have minimums.


To avoid that, you should check you stops against that

SymbolInfoInteger(_Symbol,SYMBOL_TRADE_STOPS_LEVEL)

You should print Current price toghether with your  "highprice" to confirm 

 
Gavin McLeod #:   OrderSend error 3.  
int ticketLong = OrderSend(NULL, OP_BUYSTOP, Lotsize, highPrice, slippage, 0, 0, EAname, magicNumber, 3600, clrLimeGreen);

Be careful with NULL.

  1. On MT4, you can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, iCustom does, MarketInfo does not, OrderSend does not.
  2. Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
  3. Zero is the same as PERIOD_CURRENT which means _Period. Don't hard code numbers.
  4. MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].
  5. Cloud Protector Bug? - MQL4 programming forum (2020)
 

Thank you both for helping me make my code better, I have solved the ordersend error, it was with my time to expiry variable.

int ticketLong = OrderSend(Symbol(), OP_BUYSTOP, Lotsize, highPrice, slippage, 0, 0, EAname, magicNumber, TimeCurrent() + Secstoexp, clrLimeGreen);

I needed to add the TimeCurrent() to allow MT4 to get a time reference, added a global variable to add the seconds to that.


Thanks again for the suggestions, all of this makes the learning easier.


Now, onto my ordermodify error 130 :)


GMc

Reason: