Login Page - Create Account

Support Board


Date/Time: Thu, 28 Nov 2024 23:52:18 +0000



Post From: ACSIL help please. :)

[2023-03-31 18:56:02]
TriStar Trading - Posts: 142
Here is the finished product. Thanks again for the help. Likely there are more efficient ways to do this. I'm newer to c++. Started coding with COBOL. LOL! Mike
// Description:  This study looks back the inputed number of bars to find the highest
//        positive delta or the lowest negative delta for a price bar. It compares only a
//        current positive delta bar to positive delta bars or a current negative delta bar
//        to negative delta bars. If the current price bar has positive delta
//        greater than any individual price bar positive delta in the lookback range the
//        current bar is deemed extreme positive delta. If the current price bar has negative
//        delta less than any individual price bar negative delta in the lookback range the
//        current bar is deemed extreme negative delta. Also, if a current price bar is an up bar
//        with negative delta a negative divergence signal is set. If a current price bar is a
//        down bar with positive delta a positive divergence signal is set.

// This line is required at the top of every source code file.
#include "sierrachart.h"

// This line is required. The name of the group of custom studies.
SCDLLName("TriStar_ExtDeltaDifSignals_v3")

// This line is required. The name of the custom study.
SCSFExport scsf_ExtDeltaDifSignals_v3(SCStudyInterfaceRef sc)
{
  // Section 1 - Set the configuration variables and defaults.
  SCSubgraphRef PosDeltaExtreme   = sc.Subgraph[0];
  SCSubgraphRef NegDeltaExtreme  = sc.Subgraph[1];
  SCSubgraphRef ZeroLine       = sc.Subgraph[2];
  SCSubgraphRef BullDivergence   = sc.Subgraph[3];
  SCSubgraphRef BearDivergence   = sc.Subgraph[4];
  SCSubgraphRef PosDeltaArray    = sc.Subgraph[5];
  SCSubgraphRef NegDeltaArray    = sc.Subgraph[6];

  SCInputRef LookbackNumBars  = sc.Input[0];

if (sc.SetDefaults)
{
// Set the configuration and defaults
sc.GraphName = "TST Extreme DeltaDif Signals v3";
sc.AutoLoop = 0;
sc.GraphRegion = 1; // Not Main Region
    sc.GlobalDisplayStudySubgraphsNameAndValue = 0;
    sc.DisplayStudyInputValues = 0;
    sc.ValueFormat = 0;    
    
    // Define the Subgraphs
    PosDeltaExtreme.Name = "Positive Delta Extreme";
    PosDeltaExtreme.DrawStyle = DRAWSTYLE_COLORBAR;
    PosDeltaExtreme.PrimaryColor = RGB(226,214,181); // MN Wild Wheat

    NegDeltaExtreme.Name = "Negative Delta Extreme";
    NegDeltaExtreme.DrawStyle = DRAWSTYLE_COLORBAR;
    NegDeltaExtreme.PrimaryColor = RGB(237,170,0); // MN Wild Gold

    ZeroLine.Name = "Zero Line";
    ZeroLine.DrawStyle = DRAWSTYLE_LINE;
    ZeroLine.LineStyle = LINESTYLE_SOLID;
    ZeroLine.LineWidth = 1;
    ZeroLine.PrimaryColor = COLOR_DIMGRAY;
    ZeroLine.DrawZeros = true;

    BullDivergence.Name = "Bull Divergence";
    BullDivergence.DrawStyle = DRAWSTYLE_TRIANGLE_UP;
    BullDivergence.LineWidth = 5;
    BullDivergence.PrimaryColor = RGB(226,214,181); // MN Wild Wheat

    BearDivergence.Name = "Bear Divergence";
    BearDivergence.DrawStyle = DRAWSTYLE_TRIANGLE_DOWN;
    BearDivergence.LineWidth = 5;
    BearDivergence.PrimaryColor = RGB(237,170,0); // MN Wild Gold

    PosDeltaArray.DrawStyle = DRAWSTYLE_IGNORE;
    NegDeltaArray.DrawStyle = DRAWSTYLE_IGNORE;

    // Define the inputs
    LookbackNumBars.Name = "Number of Lookback Bars";
    LookbackNumBars.SetInt(5);    
    LookbackNumBars.SetIntLimits(1, INT_MAX);

    return;
}

  // manual looping example
  for (auto index = sc.UpdateStartIndex; index < sc.ArraySize; index++)
  {
    // Init required variables
    int CurrentBarDeltaDif = sc.AskVolume[index] - sc.BidVolume[index]; // Capture the current price bar delta
    auto i = index-1;                         // Set the index one bar back to ignore the current bar
    auto countPos{ 0 };                         // Set the while loop position counter to zero

    // start at current index and decrement index value
    // until when we have reached 5 pos delta bars
    // or until index 0 is reached
    while (i > 0 && countPos < LookbackNumBars.GetInt())
    {  
      // Populate the pos/neg delta arrays depending on pos/neg delta of current bar
      if (CurrentBarDeltaDif > 0 &&                    // Current bar positive delta
        sc.AskVolume[i] > sc.BidVolume[i])                // Add to array if bar at i is delta positive
      {  
        PosDeltaArray[countPos] = sc.AskVolume[i] - sc.BidVolume[i];   // Populate the positive array at i
        countPos++;                            // Increment the array position counter
      }
      else
      if (CurrentBarDeltaDif < 0 &&                    // Current bar negative delta
        sc.AskVolume[i] < sc.BidVolume[i])                // Add to array if bar at i is delta negative
      {  
        NegDeltaArray[countPos] = sc.AskVolume[i] - sc.BidVolume[i];   // Populate the negative array at i
        countPos++;                           // Increment the array position counter
      }

      i--;                                 // Decrement the while loop index counter
    }  

    // Init required variables
    int n = LookbackNumBars.GetInt();  // Get the size of the pos/neg delta array
    int PosDeltaArrayMax = 0;      // Init the positive delta array max variable
    int NegDeltaArrayMin = 0;      // Init the negative delta array min variable

    // Loop through the delta arrays to get either the max positive bar delta or min negative bar delta
    for (int pos = 0; pos < n; pos++)
{
      if (CurrentBarDeltaDif > 0 &&          // Current bar delta GT zero
        PosDeltaArray[pos] > PosDeltaArrayMax)    // Check bar[pos] to see if GT current max delta
          PosDeltaArrayMax = PosDeltaArray[pos];  // If GT current max delta assign new max delta
      else
      if (CurrentBarDeltaDif < 0 &&          // Current bar delta LT zero
        NegDeltaArray[pos] < NegDeltaArrayMin)    // Check bar[pos] to see if LT current min delta
          NegDeltaArrayMin = NegDeltaArray[pos];  // If LT current min delta assign new min delta
    }

    // Compare the current price bar delta to either the max or min delta array to determine if extreme or not
    if (CurrentBarDeltaDif > 0)              // Current bar has positive delta
    {
      if (CurrentBarDeltaDif > PosDeltaArrayMax)    // Current bar positive delta is GT max bar delta in lookback range
          PosDeltaExtreme[index] = 1;        // Paint the extreme positive delta signal
      else                      // Current bar positive delta is LT/EQ max bar delta in lookback range
        PosDeltaExtreme[index] = 0;          // DO NOT paint the extreme positive delta signal
    }  
    else
    if (CurrentBarDeltaDif < 0)              // Current bar has negative delta
    {
      if (CurrentBarDeltaDif < NegDeltaArrayMin)    // Current bar negative delta is LT min bar delta in lookback range
          NegDeltaExtreme[index] = 1;        // Paint the extreme negative delta signal
      else                      // Current bar negative delta is GT/EQ min bar delta in lookback range
        NegDeltaExtreme[index] = 0;          // DO NOT paint the extreme negative delta signal
    }

    //Calculate delta/price divergences
    if (sc.AskVolume[index] > sc.BidVolume[index])  //Positive delta
    {
      if ( sc.Close[index] < sc.Open[index])    //Down price bar
          BearDivergence[index] = 1;      //Bearish delta/price divergence
      else
        BearDivergence[index] = 0;        //NOT bearish delta/price divergence
    }
    else
    if (sc.AskVolume[index] < sc.BidVolume[index])  //Negative delta
    {
      if (sc.Close[index] > sc.Open[index])    //Up price bar
        BullDivergence[index] = 1;        //Bullish delta/price divergence
      else
        BullDivergence[index] = 0;        //NOT Bullish delta/price divergence
    }
  }
}
//SCString Message1;
//Message1.Format("BivVolatPrice: %f",bidVolumeAtPriceLevel);
//sc.AddMessageToLog(Message1,1);      
https://www.sierrachart.com/image.php?Image=1680288699272.png