Login Page - Create Account

Support Board


Date/Time: Mon, 29 Apr 2024 15:09:48 +0000



[Programming Help] - Linear Regression indicator on the chart

View Count: 1412

[2019-10-24 09:27:52]
GiovanniD - Posts: 41
I converted this indicator that is both on Multichart and on Metatrader 5 from the Metatrader code, I prefer it to the Slope that is in SierraChart because it directly follows the price on the main chart.
The indicator that I wrote seems to work well, but being really at "day zero" about the ACSIL programming I'm not entirely sure that it is perfect and does not get stuck in every circumstance, I post it here so that those who are more experienced can make corrections and if Sierra's team finds it interesting, they can include it in their own indicators.

Good day

// The top of every source code file must include this line
#include "sierrachart.h"

// For reference, refer to this page:
// Advanced Custom Study Interface and Language (ACSIL)

// This line is required. Change the text within the quote
// marks to what you want to name your group of custom studies.
SCDLLName("Piero Custom Study DLL")

//+------------------------------------------------------------------+\\
// Calculate LRMA
//+------------------------------------------------------------------+\\
//double LRMA(const int pos,const int period,const double &price[])// period = numero di candele; pos = candela da calcolare; price = vettore chiusure prezzo
double LRMA(SCStudyInterfaceRef sc, const int pos,const int period)// period = Len, sempre fisso
{
double Res=0;
double tmpS=0,tmpW=0,wsum=0;;
for(int i=0;i<period;i++)//dalla candela attuale scende fino a Len (esclusa)
{
   tmpS+=sc.Close[pos-i];
tmpW+=sc.Close[pos-i]*(period-i);
wsum+=(period-i);
}
tmpS/=period;
tmpW/=wsum;
Res=3.0*tmpW-2.0*tmpS;
return(Res);
}  

//This is the basic framework of a study function. Change the name 'TemplateFunction' to what you require.
SCSFExport scsf_LRS2(SCStudyInterfaceRef sc)
{
  SCSubgraphRef LRS2 = sc.Subgraph[0];
  SCInputRef Length = sc.Input[3];

  if (sc.SetDefaults)
  {
    sc.GraphName = "Regressione Lineare";
    sc.AutoLoop = 1;
    sc.GraphRegion = 0;

    LRS2.Name = "LRS2";
    LRS2.DrawStyle = DRAWSTYLE_LINE;
    LRS2.LineWidth = 2;
    LRS2.PrimaryColor = COLOR_BLUE;
    LRS2.SecondaryColor = COLOR_RED;
    LRS2.SecondaryColorUsed = 1;
    LRS2.DrawZeros = true;

    Length.Name = "Length";
    Length.SetInt(9);
    Length.SetIntLimits(1,MAX_STUDY_LENGTH);

    return;
  }
  
  double& Val_PrevBar = sc.GetPersistentDouble(1);
  
  sc.DataStartIndex = Length.GetInt();
  
  //Reset
  if (sc.Index == 0) Val_PrevBar = 0;
    
  int Len=Length.GetInt();//candele
  int stato = 0;
  double risultato = 0;

risultato = LRMA(sc, sc.Index, Len);
  LRS2[sc.Index] = risultato;

  if (risultato>Val_PrevBar) stato = 1;
  Val_PrevBar = risultato;
  
  LRS2.DataColor[sc.Index] = stato > 0 ? LRS2.PrimaryColor : LRS2.SecondaryColor;

  return;
}

[2019-10-24 09:30:48]
GiovanniD - Posts: 41
"regression", not "regressor".. please correct the main title of this post.

There is certainly an error in coloring the line in real time instead of the historical one where it works well, I have to understand what to correct.

Thanks
Date Time Of Last Edit: 2019-10-24 09:48:57
[2019-10-24 17:27:31]
Sierra Chart Engineering - Posts: 104368
You have the ability with the Rename button at the top of the thread to change the title of the thread.


We did correct the title for you.
Sierra Chart Support - Engineering Level

Your definitive source for support. Other responses are from users. Try to keep your questions brief and to the point. Be aware of support policy:
https://www.sierrachart.com/index.php?l=PostingInformation.php#GeneralInformation

For the most reliable, advanced, and zero cost futures order routing, *change* to the Teton service:
Sierra Chart Teton Futures Order Routing
[2019-10-29 09:09:06]
GiovanniD - Posts: 41
This is the correctly working version adapted from MT5 and MultiCharts.NET. I don't think I'll add anything else later.

// The top of every source code file must include this line
#include "sierrachart.h"

// For reference, refer to this page:
// Advanced Custom Study Interface and Language (ACSIL)

// This line is required. Change the text within the quote
// marks to what you want to name your group of custom studies.
SCDLLName("Piero Custom Study DLL")

//+------------------------------------------------------------------+\\
// Calculate LRMA
//+------------------------------------------------------------------+\\
//double LRMA(const int pos,const int period,const double &price[])// period = numero di candele; pos = candela da calcolare; price = vettore chiusure prezzo
double LRMA(SCStudyInterfaceRef sc, const int pos,const int period)// period = Len, sempre fisso
{
double Res=0;
double tmpS=0,tmpW=0,wsum=0;;
for(int i=0;i<period;i++)//dalla candela attuale scende fino a Len (esclusa)
{
   tmpS+=sc.Close[pos-i];
tmpW+=sc.Close[pos-i]*(period-i);
wsum+=(period-i);
}
tmpS/=period;
tmpW/=wsum;
Res=3.0*tmpW-2.0*tmpS;
return(Res);
}  

//This is the basic framework of a study function. Change the name 'TemplateFunction' to what you require.
SCSFExport scsf_LRS2(SCStudyInterfaceRef sc)
{
  SCSubgraphRef LRS2 = sc.Subgraph[0];
  SCInputRef Length = sc.Input[1];

  if (sc.SetDefaults)
  {
    sc.GraphName = "Regressione Lineare";
    sc.AutoLoop = 1;
    sc.GraphRegion = 0;

    LRS2.Name = "LRS2";
    LRS2.DrawStyle = DRAWSTYLE_LINE;
    LRS2.LineWidth = 2;
    LRS2.PrimaryColor = COLOR_BLUE;
    LRS2.SecondaryColor = COLOR_RED;
    LRS2.SecondaryColorUsed = 1;
    LRS2.DrawZeros = true;

    Length.Name = "Length";
    Length.SetInt(9);
    Length.SetIntLimits(1,MAX_STUDY_LENGTH);

    return;
  }
  
  int& stato = sc.GetPersistentInt(1);
  
  sc.DataStartIndex = Length.GetInt();
  
  //Reset
  if (sc.Index == 0)
  {
    stato = 0;
  }
    
  int Len=Length.GetInt();
  double risultato = 0;

risultato = LRMA(sc, sc.Index, Len);
    
  if (risultato>LRS2[sc.Index-1]) stato = 1;
  else if (risultato<LRS2[sc.Index-1]) stato = 0;
  
  if (stato == 1)
  {
    LRS2[sc.Index] = risultato;
    LRS2.DataColor[sc.Index] = LRS2.PrimaryColor;
  }
  else if (stato == 0)
  {
    LRS2[sc.Index] = risultato;
    LRS2.DataColor[sc.Index] = LRS2.SecondaryColor;
  }  

  return;
}

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

Login

Login Page - Create Account