|
Juliettpapa
2011-05-02 10:37:26
|
|
Has anybody written the supertrend indicator?
I have tried to do it, but it is to heavy for a beginner....
|
|
|
|
QuantShare
2011-05-02 14:57:46
0
|
|
Do you have the formula of the supertrend indicator in another program language? how it looks like? (I saw a lot of versions)
|
|
|
|
|
QuantShare
2011-05-03 06:10:36
1
|
|
Best Answer
After you add the indicator, right click on a chart then add the following formula: (Let me know if this is what you were looking for)
s1 = supertrend(10, 3);
plot(absolute(s1), "", colorRed);
UpdateColor(s1 < 0, colorDarkGreen);
Indicator:
VectorD volaValue = TA.Atr(Period);
VectorD avgValue = TA.MedPrice();
VectorD upperValue = cFunctions.CreateNumericVector();
VectorD lowerValue = cFunctions.CreateNumericVector();
VectorD trendFlag = cFunctions.CreateNumericVector();
VectorD flagValue1 = cFunctions.CreateNumericVector();
VectorD flagValue2 = cFunctions.CreateNumericVector();
VectorD close = cFunctions.Close;
double lastTrendValue = 0;
for(int i=1;i < result.Length;i++)
{
upperValue[i] = avgValue[i] + ( Factor[i] * volaValue[i] );
lowerValue[i] = avgValue[i] - ( Factor[i] * volaValue[i] );
if(i == 0)
{
continue;
}
if(close[i] > upperValue[i - 1])
{
lastTrendValue = 1;
}
if(close[i] < lowerValue[i - 1])
{
lastTrendValue = -1;
}
trendFlag[i] = lastTrendValue;
if(trendFlag[i] < 0 && trendFlag[i - 1] > 0)
{
flagValue1[i] = 1;
}
else
{
flagValue1[i] = 0;
}
if(trendFlag[i] > 0 && trendFlag[i - 1] < 0)
{
flagValue2[i] = 1;
}
else
{
flagValue2[i] = 0;
}
if(trendFlag[i] > 0 && lowerValue[i] < lowerValue[i - 1])
{
lowerValue[i] = lowerValue[i - 1];
}
if(trendFlag[i] < 0 && upperValue[i] > upperValue[i - 1])
{
upperValue[i] = upperValue[i - 1];
}
if(flagValue1[i] == 1)
{
upperValue[i] = avgValue[i] + ( Factor[i] * volaValue[i] );
}
if(flagValue2[i] == 1)
{
lowerValue[i] = avgValue[i] - ( Factor[i] * volaValue[i] );
}
if(trendFlag[i] == 1)
{
result[i] = -lowerValue[i];
}
else
{
result[i] = upperValue[i];
}
}
|
|
|
|
|
QuantShare
2011-05-03 13:47:45
0
|
|
I tested it here and I am getting the right indicator (picture 2)
I forget to tell you to add 2 parameters: (Period and Factor)
|
|
|
|
Juliettpapa
2011-05-03 15:18:54
0
|
|
OK. Once again something with my charts.
When using a new one, it works fine.
Well done!
Thanks.
|
|
|
|
|
QuantShare
2011-05-04 05:37:44
0
|
|
The green line is displayed when a certain condition is met. Otherwise only the red line is shown. It is normal that decreasing the Factor will result in more signals and thus displaying the green line.
|
|
|
|
|
QuantShare
2011-05-05 05:14:37
0
|
|
Change the following line:
for(int i=0;i < result.Length;i++)
to
for(int i=1;i < result.Length;i++)
|
|
|
|
|
|