Click here to Login





                                                   coding a strat using supertrend

  0

0
ivanf
2017-04-13 08:10:22


Hi,

this might be a dumb question....however

I'm wanting to use the indicator "supetrend" in a strategy ( https://www.quantshare.com/item-1038-supertrend-indicator)

Basically I'm using an external symbol XJO as a marketiming input, if the index is above it's supertrend then its ok to go long on stocks

However, I'm a bit confused (newbie) on how to calc the supertrend of this external symbol , since I don't know how to incorporate the symbol into the formula

ie how do I calc the supertrend of XAO when the supertrend indicator doesn't take a symbol input ?

This is what I started to code but I know its not right . I want to calc the supertend of XJO and also the supertrend of individual stocks before I buy

stLenMT=10;
stFactMT=3;
stLen=10;
stFact=3;


// marketiming based on XAO holding above its supertrend

MTindex =GetSeries('XAO', Close);

MTbuy= MTindex > supertrend(stLenMT, stFactMT);


// buy when stocks cross up thru their own supertrend and overall marketiming is a buy

buy = cross(close, supertrend(stLen, stFact)) and MTBuy ;



thanks for any help
Ivan



QuantShare
2017-04-14 04:16:18

  0

This indicator cannot be used to calculate other symbol supertrend. You need to update the C# formula of the "supertrend" indicator, add a new parameter (that takes the close series for example) and use that series instead of the "close" series of the analyzed security.


ivanf
2017-04-14 05:00:07

  0

thanks for the reply. To implement your suggestion is that easy to do ? The supertrend indicator references a function , which is copied below. Are you able to show me how to "hardcode" for symbol XJO.AX ?

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];
}
}

result[0] = double.NaN;



QuantShare
2017-04-15 04:53:44

  0

You need to create three parameters in the function to pass the close, high and low of the security you want to analyze (using GetSeries function)

After that, you need to change the script and update the following three lines:
VectorD volaValue = TA.Atr(Period);
VectorD avgValue = TA.MedPrice();

VectorD close = cFunctions.Close;

with something like this:
VectorD volaValue = TA.Atr(high, low, close Period);
VectorD avgValue = TA.MedPrice(high, low);

VectorD close = close;


where close, high and low are the name of the parameters that you need to add to the function.



ivanf
2017-04-17 22:28:33

  0

ok thanks for the info, I will have a crack at it , not sure how it will go given my coding skills



ivanf
2017-04-22 22:25:47

  0

Hi there again,

edits were done as you suggested , but I've hit an error . Please see here
https://www.dropbox.com/s/xc8zwf58ys9gja2/2017-04-23_11-43-22.png?dl=0

appears to not like the statement
VectorD close = close;


Also, its not clear to me how I actually ''hardcode'' the symbol into this ? I guess I create a param to input the symbol string, but not sure what to do with it after that

Any help would be appreciated.

thanks
Ivan



QuantShare
2017-04-24 04:22:37

  0

You should give it another name.

VectorD close = close1; // where "close1" is the name of one parameter

Since you are passing the close, open, high and low of a specific security, there is no need to pass the symbol name.
For the "close1" parameter for example, you should pass in QS language: Getseries("GOOG", close)



ivanf
2017-04-27 02:21:41

  0

looks like I have success now !


thanks for your help



No more messages
0




Reply:

No html code. URLs turn into links automatically.

Type in the trading objects you want to include: - Add Objects
To add a trading object in your message, type in the object name, select it and then click on "Add Objects"










QuantShare

Trading Items
Stock market prediction using traderbots
Trading rules using the VIX Index
Put-Call Ratio Index using Market Makers Trades
Market Volatility - Composite Indicator using Stocks Standard Dev...
Supertrend Indicator

How-to Lessons
How to draw an horizontal segment line using QS language
How to plot a stock using different periods in the same chart
How to optimize a neural network using a genetic algorithm
How to get stocks for a particular index using the global script
How to calculate the average of a time series using the money man...

Related Forum Threads
Dynamic Symbol Using CompareAssets()
Can I update the sector and industry information in the stock dat...
supertrend
Best practices for using a Global Variable
Problem drawing trend line using version 2.91

Blog Posts
Trading Items: Data Download using .Net Scripts
Create a trading strategy using the money management tool - Part ...
Using Average True Range to Measure Intraday Volatility
How to create a trading system, screen and composite using earnin...
Forecast volatility using trading rules and neural networks









QuantShare
Product
QuantShare
Features
Create an account
Affiliate Program
Support
Contact Us
Trading Forum
How-to Lessons
Manual
Company
About Us
Privacy
Terms of Use

Copyright © 2024 QuantShare.com
Social Media
Follow us on Facebook
Twitter Follow us on Twitter
Google+
Follow us on Google+
RSS Trading Items



Trading financial instruments, including foreign exchange on margin, carries a high level of risk and is not suitable for all investors. The high degree of leverage can work against you as well as for you. Before deciding to invest in financial instruments or foreign exchange you should carefully consider your investment objectives, level of experience, and risk appetite. The possibility exists that you could sustain a loss of some or all of your initial investment and therefore you should not invest money that you cannot afford to lose. You should be aware of all the risks associated with trading and seek advice from an independent financial advisor if you have any doubts.