how to reset calculations for each trading session?

 

Hi, I have been trying to code the standard deviation for a VWAP indicator i found but it wasn't plotting the deviation bands, and someone in my previous post said if I understood the code and they were right, so I decided to write a VWAP indicator from scratch, but my code isn't plotting either then i printed the values to see if there's an error but they are too high it seems that the reason i dont see the plot is because is too high?, my guess is that the code isn't resetting at the beginning of the session.


//+------------------------------------------------------------------+
//|                                                    Kris VWAP.mq5 |
//|                                  Copyright 2023, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers        2
#property indicator_plots          1

#property indicator_label1         "VWAP Session"
#property indicator_type1          DRAW_COLOR_LINE
#property indicator_color1         clrYellow
#property indicator_style1         STYLE_SOLID
#property indicator_width1         2

//--- Define variables
datetime    prev_time      = 0;      // to check bar time
double      VWAP           = 0;
double      Sum_Price_Vol  = 0;      // the sum of the average price multiplied by volume
long        Sum_Vol        = 0;      // the sum of the volume
double      VWAPBuffer[];
double      AvPrice[];               // average price
double      price_vol[];             // the total traded price volume

datetime    session_start  = 0;      

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
     IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
     SetIndexBuffer(0,VWAPBuffer,INDICATOR_DATA);
     SetIndexBuffer(1,VWAPBuffer,INDICATOR_CALCULATIONS);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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[])
  {
//---
     ArrayResize(VWAPBuffer, rates_total);
     ArrayResize(AvPrice, rates_total);
     ArrayResize(price_vol, rates_total);
          
     // check for the first call to OnCalculate()
     if (prev_calculated == 0)
     {
          // initialize session_start variable
          session_start = TimeTradeServer();
     }

     // loop through bars
     for (int i = 0; i < rates_total; i++)
     {          
          // check if new session
          if (TimeTradeServer() - session_start >= PERIOD_D1)
          {
               Sum_Price_Vol = 0;
               Sum_Vol       = 0;
               session_start = TimeCurrent();
          }
          
          // check if new bar
          if (time[i] != prev_time)
          {
               // Calculate average price
               AvPrice[i] = (high[i] + low[i] + close[i]) / 3;
               
               if (tick_volume[i])
               {          
                    // calculate the price volume
                    price_vol[i] = AvPrice[i] * tick_volume[i];          
                    // accumulate total price volume and total volume
                    Sum_Price_Vol += price_vol[i];
                    Sum_Vol       += tick_volume[i];
               }
               else if (volume[i])
               {
                    // calculate the price volume
                    price_vol[i] = AvPrice[i] * volume[i];          
                    // accumulate total price volume and total volume
                    Sum_Price_Vol += price_vol[i];
                    Sum_Vol       += volume[i];
               }
               Print("average price = ", AvPrice[i]);
               Print("Tick volume = ", tick_volume[i]);
               Print("price vol = ", price_vol[i]);
               Print("Sum Vol = ", Sum_Vol);
          
               // calculate the VWAP
               if (Sum_Vol > 0)
               {
                    VWAP = Sum_Price_Vol / Sum_Vol;
               }
          
               // plot VWAP
               VWAPBuffer[i] = VWAP;
               Print("VWAP buffer = ", VWAPBuffer[i]);
          
               // update previous time
               prev_time = time[i];
          }
          else
          {
               // copy previous VWAP value
               VWAPBuffer[i] = VWAPBuffer[i-1];
          }
          
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
 
Krimelio:

Hi, I have been trying to code the standard deviation for a VWAP indicator i found but it wasn't plotting the deviation bands, and someone in my previous post said if I understood the code and they were right, so I decided to write a VWAP indicator from scratch, but my code isn't plotting either then i printed the values to see if there's an error but they are too high it seems that the reason i dont see the plot is because is too high?, my guess is that the code isn't resetting at the beginning of the session.


TimeTradeServer() - session_start >= PeriodSeconds(PERIOD_D1)
Reason: