3 registered members (Ayumi, Akow, AndrewAMD),
1,505
guests, and 9
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Re: Linear Regression
[Re: MatPed]
#464462
02/15/17 16:57
02/15/17 16:57
|
Joined: Dec 2016
Posts: 13 Italy
Andrea66
OP
Newbie
|
OP
Newbie
Joined: Dec 2016
Posts: 13
Italy
|
I tried with the following code without success, sorry I'm a newbie:
#define N 10 // Number of bars
function run() {
var myClose[10] = { 1,3,9,4,12,6,5,10,15,5}; var LR_line[10]; // variables var a,b,c,sumy=0.0,sumx=0.0,sumxy=0.0,sumx2=0.0,h=0.0,l=0.0; int x,i; int barsToCount=50; for( i=0; i<N; i++) { sumy+=myClose[i]; sumxy+=myClose[i]*i; sumx+=i; sumx2+=i*i; } c=sumx2*N-sumx*sumx; // Line equation b=(sumxy*barsToCount-sumx*sumy)/c; a=(sumy-sumx*b)/N; for(x=0;x<N;x++) { LR_line[x]=a+b*x; plot("NEW_linreg", LR_line[x],MAIN, RED); } }
The calculation should be correct; the issue is related how to plot the straight line in a time chart.
Last edited by Andrea66; 02/15/17 17:10.
|
|
|
Re: Linear Regression
[Re: Andrea66]
#464489
02/17/17 20:24
02/17/17 20:24
|
Joined: Dec 2016
Posts: 13 Italy
Andrea66
OP
Newbie
|
OP
Newbie
Joined: Dec 2016
Posts: 13
Italy
|
This is another example from cAlgo :
using System; using cAlgo.API; using cAlgo.API.Internals; using cAlgo.API.Indicators; namespace cAlgo.Indicators { [Indicator(IsOverlay = true, AccessRights = AccessRights.None)] public class LinearRegressionChannel : Indicator { [Parameter(DefaultValue = 200)] public int Bars { get; set; } [Parameter(DefaultValue = "Yellow")] public string Color { get; set; } [Parameter(DefaultValue = 1.0)] public double LineThickness { get; set; } [Parameter("Center", DefaultValue = true)] public bool ShowCenter { get; set; } [Parameter("Channel", DefaultValue = true)] public bool ShowChannel { get; set; } [Parameter("Standard deviation", DefaultValue = true)] public bool ShowDeviantion { get; set; } private Colors color; protected override void Initialize() { // Parse color from string, e.g. "Yellow", "Green", "Red". string must start with large letter, "Red" is valid, "red" - not. if (!Enum.TryParse(Color, out color)) color = Colors.Yellow; } public override void Calculate(int index) { if (IsLastBar) LinearRegression(MarketSeries.Close); } private void LinearRegression(DataSeries series) { // Linear regresion double sum_x = 0, sum_x2 = 0, sum_y = 0, sum_xy = 0; int start = series.Count - Bars; int end = series.Count - 1; for (int i = start; i <= end; i++) { sum_x += 1.0 * i; sum_x2 += 1.0 * i * i; sum_y += series[i]; sum_xy += series[i] * i; } double a = (Bars * sum_xy - sum_x * sum_y) / (Bars * sum_x2 - sum_x * sum_x); double b = (sum_y - a * sum_x) / Bars; // Calculate maximum and standard devaitions double maxDeviation = 0; double sumDevation = 0; for (int i = start; i <= end; i++) { double price = a * i + b; maxDeviation = Math.Max(Math.Abs(series[i] - price), maxDeviation); sumDevation += Math.Pow(series[i] - price, 2.0); } double stdDeviation = Math.Sqrt(sumDevation / Bars); // draw in future end += 20; double pr1 = a * start + b; double pr2 = a * end + b; if (ShowCenter) { ChartObjects.DrawLine("center", start, pr1, end, pr2, color, LineThickness, LineStyle.Lines); } if (ShowChannel) { ChartObjects.DrawLine("top", start, pr1 + maxDeviation, end, pr2 + maxDeviation, color, LineThickness, LineStyle.Solid); ChartObjects.DrawLine("bottom", start, pr1 - maxDeviation, end, pr2 - maxDeviation, color, LineThickness, LineStyle.Solid); } if (ShowDeviantion) { ChartObjects.DrawLine("dev-top", start, pr1 + stdDeviation, end, pr2 + stdDeviation, color, LineThickness, LineStyle.DotsVeryRare); ChartObjects.DrawLine("dev-bottom", start, pr1 - stdDeviation, end, pr2 - stdDeviation, color, LineThickness, LineStyle.DotsVeryRare); } } } }
thus the calculation should be fine but what about the plotting in Zorro?
Last edited by Andrea66; 02/17/17 20:24.
|
|
|
|