The idea of the indicator is to make a best fit line over a variable called backwards amount of ticks. Then, the best fit line is projected forward a variable called forward number of ticks. The value that is projected forward ticks ahead is then assigned as the current value of the indicator.
My problem is that I get a constant value of 6.324020 for the indicator's value and it looks like a horizontal line.
Does anyone know or have any ideas about my problem's solution?
Here's my code:
//+------------------------------------------------------------------+//| TSF2.mq5 |//| Copyright 2010, MetaQuotes Software Corp. |//| http://www.mql5.com |//+------------------------------------------------------------------+#property copyright "Copyright 2010, MetaQuotes Software Corp."#property link "http://www.mql5.com"#property version "1.00"#property indicator_separate_window#property indicator_buffers 1#property indicator_plots 1//--- plot TSF#property indicator_label1 "TSF"#property indicator_type1 DRAW_LINE#property indicator_color1 Red#property indicator_style1 STYLE_SOLID#property indicator_width1 1//--- input parametersinputint forwards=5;
inputint backwards=20;
// global variables// double xbar,ybar,B,TSF,num,denom;//--- indicator buffersdouble TSFBuffer[];
//+------------------------------------------------------------------+//| Custom indicator initialization function |//+------------------------------------------------------------------+voidOnInit()
{
//--- indicator buffers mappingSetIndexBuffer(0,TSFBuffer,INDICATOR_DATA);
}
//+------------------------------------------------------------------+//| Custom indicator iteration function |//+------------------------------------------------------------------+intOnCalculate(
constint rates_total, // number of available bars in history at the current tickconstint prev_calculated,// number of bars, calculated at previous tickconstint begin, // index of the first barconstdouble &price[] // price array for the calculation
)
{
double xbar = 0;
double ybar = 0;
double numerator = 1;
double denom = 1;
double TSF = 0;
if (prev_calculated>backwards){
for (int i = prev_calculated-backwards; i<prev_calculated; i++){ // for average totals
xbar = xbar + 1;
ybar = ybar + price[i];
}
// Get averages
xbar = xbar/ backwards;
ybar = ybar/ backwards;
for (int y = prev_calculated-backwards; y < prev_calculated; y++){
numerator = numerator + (price[y]-ybar)*(1-xbar);
denom = denom + (1-xbar)*(1-xbar);
}
}
TSF = (numerator/denom)*forwards + price[rates_total-1];
TSFBuffer[rates_total-1] = TSF;
//---//--- return value of prev_calculated for next callreturn(rates_total);
}
//+------------------------------------------------------------------+
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
The idea of the indicator is to make a best fit line over a variable called backwards amount of ticks. Then, the best fit line is projected forward a variable called forward number of ticks. The value that is projected forward ticks ahead is then assigned as the current value of the indicator.
My problem is that I get a constant value of 6.324020 for the indicator's value and it looks like a horizontal line.
Does anyone know or have any ideas about my problem's solution?
Here's my code: