Hello i am trying to change this code for my purpose. This code draws regression lines at chart. With inputs of start_bar and end_bar. start_bar's value is length of calculation. end_bar's value is always zero. I need to change this code with specific value of end_bar.
I directly changed end_bar's value with any of integer number. The I replaced n with n2=start_bar - end_bar. But it can not work with correct calculation. If you know please help to how to fix this problem
Thank you
#property copyright//---- link to the website of the author#property link"http://www.metaquotes.net"//---- indicator version#property version"1.00"#property description"Linear Regression"//---- drawing the indicator in the main window#property indicator_chart_window#property indicator_buffers0#property indicator_plots0//+------------------------------------------------+//| Declaration of constants |//+------------------------------------------------+#define RESET 0// the constant for getting the command for the indicator recalculation back to the terminal//+------------------------------------------------+//| Enumeration for the level width |//+------------------------------------------------+ enum ENUM_WIDTH // type of constant
{
w_1 = 1, // 1
w_2, // 2
w_3, // 3
w_4, // 4
w_5 // 5
};
//+------------------------------------------------+//| Indicator input parameters |//+------------------------------------------------+inputstring lines_sirname="Linear_Regression_"; // Graphic objects group nameinputENUM_TIMEFRAMES period=PERIOD_CURRENT; // Chart periodinputint LR_length=34; // Indicator calculation periodinputbool Deletelevel=true; // Level deletioninputcolor LR_c=clrBlue; // Middle line colorinputENUM_LINE_STYLE LR_style=STYLE_DOT; // Middle line styleinput ENUM_WIDTH LR_width=w_1; // Middle line widthinputdouble std_channel_1=1; // Minimum regressioninputcolor c_1=clrBlue; // Minimum line colorinputENUM_LINE_STYLE style_1=STYLE_DOT; // Minimum line styleinput ENUM_WIDTH width_1=w_1; // Minimum line widthinputdouble std_channel_2=2; // Nominal regressioninputcolor c_2=clrGray; // Nominal line colorinputENUM_LINE_STYLE style_2=STYLE_DOT; // Nominal line styleinput ENUM_WIDTH width_2=w_1; // Nominal line widthinputdouble std_channel_3=2.618; // Maximum regressioninputcolor c_3=Magenta; // Maximum line colorinputENUM_LINE_STYLE style_3=STYLE_SOLID; // Maximum line styleinput ENUM_WIDTH width_3=w_3; // Maximum line width//+----------------------------------------------+int start_bar,end_bar,n,to_copy;
string LR_name,CH1P_name,CH1M_name,CH2P_name,CH2M_name,CH3P_name,CH3M_name;
//+------------------------------------------------------------------+//| Trend line creation |//+------------------------------------------------------------------+void CreateTline(long chart_id, // chart IDstring name, // object nameint nwin, // window indexdatetime time1, // price level time 1double price1, // price level 1datetime time2, // price level time 2double price2, // price level 2color Color, // line colorint style, // line styleint width, // line widthstring text) // text
{
//----ObjectCreate(chart_id,name,OBJ_TREND,nwin,time1,price1,time2,price2);
ObjectSetInteger(chart_id,name,OBJPROP_COLOR,Color);
ObjectSetInteger(chart_id,name,OBJPROP_STYLE,style);
ObjectSetInteger(chart_id,name,OBJPROP_WIDTH,width);
ObjectSetString(chart_id,name,OBJPROP_TEXT,text);
ObjectSetInteger(chart_id,name,OBJPROP_BACK,false);
ObjectSetInteger(chart_id,name,OBJPROP_RAY_RIGHT,false);
ObjectSetInteger(chart_id,name,OBJPROP_RAY,true);
ObjectSetInteger(chart_id,name,OBJPROP_SELECTED,true);
ObjectSetInteger(chart_id,name,OBJPROP_SELECTABLE,true);
ObjectSetInteger(chart_id,name,OBJPROP_ZORDER,true);
//----
}
//+------------------------------------------------------------------+//| Trend line reinstallation |//+------------------------------------------------------------------+void SetTline(long chart_id, // chart IDstring name, // object nameint nwin, // window indexdatetime time1, // price level time 1double price1, // price level 1datetime time2, // price level time 2double price2, // price level 2color Color, // line colorint style, // line styleint width, // line widthstring text) // text
{
//----if(ObjectFind(chart_id,name)==-1)
{
CreateTline(chart_id,name,nwin,time1,price1,time2,price2,Color,style,width,text);
}
else
{
ObjectSetString(chart_id,name,OBJPROP_TEXT,text);
ObjectMove(chart_id,name,0,time1,price1);
ObjectMove(chart_id,name,1,time2,price2);
ObjectSetInteger(chart_id,name,OBJPROP_COLOR,Color);
}
//----
}
//+------------------------------------------------------------------+//| Custom indicator initialization function |//+------------------------------------------------------------------+ voidOnInit()
{
//---- initialization of variables string fullname=lines_sirname+string(period)+" "+string(LR_length);
LR_name=fullname+" TL";
CH1P_name=fullname+" +"+string(std_channel_1)+"d";
CH1M_name=fullname+" -"+string(std_channel_1)+"d";
CH2P_name=fullname+" +"+string(std_channel_2)+"d";
CH2M_name=fullname+" -"+string(std_channel_2)+"d";
CH3P_name=fullname+" +"+string(std_channel_3)+"d";
CH3M_name=fullname+" -"+string(std_channel_3)+"d";
start_bar=LR_length;
end_bar= 19;
n=start_bar-end_bar+1;
to_copy=2*n;
//----
}
//+------------------------------------------------------------------+//| Custom indicator deinitialization function |//+------------------------------------------------------------------+ voidOnDeinit(constint reason)
{
//---- delete the level, if necessaryif(Deletelevel)
{
ObjectDelete(0,LR_name);
ObjectDelete(0,CH1P_name);
ObjectDelete(0,CH1M_name);
ObjectDelete(0,CH2P_name);
ObjectDelete(0,CH2M_name);
ObjectDelete(0,CH3P_name);
ObjectDelete(0,CH3M_name);
}
//----
}
//+------------------------------------------------------------------+//| Custom indicator iteration function |//+------------------------------------------------------------------+intOnCalculate(constint rates_total, // number of bars in history at the current tickconstint prev_calculated,// number of bars calculated at previous callconstdatetime &time[],
constdouble &open[],
constdouble& high[], // price array of maximums of price for the indicator calculationconstdouble& low[], // price array of minimums of price for the indicator calculationconstdouble &close[],
constlong &tick_volume[],
constlong &volume[],
constint &spread[])
{
//---- declarations of local variables double value,a,b,c,sumy,sumx,sumxy,sumx2,iClose[];
datetimeiTime[],start_time,end_time;
//--- copy newly appeared data in the arraysif(CopyClose(NULL,period,0,to_copy,iClose)<=0) return(RESET);
if(CopyTime(NULL,period,0,to_copy,iTime)<=0) return(RESET);
//---- indexing elements in arrays as time series ArraySetAsSeries(iClose,true);
ArraySetAsSeries(iTime,true);
start_time=iTime[start_bar];
end_time=iTime[end_bar];
value=iClose[end_bar];
sumy=value;
sumx=0.0;
sumxy=0.0;
sumx2=0.0;
for(int iii=1; iii<n; iii++)
{
value=iClose[end_bar+iii];
sumy+=value;
sumxy+=value*iii;
sumx+=iii;
sumx2+=iii*iii;
}
c=sumx2*n-sumx*sumx;
if(!c) return(rates_total);
b=(sumxy*n-sumx*sumy)/c;
a=(sumy-sumx*b)/n;
double LR_price_2=a;
double LR_price_1=a+b*n;
//---- maximal deviation calculation (not used)double max_dev=0;
double deviation=0;
double dvalue=a;
for(int iii=0; iii<n; iii++)
{
value=iClose[end_bar+iii];
dvalue+=b;
deviation=MathAbs(value-dvalue);
if(max_dev<=deviation) max_dev=deviation;
}
//---- Linear regression trendline
SetTline(0,LR_name,0,start_time,LR_price_1,end_time,LR_price_2,LR_c,LR_style,LR_width,LR_name);
//---- ...standard deviation...double x=0,x_sum=0,x_avg=0,x_sum_squared=0,std_dev=0,price1,price2;
for(int iii=0; iii<start_bar; iii++)
{
x=MathAbs(iClose[iii]-ObjectGetValueByTime(0,LR_name,iTime[iii],0));
x_sum+=x;
if(iii>0)
{
x_avg=(x_avg+x)/iii;
x_sum_squared+=(x-x_avg)*(x-x_avg);
//std_dev=MathSqrt(x_sum_squared/(start_bar-1));
std_dev=MathSqrt(x_sum_squared/(n-1));
}
}
//---- ...standard deviation channels...
price1=LR_price_1+std_dev*std_channel_1;
price2=LR_price_2+std_dev*std_channel_1;
SetTline(0,CH1P_name,0,start_time,price1,end_time,price2,c_1,style_1,width_1,CH1P_name);
price1=LR_price_1-std_dev*std_channel_1;
price2=LR_price_2-std_dev*std_channel_1;
SetTline(0,CH1M_name,0,start_time,price1,end_time,price2,c_1,style_1,width_1,CH1M_name);
//----
price1=LR_price_1+std_dev*std_channel_2;
price2=LR_price_2+std_dev*std_channel_2;
SetTline(0,CH2P_name,0,start_time,price1,end_time,price2,c_2,style_2,width_2,CH2P_name);
price1=LR_price_1-std_dev*std_channel_2;
price2=LR_price_2-std_dev*std_channel_2;
SetTline(0,CH2M_name,0,start_time,price1,end_time,price2,c_2,style_2,width_2,CH2M_name);
//---- //price1=LR_price_1+std_dev*std_channel_3;//price2=LR_price_2+std_dev*std_channel_3;//SetTline(0,CH3P_name,0,start_time,price1,end_time,price2,c_3,style_3,width_3,CH3P_name);//price1=LR_price_1-std_dev*std_channel_3;//price2=LR_price_2-std_dev*std_channel_3;//SetTline(0,CH3M_name,0,start_time,price1,end_time,price2,c_3,style_3,width_3,CH3M_name);//---- return(rates_total);
}
Hello i am trying to change this code for my purpose. This code draws regression lines at chart. With inputs of start_bar and end_bar. start_bar's value is length of calculation. end_bar's value is always zero. I need to change this code with specific value of end_bar.
I directly changed end_bar's value with any of integer number. The I replaced n with n2=start_bar - end_bar. But it can not work with correct calculation. If you know please help to how to fix this problem
Thank you