Login Page - Create Account

Support Board


Date/Time: Fri, 03 May 2024 23:38:07 +0000



[User Discussion] - Line On Line by Code

View Count: 1044

[2015-10-02 03:52:39]
KhaosTrader - Posts: 128
Hi,

I managed to make a unique look on chart lines, by putting two moving averages on top of each other with different styles (see attached image).

I wanted to do this with code, so I tried, but it didn't work. Please forgive me if the code is not the best, its my first attempt at coding this platform.

Tell me what I should do to make the visual effect work..

Code below:

SCSFExport scsf_SimpMovAvg(SCStudyInterfaceRef sc)
{
  
  //sc.FreeDLL = 1;
  
  SCSubgraphRef Average_Outter = sc.Subgraph[0];
  SCSubgraphRef Average_Inner = sc.Subgraph[0];


  
  SCInputRef Length = sc.Input[0];
  
  // Set configuration variables
  if (sc.SetDefaults)
  {
    // Set the configuration and defaults
    
    sc.GraphName = "SimpleX Moving Average";
    
    sc.StudyDescription = "Example function for calculating a simple moving average.";
    
    // Set the region to draw the graph in. Region zero is the main
    // price graph region.
    sc.GraphRegion = 0;
    
    // Set the name of the first subgraph
    Average_Inner.Name = "Average";
    
    // Set the color, style and line width for the subgraph


    Average_Outter.PrimaryColor = RGB(112,0,0);
    Average_Outter.DrawStyle = DRAWSTYLE_LINE;
    Average_Outter.LineStyle = LINESTYLE_SOLID;
    Average_Outter.LineWidth = 6;
    
    Average_Inner.PrimaryColor = RGB(255,255,255);
    Average_Inner.DrawStyle = DRAWSTYLE_LINE;
    Average_Inner.LineStyle = LINESTYLE_DASHDOT;
    Average_Inner.LineWidth = 1;



    
    // Set the Length input and default it to 30
    Length.Name = "Length";
    Length.SetInt(20);
    Length.SetIntLimits(1,MAX_STUDY_LENGTH);
    Length.SetDescription("The number of bars to average.");
    
    sc.AutoLoop = 1;
    
    sc.AlertOnlyOncePerBar = true;
    
    // During development set this flag to 1, so the DLL can be modified. When development is completed, set it to 0 to improve performance.
    sc.FreeDLL = 1;
    
    // Must return before doing any data processing if sc.SetDefaults is set
    return;
  }
  
  // Do data processing
  
  // Set the index of the first array element to begin drawing at
  sc.DataStartIndex = Length.GetInt() - 1;
  
  
  // Calculate a simple moving average from the base data

  sc.SimpleMovAvg(sc.Close,Average_Outter,Length.GetInt());
    sc.SimpleMovAvg(sc.Close,Average_Inner,Length.GetInt());
  //if (sc.CrossOver(Average_Inner,sc.Close))
    //sc.SetAlert(1, "Moving average has crossed last price."); // Since we are using auto-looping we do not specify the Index parameter.
}

imageLineOnLine.png / V - Attached On 2015-10-02 03:49:34 UTC - Size: 64.51 KB - 250 views
[2015-10-02 13:03:05]
mkata - Posts: 103
KhaosTrader,
There were only two mistakes.
1) Average_Inner = sc.Subgraph[0] needed its own #
2) Each Subgraph needs to have a .Name to appear in Study Settings window (ex. Average_Inner.Name = "Average")

I went ahead and made the changes.



SCSFExport scsf_SimpMovAvg(SCStudyInterfaceRef sc)

{
SCSubgraphRef Average_Outter = sc.Subgraph[0];
SCSubgraphRef Average_Inner = sc.Subgraph[1];

SCInputRef Length = sc.Input[0];

// Set configuration variables
if (sc.SetDefaults)
{
// Set the configuration and defaults
sc.GraphName = "SimpleX Moving Average";
sc.StudyDescription = "Example function for calculating a simple moving average.";
sc.GraphRegion = 0;
sc.AutoLoop = 1;
sc.AlertOnlyOncePerBar = true;
  sc.FreeDLL = 1;


// Set the color, style and line width for the subgraph
Average_Outter.Name = "Average_Outter";
  Average_Outter.PrimaryColor = RGB(112,0,0);
  Average_Outter.DrawStyle = DRAWSTYLE_LINE;
  Average_Outter.LineStyle = LINESTYLE_SOLID;
  Average_Outter.LineWidth = 6;

  Average_Inner.Name = "Average_Inner";
  Average_Inner.PrimaryColor = RGB(255,255,255);
  Average_Inner.DrawStyle = DRAWSTYLE_LINE;
  Average_Inner.LineStyle = LINESTYLE_DASHDOT;
  Average_Inner.LineWidth = 1;

// Set the Length input
Length.Name = "Length";
  Length.SetInt(20);
  Length.SetIntLimits(1,MAX_STUDY_LENGTH);
  Length.SetDescription("The number of bars to average.");

return;

}
// Do data processing
// Calculate a simple moving average from the base data

sc.SimpleMovAvg(sc.Close,Average_Outter,Length.GetInt());
sc.SimpleMovAvg(sc.Close,Average_Inner,Length.GetInt());

}

Date Time Of Last Edit: 2015-10-02 13:03:34

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

Login

Login Page - Create Account