StockFetcher Forums · Filter Exchange · Can Super Trend Strategy be rewritten/coded for use on SF?<< 1 2 >>Post Follow-up
ron22
255 posts
msg #145605
Ignore ron22
12/17/2018 10:19:01 PM

Thank you for any help. This is way above my current ability.

//The below is Super Trend Strategy
Factor1=input(1,type =float, minval=1,maxval = 100)
Factor2=input(2,type =float, minval=1,maxval = 100)
Factor3=input(3,type =float, minval=1,maxval = 100)
Pd1=input(10,minval=1,maxval = 100)
Pd2=input(10,minval=1,maxval = 100)
Pd3=input(10,minval=1,maxval = 100)
//ST 1
Up1=hl2-(Factor1*atr(Pd1))
Up2=hl2-(Factor2*atr(Pd2))
Up3=hl2-(Factor3*atr(Pd3))
Dn1=hl2+(Factor1*atr(Pd1))
Dn2=hl2+(Factor2*atr(Pd2))
Dn3=hl2+(Factor3*atr(Pd3))
//ST1
TrendUp1=close[1]>TrendUp1[1]? max(Up1,TrendUp1[1]) : Up1
TrendUp2=close[1]>TrendUp2[1]? max(Up2,TrendUp2[1]) : Up2
TrendUp3=close[1]>TrendUp3[1]? max(Up3,TrendUp3[1]) : Up3

TrendDown1=close[1] TrendDown2=close[1] TrendDown3=close[1]
//ADX
lenadx = input(14, minval=1, title="DI Length")
lensig = input(14, title="ADX Smoothing", minval=1, maxval=50)
up = change(high)
down = -change(low)
trur = rma(tr, lenadx)
plus = fixnan(100 * rma(up > down and up > 0 ? up : 0, lenadx) / trur)
minus = fixnan(100 * rma(down > up and down > 0 ? down : 0, lenadx) / trur)
sum = plus + minus
adx = 100 * rma(abs(plus - minus) / (sum == 0 ? 1 : sum), lensig)

Trend1 = close > TrendDown1[1] ? 1: close< TrendUp1[1]? -1: nz(Trend1[1],1)
Tsl1 = Trend1==1? TrendUp1: TrendDown1
Trend2 = close > TrendDown2[1] ? 1: close< TrendUp2[1]? -1: nz(Trend2[1],1)
Tsl2 = Trend2==1? TrendUp2: TrendDown2
Trend3 = close > TrendDown3[1] ? 1: close< TrendUp3[1]? -1: nz(Trend3[1],1)
Tsl3 = Trend3==1? TrendUp3: TrendDown3

Trend4 = sma(close,3) > sma(close,6) ? 1: -1

Trend5 = close > sma(close,3) ? 1: -1
Trend6 = close > sma(close,6) ? 1: -1

Trend7 = sma(close,3) > sma(close,3)[1] ? 1: -1
Trend8 = sma(close,6) > sma(close,6)[1] ? 1: -1

Trend9 = plus > minus ? 1: -1
Trend10 = close > sar(0.02,0.02,0.2) ? 1: -1

Trendsum = Trend1 + Trend2 + Trend3 + Trend4 + Trend5 +Trend6 + Trend7 + Trend8 + Trend9 + Trend10
plot(Trendsum,color = Trendsum > 0 ? green : red, style = columns, linewidth = 3,title="Trend")


nibor100
1,044 posts
msg #145610
Ignore nibor100
12/18/2018 12:45:16 PM

ron22,

At first glance it looks like it might be possible....but I would need to know what scripting language/program that code is from as my search on the web came up with some similar code but was it buried in posts not tied to a specific program or coding language.

For instance, on the lines beginning with Up1...Up3...Dn1...Dn3 your post has hi2 and I found on the web h12. yet neither place identifies what the initial value of hi2/h12 is???

In the lines similar to
"TrendUp1=close[1]>TrendUp1[1]? max(Up1,TrendUp1[1]) : Up1:"

a. some languages close[1] would mean today's close, in others yesterday's close etc.
b. Is the ? a command or a miscopied character,
c. and what in the world does ":Up1" perform/signify at the end of the statement? (hopefully its a comment)

So a couple more clues would be helpful as I've never heard of that indicator.

Thanks,
Ed S.




ron22
255 posts
msg #145617
Ignore ron22
12/18/2018 9:01:47 PM

Ed, I really appreciate your efforts. The code might be Pine but that is only a guess. I do not expect you to put a lot of time into this because I cannot answer your questions. Thank you for trying. Ron

nibor100
1,044 posts
msg #145619
Ignore nibor100
12/19/2018 11:51:19 AM

ron22,

Curiosity got the better of me...

1. You are correct that the code you posted is in Pine Script which apparently is the coding language used on the free Trading View website (which I did not know about..Thanks).

2. Researching further on the web the Supertrend Indicator is a lot simpler than the Supertrend Strategy code that you posted which appears to be a combination of 3 Supertrend lines along with a 3 period moving avg crossing a 6 period mov avg, close greater than 3 period avg, close > 6 period avg, 3 per avg > 3 per avg 1 day ago, same for 6 per avg, some kind of 14 day custom avg of daily changes in highs and lows, and a Parabolic Sar.

Those 10 trend indicators are made to be either 1 or -1 and are all added together to get the final TrendSum indicator to be plotted on the chart as green if >0 and red if <0.

3. I found the answers to my code questions in the Pine Script manual:

a. Your 'hI2' was correctly typed and stands for (high + low)/2

b. In the code line :
"TrendUp1=close[1]>TrendUp1[1]? max(Up1,TrendUp1[1]) : Up1:"

close[1] mean's yesterday's close
b. The ? is part of Ternary conditional operator command in Pine Script, and is basically an If, Then, Else shortcut with the info after the : being the else statement and after the ? being the Then statement.

4. Are you looking for that specific Supertrend Strategy to be coded for SF or do you just need a basic Supertrend filter to which you can add as many other indicators as you please?

Note that there is probably no way to code the red and green signals so they can be shown on a stockfetcher filter results chart; which is probably no big deal because you can look at that chart on Trading View anytime for free on free real time data if I understand what I'm reading on their site.

5. Below is the basic Supertrend indicator code for Metastock which might make it easier to follow the logic of the Supertrend indicator calculation:

"Factor:=Input("Factor",1.00,10.00,3.00);
Pd:=Input("ATR Periods",1,100,10);
Up:=MP()+(Factor*ATR(Pd));
Dn:=MP()-(Factor*ATR(Pd));
Td:=If(Cross(C,LLV(Up,13)),1,If(Cross(HHV(Dn,13),C ),-1,PREV));
Dnx:=If(Dn=HighestSince(1,Cross(Td,0),Dn),Dn,PREV) ;
Upx:=If(Up=LowestSince(1,Cross(0,Td),Up),Up,PREV);
ST:=If(Td=1,Dnx,If(Td=-1,Upx,PREV));
ST"

Ed S.














ron22
255 posts
msg #145624
Ignore ron22
12/19/2018 4:28:31 PM

Ed, You are amazing. Maybe I should call you Sherlock.
A basic Supertrend filter for SF would be fine for me.
I can view the red and green signals on Trading View.
Thank you again for your research and coding.

ron22
255 posts
msg #145639
Ignore ron22
12/20/2018 1:31:41 PM

The TrendSum filter listed below was written by Kevin. I wish that I had 10% of his talent. He was gracious enough to allow me to share it with SF members. 7 of the 10 components of the Supertrend Strategy/indicator are contained in the TrendSum filter. The other 3 items cannot be coded in SF.
Would someone please make this clickable. Thank you.

/*TRENDSUM*/

/*1. IS THE MA(3) ABOVE THE MA(6)? +1 FOR YES, -1 FOR NO*/
set{var1a, count(ma(3) above ma(6), 1)}
set{var1b, count(ma(3) below ma(6), 1)}
set{var1, var1a - var1b}

/*2. IS THE CLOSE ABOVE THE MA(3)? +1 FOR YES, -1 FOR NO*/
set{var2a, count(close above ma(3), 1)}
set{var2b, count(close below ma(3), 1)}
set{var2, var2a - var2b}

/*3. IS THE CLOSE ABOVE THE MA(6)? +1 FOR YES, -1 FOR NO*/
set{var3a, count(close above ma(6), 1)}
set{var3b, count(close below ma(6), 1)}
set{var3, var3a - var3b}

/*4. IS THE SLOPE OF MA(3) POSITIVE? +1 FOR YES, -1 FOR NO*/
set{var4a, count(ma(3) above ma(3) 1 day ago, 1)}
set{var4b, count(ma(3) below ma(3) 1 day ago, 1)}
set{var4, var4a - var4b}

/*5. IS THE SLOPE OF MA(6) POSITIVE? +1 FOR YES, -1 FOR NO*/
set{var5a, count(ma(6) above ma(6) 1 day ago, 1)}
set{var5b, count(ma(6) below ma(6) 1 day ago, 1)}
set{var5, var5a - var5b}

/*6. IS THE CLOSE ABOVE THE PARABOLIC SAR? +1 FOR YES, -1 FOR NO*/
set{var6a, count(close above Parabolic SAR(0.02,0.2), 1)}
set{var6b, count(close below Parabolic SAR(0.02,0.2), 1)}
set{var6, var6a - var6b}

/*7. IS THE +DI(14) ABOVE THE -DI(14)? +1 FOR YES, -1 FOR NO*/
set{var7a, count(PDI above MDI, 1)}
set{var7b, count(PDI below MDI, 1)}
set{var7, var7a - var7b}

/*SUMMING THE INDIVIDUAL INDICATORS INTO THE TRENDSUM*/
set{sum1, var1 + var2}
set{sum2, sum1 + var3}
set{sum3, sum2 + var4}
set{sum4, sum3 + var5}
set{sum5, sum4 + var6}
set{trendsum, sum5 + var7}
SYMLIST(SPY)
ADD COLUMN TRENDSUM
DRAW MA(3)
DRAW MA(6)
DRAW PARABOLIC SAR
DRAW TRENDSUM
PLOTTYPE{TRENDSUM, ZEROBAR}
DRAW ADX


Cheese
1,374 posts
msg #145641
Ignore Cheese
12/20/2018 1:44:24 PM


Fetcher[

/* code by Kevin_in_GA */

/*TRENDSUM*/

/*1. IS THE MA(3) ABOVE THE MA(6)? +1 FOR YES, -1 FOR NO*/
set{var1a, count(ma(3) above ma(6), 1)}
set{var1b, count(ma(3) below ma(6), 1)}
set{var1, var1a - var1b}

/*2. IS THE CLOSE ABOVE THE MA(3)? +1 FOR YES, -1 FOR NO*/
set{var2a, count(close above ma(3), 1)}
set{var2b, count(close below ma(3), 1)}
set{var2, var2a - var2b}

/*3. IS THE CLOSE ABOVE THE MA(6)? +1 FOR YES, -1 FOR NO*/
set{var3a, count(close above ma(6), 1)}
set{var3b, count(close below ma(6), 1)}
set{var3, var3a - var3b}

/*4. IS THE SLOPE OF MA(3) POSITIVE? +1 FOR YES, -1 FOR NO*/
set{var4a, count(ma(3) above ma(3) 1 day ago, 1)}
set{var4b, count(ma(3) below ma(3) 1 day ago, 1)}
set{var4, var4a - var4b}

/*5. IS THE SLOPE OF MA(6) POSITIVE? +1 FOR YES, -1 FOR NO*/
set{var5a, count(ma(6) above ma(6) 1 day ago, 1)}
set{var5b, count(ma(6) below ma(6) 1 day ago, 1)}
set{var5, var5a - var5b}

/*6. IS THE CLOSE ABOVE THE PARABOLIC SAR? +1 FOR YES, -1 FOR NO*/
set{var6a, count(close above Parabolic SAR(0.02,0.2), 1)}
set{var6b, count(close below Parabolic SAR(0.02,0.2), 1)}
set{var6, var6a - var6b}

/*7. IS THE +DI(14) ABOVE THE -DI(14)? +1 FOR YES, -1 FOR NO*/
set{var7a, count(PDI above MDI, 1)}
set{var7b, count(PDI below MDI, 1)}
set{var7, var7a - var7b}

/*SUMMING THE INDIVIDUAL INDICATORS INTO THE TRENDSUM*/
set{sum1, var1 + var2}
set{sum2, sum1 + var3}
set{sum3, sum2 + var4}
set{sum4, sum3 + var5}
set{sum5, sum4 + var6}
set{trendsum, sum5 + var7}
SYMLIST(SPY)
ADD COLUMN TRENDSUM
DRAW MA(3)
DRAW MA(6)
DRAW PARABOLIC SAR
DRAW TRENDSUM
PLOTTYPE{TRENDSUM, ZEROBAR}
DRAW ADX

]



Cheese
1,374 posts
msg #145642
Ignore Cheese
12/20/2018 1:47:14 PM

ron,
The following code by four may be used as a surrogate or building block
for the ATR part.
Good luck trading.

Fetcher[
/* https://www.stockfetcher.com/forums/Filter-Exchange/ATR-Bands/114192/-1/114192 */
/* code by four modified 6/22/2013 10:57:59 PM */


apply to symlist(SPY)


/* ATR BAND FILTER */
draw ma(14)
set{ tmp , atr(14)}
set{ line_upper, ma(14) + tmp }
set{ line_lower, ma(14) - tmp }

draw line_upper on plot close
draw line_lower on plot close

]



ron22
255 posts
msg #145644
Ignore ron22
12/20/2018 2:43:13 PM

Cheese, Thank you for sharing.

Cheese
1,374 posts
msg #145646
Ignore Cheese
modified
12/20/2018 3:22:45 PM

Here are a couple more ideas as surrogates for the ATR part




Fetcher[

/* https://www.stockfetcher.com/sfforums/?mid=144666 */
/* ATR idea adapted from davesaint86 */


apply to symlist(SPY)


Set{ADR10, ATR(20) * 1}
Set{ADR20, ATR(20) * 2}
Set{ADR30, ATR(20) * 3}
Set{ATR1, high 10 day high - ADR10}
Set{ATR2, high 10 day high - ADR20}
Set{ATR3, high 10 day high - ADR30}
Set{Buy1, high 10 day high - ADR10}
Set{Buy2, high 10 day high - ADR20}
Set{Buy3, high 10 day high - ADR30}


Set{ATR4, low 10 day low + ADR10}
Set{ATR5, low 10 day low + ADR20}
Set{ATR6, low 10 day low + ADR30}
Set{Sell1, low 10 day low + ADR10}
Set{Sell2, low 10 day low + ADR20}
Set{Sell3, low 10 day low + ADR30}


draw sell3 on plot price
draw sell2 on plot price
draw sell1 on plot price

draw buy1 on plot price
draw buy2 on plot price
draw buy3 on plot price

]






Fetcher[


/* https://www.stockfetcher.com/forums/Filter-Exchange/ATR-BANDS/47847 */
/* ATR BANDS idea from TheRumpledOne modified 11/6/2006 */

apply to symlist(SPY)


/* ATR BAND FILTER */
set{ line_mid , linear regression(20) }
set{ tmp , 1.764 * cma(atr(12),55) }
set{ line_upper, lower linear regression(20) + tmp }
set{ line_lower, upper linear regression(20) - tmp }

draw line_mid on plot close
draw line_upper on plot close
draw line_lower on plot close

do not draw linear regression(20)
do not draw lowerlinear regression(20)
do not draw upper linear regression(20)


]







StockFetcher Forums · Filter Exchange · Can Super Trend Strategy be rewritten/coded for use on SF?<< 1 2 >>Post Follow-up

*** Disclaimer *** StockFetcher.com does not endorse or suggest any of the securities which are returned in any of the searches or filters. They are provided purely for informational and research purposes. StockFetcher.com does not recommend particular securities. StockFetcher.com, Vestyl Software, L.L.C. and involved content providers shall not be liable for any errors or delays in the content, or for any actions taken based on the content.


Copyright 2022 - Vestyl Software L.L.C.Terms of Service | License | Questions or comments? Contact Us
EOD Data sources: DDFPlus & CSI Data Quotes delayed during active market hours. Delay times are at least 15 mins for NASDAQ, 20 mins for NYSE and Amex. Delayed intraday data provided by DDFPlus


This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.