Login Page - Create Account

Support Board


Date/Time: Tue, 22 Apr 2025 16:47:32 +0000



[User Discussion] - Sierra unable to Plot correctly.

View Count: 181

[2025-01-16 11:08:59]
User669552 - Posts: 26
I use a range chart with 8 price levels(7 tick). The objective is to plot a red candle if the POC is in the Upper halve of red candle and plot the the green candle if the POC is in in the lower half. Despite be a very simple study sierra chart plotting incorrectly and missing some. I believe these could be other factor other than the code. Here is the simple code.



#include "sierrachart.h"

// Define the name of the DLL for this custom study
SCDLLName("ApexVolumeAtPriceStudy");

SCSFExport scsf_ApexVolumeAtPriceStudy(SCStudyInterfaceRef sc)
{
// Default settings
if (sc.SetDefaults)
{
sc.GraphName = "Apex Volume at Price Study";
sc.StudyDescription = "Detects conditions for Green and Red Candles based on POC.";
sc.AutoLoop = 0; // We will handle the loops manually

sc.MaintainVolumeAtPriceData = 1; // Ensure volume at price data is maintained
return;
}

// Get the Endidx value (Index of the bar we are analyzing)
int Endidx = sc.ArraySize - 2; // We are analyzing the second-to-last bar (the last loaded bar)

// Initialize variables
const s_VolumeAtPriceV2* p_VAP = nullptr;
int NumPriceLevels = sc.VolumeAtPriceForBars->GetSizeAtBarIndex(Endidx - 1); // Number of price levels for the bar
int VolumeAtEachLevel[NumPriceLevels];

// Retrieve volume data for each price level of the bar at Endidx-1
for (int i = 0; i < NumPriceLevels; i++) {
if (!sc.VolumeAtPriceForBars->GetVAPElementAtIndex(Endidx - 1, i, &p_VAP)) {
break; // If there's an error, stop the loop
}
VolumeAtEachLevel = p_VAP->Volume; // Store the volume for this price level
}

// Find the POC by identifying the price level with the highest volume within the entire range (0 to 7)
int POCIndex = -1;
float MaxVolume = 0.0f; // Change to float

// Loop through all price levels (0 to 7) to find the POC
for (int i = 0; i < NumPriceLevels; i++) {
if (VolumeAtEachLevel > MaxVolume) {
MaxVolume = VolumeAtEachLevel;
POCIndex = i; // The price level with the highest volume is the POC
}
}

// Check if the bar is a Green Candle (Bullish)
if (sc.Close[Endidx] > sc.Open[Endidx]) {
// Green Candle Condition: POC must be within index 0 to 3 (lower half of the candle)
if (POCIndex >= 0 && POCIndex <= 3) {
// Green Candle Condition Met - POC is within 0 to 3
s_UseTool Draw;
Draw.DrawingType = DRAWING_MARKER;
Draw.MarkerType = MARKER_SQUARE;
Draw.Color = RGB(0, 255, 0); // Green color
Draw.MarkerSize = 15;
Draw.BeginValue = sc.Low[Endidx] - (sc.TickSize * 2); // Offset below the bar
Draw.BeginIndex = Endidx;
sc.UseTool(Draw);
}
}

// Check if the bar is a Red Candle (Bearish)
if (sc.Close[Endidx] < sc.Open[Endidx]) {
// Red Candle Condition: POC must be within index 4 to 7 (upper half of the candle)
if (POCIndex >= 4 && POCIndex <= 7) {
// Red Candle Condition Met - POC is within 4 to 7
s_UseTool Draw;
Draw.DrawingType = DRAWING_MARKER;
Draw.MarkerType = MARKER_SQUARE;
Draw.Color = RGB(255, 0, 0); // Red color
Draw.MarkerSize = 15;
Draw.BeginValue = sc.High[Endidx] + (sc.TickSize * 2); // Offset above the bar
Draw.BeginIndex = Endidx;
sc.UseTool(Draw);
}
}
}

[2025-01-16 12:21:34]
User431178 - Posts: 654
Nothing wrong with Sierra plotting, the code has errors, it doesn't even compile if used exactly as shared above.

The title should read "My AI generated code doesn't work, can someone fix it?".
[2025-01-16 12:23:46]
User431178 - Posts: 654
Here is one solution.


SCSFExport scsf_ApexVolumeAtPriceStudy(SCStudyInterfaceRef sc)
{
  auto& sg_Green = sc.Subgraph[0];
  auto& sg_Red = sc.Subgraph[1];
  
  if (sc.SetDefaults)
  {
    sc.GraphName = "Apex Volume at Price Study";
    sc.StudyDescription = "Detects conditions for Green and Red Candles based on POC.";
    sc.AutoLoop = 0;
    sc.GraphRegion = 0;

    sc.MaintainVolumeAtPriceData = 1;

    sg_Green.Name = "Green";
    sg_Green.DrawStyle = DRAWSTYLE_SQUARE;
    sg_Green.PrimaryColor = RGB(0, 255, 0);
    sg_Green.LineWidth = 5;

    sg_Red.Name = "Red";
    sg_Red.DrawStyle = DRAWSTYLE_SQUARE;
    sg_Red.PrimaryColor = RGB(255, 0, 0);
    sg_Red.LineWidth = 5;

    return;
  }

  if (sc.VolumeAtPriceForBars->GetNumberOfBars() < sc.ArraySize)
    return;

  for (auto idx = sc.UpdateStartIndex; idx < sc.ArraySize; ++idx)
  {
    if (sc.GetBarHasClosedStatus(idx) != BHCS_BAR_HAS_CLOSED)
      continue;

    const auto numLevels = sc.VolumeAtPriceForBars->GetSizeAtBarIndex(idx);

    if (numLevels == 0)
      continue;

    const s_VolumeAtPriceV2* p_VAP = nullptr;

    auto pocIdx{ 0 };
    auto pocVol{ 0 };

    for (auto lvl = 0; lvl < numLevels; ++lvl)
    {
      if (!sc.VolumeAtPriceForBars->GetVAPElementAtIndex(idx, lvl, &p_VAP, true))
        continue;

      if (!p_VAP)
        continue;

      if (p_VAP->Volume > pocVol)
      {
        pocVol = p_VAP->Volume;
        pocIdx = lvl;
      }
    }

    if (sc.Close[idx] > sc.Open[idx])
    {
      if (pocIdx >= 0 && pocIdx <= 3)
        sg_Green[idx] = sc.Low[idx] - 2 * sc.TickSize;
    }
    else if (sc.Close[idx] < sc.Open[idx])
    {
      if (pocIdx >= 4 && pocIdx <= 7)
        sg_Red[idx] = sc.High[idx] + 2 * sc.TickSize;
    }
  }
}

[2025-01-16 13:55:56]
cmet - Posts: 690
Ignore the haters. AI is very useful and autonomous agents will take over all coding tasks within 3 years (you can already make a reasonable version of one with Projects). Keep learning.

This is what you're looking for:

#include "sierrachart.h"

SCDLLName("VPOC Candle Coloring")

SCSFExport scsf_VPOCCandleColoring(SCStudyInterfaceRef sc)
{
SCSubgraphRef Subgraph_GreenFill = sc.Subgraph[0];
SCSubgraphRef Subgraph_RedFill = sc.Subgraph[1];

SCInputRef Input_TickRangeTop = sc.Input[0];
SCInputRef Input_TickRangeBottom = sc.Input[1];

if (sc.SetDefaults)
{
sc.GraphName = "VPOC Candle Coloring";
sc.GraphRegion = 0;
sc.AutoLoop = 0;
sc.MaintainVolumeAtPriceData = 1;
sc.DrawZeros = false;

Subgraph_GreenFill.Name = "Green Fill";
Subgraph_GreenFill.DrawStyle = DRAWSTYLE_COLOR_BAR_CANDLE_FILL;
Subgraph_GreenFill.PrimaryColor = RGB(0, 255, 0);
Subgraph_GreenFill.DrawZeros = false;

Subgraph_RedFill.Name = "Red Fill";
Subgraph_RedFill.DrawStyle = DRAWSTYLE_COLOR_BAR_CANDLE_FILL;
Subgraph_RedFill.PrimaryColor = RGB(255, 0, 0);
Subgraph_RedFill.DrawZeros = false;

Input_TickRangeTop.Name = "VPOC Ticks from Top";
Input_TickRangeTop.SetInt(2);
Input_TickRangeTop.SetIntLimits(1, 100);

Input_TickRangeBottom.Name = "VPOC Ticks from Bottom";
Input_TickRangeBottom.SetInt(2);
Input_TickRangeBottom.SetIntLimits(1, 100);

return;
}

float TickSize = sc.TickSize;

for (int i = sc.UpdateStartIndex; i < sc.ArraySize; i++)
{
s_VolumeAtPriceV2 VolumeAtPrice;
sc.GetPointOfControlPriceVolumeForBar(i, VolumeAtPrice);

float VPOC = static_cast<float>(sc.TicksToPriceValue(VolumeAtPrice.PriceInTicks));
float BarHigh = sc.High[i];
float BarLow = sc.Low[i];

Subgraph_GreenFill[i] = 0;
Subgraph_RedFill[i] = 0;

if (VPOC >= BarHigh - (Input_TickRangeTop.GetInt() * TickSize))
{
Subgraph_GreenFill[i] = 1;
}
else if (VPOC <= BarLow + (Input_TickRangeBottom.GetInt() * TickSize))
{
Subgraph_RedFill[i] = 1;
}
}
}

This actually colors bars.

Image has Volume Point of Control for Bars as a reference.

Number of ticks customizable through inputs.
Date Time Of Last Edit: 2025-01-16 13:58:06
imagevpoc_coloring.jpg / V - Attached On 2025-01-16 13:56:28 UTC - Size: 127.14 KB - 36 views

To post a message in this thread, you need to log in with your Sierra Chart account:

Login

Login Page - Create Account