Help me to fix pending order script.

 

I want to place 1 buystop and 1 sellstop pending order and let them close in the next 6 bars, and i don't how.

//buy setup
   if ( OrderType(OP_BUYSTOP)==0)
   {
   OrderSend(NULL,OP_BUYSTOP,0.01,RR,3,RR-20*_Point,0,NULL,0,
0 ,clrGreen);
   }
   
   
   
//sell setup
   if ( OrderType(OP_SELLSTOP)==0)
   {
   OrderSend(NULL,OP_SELLSTOP,0.01,SS,3,SS+20*_Point,0,NULL,0,
0 ,clrRed);
   }
 
  1. Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum?
    Next time, post in the correct place. The moderators will likely move this thread there soon.

  2. OrderSend(NULL,OP_BUYSTOP,0.01,RR,3,RR-20*_Point,0,NULL,0, 0 ,clrGreen);

    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.07.25)

  3. You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit and open at 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).

  4. if ( OrderType(OP_SELLSTOP)==0)

    Do not post code that will not even compile.

    Perhaps you should read the manual. OrderType - Trade Functions - MQL4 Reference
       How To Ask Questions The Smart Way. (2004)
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

  5. Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number/symbol filtering on your OrderSelect / Position select loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum (2013)
              PositionClose is not working - MQL5 programming forum (2020.02.21)
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles (2006)
              Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles 2011

    You need one Magic Number for each symbol/timeframe/strategy. Trade current timeframe, one strategy, and filter by symbol requires one MN.

Reason: