#include "sierrachart.h" SCDLLName("Cummulative Delta Histogram"); SCSFExport scsf_CumulativeDeltaHistogram(SCStudyInterfaceRef sc) { // Subgraphs SCSubgraphRef Subgraph_CumulativeDelta = sc.Subgraph[0]; // Inputs SCInputRef Input_CalculationType = sc.Input[0]; SCInputRef Input_ColoringMode = sc.Input[1]; SCInputRef Input_ResetTime = sc.Input[2]; if (sc.SetDefaults) { // Set the configuration and defaults sc.GraphName = "Cumulative Delta Histogram"; sc.AutoLoop = 1; Subgraph_CumulativeDelta.Name = "Cumulative Delta"; Subgraph_CumulativeDelta.DrawStyle = DRAWSTYLE_BAR; Subgraph_CumulativeDelta.PrimaryColor = RGB(0, 255, 0); // Green for positive delta Subgraph_CumulativeDelta.SecondaryColor = RGB(255, 0, 0); // Red for negative delta Subgraph_CumulativeDelta.SecondaryColorUsed = 1; Subgraph_CumulativeDelta.DrawZeros = true; Input_CalculationType.Name = "Calculation Type"; Input_CalculationType.SetCustomInputStrings("Volume;Up/Down Tick Volume;Trades"); Input_CalculationType.SetCustomInputIndex(0); // Default to Volume Input_ColoringMode.Name = "Coloring Mode"; Input_ColoringMode.SetCustomInputStrings("Based on Zero;Based on Slope"); Input_ColoringMode.SetCustomInputIndex(0); // Default to Based on Zero Input_ResetTime.Name = "Reset Time (HH:MM:SS)"; Input_ResetTime.SetTime(HMS_TIME(9, 30, 0)); // Default to 9:30:00 return; } // Variables SCFloatArrayRef BidVolume = sc.BaseData[SC_BIDVOL]; SCFloatArrayRef AskVolume = sc.BaseData[SC_ASKVOL]; float& CumulativeDelta = sc.GetPersistentFloat(1); // Reset at user-defined time SCDateTime CurrentBarDateTime = sc.BaseDateTimeIn[sc.Index]; SCDateTime ResetTime = Input_ResetTime.GetTime(); if (CurrentBarDateTime.GetTime() >= ResetTime && sc.BaseDateTimeIn[sc.Index - 1].GetTime() < ResetTime) { // Reset cumulative delta at specified time CumulativeDelta = 0.0f; } // Perform calculation based on selected type float CurrentDelta = 0.0f; switch (Input_CalculationType.GetIndex()) { case 0: // Volume CurrentDelta = AskVolume[sc.Index] - BidVolume[sc.Index]; break; case 1: // Up/Down Tick Volume if (sc.Close[sc.Index] > sc.Open[sc.Index]) CurrentDelta = sc.Volume[sc.Index]; else if (sc.Close[sc.Index] < sc.Open[sc.Index]) CurrentDelta = -sc.Volume[sc.Index]; break; case 2: // Trades CurrentDelta = (float)sc.NumberOfTrades[sc.Index]; if (sc.Close[sc.Index] < sc.Open[sc.Index]) CurrentDelta = -CurrentDelta; break; default: CurrentDelta = 0.0f; break; } // Accumulate delta CumulativeDelta += CurrentDelta; // Set subgraph value Subgraph_CumulativeDelta[sc.Index] = CumulativeDelta; // Apply coloring based on selected mode if (Input_ColoringMode.GetIndex() == 0) // Based on Zero { if (CumulativeDelta >= 0) Subgraph_CumulativeDelta.DataColor[sc.Index] = Subgraph_CumulativeDelta.PrimaryColor; else Subgraph_CumulativeDelta.DataColor[sc.Index] = Subgraph_CumulativeDelta.SecondaryColor; } else if (Input_ColoringMode.GetIndex() == 1) // Based on Slope { if (sc.Index > 0 && CumulativeDelta > Subgraph_CumulativeDelta[sc.Index - 1]) Subgraph_CumulativeDelta.DataColor[sc.Index] = Subgraph_CumulativeDelta.PrimaryColor; else Subgraph_CumulativeDelta.DataColor[sc.Index] = Subgraph_CumulativeDelta.SecondaryColor; } }