Help needed to check on the code

 

simple range breakout but it is not submitting any buy or sell.

void OnTick()

{

    string EndOfOpeningPeriod = "09:30:";

    string CurrentTime = TimeToStr(TimeLocal(), TIME_SECONDS);

    int EndOfOpeningPeriodFound = StringFind(CurrentTime, EndOfOpeningPeriod, 0);

    int HighestCandle = iHighest(_Symbol, _Period, MODE_HIGH, 17, 0);

    int LowestCandle = iLowest(_Symbol, _Period, MODE_LOW, 17, 0);

    double currentClosePrice = iClose(Symbol(),0,0);

    double highestHigh = High[HighestCandle];

    double lowestLow = Low[LowestCandle];

       

    Comment("Current Time: ", CurrentTime);

    

    if (EndOfOpeningPeriodFound != -1)

    {

        ObjectDelete("Rectangle");

        ObjectCreate("Rectangle", OBJ_RECTANGLE, 0, Time[0], highestHigh, Time[17], lowestLow);

        

        if (currentClosePrice > highestHigh)

        {

        //Submit the order

        int ticket = OrderSend(Symbol(), OP_BUY, 0.1, Ask, 3, Bid - (StopLoss * Point), Ask + (TakeProfit * Point), "Buy Order", 123, Blue);

            if (ticket > 0)

            {

                Print("Buy order placed successfully. Ticket: ", ticket);

            }

            else

            {

                Print("Error placing buy order. Error code: ", GetLastError());

            }

        }

        else if (currentClosePrice < lowestLow)

        {

         int ticket = OrderSend(Symbol(), OP_SELL, 0.1, Bid, 3, Ask + (StopLoss * Point), Bid - (TakeProfit * Point), "Sell Order", 123, Red);

            if (ticket > 0)

            {

                Print("Sell order placed successfully. Ticket: ", ticket);

            }

            else

            {

                Print("Error placing sell order. Error code: ", GetLastError());

            }

        }

    }

}

 

Please edit your post to use the CODE button (Alt-S)!

Use the CODE button


All trades open at the same time
All trades open at the same time
  • 2023.03.26
  • www.mql5.com
Good day Could Someone please assist with the following. I would like my EA to open a new trade every 500 points(can be adjusted by user...
 
void OnTick()

{

    string EndOfOpeningPeriod = "09:30:";

    string CurrentTime = TimeToStr(TimeLocal(), TIME_SECONDS);

    int EndOfOpeningPeriodFound = StringFind(CurrentTime, EndOfOpeningPeriod, 0);

    int HighestCandle = iHighest(_Symbol, _Period, MODE_HIGH, 17, 0);

    int LowestCandle = iLowest(_Symbol, _Period, MODE_LOW, 17, 0);

    double currentClosePrice = iClose(Symbol(),0,0);

    double highestHigh = High[HighestCandle];

    double lowestLow = Low[LowestCandle];

       

    Comment("Current Time: ", CurrentTime);

    

    if (EndOfOpeningPeriodFound != -1)

    {

        ObjectDelete("Rectangle");

        ObjectCreate("Rectangle", OBJ_RECTANGLE, 0, Time[0], highestHigh, Time[17], lowestLow);

        

        if (currentClosePrice > highestHigh)

        {

        //Submit the order

        int ticket = OrderSend(Symbol(), OP_BUY, 0.1, Ask, 3, Bid - (StopLoss * Point), Ask + (TakeProfit * Point), "Buy Order", 123, Blue);

            if (ticket > 0)

            {

                Print("Buy order placed successfully. Ticket: ", ticket);

            }

            else

            {

                Print("Error placing buy order. Error code: ", GetLastError());

            }

        }

        else if (currentClosePrice < lowestLow)

        {

         int ticket = OrderSend(Symbol(), OP_SELL, 0.1, Bid, 3, Ask + (StopLoss * Point), Bid - (TakeProfit * Point), "Sell Order", 123, Red);

            if (ticket > 0)

            {

                Print("Sell order placed successfully. Ticket: ", ticket);

            }

            else

            {

                Print("Error placing sell order. Error code: ", GetLastError());

            }

        }

    }

}
 
  1. Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum #25 (2019)
              Forum rules and recommendations - General - MQL5 programming forum (2023)
              Messages Editor

  2. 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? (2017)
    Next time, post in the correct place. The moderators will likely move this thread there soon.

  3. int ticket = OrderSend(Symbol(), OP_BUY, 0.1, Ask, 3, Bid - (StopLoss * Point), Ask + (TakeProfit * Point), "Buy Order", 123, Blue);

    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 at 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, average maximum spread = 134.
      My EURCHF shows average spread = 18 points, average maximum spread = 106.
      (your broker will be similar).
                Is it reasonable to have such a huge spreads (20 PIP spreads) in EURCHF? - General - MQL5 programming forum (2022)

 
William Roeder #:
  1. Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum #25 (2019)
              Forum rules and recommendations - General - MQL5 programming forum (2023)
              Messages Editor

  2. 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? (2017)
    Next time, post in the correct place. The moderators will likely move this thread there soon.

  3. 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 at 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, average maximum spread = 134.
      My EURCHF shows average spread = 18 points, average maximum spread = 106.
      (your broker will be similar).
                Is it reasonable to have such a huge spreads (20 PIP spreads) in EURCHF? - General - MQL5 programming forum (2022)

Apology about that and thanks a lot.