CodeRe: Reset indicator at a given time every day in mt5

2
There seems to be no interest in helping me with my code, so I thought I would print the code I'm trying to alter.

Before everybody starts to copy the code I need to tell you that its just a shell for plotting the custom Delta indicator as ohlc candles in a separate window, thus making it a Cumulative Delta Volume indicator. And no I cant give out the Custom Delta indicator that's in the code.

I'm trying to make 2 things with this code, first make it start over from 0 every day and second calculate and plot a moving average of the candles in the code.
But I'm having problems with both. Its easy to just drop a moving average over the indicator on chart, but I need the buffer for it.

Any ideas would be welcome, and yes I have made efforts in making it work but I have failed every time.


Here is the original code:

Code: Select all

               
#property indicator_separate_window
#property indicator_buffers 5
#property indicator_plots   1
#property indicator_type1   DRAW_COLOR_CANDLES
#property indicator_color1  clrGreen, clrCrimson
#property indicator_label1  "Delta Open;Delta High;Delta Low;Delta Close"

// Indicator buffers
double ExtOBuffer[];
double ExtHBuffer[];
double ExtLBuffer[];
double ExtCBuffer[];
double ExtColorBuffer[];

// Delta buffers
double DeltaBuf[];
double BuyBuf[];
double SellBuf[];

input datetime inpHistoryDate=D'2022.01.01'; 

void OnInit()
{
    SetIndexBuffer(0, ExtOBuffer, INDICATOR_DATA);
    SetIndexBuffer(1, ExtHBuffer, INDICATOR_DATA);
    SetIndexBuffer(2, ExtLBuffer, INDICATOR_DATA);
    SetIndexBuffer(3, ExtCBuffer, INDICATOR_DATA);
    SetIndexBuffer(4, ExtColorBuffer, INDICATOR_COLOR_INDEX);

}

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
    int Delta;

    Delta = iCustom(NULL, PERIOD_CURRENT,"Delta_Indicator",inpHistoryDate); //Delta_Close
    if  (CopyBuffer(Delta, 0, 0, rates_total, DeltaBuf) != rates_total) return 0;    
    
    Delta = iCustom(NULL, PERIOD_CURRENT,"Delta_Indicator",inpHistoryDate); //Delta_Buy
    if  (CopyBuffer(Delta, 2, 0, rates_total, BuyBuf) != rates_total) return 0;

    Delta = iCustom(NULL, PERIOD_CURRENT,"Delta_Indicator",inpHistoryDate); //Delta_Sell
    if  (CopyBuffer(Delta, 3, 0, rates_total, SellBuf) != rates_total) return 0;

    // Preliminary calculations.
    int limit;
    if (prev_calculated <= 1)
    {
        // Set the first candle.
        ExtLBuffer[0] = 0;
        ExtHBuffer[0] = 0;
        ExtOBuffer[0] = 0;
        ExtCBuffer[0] = 0;
        limit = 1;
    }
    else limit = prev_calculated - 1;

    // The main loop of calculations.
    for (int i = limit; i < rates_total; i++)
    {
            ExtOBuffer[i] = ExtCBuffer[i-1];
            ExtLBuffer[i] = ExtCBuffer[i-1];
            ExtHBuffer[i] = ExtCBuffer[i-1];
            ExtCBuffer[i] = ExtCBuffer[i-1];
            
        if (BuyBuf[i] - SellBuf[i] < 0)
        {
            ExtOBuffer[i] = ExtCBuffer[i-1];
            ExtLBuffer[i] = ExtCBuffer[i-1]-DeltaBuf[i];
            ExtHBuffer[i] = ExtCBuffer[i-1];
            ExtCBuffer[i] = ExtCBuffer[i-1]-DeltaBuf[i];
            
            ExtColorBuffer[i] = 1.0;
        }

        else if (BuyBuf[i] - SellBuf[i] > 0)
        {
            ExtOBuffer[i] = ExtCBuffer[i-1];
            ExtLBuffer[i] = ExtCBuffer[i-1];
            ExtHBuffer[i] = ExtCBuffer[i-1]+DeltaBuf[i];
            ExtCBuffer[i] = ExtCBuffer[i-1]+DeltaBuf[i];
            
            ExtColorBuffer[i] = 0.0;
        }
    }

    return rates_total;
}
//+------------------------------------------------------------------+
here is how it looks, the problem is it just adds the delta from previous candle and so on.
Attachments

Re: Reset indicator at a given time every day in mt5

3
So...

I have tried to use this code as a restart of the ohlc values, but with no luck.

Code: Select all

int index=0;
     while (iTime(_Symbol,PERIOD_CURRENT,index+1)>=iTime(_Symbol,PERIOD_D1,0))
     {
     index++;
     }
    double close_firstCurrent_today=iClose(_Symbol,PERIOD_CURRENT,index);
//--    
    if(close_firstCurrent_today!=iClose(_Symbol,PERIOD_CURRENT,i))
    
    //My ohlc plotting here...
    
    if(close_firstCurrent_today==iClose(_Symbol,PERIOD_CURRENT,i))
    
   //My ohlc plotting here with 0 value...

I have used a similar code, which also failed

[code]
int FirstBar()
  {
   MqlRates rates_D1[],rates_Current[];
   ArraySetAsSeries(rates_D1,true);
   ArraySetAsSeries(rates_Current,true);
   int start_pos=0,count=1;
   
   if((CopyRates(Symbol(),PERIOD_D1,start_pos,count,rates_D1)==count)
   && (CopyRates(Symbol(),PERIOD_CURRENT,rates_D1[0].time,count,rates_Current)==count))
   return true;
//---
   return false;
  }  

   if(!FirstBar())
   //My ohlc plotting here...
   
   if(FirstBar())
   
   //My ohlc plotting here with 0 value...
   

I'm guessing that I need to reset the Barcount in OnCalculate() for every day instead.
I found a code that does that, but I haven't tested it yet.

For the Moving Average I have tried giving it a handle in OnInit(), but then it will only calculate it on the raw Delta values and not the ohlc values as the indicator plots.
Any suggestions?

Re: Reset indicator at a given time every day in mt5

4
It looks like you are trying to reset the OHLC values at a specific time every day. However, the code you posted does not contain any logic for resetting the values.

One way to achieve this is to use the `TimeHour()` and `TimeMinute()` functions to check the current time and compare it to the desired reset time. If the current time is equal to or greater than the reset time, you can reset the OHLC values to 0.

Here's an example of how you can modify your code to implement this reset logic:

```cpp
int resetHour = 0; // Reset hour in 24-hour format
int resetMinute = 0; // Reset minute

int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
// Check if it's time to reset the OHLC values
if (TimeHour(TimeCurrent()) >= resetHour && TimeMinute(TimeCurrent()) >= resetMinute)
{
for (int i = 0; i < rates_total; i++)
{
ExtOBuffer = 0;
ExtHBuffer = 0;
ExtLBuffer = 0;
ExtCBuffer = 0;
}
}

// Rest of your code...
}
```

In this example, the `resetHour` and `resetMinute` variables store the desired reset time. Inside the `OnCalculate` function, we check if the current hour and minute are equal to or greater than the reset time. If so, we loop through all the elements of the OHLC buffers and set them to 0.

Note that this code assumes you are using the local time of your trading platform. If you need to use a different time zone, you can adjust the logic accordingly.


Who is online

Users browsing this forum: Amazon [Bot] and 19 guests