// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ //@version=4 //author: @kivancozbilgic // www.BillHowell.ca 19Mar2021 [adaptations, incremental code] // https://www.tradingview.com/chart/kfkUtvGu/# //********************************** // Here's my first shot at learning TradingView PineScript. I have adapted Kıvanç Özbilgiç's Turtle Trade PineScript : //https://tr.tradingview.com/script/pB5nv16J/?utm_source=notification_email&utm_medium=email&utm_campaign=notification_pubscript_update // //I've also added an 83-year semi-log detrended SP500 index, which is explained at : //http://www.billhowell.ca/economics,%20markets/SP500/multi-fractal/1872-2020%20SP500%20index,%20ratio%20of%20opening%20price%20to%20semi-log%20detrended%20price.html // 16Mar2021 start upgrade- add Fibonacci mirror // 19Mar2021 // Why does the "All" timescale go back only to ~2003? // The timescale selections still use the full data range of ~2003-present, // rather than restricting the [min, max] to the timeframe covered!! // 21Mar2021 upgrade with Fibonacci levels, [smalest, biggest]smalest(y_to_yTrend,2500) study(title="Turtle Trade Channels Indicator", shorttitle="Özbilgiç", overlay=true, resolution="") //+-----+ // Setup - generic functions, global variables year_fraction = time / 1000 / 3600 / 24 / 365.25 + 1970 // https://www.tradingview.com/pine-script-docs/en/v4/appendix/HOWTOs.html#count-bars-in-a-dataset // Find the highest and lowest values for the entire dataset // 20Mar2021 Howell - adapt this for viewable data // as used further below, a series length of 2500 is just a simple guess. It should be calculated biggest(series,size) => max = 0.0 for i = 0 to size if series[i] > max max := series[i] max smalest(series,size) => min = 1e5 for i = 0 to size if series[i] < min min := series[i] min // n_bars = total number of bars shown in "viewable window", standard timeperiods only! // adapted from : // https://stackoverflow.com/questions/50087999/how-to-know-which-time-period-you-are-on-similar-to-isdaily-isweekly // how to know which time period you are on similar to isdaily, isweekly? // answered May 17 '18 at 6:07, Harold // "1D" - 1s chart? // "5D" - 5m chart // "1M" - 30m chart // "3M" - 1h chart // "6M" - 2h chart // "1Y" - 1D chart // "5Y" - 1W chart // timeframe.isdwm // timeframe.isintraday // timeframe.isminutes // timeframe.isseconds // timeframe.multiplier // timeframe.period n_bars = 2500 if timeframe.period == "1D" n_bars = 24*60*60 else if timeframe.period == "5D" n_bars = 24*60/5*7 else if timeframe.period == "1M" n_bars = 24*60*30 else if timeframe.period == "3M" n_bars = 24*30*3 else if timeframe.period == "6M" n_bars = 60/5*30*6 else if timeframe.period == "1Y" n_bars = 30*12 else if timeframe.period == "5Y" n_bars = 30*12*5 else if timeframe.period == "All" n_bars = 30*12*50 //+-----+ // calculate 83 year detrended data, [upper, lower] "Fibonnacci mirror trendlines" for a time period calc_yTrend(x) => expnt = 0.792392+0.0289587*(x - 1926.25) // for 1871-1926 =10^( 0.784617 + (0.000140925*(x - 1871.08))) SP500base = pow(10,expnt) y_Trend = calc_yTrend(year_fraction) y_to_yTrend = close / y_Trend y_to_yTrend_min = smalest(y_to_yTrend,n_bars) y_to_yTrend_max = biggest(y_to_yTrend,n_bars) y_Trend_minLine = y_Trend * y_to_yTrend_min y_Trend_maxLine = y_Trend * y_to_yTrend_max //plot(y_Trend_minLine, title="y_Trend_min", color=color.red, linewidth=1, style=6) //plot(y_Trend_maxLine, title="y_Trend_max", color=color.green, linewidth=1, style=6) //plot(barstate.islast ? y_Trend_minLine : na, title="y_Trend_min", color=color.red, linewidth=1, style=6) //plot(barstate.islast ? y_Trend_maxLine : na, title="y_Trend_max", color=color.green, linewidth=1, style=6) //+-----+ // plot Fibonacci mirror // 16Mar2015 Howell adapted from "TradingView auto fib extension" standard PineScript library // The Fibonacci series is mirrored [above, below] 1.000 : // * = non-primary-Fibonacci but often used (also 0.65?) // 0.055728 17.94430 // 0.090170 11.09020 // 0.145898 6.854100 // 0.236072 4.236000 // 0.381971 2.618000 // 0.500000 2.000000 * // 0.618047 1.618000 // 0.786164 1.272000 * // 1.000000 1.000000 // 1.272000 0.786164 * // 1.618000 0.381971 // 2.000000 0.500000 * // 2.618000 0.618047 // 4.236000 0.236072 // 6.854100 0.145898 // 11.09020 0.090170 // 17.94430 0.055728 // seems that I must calculate all, even though some will noe used? y_Trend_0_055 = 0.055728 * y_Trend y_Trend_0_090 = 0.090170 * y_Trend y_Trend_0_145 = 0.145898 * y_Trend y_Trend_0_236 = 0.236072 * y_Trend y_Trend_0_381 = 0.381971 * y_Trend y_Trend_0_500 = 0.500000 * y_Trend y_Trend_0_618 = 0.618047 * y_Trend y_Trend_0_786 = 0.786164 * y_Trend y_Trend_1_000 = 1.000000 * y_Trend y_Trend_1_272 = 1.272000 * y_Trend y_Trend_1_618 = 1.618000 * y_Trend y_Trend_2_000 = 2.000000 * y_Trend y_Trend_2_618 = 2.618000 * y_Trend y_Trend_4_236 = 4.236000 * y_Trend y_Trend_6_854 = 6.854100 * y_Trend y_Trend_11_09 = 11.09020 * y_Trend y_Trend_17_94 = 17.94430 * y_Trend // pain in the butt : cannot put "plot" within a conditional, to reduce all below to one function!!??!! plot(y_to_yTrend_min < 0.055728 and 0.055728 < y_to_yTrend_max ? y_Trend_0_055 : na, title="0.05", color=color.gray, linewidth=1, style=6) plot(y_to_yTrend_min < 0.090170 and 0.090170 < y_to_yTrend_max ? y_Trend_0_090 : na, title="0.09", color=color.gray, linewidth=1, style=6) plot(y_to_yTrend_min < 0.145898 and 0.145898 < y_to_yTrend_max ? y_Trend_0_145 : na, title="0.14", color=color.gray, linewidth=1, style=6) plot(y_to_yTrend_min < 0.236072 and 0.236072 < y_to_yTrend_max ? y_Trend_0_236 : na, title="0.23", color=color.gray, linewidth=1, style=6) plot(y_to_yTrend_min < 0.381971 and 0.381971 < y_to_yTrend_max ? y_Trend_0_381 : na, title="0.38", color=color.gray, linewidth=1, style=6) plot(y_to_yTrend_min < 0.500000 and 0.500000 < y_to_yTrend_max ? y_Trend_0_500 : na, title="0.50", color=color.gray, linewidth=1, style=6) plot(y_to_yTrend_min < 0.618047 and 0.618047 < y_to_yTrend_max ? y_Trend_0_618 : na, title="0.61", color=color.gray, linewidth=1, style=6) plot(y_to_yTrend_min < 0.786164 and 0.786164 < y_to_yTrend_max ? y_Trend_0_786 : na, title="0.78", color=color.gray, linewidth=1, style=6) plot(y_to_yTrend_min < 1.000000 and 1.000000 < y_to_yTrend_max ? y_Trend_1_000 : na, title="1.00", color=color.orange, linewidth=1, style=6) plot(y_to_yTrend_min < 1.272000 and 1.272000 < y_to_yTrend_max ? y_Trend_1_272 : na, title="1.27", color=color.gray, linewidth=1, style=6) plot(y_to_yTrend_min < 1.618000 and 1.618000 < y_to_yTrend_max ? y_Trend_1_618 : na, title="1.61", color=color.gray, linewidth=1, style=6) plot(y_to_yTrend_min < 2.000000 and 2.000000 < y_to_yTrend_max ? y_Trend_2_000 : na, title="2.00", color=color.gray, linewidth=1, style=6) plot(y_to_yTrend_min < 2.618000 and 2.618000 < y_to_yTrend_max ? y_Trend_2_618 : na, title="2.61", color=color.gray, linewidth=1, style=6) plot(y_to_yTrend_min < 4.236000 and 4.236000 < y_to_yTrend_max ? y_Trend_4_236 : na, title="4.23", color=color.gray, linewidth=1, style=6) plot(y_to_yTrend_min < 6.854100 and 6.854100 < y_to_yTrend_max ? y_Trend_6_854 : na, title="6.85", color=color.gray, linewidth=1, style=6) plot(y_to_yTrend_min < 11.09020 and 11.09020 < y_to_yTrend_max ? y_Trend_11_09 : na, title="11.0", color=color.gray, linewidth=1, style=6) plot(y_to_yTrend_min < 17.94430 and 17.94430 < y_to_yTrend_max ? y_Trend_17_94 : na, title="17.9", color=color.gray, linewidth=1, style=6) // Howell end-code //********************************** //********************************** // Kıvanç Özbilgiç's Turtle Trade PineScript : length = input(20,"Entry Length", minval=1) len2=input(10, "Exit Length", minval=1) showsignals = input(title="Show Entry/Exit Signals ?" , type=input.bool, defval=true) highlighting = input(title="Highlighter On/Off ?" , type=input.bool, defval=true) lower = lowest(length) upper = highest(length) u = plot(upper, "Upper", color=#0094FF) l = plot(lower, "Lower", color=#0094FF) up=highest(high,length) down=lowest(low,length) sup=highest(high,len2) sdown=lowest(low,len2) K1=barssince(high>=up[1])<=barssince(low<=down[1]) ? down : up K2=iff(barssince(high>=up[1])<=barssince(low<=down[1]),sdown,sup) K3=iff(close>K1,down,na) K4=iff(close