Login Page - Create Account

Support Board


Date/Time: Thu, 16 May 2024 08:48:27 +0000



Post From: Linear Regression indicator on the chart

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