4hr candle close stoploss coding for fx robot

 

Good Day MQL5 community .


Am looking coding for my fx  robot to command the robot  to set a stop-loss right after the 4hr candle closes

I would really appreciate your assistance .

 
How to make a search on the forum/CodeBase/etc
https://www.mql5.com/en/forum/193510
How can I search for indicators and other elements in this forum?
How can I search for indicators and other elements in this forum?
  • 2017.05.29
  • www.mql5.com
How can I search for indicators in this forum? I need the Hodrick Prescott Filter for MT4. Please, if anyone can help me, I thank you in advance...
 

Just to remind:

Coders (any coder) are coding for free:

  • if it is interesting for them personally, or
  • if it is interesting for many members of this forum.

and Freelance section of the forum should be used in most of the cases.

Trading applications for MetaTrader 5 to order
Trading applications for MetaTrader 5 to order
  • 2024.03.02
  • www.mql5.com
The largest freelance service with MQL5 application developers
 
Jotam088:

Good Day MQL5 community .


Am looking coding for my fx  robot to command the robot  to set a stop-loss right after the 4hr candle closes

I would really appreciate your assistance .

I can give you a few ideas on this.

//+------------------------------------------------------------------+
//|                                               Jotam Stoploss.mq5 |
//|                        Copyright 2024, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property strict

// Input parameters

// Global variables
double prev_four_hour_open = 0;
double prev_four_hour_close = 0;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
    // Set stop loss at the close of the 4-hour candle
    SetStopLossAtFourHourClose();

    return INIT_SUCCEEDED;
}

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
    // Place any clean up code here if you need to
}

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
    // This function is called on each tick, if you have code here, don't leave it out
}

//+----------------------------------------------------------------------------+
//| Function to set stop loss at the close of the 4-hour candle                |
//+----------------------------------------------------------------------------+
void SetStopLossAtFourHourClose()  //Here we start with the function you need, SL at end of the 4hr candle
{
    // Calculate stop loss price
    double stop_loss_price = 0;
    if (prev_four_hour_close > 0)
    {
        stop_loss_price = prev_four_hour_close - StopLossDistance * Point;
    }

    // Set stop loss
    if (stop_loss_price > 0)
    {
        bool result = OrderModify(OrderTicket(), 0, stop_loss_price, OrderTakeProfit(), 0, clrNONE);
        if (!result)
            Print("Failed to modify stop loss! Error code:", GetLastError());
        else
            Print("Stop loss set successfully at ", stop_loss_price);
    }
}

//+------------------------------------------------------------------+
//| Function to track the open and close prices of the 4-hour candle |
//+------------------------------------------------------------------+
void OnTimer()
{
    datetime current_time = TimeCurrent();
    datetime last_bar_time = Time[0];
    datetime prev_bar_time = Time[1];

    // Check if the previous bar closed 4 hours ago
    if (TimeToStruct(prev_bar_time).hour % 4 == 0 && TimeToStruct(last_bar_time).hour % 4 != 0)
    {
        // Update previous 4-hour open and close prices
        prev_four_hour_open = Open[1];
        prev_four_hour_close = Close[1];
    }
}

There are other approaches that can be followed, but i think this is quite straight forward. 

Reason: