Login Page - Create Account

Support Board


Date/Time: Wed, 12 Mar 2025 12:38:34 +0000



Post From: What am I doing wrong? Combining scsf_BarTimeDuration and scsf_BackgroundDrawStyleExample

[2022-05-05 03:00:32]
silvereagle - Posts: 23
Hi friends. I was attempting to write a study that changed the color of the background of a bar-column based off a duration of seconds ---I use a lot of different colors for different things so my needs go beyond primary/secondary/etc.

I know this is possible using Color Background Based on Alert Condition with appropriate formulas, and I've done this in the past but I was looking for more advanced control from an ASCIL study.

I was attempting to "blend" two sierra studies into my own unique one, but I just cant get this to work. I gotta be missing something. Code Below. Many thanks to anyone who can nudge me in the right direction.

- scsf_BarTimeDuration
- scsf_BackgroundDrawStyleExample

My thought was to:
1. Get the time difference
2. Get the seconds from that TimeDiffDateTime
3. Set Subgraph_BackgroundDC.DataColor to RGB color

This is my code, mostly taken from the sierra studies themselves.



SCSFExport scsf_BackgroundDrawStyleExample(SCStudyInterfaceRef sc)
{
  SCSubgraphRef Subgraph_BackgroundDC = sc.Subgraph[0];

  if (sc.SetDefaults)
  {
    // Set the configuration and defaults
    sc.GraphName = "Background DrawStyle Example";
    sc.StudyDescription = "Background DrawStyle Example";

    sc.AutoLoop = true;

    sc.GraphRegion = 0;
    sc.DrawStudyUnderneathMainPriceGraph = 1; // not required in studies, but want color behind price for this study

    Subgraph_BackgroundDC.Name = "Background DataColor";
    Subgraph_BackgroundDC.DrawStyle = DRAWSTYLE_BACKGROUND;
    Subgraph_BackgroundDC.PrimaryColor = RGB(255, 255, 255);

    return;
  }

  // Step 1: Get duration
  // code from: scsf_BarTimeDuration
  SCDateTime BarEndDateTime = sc.GetEndingDateTimeForBarIndex(sc.Index);
  double TimeDiff = static_cast<float>((BarEndDateTime - sc.BaseDateTimeIn[sc.Index] + SCDateTime::MICROSECONDS(1)).GetAsDouble());

  // Step 2: Get seconds from duration
  SCDateTime TimeDiffDateTime = SCDateTime(TimeDiff);
  int BarDurationAsSeconds = TimeDiffDateTime.GetTimeInSeconds();

  if (BarDurationAsSeconds >= 0 && BarDurationAsSeconds < 15) {
    Subgraph_BackgroundDC.DataColor[sc.Index] = RGB(204, 153, 255);
  };

  if (BarDurationAsSeconds >= 15 && BarDurationAsSeconds < 30) {
    Subgraph_BackgroundDC.DataColor[sc.Index] = RGB(51, 204, 51);
  };

  if (BarDurationAsSeconds >= 30 && BarDurationAsSeconds < 45) {
    Subgraph_BackgroundDC.DataColor[sc.Index] = RGB(158, 70, 255);
  };

  /* ----------------------------------------
  * code from scsf_BackgroundDrawStyleExample
  * -----------------------------------------
  
  int min = sc.BaseDateTimeIn[sc.Index].GetMinute();

  if (min > 0 && min < 15)
    Subgraph_Background[sc.Index] = 0; // do not color background
  else if (min >= 15 && min < 30)
    Subgraph_Background[sc.Index] = 1; // use primary color
  else if (min >= 30 && min < 45)
    Subgraph_Background[sc.Index] = -1; // use secondary color
  else if (min >= 45 && min < 60)
  {
    Subgraph_BackgroundDC[sc.Index] = 1;
    Subgraph_BackgroundDC.DataColor[sc.Index] = RGB(0, 0, 17 * (60 - min));
  }

  */






}