Gamestudio Links
Zorro Links
Newest Posts
zorro 64bit command line support
by 7th_zorro. 04/20/24 10:06
StartWeek not working as it should
by jcl. 04/20/24 08:38
Data from CSV not parsed correctly
by jcl. 04/20/24 08:32
Zorro FIX plugin - Experimental
by jcl. 04/20/24 08:30
folder management functions
by VoroneTZ. 04/17/24 06:52
lookback setting performance issue
by 7th_zorro. 04/16/24 03:08
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:46
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
3 registered members (7th_zorro, Aku_Aku, 1 invisible), 579 guests, and 1 spider.
Key: Admin, Global Mod, Mod
Newest Members
EternallyCurious, howardR, 11honza11, ccorrea, sakolin
19047 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 2 of 2 1 2
Re: Linear Regression [Re: Andrea66] #464461
02/15/17 15:59
02/15/17 15:59
Joined: Feb 2015
Posts: 652
Milano, Italy
M
MatPed Offline
User
MatPed  Offline
User
M

Joined: Feb 2015
Posts: 652
Milano, Italy
yes,
it does not work in that way. Fill the myClose with some sample data and than check the math. When the calculation will be ok will fix the first part.

delete the
StartDate= 20060101;
EndDate = 20060601;

BarPeriod = 15;

just plain c

Re: Linear Regression [Re: MatPed] #464462
02/15/17 16:57
02/15/17 16:57
Joined: Dec 2016
Posts: 13
Italy
Andrea66 Offline OP
Newbie
Andrea66  Offline 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 Offline OP
Newbie
Andrea66  Offline 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.
Re: Linear Regression [Re: Andrea66] #464492
02/18/17 09:50
02/18/17 09:50
Joined: Feb 2015
Posts: 652
Milano, Italy
M
MatPed Offline
User
MatPed  Offline
User
M

Joined: Feb 2015
Posts: 652
Milano, Italy
Ciao,
the calculation for getting the line equation look correct to me, but I am not an expert on how plotting with Zorro.
Sorry

Page 2 of 2 1 2

Moderated by  Petra 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1