Login Page - Create Account

Support Board


Date/Time: Thu, 16 May 2024 07:37:34 +0000



Post From: Linear Regression indicator on the chart

[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;
}