//+------------------------------------------------------------------+ //| CumulativeDelta.mq5 | //| Copyright © 2023, YourName | //| | //+------------------------------------------------------------------+ #property copyright "YourName" #property link "https://www.yourwebsite.com" #property version "1.00" // #include "../Libraries/x-saherelm.common.lib.mq5" // #property indicator_separate_window #property indicator_buffers 3 #property indicator_plots 3 // //--- Plot settings for Line #property indicator_label1 "Cumulative Delta" #property indicator_type1 DRAW_LINE #property indicator_color1 clrYellow // //--- Plot settings for Histogram #property indicator_label2 "Delta" #property indicator_type2 DRAW_HISTOGRAM #property indicator_color2 clrGray // //--- Plot settings for Moving Average line #property indicator_label3 "Signal" #property indicator_type3 DRAW_LINE #property indicator_color3 clrOrchid // //--- Input parameters input int MAPeriod = 14; // Period for the moving average // //--- Indicator buffers double MABuffer[]; double DeltaBuffer[]; double CumulativeDeltaBuffer[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { // // Attach buffer to the indicator SetIndexBuffer(2, MABuffer, INDICATOR_DATA); SetIndexBuffer(1, DeltaBuffer, INDICATOR_DATA); SetIndexBuffer(0, CumulativeDeltaBuffer, INDICATOR_DATA); IndicatorSetString(INDICATOR_SHORTNAME, "Cumulative Delta"); // 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[] // ) { // // Start from the first uncalculated bar int start = MathMax(prev_calculated - 1, 0); // // Loop through bars for (int i = start; i < rates_total; i++) { // // Calculate Delta based on bar-level data double delta = 0.0; if (close[i] > open[i]) { delta = (double)tick_volume[i]; // Buying pressure (bullish bar) } else if (close[i] < open[i]) { delta = -(double)tick_volume[i]; // Selling pressure (bearish bar) } // // Accumulate cumulative delta if (i == 0) { // DeltaBuffer[i] = delta; CumulativeDeltaBuffer[i] = delta; // First bar starts with delta } else { // DeltaBuffer[i] = DeltaBuffer[i - 1] + delta; CumulativeDeltaBuffer[i] = CumulativeDeltaBuffer[i - 1] + delta; } // // Calculate Moving Average of Cumulative Delta if (i >= MAPeriod - 1) { // double sum = 0.0; for (int j = 0; j < MAPeriod; j++) { sum += CumulativeDeltaBuffer[i - j]; } // MABuffer[i] = sum / MAPeriod; // Simple Moving Average } else { MABuffer[i] = EMPTY_VALUE; // Not enough data for MA } } // return (rates_total); } //+------------------------------------------------------------------+