help with my code i want it to run once per bar/candle but keeps placing multiple trades at the same time and i dont have a clue why? can anyone help me out

 
// Input settings
input double LotSize = 0.01;
input double StopLoss = 0;
input double TakeProfit = 0;

void OnTick()
{
    // Get the open and close prices of the previous bar
    double open1 = iOpen(NULL, PERIOD_CURRENT, 1);
    double close0 = iClose(NULL, PERIOD_CURRENT, 0);

    // Get the close price of the current bar
    double close = Close[0];

    // check if close of previous candle is greater than its open
    if (close0 > open1)
    {
        // open buy order
        int ticket = OrderSend(Symbol(), OP_BUY, LotSize, Ask, 3, StopLoss, TakeProfit, "My EA Buy", 16384, 0, Green);
        if (ticket > 0)
        {
            Print("Buy order opened. Ticket: ", ticket);
        }
        else
        {
            Print("Error opening buy order. Error code: ", GetLastError());
        }
    }
    // check if close of previous candle is less than its open
    else if (close0 < open1)
    {
        // open sell order
        int ticket = OrderSend(Symbol(), OP_SELL, LotSize, Bid, 3, StopLoss, TakeProfit, "My EA Sell", 16384, 0, Red);
        if (ticket > 0)
        {
            Print("Sell order opened. Ticket: ", ticket);
        }
        else
        {
            Print("Error opening sell order. Error code: ", GetLastError());
        }
    }

    // check if close of current candle is less than its open
    if (close0 < Open[1])
    {
        // close all buy orders
        int total = OrdersTotal();
        for (int i=0; i<total; i++)
        {
            if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
            {
                if (OrderType() == OP_BUY)
                {
                    bool result = OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 3);
                    if (result)
                    {
                        Print("Buy order closed. Ticket: ", OrderTicket());
                    }
                    else
                    {
                        Print("Error closing buy order. Error code: ", GetLastError());
                    }
                }
            }
        }
    }
    // check if close of current candle is greater than its open
    else if (close0 > Open[1])
    {
        // close all sell orders
        int total = OrdersTotal();
        for (int i=0; i<total; i++)
        {
            if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
            {
                if (OrderType() == OP_SELL)
                {
                    bool result = OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 3);
                    if (result)
                    {
                        Print("Sell order closed. Ticket: ", OrderTicket());
                    }
                    else
                    {
                        Print("Error closing sell order. Error code: ", GetLastError());
                    }
                }
            }
        }
    }
}

 

Forum on trading, automated trading systems and testing trading strategies

icustom function in expert 0.0 problem

Fernando Carreiro, 2023.02.13 18:29

Please edit your post (don't create a new post) and replace your code properly (with "</>" or Alt-S), or attach the original file directly with the "+ Attach file" button below the text box.

NB! Very important! DO NOT create a new post. EDIT your original post. An also remove all unnecessary white-space from your code.


 
Learn to search: NewBar => many solutions :)
 
Ignautt Thahane: help with my code i want it to run once per bar/candle but keeps placing multiple trades at the same time and i dont have a clue why? can anyone help me out
  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 (2019)
              Messages Editor

  2. You posted no question(s). A title should be short and about the subject.

  3. If you had used this, you would have found your answer.
        How can I search for indicators and other elements in this forum? - MQL5 programming forum #1 (2017) #10 (2021)
        How To Ask Questions The Smart Way. (2004)
          How To Interpret Answers.
              RTFM and STFW: How To Tell You've Seriously Screwed Up.

  4. Help you with what? You haven't stated a problem, you stated a want. Show us your attempt (using the CODE button) and state the nature of your difficulty.
              No free help (2017)

    Or pay someone. Top of every page is the link Freelance.
              Hiring to write script - General - MQL5 programming forum (2018)

    We're not going to code it for you (although it could happen if you are lucky or the issue is interesting).
              No free help (2017)

 

Code Base

Detecting the start of a new bar or candle

Fernando Carreiro, 2022.04.24 00:38

Detecting the start of a new bar or candle, in the OnTick() event handler of an expert advisor.
 
// Input settings
input double LotSize = 0.01;
input double StopLoss = 0;
input double TakeProfit = 0;

void OnTick()
{
    // Get the open and close prices of the previous bar
    double open1 = iOpen(NULL, PERIOD_CURRENT, 1);
    double close0 = iClose(NULL, PERIOD_CURRENT, 0);

    // Get the close price of the current bar
    double close = Close[0];

    // check if close of previous candle is greater than its open
    if (close0 > open1)
    {
        // open buy order
        int ticket = OrderSend(Symbol(), OP_BUY, LotSize, Ask, 3, StopLoss, TakeProfit, "My EA Buy", 16384, 0, Green);
        if (ticket > 0)
        {
            Print("Buy order opened. Ticket: ", ticket);
        }
        else
        {
            Print("Error opening buy order. Error code: ", GetLastError());
        }
    }
    // check if close of previous candle is less than its open
    else if (close0 < open1)
    {
        // open sell order
        int ticket = OrderSend(Symbol(), OP_SELL, LotSize, Bid, 3, StopLoss, TakeProfit, "My EA Sell", 16384, 0, Red);
        if (ticket > 0)
        {
            Print("Sell order opened. Ticket: ", ticket);
        }
        else
        {
            Print("Error opening sell order. Error code: ", GetLastError());
        }
    }

    // check if close of current candle is less than its open
    if (close0 < Open[1])
    {
        // close all buy orders
        int total = OrdersTotal();
        for (int i=0; i<total; i++)
        {
            if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
            {
                if (OrderType() == OP_BUY)
                {
                    bool result = OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 3);
                    if (result)
                    {
                        Print("Buy order closed. Ticket: ", OrderTicket());
                    }
                    else
                    {
                        Print("Error closing buy order. Error code: ", GetLastError());
                    }
                }
            }
        }
    }
    // check if close of current candle is greater than its open
    else if (close0 > Open[1])
    {
        // close all sell orders
        int total = OrdersTotal();
        for (int i=0; i<total; i++)
        {
            if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
            {
                if (OrderType() == OP_SELL)
                {
                    bool result = OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 3);
                    if (result)
                    {
                        Print("Sell order closed. Ticket: ", OrderTicket());
                    }
                    else
                    {
                        Print("Error closing sell order. Error code: ", GetLastError());
                    }
                }
            }
        }
    }
}
 
Did you not read? ... NB! Very important! DO NOT create a new post. EDIT your original post.
Reason: