├── screenshots ├── s1.png ├── s2.png ├── s3.png ├── s4.png ├── s5.png └── s6.png ├── pinescript ├── ind_hour_minutes.psc ├── ind_midsession_cng.psc ├── ind_flexible_atr.psc ├── rsi_multi_timeframe.psc ├── ind_roc_multi_timeframe.psc ├── ind_volume_candles.psc ├── ind_parabolic_sar.psc ├── ind_donch_multi_timeframe.psc ├── ind_atr_vol_breakout_v1.psc ├── ind_open_range_breakout.psc ├── ind_donch_multi_timeframe_close_only.psc ├── ind_trendless_v1.psc ├── donch_crossover.psc ├── ind_atr_multi_timeframe.psc ├── ind_rsi_multi_timeframe.psc ├── ind_benchmark_relative_strength.psc ├── ind_ohlc_multi_timeframe.psc ├── ind_eap_anchor.psc ├── ind_atr_stops_v1.psc ├── ind_parabolic_sar_multi_timeframe.psc ├── ind_vwap_anchor.psc ├── macd_histogram_momentum_v1.psc ├── ind_rsi_ema.psc ├── momentum_angle_strategy.psc ├── ind_oc_oscillator.psc ├── kalman_ma.psc ├── ind_donch_momentum_v1.psc ├── 2donch_margin_breakout_v2.psc ├── bollinger_bands_mean_reversion.psc ├── 2donch_margin_breakout.psc ├── atrdonch_margin_breakout.psc ├── atrdonch2_breakout.psc ├── ind_atr_multi_timeframe_with_bollinger_bands_v1.psc ├── ind_momentum.psc ├── ind_heikinashi.psc ├── ind_atr_stops_v2.psc ├── adx_multi_timeframe.psc ├── ind_adx_multi_timeframe.psc ├── roc_of_ema_crossover.psc ├── atrdonch_margin_breakout_v2.psc ├── ren1_with_atr_ema.psc ├── ind_bollinger_bands_multi_timeframe.psc ├── linear_regression_slope_strategy.psc ├── ind_fractal_adaptive_moving_average.psc ├── macd_crossover_v1.psc ├── kelner_ch_breakout.psc ├── ind_volatility_bb_bands_multi_timeframe.psc ├── ma_channel_v1.psc ├── ind_session_ema.psc ├── ema_crossover_with_rsi_filter.psc ├── supertrend.psc ├── ind_trend_filter_using_cumsum_of_ma_crossovers.psc ├── ind_atr_multi_timeframe_with_donch.psc ├── donch_breakout_with_rsi_filter.psc ├── ind_roc_trend_strength.psc ├── ema_crossover_with_rsi_multi_timeframe_filter.psc ├── ind_ehlers_time_warp_laguerre_filter_v1.psc ├── ind_donch_volatility_v1.psc ├── ind_adx_multi_timeframe_with_lag_signal.psc ├── ind_pivot_levels_v1.psc ├── r1_s1_dmark_breakout.psc ├── ind_moving_average_channel_MAC.psc ├── ind_rsi_ehlers_instantaneous_trend.psc ├── ind_moving_averages.psc ├── dema_crossover_with_rsi_multi_timeframe_filter.psc ├── ind_ehlers_instantaneous_trend.psc ├── ind_stochastic_multi_timeframe.psc ├── rsi_rsi_crossover.psc ├── rsi_mean_reversion_v3.psc ├── ind_volatility_bb_kt_multi_timeframe.psc ├── donch_basis_crossover_with_its_lag.psc ├── r1_s1_breakout.psc ├── r1_s1_M_breakout.psc ├── ind_cpr_daily_weekly_combined.psc ├── ind_pivot_levels_ol2_v1.psc ├── roc-pct_mean_reversion_v1.psc ├── ind_ehlers_time_warp_laguerre_rsi_v1.psc ├── rsi_ch_breakout_v2.psc ├── heikinashi_donch_breakout.psc ├── ind_candlestick_patterns.psc ├── inverse_fisher_transform_on_rsi.psc ├── rsi_mean_reversion_v4_roc-pct_of_rsi.psc ├── guppy_ma_cross.psc ├── ind_pivot_points_std.psc ├── ind_orb-c.psc ├── roc_pct_cross50_touch0.psc ├── orb-a_strategy.psc ├── r1_s1_dmark_breakout_intraday.psc ├── ind_ehlers_fisher_transform.psc ├── rsi_mean_reversion_v2.psc ├── orb-b_basis_cross_with_its_lag_v1.psc ├── rsi_mean_reversion_using_single_ema_filter_and_ema_crossover_as_exit.psc ├── supertrend_hl_smoothing.psc ├── rsi_mean_reversion_using_single_ema_filter.psc ├── bollinger_bands_vol_expansion_trend_trader_with_ema_filter.psc ├── supertrend_with_rising_adx_filter.psc ├── r1_s1_classic_breakout.psc ├── r1_s1_fibonacci_breakout.psc ├── donch_breakout_with_mean_reversion_v1.psc ├── donch_bas_crossover_with_adx_filter.psc ├── ren3_crossover.psc ├── ren0.psc ├── vidya_crossover.psc ├── bollinger_rsi_v1.psc ├── ren1_heikinashi_crossover.psc ├── donch_1day_swing_trader_v1.psc ├── donch_breakout_scalper.psc ├── ind_ehlers_empirical_mode_decomposition.psc ├── mean_reversion_v1.psc ├── ren1_heikinashi_crossover_with_multi_timeframe_atr.psc ├── donch_breakout_v1.psc ├── bollinger_bands_mean_reversion_mtr_v1.psc ├── bollinger_bands_mean_reversion_with_ema_filter.psc ├── ind_pivot_points_flexible.psc ├── pivot_levels_breakout_v4.psc ├── supertrend_hl_smoothing_with_rising_adx_filter.psc ├── linreg_donch_regime_filter_with_ema_trend_filter.psc ├── roc_of_ema_with_donch_mid_cross.psc ├── rsi_ch_breakout.psc ├── pivot_levels_breakout_v5.psc ├── ind_rsi_orb-b.psc ├── roc_of_ema.psc ├── vwap_anchored_multi_timeframe_crossover.psc ├── mean_reversion_rsi_trend_isolator_ehlers_instantaneous_trend_v1.psc ├── ind_orb-b_on_rsi_v1.psc ├── rsi_trend_isolator_ehlers_instantaneous_trend_v1.psc ├── ind_orb-a_with_pivots.psc ├── ehlers_time_warp_laguerre_rsi_threshold_v1.psc ├── pivot_levels_breakout_v1.psc └── fisher_transform_mtr_trend_trader.psc ├── README.md └── afl ├── legacy ├── ema_crossover.afl ├── donch_breakout.afl ├── donch_ema_crossover.afl └── mtf_pivot_system_indicator.afl ├── donch_channel_breakouts_with_exit_via_basis_crossovers_v1.afl └── r1_s1_breakouts.afl /screenshots/s1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vikaschouhan/algotrade/HEAD/screenshots/s1.png -------------------------------------------------------------------------------- /screenshots/s2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vikaschouhan/algotrade/HEAD/screenshots/s2.png -------------------------------------------------------------------------------- /screenshots/s3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vikaschouhan/algotrade/HEAD/screenshots/s3.png -------------------------------------------------------------------------------- /screenshots/s4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vikaschouhan/algotrade/HEAD/screenshots/s4.png -------------------------------------------------------------------------------- /screenshots/s5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vikaschouhan/algotrade/HEAD/screenshots/s5.png -------------------------------------------------------------------------------- /screenshots/s6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vikaschouhan/algotrade/HEAD/screenshots/s6.png -------------------------------------------------------------------------------- /pinescript/ind_hour_minutes.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | study("Hour minutes", overlay=false) 3 | 4 | plot(hour, title='Hour') 5 | plot(minute, title='Minutes') 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Algotrade 2 | A collection of trading indicators/algorithms for Tradingview (Pinescript) and Amibroker. Some of these algorithms have been innovatively designed by me only based on some specific observations. 3 | This project is made available under GNU GPLv2. 4 | -------------------------------------------------------------------------------- /pinescript/ind_midsession_cng.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | study("Mid-Session change", overlay=true) 3 | 4 | is_newbar2(res, sess) => 5 | t_cng = change(time(res, sess)) 6 | tt_cng = change(na(t_cng) ? 1:0) != 0 7 | // 8 | 9 | plot(valuewhen(is_newbar2('D', '0900-1200'), close, 0)) 10 | -------------------------------------------------------------------------------- /pinescript/ind_flexible_atr.psc: -------------------------------------------------------------------------------- 1 | //@version=3 2 | study(title="FTR", shorttitle="FTR") 3 | ftr_lag = input(9) 4 | ftr_ema_len = input(14) 5 | 6 | ftr(lag, ftr_len) => 7 | tr_t = max(max((high - low), (high - close[lag])), (low - close[lag])) 8 | ftr_t = ema(tr_t, ftr_len) 9 | 10 | plot(ftr(ftr_lag, ftr_ema_len), color=red, linewidth=2) 11 | -------------------------------------------------------------------------------- /pinescript/rsi_multi_timeframe.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | study("RSI multiframe", overlay=false) 3 | 4 | rsi_len = input(title="RSI Length", defval=14) 5 | rsi_res = input(title="RSI timeframe", type=input.resolution, defval="1D") 6 | 7 | rsi_ind = rsi(close, rsi_len) 8 | rsi_ind_m = security(syminfo.tickerid, rsi_res, rsi_ind) 9 | 10 | plot(rsi_ind_m, color=color.blue) 11 | -------------------------------------------------------------------------------- /pinescript/ind_roc_multi_timeframe.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | study("ROC MTF", "ROC MTF", overlay=false) 3 | time_frame = input(defval='1D', type=input.resolution, title='Time Frame') 4 | period = input(defval=14, type=input.integer, title='Loopback Period') 5 | 6 | // Calculate roc for time frame 7 | roc_t = security(syminfo.tickerid, time_frame, roc(close, period)) 8 | 9 | // Plot 10 | plot(roc_t, color=color.blue, title='ROC MTF') 11 | -------------------------------------------------------------------------------- /pinescript/ind_volume_candles.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | study("Volume Candles", overlay=false) 3 | 4 | candle_up = (close > open) ? 1:-1 5 | color_up = (candle_up == 1) ? color.green:color.red 6 | vol_sum = cum(volume* candle_up) 7 | vol_open = vol_sum[1] 8 | vol_close = vol_sum 9 | vol_high = (vol_open < vol_close) ? vol_close:vol_open 10 | vol_low = (vol_open < vol_close) ? vol_open:vol_close 11 | 12 | plotcandle(vol_open, vol_high, vol_low, vol_close, color=color_up) 13 | -------------------------------------------------------------------------------- /pinescript/ind_parabolic_sar.psc: -------------------------------------------------------------------------------- 1 | //@version=3 2 | study("Parabolic SAR", shorttitle="PSAR", overlay=true) 3 | 4 | psar_start = input(title="PSAR Start", type=float, step=0.001, defval=0.02) 5 | psar_increment = input(title="PSAR Increment", type=float, step=0.001, defval=0.02) 6 | psar_maximum = input(title="PSAR Maximum", type=float, step=0.01, defval=0.2) 7 | 8 | psar = sar(psar_start, psar_increment, psar_maximum) 9 | dir = psar < close ? 1 : -1 10 | 11 | psarColor = dir == 1 ? #3388bb : #fdcc02 12 | plot(psar, title="PSAR", style=circles, color=psarColor, linewidth=2) 13 | -------------------------------------------------------------------------------- /pinescript/ind_donch_multi_timeframe.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | study("Donchian Multi Timeframe", overlay=true) 3 | 4 | donch_len = input(title="Donchian length", defval=2, minval=1) 5 | donch_res = input(title="Dinchian Resolution", defval='1D', type=input.resolution) 6 | 7 | donch_hi = highest(donch_len) 8 | donch_lo = lowest(donch_len) 9 | 10 | donch_hi_tf = security(syminfo.tickerid, donch_res, donch_hi) 11 | donch_lo_tf = security(syminfo.tickerid, donch_res, donch_lo) 12 | 13 | plot(donch_hi_tf, color=color.green, title='Donch HI') 14 | plot(donch_lo_tf, color=color.red, title='Donch LO') 15 | -------------------------------------------------------------------------------- /pinescript/ind_atr_vol_breakout_v1.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | study("ATR volatility breakout v1", overlay=false) 3 | 4 | atr_len_1 = input(defval=4, title='Shorter ATR Length', type=input.integer) 5 | atr_len_2 = input(defval=14, title='Longer ATR Length', type=input.integer) 6 | atr_tf = input(defval='', title='ATR Time Frame', type=input.resolution) 7 | 8 | atr_1 = security(syminfo.tickerid, atr_tf, atr(atr_len_1)) 9 | atr_2 = security(syminfo.tickerid, atr_tf, atr(atr_len_2)) 10 | 11 | plot(atr_1, color=color.blue, title='Short ATR') 12 | plot(atr_2, color=color.black, title='Long ATR') 13 | -------------------------------------------------------------------------------- /pinescript/ind_open_range_breakout.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | study(title="ORB", shorttitle="ORB", overlay=true) 3 | 4 | tframe = input(defval="15", type=input.resolution) 5 | 6 | is_newbar(res) => 7 | change(time(res)) != 0 8 | // 9 | 10 | high_range = valuewhen(is_newbar('D'), high, 0) 11 | low_range = valuewhen(is_newbar('D'), low, 0) 12 | 13 | high_rangeL = security(syminfo.tickerid, tframe, high_range) 14 | low_rangeL = security(syminfo.tickerid, tframe, low_range) 15 | 16 | plot(high_rangeL, color=color.green, linewidth=2) 17 | plot(low_rangeL, color=color.red, linewidth=2) 18 | 19 | -------------------------------------------------------------------------------- /pinescript/ind_donch_multi_timeframe_close_only.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | study("Donchian Multi Timeframe - Close only", overlay=true) 3 | 4 | donch_len = input(title="Donchian length", defval=2, minval=1) 5 | donch_res = input(title="Dinchian Resolution", defval='1D', type=input.resolution) 6 | 7 | donch_hi = highest(close, donch_len) 8 | donch_lo = lowest(close, donch_len) 9 | 10 | donch_hi_tf = security(syminfo.tickerid, donch_res, donch_hi) 11 | donch_lo_tf = security(syminfo.tickerid, donch_res, donch_lo) 12 | 13 | plot(donch_hi_tf, color=color.green, title='Donch HI') 14 | plot(donch_lo_tf, color=color.red, title='Donch LO') 15 | -------------------------------------------------------------------------------- /pinescript/ind_trendless_v1.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | study("trendless indicator v1", overlay=false) 3 | 4 | rsi_len = input(14, title='RSI Length', type=input.integer) 5 | rsi_bhi = input(60, title='RSI Band High', type=input.integer) 6 | rsi_blo = input(40, title='RSI Band Low', type=input.integer) 7 | rsi_dlen = input(50, title='RSI Donchian Length', type=input.integer) 8 | rsi_tf = input('', title='RSI Time Frame', type=input.resolution) 9 | 10 | rsi_t = security(syminfo.tickerid, rsi_tf, rsi(close, rsi_len)) 11 | tr_less = iff((highest(rsi_t, rsi_dlen) <= rsi_bhi and lowest(rsi_t, rsi_dlen) >= rsi_blo), 1.0, 0.0) 12 | 13 | plot(tr_less) 14 | -------------------------------------------------------------------------------- /pinescript/donch_crossover.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | strategy(title='Donch Crossover', shorttitle='Donch Crossover', overlay=true, slippage=20) 3 | 4 | donch_1 = input(defval=25, title="Short Donchian Length") 5 | donch_2 = input(defval=30, title="Long Donchian Length") 6 | 7 | bas_1 = (highest(donch_1) + lowest(donch_1))/2 8 | bas_2 = (highest(donch_2) + lowest(donch_2))/2 9 | 10 | buy = crossover(bas_1, bas_2) 11 | sell = crossunder(bas_1, bas_2) 12 | 13 | strategy.entry("long", strategy.long, when = buy) 14 | strategy.entry("short", strategy.short, when = sell) 15 | 16 | plot(bas_1, color=color.red) 17 | plot(bas_2, color=color.green) 18 | -------------------------------------------------------------------------------- /pinescript/ind_atr_multi_timeframe.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | study('ATR multitimeframe', overlay=false) 3 | 4 | atr_len = input(title="ATR Length", defval=20) 5 | don_len = input(title="Donchian Length", defval=20) 6 | atr_res = input(title="ATR timeframe", type=input.resolution, defval="1D") 7 | 8 | atr_new = security(syminfo.tickerid, atr_res, atr(atr_len)) 9 | d_hi = highest(atr_new, don_len) 10 | d_lo = lowest(atr_new, don_len) 11 | d_mid = avg(d_hi, d_lo) 12 | 13 | plot(atr_new, color=color.black, title='ATR') 14 | plot(d_hi, color=color.green, title="D+") 15 | plot(d_lo, color=color.red, title="D-") 16 | plot(d_mid, color=color.orange, title="D=") 17 | -------------------------------------------------------------------------------- /pinescript/ind_rsi_multi_timeframe.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | study("RSI multiframe", overlay=false) 3 | 4 | rsi_len = input(title="RSI Length", defval=14) 5 | rsi_res = input(title="RSI timeframe", type=input.resolution, defval="1D") 6 | rsi_elen = input(title="EMA smooth", type=input.integer, defval=6) 7 | 8 | rsi_ind = rsi(close, rsi_len) 9 | rsi_ema = ema(rsi_ind, rsi_elen) 10 | rsi_ind_m = security(syminfo.tickerid, rsi_res, rsi_ind) 11 | rsi_ema_m = security(syminfo.tickerid, rsi_res, rsi_ema) 12 | 13 | plot(rsi_ind_m, color=color.blue, title='RSI') 14 | plot(rsi_ema_m, color=color.olive, title='RSI-d') 15 | plot(50.0, color=color.red, title='L50', linewidth=2) 16 | -------------------------------------------------------------------------------- /pinescript/ind_benchmark_relative_strength.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | strategy("Benchmark Relative Strength", overlay=false) 3 | 4 | benchmark_ticker = input(defval='NSE:NIFTY', type=input.string, options=['NSE:NIFTY'], title='Benchmark') 5 | source = input(defval=close, type=input.source, title='Source') 6 | time_frame = input(defval='1D', type=input.resolution, title='Time Frame') 7 | 8 | benchmark = security(benchmark_ticker, time_frame, source) 9 | source_tf = security(syminfo.tickerid, time_frame, source) 10 | rel_str_sig = source_tf/benchmark 11 | 12 | plot(rel_str_sig, color=color.blue, title='Relative Strength wrt BenchMark') 13 | -------------------------------------------------------------------------------- /pinescript/ind_ohlc_multi_timeframe.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | study("Ohlc Multi TimeFrame", overlay=true) 3 | 4 | // inputs 5 | time_frame = input('1D', type=input.resolution, title='Time Frame') 6 | 7 | // Get multi timeframe OHLC 8 | open_t = security(syminfo.tickerid, time_frame, open) 9 | close_t = security(syminfo.tickerid, time_frame, close) 10 | high_t = security(syminfo.tickerid, time_frame, high) 11 | low_t = security(syminfo.tickerid, time_frame, low) 12 | 13 | // Plot 14 | plot(open_t, color=color.green, title='O') 15 | plot(close_t, color=color.red, title='C') 16 | plot(high_t, color=color.orange, title='H') 17 | plot(low_t, color=color.olive, title='L') 18 | -------------------------------------------------------------------------------- /pinescript/ind_eap_anchor.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | study("EAP multitimeframe", overlay=true) 3 | 4 | time_frame = input(defval="W", type=input.resolution, title="Time frame") 5 | 6 | start_time = security(syminfo.tickerid, time_frame, time) 7 | new_session = iff(change(start_time), 1, 0) 8 | 9 | //------------------------------------------------ 10 | eap_sum_fn() => 11 | eap_sum = 0.0 12 | eap_sum := (new_session == 1) ? hl2 : (eap_sum[1]+hl2) 13 | eap_sum 14 | // 15 | e_sum_fn() => 16 | p_sum = 0.0 17 | p_sum := (new_session == 1) ? 1 : (p_sum[1]+1) 18 | p_sum 19 | // 20 | 21 | eap_sig = eap_sum_fn()/e_sum_fn() 22 | plot(eap_sig, title="EAP", color=color.blue) 23 | -------------------------------------------------------------------------------- /pinescript/ind_atr_stops_v1.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | study("ATR Trailing Stoploss v1", overlay=true) 3 | 4 | atr_period = input(defval=5, title="ATR Period", minval=1, maxval=500) 5 | lb_period = input(defval=10, title="Loopback Period", minval=1, maxval=500) 6 | atr_mult = input(defval=1.5, title="ATR Multiplier", minval=0.1) 7 | 8 | ts_long = highest(high - atr_mult*atr(atr_period), lb_period) 9 | ts_short = lowest(low + atr_mult*atr(atr_period), lb_period) 10 | 11 | plot(ts_long, color=color.green, linewidth=1, style=plot.style_stepline, title="ATR Trailing Stoploss") 12 | plot(ts_short, color=color.red, linewidth=1, style=plot.style_stepline, title="ATR Trailing Stoploss") 13 | -------------------------------------------------------------------------------- /pinescript/ind_parabolic_sar_multi_timeframe.psc: -------------------------------------------------------------------------------- 1 | //@version=3 2 | study("Parabolic SAR Multi timeframe", shorttitle="PSAR MTF", overlay=true) 3 | 4 | psar_start = input(title="PSAR Start", type=float, step=0.001, defval=0.02) 5 | psar_increment = input(title="PSAR Increment", type=float, step=0.001, defval=0.02) 6 | psar_maximum = input(title="PSAR Maximum", type=float, step=0.01, defval=0.2) 7 | psar_resolution = input(title="Resolution", type=resolution, defval='1D') 8 | 9 | psar = security(tickerid, psar_resolution, sar(psar_start, psar_increment, psar_maximum)) 10 | dir = psar < close ? 1 : -1 11 | 12 | psarColor = dir == 1 ? #3388bb : #fdcc02 13 | plot(psar, title="PSAR", style=circles, color=psarColor, linewidth=2) 14 | -------------------------------------------------------------------------------- /pinescript/ind_vwap_anchor.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | study("VWAP multitimeframe", overlay=true) 3 | 4 | time_frame = input(defval="W", type=input.resolution, title="Time frame") 5 | 6 | start_time = security(syminfo.tickerid, time_frame, time) 7 | new_session = iff(change(start_time), 1, 0) 8 | 9 | //------------------------------------------------ 10 | vwap_sum_fn() => 11 | vwap_sum = 0.0 12 | vwap_sum := (new_session == 1) ? (hl2*volume) : (vwap_sum[1]+hl2*volume) 13 | vwap_sum 14 | // 15 | vol_sum_fn() => 16 | vol_sum = 0.0 17 | vol_sum := (new_session == 1) ? volume : (vol_sum[1]+volume) 18 | vol_sum 19 | // 20 | 21 | vwap_sig = vwap_sum_fn()/vol_sum_fn() 22 | plot(vwap_sig, title="VWAP", color=color.blue) 23 | -------------------------------------------------------------------------------- /pinescript/macd_histogram_momentum_v1.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | study("macd histogram momentum v1", overlay=false) 3 | 4 | macd_fastlen = input(12, title='MACD fast length', type=input.integer) 5 | macd_slowlen = input(26, title='MACD slow length', type=input.integer) 6 | macd_lastn = input(10, title='MACD last n bars', type=input.integer) 7 | macd_tf = input('', title='MACD Time Frame', type=input.resolution) 8 | 9 | [macdLine, signalLine, histLine] = security(syminfo.tickerid, macd_tf, macd(close, macd_fastlen, macd_slowlen, 9)) 10 | 11 | histline_h = highest(histLine, macd_lastn) 12 | histline_l = lowest(histLine, macd_lastn) 13 | 14 | histline_t = iff(histLine < histline_h[1], -1, iff(histLine > histline_l[1], 1, 0)) 15 | 16 | plot(histline_t) 17 | -------------------------------------------------------------------------------- /pinescript/ind_rsi_ema.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | study(title="RSI - EMA", overlay=false) 3 | 4 | /////////////////////////////////////////////////////////////////////// 5 | // Inputs 6 | time_frame = input(defval='1D', title="Resolution", type=input.resolution) 7 | ema_len = input(defval=2, title="EMA Length") 8 | rsi_len = input(defval=2, title="RSI Length") 9 | 10 | /////////////////////////////////////////////////////////////////////// 11 | // Indicators 12 | ema_ind_t = ema(close, ema_len) 13 | rsi_ind_t = rsi(ema_ind_t, rsi_len) 14 | rsi_ind = security(syminfo.tickerid, time_frame, rsi_ind_t) 15 | 16 | ///////////////////////////////////////////////////////////////////////// 17 | // Plots 18 | plot(rsi_ind, color=color.green, linewidth=1) 19 | -------------------------------------------------------------------------------- /pinescript/momentum_angle_strategy.psc: -------------------------------------------------------------------------------- 1 | //@version=3 2 | strategy("Momentum Str1", overlay=true, slippage=20) 3 | 4 | ema_len = input(9, type=integer, title="EMA Length") 5 | moms_thr = input(0.01, type=float, title="Mom Slope Threshold", step=0.01) 6 | slope_p = input(2, type=integer, title="Slope Lookback period") 7 | 8 | ema_t = ema(close, ema_len) 9 | 10 | buy = (ema_t - ema_t[slope_p]) > moms_thr 11 | selli = (ema_t - ema_t[slope_p]) < moms_thr 12 | sell = (ema_t - ema_t[slope_p]) < -moms_thr 13 | buyi = (ema_t - ema_t[slope_p]) > -moms_thr 14 | 15 | strategy.entry("L", strategy.long, when = buy) 16 | strategy.exit("L", when = selli) 17 | strategy.entry("S", strategy.short, when = sell) 18 | strategy.exit("S", when = buyi) 19 | 20 | plot(ema_t, color=red) 21 | -------------------------------------------------------------------------------- /pinescript/ind_oc_oscillator.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | study("O/C oscillator", overlay=false) 3 | close_look_back_len = input(defval=9, type=input.integer, title='Lookback period for close') 4 | open_look_back_len = input(defval=18, type=input.integer, title='Lookback period for open') 5 | time_frame = input(defval='5', type=input.resolution, title='Time Frame') 6 | 7 | close_smooth_s = ema(close, close_look_back_len) 8 | open_smooth_s = ema(open, open_look_back_len) 9 | ind_sig_s = (close_smooth_s - open_smooth_s) 10 | ind_sig = security(syminfo.ticker, time_frame, ind_sig_s) 11 | 12 | plot(ind_sig, color=color.blue, title='OC oscillator') 13 | plot(0.0, color=color.black, title='L0') 14 | -------------------------------------------------------------------------------- /pinescript/kalman_ma.psc: -------------------------------------------------------------------------------- 1 | //@version=3 2 | strategy("Kalman MA",shorttitle="KLMF",overlay=true) 3 | ma_period = input(1, title="ma period", type=integer) 4 | p = ohlc4 5 | value1 = 0.0 6 | value2 = 0.0 7 | klmf = 0.0 8 | 9 | value1 := 0.2*(p-p[1]) + 0.8*nz(value1[1]) 10 | value2 := 0.1*(high-low)+0.8*nz(value2[1]) 11 | lambda = abs(value1/value2) 12 | 13 | alpha = (-lambda*lambda + sqrt(lambda*lambda*lambda*lambda + 16*lambda*lambda))/8 14 | klmf := alpha*p + (1-alpha)*nz(klmf[1]) 15 | klmf_ema = ema(klmf, ma_period) 16 | 17 | plot(klmf, linewidth=3, color=blue, title="klmf", editable = true) 18 | 19 | long = crossover(klmf, klmf_ema) 20 | short = crossunder(klmf, klmf_ema) 21 | 22 | strategy.entry("L", strategy.long, when=long) 23 | strategy.entry("S", strategy.short, when=short) 24 | -------------------------------------------------------------------------------- /pinescript/ind_donch_momentum_v1.psc: -------------------------------------------------------------------------------- 1 | //@version=5 2 | indicator("Donch momentum v1", overlay=false) 3 | 4 | ////////////////////////////////////////////////// 5 | // Parameters 6 | donch_period = input.int(defval=20, title='Donchian Period') 7 | mom_smooth_period = input.int(defval=3, title='Donchian Momentum Smooth Period') 8 | 9 | ////////////////////////////////////////////////////// 10 | // Functions 11 | d_avg(d_len) => 12 | math.avg(ta.highest(high, d_len), ta.lowest(low, d_len)) 13 | // 14 | 15 | //////////////////////////////////////////////////////// 16 | // Momentum indicator 17 | sig = ta.ema(close-d_avg(donch_period), mom_smooth_period) 18 | 19 | ///////////////////////////////////////////////////////// 20 | // Plots 21 | plot(sig, color=color.red, title='Donchian Momentum') 22 | -------------------------------------------------------------------------------- /pinescript/2donch_margin_breakout_v2.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | strategy(title="2Donch margin breakout", shorttitle="2Donch margin breakout", overlay=true, slippage=20) 3 | 4 | donch_len = input(defval=14, title="Donnchian Channel Length") 5 | donch_slen = input(defval=1, title="Smooth Length") 6 | margin = input(defval=2, title="Margin Points") 7 | 8 | hhv = highest(ema(high, donch_slen), donch_len) 9 | llv = lowest(ema(low, donch_slen), donch_len) 10 | ba = (hhv + llv)/2 11 | 12 | buy = crossover(close, (llv + margin)[1]) 13 | sell = crossunder(close, (hhv - margin)[1]) 14 | 15 | strategy.entry("L", strategy.long, when = buy) 16 | strategy.entry("S", strategy.short, when = sell) 17 | 18 | plot(hhv, color=color.red, linewidth=1, title="Donch HHV") 19 | plot(llv, color=color.blue, linewidth=1, title="Donch LLV") 20 | -------------------------------------------------------------------------------- /pinescript/bollinger_bands_mean_reversion.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | strategy("Bollinger Bands Mean Reversion", overlay=true, slippage=20) 3 | 4 | bb_length = input(title="BBLength", defval=20, minval=1) 5 | bb_mult = input(title="BBMult", defval=2.0, minval=0.001, maxval=50, step=0.1) 6 | 7 | bb_basis = sma(close, bb_length) 8 | bb_dev = bb_mult * stdev(close, bb_length) 9 | 10 | bb_upper = bb_basis + bb_dev 11 | bb_lower = bb_basis - bb_dev 12 | 13 | buy = crossover(close, bb_lower) 14 | sell = crossunder(close, bb_upper) 15 | 16 | strategy.entry("Long", strategy.long, when = buy) 17 | strategy.entry("Short", strategy.short, when = sell) 18 | 19 | 20 | plot(bb_basis, title="BB-middle", color=color.red) 21 | plot(bb_upper, title="BB-upper", color=color.olive) 22 | plot(bb_lower, title="BB-lower", color=color.green) 23 | -------------------------------------------------------------------------------- /pinescript/2donch_margin_breakout.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | strategy(title="2Donch margin breakout", shorttitle="2Donch margin breakout", overlay=true, slippage=20) 3 | 4 | donch_len = input(defval=14, title="Donnchian Channel Length") 5 | margin = input(defval=2, title="Margin Points") 6 | donch_len2 = input(defval=2, title="Double Donchian Length") 7 | 8 | hhv = highest(highest(donch_len)[1], donch_len2) 9 | llv = lowest(lowest(donch_len)[1], donch_len2) 10 | ba = (hhv + llv)/2 11 | 12 | buy = crossover(close, (hhv + margin)[1]) 13 | sell = crossunder(close, (llv - margin)[1]) 14 | 15 | strategy.entry("L", strategy.long, when = buy) 16 | strategy.entry("S", strategy.short, when = sell) 17 | 18 | plot(hhv, color=color.red, linewidth=1, title="Donch HHV") 19 | plot(llv, color=color.blue, linewidth=1, title="Donch LLV") 20 | -------------------------------------------------------------------------------- /pinescript/atrdonch_margin_breakout.psc: -------------------------------------------------------------------------------- 1 | //@version=3 2 | strategy(title="ATRDonch Margin Breakout", shorttitle="ATRDonch Margin Breakout", overlay=true, slippage=20) 3 | 4 | donch_len = input(title="DonchianLength", defval=14, minval=1, step=1) 5 | margin = input(title="Margin", defval=2.0, minval=0.0, step=0.1) 6 | atr_mul = input(title="ATRMult", defval=1.0, minval=0.1, step=0.1) 7 | atr_len = input(title="ATRLen", defval=14, minval=1, step=1) 8 | 9 | hhv = highest(donch_len) 10 | llv = lowest(donch_len) 11 | ba = (hhv + llv)/2 12 | up = ba + atr_mul*atr(atr_len) 13 | dn = ba - atr_mul*atr(atr_len) 14 | 15 | buy = crossover(close, up[1] + margin) 16 | sell = crossunder(close, dn[1] - margin) 17 | 18 | strategy.entry("L", strategy.long, when = buy) 19 | strategy.entry("S", strategy.short, when = sell) 20 | 21 | plot(hhv, color=red, linewidth=1) 22 | plot(llv, color=blue, linewidth=1) 23 | -------------------------------------------------------------------------------- /pinescript/atrdonch2_breakout.psc: -------------------------------------------------------------------------------- 1 | //@version=3 2 | strategy(title="ATRDonch 2 breakout", shorttitle="ATRDonch 2 breakout", overlay=true, slippage=20) 3 | 4 | donch_len = input(title="DonchianLength", defval=14, minval=1, step=1) 5 | donch_len2 = input(title="DonchianLength2", defval=8, minval=1, step=1) 6 | atr_mul = input(title="ATRMult", defval=1.0, minval=0.1, step=0.1) 7 | atr_len = input(title="ATRLen", defval=14, minval=1, step=1) 8 | 9 | hhv = highest(donch_len) 10 | llv = lowest(donch_len) 11 | ba = (hhv + llv)/2 12 | up = highest(ba + atr_mul*atr(atr_len), donch_len2) 13 | dn = lowest(ba - atr_mul*atr(atr_len), donch_len2) 14 | 15 | buy = crossover(close, up[1]) 16 | sell = crossunder(close, dn[1]) 17 | 18 | strategy.entry("L", strategy.long, when = buy) 19 | strategy.entry("S", strategy.short, when = sell) 20 | 21 | plot(up, color=red, linewidth=1) 22 | plot(dn, color=blue, linewidth=1) 23 | -------------------------------------------------------------------------------- /pinescript/ind_atr_multi_timeframe_with_bollinger_bands_v1.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | study('ATR multitimeframe with bollinger bands', overlay=false) 3 | 4 | atr_len = input(title="ATR Length", defval=20) 5 | bol_len = input(title="Bollinger Length", defval=20) 6 | bol_mul = input(title="Bollinger Multiplier", defval=2) 7 | atr_res = input(title="ATR timeframe", type=input.resolution, defval="1D") 8 | 9 | atr_t = atr(atr_len) 10 | bol_bas = sma(atr_t, bol_len) 11 | bol_up = bol_bas + bol_mul*stdev(atr_t, bol_len) 12 | bol_dn = bol_bas - bol_mul*stdev(atr_t, bol_len) 13 | 14 | atr_ind = security(syminfo.tickerid, atr_res, atr_t) 15 | bolu_ind = security(syminfo.tickerid, atr_res, bol_up) 16 | bold_ind = security(syminfo.tickerid, atr_res, bol_dn) 17 | 18 | plot(atr_ind, color=color.black, title='ATR') 19 | plot(bolu_ind, color=color.green, title="BOL-U") 20 | plot(bold_ind, color=color.red, title="BOL-D") 21 | -------------------------------------------------------------------------------- /pinescript/ind_momentum.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | study("Momentum Indicator", overlay=false) 3 | 4 | mom_lback_period = input(defval=24, type=input.integer, title='Loopback period') 5 | mom_tframe = input(defval='1D', type=input.resolution, title='Time frame') 6 | mom_elen = input(defval=24, type=input.integer, title='Smooth Length') 7 | 8 | momentum(seria, length) => 9 | mom = seria - seria[length] 10 | mom 11 | // 12 | 13 | mom_ind_s = momentum(close, mom_lback_period) 14 | mom_ind_ema_s = ema(mom_ind_s, mom_elen) 15 | mom_ind = security(syminfo.ticker, mom_tframe, mom_ind_s) 16 | mom_ind_ema = security(syminfo.ticker, mom_tframe, mom_ind_ema_s) 17 | 18 | 19 | plot(mom_ind, color=color.blue, title='MOM') 20 | plot(mom_ind_ema, color=color.olive, title='MOM MA') 21 | plot(0.0, color=color.black, linewidth=2) 22 | -------------------------------------------------------------------------------- /pinescript/ind_heikinashi.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | study("Heikin Ashi", shorttitle="Heikin Ashi", overlay=false) 3 | 4 | //////////////////////////////// 5 | /// Inputs 6 | time_frame = input(defval='1D', type=input.resolution, title='Time Frame') 7 | 8 | ///////////////////////////////// 9 | /// Signals 10 | ha_open = security(heikinashi(syminfo.tickerid), time_frame, open) 11 | ha_high = security(heikinashi(syminfo.tickerid), time_frame, high) 12 | ha_low = security(heikinashi(syminfo.tickerid), time_frame, low) 13 | ha_close = security(heikinashi(syminfo.tickerid), time_frame, close) 14 | 15 | ///////////////////////////////// 16 | /// Plots 17 | plotcandle(iff(ha_open < ha_close, ha_open, na), ha_high, ha_low, ha_close, title='Green Candles', color=#53b987, wickcolor=#53b987, bordercolor=#53b987) 18 | plotcandle(iff(ha_open >= ha_close, ha_open, na), ha_high, ha_low, ha_close, title='Red Candles', color=#eb4d5c, wickcolor=#eb4d5c, bordercolor=#eb4d5c) 19 | -------------------------------------------------------------------------------- /pinescript/ind_atr_stops_v2.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | study(title="ATR Trailing Stoploss v2", overlay = true) 3 | 4 | atr_period = input(defval=5, type=input.integer, title="ATR Period") 5 | atr_mult = input(defval=1.5, type=input.float, title="ATR Multiplier") 6 | 7 | n_loss = atr_mult * atr(atr_period) 8 | trail_loss = 0.0 9 | trail_loss := iff(close > nz(trail_loss[1], 0) and close[1] > nz(trail_loss[1], 0), max(nz(trail_loss[1]), close - n_loss), iff(close < nz(trail_loss[1], 0) and close[1] < nz(trail_loss[1], 0), min(nz(trail_loss[1]), close + n_loss), iff(close > nz(trail_loss[1], 0), close - n_loss, close + n_loss))) 10 | 11 | pos = 0.0 12 | pos := iff(close[1] < nz(trail_loss[1], 0) and close > nz(trail_loss[1], 0), 1, iff(close[1] > nz(trail_loss[1], 0) and close < nz(trail_loss[1], 0), -1, nz(pos[1], 0))) 13 | p_color = pos == -1 ? color.red : pos == 1 ? color.green : color.blue 14 | 15 | plot(trail_loss, color=p_color, title="ATR Trailing Stop v2") 16 | -------------------------------------------------------------------------------- /pinescript/adx_multi_timeframe.psc: -------------------------------------------------------------------------------- 1 | //@version=3 2 | study('ADX', overlay=false) 3 | 4 | adx_eml = input(title="ADX EMA Length", defval=9) 5 | adx_len = input(title="ADX Length", defval=20) 6 | adx_res = input(title="ADX timeframe", type=resolution, defval="1D") 7 | 8 | // ADX 9 | dirmov(len) => 10 | up = change(high) 11 | down = -change(low) 12 | truerange = rma(tr, len) 13 | plus = fixnan(100 * rma(up > down and up > 0 ? up : 0, len) / truerange) 14 | minus = fixnan(100 * rma(down > up and down > 0 ? down : 0, len) / truerange) 15 | [plus, minus] 16 | 17 | adx(adx_len) => 18 | [plus, minus] = dirmov(adx_len) 19 | sum = plus + minus 20 | adx = 100 * rma(abs(plus - minus) / (sum == 0 ? 1 : sum), adx_len) 21 | [adx, plus, minus] 22 | 23 | [ADX, up, down] = adx(adx_len) 24 | ADX_em = ema(ADX, adx_eml) 25 | ADX_new = security(tickerid, adx_res, ADX) 26 | ADX_ema = security(tickerid, adx_res, ADX_em) 27 | 28 | plot(ADX_new, color=red) 29 | plot(ADX_ema, color=blue) 30 | -------------------------------------------------------------------------------- /pinescript/ind_adx_multi_timeframe.psc: -------------------------------------------------------------------------------- 1 | //@version=3 2 | study('ADX', overlay=false) 3 | 4 | adx_eml = input(title="ADX EMA Length", defval=9) 5 | adx_len = input(title="ADX Length", defval=20) 6 | adx_res = input(title="ADX timeframe", type=resolution, defval="1D") 7 | 8 | // ADX 9 | dirmov(len) => 10 | up = change(high) 11 | down = -change(low) 12 | truerange = rma(tr, len) 13 | plus = fixnan(100 * rma(up > down and up > 0 ? up : 0, len) / truerange) 14 | minus = fixnan(100 * rma(down > up and down > 0 ? down : 0, len) / truerange) 15 | [plus, minus] 16 | 17 | adx(adx_len) => 18 | [plus, minus] = dirmov(adx_len) 19 | sum = plus + minus 20 | adx = 100 * rma(abs(plus - minus) / (sum == 0 ? 1 : sum), adx_len) 21 | [adx, plus, minus] 22 | 23 | [ADX, up, down] = adx(adx_len) 24 | ADX_em = ema(ADX, adx_eml) 25 | ADX_new = security(tickerid, adx_res, ADX) 26 | ADX_ema = security(tickerid, adx_res, ADX_em) 27 | 28 | plot(ADX_new, color=red) 29 | plot(ADX_ema, color=blue) 30 | -------------------------------------------------------------------------------- /pinescript/roc_of_ema_crossover.psc: -------------------------------------------------------------------------------- 1 | //@version=5 2 | strategy(title="ROC of ema crossover", shorttitle="ROC of ema crossover", default_qty_type=strategy.percent_of_equity, default_qty_value=100) 3 | 4 | //////////////////////////////////////// 5 | // Inputs 6 | roc_length = input.int(9, minval=1, title='ROC Length') 7 | roc_thresh = input.int(0, title='ROC Threshold') 8 | 9 | //////////////////////////////////////////// 10 | // Signals 11 | roc_signal = ta.roc(ta.sma(close, roc_length), roc_length) 12 | 13 | ///////////////////////////////////////////// 14 | // Buy/Sell Signals 15 | buy_sig = ta.crossover(roc_signal, roc_thresh) 16 | sell_sig = ta.crossunder(roc_signal, 0) 17 | 18 | //////////////////////////////////////////// 19 | // Execute positions 20 | if buy_sig 21 | strategy.entry("Long", strategy.long) 22 | // 23 | if sell_sig 24 | strategy.close("Long") 25 | // 26 | 27 | /////////////////////////////////////////////// 28 | // Plots 29 | plot(roc_signal, color=color.lime, title="ROC") 30 | -------------------------------------------------------------------------------- /pinescript/atrdonch_margin_breakout_v2.psc: -------------------------------------------------------------------------------- 1 | //@version=3 2 | strategy(title="ATRDonch Margin Breakout v2", shorttitle="ATRDonch Margin Breakout v2", overlay=true, slippage=20) 3 | 4 | donch_len = input(title="DonchianLength", defval=14, minval=1, step=1) 5 | smooth_len = input(title="SmoothLength", defval=1, minval=1, step=1) 6 | margin = input(title="Margin", defval=2.0, minval=0.0, step=1) 7 | atr_mul = input(title="ATRMult", defval=1.0, minval=0.1, step=0.1) 8 | atr_len = input(title="ATRLen", defval=14, minval=1, step=1) 9 | 10 | hhv = highest(ema(high, smooth_len), donch_len) 11 | llv = lowest(ema(low, smooth_len), donch_len) 12 | ba = (hhv + llv)/2 13 | up = ba + atr_mul*atr(atr_len) 14 | dn = ba - atr_mul*atr(atr_len) 15 | 16 | buy = crossover(close, dn[1] + margin) 17 | sell = crossunder(close, up[1] - margin) 18 | 19 | strategy.entry("L", strategy.long, when = buy) 20 | strategy.entry("S", strategy.short, when = sell) 21 | 22 | plot(hhv, color=red, linewidth=1) 23 | plot(llv, color=blue, linewidth=1) 24 | -------------------------------------------------------------------------------- /pinescript/ren1_with_atr_ema.psc: -------------------------------------------------------------------------------- 1 | //@version=3 2 | strategy(title="Ren1 with ATR EMA", shorttitle="Ren1 with ATR EMA", overlay=true) 3 | 4 | atr_len = input(10, type=integer, title="ATR Length") 5 | hl_len = input(10, type=integer, title="EMA Length") 6 | 7 | ren(src, atr_len, ema_len) => 8 | sig = src 9 | atr_t = ema(atr(atr_len), ema_len) 10 | ema_t = ema(src, ema_len) 11 | 12 | if na(sig[1]) 13 | //sig := lowest(low, 20) 14 | sig := ema_t[1] 15 | else 16 | if (ema_t > sig[1] + atr_t) 17 | sig := sig[1] + atr_t 18 | else 19 | if (ema_t < sig[1] - atr_t) 20 | sig := sig[1] - atr_t 21 | else 22 | sig := sig[1] 23 | // 24 | // 25 | // 26 | // 27 | 28 | sig2 = ren(open, atr_len, hl_len) 29 | 30 | buy = (sig2 > sig2[1]) 31 | sell = (sig2 < sig2[1]) 32 | 33 | strategy.entry("L", strategy.long, when=buy) 34 | strategy.entry("S", strategy.short, when=sell) 35 | 36 | plot(sig2, color=red, linewidth=2) 37 | -------------------------------------------------------------------------------- /pinescript/ind_bollinger_bands_multi_timeframe.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | study("Bollinger Bands MTR", overlay=true) 3 | 4 | bb_source = input(defval=close, type=input.source, title='Source') 5 | bb_length = input(defval=20, type=input.integer, title='Length', minval=1) 6 | bb_mult = input(defval=2.0, type=input.float, title='Multiplier', minval=0.01, maxval=50, step=0.01) 7 | bb_tf = input(defval='1D', type=input.resolution, title='Timeframe') 8 | 9 | bb_basis = sma(bb_source, bb_length) 10 | bb_dev = bb_mult * stdev(bb_source, bb_length) 11 | 12 | bb_upper = bb_basis + bb_dev 13 | bb_lower = bb_basis - bb_dev 14 | 15 | bb_basis_t = security(syminfo.tickerid, bb_tf, bb_basis) 16 | bb_upper_t = security(syminfo.tickerid, bb_tf, bb_upper) 17 | bb_lower_t = security(syminfo.tickerid, bb_tf, bb_lower) 18 | 19 | plot(bb_upper_t, color=color.green, linewidth=1, title='BB_U') 20 | plot(bb_lower_t, color=color.red, linewidth=1, title='BB_L') 21 | plot(bb_basis_t, color=color.black, linewidth=1, title='BB_B') 22 | -------------------------------------------------------------------------------- /pinescript/linear_regression_slope_strategy.psc: -------------------------------------------------------------------------------- 1 | //@version=3 2 | strategy(title="LRS strategy", shorttitle="LRS strategy", overlay=true) 3 | 4 | lr_len = input(defval=150, minval=1, title="Linear Regression Length") 5 | ema_len = input(defval=9, minval=1, title="EMA Length") 6 | lb_len = input(defval=1, minval=1, title="Look Back Period Length") 7 | thresh = input(defval=0.000001, minval=0, title="LR Slope threshold", step=0.00001) 8 | src = input(close, type=source) 9 | 10 | src_n = ema(src, ema_len) 11 | 12 | lrc = linreg(src_n[0], lr_len, 0) 13 | lrprev = linreg(src_n[lb_len], lr_len, 0) 14 | slope = ((lrc - lrprev) / interval) 15 | 16 | buy = (slope > thresh) 17 | sell = (slope < -thresh) 18 | 19 | strategy.entry("Long", strategy.long, when = buy) 20 | strategy.entry("Short", strategy.short, when = sell) 21 | 22 | //Please if somebody have suggestions how show this better, let me know 23 | plot(lrc, color = red, title = "Linear Regression Curve", style = line, linewidth = 2) 24 | plotarrow(slope, colorup=teal, colordown=orange, transp=40) 25 | -------------------------------------------------------------------------------- /pinescript/ind_fractal_adaptive_moving_average.psc: -------------------------------------------------------------------------------- 1 | //@version=2 2 | study("Fractal Adaptive Moving Average",shorttitle="FRAMA",overlay=true) 3 | 4 | price = input(hl2) 5 | len = input(defval=16,minval=1) 6 | FC = input(defval=1,minval=1) 7 | SC = input(defval=198,minval=1) 8 | len1 = len/2 9 | w = log(2/(SC+1)) 10 | H1 = highest(high,len1) 11 | L1 = lowest(low,len1) 12 | N1 = (H1-L1)/len1 13 | H2 = highest(high,len)[len1] 14 | L2 = lowest(low,len)[len1] 15 | N2 = (H2-L2)/len1 16 | H3 = highest(high,len) 17 | L3 = lowest(low,len) 18 | N3 = (H3-L3)/len 19 | 20 | dimen1 = (log(N1+N2)-log(N3))/log(2) 21 | dimen = iff(N1>0 and N2>0 and N3>0,dimen1,nz(dimen1[1])) 22 | alpha1 = exp(w*(dimen-1)) 23 | oldalpha = alpha1>1?1:(alpha1<0.01?0.01:alpha1) 24 | oldN = (2-oldalpha)/oldalpha 25 | N = (((SC-FC)*(oldN-1))/(SC-1))+FC 26 | alpha_ = 2/(N+1) 27 | alpha = alpha_<2/(SC+1)?2/(SC+1):(alpha_>1?1:alpha_) 28 | out = (1-alpha)*nz(out[1]) + alpha*price 29 | 30 | plot(out, title="FRAMA", color=blue, transp=0) 31 | -------------------------------------------------------------------------------- /pinescript/macd_crossover_v1.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | strategy("MACD crossover", overlay=false, slippage=20) 3 | 4 | macd_fast_len = input(title="MACD Fast Length", type=input.integer, defval=9) 5 | macd_slow_len = input(title="MACD Slow Length", type=input.integer, defval=26) 6 | macd_sig_len = input(title="MACD Sig Length", type=input.integer, defval=9) 7 | 8 | macd_fast_ma = sma(close, macd_fast_len) 9 | macd_slow_ma = sma(close, macd_slow_len) 10 | macd_macd = macd_fast_ma - macd_slow_ma 11 | macd_signal = sma(macd_macd, 9) 12 | 13 | buy = crossover(macd_macd, macd_signal) and macd_macd > 0 14 | sell = crossunder(macd_macd, macd_signal) or crossunder(macd_macd, 0) 15 | short = crossunder(macd_macd, macd_signal) and macd_macd < 0 16 | cover = crossover(macd_macd, macd_signal) or crossover(macd_macd, 0) 17 | 18 | strategy.entry("L", strategy.long, when=buy) 19 | strategy.close("L", when=sell) 20 | strategy.entry("S", strategy.short, when=sell) 21 | strategy.close("S", when=cover) 22 | 23 | plot(macd_macd, color=color.red, title="MACD") 24 | plot(macd_signal, color=color.blue, title="MACD Sig") 25 | -------------------------------------------------------------------------------- /pinescript/kelner_ch_breakout.psc: -------------------------------------------------------------------------------- 1 | //@version=3 2 | strategy("Keltner Breakout", overlay=true, slippage=20) 3 | 4 | ema_len = input(200, type=integer, title='EMA Length') 5 | k_len = input(20, type=integer, title='K Length') 6 | mult = input(0.5, type=float, title='Range multiplier', step=0.01) 7 | ema_len2 = input(9, type=integer, title='Price Smooth Length') 8 | 9 | src = ema(close, ema_len) 10 | src2 = ema(close, ema_len2) 11 | range = atr(14) //ema(high, ema_len) - ema(low, ema_len) 12 | 13 | //ema_t = ema(src, k_len) 14 | //range_t = ema(range, k_len) 15 | upper = src + range * mult 16 | lower = src - range * mult 17 | 18 | buy = crossover(src2, upper) 19 | selli = crossunder(src2, upper) 20 | sell = crossunder(src2, lower) 21 | buyi = crossover(src2, lower) 22 | 23 | strategy.entry("L", strategy.long, when = buy) 24 | strategy.close("L", when = selli) 25 | strategy.entry("S", strategy.short, when = sell) 26 | strategy.close("S", when = buyi) 27 | 28 | plot(src, color=blue) 29 | plot(src2, color=olive) 30 | plot(upper, color=green) 31 | plot(lower, color=red) 32 | -------------------------------------------------------------------------------- /pinescript/ind_volatility_bb_bands_multi_timeframe.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | study("BB Vol MTR", overlay=false) 3 | 4 | bb_source = input(defval=close, type=input.source, title='Source') 5 | bb_length = input(defval=20, type=input.integer, title='Length', minval=1) 6 | bb_mult = input(defval=2.0, type=input.float, title='Multiplier', minval=0.01, maxval=50, step=0.01) 7 | bb_tf = input(defval='1D', type=input.resolution, title='Timeframe') 8 | bb_vol_el = input(defval=9, type=input.integer, title='EMA Length') 9 | 10 | bb_basis = sma(bb_source, bb_length) 11 | bb_dev = bb_mult * stdev(bb_source, bb_length) 12 | 13 | bb_upper = bb_basis + bb_dev 14 | bb_lower = bb_basis - bb_dev 15 | 16 | bb_basis_t = security(syminfo.tickerid, bb_tf, bb_basis) 17 | bb_upper_t = security(syminfo.tickerid, bb_tf, bb_upper) 18 | bb_lower_t = security(syminfo.tickerid, bb_tf, bb_lower) 19 | vol_t = bb_upper_t - bb_lower_t 20 | vol_tt = ema(vol_t, bb_vol_el) 21 | 22 | plot(vol_t, color=color.green, linewidth=1, title='VOL_T') 23 | plot(vol_tt, color=color.black, linewidth=1, title='VOL_T_EMA') 24 | -------------------------------------------------------------------------------- /pinescript/ma_channel_v1.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | strategy("MA Channel v1", overlay=true) 3 | 4 | ma_len = input(10, title='MA Length', type=input.integer) 5 | ma_tf = input('', title='MA Time Frame', type=input.resolution) 6 | ma_type = input('sma', title='MA Type', options=['sma', 'ema']) 7 | 8 | get_ma() => 9 | ma_high_t = sma(high, ma_len) 10 | ma_low_t = sma(low, ma_len) 11 | if ma_type == 'ema' 12 | ma_high_t := ema(high, ma_len) 13 | ma_low_t := ema(low, ma_len) 14 | // 15 | [ma_high_t, ma_low_t] 16 | // 17 | 18 | [ma_high_t, ma_low_t] = get_ma() 19 | ma_high = security(syminfo.tickerid, ma_tf, ma_high_t) 20 | ma_low = security(syminfo.tickerid, ma_tf, ma_low_t) 21 | 22 | buy = low > ma_high and low[1] > ma_high[1] and low[2] > ma_high[2] and low[3] > ma_high[3] //and low[4] > ma_high[4] 23 | short = high < ma_low and high[1] < ma_low[1] and high[2] < ma_low[2] and high[3] < ma_low[3] //and high[4] < ma_low[4] 24 | 25 | strategy.entry("L", strategy.long, when=buy) 26 | strategy.entry("S", strategy.short, when=short) 27 | 28 | plot(ma_high, title='MA High') 29 | plot(ma_low, title='MA Low') 30 | -------------------------------------------------------------------------------- /pinescript/ind_session_ema.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | study(title="EMA-Sess", shorttitle="ESess", overlay=true) 3 | 4 | /////////////////////////////////////////// 5 | /// Inputs 6 | st_src = input(defval=close, type=input.source, title='Source') 7 | ema_period = input(defval=9, type=input.integer, title='EMA period') 8 | time_frame = input(defval="D", type=input.resolution, title="Time frame") 9 | 10 | /////////////////////////////////////////// 11 | /// Session specific EMA (with same algo as VWAP, but without volume) 12 | new_session = iff(change(time(time_frame)), 1, 0) 13 | 14 | _vap_sum_fn(src) => 15 | vwap_sum = 0.0 16 | vwap_sum := (new_session == 1) ? src : (vwap_sum[1]+src) 17 | vwap_sum 18 | // 19 | _v_sum_fn() => 20 | vol_sum = 0.0 21 | vol_sum := (new_session == 1) ? 1 : (vol_sum[1]+1) 22 | vol_sum 23 | // 24 | 25 | /////////////////////////////////////////// 26 | /// Signals 27 | src_t = ema(st_src, ema_period) 28 | vap_sig = _vap_sum_fn(src_t)/_v_sum_fn() 29 | 30 | /////////////////////////////////////////// 31 | /// Plots 32 | plot(vap_sig, title='EMA-S', color=color.red) 33 | 34 | -------------------------------------------------------------------------------- /pinescript/ema_crossover_with_rsi_filter.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | strategy("RSI + EMA Crossover", overlay=true, slippage=20) 3 | 4 | rsi_len = input(title="RSI Length", type=input.integer, defval=9) 5 | es_len = input(title="EMA short Length", type=input.integer, defval=5) 6 | el_len = input(title="EMA long Length", type=input.integer, defval=9) 7 | 8 | rsi_sig = rsi(close, rsi_len) 9 | ema_short = ema(close, es_len) 10 | ema_long = ema(close, el_len) 11 | 12 | buy = ((rsi_sig > rsi_sig[1]) and (rsi_sig > 50) and crossover(ema_short, ema_long)) or ((ema_short > ema_long) and crossover(rsi_sig, 50)) 13 | sell = crossunder(ema_short, ema_long) //or crossunder(rsi_sig, 50) 14 | short = ((rsi_sig < rsi_sig[1]) and (rsi_sig < 50) and crossunder(ema_short, ema_long)) or ((ema_short < ema_long) and crossunder(rsi_sig, 50)) 15 | cover = crossover(rsi_sig, 50) //or crossover(ema_short, ema_long) 16 | 17 | strategy.entry("L", strategy.long, when=buy) 18 | strategy.close("L", when=sell) 19 | strategy.entry("S", strategy.short, when=short) 20 | strategy.close("S", when=cover) 21 | 22 | plot(ema_short, color=color.red, title="EMAs") 23 | plot(ema_long, color=color.blue, title="EMAl") 24 | -------------------------------------------------------------------------------- /pinescript/supertrend.psc: -------------------------------------------------------------------------------- 1 | strategy("Supertrend Strategy", "Supertrend Strategy", overlay=true, slippage=20) 2 | 3 | Factor = input(4, minval=1, maxval=100) 4 | Pd = input(10, minval=1, maxval=100) 5 | res = input(type=resolution, defval="30") 6 | 7 | SuperTrend(factor, pd, src) => 8 | Up = hl2 -(factor*atr(pd)) 9 | Dn = hl2 +(factor*atr(Pd)) 10 | 11 | TrendUp = (src[1] > TrendUp[1]) ? max(Up, TrendUp[1]) : Up 12 | TrendDown = (src[1] < TrendDown[1]) ? min(Dn, TrendDown[1]) : Dn 13 | 14 | Trend = (src > TrendDown[1]) ? 1: (src < TrendUp[1]) ? -1: nz(Trend[1],1) 15 | Tsl = Trend==1? TrendUp: TrendDown 16 | 17 | [Trend, Tsl] 18 | // 19 | 20 | [Trend, Tsl] = SuperTrend(Factor, Pd, close) 21 | 22 | Trend_res = security(tickerid,res,Trend) 23 | Tsl_res = security(tickerid,res,Tsl) 24 | lcolor_res = Trend_res == 1 ? green : red 25 | 26 | buy = (Trend_res == 1) and (Trend_res[1] != 1) 27 | sell = (Trend_res != 1) and (Trend_res[1] == 1) 28 | 29 | strategy.entry("Long", strategy.long, when = buy) 30 | strategy.entry("Short", strategy.short, when = sell) 31 | 32 | plot(Tsl_res, color = lcolor_res , style = linebr , linewidth = 1,title = "SuperTrend") 33 | -------------------------------------------------------------------------------- /pinescript/ind_trend_filter_using_cumsum_of_ma_crossovers.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | study("Trend filter using cumsum of MA crossovers", overlay=false) 3 | 4 | trnd_ema_length_short = input(defval=2, title='TR Short EMA Length', type=input.integer) 5 | trnd_ema_length_long = input(defval=5, title='TR Long EMA Length', type=input.integer) 6 | trnd_last_periods = input(defval=5, title='TR Loopback Period Length', type=input.integer) 7 | trnd_time_frame = input(defval='1D', title='TR Time Frame', type=input.resolution) 8 | 9 | // Indicator calculation 10 | trnd_ema_crossover = crossover(ema(close, trnd_ema_length_short), ema(close, trnd_ema_length_long)) != 0 ? 1.0:0.0 11 | trnd_ind_t_ = sma(trnd_ema_crossover, trnd_last_periods) * trnd_last_periods 12 | trnd_ind_stdev_t_ = stdev(trnd_ind_t_, trnd_last_periods) 13 | trnd_ind_t = security(syminfo.tickerid, trnd_time_frame, trnd_ind_t_) 14 | trnd_ind_stdev_t = security(syminfo.tickerid, trnd_time_frame, trnd_ind_stdev_t_) 15 | 16 | // Plots 17 | plot(trnd_ind_t, color=color.green, title='TRND') 18 | plot(trnd_ind_stdev_t, color=color.red, title='TRND_STDEV') 19 | -------------------------------------------------------------------------------- /pinescript/ind_atr_multi_timeframe_with_donch.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | study('ADX multitimeframe - Donch', overlay=false) 3 | 4 | adx_dml = input(title="ADX Donch Length", defval=9) 5 | adx_len = input(title="ADX Length", defval=20) 6 | adx_res = input(title="ADX timeframe", type=input.resolution, defval="1D") 7 | 8 | // ADX 9 | dirmov(len) => 10 | up = change(high) 11 | down = -change(low) 12 | truerange = rma(tr, len) 13 | plus = fixnan(100 * rma(up > down and up > 0 ? up : 0, len) / truerange) 14 | minus = fixnan(100 * rma(down > up and down > 0 ? down : 0, len) / truerange) 15 | [plus, minus] 16 | 17 | adx(adx_len) => 18 | [plus, minus] = dirmov(adx_len) 19 | sum = plus + minus 20 | adx = 100 * rma(abs(plus - minus) / (sum == 0 ? 1 : sum), adx_len) 21 | [adx, plus, minus] 22 | 23 | [ADX, up, down] = adx(adx_len) 24 | ADX_dhi = highest(ADX, adx_dml) 25 | ADX_dlo = lowest(ADX, adx_dml) 26 | ADX_new = security(syminfo.tickerid, adx_res, ADX) 27 | ADX_hi = security(syminfo.tickerid, adx_res, ADX_dhi) 28 | ADX_lo = security(syminfo.tickerid, adx_res, ADX_dlo) 29 | 30 | plot(ADX_new, color=color.red) 31 | plot(ADX_hi, color=color.green) 32 | plot(ADX_lo, color=color.black) 33 | -------------------------------------------------------------------------------- /pinescript/donch_breakout_with_rsi_filter.psc: -------------------------------------------------------------------------------- 1 | //@version=3 2 | strategy('donch channel breakout with RSI filter', overlay=true, slippage=20) 3 | 4 | ch_wi = input(50, type=integer, title='channel width') 5 | rsi_len = input(14, type=integer, title='rsi len') 6 | rsi_ema = input(3, type=integer, title='rsi smooth period') 7 | rsi_sen = input(false, type=bool, title='rsi smooth enable') 8 | rsi_hi = input(70, type=float, title='rsi high', minval=10, maxval=90) 9 | rsi_lo = input(30, type=float, title='rsi low', minval=10, maxval=90) 10 | 11 | hhv = highest(ch_wi) 12 | llv = lowest(ch_wi) 13 | bas = avg(hhv, llv) 14 | 15 | rsi_t = if rsi_sen 16 | ema(rsi(close, rsi_len), rsi_ema) 17 | else 18 | rsi(close, rsi_len) 19 | // 20 | 21 | //tr_stop_long = hhv * (1 - sl_inp) 22 | //tr_stop_short = llv * (1 + sl_inp) 23 | 24 | buy = close > hhv[1] and rsi_t > rsi_hi 25 | sell = close < llv[1] and rsi_t < rsi_lo 26 | selli = close < llv[1] 27 | buyi = close > hhv[1] 28 | 29 | strategy.entry("long", strategy.long, when = buy) 30 | strategy.close("long", when = selli) 31 | strategy.entry("short", strategy.short, when = sell) 32 | strategy.close("short", when = buyi) 33 | 34 | plot(hhv) 35 | plot(llv) 36 | -------------------------------------------------------------------------------- /pinescript/ind_roc_trend_strength.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | study(title="ROC trend strength", overlay=false) 3 | 4 | //////////////////////////////////////////////////////////////////////////////// 5 | // Inputs 6 | roc_source = input(defval=close, title='ROC Source', type=input.source) 7 | roc_length = input(defval=2, title='ROC Loopback Length', type=input.integer, minval=1) 8 | roc_pct_change = input(defval=10, title='ROC Percentage change threshold', type=input.integer, minval=1) 9 | roc_tf = input(defval='1D', title='ROC Time Frame', type=input.resolution) 10 | 11 | ///////////////////////////////////////////////////////////////////////////////// 12 | // Calculate indicators 13 | roc_org_t = 100 * (roc_source - roc_source[roc_length])/roc_source[roc_length] 14 | roc_ema_t = ema(roc_org_t, roc_length/2) 15 | ind_eroc_t = security(syminfo.tickerid, roc_tf, roc_ema_t) 16 | 17 | ////////////////////////////////////////////////////////////////////////////////// 18 | // Plots 19 | plot(ind_eroc_t, color=color.blue, title='EROC') 20 | plot(roc_pct_change/2, color=color.black, title='UThr') 21 | plot(-roc_pct_change/2, color=color.black, title='LThr') 22 | -------------------------------------------------------------------------------- /pinescript/ema_crossover_with_rsi_multi_timeframe_filter.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | strategy("RSI + EMA Crossover", overlay=true, slippage=20) 3 | 4 | rsi_len = input(title="RSI Length", type=input.integer, defval=9) 5 | rsi_trs = input(title="RSI timeframe", type=input.resolution, defval='1D') 6 | es_len = input(title="EMA short Length", type=input.integer, defval=5) 7 | el_len = input(title="EMA long Length", type=input.integer, defval=9) 8 | 9 | rsi_sig = security(syminfo.tickerid, rsi_trs, rsi(close, rsi_len)) 10 | ema_short = ema(close, es_len) 11 | ema_long = ema(close, el_len) 12 | 13 | buy = ((rsi_sig > rsi_sig[1]) and (rsi_sig > 50) and crossover(ema_short, ema_long)) or ((ema_short > ema_long) and crossover(rsi_sig, 50)) 14 | sell = crossunder(rsi_sig, 50) 15 | short = ((rsi_sig < rsi_sig[1]) and (rsi_sig < 50) and crossunder(ema_short, ema_long)) or ((ema_short < ema_long) and crossunder(rsi_sig, 50)) 16 | cover = crossover(rsi_sig, 50) 17 | 18 | strategy.entry("L", strategy.long, when=buy) 19 | strategy.close("L", when=sell) 20 | strategy.entry("S", strategy.short, when=short) 21 | strategy.close("S", when=cover) 22 | 23 | plot(ema_short, color=color.red, title="EMAs") 24 | plot(ema_long, color=color.blue, title="EMAl") 25 | -------------------------------------------------------------------------------- /pinescript/ind_ehlers_time_warp_laguerre_filter_v1.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | study("Ehlers Laguerre Filter v1", overlay=true) 3 | 4 | /////////////////////////////////////////////////// 5 | // Inputs 6 | filt_gamma = input(defval=0.8, title="Gamma", type=input.float, step=0.1) 7 | time_frame = input(defval='1D', title="Time Frame", type=input.resolution) 8 | src = input(defval=close, title="Sourc", type=input.source) 9 | 10 | /////////////////////////////////////////////////// 11 | // Calculate Signals 12 | Average_v1(L0, L1, L2, L3) => 13 | (L0 + 2*L1 + 2*L2 + L3)/6 14 | 15 | Average_v2(L0, L1, L2, L3) => 16 | (L0 + L1 + L2 + L3)/4 17 | 18 | Laguerre_v1(gamma, src) => 19 | L0 = src 20 | L1 = src 21 | L2 = src 22 | L3 = src 23 | L0 := (1-gamma)*src + gamma*nz(L0[1]) 24 | L1 := -gamma*L0 + L0[1] + gamma*nz(L1[1]) 25 | L2 := -gamma*L1 + L1[1] + gamma*nz(L2[1]) 26 | L3 := -gamma*L2 + L2[1] + gamma*nz(L3[1]) 27 | Average_v1(L0, L1, L2, L3) 28 | // 29 | 30 | filt_ind_t = Laguerre_v1(filt_gamma, src) 31 | filt_ind = security(syminfo.tickerid, time_frame, filt_ind_t) 32 | 33 | //////////////////////////////////////////////////////////// 34 | // Plots 35 | plot(filt_ind, title='Laguerre Filter') 36 | -------------------------------------------------------------------------------- /pinescript/ind_donch_volatility_v1.psc: -------------------------------------------------------------------------------- 1 | //@version=5 2 | indicator("Donch range v1", overlay=false) 3 | 4 | ////////////////////////////////////////////////////////// 5 | // Parameters 6 | donch_period = input.int(defval=20, title='Donchian Period') 7 | donch_tf = input.timeframe(defval='', title='Donchian Time Frame') 8 | donch_rng_ema_period = input.int(defval=5, title='Donchian Range Smooth EMA Period') 9 | 10 | ////////////////////////////////////////////////////////////////// 11 | // Misc functions 12 | // Get data from high time frame 13 | get_security(_src, _tf) => 14 | request.security(syminfo.tickerid, _tf, _src[1], gaps=barmerge.gaps_on, lookahead=barmerge.lookahead_on) 15 | // 16 | 17 | //////////////////////////////////////////////////////////////// 18 | // Indicators 19 | donch_hi = get_security(ta.highest(high, donch_period), donch_tf) 20 | donch_lo = get_security(ta.lowest(low, donch_period), donch_tf) 21 | donch_rng = donch_hi - donch_lo 22 | donch_rng_ema = ta.ema(donch_rng, donch_rng_ema_period) 23 | 24 | ///////////////////////////////////////////////////////////////// 25 | // Plots 26 | plot(donch_rng, color=color.green, title='Donchian Range Average') 27 | plot(donch_rng_ema, color=color.blue, title='Donchian Range EMA') 28 | -------------------------------------------------------------------------------- /pinescript/ind_adx_multi_timeframe_with_lag_signal.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | study('ADX MT - Lag v1', overlay=false) 3 | 4 | //////////////////////////////////////////////////////////// 5 | // Inputs 6 | adx_lag = input(title="ADX Lag", defval=1) 7 | adx_len = input(title="ADX Length", defval=20) 8 | adx_res = input(title="ADX timeframe", type=input.resolution, defval="1D") 9 | 10 | //////////////////////////////////////////////////////////// 11 | // Indicators 12 | // ADX 13 | dirmov(len) => 14 | up = change(high) 15 | down = -change(low) 16 | truerange = rma(tr, len) 17 | plus = fixnan(100 * rma(up > down and up > 0 ? up : 0, len) / truerange) 18 | minus = fixnan(100 * rma(down > up and down > 0 ? down : 0, len) / truerange) 19 | [plus, minus] 20 | 21 | adx(adx_len) => 22 | [plus, minus] = dirmov(adx_len) 23 | sum = plus + minus 24 | adx = 100 * rma(abs(plus - minus) / (sum == 0 ? 1 : sum), adx_len) 25 | [adx, plus, minus] 26 | 27 | [ADX, up, down] = adx(adx_len) 28 | ADX_new = security(syminfo.tickerid, adx_res, ADX) 29 | ADX_new_lag = security(syminfo.tickerid, adx_res, ADX[adx_lag]) 30 | 31 | /////////////////////////////////////////////////////////////// 32 | // Plots 33 | plot(ADX_new, color=color.red) 34 | plot(ADX_new_lag, color=color.green) 35 | -------------------------------------------------------------------------------- /pinescript/ind_pivot_levels_v1.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | study("Pivot Levels v1", overlay=true) 3 | 4 | //////////////////////////////////////////////////////////////////// 5 | // Inputs 6 | pivot_bars = input(defval=2, title='Pivot Bars Length', type=input.integer) 7 | pivot_tframe = input(defval='1D', title='Pivot Time Frame', type=input.resolution) 8 | use_pivot_tframe = input(defval=false, title='Use Diff Pivot Time Frame', type=input.bool) 9 | 10 | /////////////////////////////////////////////////////////////////// 11 | // Get signals 12 | phigh_t = pivothigh(pivot_bars, pivot_bars) 13 | plow_t = pivotlow(pivot_bars, pivot_bars) 14 | phigh = phigh_t 15 | plow = plow_t 16 | phigh_tf = security(syminfo.tickerid, pivot_tframe, phigh_t) 17 | plow_tf = security(syminfo.tickerid, pivot_tframe, plow_t) 18 | if use_pivot_tframe 19 | phigh := phigh_tf 20 | plow := plow_tf 21 | // 22 | phigh_san = phigh 23 | plow_san = plow 24 | phigh_san := nz(phigh[1]) ? phigh[1] : phigh_san[1] 25 | plow_san := nz(plow[1]) ? plow[1] : plow_san[1] 26 | 27 | ///////////////////////////////////////////////////////////////////// 28 | // Plots 29 | plot(phigh_san, color=color.green, title='P+') 30 | plot(plow_san, color=color.red, title='P-') 31 | -------------------------------------------------------------------------------- /pinescript/r1_s1_dmark_breakout.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | strategy("R1-S1 DeMark breakout", overlay=true) 3 | 4 | time_frame = input("D", type=input.resolution) 5 | tolerance = input(5, type=input.integer) 6 | 7 | pivot_dmark(time_frame) => 8 | prev_close = security(syminfo.tickerid, time_frame, close[1], lookahead=true) 9 | prev_open = security(syminfo.tickerid, time_frame, open[1], lookahead=true) 10 | prev_high = security(syminfo.tickerid, time_frame, high[1], lookahead=true) 11 | prev_low = security(syminfo.tickerid, time_frame, low[1], lookahead=true) 12 | 13 | X = (prev_close > prev_open) ? (2*prev_high + prev_low + prev_close) : ((prev_close < prev_close) ? (prev_high + 2*prev_low + prev_close) : (prev_high + prev_low + 2*prev_close)) 14 | R1 = (X/2) - prev_low 15 | S1 = (X/2) - prev_high 16 | 17 | [R1, S1] 18 | // 19 | 20 | [r_level, s_level] = pivot_dmark(time_frame) 21 | 22 | buy = close > r_level + tolerance 23 | sell = close < s_level - tolerance 24 | 25 | strategy.entry("L", strategy.long, when=buy) 26 | strategy.entry("S", strategy.short, when=sell) 27 | 28 | plot(r_level, style=plot.style_circles, linewidth=2, color=color.green, title="R1") 29 | plot(s_level, style=plot.style_circles, linewidth=2, color=color.red, title="S1") 30 | -------------------------------------------------------------------------------- /pinescript/ind_moving_average_channel_MAC.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | study("Moving Average Channel Indicator", overlay=true) 3 | 4 | ma_len = input(defval=9, type=input.integer, title='Moving Average Period') 5 | time_frame = input(defval='5', type=input.resolution, title='Time Frame') 6 | ma_type = input(defval='ema', type=input.string, title='MA type', options=['ema', 'sma']) 7 | 8 | ////////////////////////////// 9 | /// Calculate high and low levels 10 | hi_line_ema = security(syminfo.ticker, time_frame, ema(high, ma_len)) 11 | lo_line_ema = security(syminfo.ticker, time_frame, ema(low, ma_len)) 12 | hi_line_sma = security(syminfo.ticker, time_frame, sma(high, ma_len)) 13 | lo_line_sma = security(syminfo.ticker, time_frame, sma(low, ma_len)) 14 | 15 | hi_line = 0.0 16 | lo_line = 0.0 17 | if ma_type == 'ema' 18 | hi_line := hi_line_ema 19 | lo_line := lo_line_ema 20 | // 21 | if ma_type == 'sma' 22 | hi_line := hi_line_sma 23 | lo_line := lo_line_sma 24 | // 25 | mid_line = avg(hi_line, lo_line) 26 | 27 | //////////////////////////////// 28 | /// Plot 29 | plot(hi_line, color=color.green, title='Ch HI') 30 | plot(lo_line, color=color.red, title='Ch LO') 31 | plot(mid_line, color=color.olive, title='Ch MID') 32 | -------------------------------------------------------------------------------- /pinescript/ind_rsi_ehlers_instantaneous_trend.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | study(title="RSI - Ehlers Instantaneous Trend", overlay=false) 3 | 4 | //////////////////////////////////////////////////// 5 | // Inputs 6 | ind_src = input(hl2, title="Source") 7 | ind_alpha = input(0.07, title="Alpha", step=0.01) 8 | ind_rsi_len = input(3, title="RSI Length") 9 | ind_timeframe = input('1D', title="Time Frame", type=input.resolution) 10 | 11 | ///////////////////////////////////////////////////// 12 | // Indicators 13 | ehlers_itrend() => 14 | ind_it = close 15 | ind_lag = close 16 | ind_it := (ind_alpha-((ind_alpha*ind_alpha)/4.0))*ind_src + 0.5*ind_alpha*ind_alpha*ind_src[1] - (ind_alpha-0.75*ind_alpha*ind_alpha)*ind_src[2] + 2*(1-ind_alpha)*nz(ind_it[1], ((ind_src+2*ind_src[1]+ind_src[2])/4.0)) - (1-ind_alpha)*(1-ind_alpha)*nz(ind_it[2], ((ind_src+2*ind_src[1]+ind_src[2])/4.0)) 17 | ind_lag := 2.0*ind_it-nz(ind_it[2]) 18 | [ind_it, ind_lag] 19 | // 20 | 21 | [ind_it_t, ind_lag_t] = ehlers_itrend() 22 | rsi_t = rsi(ind_it_t, ind_rsi_len) 23 | ind_rsi = security(syminfo.tickerid, ind_timeframe, rsi_t) 24 | 25 | ///////////////////////////////////////////////////// 26 | // Plots 27 | plot(ind_rsi, color=color.red, linewidth=1, title="RSI - EITrend") 28 | -------------------------------------------------------------------------------- /pinescript/ind_moving_averages.psc: -------------------------------------------------------------------------------- 1 | // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ 2 | // © presentisgood 3 | 4 | //@version=4 5 | study("Moving Averages", overlay=true) 6 | 7 | ma_p1 = input(defval=9, title='MA Length 1') 8 | ma_p2 = input(defval=14, title='MA Length 2') 9 | ma_p3 = input(defval=50, title='MA Length 3') 10 | ma_p4 = input(defval=100, title='MA Length 4') 11 | ma_p5 = input(defval=200, title='MA Length 5') 12 | ma_type = input(defval='EMA', title='MA type', options=['EMA', 'SMA']) 13 | 14 | ma_1 = 0.0 15 | ma_2 = 0.0 16 | ma_3 = 0.0 17 | ma_4 = 0.0 18 | ma_5 = 0.0 19 | 20 | if ma_type == 'EMA' 21 | ma_1 := ema(close, ma_p1) 22 | ma_2 := ema(close, ma_p2) 23 | ma_3 := ema(close, ma_p3) 24 | ma_4 := ema(close, ma_p4) 25 | ma_5 := ema(close, ma_p5) 26 | // 27 | if ma_type == 'SMA' 28 | ma_1 := sma(close, ma_p1) 29 | ma_2 := sma(close, ma_p2) 30 | ma_3 := sma(close, ma_p3) 31 | ma_4 := sma(close, ma_p4) 32 | ma_5 := sma(close, ma_p5) 33 | // 34 | 35 | plot(ma_1, title='MA1', color=color.green) 36 | plot(ma_2, title='MA2', color=color.blue) 37 | plot(ma_3, title='MA3', color=color.red) 38 | plot(ma_4, title='MA4', color=color.olive) 39 | plot(ma_5, title='MA5', color=color.orange) 40 | -------------------------------------------------------------------------------- /pinescript/dema_crossover_with_rsi_multi_timeframe_filter.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | strategy("DEMA crossover with multiframe RSI filter", overlay=true, slippage=20) 3 | 4 | dema_short_len = input(title="DEMA fast len", defval=3) 5 | dema_long_len = input(title="DEMA slow len", defval=10) 6 | rsi_len = input(title="RSI len", defval=14) 7 | rsi_low_lim = input(title="RSI oversold limit", defval=30) 8 | rsi_high_lim = input(title="RSI overbought limit", defval=50) 9 | rsi_res = input(title="RSI timeframe", type=input.resolution, defval="1D") 10 | 11 | dema(src, dlen) => 12 | retv = (2 * ema(src, dlen) - (ema(ema(src, dlen), dlen))) 13 | 14 | rsi_ind = rsi(close, rsi_len) 15 | rsi_ind_m = security(syminfo.tickerid, rsi_res, rsi_ind) 16 | 17 | dema_short = dema(close, dema_short_len) 18 | dema_long = dema(close, dema_long_len) 19 | 20 | buy = crossover(dema_short, dema_long) and (rsi_ind_m < rsi_low_lim) 21 | sell = crossunder(dema_short, dema_long) 22 | short = crossunder(dema_short, dema_long) and (rsi_ind_m > rsi_high_lim) 23 | cover = crossover(dema_short, dema_long) 24 | 25 | strategy.entry("L", strategy.long, when=buy) 26 | strategy.close("L", when=sell) 27 | strategy.entry("S", strategy.short, when=short) 28 | strategy.close("S", when=cover) 29 | 30 | plot(dema_short, color=color.green, title="DEMA fast") 31 | plot(dema_long, color=color.red, title="DEMA slow") 32 | -------------------------------------------------------------------------------- /pinescript/ind_ehlers_instantaneous_trend.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | study(title="Ehlers Instantaneous Trend", overlay=true) 3 | 4 | //////////////////////////////////////////////////// 5 | // Inputs 6 | ind_src = input(hl2, title="Source") 7 | ind_alpha = input(0.07, title="Alpha", step=0.01) 8 | ind_timeframe = input('', title="Time Frame", type=input.resolution) 9 | 10 | ///////////////////////////////////////////////////// 11 | // Indicators 12 | ehlers_itrend() => 13 | ind_it = close 14 | ind_lag = close 15 | ind_it := (ind_alpha-((ind_alpha*ind_alpha)/4.0))*ind_src + 0.5*ind_alpha*ind_alpha*ind_src[1] - (ind_alpha-0.75*ind_alpha*ind_alpha)*ind_src[2] + 2*(1-ind_alpha)*nz(ind_it[1], ((ind_src+2*ind_src[1]+ind_src[2])/4.0)) - (1-ind_alpha)*(1-ind_alpha)*nz(ind_it[2], ((ind_src+2*ind_src[1]+ind_src[2])/4.0)) 16 | ind_lag := 2.0*ind_it-nz(ind_it[2]) 17 | [ind_it, ind_lag] 18 | // 19 | 20 | [ind_it_t, ind_lag_t] = ehlers_itrend() 21 | ind_it = security(syminfo.tickerid, ind_timeframe, ind_it_t[1], lookahead=barmerge.lookahead_on) 22 | ind_lag = security(syminfo.tickerid, ind_timeframe, ind_lag_t[1], lookahead=barmerge.lookahead_on) 23 | 24 | ///////////////////////////////////////////////////// 25 | // Plots 26 | plot(ind_it, color=color.red, linewidth=1, title="Trend") 27 | plot(ind_lag, color=color.blue, linewidth=1, title="Trigger") 28 | -------------------------------------------------------------------------------- /pinescript/ind_stochastic_multi_timeframe.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | strategy("Stochastic MTF", overlay=false) 3 | 4 | stoch_length = input(defval=14, title='Stochastic Length', type=input.integer) 5 | smooth_k = input(defval=3, title='SmoothK', type=input.integer) 6 | smooth_d = input(defval=3, title='SmoothD', type=input.integer) 7 | tframe = input(defval='60', title='Stochastic Timeframe',type=input.resolution) 8 | l_margin = input(defval=25, title='Overbought/Oversold margin', type=input.integer, maxval=100, minval=0) 9 | 10 | l_high = 100 - l_margin 11 | l_low = l_margin 12 | 13 | //////////////////////////// 14 | // Stochastic indicator 15 | stochastic(length_, smooth_k_, smooth_d_, tf_) => 16 | k_sig = sma(stoch(close, high, low, length_), smooth_k_) 17 | d_sig = sma(k_sig, smooth_d_) 18 | ds_sig = security(syminfo.ticker, tf_, d_sig) 19 | ks_sig = security(syminfo.ticker, tf_, k_sig) 20 | [ks_sig, ds_sig] 21 | // 22 | 23 | [k_sig, d_sig] = stochastic(stoch_length, smooth_k, smooth_d, tframe) 24 | 25 | ///////////////////////////// 26 | /// Plot signals 27 | plot(k_sig, title='SmoothK', color=color.red) 28 | plot(d_sig, title='SmoothD', color=color.blue) 29 | plot(l_high, title='L-high', color=color.black, linewidth=2) 30 | plot(l_low, title='L-low', color=color.black, linewidth=2) 31 | -------------------------------------------------------------------------------- /pinescript/rsi_rsi_crossover.psc: -------------------------------------------------------------------------------- 1 | //@version=3 2 | strategy(title="RSI - RSI Crossover", shorttitle="RSI - RSI Crossover", overlay=false) 3 | 4 | //res = input("15", type=resolution) 5 | rsi_len = input(30, type=integer, title="RSI Length") 6 | ema_len1 = input(10, type=integer, title="RSI pre-smooth EMA Length") 7 | ema_len2 = input(10, type=integer, title="RSI post-smooth EMA Length") 8 | 9 | // === INPUT BACKTEST RANGE === 10 | FromMonth = input(defval = 6, title = "From Month", minval = 1, maxval = 12) 11 | FromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31) 12 | FromYear = input(defval = 2017, title = "From Year") 13 | 14 | // === FUNCTION EXAMPLE === 15 | start = timestamp(FromYear, FromMonth, FromDay, 00, 00) // backtest start window 16 | finish = time // backtest finish window 17 | window() => time >= start and time <= finish ? true : false // create function "within window of time" 18 | 19 | ema_sig0 = ema(close, ema_len1) 20 | rsi_sig = rsi(ema_sig0, rsi_len) 21 | rsi_sig_m = ema(rsi_sig, ema_len2) 22 | 23 | buy = crossover(rsi_sig, rsi_sig_m) 24 | sell = crossunder(rsi_sig, rsi_sig_m) 25 | 26 | strategy.entry("L", strategy.long, when = window() and buy) 27 | //strategy.close("L", when = selli) 28 | strategy.entry("S", strategy.short, when = window() and sell) 29 | //strategy.close("S", when = buyi) 30 | 31 | plot(rsi_sig, color=red, linewidth=2) 32 | plot(rsi_sig_m, color=green, linewidth=1) 33 | -------------------------------------------------------------------------------- /pinescript/rsi_mean_reversion_v3.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | strategy(title="RSI mean rev v3", overlay=false, default_qty_type=strategy.percent_of_equity, default_qty_value=100) 3 | 4 | /////////////////////////////////////// 5 | // Inputs 6 | time_frame = input(defval='', title='Timeframe', type=input.resolution) 7 | rsi_period = input(defval=8, title='RSI period', type=input.integer) 8 | rsi_thresh = input(defval=30, title='RSI Threshold') 9 | ema_len = input(defval=100, title='Trend EMA') 10 | en_long = input(defval=true, title='Enable Long', type=input.bool) 11 | en_short = input(defval=false, title='Enable Short', type=input.bool) 12 | 13 | /////////////////////////////////////// 14 | // Calculate signals 15 | rsi_hi = 100 - rsi_thresh 16 | rsi_lo = rsi_thresh 17 | rsi_sig = security(syminfo.tickerid, time_frame, rsi(close, rsi_period)) 18 | ema_sig = ema(close, ema_len) 19 | 20 | /////////////////////////////////////// 21 | // Execute positions 22 | buy = (close > ema_sig) and crossover(rsi_sig, rsi_lo) 23 | sell = crossunder(rsi_sig, rsi_hi) 24 | short = (close < ema_sig) and crossunder(rsi_sig, rsi_hi) 25 | cover = crossover(rsi_sig, rsi_lo) 26 | 27 | if en_long 28 | strategy.entry("L", strategy.long, when=buy) 29 | strategy.close("L", when=sell) 30 | // 31 | if en_short 32 | strategy.entry("S", strategy.short, when=short) 33 | strategy.close("S", when=cover) 34 | // 35 | 36 | //////////////////////////////////////// 37 | // Plots 38 | plot(rsi_sig) 39 | -------------------------------------------------------------------------------- /pinescript/ind_volatility_bb_kt_multi_timeframe.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | study("BB-KT Volatility Indicator", overlay=false) 3 | 4 | source = input(defval=close, type=input.source, title='Source') 5 | length = input(defval=20, type=input.integer, title='Length', minval=1) 6 | mult = input(defval=2.0, type=input.float, title='Multiplier') 7 | atr_p = input(defval=14, type=input.integer, title='ATR Period (Only if it is used)') 8 | tframe = input(defval='1D', type=input.resolution, title='Timeframe') 9 | ema_p = input(defval=9, type=input.integer, title='Volatility Smooth EMA Loopback Period') 10 | use_atr = input(defval=false, type=input.bool, title='Use ATR for Keltner ?') 11 | 12 | // Bollinger Bands 13 | bb_basis = sma(source, length) 14 | bb_dev = mult * stdev(source, length) 15 | 16 | bb_upper = bb_basis + bb_dev 17 | bb_lower = bb_basis - bb_dev 18 | 19 | // Keltener Channel 20 | kt_range = use_atr ? atr(atr_p) : tr 21 | kt_rangema = sma(kt_range, length) 22 | kt_upper = bb_basis + kt_rangema * mult 23 | kt_lower = bb_basis - kt_rangema * mult 24 | 25 | vol_ind = (bb_upper - kt_upper) 26 | vol_ind_e = ema(vol_ind, ema_p) 27 | vol_ind_t = security(syminfo.tickerid, tframe, vol_ind) 28 | vol_ind_tt = security(syminfo.tickerid, tframe, vol_ind_e) 29 | 30 | plot(vol_ind_t, color=color.blue, linewidth=1, title='VOL_I') 31 | plot(vol_ind_tt, color=color.black, linewidth=1, title='VOL_IS') 32 | -------------------------------------------------------------------------------- /pinescript/donch_basis_crossover_with_its_lag.psc: -------------------------------------------------------------------------------- 1 | //@version=5 2 | strategy("Donchian Basis crossover - v1", overlay=true) 3 | 4 | ////////////////////////////////////////////////// 5 | // Inputs 6 | donch_len = input.int(200, title='Donchian Length') 7 | donch_lag = input.int(10, title='Donchian Lag') 8 | donch_tf = input.timeframe('', title='Donhcian Timeframe') 9 | donch_thr = input.float(0.0, title='Donchian Threshold') 10 | 11 | ////////////////////////////////////////////////// 12 | // Calculate indicators 13 | donch_ind_tt = math.avg(ta.highest(close, donch_len), ta.lowest(close, donch_len)) 14 | donch_ind = request.security(syminfo.tickerid, donch_tf, donch_ind_tt[1], gaps=barmerge.gaps_on, lookahead=barmerge.lookahead_on) 15 | donch_ind_lag = request.security(syminfo.tickerid, donch_tf, donch_ind[1+donch_lag], gaps=barmerge.gaps_on, lookahead=barmerge.lookahead_on) 16 | 17 | ///////////////////////////////////////////////// 18 | // Calculate buy sell signals 19 | buy = ta.crossover(donch_ind, donch_ind_lag) and (donch_ind > donch_ind_lag + donch_thr) 20 | sell = ta.crossunder(donch_ind, donch_ind_lag) and (donch_ind < donch_ind_lag - donch_thr) 21 | 22 | //////////////////////////////////////////////// 23 | // Execute positions 24 | strategy.entry("L", strategy.long, when=buy) 25 | strategy.entry("S", strategy.short, when=sell) 26 | 27 | ///////////////////////////////////////////////// 28 | // Plot indicators 29 | plot(donch_ind, color=color.green, title='Dind') 30 | plot(donch_ind_lag, color=color.red, title='Dind_lag') 31 | -------------------------------------------------------------------------------- /pinescript/r1_s1_breakout.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | strategy("R1-S1 breakout", overlay=true) 3 | 4 | time_frame = input("D", type=input.resolution) 5 | tolerance = input(5, type=input.integer) 6 | 7 | pivot_cpr(time_frame) => 8 | prev_close = security(syminfo.tickerid, time_frame, close[1], lookahead=true) 9 | prev_open = security(syminfo.tickerid, time_frame, open[1], lookahead=true) 10 | prev_high = security(syminfo.tickerid, time_frame, high[1], lookahead=true) 11 | prev_low = security(syminfo.tickerid, time_frame, low[1], lookahead=true) 12 | 13 | pi_level = (prev_high + prev_low + prev_close)/3 14 | bc_level = (prev_high + prev_low)/2 15 | tc_level = (pi_level - bc_level) + pi_level 16 | r1_level = pi_level * 2 - prev_low 17 | s1_level = pi_level * 2 - prev_high 18 | r2_level = (pi_level - s1_level) + r1_level 19 | s2_level = pi_level - (r1_level - s1_level) 20 | 21 | [pi_level, tc_level, bc_level, r1_level, s1_level, r2_level, s2_level] 22 | // 23 | 24 | [pi_level, tc_level, bc_level, r1_level, s1_level, r2_level, s2_level] = pivot_cpr(time_frame) 25 | 26 | buy = close > r1_level + tolerance 27 | sell = close < s1_level - tolerance 28 | 29 | strategy.entry("L", strategy.long, when=buy) 30 | strategy.entry("S", strategy.short, when=sell) 31 | 32 | plot(r1_level, style=plot.style_circles, linewidth=2, color=color.green, title="R1") 33 | plot(s1_level, style=plot.style_circles, linewidth=2, color=color.red, title="S1") 34 | -------------------------------------------------------------------------------- /pinescript/r1_s1_M_breakout.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | strategy("R1-S1 M breakout", overlay=true) 3 | 4 | time_frame = input("D", type=input.resolution) 5 | tolerance = input(5.0, type=input.float) 6 | m_factor = input(1.0, type=input.float, step=0.01) 7 | is_tol_perc = input(false, type=input.bool) 8 | 9 | pivot_M(time_frame) => 10 | prev_close = security(syminfo.tickerid, time_frame, close[1], lookahead=true) 11 | prev_open = security(syminfo.tickerid, time_frame, open[1], lookahead=true) 12 | prev_high = security(syminfo.tickerid, time_frame, high[1], lookahead=true) 13 | prev_low = security(syminfo.tickerid, time_frame, low[1], lookahead=true) 14 | 15 | //X = (prev_close > prev_open) ? (2*prev_high + prev_low + prev_close) : ((prev_close < prev_open) ? (prev_high + 2*prev_low + prev_close) : (prev_high + prev_low + 2*prev_close)) 16 | X = (prev_high + prev_low + prev_close)/3 17 | R1 = (X) + m_factor*(prev_high - prev_low) 18 | S1 = (X) - m_factor*(prev_high - prev_low) 19 | 20 | [R1, S1] 21 | // 22 | 23 | [r_level, s_level] = pivot_M(time_frame) 24 | 25 | buy = close > r_level + tolerance 26 | sell = close < s_level - tolerance 27 | if is_tol_perc 28 | buy := close > r_level * (1 + tolerance/100.0) 29 | sell := close < s_level * (1 - tolerance/100.0) 30 | // 31 | 32 | strategy.entry("L", strategy.long, when=buy) 33 | strategy.entry("S", strategy.short, when=sell) 34 | 35 | plot(r_level, style=plot.style_circles, linewidth=2, color=color.green, title="R1") 36 | plot(s_level, style=plot.style_circles, linewidth=2, color=color.red, title="S1") 37 | -------------------------------------------------------------------------------- /pinescript/ind_cpr_daily_weekly_combined.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | study("CPR Combined", overlay=true) 3 | 4 | cpr(time_frame) => 5 | prev_close = security(syminfo.tickerid, time_frame, close[1], lookahead=true) 6 | prev_open = security(syminfo.tickerid, time_frame, open[1], lookahead=true) 7 | prev_high = security(syminfo.tickerid, time_frame, high[1], lookahead=true) 8 | prev_low = security(syminfo.tickerid, time_frame, low[1], lookahead=true) 9 | 10 | pi_level = (prev_high + prev_low + prev_close)/3 11 | bc_level = (prev_high + prev_low)/2 12 | tc_level = (pi_level - bc_level) + pi_level 13 | r1_level = pi_level * 2 - prev_low 14 | s1_level = pi_level * 2 - prev_high 15 | r2_level = (pi_level - s1_level) + r1_level 16 | s2_level = pi_level - (r1_level - s1_level) 17 | 18 | [pi_level, tc_level, bc_level, r1_level, s1_level, r2_level, s2_level, prev_high, prev_low] 19 | // 20 | 21 | [d_pi_level, d_tc_level, d_bc_level, d_r1_level, d_s1_level, d_r2_level, d_s2_level, d_hi_level, d_lo_level] = cpr('1D') 22 | [w_pi_level, w_tc_level, w_bc_level, w_r1_level, w_s1_level, w_r2_level, w_s2_level, w_hi_level, w_lo_level] = cpr('1W') 23 | 24 | plot(d_bc_level, linewidth=1, color=color.blue, title="D_BC") 25 | plot(d_tc_level, linewidth=1, color=color.blue, title="D_TC") 26 | plot(d_hi_level, linewidth=1, color=color.black, title="D_PHI") 27 | plot(d_lo_level, linewidth=1, color=color.olive, title="D_PLO") 28 | plot(w_hi_level, linewidth=1, color=color.green, title="W_PHI") 29 | plot(w_lo_level, linewidth=1, color=color.red, title="W_PLO") 30 | -------------------------------------------------------------------------------- /pinescript/ind_pivot_levels_ol2_v1.psc: -------------------------------------------------------------------------------- 1 | //@version=5 2 | indicator("Pivot Levels v2 (Using ol2 as source)", overlay=true) 3 | 4 | //////////////////////////////////////////////////////////////////// 5 | // Inputs 6 | pivot_bars = input.int(defval=2, title='Pivot Bars Length') 7 | pivot_tframe = input.timeframe(defval='1D', title='Pivot Time Frame') 8 | 9 | ////////////////////////////////////////////////////////////////// 10 | // Misc functions 11 | /////////////////////////////////////////////////////// 12 | // Get data from high time frame 13 | get_security(_src, _tf) => 14 | request.security(syminfo.tickerid, _tf, _src[1], gaps=barmerge.gaps_on, lookahead=barmerge.lookahead_on) 15 | // 16 | 17 | // Get Pivots 18 | pivots_ol2(_pivot_bars, _pivot_tf) => 19 | phigh_t = ta.pivothigh(math.avg(open, close), _pivot_bars, _pivot_bars) 20 | plow_t = ta.pivotlow(math.avg(open, close), _pivot_bars, _pivot_bars) 21 | phigh = phigh_t 22 | plow = plow_t 23 | phigh_tf = get_security(phigh_t, _pivot_tf) 24 | plow_tf = get_security(plow_t, _pivot_tf) 25 | phigh_san = phigh_tf 26 | plow_san = plow_tf 27 | phigh_san := nz(phigh_tf[1]) ? phigh_tf[1] : phigh_san[1] 28 | plow_san := nz(plow_tf[1]) ? plow_tf[1] : plow_san[1] 29 | [phigh_san, plow_san] 30 | // 31 | 32 | //////////////////////////////////////////////////////////////////// 33 | // Indicators 34 | [phigh_san, plow_san] = pivots_ol2(pivot_bars, pivot_tframe) 35 | 36 | ///////////////////////////////////////////////////////////////////// 37 | // Plots 38 | plot(phigh_san, color=color.green, title='P+') 39 | plot(plow_san, color=color.red, title='P-') 40 | -------------------------------------------------------------------------------- /pinescript/roc-pct_mean_reversion_v1.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | strategy(title="ROC-pct mean rev v1", overlay=false, default_qty_type=strategy.percent_of_equity, default_qty_value=100) 3 | 4 | /////////////////////////////////////// 5 | // Inputs 6 | time_frame = input(defval='', title='Timeframe', type=input.resolution) 7 | rocpct_period = input(defval=8, title='ROC-pct period', type=input.integer) 8 | ema_len = input(defval=100, title='Trend EMA') 9 | en_long = input(defval=true, title='Enable Long', type=input.bool) 10 | en_short = input(defval=false, title='Enable Short', type=input.bool) 11 | en_tfilt = input(defval=false, title='Enable Trend Filter', type=input.bool) 12 | 13 | /////////////////////////////////////// 14 | // Calculate signals 15 | roc_pct_t = (close - lowest(close, rocpct_period))/(highest(close, rocpct_period) - lowest(close, rocpct_period)) * 100 16 | roc_pct_sig = security(syminfo.tickerid, time_frame, roc_pct_t) 17 | ema_sig = ema(close, ema_len) 18 | trend_up = en_tfilt ? (close > ema_sig) : true 19 | trend_dn = en_tfilt ? (close < ema_sig) : true 20 | 21 | /////////////////////////////////////// 22 | // Execute positions 23 | 24 | buy = trend_up and crossover(roc_pct_sig, 50) 25 | sell = crossunder(roc_pct_sig, 50) 26 | short = trend_dn and crossunder(roc_pct_sig, 50) 27 | cover = crossover(roc_pct_sig, 50) 28 | 29 | if en_long 30 | strategy.entry("L", strategy.long, when=buy) 31 | strategy.close("L", when=sell) 32 | // 33 | if en_short 34 | strategy.entry("S", strategy.short, when=short) 35 | strategy.close("S", when=cover) 36 | // 37 | 38 | //////////////////////////////////////// 39 | // Plots 40 | plot(roc_pct_sig) 41 | -------------------------------------------------------------------------------- /pinescript/ind_ehlers_time_warp_laguerre_rsi_v1.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | study("Ehlers Laguerre RSI v1", overlay=false) 3 | 4 | /////////////////////////////////////////////////// 5 | // Inputs 6 | filt_gamma = input(defval=0.8, title="Gamma", type=input.float, step=0.1) 7 | time_frame = input(defval='1D', title="Time Frame", type=input.resolution) 8 | src = input(defval=close, title="Sourc", type=input.source) 9 | ema_len = input(defval=0, title="RSI Smooth EMA Length", type=input.integer) 10 | 11 | /////////////////////////////////////////////////// 12 | // Calculate Signals 13 | LaguerreRSI_v1(gamma, src) => 14 | L0 = src 15 | L1 = src 16 | L2 = src 17 | L3 = src 18 | L0 := (1-gamma)*src + gamma*nz(L0[1]) 19 | L1 := -gamma*L0 + L0[1] + gamma*nz(L1[1]) 20 | L2 := -gamma*L1 + L1[1] + gamma*nz(L2[1]) 21 | L3 := -gamma*L2 + L2[1] + gamma*nz(L3[1]) 22 | CU = 0.0 23 | CD = 0.0 24 | if (L0>=L1) 25 | CU := (L0-L1) 26 | else 27 | CD := (L1-L0) 28 | // 29 | if (L1>=L2) 30 | CU := CU + (L1-L2) 31 | else 32 | CD := CD + (L2-L1) 33 | // 34 | if (L2>=L3) 35 | CU := CU + (L2-L3) 36 | else 37 | CD := CD + (L3-L2) 38 | // 39 | CU_new = CU 40 | CD_new = CD 41 | if ema_len > 0 42 | CU_new := ema(CU, ema_len) 43 | CD_new := ema(CD, ema_len) 44 | // 45 | RSI = CU_new/(CU_new+CD_new) 46 | // 47 | 48 | filt_ind_t = LaguerreRSI_v1(filt_gamma, src) 49 | filt_ind = security(syminfo.tickerid, time_frame, filt_ind_t) 50 | 51 | //////////////////////////////////////////////////////////// 52 | // Plots 53 | plot(filt_ind, title='Laguerre RSI') 54 | -------------------------------------------------------------------------------- /pinescript/rsi_ch_breakout_v2.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | strategy("RSI channel breakout v2", overlay=false) 3 | 4 | /////////////////////////////////////////////////////////////////////// 5 | // Inputs 6 | rsi_period = input(defval=14, title="RSI Period", type=input.integer, minval=1, maxval=100, step=1) 7 | donch_period = input(defval=9, title="Donch RSI Period", type=input.integer, minval=1, maxval=200, step=1) 8 | time_frame = input(defval='1D', title='RSI Time frame', type=input.resolution) 9 | 10 | //////////////////////////////////////////////////////////////////// 11 | // Get indicator signals 12 | cum_rsi_t = rsi(close, rsi_period) + rsi(close, rsi_period)[1] + rsi(close, rsi_period)[2] 13 | donch_rsi_hi_t = highest(cum_rsi_t, donch_period) 14 | donch_rsi_lo_t = lowest(cum_rsi_t, donch_period) 15 | cum_rsi = security(syminfo.tickerid, time_frame, cum_rsi_t) 16 | donch_rsi_hi = security(syminfo.tickerid, time_frame, donch_rsi_hi_t) 17 | donch_rsi_lo = security(syminfo.tickerid, time_frame, donch_rsi_lo_t) 18 | 19 | //////////////////////////////////////////////////////////////// 20 | // Position signals 21 | buy = cum_rsi > donch_rsi_hi[1] 22 | short = cum_rsi < donch_rsi_lo[1] 23 | 24 | //////////////////////////////////////////////////////////////// 25 | // Execute positions 26 | strategy.entry("long", strategy.long, when=buy) 27 | strategy.entry("short", strategy.short, when=short) 28 | 29 | //////////////////////////////////////////////////////////////// 30 | // Plots 31 | plot(cum_rsi, title="CUM RSI", color=color.green) 32 | plot(donch_rsi_hi, title="CUM RSI HI", color=color.olive) 33 | plot(donch_rsi_lo, title="CUM_RSI_LO", color=color.red) 34 | -------------------------------------------------------------------------------- /pinescript/heikinashi_donch_breakout.psc: -------------------------------------------------------------------------------- 1 | strategy(title="Donchian Heikinashi", shorttitle="Donchian Heikinashi", overlay=true, slippage=20) 2 | 3 | length = input(20, minval=1, title="Channel Length") 4 | lag = input(0, minval=0, title ="Lag") 5 | 6 | // === INPUT BACKTEST RANGE === 7 | FromMonth = input(defval = 6, title = "From Month", minval = 1, maxval = 12) 8 | FromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31) 9 | FromYear = input(defval = 2017, title = "From Year") 10 | 11 | start = timestamp(FromYear, FromMonth, FromDay, 00, 00) // backtest start window 12 | finish = time // backtest finish window 13 | 14 | window() => time >= start and time <= finish ? true : false // create function "within window of time" 15 | 16 | close_h = security(heikinashi(tickerid), period, close) 17 | high_h = security(heikinashi(tickerid), period, high) 18 | low_h = security(heikinashi(tickerid), period, low) 19 | 20 | upper = highest(high_h, length) 21 | lower = lowest(low_h, length) 22 | basis = avg(upper, lower) 23 | 24 | buy = close_h > upper[lag+1] 25 | sell = close_h < lower[lag+1] 26 | 27 | // === EXECUTION === 28 | strategy.entry("L", strategy.long, when = window() and buy) // buy long when "within window of time" AND crossover 29 | //strategy.close("L", when = window() and sell) // sell long when "within window of time" AND crossunder 30 | strategy.entry("S", strategy.short, when = window() and sell) 31 | //strategy.close("S", when = window() and buy) 32 | 33 | plot(lower, style=line, linewidth=1, color=red, offset=1) 34 | plot(upper, style=line, linewidth=1, color=green, offset=1) 35 | plot(basis, color=black, style=line, linewidth=1, title="Mid-Line Average") 36 | -------------------------------------------------------------------------------- /pinescript/ind_candlestick_patterns.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | study("Candlestick patterns", overlay=true) 3 | 4 | ///////////////////////////////////// 5 | /// Params 6 | shape_loc = location.belowbar 7 | shape_style = shape.arrowup 8 | 9 | //////////////////////////////////// 10 | /// Patterns 11 | // Inside bars 12 | ibar_0 = (high < high[1]) and (low > low[1]) 13 | ibar_1 = (high < high[2]) and (low > low[2]) and (high[1] < high[2]) and (low[1] > low[2]) 14 | ibar_2 = (high < high[3]) and (low > low[3]) and (high[1] < high[3]) and (low[1] > low[3]) and (high[2] < high[3]) and (low[2] > low[3]) 15 | 16 | // Outside bars 17 | obar_0 = (high > high[1]) and (low < low[1]) 18 | obar_1 = (high > high[2]) and (low < low[2]) and (high[1] > high[2]) and (low[1] < low[2]) 19 | obar_2 = (high > high[3]) and (low < low[3]) and (high[1] > high[3]) and (low[1] < low[3]) and (high[2] > high[3]) and (low[2] < low[3]) 20 | 21 | //////////////////////////////////// 22 | /// Plot patterns 23 | plotshape(ibar_0, title= "Inside_bar", location=shape_loc, color=color.lime, style=shape_style, text="IB") 24 | plotshape(ibar_1, title="Inside_bar1", location=shape_loc, color=color.teal, style=shape_style, text="IB1") 25 | plotshape(ibar_2, title="Inside_bar2", location=shape_loc, color=color.blue, style=shape_style, text="IB2") 26 | 27 | plotshape(obar_0, title="Outside_bar", location=shape_loc, color=color.orange, style=shape_style, text="OB") 28 | plotshape(obar_1, title="Outside_bar1", location=shape_loc, color=color.red, style=shape_style, text="OB1") 29 | plotshape(obar_2, title="Outside_bar2", location=shape_loc, color=color.navy, style=shape_style, text="OB2") 30 | -------------------------------------------------------------------------------- /pinescript/inverse_fisher_transform_on_rsi.psc: -------------------------------------------------------------------------------- 1 | //@version=5 2 | // Inverse Fisher Transform applied to simple smoothed RSI 3 | // 4 | strategy("Inverse Fisher Transform on RSI", shorttitle="IFTRSI") 5 | 6 | ///////////////////////////////////////////////// 7 | // Parameters 8 | ifish_rsi_length = input.int(defval=5, title="Inv Fisher Transform - RSI Length") 9 | ifish_wma_length = input.int(defval=9, title="Inv Fisher Transform - RSI Smoothing length") 10 | ifish_psma_length = input.int(defval=1, title="Inv Fisher Transform - Post Smooth Length") 11 | ifish_timeframe = input.timeframe(defval="", title="Inv Fisher Transform - RSI TimeFrame") 12 | 13 | //////////////////////////////////////////////// 14 | // Signals 15 | ifish_rsi_ind = 0.1 * (ta.rsi(close, ifish_rsi_length) - 50) 16 | ifish_srsi_ind = ta.wma(ifish_rsi_ind, ifish_wma_length) 17 | ifish_input = request.security(syminfo.tickerid, ifish_timeframe, ifish_srsi_ind[1], barmerge.gaps_on, lookahead=barmerge.lookahead_on) 18 | ifish_ind = ta.ema((math.exp(2*ifish_input) - 1)/(math.exp(2*ifish_input) + 1), ifish_psma_length) 19 | 20 | ///////////////////////////////////////////////// 21 | // Positional signals 22 | buy_sig = ta.crossover(ifish_ind, 0) 23 | sell_sig = ta.crossunder(ifish_ind, 0) 24 | 25 | ///////////////////////////////////////////////// 26 | // Execute signals 27 | strategy.entry("L", strategy.long, when=buy_sig) 28 | strategy.entry("S", strategy.short, when=sell_sig) 29 | 30 | ///////////////////////////////////////////////// 31 | // Plot signals 32 | plot(ifish_ind, color=color.green, linewidth=1) 33 | hline(0.5, color=color.red) 34 | hline(-0.5, color=color.green) 35 | -------------------------------------------------------------------------------- /pinescript/rsi_mean_reversion_v4_roc-pct_of_rsi.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | strategy(title="RSI mean rev v4 - Roc-pct of RSI", overlay=false, default_qty_type=strategy.percent_of_equity, default_qty_value=100) 3 | 4 | /////////////////////////////////////// 5 | // Inputs 6 | time_frame = input(defval='', title='Timeframe', type=input.resolution) 7 | rsi_period = input(defval=8, title='RSI period', type=input.integer) 8 | ema_len = input(defval=100, title='Trend EMA') 9 | en_long = input(defval=true, title='Enable Long', type=input.bool) 10 | en_short = input(defval=false, title='Enable Short', type=input.bool) 11 | en_tfilt = input(defval=true, title='Enable Trend Filter', type=input.bool) 12 | 13 | /////////////////////////////////////// 14 | // Calculate signals 15 | rsi_sig_t = rsi(close, rsi_period) 16 | rsi_sig_tt = (rsi_sig_t - lowest(rsi_sig_t, rsi_period))/(highest(rsi_sig_t, rsi_period) - lowest(rsi_sig_t, rsi_period)) * 100 17 | rsi_sig = security(syminfo.tickerid, time_frame, rsi_sig_tt) 18 | ema_sig = ema(close, ema_len) 19 | trend_up = en_tfilt ? (close > ema_sig) : true 20 | trend_dn = en_tfilt ? (close < ema_sig) : false 21 | 22 | /////////////////////////////////////// 23 | // Execute positions 24 | buy = trend_up and crossover(rsi_sig, 50) 25 | sell = crossunder(rsi_sig, 50) 26 | short = trend_dn and crossunder(rsi_sig, 50) 27 | cover = crossover(rsi_sig, 50) 28 | 29 | if en_long 30 | strategy.entry("L", strategy.long, when=buy) 31 | strategy.close("L", when=sell) 32 | // 33 | if en_short 34 | strategy.entry("S", strategy.short, when=short) 35 | strategy.close("S", when=cover) 36 | // 37 | 38 | //////////////////////////////////////// 39 | // Plots 40 | plot(rsi_sig) 41 | -------------------------------------------------------------------------------- /pinescript/guppy_ma_cross.psc: -------------------------------------------------------------------------------- 1 | strategy(title="Guppy MA", shorttitle="Guppy MA", slippage=20) 2 | 3 | guppy_ema_len1 = input(defval=2, title="Guppy EMA pre-smooth length", minval=1) 4 | guppy_ema_len2 = input(defval=5, title="Guppy EMA post-smooth length", minval=1) 5 | 6 | // === INPUT BACKTEST RANGE === 7 | FromMonth = input(defval = 6, title = "From Month", minval = 1, maxval = 12) 8 | FromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31) 9 | FromYear = input(defval = 2017, title = "From Year") 10 | 11 | start = timestamp(FromYear, FromMonth, FromDay, 00, 00) // backtest start window 12 | finish = time // backtest finish window 13 | 14 | window() => time >= start and time <= finish ? true : false 15 | 16 | guppy0 = ema(close, 3) 17 | guppy1 = ema(close, 5) 18 | guppy2 = ema(close, 8) 19 | guppy3 = ema(close, 12) 20 | guppy4 = ema(close, 15) 21 | 22 | g1 = (guppy0+guppy1+guppy2+guppy3+guppy4)/5 23 | 24 | guppy5 = ema(close, 30) 25 | guppy6 = ema(close, 35) 26 | guppy7 = ema(close, 40) 27 | guppy8 = ema(close, 45) 28 | guppy9 = ema(close, 50) 29 | 30 | g2 = (guppy5+guppy6+guppy7+guppy8+guppy9)/5 31 | 32 | fast_guppy = ema(g1 - g2, guppy_ema_len1) 33 | slow_guppy = ema(fast_guppy, guppy_ema_len2) 34 | 35 | buy = crossover(fast_guppy, slow_guppy) //close_h > upper[lag+1] 36 | sell = crossunder(fast_guppy, slow_guppy) //close_h < lower[lag+1] 37 | 38 | // === EXECUTION === 39 | strategy.entry("L", strategy.long, when = window() and buy) // buy long when "within window of time" AND crossover 40 | strategy.entry("S", strategy.short, when = window() and sell) 41 | 42 | plot(fast_guppy, style=line, linewidth=1, color=red, offset=1) 43 | plot(slow_guppy, style=line, linewidth=1, color=green, offset=1) 44 | -------------------------------------------------------------------------------- /pinescript/ind_pivot_points_std.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | study("Pivot Points Standard", overlay=true) 3 | 4 | time_frame = input("D", type=input.resolution) 5 | 6 | cpr(time_frame) => 7 | prev_close = security(syminfo.tickerid, time_frame, close[1], lookahead=true) 8 | prev_open = security(syminfo.tickerid, time_frame, open[1], lookahead=true) 9 | prev_high = security(syminfo.tickerid, time_frame, high[1], lookahead=true) 10 | prev_low = security(syminfo.tickerid, time_frame, low[1], lookahead=true) 11 | 12 | pi_level = (prev_high + prev_low + prev_close)/3 13 | bc_level = (prev_high + prev_low)/2 14 | tc_level = (pi_level - bc_level) + pi_level 15 | r1_level = pi_level * 2 - prev_low 16 | s1_level = pi_level * 2 - prev_high 17 | r2_level = (pi_level - s1_level) + r1_level 18 | s2_level = pi_level - (r1_level - s1_level) 19 | 20 | [pi_level, tc_level, bc_level, r1_level, s1_level, r2_level, s2_level] 21 | // 22 | 23 | [pi_level, tc_level, bc_level, r1_level, s1_level, r2_level, s2_level] = cpr(time_frame) 24 | 25 | plot(pi_level, style=plot.style_circles, linewidth=2, color=color.black, title="PI") 26 | plot(bc_level, style=plot.style_circles, linewidth=2, color=color.blue, title="BC") 27 | plot(tc_level, style=plot.style_circles, linewidth=2, color=color.blue, title="TC") 28 | plot(r1_level, style=plot.style_circles, linewidth=2, color=color.green, title="R1") 29 | plot(s1_level, style=plot.style_circles, linewidth=2, color=color.red, title="S1") 30 | plot(r2_level, style=plot.style_circles, linewidth=1, color=color.orange, title="R2") 31 | plot(s2_level, style=plot.style_circles, linewidth=1, color=color.maroon, title="S2") 32 | -------------------------------------------------------------------------------- /pinescript/ind_orb-c.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | study(title="ORB-C", shorttitle="ORB-C", overlay=true) 3 | 4 | /////////////////////////////////// 5 | /// Inputs 6 | time_frame_m = input(defval='15m', title="Resolution", options=['1m', '5m', '10m', '15m', '30m', '45m', '1h', '2h', '4h', '1D', '2D', '4D', '1W', '2W', '1M', '2M', '6M']) 7 | bar_lag = input(defval=0, title="Ignore first n bars", type=input.integer) 8 | 9 | /////////////////////////////////// 10 | /// Functions 11 | get_time_frame(tf) => 12 | (tf == '1m') ? "1" 13 | : (tf == '5m') ? "5" 14 | : (tf == '10m') ? "10" 15 | : (tf == '15m') ? "15" 16 | : (tf == '30m') ? "30" 17 | : (tf == '45m') ? "45" 18 | : (tf == '1h') ? "60" 19 | : (tf == '2h') ? "120" 20 | : (tf == '4h') ? "240" 21 | : (tf == '1D') ? "D" 22 | : (tf == '2D') ? "2D" 23 | : (tf == '4D') ? "4D" 24 | : (tf == '1W') ? "W" 25 | : (tf == '2W') ? "2W" 26 | : (tf == '1M') ? "M" 27 | : (tf == '2M') ? "2M" 28 | : (tf == '6M') ? "6M" 29 | : "wrong resolution" 30 | // 31 | time_frame = get_time_frame(time_frame_m) 32 | 33 | is_newbar(res, lag) => 34 | change(time(res)[lag]) != 0 35 | // 36 | 37 | ///////////////////////////////////////////////////////// 38 | /// Signals 39 | high_range = valuewhen(is_newbar('D', bar_lag), high, 0) 40 | low_range = valuewhen(is_newbar('D', bar_lag), low, 0) 41 | 42 | high_rangeL = security(syminfo.tickerid, time_frame, high_range) 43 | low_rangeL = security(syminfo.tickerid, time_frame, low_range) 44 | 45 | //////////////////////////////////////////////////////// 46 | ///Plots 47 | plot(high_rangeL, color=color.green, linewidth=2, title='H_RL') 48 | plot(low_rangeL, color=color.red, linewidth=2, title='L_RL') 49 | -------------------------------------------------------------------------------- /pinescript/roc_pct_cross50_touch0.psc: -------------------------------------------------------------------------------- 1 | //@version=5 2 | strategy(title="ROC-pct x50 t0", shorttitle="ROC-pct x50 t0", default_qty_type=strategy.percent_of_equity, default_qty_value=100) 3 | 4 | //////////////////////////////////////// 5 | // Inputs 6 | length = input.int(9, minval=1, title='ROC-pct Length') 7 | source = input(close, "Source") 8 | tf = input.timeframe('', title='ROC-pct Time Frame') 9 | en_short = input.bool(false, title='Enable Shorts ?') 10 | 11 | //////////////////////////////////////////// 12 | // Signals 13 | source_roc = ta.roc(source, length) 14 | roc_t = 100 * (source - ta.lowest(source, length))/(ta.highest(source, length) - ta.lowest(source, length)) 15 | roc2_t = 100 * (source_roc - ta.lowest(source_roc, length))/(ta.highest(source_roc, length) - ta.lowest(source_roc, length)) 16 | 17 | roc = request.security(syminfo.tickerid, tf, roc_t) 18 | roc2 = request.security(syminfo.tickerid, tf, roc2_t) 19 | 20 | ///////////////////////////////////////////// 21 | // Buy/Sell Signals 22 | buy_sig = ta.crossover(roc, 50) //and (ro2 > 50)c 23 | sell_sig = (roc == 0) //or (roc2 < 50) 24 | short_sig = ta.crossunder(roc, 50) //and (roc2 < 50) 25 | cover_sig = (roc == 100) //or (roc2 > 50) 26 | 27 | //////////////////////////////////////////// 28 | // Execute positions 29 | if buy_sig 30 | strategy.entry("Long", strategy.long) 31 | // 32 | if sell_sig 33 | strategy.close("Long") 34 | // 35 | if en_short 36 | if short_sig 37 | strategy.entry("Short", strategy.short) 38 | // 39 | if cover_sig 40 | strategy.close("Short") 41 | // 42 | // 43 | 44 | /////////////////////////////////////////////// 45 | // Plots 46 | plot(roc, color=color.lime, title="ROC") 47 | plot(roc2, color=color.red, title="ROC2") 48 | hline(50, color=#787B86, title="Zero Line") 49 | -------------------------------------------------------------------------------- /pinescript/orb-a_strategy.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | strategy(title="ORB-A strategy", shorttitle="ORB-A strategy", overlay=true) 3 | 4 | tolerance = input(defval=0, title="Tolerance") 5 | time_frame_m = input(defval='1D', title="Resolution", options=['1m', '5m', '10m', '15m', '30m', '45m', '1h', '2h', '4h', '1D', '2D', '4D', '1W', '2W', '1M', '2M', '6M']) 6 | 7 | get_time_frame(tf) => 8 | (tf == '1m') ? "1" 9 | : (tf == '5m') ? "5" 10 | : (tf == '10m') ? "10" 11 | : (tf == '15m') ? "15" 12 | : (tf == '30m') ? "30" 13 | : (tf == '45m') ? "45" 14 | : (tf == '1h') ? "60" 15 | : (tf == '2h') ? "120" 16 | : (tf == '4h') ? "240" 17 | : (tf == '1D') ? "D" 18 | : (tf == '2D') ? "2D" 19 | : (tf == '4D') ? "4D" 20 | : (tf == '1W') ? "W" 21 | : (tf == '2W') ? "2W" 22 | : (tf == '1M') ? "M" 23 | : (tf == '2M') ? "2M" 24 | : (tf == '6M') ? "6M" 25 | : "wrong resolution" 26 | // 27 | time_frame = get_time_frame(time_frame_m) 28 | 29 | 30 | is_newbar(res) => 31 | change(time(res)) != 0 32 | // 33 | 34 | high_range = valuewhen(is_newbar('D'), high, 0) 35 | low_range = valuewhen(is_newbar('D'), low, 0) 36 | 37 | high_rangeL = security(syminfo.tickerid, time_frame, high_range) 38 | low_rangeL = security(syminfo.tickerid, time_frame, low_range) 39 | //range = (high_rangeL - low_rangeL)/low_rangeL 40 | 41 | buy = crossover(close, high_rangeL+tolerance) 42 | sell = crossunder(close - tolerance, low_rangeL-tolerance) 43 | 44 | strategy.entry("L", strategy.long, when=buy) 45 | strategy.entry("S", strategy.short, when=sell) 46 | 47 | //plotshape(range < 0.01, style=shape.circle, location=location.belowbar, color=color.red) 48 | plot(high_rangeL, color=color.green, linewidth=2) 49 | plot(low_rangeL, color=color.red, linewidth=2) 50 | 51 | -------------------------------------------------------------------------------- /pinescript/r1_s1_dmark_breakout_intraday.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | strategy("R1-S1 DeMark breakout intraday", overlay=true) 3 | 4 | time_frame = input("D", type=input.resolution) 5 | tolerance = input(5, type=input.integer) 6 | 7 | intraday_sess = "1500-1515" 8 | trade_sess = "0915-1500" 9 | t_session = time(timeframe.period, intraday_sess) 10 | t_sess_trade = time(timeframe.period, trade_sess) 11 | sess_over = not na(t_session[1]) and na(t_session) 12 | sess_valid = not na(t_sess_trade[0]) 13 | 14 | 15 | pivot_dmark(time_frame) => 16 | prev_close = security(syminfo.tickerid, time_frame, close[1], lookahead=true) 17 | prev_open = security(syminfo.tickerid, time_frame, open[1], lookahead=true) 18 | prev_high = security(syminfo.tickerid, time_frame, high[1], lookahead=true) 19 | prev_low = security(syminfo.tickerid, time_frame, low[1], lookahead=true) 20 | 21 | X = (prev_close > prev_open) ? (2*prev_high + prev_low + prev_close) : ((prev_close < prev_close) ? (prev_high + 2*prev_low + prev_close) : (prev_high + prev_low + 2*prev_close)) 22 | R1 = (X/2) - prev_low 23 | S1 = (X/2) - prev_high 24 | 25 | [R1, S1] 26 | // 27 | 28 | [r_level, s_level] = pivot_dmark(time_frame) 29 | 30 | buy = close > r_level + tolerance 31 | sell = close < s_level - tolerance 32 | 33 | strategy.entry("L", strategy.long, when=buy and sess_valid) 34 | strategy.entry("S", strategy.short, when=sell and sess_valid) 35 | //strategy.close("L", when=crossunder(close, r_level) and sess_valid) 36 | //strategy.close("S", when=crossover(close, s_level) and sess_valid) 37 | strategy.close_all(when=sess_over) 38 | 39 | plot(r_level, style=plot.style_circles, linewidth=2, color=color.green, title="R1") 40 | plot(s_level, style=plot.style_circles, linewidth=2, color=color.red, title="S1") 41 | -------------------------------------------------------------------------------- /pinescript/ind_ehlers_fisher_transform.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | study(title="Fisher Transform MTR", overlay=false) 3 | 4 | ///////////////////////////////////////////////////////////////////// 5 | // Inputs 6 | fish_len = input(defval=10, title='Channel Length', type=input.integer) 7 | param1 = input(defval=0.33, title='Param 1', type=input.float, maxval=1.0, minval=0.0, step=0.01) 8 | param2 = input(defval=0.5, title='Param 2', type=input.float, maxval=1.0, minval=0.0, step=0.01) 9 | fish_lag = input(defval=1, title='Fisher Lag signal length', type=input.integer) 10 | fish_mom_p = input(defval=1, title='Fisher Momentum Loopback Period Length', type=input.integer) 11 | time_frame = input(defval='1D', title='Time Frame', type=input.resolution) 12 | 13 | ////////////////////////////////////////////////////////////////// 14 | // Calculate signals 15 | 16 | fisher_transform(fish_len, param1, param2) => 17 | max_hi = highest(hl2, fish_len) 18 | min_lo = lowest(hl2, fish_len) 19 | value1 = 0.0 20 | value1 := param1*2*((hl2-min_lo)/(max_hi-min_lo)-0.5) + (1-param1)*nz(value1[1]) 21 | value2 = iff(value1>.99, .999, iff(value1<-.99, -.999, value1)) 22 | fish_t = 0.0 23 | fish_t := param2*log((1+value2)/(1-value2)) + 0.5*nz(fish_t[1]) 24 | // 25 | 26 | fish_t = fisher_transform(fish_len, param1, param2) 27 | fish = security(syminfo.tickerid, time_frame, fish_t) 28 | fish_mom_t = fish_len*mom(fish_t, fish_mom_p) 29 | fish_mom = security(syminfo.tickerid, time_frame, fish_mom_t) 30 | 31 | ///////////////////////////////////////////////////////////////// 32 | // Plots 33 | plot(fish, color=color.green, title="Fisher") 34 | plot(nz(fish[fish_lag]), color=color.red, title="Trigger") 35 | plot(fish_mom, color=color.blue, title="Momentum") 36 | -------------------------------------------------------------------------------- /pinescript/rsi_mean_reversion_v2.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | strategy(title="RSI mean rev v2", overlay=false) 3 | 4 | ///////////////////////////////////////////////////////////// 5 | // Inputs 6 | time_frame = input(defval='D', title='Timeframe', type=input.resolution) 7 | stop_loss = input(1.0, title="Stop loss", type=input.float) 8 | use_sperc = input(false, title='Stop loss & tolerance are in %centage(s)', type=input.bool) 9 | rsi_period = input(defval=8, title='RSI period', type=input.integer) 10 | rsi_high = input(defval=70, title='RSI High Boundary') 11 | rsi_low = input(defval=30, title='RSI Low Boundary') 12 | ema_len = input(defval=100, title='Trend EMA') 13 | 14 | /////////////////////////////////////////////////////////////// 15 | // Calculate stops 16 | stop_l = use_sperc ? strategy.position_avg_price*(1-stop_loss/100.0) : (strategy.position_avg_price-stop_loss) 17 | stop_s = use_sperc ? strategy.position_avg_price*(1+stop_loss/100.0) : (strategy.position_avg_price+stop_loss) 18 | 19 | /////////////////////////////////////////////////////////////////////// 20 | // Calculate signals 21 | rsi_sig = security(syminfo.tickerid, time_frame, rsi(close, rsi_period)) 22 | ema_sig = ema(close, ema_len) 23 | 24 | buy = (close > ema_sig) and crossover(rsi_sig, rsi_low) 25 | sell = crossunder(rsi_sig, rsi_high) or crossunder(close, stop_l) 26 | short = (close < ema_sig) and crossunder(rsi_sig, rsi_high) 27 | cover = crossover(rsi_sig, rsi_low) or crossover(close, stop_s) 28 | 29 | /////////////////////////////////////////////////////////////////// 30 | // Execute signals 31 | strategy.entry("L", strategy.long, when=buy) 32 | strategy.close("L", when=sell) 33 | strategy.entry("S", strategy.short, when=short) 34 | strategy.close("S", when=cover) 35 | 36 | //////////////////////////////////////////////////////////////////// 37 | // Plots 38 | plot(rsi_sig, color=color.blue, title="RSI") 39 | -------------------------------------------------------------------------------- /pinescript/orb-b_basis_cross_with_its_lag_v1.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | strategy(title="ORB-B basis crossover with its lag - v1", overlay=true) 3 | 4 | /////////////////////////////////////////////////////////////////////// 5 | // Inputs 6 | time_frame_m = input(defval='60', title="Resolution", type=input.resolution) 7 | time_frame_n = input(defval='1D', title="Time Gap", type=input.resolution) 8 | lag_period = input(defval=10, title="Lag Period", type=input.integer) 9 | smooth_period = input(defval=1, title='Pre-smooth period', type=input.integer) 10 | 11 | ////////////////////////////////////////////////////////////////////////// 12 | // Misc functions 13 | is_newbar(res) => 14 | change(time(res)) != 0 15 | // 16 | 17 | /////////////////////////////////////////////////////////////////////////// 18 | // Main signals (ORB-B) 19 | high_range = valuewhen(is_newbar(time_frame_n), smooth_period ? ema(high, smooth_period) : high, 0) 20 | low_range = valuewhen(is_newbar(time_frame_n), smooth_period ? ema(low, smooth_period) : low, 0) 21 | basis = security(syminfo.tickerid, time_frame_m, avg(high_range, low_range)[1], gaps=barmerge.gaps_on, lookahead=barmerge.lookahead_on) 22 | basis_lagged = security(syminfo.tickerid, time_frame_m, avg(high_range, low_range)[1+lag_period], gaps=barmerge.gaps_on, lookahead=barmerge.lookahead_on) 23 | 24 | //////////////////////////////////////////////////////////////////////// 25 | // Positonal signals 26 | buy = crossover(basis, basis_lagged) 27 | sell = crossunder(basis, basis_lagged) 28 | 29 | ////////////////////////////////////////////////////////////////////// 30 | // Execute positions 31 | strategy.entry("L", strategy.long, when=buy) 32 | strategy.entry("S", strategy.short, when=sell) 33 | 34 | ///////////////////////////////////////////////////////////////////////// 35 | // Plots 36 | plot(basis, color=color.green, linewidth=2) 37 | plot(basis_lagged, color=color.red, linewidth=2) 38 | -------------------------------------------------------------------------------- /pinescript/rsi_mean_reversion_using_single_ema_filter_and_ema_crossover_as_exit.psc: -------------------------------------------------------------------------------- 1 | //@version=5 2 | strategy("RSI mean reversion with single ema filter and ema crossover as exit", overlay=false) 3 | 4 | ///////////////////////////////////////////////////////////////////// 5 | // Inputs 6 | time_frame = input.timeframe(defval="", title="RSI Time frame") 7 | rsi_len = input.int(defval=2, title="RSI Length") 8 | rsi_threshold = input.int(defval=2, title="RSI Threshold") 9 | ema_time_frame = input.timeframe(defval="", title="EMA Time Frame") 10 | ema_len = input.int(defval=700, title="EMA Length") 11 | 12 | ///////////////////////////////////////////////////////////////////// 13 | // Get signals 14 | rsi_sig = request.security(syminfo.tickerid, time_frame, ta.rsi(close, rsi_len)[1], gaps=barmerge.gaps_on, lookahead=barmerge.lookahead_on) 15 | ema_sig = request.security(syminfo.tickerid, ema_time_frame, ta.ema(close, ema_len)[1], gaps=barmerge.gaps_on, lookahead=barmerge.lookahead_on) 16 | 17 | ///////////////////////////////////////////////////////////////////// 18 | // Positional signals 19 | buy = ta.crossunder(rsi_sig, rsi_threshold) and (close > ema_sig) 20 | sell = ta.crossunder(close, ema_sig) 21 | short = ta.crossover(rsi_sig, 100-rsi_threshold) and (close < ema_sig) 22 | cover = ta.crossover(close, ema_sig) 23 | 24 | //////////////////////////////////////////////////////////////////// 25 | // Execute positions 26 | if buy 27 | strategy.entry("L", strategy.long) 28 | if sell 29 | strategy.close("L") 30 | if short 31 | strategy.entry("S", strategy.short) 32 | if cover 33 | strategy.close("S") 34 | // 35 | 36 | ////////////////////////////////////////////////////////////////////// 37 | // Plotting functions 38 | plot(rsi_sig, color=color.blue, title="RSI") 39 | plot(rsi_threshold, color=color.black, title="RSI LThr") 40 | plot(100-rsi_threshold, color=color.black, title="RSI HThr") 41 | -------------------------------------------------------------------------------- /pinescript/supertrend_hl_smoothing.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | strategy("Supertrend with hl smoothing", "Supertrend with hl smoothing", overlay=true) 3 | 4 | atr_factor = input(defval=4, type=input.integer, title='Supertrend Factor') 5 | atr_period = input(defval=10, type=input.integer, title='Supertrend Period') 6 | ema_len = input(defval=2, type=input.integer, title='Smooth EMA Loopback Period') 7 | long_only = input(defval=false, type=input.bool, title='Long only') 8 | 9 | ///////////////////////////////////// 10 | /// Supertrend fn 11 | supertrend(factor, period) => 12 | hl22 = (ema(high, ema_len) + ema(low, ema_len))/2 13 | cl22 = close //ema(close, ema_len) 14 | Up = hl22 - (factor*atr(period)) 15 | Dn = hl22 + (factor*atr(period)) 16 | TrendUp = 0.0 17 | TrendDn = 0.0 18 | Trend = 0.0 19 | TrendUp := cl22[1]>TrendUp[1] ? max(Up,TrendUp[1]) : Up 20 | TrendDn := cl22[1] TrendDn[1] ? 1: cl22 < TrendUp[1]? -1: nz(Trend[1],1) 22 | Tsl = Trend==1? TrendUp: TrendDn 23 | 24 | [Tsl, Trend, TrendUp, TrendDn] 25 | // 26 | 27 | [tsl, trend, trend_up, trend_dn] = supertrend(atr_factor, atr_period) 28 | lcolor_res = trend == 1 ? color.green : color.red 29 | 30 | ///////////////////////////////////////////////////// 31 | /// Calc position signals 32 | buy = (trend == 1) and (trend[1] != 1) 33 | sell = (trend != 1) and (trend[1] == 1) 34 | 35 | //////////////////////////////////////////////////// 36 | /// Execute trades 37 | strategy.entry("Long", strategy.long, when=buy) 38 | if long_only 39 | strategy.close("Long", when=sell) 40 | else 41 | strategy.entry("Short", strategy.short, when=sell) 42 | // 43 | 44 | //////////////////////////////////////////////////// 45 | /// Plots 46 | plot(tsl, color=lcolor_res, linewidth = 1, title = "SuperTrend") 47 | -------------------------------------------------------------------------------- /pinescript/rsi_mean_reversion_using_single_ema_filter.psc: -------------------------------------------------------------------------------- 1 | //@version=5 2 | strategy("RSI mean reversion with single ema filter", overlay=false) 3 | 4 | ///////////////////////////////////////////////////////////////////// 5 | // Inputs 6 | time_frame = input.timeframe(defval="", title="RSI Time frame") 7 | rsi_len = input.int(defval=2, title="RSI Length") 8 | rsi_threshold = input.int(defval=2, title="RSI Threshold") 9 | ema_time_frame = input.timeframe(defval="", title="EMA Time Frame") 10 | ema_len = input.int(defval=700, title="EMA Length") 11 | 12 | ///////////////////////////////////////////////////////////////////// 13 | // Get signals 14 | rsi_sig = request.security(syminfo.tickerid, time_frame, ta.rsi(close, rsi_len)[1], gaps=barmerge.gaps_on, lookahead=barmerge.lookahead_on) 15 | ema_sig = request.security(syminfo.tickerid, ema_time_frame, ta.ema(close, ema_len)[1], gaps=barmerge.gaps_on, lookahead=barmerge.lookahead_on) 16 | 17 | ///////////////////////////////////////////////////////////////////// 18 | // Positional signals 19 | buy = ta.crossunder(rsi_sig, rsi_threshold) and (close > ema_sig) 20 | sell = ta.crossover(rsi_sig, 100-rsi_threshold) or ta.crossunder(close, ema_sig) 21 | short = ta.crossover(rsi_sig, 100-rsi_threshold) and (close < ema_sig) 22 | cover = ta.crossunder(rsi_sig, rsi_threshold) or ta.crossover(close, ema_sig) 23 | 24 | //////////////////////////////////////////////////////////////////// 25 | // Execute positions 26 | if buy 27 | strategy.entry("L", strategy.long) 28 | if sell 29 | strategy.close("L") 30 | if short 31 | strategy.entry("S", strategy.short) 32 | if cover 33 | strategy.close("S") 34 | // 35 | 36 | ////////////////////////////////////////////////////////////////////// 37 | // Plotting functions 38 | plot(rsi_sig, color=color.blue, title="RSI") 39 | plot(rsi_threshold, color=color.black, title="RSI LThr") 40 | plot(100-rsi_threshold, color=color.black, title="RSI HThr") 41 | -------------------------------------------------------------------------------- /pinescript/bollinger_bands_vol_expansion_trend_trader_with_ema_filter.psc: -------------------------------------------------------------------------------- 1 | //@version=5 2 | strategy("BB Vol expansion trend trader", overlay=false) 3 | 4 | //////////////////////////////////////////////////// 5 | // Inputs 6 | bb_length = input.int(defval=20, title='BB Length') 7 | bb_tf = input.timeframe(defval='', title='BB Timeframe') 8 | bb_lag = input.int(defval=1, title='BB Lookback period for detecting rising/falling') 9 | ema_len = input.int(defval=400, title='EMA Filter Length') 10 | ema_tf = input.timeframe(defval='', title='EMA Time Frame') 11 | 12 | /////////////////////////////////////////////////////// 13 | // Get data from high time frame 14 | get_security(_src, _tf) => 15 | request.security(syminfo.tickerid, _tf, _src[1], gaps=barmerge.gaps_on, lookahead=barmerge.lookahead_on) 16 | // 17 | 18 | ////////////////////////////////////////////////////// 19 | // Base indicator 20 | bb_dev = 2.0 * ta.stdev(close, bb_length) 21 | bb_range = get_security(2 * bb_dev, bb_tf) 22 | 23 | ////////////////////////////////////////////////////// 24 | // Trend filter 25 | ema_sig = get_security(ta.ema(close, ema_len), ema_tf) 26 | 27 | ////////////////////////////////////////////////////// 28 | // Positions 29 | buy = (bb_range > bb_range[bb_lag]) and (close > ema_sig) 30 | sell = (bb_range < bb_range[bb_lag]) or (close < ema_sig) 31 | short = (bb_range > bb_range[bb_lag]) and (close < ema_sig) 32 | cover = (bb_range < bb_range[bb_lag]) or (close > ema_sig) 33 | 34 | ////////////////////////////////////////////////////// 35 | // Execute positions 36 | if buy 37 | strategy.entry("L", strategy.long) 38 | if sell and strategy.position_size > 0 39 | strategy.close("L") 40 | if short 41 | strategy.entry("S", strategy.short) 42 | if cover and strategy.position_size < 0 43 | strategy.close("S") 44 | 45 | /////////////////////////////////////////////////////// 46 | // Plots 47 | plot(bb_range, color=color.green, linewidth=1, title='BB_Range') 48 | -------------------------------------------------------------------------------- /pinescript/supertrend_with_rising_adx_filter.psc: -------------------------------------------------------------------------------- 1 | //@version=2 2 | strategy("Supertrend with rising ADX filter", overlay = true) 3 | 4 | factor = input(title="Supertrend Mult", defval=3, minval=1,maxval = 100) 5 | period = input(title="Supertrend Period", defval=7, minval=1,maxval = 100) 6 | // For ADX 7 | adx_eml = input(title="ADX Ema Length", defval=9) 8 | adx_len = input(title="ADX Length", defval=20) 9 | adx_res = input(title="ADX timeframe", type=resolution, defval="1D") 10 | 11 | // ADX 12 | dirmov(len) => 13 | up = change(high) 14 | down = -change(low) 15 | truerange = rma(tr, len) 16 | plus = fixnan(100 * rma(up > down and up > 0 ? up : 0, len) / truerange) 17 | minus = fixnan(100 * rma(down > up and down > 0 ? down : 0, len) / truerange) 18 | [plus, minus] 19 | 20 | adx(adx_len) => 21 | [plus, minus] = dirmov(adx_len) 22 | sum = plus + minus 23 | adx = 100 * rma(abs(plus - minus) / (sum == 0 ? 1 : sum), adx_len) 24 | [adx, plus, minus] 25 | 26 | [ADX, up, down] = adx(adx_len) 27 | ADX_ema = security(tickerid, adx_res, ema(ADX, adx_eml)) 28 | ADX_new = security(tickerid, adx_res, ADX) 29 | 30 | 31 | // For Supertrend 32 | Up=hl2-(factor*atr(period)) 33 | Dn=hl2+(factor*atr(period)) 34 | 35 | 36 | TrendUp=close[1]>TrendUp[1]? max(Up,TrendUp[1]) : Up 37 | TrendDown=close[1] TrendDown[1] ? 1: close< TrendUp[1]? -1: nz(Trend[1],1) 40 | Tsl = Trend==1? TrendUp: TrendDown 41 | 42 | linecolor = Trend == 1 ? green : red 43 | 44 | buy = (Trend == 1) and (Trend[1] != 1) 45 | sell = (Trend != 1) and (Trend[1] == 1) 46 | 47 | 48 | // Executiom 49 | strategy.entry("L", strategy.long, when=buy and ADX_ema > ADX_new) 50 | strategy.close("L", when=sell) 51 | strategy.entry("S", strategy.short, when=sell and ADX_ema > ADX_new) 52 | strategy.close("S", when=buy) 53 | 54 | plot(Tsl, color = linecolor , style = line , linewidth = 2,title = "SuperTrend") 55 | -------------------------------------------------------------------------------- /pinescript/r1_s1_classic_breakout.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | strategy("PV r1-s1 breakout", overlay=true, slippage=20) 3 | 4 | time_frame = input("D", type=input.resolution) 5 | tolerance = input(defval=0.0, type=input.float) 6 | pivot_type = input(defval="r1-s1", type=input.string, options=["r1-s1", "r2-s2", "h-l"]) 7 | 8 | cpr(time_frame) => 9 | prev_close = security(syminfo.tickerid, time_frame, close[1], lookahead=true) 10 | prev_open = security(syminfo.tickerid, time_frame, open[1], lookahead=true) 11 | prev_high = security(syminfo.tickerid, time_frame, high[1], lookahead=true) 12 | prev_low = security(syminfo.tickerid, time_frame, low[1], lookahead=true) 13 | 14 | pi_level = (prev_high + prev_low + prev_close)/3 15 | bc_level = (prev_high + prev_low)/2 16 | tc_level = (pi_level - bc_level) + pi_level 17 | r1_level = pi_level * 2 - prev_low 18 | s1_level = pi_level * 2 - prev_high 19 | r2_level = (pi_level - s1_level) + r1_level 20 | s2_level = pi_level - (r1_level - s1_level) 21 | 22 | [pi_level, tc_level, bc_level, r1_level, s1_level, r2_level, s2_level, prev_high, prev_low] 23 | // 24 | 25 | [pi_level, tc_level, bc_level, r1_level, s1_level, r2_level, s2_level, hi_level, lo_level] = cpr(time_frame) 26 | 27 | r_level = r1_level 28 | s_level = s1_level 29 | if pivot_type == "r1-s1" 30 | r_level := r1_level 31 | s_level := s1_level 32 | // 33 | if pivot_type == "r2-s2" 34 | r_level := r2_level 35 | s_level := s2_level 36 | // 37 | if pivot_type == "h-l" 38 | r_level := hi_level 39 | s_level := lo_level 40 | // 41 | 42 | buy = crossover(close, r_level + tolerance) 43 | sell = crossunder(close, s_level - tolerance) 44 | 45 | strategy.entry("L", strategy.long, when=buy) 46 | strategy.entry("S", strategy.short, when=sell) 47 | 48 | plot(r_level, linewidth=2, color=color.black, title="R") 49 | plot(s_level, linewidth=2, color=color.blue, title="S") 50 | -------------------------------------------------------------------------------- /pinescript/r1_s1_fibonacci_breakout.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | strategy("R1-S1 Fibonacci breakout", overlay=true) 3 | 4 | time_frame = input("D", type=input.resolution) 5 | tolerance = input(5.0, type=input.float) 6 | is_tol_perc = input(false, type=input.bool) 7 | levels_type = input(defval="r1-s1", title="levels_type", type=input.string, options=["r1-s1", "r2-s2", "r3-s3"]) 8 | 9 | pivot_dmark(time_frame) => 10 | prev_close = security(syminfo.tickerid, time_frame, close[1], lookahead=true) 11 | prev_open = security(syminfo.tickerid, time_frame, open[1], lookahead=true) 12 | prev_high = security(syminfo.tickerid, time_frame, high[1], lookahead=true) 13 | prev_low = security(syminfo.tickerid, time_frame, low[1], lookahead=true) 14 | 15 | PP = (prev_high + prev_low + prev_close)/3 16 | R1 = PP + 0.382*(prev_high - prev_low) 17 | S1 = PP - 0.382*(prev_high - prev_low) 18 | R2 = PP + 0.618*(prev_high - prev_low) 19 | S2 = PP - 0.618*(prev_high - prev_low) 20 | R3 = PP + (prev_high - prev_low) 21 | S3 = PP - (prev_high - prev_low) 22 | 23 | [PP, R1, S1, R2, S2, R3, S3] 24 | // 25 | 26 | [p_level, r1_level, s1_level, r2_level, s2_level, r3_level, s3_level] = pivot_dmark(time_frame) 27 | 28 | r_level = r1_level 29 | s_level = s1_level 30 | if levels_type == "r2-s2" 31 | r_level := r2_level 32 | s_level := s2_level 33 | if levels_type == "r3-s3" 34 | r_level := r3_level 35 | s_level := s3_level 36 | // 37 | 38 | buy = close > r_level + tolerance 39 | sell = close < s_level - tolerance 40 | if is_tol_perc 41 | buy := close > r_level * (1 + tolerance/100.0) 42 | sell := close < s_level * (1 - tolerance/100.0) 43 | // 44 | 45 | strategy.entry("L", strategy.long, when=buy) 46 | strategy.entry("S", strategy.short, when=sell) 47 | 48 | plot(r_level, style=plot.style_circles, linewidth=2, color=color.green, title="R1") 49 | plot(s_level, style=plot.style_circles, linewidth=2, color=color.red, title="S1") 50 | -------------------------------------------------------------------------------- /pinescript/donch_breakout_with_mean_reversion_v1.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | strategy("Donchian channel with mean reversion v1", overlay=true) 3 | 4 | //////////////////////////////////////////////////////////////////////////// 5 | // Inputs 6 | d1_len = input(10, title='Donchian Larger Time Frame Length', type=input.integer) 7 | d2_len = input(10, title='Donchian Smaller Time Frame Length', type=input.integer) 8 | d1_tf = input('1D', title='Larger Time Frame', type=input.resolution) 9 | d2_tf = input('', title='Smaller Time Frame', type=input.resolution) 10 | 11 | /////////////////////////////////////////////////////////////////////////// 12 | // Calculate donchian levels 13 | d1_hi = security(syminfo.tickerid, d1_tf, highest(d1_len)) 14 | d1_lo = security(syminfo.tickerid, d1_tf, lowest(d1_len)) 15 | d2_hi = security(syminfo.tickerid, d2_tf, highest(d2_len)) 16 | d2_lo = security(syminfo.tickerid, d2_tf, lowest(d2_len)) 17 | 18 | //////////////////////////////////////////////////////////////////////////// 19 | // Calculate positions 20 | // This is for normal trend following 21 | buy = crossover(close, d1_hi[1]) 22 | sell = crossunder(close, d2_lo[1]) 23 | short = crossunder(close, d1_lo[1]) 24 | cover = crossover(close, d2_hi[1]) 25 | 26 | // This is for mean reversion 27 | if d2_hi <= d1_hi and d2_lo >= d1_lo 28 | buy := crossunder(close, d2_lo[1]) 29 | short := crossover(close, d2_hi[1]) 30 | sell := short 31 | cover := buy 32 | 33 | //////////////////////////////////////////////////////////////////////////// 34 | // Execute positions 35 | strategy.entry('L', strategy.long, when=buy) 36 | strategy.entry('S', strategy.short, when=short) 37 | strategy.close('L', when=sell) 38 | strategy.close('S', when=cover) 39 | 40 | //////////////////////////////////////////////////////////////////////////// 41 | // Plots 42 | plot(d1_hi, color=color.green, title='DonchL Hi') 43 | plot(d1_lo, color=color.red, title='DonchL Lo') 44 | plot(d2_hi, color=color.navy, title='DonchS Hi') 45 | plot(d2_lo, color=color.orange, title='DonchS Lo') 46 | -------------------------------------------------------------------------------- /pinescript/donch_bas_crossover_with_adx_filter.psc: -------------------------------------------------------------------------------- 1 | //@version=3 2 | strategy('Donch - EMA with ADX filter', overlay=true, slippage=20) 3 | 4 | ch_len = input(title="Donch Channel Length", defval=20) 5 | ema_len = input(title="EMA Length", defval=9) 6 | adx_lim = input(title="ADX Limit", defval=20) 7 | adx_len = input(title="ADX Length", defval=20) 8 | adx_res = input(title="ADX timeframe", type=resolution, defval="1D") 9 | 10 | // ADX 11 | dirmov(len) => 12 | up = change(high) 13 | down = -change(low) 14 | truerange = rma(tr, len) 15 | plus = fixnan(100 * rma(up > down and up > 0 ? up : 0, len) / truerange) 16 | minus = fixnan(100 * rma(down > up and down > 0 ? down : 0, len) / truerange) 17 | [plus, minus] 18 | 19 | adx(adx_len) => 20 | [plus, minus] = dirmov(adx_len) 21 | sum = plus + minus 22 | adx = 100 * rma(abs(plus - minus) / (sum == 0 ? 1 : sum), adx_len) 23 | [adx, plus, minus] 24 | 25 | [ADX, up, down] = adx(adx_len) 26 | ADX_new = security(tickerid, adx_res, ADX) 27 | 28 | // === INPUT BACKTEST RANGE === 29 | FromMonth = input(defval = 6, title = "From Month", minval = 1, maxval = 12) 30 | FromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31) 31 | FromYear = input(defval = 2017, title = "From Year") 32 | 33 | // === FUNCTION EXAMPLE === 34 | start = timestamp(FromYear, FromMonth, FromDay, 00, 00) // backtest start window 35 | finish = time // backtest finish window 36 | window() => time >= start and time <= finish ? true : false // create function "within window of time" 37 | 38 | bas = avg(highest(ch_len), lowest(ch_len)) 39 | bas_e = ema(bas, ema_len) 40 | 41 | buy = crossover(bas, bas_e) 42 | sell = crossunder(bas, bas_e) 43 | 44 | strategy.entry("long", strategy.long, when = window() and buy and ADX_new > adx_lim) 45 | strategy.close("long", when = window() and sell) 46 | strategy.entry("short", strategy.short, when = window() and sell and ADX_new > adx_lim) 47 | strategy.close("short", when = window() and buy) 48 | 49 | plot(bas, color=red) 50 | plot(bas_e, color=green) 51 | -------------------------------------------------------------------------------- /pinescript/ren3_crossover.psc: -------------------------------------------------------------------------------- 1 | //@version=3 2 | strategy(title="Ren3", shorttitle="Ren3", overlay=true) 3 | 4 | box_len = input(1.0, type=float, title="REN3 Box Length", step=1.0) 5 | ema_len_pre = input(2, type=integer, title="REN3 pre EMA Length") 6 | ema_len = input(9, type=integer, title="REN3 post EMA Length") 7 | atr_mult = input(1.0, type=float, title="ATR Multiplier", step=0.1) 8 | pre_smooth = input(true, type=bool, title="Smooth Price before applying REN3") 9 | 10 | // === INPUT BACKTEST RANGE === 11 | FromMonth = input(defval = 6, title = "From Month", minval = 1, maxval = 12) 12 | FromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31) 13 | FromYear = input(defval = 2017, title = "From Year") 14 | 15 | // === FUNCTION EXAMPLE === 16 | start = timestamp(FromYear, FromMonth, FromDay, 00, 00) // backtest start window 17 | finish = time // backtest finish window 18 | window() => time >= start and time <= finish ? true : false // create function "within window of time" 19 | 20 | ren(src, atr_len) => 21 | sig = src 22 | atr_t = atr_len * atr_mult 23 | 24 | if na(sig[1]) 25 | //sig := lowest(low, 20) 26 | sig := src[1] 27 | else 28 | if (src > sig[1] + atr_t) 29 | sig := sig[1] + atr_t 30 | else 31 | if (src < sig[1] - atr_t) 32 | sig := sig[1] - atr_t 33 | else 34 | sig := sig[1] 35 | // 36 | // 37 | // 38 | // 39 | 40 | open_h = (pre_smooth) ? ema(close, ema_len_pre) : close 41 | 42 | //sig2 = ren(close, atr_len, hl_len) 43 | renk_sig = ren(open_h, box_len) 44 | renk_sig_ema = ema(renk_sig, ema_len) 45 | 46 | buy = crossover(renk_sig, renk_sig_ema) 47 | sell = crossunder(renk_sig, renk_sig_ema) 48 | 49 | strategy.entry("L", strategy.long, when = window() and buy) 50 | //strategy.close("L", when = selli) 51 | strategy.entry("S", strategy.short, when = window() and sell) 52 | //strategy.close("S", when = buyi) 53 | 54 | plot(renk_sig, color=red, linewidth=2) 55 | plot(renk_sig_ema, color=blue, linewidth=1) 56 | -------------------------------------------------------------------------------- /pinescript/ren0.psc: -------------------------------------------------------------------------------- 1 | //@version=3 2 | strategy(title="Ren0", shorttitle="Ren0", overlay=true) 3 | 4 | //res = input("15", type=resolution) 5 | atr_len = input(10, type=integer, title="ATR Length") 6 | ema_len = input(10, type=integer, title="REN0 EMA Length") 7 | donch_len = input(10, type=integer, title="Donch Length") 8 | // === INPUT BACKTEST RANGE === 9 | FromMonth = input(defval = 6, title = "From Month", minval = 1, maxval = 12) 10 | FromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31) 11 | FromYear = input(defval = 2017, title = "From Year") 12 | 13 | // === FUNCTION EXAMPLE === 14 | start = timestamp(FromYear, FromMonth, FromDay, 00, 00) // backtest start window 15 | finish = time // backtest finish window 16 | window() => time >= start and time <= finish ? true : false // create function "within window of time" 17 | 18 | ren(src, atr_len) => 19 | sig = src 20 | atr_t = atr(atr_len) 21 | 22 | if na(sig[1]) 23 | //sig := lowest(low, 20) 24 | sig := src[1] 25 | else 26 | if (src > sig[1] + atr_t) 27 | sig := sig[1] + atr_t 28 | else 29 | if (src < sig[1] - atr_t) 30 | sig := sig[1] - atr_t 31 | else 32 | sig := sig[1] 33 | // 34 | // 35 | // 36 | // 37 | 38 | //sig2 = ren(close, atr_len, hl_len) 39 | renk_sig = ren(open, atr_len) 40 | renk_sig_ea = ema(renk_sig, ema_len) 41 | donch_hi = highest(close, donch_len) 42 | donch_lo = lowest(close, donch_len) 43 | 44 | buy = (close > donch_hi[1]) and (renk_sig > renk_sig_ea) 45 | sell = (close < donch_lo[1]) and (renk_sig < renk_sig_ea) 46 | 47 | strategy.entry("L", strategy.long, when = window() and buy) 48 | //strategy.close("L", when = selli) 49 | strategy.entry("S", strategy.short, when = window() and sell) 50 | //strategy.close("S", when = buyi) 51 | 52 | plot(renk_sig, color=red, linewidth=2) 53 | plot(renk_sig_ea, color=green, linewidth=1) 54 | 55 | plot(donch_hi, color=blue, linewidth=1, style=cross) 56 | plot(donch_lo, color=orange, linewidth=1, style=cross) 57 | -------------------------------------------------------------------------------- /pinescript/vidya_crossover.psc: -------------------------------------------------------------------------------- 1 | //@version=3 2 | strategy("vidya crossover", shorttitle = "vidya crossover", overlay=true) 3 | 4 | // === INPUT SMA === 5 | fa_period = input(defval = 9, type = integer, title = "Fast MA", minval = 1, step = 1) 6 | sa_period = input(defval = 4, type = integer, title = "Slow MA", minval = 1, step = 1) 7 | 8 | // === INPUT BACKTEST RANGE === 9 | FromMonth = input(defval = 6, title = "From Month", minval = 1, maxval = 12) 10 | FromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31) 11 | FromYear = input(defval = 2017, title = "From Year") 12 | 13 | // === FUNCTION EXAMPLE === 14 | start = timestamp(FromYear, FromMonth, FromDay, 00, 00) // backtest start window 15 | finish = time // backtest finish window 16 | window() => time >= start and time <= finish ? true : false // create function "within window of time" 17 | 18 | // Chande Momentum Oscillator 19 | f1(m) => m >= 0.0 ? m : 0.0 20 | f2(m) => m >= 0.0 ? 0.0 : -m 21 | vidya(src, length) => 22 | diff = change(src) 23 | upSum = sum(f1(diff), length) 24 | downSum = sum(f2(diff), length) 25 | 26 | cmo = (upSum - downSum) / (upSum + downSum) 27 | 28 | factor = (2 / (length + 1)) 29 | 30 | ind = 0.0 31 | ind := src * factor * abs(cmo) + nz(ind[1]) * (1 - factor * abs(cmo)) 32 | 33 | fast_ma_line = vidya(close, fa_period) 34 | slow_ma_line = vidya(fast_ma_line, sa_period) 35 | 36 | buy = crossover(fast_ma_line, slow_ma_line) 37 | sell = crossunder(fast_ma_line, slow_ma_line) 38 | 39 | // === EXECUTION === 40 | strategy.entry("L", strategy.long, when = window() and buy) // buy long when "within window of time" AND crossover 41 | //strategy.close("L", when = window() and selli) // sell long when "within window of time" AND crossunder 42 | strategy.entry("S", strategy.short, when = window() and sell) 43 | //strategy.close("S", when = window() and buyi) 44 | 45 | plot(fast_ma_line, title = 'FastMA', color = red, linewidth = 1, style = line) // plot FastMA 46 | plot(slow_ma_line, title = 'SlowMA', color = blue, linewidth = 1, style = line) 47 | -------------------------------------------------------------------------------- /pinescript/bollinger_rsi_v1.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | strategy("Bollinger + RSI v1", overlay=true) 3 | 4 | rsi_length = input(defval=6, title='RSI Length', type=input.integer) 5 | rsi_threshold = input(defval=20, title='RSI threshold', type=input.integer, maxval=100, minval=0) 6 | rsi_timeframe = input(defval='1D', title='RSI Timeframe', type=input.resolution) 7 | bb_length = input(defval=200, title='Bollinger Bands Length', type=input.integer) 8 | bb_mult = input(defval=2, title='Bollinger Bands Multiplier', type=input.integer) 9 | bb_timeframe = input(defval='1D', title='Bollinger Bands TimeFrame', type=input.resolution) 10 | long_only = input(defval=true, title='Long only', type=input.bool) 11 | 12 | //////////////////////////////////////////////// 13 | /// Calculate RSI 14 | rsi_oversold = 100-rsi_threshold 15 | rsi_overbought = rsi_threshold 16 | rsi_ind_t = rsi(close, rsi_length) 17 | rsi_ind = security(syminfo.tickerid, rsi_timeframe, rsi_ind_t) 18 | 19 | ///////////////////////////////////////////////// 20 | /// Bollinger Bands 21 | bb_basis = sma(close, bb_length) 22 | bb_dev = bb_mult * stdev(close, bb_length) 23 | bb_upper_t = bb_basis + bb_dev 24 | bb_lower_t = bb_basis - bb_dev 25 | bb_upper = security(syminfo.tickerid, bb_timeframe, bb_upper_t) 26 | bb_lower = security(syminfo.tickerid, bb_timeframe, bb_lower_t) 27 | 28 | //////////////////////////////////////////////////// 29 | /// Define & Execute Positions 30 | buy = crossover(close, bb_lower) and crossover(rsi_ind, rsi_oversold) 31 | short = crossunder(close, bb_upper) and crossunder(rsi_ind, rsi_overbought) 32 | 33 | strategy.entry("L", strategy.long, when=buy) 34 | if long_only 35 | strategy.close("L", when=short) 36 | else 37 | strategy.entry("S", strategy.short, when=short) 38 | // 39 | 40 | //////////////////////////////////////////////////// 41 | /// Plots 42 | plot(bb_upper, color=color.green, title='BB+') 43 | plot(bb_lower, color=color.red, title='BB-') 44 | -------------------------------------------------------------------------------- /afl/legacy/ema_crossover.afl: -------------------------------------------------------------------------------- 1 | _SECTION_BEGIN("EMA Crossover Trading System"); 2 | 3 | SetChartOptions(0,chartShowArrows|chartShowDates); 4 | _N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C )); 5 | 6 | //Initial Parameters 7 | 8 | SetOption("FuturesMode" ,True); 9 | SetPositionSize(1, spsShares); 10 | 11 | //Parameters 12 | MALength1 = optimize("MALength1", Param("MALength1", 14, 1, 100, 1), 1, 100, 1); 13 | MALength2 = optimize("MALength2", Param("MALength2", 21, 1, 100, 1), 1, 100, 1); 14 | slippage = Param("Slippage", 2, 0, 10, 0.1); 15 | 16 | BuyPrice += slippage; 17 | SellPrice -= slippage; 18 | ShortPrice -= slippage; 19 | CoverPrice += slippage; 20 | 21 | //Buy-Sell Logic 22 | Buy = Cross(ema(C, MALength1), ema(C, MALength2)); 23 | Sell = Cross(ema(C, MALength2), ema(C, MALength1)) ; 24 | Buy = ExRem( Buy, Sell ); 25 | Sell = ExRem( Sell, Buy ); 26 | Short = Sell; 27 | Cover = Buy; 28 | 29 | 30 | Plot(Close, "Price", colorWhite, styleCandle); 31 | Plot(ema(C, MALength1), "FastEMA",colorWhite); 32 | Plot(ema(C, MALength2), "SlowEMA",colorBlue); 33 | 34 | /* Plot Buy and Sell Signal Arrows */ 35 | PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40); 36 | PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50); 37 | PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45); 38 | PlotShapes(IIf(Cover, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40); 39 | PlotShapes(IIf(Cover, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50); 40 | PlotShapes(IIf(Cover, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45); 41 | PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=40); 42 | PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50); 43 | PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45); 44 | PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=40); 45 | PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50); 46 | PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45); 47 | 48 | _SECTION_END(); 49 | -------------------------------------------------------------------------------- /pinescript/ren1_heikinashi_crossover.psc: -------------------------------------------------------------------------------- 1 | //@version=3 2 | strategy(title="Ren1 - Heikinashi", shorttitle="Ren1 - Heikinashi", overlay=true) 3 | 4 | //res = input("15", type=resolution) 5 | atr_len = input(10, type=integer, title="ATR Length") 6 | ema_len = input(10, type=integer, title="REN0 EMA Length") 7 | atr_mult = input(1.0, type=float, title="ATR Multiplier", step=0.1) 8 | lag = input(0, type=integer, title="Lag") 9 | 10 | // === INPUT BACKTEST RANGE === 11 | FromMonth = input(defval = 6, title = "From Month", minval = 1, maxval = 12) 12 | FromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31) 13 | FromYear = input(defval = 2017, title = "From Year") 14 | 15 | // === FUNCTION EXAMPLE === 16 | start = timestamp(FromYear, FromMonth, FromDay, 00, 00) // backtest start window 17 | finish = time // backtest finish window 18 | window() => time >= start and time <= finish ? true : false // create function "within window of time" 19 | 20 | ren(src, atr_len) => 21 | sig = src 22 | atr_t = atr(atr_len) * atr_mult 23 | 24 | if na(sig[1]) 25 | //sig := lowest(low, 20) 26 | sig := src[1] 27 | else 28 | if (src > sig[1] + atr_t) 29 | sig := sig[1] + atr_t 30 | else 31 | if (src < sig[1] - atr_t) 32 | sig := sig[1] - atr_t 33 | else 34 | sig := sig[1] 35 | // 36 | // 37 | // 38 | // 39 | 40 | open_h = security(heikinashi(tickerid), period, open) 41 | close_h = security(heikinashi(tickerid), period, close) 42 | high_h = security(heikinashi(tickerid), period, high) 43 | low_h = security(heikinashi(tickerid), period, low) 44 | 45 | //sig2 = ren(close, atr_len, hl_len) 46 | renk_sig = ren(open_h, atr_len) 47 | renk_sig_ea = ema(renk_sig, ema_len) 48 | 49 | buy = (renk_sig[lag] > renk_sig_ea[lag]) 50 | sell = (renk_sig[lag] < renk_sig_ea[lag]) 51 | 52 | strategy.entry("L", strategy.long, when = window() and buy) 53 | //strategy.close("L", when = selli) 54 | strategy.entry("S", strategy.short, when = window() and sell) 55 | //strategy.close("S", when = buyi) 56 | 57 | plot(renk_sig, color=red, linewidth=2) 58 | plot(renk_sig_ea, color=green, linewidth=1) 59 | -------------------------------------------------------------------------------- /pinescript/donch_1day_swing_trader_v1.psc: -------------------------------------------------------------------------------- 1 | //@version=5 2 | strategy("Donchian swing trader v1", overlay=true, process_orders_on_close=true) 3 | 4 | /////////////////////////////////////////////////////// 5 | // Parameters 6 | /////////////////////////////////////////////////////// 7 | donch_period = input.int(defval=20, title='Donchian Time Frame') 8 | donch_stop_thr = input.float(defval=0, title='Donchian Stop Threshold', step=1.0) 9 | donch_tf = input.timeframe(defval='', title='Donchian Time Frame') 10 | allow_short = input.bool(defval=true, title='Allow Short trades') 11 | 12 | /////////////////////////////////////////////////////// 13 | // Get data from high time frame 14 | get_security(_src, _tf) => 15 | request.security(syminfo.tickerid, _tf, _src[1], gaps=barmerge.gaps_on, lookahead=barmerge.lookahead_on) 16 | // 17 | 18 | /////////////////////////////////////////////////////// 19 | // Indicators 20 | donch_hi = get_security(ta.highest(high, donch_period), donch_tf) 21 | donch_lo = get_security(ta.lowest(low, donch_period), donch_tf) 22 | donch_bas = math.avg(donch_hi, donch_lo) 23 | 24 | ////////////////////////////////////////////////////////// 25 | // Position signals 26 | ////////////////////////////////////////////////////////// 27 | buy_signal = ta.crossover(close, donch_bas[1]) or ((close > high[1]) and (close > donch_bas)) 28 | sell_signal = (close < low[1] - donch_stop_thr) or ta.crossunder(close, donch_bas[1]) 29 | short_signal = ta.crossunder(close, donch_bas[1]) or ((close < low[1]) and (close < donch_bas)) 30 | cover_signal = (close > high[1] + donch_stop_thr) or ta.crossover(close, donch_bas[1]) 31 | 32 | ///////////////////////////////////////////////////////// 33 | // Execute positions 34 | ////////////////////////////////////////////////////////// 35 | if buy_signal 36 | strategy.entry("L", strategy.long) 37 | if sell_signal 38 | strategy.close("L") 39 | if allow_short 40 | if short_signal 41 | strategy.entry("S", strategy.short) 42 | if cover_signal 43 | strategy.close("S") 44 | // 45 | // 46 | 47 | ///////////////////////////////////////////////////////// 48 | // Plots 49 | ///////////////////////////////////////////////////////// 50 | plot(donch_bas, "DHI", color.aqua) 51 | -------------------------------------------------------------------------------- /afl/legacy/donch_breakout.afl: -------------------------------------------------------------------------------- 1 | _SECTION_BEGIN("Donchian Breakout"); 2 | SetChartOptions(0,chartShowArrows|chartShowDates); 3 | 4 | SetOption("PriceBoundChecking", False); 5 | 6 | //Parameters 7 | donch_length = optimize("DonchianLength", Param("DonchianLength", 15, 1, 100), 1, 100, 1); 8 | donch_ema_per = optimize("DonchianSmoothLength", Param("DonchianSmoothLength", 1, 10, 1), 1, 10, 1); 9 | slippage = Param("Slippage", 0.15, 0, 10, 0.1); 10 | fut_mode = ParamToggle("Futures Mode", "No|Yes", 1); 11 | 12 | if (fut_mode){ 13 | // Initial parameters 14 | SetOption("FuturesMode" ,True); 15 | SetPositionSize(1, spsShares); 16 | } 17 | 18 | BuyPrice += slippage; 19 | SellPrice -= slippage; 20 | ShortPrice -= slippage; 21 | CoverPrice += slippage; 22 | 23 | // Define indicators 24 | donch_upper = HHV(EMA(Ref(H,-1), donch_ema_per), donch_length); 25 | donch_lower = LLV(EMA(Ref(L,-1), donch_ema_per), donch_length); 26 | 27 | //Buy-Sell Logic 28 | Buy = Close > Ref(donch_upper, -1); 29 | Sell = Close < Ref(donch_lower, -1); 30 | Buy = ExRem(Buy, Sell); 31 | Sell = ExRem(Sell, Buy); 32 | 33 | Short = Sell; 34 | Cover = Buy; 35 | 36 | Plot(Close, "Price", colorWhite, styleCandle); 37 | Plot(donch_upper, "donch_upper", colorGreen); 38 | Plot(donch_lower, "donch_lower",colorRed); 39 | 40 | /* Plot Buy and Sell Signal Arrows */ 41 | PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40); 42 | PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50); 43 | PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45); 44 | PlotShapes(IIf(Cover, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40); 45 | PlotShapes(IIf(Cover, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50); 46 | PlotShapes(IIf(Cover, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45); 47 | PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=40); 48 | PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50); 49 | PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45); 50 | PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=40); 51 | PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50); 52 | PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45); 53 | 54 | _SECTION_END(); 55 | -------------------------------------------------------------------------------- /pinescript/donch_breakout_scalper.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | strategy("Donchin Breakout Scalper", overlay=true, slippage=20) 3 | 4 | ch_len = input(300, title="Donchian Channel Length") 5 | use_close = input(true, title='Use CLose (instead of High Low)', type=input.bool) 6 | resol = input("1D", title='Resolution', type=input.resolution) 7 | stop_loss = input(1.0, title="Stop loss", type=input.float) 8 | use_iday = input(false, title='Use intraday', type=input.bool) 9 | 10 | end_hr = input(15, title='End session hour', type=input.integer) 11 | end_min = input(14, title='End session minutes', type=input.integer) 12 | 13 | ch_hi_s = use_close ? highest(close, ch_len) : highest(high, ch_len) 14 | ch_lo_s = use_close ? lowest(close, ch_len) : lowest(low, ch_len) 15 | ch_hi = security(syminfo.tickerid, resol, ch_hi_s) 16 | ch_lo = security(syminfo.tickerid, resol, ch_lo_s) 17 | 18 | // check if this candle is close of session 19 | chk_close_time(hr, min) => 20 | time_sig = (hour[0] == hr) and (minute[0] > min and minute[1] < min) 21 | time_sig 22 | // 23 | 24 | // chechk if this candle doesn't fall in close session 25 | chk_not_close_time(hr) => 26 | time_ncs = (hour[0] < hr) 27 | [time_ncs] 28 | // 29 | 30 | stop_l = (use_close) ? (close - stop_loss) : (low - stop_loss) 31 | stop_s = (use_close) ? (close + stop_loss) : (high + stop_loss) 32 | //stop_l = ch_hi - stop_loss 33 | //stop_s = ch_lo + stop_loss 34 | 35 | 36 | buy = use_iday ? (crossover(close, ch_hi[1]) and (hour < end_hr)) : crossover(close, ch_hi[1]) 37 | sell = use_iday ? (crossunder(close, stop_l[1]) or chk_close_time(end_hr, end_min)) : crossunder(close, stop_l[1]) 38 | short = use_iday ? (crossunder(close, ch_lo[1]) and (hour < end_hr)) : crossunder(close, ch_lo[1]) 39 | cover = use_iday ? (crossover(close, stop_s[1]) or chk_close_time(end_hr, end_min)) : crossover(close, stop_s[1]) 40 | 41 | strategy.entry("L", strategy.long, when=buy) 42 | strategy.close("L", when=sell) 43 | strategy.entry("S", strategy.short, when=short) 44 | strategy.close("S", when=cover) 45 | 46 | plot(ch_hi, color=color.green, title="HI") 47 | plot(ch_lo, color=color.red, title="LO") 48 | //plot(stop_l, color=color.olive, title="StopHI") 49 | //plot(stop_s, color=color.blue, title="StopLO") 50 | -------------------------------------------------------------------------------- /pinescript/ind_ehlers_empirical_mode_decomposition.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | study(title="Ehlers Bandpass Filter v1 - Empirical Mode decomposition", overlay=false) 3 | 4 | /////////////////////////////////////////////////// 5 | // Inputs 6 | length = input(defval=20, type=input.integer, title="Period") 7 | delta = input(defval=0.1, type=input.float, title="Delta", step=0.1) 8 | pv_sma_len = input(defval=50, type=input.integer, title='Peak Valley smooth length') 9 | thr_fract = input(defval=0.1, type=input.float, title='Threshold - Fraction of Peak Valley averages') 10 | time_frame = input(defval='1D', type=input.resolution, title="Time Frame") 11 | src = hl2 12 | 13 | /////////////////////////////////////////////////// 14 | // Generate signals 15 | // Band pass filter 16 | BandPassv1(src, length, delta) => 17 | beta = cos(3.14*(360/length)/180) 18 | gamma = 1/cos(3.14*(720*delta/length)/180) 19 | alpha = gamma-sqrt(gamma*gamma-1) 20 | bpass = 0.0 21 | bpass := 0.5*(1-alpha)*(src-src[2]) + beta*(1+alpha)*nz(bpass[1]) - alpha*nz(bpass[2]) 22 | trend = sma(bpass, 2*length) 23 | [bpass, trend] 24 | 25 | [band_pass_t, trend_t] = BandPassv1(src, length, delta) 26 | band_pass = security(syminfo.tickerid, time_frame, band_pass_t) 27 | trend = security(syminfo.tickerid, time_frame, trend_t) 28 | 29 | // Get peaks and valleys and trigger thresholds 30 | peaks = band_pass 31 | valleys = band_pass 32 | if band_pass[1] > band_pass and band_pass[1] > band_pass[2] 33 | peaks := band_pass[1] 34 | else 35 | peaks := peaks[1] 36 | // 37 | if band_pass[1] < band_pass and band_pass[1] < band_pass[2] 38 | valleys := band_pass[1] 39 | else 40 | valleys := valleys[1] 41 | // 42 | peaks_smooth = sma(peaks, pv_sma_len) 43 | valleys_smooth = sma(valleys, pv_sma_len) 44 | thr_high = thr_fract*peaks_smooth 45 | thr_lower = thr_fract*valleys_smooth 46 | 47 | ///////////////////////////////////////////////////// 48 | // Plots 49 | plot(band_pass, color=color.blue, title='BPASS') 50 | plot(trend, color=color.red, title='Trend') 51 | plot(thr_high, color=color.green, title='Upper Threshold') 52 | plot(thr_lower, color=color.olive, title='Lower Threshold') 53 | -------------------------------------------------------------------------------- /pinescript/mean_reversion_v1.psc: -------------------------------------------------------------------------------- 1 | //@version=5 2 | strategy("Mean reversion v1", overlay=false, default_qty_type=strategy.percent_of_equity, default_qty_value=100) 3 | 4 | //////////////////////////////////////////////////////////////////////// 5 | // Inputs 6 | period = input.int(defval=14, title='Loopback period') 7 | threshold = input.int(defval=14, title='Threshold') 8 | tperiod = input.int(defval=100, title='Trend EMA') 9 | tfilt_en = input.bool(defval=true, title='Enable Trend Filter') 10 | en_long = input.bool(defval=true, title='Enable Long') 11 | en_short = input.bool(defval=false, title='Enable Short') 12 | 13 | ////////////////////////////////////////////////////////////////////// 14 | // Utility functions 15 | rescale_fn(p_, p_length_) => 16 | (p_ - ta.lowest(p_, p_length_))/(ta.highest(p_, p_length_) - ta.lowest(p_, p_length_)) * 100 17 | // 18 | 19 | ma_fn(p_, p_length_) => 20 | ta.sma(p_, p_length_) 21 | // 22 | 23 | dpo_fn(p_, p_length_) => 24 | rescale_fn((p_ - ma_fn(p_, p_length_))/ma_fn(p_, p_length_), p_length_) 25 | // 26 | 27 | ///////////////////////////////////////////////////////////////////// 28 | // Get signals 29 | 30 | // Main signal 31 | mrev_sig = dpo_fn(close, period) 32 | 33 | // Trend Filter signals 34 | trend_ma = ta.ema(close, tperiod) 35 | trend_up = tfilt_en ? (close > trend_ma) : true 36 | trend_dn = tfilt_en ? (close < trend_ma) : true 37 | 38 | thr_hi = 100 - threshold 39 | thr_lo = threshold 40 | 41 | // Execution signals 42 | buy_sig = trend_up and (mrev_sig < thr_lo) 43 | sell_sig = (mrev_sig > thr_hi) 44 | short_sig = trend_dn and (mrev_sig > thr_hi) 45 | cover_sig = (mrev_sig < thr_lo) 46 | 47 | ///////////////////////////////////////////////////////////////////// 48 | // Execute positions 49 | if en_long 50 | if buy_sig 51 | strategy.entry("Long", strategy.long) 52 | // 53 | if sell_sig 54 | strategy.close("Long") 55 | // 56 | // 57 | if en_short 58 | if short_sig 59 | strategy.entry("Short", strategy.short) 60 | // 61 | if cover_sig 62 | strategy.close("Short") 63 | // 64 | // 65 | 66 | //////////////////////////////////////////////////////////////// 67 | // Plots 68 | plot(mrev_sig, color=color.blue, title='signal') 69 | hline(thr_lo, title='Threshold Low') 70 | hline(thr_hi, title='Threshold High') 71 | -------------------------------------------------------------------------------- /afl/legacy/donch_ema_crossover.afl: -------------------------------------------------------------------------------- 1 | _SECTION_BEGIN("Donchian EMA Crossover"); 2 | SetChartOptions(0,chartShowArrows|chartShowDates); 3 | 4 | SetOption("PriceBoundChecking", False); 5 | 6 | //Parameters 7 | donch_length = optimize("DonchianLength", Param("DonchianLength", 15, 1, 100), 1, 100, 1); 8 | donch_ema_per = optimize("DonchianEMALength", Param("DonchianEMALength", 9, 1, 50), 1, 50, 1); 9 | slippage = Param("Slippage", 0.15, 0, 10, 0.1); 10 | fut_mode = ParamToggle("Futures Mode", "No|Yes", 1); 11 | 12 | if (fut_mode){ 13 | // Initial parameters 14 | SetOption("FuturesMode" ,True); 15 | SetPositionSize(1, spsShares); 16 | } 17 | 18 | BuyPrice += slippage; 19 | SellPrice -= slippage; 20 | ShortPrice -= slippage; 21 | CoverPrice += slippage; 22 | 23 | // Define indicators 24 | donch_upper = HHV(Ref(H,-1), donch_length); 25 | donch_lower = LLV(Ref(L,-1), donch_length); 26 | donch_middle = (donch_upper + donch_lower)/2; 27 | donch_ema = EMA(donch_middle, donch_ema_per); 28 | 29 | //Buy-Sell Logic 30 | Buy = Cross(donch_middle, donch_ema); 31 | Sell = Cross(donch_ema, donch_middle); 32 | 33 | Short = Cross(donch_ema, donch_middle); 34 | Cover = Cross(donch_middle, donch_ema); 35 | 36 | Plot(Close, "Price", colorWhite, styleCandle); 37 | Plot(donch_middle, "donch_middle", colorWhite); 38 | Plot(donch_ema, "donch_ema",colorBlue); 39 | 40 | /* Plot Buy and Sell Signal Arrows */ 41 | PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40); 42 | PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50); 43 | PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45); 44 | PlotShapes(IIf(Cover, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40); 45 | PlotShapes(IIf(Cover, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50); 46 | PlotShapes(IIf(Cover, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45); 47 | PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=40); 48 | PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50); 49 | PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45); 50 | PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=40); 51 | PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50); 52 | PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45); 53 | 54 | _SECTION_END(); 55 | -------------------------------------------------------------------------------- /pinescript/ren1_heikinashi_crossover_with_multi_timeframe_atr.psc: -------------------------------------------------------------------------------- 1 | //@version=3 2 | strategy(title="Ren1 - Heikinashi with multi-timeframe ATR", shorttitle="Ren1 - Heikinashi with MTF ATR", overlay=true, slippage=20) 3 | 4 | atr_len = input(10, type=integer, title="ATR Length") 5 | ema_len = input(10, type=integer, title="REN0 EMA Length") 6 | atr_mult = input(1.0, type=float, title="ATR Multiplier", step=1.0) 7 | lag = input(0, type=integer, title="Lag") 8 | atr_tf = input('1', type=resolution, title='ATR timeframe') 9 | 10 | // === INPUT BACKTEST RANGE === 11 | FromMonth = input(defval = 6, title = "From Month", minval = 1, maxval = 12) 12 | FromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31) 13 | FromYear = input(defval = 2017, title = "From Year") 14 | 15 | // === FUNCTION EXAMPLE === 16 | start = timestamp(FromYear, FromMonth, FromDay, 00, 00) // backtest start window 17 | finish = time // backtest finish window 18 | window() => time >= start and time <= finish ? true : false // create function "within window of time" 19 | 20 | ren(src, atr_len, atr_tf) => 21 | sig = src 22 | atr_t = security(tickerid, atr_tf, atr(atr_len)) * atr_mult 23 | 24 | if na(sig[1]) 25 | //sig := lowest(low, 20) 26 | sig := src[1] 27 | else 28 | if (src > sig[1] + atr_t) 29 | sig := sig[1] + atr_t 30 | else 31 | if (src < sig[1] - atr_t) 32 | sig := sig[1] - atr_t 33 | else 34 | sig := sig[1] 35 | // 36 | // 37 | // 38 | // 39 | 40 | open_h = security(heikinashi(tickerid), period, open) 41 | close_h = security(heikinashi(tickerid), period, close) 42 | high_h = security(heikinashi(tickerid), period, high) 43 | low_h = security(heikinashi(tickerid), period, low) 44 | 45 | //sig2 = ren(close, atr_len, hl_len) 46 | renk_sig = ren(open_h, atr_len, atr_tf) 47 | renk_sig_ea = ema(renk_sig, ema_len) 48 | 49 | buy = (renk_sig[lag] > renk_sig_ea[lag]) 50 | sell = (renk_sig[lag] < renk_sig_ea[lag]) 51 | 52 | strategy.entry("L", strategy.long, when = window() and buy) 53 | //strategy.close("L", when = selli) 54 | strategy.entry("S", strategy.short, when = window() and sell) 55 | //strategy.close("S", when = buyi) 56 | 57 | plot(renk_sig, color=red, linewidth=2) 58 | plot(renk_sig_ea, color=green, linewidth=1) 59 | -------------------------------------------------------------------------------- /pinescript/donch_breakout_v1.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | strategy("Donchian Breakout v1", overlay=true) 3 | 4 | //////////////////////////////////// 5 | // Inputs 6 | donch_len = input(title="Donchian length", defval=2, minval=1) 7 | donch_res = input(title="Dinchian Resolution", defval='', type=input.resolution) 8 | tolerance = input(defval=0.0, type=input.float) 9 | stop_loss = input(defval=10.0, title="Stop loss", type=input.float) 10 | use_sperc = input(defval=true, title='Stop loss & tolerance are in %centage(s)', type=input.bool) 11 | use_tstop = input(defval=true, title='Use trailing stop', type=input.bool) 12 | long_only = input(defval=true, title='Long only', type=input.bool) 13 | 14 | ///////////////////////////////////// 15 | /// Calculate stops 16 | stop_l = use_sperc ? strategy.position_avg_price*(1-stop_loss/100.0) : (strategy.position_avg_price-stop_loss) 17 | stop_s = use_sperc ? strategy.position_avg_price*(1+stop_loss/100.0) : (strategy.position_avg_price+stop_loss) 18 | if use_tstop 19 | stop_l := use_sperc ? close*(1-stop_loss/100.0) : (close-stop_loss) 20 | stop_s := use_sperc ? close*(1+stop_loss/100.0) : (close+stop_loss) 21 | // 22 | 23 | 24 | donch_hi = highest(close, donch_len) 25 | donch_lo = lowest(close, donch_len) 26 | 27 | donch_hi_tf = security(syminfo.tickerid, donch_res, donch_hi) 28 | donch_lo_tf = security(syminfo.tickerid, donch_res, donch_lo) 29 | 30 | ////////////////////////////////////// 31 | /// Calculate actual levels after adjusting for tolerance 32 | tolbu = use_sperc ? donch_hi_tf*(1+tolerance/100.0) : (donch_hi_tf+tolerance) 33 | tolbl = use_sperc ? donch_lo_tf*(1-tolerance/100.0) : (donch_lo_tf-tolerance) 34 | 35 | buy = crossover(close, tolbu[1]) 36 | sell = crossunder(close, stop_l[1]) //or crossunder(close, tolbl[1]) 37 | short = crossunder(close, tolbl[1]) 38 | cover = crossover(close, stop_s[1]) //or crossover(close, tolbu[1]) 39 | 40 | ///////////////////////////////////////// 41 | /// Execute signals 42 | strategy.entry("L", strategy.long, when=buy) 43 | strategy.close("L", when=sell) 44 | if long_only == false 45 | strategy.entry("S", strategy.short, when=short) 46 | strategy.close("S", when=cover) 47 | // 48 | 49 | /////////////////////////////////////////// 50 | // Plot signals 51 | plot(donch_hi_tf, color=color.green, title='Donch HI') 52 | plot(donch_lo_tf, color=color.red, title='Donch LO') 53 | -------------------------------------------------------------------------------- /pinescript/bollinger_bands_mean_reversion_mtr_v1.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | strategy("Bollinger Bands Mean Reversion MTR", overlay=true, slippage=20) 3 | 4 | //////////////////////////////////////////////////////////////////////// 5 | // Inputs 6 | bb_length = input(title="BBLength", defval=20, minval=1) 7 | bb_mult = input(title="BBMult", defval=2.0, minval=0.001, maxval=50, step=0.1) 8 | bb_tf = input(title="BBTimeFrame", defval='1D', type=input.resolution) 9 | stop_loss = input(defval=1.0, title="Stop loss", type=input.float) 10 | use_sperc = input(defval=false, title='Stop loss & tolerance are in %centage(s)', type=input.bool) 11 | use_tstop = input(defval=true, title='Use trailing stop', type=input.bool) 12 | 13 | 14 | /////////////////////////////////////////////////////////////////////////// 15 | // Indicator calculation 16 | bb_basis_i = sma(close, bb_length) 17 | bb_dev_i = bb_mult * stdev(close, bb_length) 18 | bb_upper = security(syminfo.tickerid, bb_tf, bb_basis_i + bb_dev_i) 19 | bb_lower = security(syminfo.tickerid, bb_tf, bb_basis_i - bb_dev_i) 20 | bb_basis = security(syminfo.tickerid, bb_tf, bb_basis_i) 21 | 22 | ////////////////////////////////////////////////////////////////////////////// 23 | // Stop loss calculation 24 | stop_l = use_sperc ? strategy.position_avg_price*(1-stop_loss/100.0) : (strategy.position_avg_price-stop_loss) 25 | stop_s = use_sperc ? strategy.position_avg_price*(1+stop_loss/100.0) : (strategy.position_avg_price+stop_loss) 26 | if use_tstop 27 | stop_l := use_sperc ? close*(1-stop_loss/100.0) : (close - stop_loss) 28 | stop_s := use_sperc ? close*(1+stop_loss/100.0) : (close + stop_loss) 29 | 30 | /////////////////////////////////////////////////////////////////////// 31 | // Execution signals 32 | buy = crossover(close, bb_lower) 33 | sell = crossunder(close, bb_upper) or crossunder(close, stop_l[1]) 34 | short = crossunder(close, bb_upper) 35 | cover = crossover(close, bb_lower) or crossover(close, stop_s[1]) 36 | 37 | strategy.entry("Long", strategy.long, when=buy) 38 | strategy.entry("Short", strategy.short, when=short) 39 | strategy.close("Long", when=sell) 40 | strategy.close("Short", when=cover) 41 | 42 | ///////////////////////////////////////////////////////////////// 43 | // Plots 44 | plot(bb_basis, title="BB-middle", color=color.red) 45 | plot(bb_upper, title="BB-upper", color=color.olive) 46 | plot(bb_lower, title="BB-lower", color=color.green) 47 | -------------------------------------------------------------------------------- /pinescript/bollinger_bands_mean_reversion_with_ema_filter.psc: -------------------------------------------------------------------------------- 1 | //@version=5 2 | strategy("BB mean reversion with ema filter", overlay=true) 3 | 4 | //////////////////////////////////////////////////// 5 | // Inputs 6 | bb_length = input.int(defval=20, title='BB Length') 7 | bb_mult = input.float(defval=2.0, title='BB Multiplier') 8 | bb_tf = input.timeframe(defval='', title='BB Timeframe') 9 | ema_len = input.int(defval=400, title='EMA Filter Length') 10 | ema_tf = input.timeframe(defval='', title='EMA Time Frame') 11 | exit_mode = input.string(defval='Major', title='Trade exit mode', options=['Major', 'Minor']) 12 | 13 | /////////////////////////////////////////////////////// 14 | // Get data from high time frame 15 | get_security(_src, _tf) => 16 | request.security(syminfo.tickerid, _tf, _src[1], gaps=barmerge.gaps_on, lookahead=barmerge.lookahead_on) 17 | // 18 | 19 | ////////////////////////////////////////////////////// 20 | // Base indicator 21 | bb_dev = bb_mult * ta.stdev(close, bb_length) 22 | bb_bas = ta.sma(close, bb_length) 23 | bb_up = bb_bas + bb_dev 24 | bb_dn = bb_bas - bb_dev 25 | bb_up_sig = get_security(bb_up, bb_tf) 26 | bb_dn_sig = get_security(bb_dn, bb_tf) 27 | 28 | ////////////////////////////////////////////////////// 29 | // Trend filter 30 | ema_sig = get_security(ta.ema(close, ema_len), ema_tf) 31 | 32 | ////////////////////////////////////////////////////// 33 | // Positions 34 | buy = ta.crossunder(close, bb_dn_sig) and (close > ema_sig) 35 | sell = ta.crossunder(close, bb_up_sig) or (close < ema_sig) 36 | short = ta.crossover(close, bb_up_sig) and (close < ema_sig) 37 | cover = ta.crossover(close, bb_dn_sig) or (close > ema_sig) 38 | 39 | if exit_mode == 'Minor' 40 | sell := ta.crossover(close, bb_up_sig) or (close < ema_sig) 41 | cover := ta.crossunder(close, bb_dn_sig) or (close > ema_sig) 42 | // 43 | 44 | ////////////////////////////////////////////////////// 45 | // Execute positions 46 | if buy 47 | strategy.entry("L", strategy.long) 48 | if sell and strategy.position_size > 0 49 | strategy.close("L") 50 | if short 51 | strategy.entry("S", strategy.short) 52 | if cover and strategy.position_size < 0 53 | strategy.close("S") 54 | 55 | /////////////////////////////////////////////////////// 56 | // Plots 57 | plot(bb_up_sig, color=color.green, linewidth=1, title='BB_Up') 58 | plot(bb_dn_sig, color=color.red, linewidth=1, title='BB_Dn') 59 | plot(ema_sig, color=color.black, linewidth=1, title='EMA') 60 | -------------------------------------------------------------------------------- /pinescript/ind_pivot_points_flexible.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | study("Pivot Points Flexible", overlay=true) 3 | 4 | time_frame_m = input(defval='1D', title="Resolution", options=['1m', '5m', '10m', '15m', '30m', '45m', '1h', '2h', '4h', '1D', '2D', '4D', '1W', '2W', '1M', '2M', '6M']) 5 | 6 | get_time_frame(tf) => 7 | (tf == '1m') ? "1" 8 | : (tf == '5m') ? "5" 9 | : (tf == '10m') ? "10" 10 | : (tf == '15m') ? "15" 11 | : (tf == '30m') ? "30" 12 | : (tf == '45m') ? "45" 13 | : (tf == '1h') ? "60" 14 | : (tf == '2h') ? "120" 15 | : (tf == '4h') ? "240" 16 | : (tf == '1D') ? "D" 17 | : (tf == '2D') ? "2D" 18 | : (tf == '4D') ? "4D" 19 | : (tf == '1W') ? "W" 20 | : (tf == '2W') ? "2W" 21 | : (tf == '1M') ? "M" 22 | : (tf == '2M') ? "2M" 23 | : (tf == '6M') ? "6M" 24 | : "wrong resolution" 25 | // 26 | time_frame = get_time_frame(time_frame_m) 27 | 28 | 29 | cpr(time_frame) => 30 | prev_close = security(syminfo.tickerid, time_frame, close[1], lookahead=true) 31 | prev_open = security(syminfo.tickerid, time_frame, open[1], lookahead=true) 32 | prev_high = security(syminfo.tickerid, time_frame, high[1], lookahead=true) 33 | prev_low = security(syminfo.tickerid, time_frame, low[1], lookahead=true) 34 | 35 | pi_level = (prev_high + prev_low + prev_close)/3 36 | bc_level = (prev_high + prev_low)/2 37 | tc_level = (pi_level - bc_level) + pi_level 38 | r1_level = pi_level * 2 - prev_low 39 | s1_level = pi_level * 2 - prev_high 40 | r2_level = (pi_level - s1_level) + r1_level 41 | s2_level = pi_level - (r1_level - s1_level) 42 | 43 | [pi_level, tc_level, bc_level, r1_level, s1_level, r2_level, s2_level] 44 | // 45 | 46 | [pi_level, tc_level, bc_level, r1_level, s1_level, r2_level, s2_level] = cpr(time_frame) 47 | 48 | plot(pi_level, style=plot.style_circles, linewidth=2, color=color.black, title="PI") 49 | plot(bc_level, style=plot.style_circles, linewidth=2, color=color.blue, title="BC") 50 | plot(tc_level, style=plot.style_circles, linewidth=2, color=color.blue, title="TC") 51 | plot(r1_level, style=plot.style_circles, linewidth=2, color=color.green, title="R1") 52 | plot(s1_level, style=plot.style_circles, linewidth=2, color=color.red, title="S1") 53 | plot(r2_level, style=plot.style_circles, linewidth=1, color=color.orange, title="R2") 54 | plot(s2_level, style=plot.style_circles, linewidth=1, color=color.maroon, title="S2") 55 | -------------------------------------------------------------------------------- /pinescript/pivot_levels_breakout_v4.psc: -------------------------------------------------------------------------------- 1 | //@version=5 2 | strategy("Pivot Levels v2 (Using ol2 as source) - close cross", overlay=true) 3 | 4 | //////////////////////////////////////////////////////////////////// 5 | // Inputs 6 | pivot_bars = input.int(defval=2, title='Pivot Bars Length') 7 | pivot_tframe = input.timeframe(defval='1D', title='Pivot Time Frame') 8 | pivot_donch_len = input.int(defval=5, title='Donchian smooth period for pivot levels') 9 | cross_tolerance = input.int(defval=0, title='Tolerance value') 10 | allow_shorts = input.bool(defval=true, title='Allow Short trades ?') 11 | 12 | ////////////////////////////////////////////////////////////////// 13 | // Misc functions 14 | /////////////////////////////////////////////////////// 15 | // Get data from high time frame 16 | get_security(_src, _tf) => 17 | request.security(syminfo.tickerid, _tf, _src[1], gaps=barmerge.gaps_on, lookahead=barmerge.lookahead_on) 18 | // 19 | 20 | // Get Pivots 21 | pivots_ol2(_pivot_bars, _pivot_tf) => 22 | phigh_t = ta.pivothigh(math.avg(open, close), _pivot_bars, _pivot_bars) 23 | plow_t = ta.pivotlow(math.avg(open, close), _pivot_bars, _pivot_bars) 24 | phigh = phigh_t 25 | plow = plow_t 26 | phigh_tf = get_security(phigh_t, _pivot_tf) 27 | plow_tf = get_security(plow_t, _pivot_tf) 28 | phigh_san = phigh_tf 29 | plow_san = plow_tf 30 | phigh_san := nz(phigh_tf[1]) ? phigh_tf[1] : phigh_san[1] 31 | plow_san := nz(plow_tf[1]) ? plow_tf[1] : plow_san[1] 32 | [phigh_san, plow_san] 33 | // 34 | 35 | //////////////////////////////////////////////////////////////////// 36 | // Indicators 37 | [phigh_san_, plow_san_] = pivots_ol2(pivot_bars, pivot_tframe) 38 | phigh_san = ta.highest(phigh_san_, pivot_donch_len) 39 | plow_san = ta.lowest(plow_san_, pivot_donch_len) 40 | 41 | /////////////////////////////////////////////////////////////////// 42 | // Signals 43 | buy_sig = ta.crossover(close, phigh_san + cross_tolerance) 44 | sell_sig = ta.crossunder(close, plow_san - cross_tolerance) 45 | 46 | if buy_sig 47 | strategy.entry("L", strategy.long) 48 | if sell_sig 49 | if allow_shorts 50 | strategy.entry("S", strategy.short) 51 | else 52 | strategy.close("L") 53 | // 54 | // 55 | 56 | ///////////////////////////////////////////////////////////////////// 57 | // Plots 58 | plot(phigh_san, color=color.green, title='P+') 59 | plot(plow_san, color=color.red, title='P-') 60 | -------------------------------------------------------------------------------- /pinescript/supertrend_hl_smoothing_with_rising_adx_filter.psc: -------------------------------------------------------------------------------- 1 | //@version=2 2 | strategy("Supertrend with hl smoothing and rising ADX filter", "Supertrend with hl smoothing and rising ADX filter", overlay = true) 3 | 4 | factor = input(title="Factor", defval=4, minval=1, maxval = 1000) 5 | period = input(title="Period", defval=10, minval=1, maxval = 1000) 6 | ema_len = input(title="Smooth EMA Length", defval=2, minval=1, maxval=1000) 7 | //long_only = input(title="Long only", false, type=bool) 8 | // For ADX 9 | adx_eml = input(title="ADX Ema Length", defval=9) 10 | adx_len = input(title="ADX Length", defval=20) 11 | adx_res = input(title="ADX timeframe", type=resolution, defval="1D") 12 | 13 | // ADX 14 | dirmov(len) => 15 | up = change(high) 16 | down = -change(low) 17 | truerange = rma(tr, len) 18 | plus = fixnan(100 * rma(up > down and up > 0 ? up : 0, len) / truerange) 19 | minus = fixnan(100 * rma(down > up and down > 0 ? down : 0, len) / truerange) 20 | [plus, minus] 21 | 22 | adx(adx_len) => 23 | [plus, minus] = dirmov(adx_len) 24 | sum = plus + minus 25 | adx = 100 * rma(abs(plus - minus) / (sum == 0 ? 1 : sum), adx_len) 26 | [adx, plus, minus] 27 | 28 | [ADX, up, down] = adx(adx_len) 29 | ADX_ema = security(tickerid, adx_res, ema(ADX, adx_eml)) 30 | ADX_new = security(tickerid, adx_res, ADX) 31 | 32 | // Supertrend 33 | supertrend() => 34 | hl22 = (ema(high, ema_len) + ema(low, ema_len))/2 35 | cl22 = close //ema(close, ema_len) 36 | 37 | Up =hl22 - (factor*atr(period)) 38 | Dn =hl22 + (factor*atr(period)) 39 | 40 | TrendUp = cl22[1]>TrendUp[1]? max(Up,TrendUp[1]) : Up 41 | TrendDown = cl22[1] TrendDown[1] ? 1: cl22 < TrendUp[1]? -1: nz(Trend[1],1) 43 | Tsl = Trend==1? TrendUp: TrendDown 44 | 45 | [Tsl, Trend] 46 | // 47 | 48 | [Tsl, Trend] = supertrend() 49 | 50 | lcolor_res = Trend == 1 ? green : red 51 | 52 | //buy = (Trend == 1) and (Trend[1] != 1) 53 | //sell = (Trend != 1) and (Trend[1] == 1) 54 | buy_tr = (Trend == 1) 55 | sell_tr = (Trend != 1) 56 | 57 | 58 | // Executiom 59 | strategy.entry("L", strategy.long, when=buy_tr and ADX_new > ADX_ema) 60 | strategy.close("L", when=sell_tr) 61 | strategy.entry("S", strategy.short, when=sell_tr and ADX_new > ADX_ema) 62 | strategy.close("S", when=buy_tr) 63 | 64 | plot(Tsl, color = lcolor_res , style = linebr , linewidth = 1,title = "SuperTrend") 65 | -------------------------------------------------------------------------------- /pinescript/linreg_donch_regime_filter_with_ema_trend_filter.psc: -------------------------------------------------------------------------------- 1 | //@version=5 2 | strategy("Linreg and Donch Regime filter with Moving average trend filter", overlay=false, default_qty_type=strategy.percent_of_equity, default_qty_value=100) 3 | 4 | //////////////////////////////////////////////////////////////////// 5 | // Inputs 6 | donch_len = input.int(defval=20, title="Regime Filter - Donchian Channel Length") 7 | linreg_len = input.int(defval=20, title="Regime Filter - Linear Regression Length") 8 | ma_len = input.int(defval=200, title="Trend Filter - Moving Average Length") 9 | ma_tf = input.timeframe(defval="", title="Moving Average Time Frame") 10 | reg_tf = input.timeframe(defval="", title="Regime Filter Time Frame") 11 | en_short = input.bool(defval=false, title="Enable Short Positions ?") 12 | 13 | /////////////////////////////////////////////////////////////////// 14 | // Signals 15 | lreg = ta.linreg(close, linreg_len, 0) 16 | lreg_pct = (lreg - ta.lowest(lreg, linreg_len))/(ta.highest(lreg, linreg_len) - ta.lowest(lreg, linreg_len)) * 100 17 | 18 | donch_hi = ta.highest(high, donch_len) 19 | donch_low = ta.lowest(low, donch_len) 20 | donch_mid = math.avg(donch_hi, donch_low) 21 | donch_sum_ind = math.sum(close - donch_mid, donch_len) 22 | donch_sum_pct = (donch_sum_ind - ta.lowest(donch_sum_ind, donch_len))/(ta.highest(donch_sum_ind, donch_len) - ta.lowest(donch_sum_ind, donch_len)) * 100 23 | 24 | regime_sig = (donch_sum_pct == 100 or lreg_pct == 100) ? 100 : (donch_sum_pct == 0 or lreg_pct == 0) ? 0 : 50 25 | //regime_sig = (lreg_pct == 100) ? 100 : (lreg_pct == 0) ? 0 : lreg_pct 26 | regime_sig_tf = request.security(syminfo.tickerid, reg_tf, regime_sig) 27 | ma_sig = ta.ema(close, ma_len) 28 | ma_sig_tf = request.security(syminfo.tickerid, ma_tf, ma_sig) 29 | 30 | buy_sig = (close > ma_sig_tf) and (regime_sig_tf == 100) 31 | sell_sig = ((regime_sig_tf < 100) and (regime_sig_tf[1] < 100)) 32 | short_sig = (close < ma_sig_tf) and (regime_sig_tf == 0) 33 | cover_sig = ((regime_sig_tf > 0) and (regime_sig_tf[1] > 100)) 34 | 35 | ///////////////////////////////////////////////////////////////////// 36 | // Take positions 37 | if buy_sig 38 | strategy.entry("L", strategy.long) 39 | // 40 | if sell_sig 41 | strategy.close("L") 42 | // 43 | if en_short 44 | if short_sig 45 | strategy.entry("S", strategy.short) 46 | // 47 | if cover_sig 48 | strategy.close("S") 49 | // 50 | // 51 | 52 | //////////////////////////////////////////////////////////////////// 53 | // Plots 54 | plot(regime_sig, color=color.red, title='Regime Signal') 55 | -------------------------------------------------------------------------------- /pinescript/roc_of_ema_with_donch_mid_cross.psc: -------------------------------------------------------------------------------- 1 | //@version=5 2 | strategy("ROC of EMA strategy - Donch", overlay=false, default_qty_type=strategy.percent_of_equity, default_qty_value=100) 3 | 4 | //////////////////////////////////////////////////////////////// 5 | // Inputs 6 | roc_length = input.int(defval=150, title='ROC Length', minval=1) 7 | roc_smlength = input.int(defval=20, title='ROC Smooth Length') 8 | roc_timeframe = input.timeframe(defval='', title='ROC Time Frame') 9 | en_short = input.bool(defval=false, title='Enable Shorts ?') 10 | 11 | //////////////////////////////////////////////////////////////// 12 | // Signa;s 13 | roc_sig = ta.roc(ta.ema(close, roc_smlength), roc_length) 14 | roc_dhi_sig = ta.highest(roc_sig, roc_length) 15 | roc_dlo_sig = ta.lowest(roc_sig, roc_length) 16 | roc_dmid_sig = math.avg(roc_dlo_sig, roc_dhi_sig) 17 | 18 | //roc_lreg_sig = ta.linreg(roc_sig, roc_length, 0) 19 | 20 | roc_sig_t = request.security(syminfo.tickerid, roc_timeframe, roc_sig) 21 | roc_dhi_sig_t = request.security(syminfo.tickerid, roc_timeframe, roc_dhi_sig) 22 | roc_dlo_sig_t = request.security(syminfo.tickerid, roc_timeframe, roc_dlo_sig) 23 | roc_dmid_sig_t = request.security(syminfo.tickerid, roc_timeframe, roc_dmid_sig) 24 | 25 | //roc_lreg_sig_t = request.security(syminfo.tickerid, roc_timeframe, roc_lreg_sig) 26 | 27 | buy_sig_1 = ta.crossover(roc_sig_t, roc_dmid_sig_t) and (roc_sig_t > 0) 28 | buy_sig_2 = ta.crossover(roc_sig_t, 0) and (roc_sig_t > roc_dmid_sig_t) 29 | buy_sig = buy_sig_1 30 | sell_sig = ta.crossunder(roc_sig_t, roc_dmid_sig_t) or ta.crossunder(roc_sig_t, 0) 31 | short_sig_1 = ta.crossunder(roc_sig_t, roc_dmid_sig_t) and (roc_sig_t < 0) 32 | short_sig_2 = ta.crossunder(roc_sig_t, 0) and (roc_sig_t < roc_dmid_sig_t) 33 | short_sig = short_sig_1 34 | cover_sig = ta.crossover(roc_sig_t, roc_dmid_sig_t) or ta.crossover(roc_sig_t, 0) 35 | 36 | /////////////////////////////////////////////////////////////// 37 | // Execute positions 38 | // 39 | if buy_sig 40 | strategy.entry("L", strategy.long) 41 | // 42 | if sell_sig 43 | strategy.close("L") 44 | // 45 | if en_short 46 | if short_sig 47 | strategy.entry("S", strategy.short) 48 | // 49 | if cover_sig 50 | strategy.close("S") 51 | // 52 | // 53 | 54 | /////////////////////////////////////////////////////////// 55 | // Plots 56 | plot(roc_sig_t, color=#2962FF, title="ROC") 57 | plot(roc_dmid_sig_t, color=color.green, title="ROC DMID") 58 | //plot(roc_lreg, color=color.orange, title="ROC LREG") 59 | hline(0, color=#787B86, title="Zero Line") 60 | -------------------------------------------------------------------------------- /pinescript/rsi_ch_breakout.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | strategy("RSI channel breakout", overlay=false) 3 | 4 | /////////////////////////////////////////////////////////////////////// 5 | // Inputs 6 | rsi_period = input(defval=14, title="RSI Period", type=input.integer, minval=1, maxval=100, step=1) 7 | donch_period = input(defval=9, title="Donch RSI Period", type=input.integer, minval=1, maxval=200, step=1) 8 | time_frame = input(defval='1D', title='RSI Time frame', type=input.resolution) 9 | stop_loss = input(defval=1.0, title="Stop loss", type=input.float) 10 | use_sperc = input(defval=false, title='Stop loss & tolerance are in %centage(s)', type=input.bool) 11 | use_tstop = input(defval=true, title='Use trailing stop', type=input.bool) 12 | 13 | /////////////////////////////////////////////////////////////////////////// 14 | // Calculate stop losses 15 | stop_l = use_sperc ? strategy.position_avg_price*(1-stop_loss/100.0) : (strategy.position_avg_price-stop_loss) 16 | stop_s = use_sperc ? strategy.position_avg_price*(1+stop_loss/100.0) : (strategy.position_avg_price+stop_loss) 17 | if use_tstop 18 | stop_l := use_sperc ? close*(1-stop_loss/100.0) : (close - stop_loss) 19 | stop_s := use_sperc ? close*(1+stop_loss/100.0) : (close + stop_loss) 20 | // 21 | 22 | //////////////////////////////////////////////////////////////////// 23 | // Get indicator signals 24 | cum_rsi_t = rsi(close, rsi_period) + rsi(close, rsi_period)[1] + rsi(close, rsi_period)[2] 25 | donch_rsi_hi_t = highest(cum_rsi_t, donch_period) 26 | donch_rsi_lo_t = lowest(cum_rsi_t, donch_period) 27 | cum_rsi = security(syminfo.tickerid, time_frame, cum_rsi_t) 28 | donch_rsi_hi = security(syminfo.tickerid, time_frame, donch_rsi_hi_t) 29 | donch_rsi_lo = security(syminfo.tickerid, time_frame, donch_rsi_lo_t) 30 | 31 | //////////////////////////////////////////////////////////////// 32 | // Position signals 33 | buy = cum_rsi > donch_rsi_hi[1] 34 | sell = crossunder(close, stop_l[1]) 35 | short = cum_rsi < donch_rsi_lo[1] 36 | cover = crossover(close, stop_s[1]) 37 | 38 | //////////////////////////////////////////////////////////////// 39 | // Execute positions 40 | strategy.entry("long", strategy.long, when=buy) 41 | strategy.close("long", when=sell) 42 | strategy.entry("short", strategy.short, when=short) 43 | strategy.close("short", when=cover) 44 | 45 | //////////////////////////////////////////////////////////////// 46 | // Plots 47 | plot(cum_rsi, title="CUM RSI", color=color.green) 48 | plot(donch_rsi_hi, title="CUM RSI HI", color=color.olive) 49 | plot(donch_rsi_lo, title="CUM_RSI_LO", color=color.red) 50 | -------------------------------------------------------------------------------- /pinescript/pivot_levels_breakout_v5.psc: -------------------------------------------------------------------------------- 1 | //@version=5 2 | strategy("Pivot Levels v2 (Using ol2 as source) - close cross with band len", overlay=true) 3 | 4 | //////////////////////////////////////////////////////////////////// 5 | // Inputs 6 | pivot_bars = input.int(defval=2, title='Pivot Bars Length') 7 | pivot_tframe = input.timeframe(defval='1D', title='Pivot Time Frame') 8 | pivot_donch_len = input.int(defval=5, title='Donchian smooth period for pivot levels') 9 | pivot_band_len = input.int(defval=5, title='Pivot band length') 10 | allow_shorts = input.bool(defval=true, title='Allow Short trades ?') 11 | 12 | ////////////////////////////////////////////////////////////////// 13 | // Misc functions 14 | /////////////////////////////////////////////////////// 15 | // Get data from high time frame 16 | get_security(_src, _tf) => 17 | request.security(syminfo.tickerid, _tf, _src[1], gaps=barmerge.gaps_on, lookahead=barmerge.lookahead_on) 18 | // 19 | 20 | // Get Pivots 21 | pivots_ol2(_pivot_bars, _pivot_tf) => 22 | phigh_t = ta.pivothigh(math.avg(open, close), _pivot_bars, _pivot_bars) 23 | plow_t = ta.pivotlow(math.avg(open, close), _pivot_bars, _pivot_bars) 24 | phigh = phigh_t 25 | plow = plow_t 26 | phigh_tf = get_security(phigh_t, _pivot_tf) 27 | plow_tf = get_security(plow_t, _pivot_tf) 28 | phigh_san = phigh_tf 29 | plow_san = plow_tf 30 | phigh_san := nz(phigh_tf[1]) ? phigh_tf[1] : phigh_san[1] 31 | plow_san := nz(plow_tf[1]) ? plow_tf[1] : plow_san[1] 32 | [phigh_san, plow_san] 33 | // 34 | 35 | //////////////////////////////////////////////////////////////////// 36 | // Indicators 37 | [phigh_san_, plow_san_] = pivots_ol2(pivot_bars, pivot_tframe) 38 | phigh_san = ta.highest(phigh_san_, pivot_donch_len) 39 | plow_san = ta.lowest(plow_san_, pivot_donch_len) 40 | low_band = phigh_san - pivot_band_len 41 | high_band = plow_san + pivot_band_len 42 | oc2 = math.avg(open, close) 43 | 44 | /////////////////////////////////////////////////////////////////// 45 | // Signals 46 | buy_sig = ta.crossover(oc2, high_band) 47 | sell_sig = ta.crossunder(oc2, low_band) 48 | 49 | if buy_sig 50 | strategy.entry("L", strategy.long) 51 | if sell_sig 52 | if allow_shorts 53 | strategy.entry("S", strategy.short) 54 | else 55 | strategy.close("L") 56 | // 57 | // 58 | 59 | ///////////////////////////////////////////////////////////////////// 60 | // Plots 61 | plot(phigh_san, color=color.green, title='P+') 62 | plot(plow_san, color=color.red, title='P-') 63 | plot(high_band, color=color.olive, title='Band High', style=plot.style_stepline) 64 | plot(low_band, color=color.orange, title='Band Low', style=plot.style_stepline) 65 | -------------------------------------------------------------------------------- /afl/donch_channel_breakouts_with_exit_via_basis_crossovers_v1.afl: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | _SECTION_BEGIN("Donchian Channel Breakouts v1"); 4 | 5 | //////////////////////////////////////////////////////// 6 | // Backtester control parameters 7 | //////////////////////////////////////////////////////// 8 | A_ControlParams(); 9 | 10 | ///////////////////////////////////////////////////////// 11 | // Parameters 12 | ///////////////////////////////////////////////////////// 13 | // Master parameter controlling if script is to be run in 14 | // optimizer mode or backtest mode. Appropriate parameters 15 | // are seected based on this master mode. 16 | Optimizer_run = ParamToggle("Optimize", "No|Yes", 0); 17 | 18 | // Parameters 19 | _SECTION_BEGIN("Optimizer parameters"); 20 | t_frame_donch_index = Optimize("Donchian Time Frame index", 0, 0, G_NumTimeFrames()-1, 1); 21 | donch_period_opt = Optimize("Donchian Period", 5, 5, 200, 5); 22 | _SECTION_END(); 23 | 24 | _SECTION_BEGIN("Backtest parameters"); 25 | // Parameters 26 | t_frame_donch = ParamList("Donchian Time Frame", TIME_FRAMES); 27 | donch_period = Param("Donchian Period", 5, 5, 200, 5); 28 | _SECTION_END(); 29 | 30 | // Some parameters need to be calculated for optimizer mode run 31 | if (Optimizer_run){ 32 | t_frame_donch = G_TimeFrameFromIndex(t_frame_donch_index); 33 | donch_period = donch_period_opt; 34 | } 35 | 36 | /////////////////////////////////////////////////////////// 37 | // Indicators 38 | /////////////////////////////////////////////////////////// 39 | 40 | donch_hi = G_Function2("HHV", t_frame_donch, -1, "High", donch_period); 41 | donch_lo = G_Function2("LLV", t_frame_donch, -1, "Low", donch_period); 42 | donch_bas = (donch_hi + donch_lo)/2; 43 | 44 | /////////////////////////////////////////////////////////// 45 | // Trade signals 46 | /////////////////////////////////////////////////////////// 47 | Buy = Cross(Close, donch_hi); 48 | Short = Cross(donch_lo, Close); 49 | Sell = Cross(donch_bas, Close); 50 | Cover = Cross(Close, donch_bas); 51 | 52 | Buy = ExRem(Buy, Sell); 53 | Sell = ExRem(Sell, Buy); 54 | Short = ExRem(Short, Cover); 55 | Cover = ExRem(Cover, Short); 56 | 57 | BuyPrice = Open; 58 | SellPrice = Open; 59 | ShortPrice = Open; 60 | CoverPrice = Open; 61 | 62 | /////////////////////////////////////////////////////////// 63 | // Plots 64 | /////////////////////////////////////////////////////////// 65 | // Indicator plots 66 | Plot(Close, "Price", colorBlack, styleCandle); 67 | Plot(donch_hi, "DHi", colorBrightGreen, styleLine); 68 | Plot(donch_lo, "DLo", colorBrown, styleLine); 69 | Plot(donch_bas, "DMid", colorBlue, styleLine); 70 | 71 | // Plot trades 72 | shape = Buy * shapeUpArrow + Sell * shapeDownArrow; 73 | PlotShapes(shape, IIf(Buy, colorGreen, colorRed), 0, IIf(Buy, Low, High)); 74 | 75 | _SECTION_END(); 76 | -------------------------------------------------------------------------------- /pinescript/ind_rsi_orb-b.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | study(title="RSI - orb-b", overlay=false) 3 | 4 | /////////////////////////////////////////////////////////////////////// 5 | // Inputs 6 | time_frame_m = input(defval='1D', title="Resolution", options=['1m', '5m', '10m', '15m', '30m', '45m', '1h', '2h', '4h', '1D', '2D', '4D', '1W', '2W', '1M', '2M', '6M']) 7 | time_frame_n = input(defval='1D', title="Time Gap", options=['1m', '5m', '10m', '15m', '30m', '45m', '1h', '2h', '4h', '1D', '2D', '4D', '1W', '2W', '1M', '2M', '6M']) 8 | post_smooth = input(defval=false, title='Also smooth after ORB', type=input.bool) 9 | ema_len = input(defval=1, title='Smooth EMA Length') 10 | rsi_len = input(defval=4, title='RSI Length') 11 | 12 | ////////////////////////////////////////////////////////////////////////// 13 | // Misc functions 14 | get_time_frame(tf) => 15 | (tf == '1m') ? "1" 16 | : (tf == '5m') ? "5" 17 | : (tf == '10m') ? "10" 18 | : (tf == '15m') ? "15" 19 | : (tf == '30m') ? "30" 20 | : (tf == '45m') ? "45" 21 | : (tf == '1h') ? "60" 22 | : (tf == '2h') ? "120" 23 | : (tf == '4h') ? "240" 24 | : (tf == '1D') ? "D" 25 | : (tf == '2D') ? "2D" 26 | : (tf == '4D') ? "4D" 27 | : (tf == '1W') ? "W" 28 | : (tf == '2W') ? "2W" 29 | : (tf == '1M') ? "M" 30 | : (tf == '2M') ? "2M" 31 | : (tf == '6M') ? "6M" 32 | : "wrong resolution" 33 | // 34 | time_frame = get_time_frame(time_frame_m) 35 | time_gap = get_time_frame(time_frame_n) 36 | 37 | is_newbar(res) => 38 | change(time(res)) != 0 39 | // 40 | 41 | // check if this candle is close of session 42 | chk_close_time(hr, min) => 43 | time_sig = (hour[0] == hr) and (minute[0] > min and minute[1] <= min) 44 | time_sig 45 | // 46 | 47 | // chechk if this candle doesn't fall in close session 48 | chk_not_close_time(hr) => 49 | time_ncs = (hour[0] < hr) 50 | [time_ncs] 51 | // 52 | 53 | /////////////////////////////////////////////////////////////////////////// 54 | // Main signals (ORB-B) 55 | high_range = valuewhen(is_newbar(time_gap), high, 0) 56 | low_range = valuewhen(is_newbar(time_gap), low, 0) 57 | 58 | high_rangeL_n = security(syminfo.tickerid, time_frame, high_range) 59 | low_rangeL_n = security(syminfo.tickerid, time_frame, low_range) 60 | high_rangeL_s = security(syminfo.tickerid, time_frame, ema(high_range, ema_len)) 61 | low_rangeL_s = security(syminfo.tickerid, time_frame, ema(low_range, ema_len)) 62 | 63 | high_rangeL = high_rangeL_n 64 | low_rangeL = low_rangeL_n 65 | if post_smooth 66 | high_rangeL := high_rangeL_s 67 | low_rangeL := low_rangeL_s 68 | // 69 | 70 | ind_sig = avg(high_rangeL, low_rangeL) 71 | rsi_ind = rsi(ind_sig, rsi_len) 72 | 73 | ///////////////////////////////////////////////////////////////////////// 74 | // Plots 75 | plot(rsi_ind, color=color.green, linewidth=1) 76 | -------------------------------------------------------------------------------- /pinescript/roc_of_ema.psc: -------------------------------------------------------------------------------- 1 | //@version=5 2 | strategy("ROC of EMA strategy", overlay=false, default_qty_type=strategy.percent_of_equity, default_qty_value=100) 3 | 4 | //////////////////////////////////////////////////////////////// 5 | // Inputs 6 | roc_length = input.int(defval=150, title='ROC Length', minval=1) 7 | roc_smlength = input.int(defval=20, title='ROC Smooth Length') 8 | roc_timeframe = input.timeframe(defval='', title='ROC Time Frame') 9 | en_short = input.bool(defval=false, title='Enable Shorts ?') 10 | 11 | //////////////////////////////////////////////////////////////// 12 | // Signa;s 13 | roc_sig = ta.roc(ta.ema(close, roc_smlength), roc_length) 14 | roc_ema_sig = ta.ema(roc_sig, roc_length) 15 | //roc_lreg_sig = ta.linreg(roc_sig, roc_length, 0) 16 | 17 | roc_sig_t = request.security(syminfo.tickerid, roc_timeframe, roc_sig) 18 | roc_ema_sig_t = request.security(syminfo.tickerid, roc_timeframe, roc_ema_sig) 19 | //roc_lreg_sig_t = request.security(syminfo.tickerid, roc_timeframe, roc_lreg_sig) 20 | 21 | buy_sig_ema_c1 = ta.crossover(roc_sig_t, roc_ema_sig_t) and (roc_sig_t > 0) 22 | buy_sig_ema_c2 = ta.crossover(roc_sig_t, 0) and (roc_sig_t > roc_ema_sig_t) 23 | buy_sig_ema = buy_sig_ema_c1 //or buy_sig_ema_c2 24 | sell_sig_ema = ta.crossunder(roc_sig_t, roc_ema_sig_t) or ta.crossunder(roc_sig_t, 0) 25 | short_sig_ema_c1 = ta.crossunder(roc_sig_t, roc_ema_sig_t) and (roc_sig_t < 0) 26 | short_sig_ema_c2 = ta.crossunder(roc_sig_t, 0) and (roc_sig_t < roc_ema_sig_t) 27 | short_sig_ema = short_sig_ema_c1 //or short_sig_ema_c2 28 | cover_sig_ema = ta.crossover(roc_sig_t, roc_ema_sig_t) or ta.crossover(roc_sig_t, 0) 29 | 30 | //buy_sig_lreg = ta.crossover(roc_sig_t, roc_lreg_sig_t) and (roc_sig_t > 0) 31 | //sell_sig_lreg = ta.crossunder(roc_sig_t, roc_lreg_sig_t) or ta.crossunder(roc_sig_t, 0) 32 | //short_sig_lreg = ta.crossunder(roc_sig_t, roc_lreg_sig_t) and (roc_sig_t < 0) 33 | //cover_sig_lreg = ta.crossover(roc_sig_t, roc_lreg_sig_t) or ta.crossover(roc_sig_t, 0) 34 | 35 | buy_sig = buy_sig_ema 36 | sell_sig = sell_sig_ema 37 | short_sig = short_sig_ema 38 | cover_sig = cover_sig_ema 39 | 40 | /////////////////////////////////////////////////////////////// 41 | // Execute positions 42 | if buy_sig 43 | strategy.entry("L", strategy.long) 44 | // 45 | if sell_sig 46 | strategy.close("L") 47 | // 48 | if en_short 49 | if short_sig 50 | strategy.entry("S", strategy.short) 51 | // 52 | if cover_sig 53 | strategy.close("S") 54 | // 55 | // 56 | 57 | /////////////////////////////////////////////////////////// 58 | // Plots 59 | plot(roc_sig_t, color=#2962FF, title="ROC") 60 | plot(roc_ema_sig_t, color=color.green, title="ROC EMA") 61 | //plot(roc_lreg, color=color.orange, title="ROC LREG") 62 | hline(0, color=#787B86, title="Zero Line") 63 | -------------------------------------------------------------------------------- /afl/r1_s1_breakouts.afl: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | _SECTION_BEGIN("R1-S1 Breakout"); 4 | 5 | //////////////////////////////////////////////////////// 6 | // Backtester control parameters 7 | //////////////////////////////////////////////////////// 8 | A_ControlParams(); 9 | 10 | ///////////////////////////////////////////////////////// 11 | // Parameters 12 | ///////////////////////////////////////////////////////// 13 | // Master parameter controlling if script is to be run in 14 | // optimizer mode or backtest mode. Appropriate parameters 15 | // are seected based on this master mode. 16 | Optimizer_run = ParamToggle("Optimize", "No|Yes", 0); 17 | 18 | // Parameters 19 | _SECTION_BEGIN("Optimizer parameters"); 20 | t_frame_pivots_index = Optimize("Pivot Time Frame index", 0, 0, G_NumTimeFrames()-1, 1); 21 | rs_type_index = Optimize("RS Type index", 0, 0, G_NumRSLevels()-1, 1); 22 | Cross_thr_opt = Optimize("Threshold", 20, 0, 50, 5); 23 | _SECTION_END(); 24 | 25 | _SECTION_BEGIN("Backtest parameters"); 26 | // Parameters 27 | t_frame_pivots = ParamList("Pivot Time Frame", TIME_FRAMES); 28 | rs_type = ParamList("RS Type", RS_LEVELS); 29 | Cross_thr = Param("Threshold", 20, 0, 50, 5); 30 | _SECTION_END(); 31 | 32 | // Some parameters need to be calculated for optimizer mode run 33 | if (Optimizer_run){ 34 | t_frame_pivots = G_TimeFrameFromIndex(t_frame_pivots_index); 35 | rs_type = G_RSLevelsFromIndex(rs_type_index); 36 | Cross_thr = Cross_thr_opt; 37 | } 38 | 39 | /////////////////////////////////////////////////////////// 40 | // Indicators 41 | /////////////////////////////////////////////////////////// 42 | G_PivotsRS(t_frame_pivots, rs_type); 43 | R_Level = VarGet("R"); 44 | S_Level = VarGet("S"); 45 | 46 | /////////////////////////////////////////////////////////// 47 | // Trade signals 48 | /////////////////////////////////////////////////////////// 49 | Buy = Cross(Close, R1 + Cross_thr); 50 | Short = Cross(S1 - Cross_thr, Close); 51 | Sell = Short; 52 | Cover = Buy; 53 | 54 | Buy = ExRem(Buy, Sell); 55 | Sell = ExRem(Sell, Buy); 56 | Short = ExRem(Short, Cover); 57 | Cover = ExRem(Cover, Short); 58 | 59 | BuyPrice = Open; 60 | SellPrice = Open; 61 | ShortPrice = Open; 62 | CoverPrice = Open; 63 | 64 | /////////////////////////////////////////////////////////// 65 | // Plots 66 | /////////////////////////////////////////////////////////// 67 | Equity_curve = Equity(); 68 | Maxe = LastValue(Highest(Equity_curve)); 69 | 70 | // Plot price 71 | Plot(Close, "Price", colorBlack, styleCandle); 72 | 73 | // Plot indicators 74 | Plot(R1, "R1", colorBrightGreen, styleLine); 75 | Plot(S1, "S1", colorBrown, styleLine); 76 | 77 | // Plot trades 78 | shape = Buy * shapeUpArrow + Sell * shapeDownArrow; 79 | PlotShapes(shape, IIf(Buy, colorGreen, colorRed), 0, IIf(Buy, Low, High)); 80 | 81 | // Plot Equity curve 82 | Plot(Equity_curve, "Equity", colorBlue, styleLine|styleOwnScale, 0, Maxe); 83 | 84 | _SECTION_END(); 85 | -------------------------------------------------------------------------------- /pinescript/vwap_anchored_multi_timeframe_crossover.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | strategy("VWAP MTF crossover", overlay=true) 3 | 4 | time_frame = input(defval="W", type=input.resolution, title="Time frame") 5 | tolerance = input(1.0, type=input.float, title="Tolerance") 6 | stop_loss = input(1.0, type=input.float, title="Stop loss") 7 | use_sperc = input(false, type=input.bool, title='Tolerance and stop loss in %centage(s)') 8 | use_iday = input(false, type=input.bool, title='Use intraday') 9 | end_hr = input(15, type=input.integer, title='End session hour') 10 | end_min = input(14, type=input.integer, title='End session minutes') 11 | use_tstop = input(true, title='Use trailing stop', type=input.bool) 12 | 13 | start_time = security(syminfo.tickerid, time_frame, time) 14 | new_session = iff(change(start_time), 1, 0) 15 | 16 | // check if this candle is close of session 17 | chk_close_time(hr, min) => 18 | time_sig = (hour[0] == hr) and (minute[0] > min and minute[1] < min) 19 | time_sig 20 | // 21 | 22 | // chechk if this candle doesn't fall in close session 23 | chk_not_close_time(hr) => 24 | time_ncs = (hour[0] < hr) 25 | [time_ncs] 26 | // 27 | 28 | 29 | //------------------------------------------------ 30 | vwap_sum_fn() => 31 | vwap_sum = 0.0 32 | vwap_sum := (new_session == 1) ? (hl2*volume) : (vwap_sum[1]+hl2*volume) 33 | vwap_sum 34 | // 35 | vol_sum_fn() => 36 | vol_sum = 0.0 37 | vol_sum := (new_session == 1) ? volume : (vol_sum[1]+volume) 38 | vol_sum 39 | // 40 | 41 | vwap_sig = vwap_sum_fn()/vol_sum_fn() 42 | 43 | vwap_sl = use_sperc ? vwap_sig*(1+tolerance/100.0) : (vwap_sig+tolerance) 44 | vwap_ss = use_sperc ? vwap_sig*(1-tolerance/100.0) : (vwap_sig-tolerance) 45 | 46 | stop_l = use_sperc ? strategy.position_avg_price*(1-stop_loss/100.0) : (strategy.position_avg_price-stop_loss) 47 | stop_s = use_sperc ? strategy.position_avg_price*(1+stop_loss/100.0) : (strategy.position_avg_price+stop_loss) 48 | if use_tstop 49 | stop_lt = use_sperc ? close*(1-stop_loss/100.0) : (close - stop_loss) 50 | stop_st = use_sperc ? close*(1+stop_loss/100.0) : (close + stop_loss) 51 | stop_l := stop_lt //max(stop_l, stop_lt) 52 | stop_s := stop_st //min(stop_s, stop_st) 53 | // 54 | 55 | buy = use_iday ? (crossover(close, vwap_sl) and (hour < end_hr)) : crossover(close, vwap_sl) 56 | sell = use_iday ? (crossunder(close, stop_l[1]) or chk_close_time(end_hr, end_min)) : crossunder(close, stop_l[1]) 57 | short = use_iday ? (crossunder(close, vwap_ss) and (hour < end_hr)) : crossunder(close, vwap_ss) 58 | cover = use_iday ? (crossover(close, stop_s[1]) or chk_close_time(end_hr, end_min)) : crossover(close, stop_s[1]) 59 | 60 | strategy.entry("L", strategy.long, when=buy) 61 | strategy.close("L", when=sell) 62 | strategy.entry("S", strategy.short, when=short) 63 | strategy.close("S", when=cover) 64 | 65 | plot(vwap_sig, title="VWAP", color=color.blue) 66 | -------------------------------------------------------------------------------- /afl/legacy/mtf_pivot_system_indicator.afl: -------------------------------------------------------------------------------- 1 | _SECTION_BEGIN("Multi TF Pivot indicator"); 2 | 3 | ////////////////////////////////////////////////// 4 | // Get Pivot points based on generic High and Low Levels 5 | function GetPV(nbar_t, H_level, L_Level) 6 | { 7 | PHigh = H_Level > Ref(HHV(H_Level, nbar_t), -1) AND Ref(HHV(H_Level, nbar_t), nbar_t) <= H_Level; 8 | PLow = L_Level < Ref(LLV(L_Level, nbar_t), -1) AND Ref(LLV(L_Level, nbar_t), nbar_t) >= L_Level; 9 | VarSet("PHigh", PHigh); 10 | VarSet("PLow", PLow); 11 | } 12 | 13 | ///////////////////////////////////////////////// 14 | // Get Pivots based on High Low 15 | function GetPVHL(nbar_t) 16 | { 17 | GetPV(nbar_t, H, L); 18 | } 19 | 20 | ///////////////////////////////////////////////// 21 | // Main logic starts here 22 | nbar_t = Param("nbar", 1, 1, 20, 1); 23 | t_frame = ParamList("time_frame", "DAILY|2*DAILY|4*DAILY|WEEKLY|2*WEEKLY", 0); 24 | show_all_plots = ParamToggle("show_all_plots", "No|Yes", 0); 25 | 26 | tFrame = 0; 27 | if (t_frame == "DAILY"){ 28 | tFrame = inDaily; 29 | }else if (t_frame == "2*DAILY"){ 30 | tFrame = 2*inDaily; 31 | }else if (t_frame == "4*DAILY"){ 32 | tFrame = 4*inDaily; 33 | }else if (t_frame == "WEEKLY"){ 34 | tFrame = inWeekly; 35 | }else if (t_frame == "2*WEEKLY"){ 36 | tFrame = 2*inWeekly; 37 | } 38 | 39 | // Set custom Time Frame 40 | TimeFrameSet(tFrame); 41 | 42 | // Define conditions 43 | GetPVHL(nbar_t); 44 | PHigh = VarGet("PHigh"); 45 | PLow = VarGet("PLow"); 46 | 47 | // High Fractals (Take last 4) 48 | PHighPrice1t = ValueWhen(PHigh, H, 1); 49 | PHighPrice2t = ValueWhen(PHigh, H, 2); 50 | PHighPrice3t = ValueWhen(PHigh, H, 3); 51 | PHighPrice4t = ValueWhen(PHigh, H, 4); 52 | 53 | // Low Fractals (Take last 4) 54 | PLowPrice1t = ValueWhen(PLow, L, 1); 55 | PLowPrice2t = ValueWhen(PLow, L, 2); 56 | PLowPrice3t = ValueWhen(PLow, L, 3); 57 | PLowPrice4t = ValueWhen(PLow, L, 4); 58 | 59 | // Restore Time Frame 60 | TimeFrameRestore(); 61 | 62 | PHighPrice1 = TimeFrameExpand(PHighPrice1t, tFrame); 63 | PHighPrice2 = TimeFrameExpand(PHighPrice2t, tFrame); 64 | PHighPrice3 = TimeFrameExpand(PHighPrice3t, tFrame); 65 | PHighPrice4 = TimeFrameExpand(PHighPrice4t, tFrame); 66 | 67 | PLowPrice1 = TimeFrameExpand(PLowPrice1t, tFrame); 68 | PLowPrice2 = TimeFrameExpand(PLowPrice2t, tFrame); 69 | PLowPrice3 = TimeFrameExpand(PLowPrice3t, tFrame); 70 | PLowPrice4 = TimeFrameExpand(PLowPrice4t, tFrame); 71 | 72 | // Plots 73 | if (show_all_plots) 74 | { 75 | Plot(PHighPrice1, "PH1", colorBlue); 76 | Plot(PHighPrice2, "PH2", colorPink); 77 | Plot(PHighPrice3, "PH3", colorBlueGrey); 78 | Plot(PHighPrice4, "PH4", colorBrown); 79 | } 80 | Plot(PLowPrice1, "PL1", colorGreen); 81 | Plot(PLowPrice2, "PL2", colorRed); 82 | Plot(PLowPrice3, "PL3", colorDarkYellow); 83 | Plot(PLowPrice4, "PL4", colorAqua); 84 | 85 | _SECTION_END(); 86 | -------------------------------------------------------------------------------- /pinescript/mean_reversion_rsi_trend_isolator_ehlers_instantaneous_trend_v1.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | strategy(title="Mean Reversion - RSI Trend isolator v1 - Ehlers Instantaneous Trend", overlay=false) 3 | 4 | //////////////////////////////////////////////////// 5 | // Inputs 6 | ind_src = input(hl2, title="Source") 7 | ind_alpha = input(0.07, title="Alpha", step=0.01) 8 | ind_rsi_len = input(3, title="RSI Length") 9 | ind_timeframe = input('1D', title="Time Frame", type=input.resolution) 10 | ind_rsi_thr = input(5, title="RSI upper and lower boundary thresholds") 11 | stop_loss = input(1.0, title="Stop loss", type=input.float) 12 | use_sperc = input(false, title='Stop loss & tolerance are in %centage(s)', type=input.bool) 13 | use_tstop = input(true, title='Use trailing stop', type=input.bool) 14 | 15 | /////////////////////////////////////////////////////////////////////////// 16 | // Calculate stop losses 17 | stop_l = use_sperc ? strategy.position_avg_price*(1-stop_loss/100.0) : (strategy.position_avg_price-stop_loss) 18 | stop_s = use_sperc ? strategy.position_avg_price*(1+stop_loss/100.0) : (strategy.position_avg_price+stop_loss) 19 | if use_tstop 20 | stop_l := use_sperc ? close*(1-stop_loss/100.0) : (close - stop_loss) 21 | stop_s := use_sperc ? close*(1+stop_loss/100.0) : (close + stop_loss) 22 | // 23 | 24 | 25 | ///////////////////////////////////////////////////// 26 | // Indicators 27 | ehlers_itrend() => 28 | ind_it = close 29 | ind_lag = close 30 | ind_it := (ind_alpha-((ind_alpha*ind_alpha)/4.0))*ind_src + 0.5*ind_alpha*ind_alpha*ind_src[1] - (ind_alpha-0.75*ind_alpha*ind_alpha)*ind_src[2] + 2*(1-ind_alpha)*nz(ind_it[1], ((ind_src+2*ind_src[1]+ind_src[2])/4.0)) - (1-ind_alpha)*(1-ind_alpha)*nz(ind_it[2], ((ind_src+2*ind_src[1]+ind_src[2])/4.0)) 31 | ind_lag := 2.0*ind_it-nz(ind_it[2]) 32 | [ind_it, ind_lag] 33 | // 34 | 35 | [ind_it_t, ind_lag_t] = ehlers_itrend() 36 | rsi_t = rsi(ind_it_t, ind_rsi_len) 37 | ind_rsi = security(syminfo.tickerid, ind_timeframe, rsi_t) 38 | rsi_hbnd = 100 - ind_rsi_thr 39 | rsi_lbnd = ind_rsi_thr 40 | 41 | ////////////////////////////////////////////////////// 42 | // Position signals 43 | buy = crossover(ind_rsi, rsi_lbnd) 44 | sell = crossunder(close, stop_l[1]) or crossover(ind_rsi, rsi_hbnd) or crossunder(ind_rsi, rsi_lbnd) 45 | short = crossunder(ind_rsi, rsi_hbnd) 46 | cover = crossover(close, stop_s[1]) or crossunder(ind_rsi, rsi_lbnd) or crossover(ind_rsi, rsi_hbnd) 47 | 48 | /////////////////////////////////////////////////////// 49 | // Execute signals 50 | strategy.entry("L", strategy.long, when=buy) 51 | strategy.close("L", when=sell) 52 | strategy.entry("S", strategy.short, when=short) 53 | strategy.close("S", when=cover) 54 | 55 | ///////////////////////////////////////////////////// 56 | // Plots 57 | plot(ind_rsi, color=color.red, linewidth=1, title="RSI - EITrend") 58 | plot(rsi_hbnd, color=color.black) 59 | plot(rsi_lbnd, color=color.black) 60 | -------------------------------------------------------------------------------- /pinescript/ind_orb-b_on_rsi_v1.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | study("ORB-B RSI v1") 3 | 4 | ////////////////////////////////////////////////////////////////////// 5 | // Inputs 6 | time_frame_m = input(defval='15m', title="Resolution", options=['1m', '5m', '10m', '15m', '30m', '45m', '1h', '2h', '4h', '1D', '2D', '4D', '1W', '2W', '1M', '2M', '6M']) 7 | time_frame_n = input(defval='1D', title="Time Gap", options=['1m', '5m', '10m', '15m', '30m', '45m', '1h', '2h', '4h', '1D', '2D', '4D', '1W', '2W', '1M', '2M', '6M']) 8 | post_smooth = input(defval=false, title='Also smooth after ORB', type=input.bool) 9 | rsi_len = input(defval=4, title='RSI Length') 10 | rsi_src = input(defval=open, title='RSI Source', type=input.source) 11 | rsi_dlen = input(defval=1, title='RSI Donchian Length', type=input.integer) 12 | ema_len = input(defval=1, title='EMA Length for ORB smooth', type=input.integer) 13 | 14 | ////////////////////////////////////////////////////////////////////////// 15 | // Misc functions 16 | get_time_frame(tf) => 17 | (tf == '1m') ? "1" 18 | : (tf == '5m') ? "5" 19 | : (tf == '10m') ? "10" 20 | : (tf == '15m') ? "15" 21 | : (tf == '30m') ? "30" 22 | : (tf == '45m') ? "45" 23 | : (tf == '1h') ? "60" 24 | : (tf == '2h') ? "120" 25 | : (tf == '4h') ? "240" 26 | : (tf == '1D') ? "D" 27 | : (tf == '2D') ? "2D" 28 | : (tf == '4D') ? "4D" 29 | : (tf == '1W') ? "W" 30 | : (tf == '2W') ? "2W" 31 | : (tf == '1M') ? "M" 32 | : (tf == '2M') ? "2M" 33 | : (tf == '6M') ? "6M" 34 | : "wrong resolution" 35 | // 36 | time_frame = get_time_frame(time_frame_m) 37 | time_gap = get_time_frame(time_frame_n) 38 | 39 | is_newbar(res) => 40 | change(time(res)) != 0 41 | // 42 | 43 | /////////////////////////////////////////////////////////////////////////// 44 | // Main signals (ORB-B) 45 | 46 | // Get RSI hi/lo bands 47 | rsi_sig = rsi(rsi_src, rsi_len) 48 | rsi_hi = highest(rsi_sig, rsi_dlen) 49 | rsi_lo = lowest(rsi_sig, rsi_dlen) 50 | 51 | // Calculate global hi/lo range based on morning openings 52 | high_range = valuewhen(is_newbar(time_gap), rsi_hi, 0) 53 | low_range = valuewhen(is_newbar(time_gap), rsi_lo, 0) 54 | 55 | // Calculate time framed based hi/lo ranges 56 | high_rangeL_n = security(syminfo.tickerid, time_frame, high_range) 57 | low_rangeL_n = security(syminfo.tickerid, time_frame, low_range) 58 | high_rangeL_s = security(syminfo.tickerid, time_frame, ema(high_range, ema_len)) 59 | low_rangeL_s = security(syminfo.tickerid, time_frame, ema(low_range, ema_len)) 60 | rsi_signal = security(syminfo.tickerid, time_frame, rsi_sig) 61 | 62 | // Do any ema smoothing (if required) 63 | high_rangeL = high_rangeL_n 64 | low_rangeL = low_rangeL_n 65 | if post_smooth 66 | high_rangeL := high_rangeL_s 67 | low_rangeL := low_rangeL_s 68 | // 69 | 70 | ///////////////////////////////////////////////////////////////////////////// 71 | // Plots 72 | plot(high_rangeL, color=color.green, title='HI+') 73 | plot(low_rangeL, color=color.red, title='LO+') 74 | plot(rsi_signal, color=color.blue, title='RSI') 75 | -------------------------------------------------------------------------------- /pinescript/rsi_trend_isolator_ehlers_instantaneous_trend_v1.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | strategy(title="RSI Trend isolator v1 - Ehlers Instantaneous Trend", overlay=false) 3 | 4 | //////////////////////////////////////////////////// 5 | // Inputs 6 | ind_src = input(hl2, title="Source") 7 | ind_alpha = input(0.07, title="Alpha", step=0.01) 8 | ind_rsi_len = input(3, title="RSI Length") 9 | ind_timeframe = input('1D', title="Time Frame", type=input.resolution) 10 | ind_rsi_thr = input(5, title="RSI upper and lower boundary thresholds") 11 | stop_loss = input(1.0, title="Stop loss", type=input.float) 12 | use_sperc = input(false, title='Stop loss & tolerance are in %centage(s)', type=input.bool) 13 | use_tstop = input(true, title='Use trailing stop', type=input.bool) 14 | 15 | /////////////////////////////////////////////////////////////////////////// 16 | // Calculate stop losses 17 | stop_l = use_sperc ? strategy.position_avg_price*(1-stop_loss/100.0) : (strategy.position_avg_price-stop_loss) 18 | stop_s = use_sperc ? strategy.position_avg_price*(1+stop_loss/100.0) : (strategy.position_avg_price+stop_loss) 19 | if use_tstop 20 | stop_l := use_sperc ? close*(1-stop_loss/100.0) : (close - stop_loss) 21 | stop_s := use_sperc ? close*(1+stop_loss/100.0) : (close + stop_loss) 22 | // 23 | 24 | 25 | ///////////////////////////////////////////////////// 26 | // Indicators 27 | ehlers_itrend() => 28 | ind_it = close 29 | ind_lag = close 30 | ind_it := (ind_alpha-((ind_alpha*ind_alpha)/4.0))*ind_src + 0.5*ind_alpha*ind_alpha*ind_src[1] - (ind_alpha-0.75*ind_alpha*ind_alpha)*ind_src[2] + 2*(1-ind_alpha)*nz(ind_it[1], ((ind_src+2*ind_src[1]+ind_src[2])/4.0)) - (1-ind_alpha)*(1-ind_alpha)*nz(ind_it[2], ((ind_src+2*ind_src[1]+ind_src[2])/4.0)) 31 | ind_lag := 2.0*ind_it-nz(ind_it[2]) 32 | [ind_it, ind_lag] 33 | // 34 | 35 | [ind_it_t, ind_lag_t] = ehlers_itrend() 36 | rsi_t = rsi(ind_it_t, ind_rsi_len) 37 | ind_rsi = security(syminfo.tickerid, ind_timeframe, rsi_t) 38 | rsi_hbnd = 100 - ind_rsi_thr 39 | rsi_lbnd = ind_rsi_thr 40 | 41 | ////////////////////////////////////////////////////// 42 | // Position signals 43 | buy = (ind_rsi > rsi_hbnd) and (ind_rsi[1] > rsi_hbnd) and (ind_rsi[2] > rsi_hbnd) and (ind_rsi[3] > rsi_hbnd) 44 | sell = crossunder(close, stop_l[1]) or crossunder(ind_rsi, rsi_hbnd) 45 | short = (ind_rsi < rsi_lbnd) and (ind_rsi[1] < rsi_lbnd) and (ind_rsi[2] < rsi_lbnd) and (ind_rsi[3] < rsi_lbnd) 46 | cover = crossover(close, stop_s[1]) or crossover(ind_rsi, rsi_lbnd) 47 | 48 | /////////////////////////////////////////////////////// 49 | // Execute signals 50 | strategy.entry("L", strategy.long, when=buy) 51 | strategy.close("L", when=sell) 52 | strategy.entry("S", strategy.short, when=short) 53 | strategy.close("S", when=cover) 54 | 55 | ///////////////////////////////////////////////////// 56 | // Plots 57 | plot(ind_rsi, color=color.red, linewidth=1, title="RSI - EITrend") 58 | plot(rsi_hbnd, color=color.black) 59 | plot(rsi_lbnd, color=color.black) 60 | -------------------------------------------------------------------------------- /pinescript/ind_orb-a_with_pivots.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | study(title="ORB-A with pivots", shorttitle="ORB-A-P", overlay=true) 3 | 4 | /////////////////////////////////// 5 | /// Inputs 6 | time_frame_m = input(defval='15m', title="Resolution", options=['1m', '5m', '10m', '15m', '30m', '45m', '1h', '2h', '4h', '1D', '2D', '4D', '1W', '2W', '1M', '2M', '6M']) 7 | 8 | /////////////////////////////////// 9 | /// Functions 10 | get_time_frame(tf) => 11 | (tf == '1m') ? "1" 12 | : (tf == '5m') ? "5" 13 | : (tf == '10m') ? "10" 14 | : (tf == '15m') ? "15" 15 | : (tf == '30m') ? "30" 16 | : (tf == '45m') ? "45" 17 | : (tf == '1h') ? "60" 18 | : (tf == '2h') ? "120" 19 | : (tf == '4h') ? "240" 20 | : (tf == '1D') ? "D" 21 | : (tf == '2D') ? "2D" 22 | : (tf == '4D') ? "4D" 23 | : (tf == '1W') ? "W" 24 | : (tf == '2W') ? "2W" 25 | : (tf == '1M') ? "M" 26 | : (tf == '2M') ? "2M" 27 | : (tf == '6M') ? "6M" 28 | : "wrong resolution" 29 | // 30 | time_frame = get_time_frame(time_frame_m) 31 | 32 | is_newbar(res) => 33 | change(time(res)) != 0 34 | // 35 | 36 | _pivots(prev_close, prev_open, prev_high, prev_low) => 37 | pi_level = (prev_high + prev_low + prev_close)/3 38 | bc_level = (prev_high + prev_low)/2 39 | tc_level = (pi_level - bc_level) + pi_level 40 | r1_level = pi_level * 2 - prev_low 41 | s1_level = pi_level * 2 - prev_high 42 | r2_level = (pi_level - s1_level) + r1_level 43 | s2_level = pi_level - (r1_level - s1_level) 44 | r3_level = prev_high + 2*(pi_level - prev_low) 45 | s3_level = prev_low - 2*(prev_high - pi_level) 46 | 47 | [pi_level, tc_level, bc_level, r1_level, s1_level, r2_level, s2_level, r3_level, s3_level] 48 | // 49 | 50 | ///////////////////////////////////////////////////////// 51 | /// Signals 52 | high_range = valuewhen(is_newbar('D'), high, 0) 53 | low_range = valuewhen(is_newbar('D'), low, 0) 54 | close_range = valuewhen(is_newbar('D'), close, 0) 55 | open_range = valuewhen(is_newbar('D'), open, 0) 56 | 57 | high_rangeL = security(syminfo.tickerid, time_frame, high_range) 58 | low_rangeL = security(syminfo.tickerid, time_frame, low_range) 59 | close_rangeL = security(syminfo.tickerid, time_frame, close_range) 60 | open_rangeL = security(syminfo.tickerid, time_frame, open_range) 61 | [pi_rl, tc_rl, bc_rl, r1_rl, s1_rl, r2_rl, s2_rl, r3_rl, s3_rl] = _pivots(close_rangeL, open_rangeL, high_rangeL, low_rangeL) 62 | 63 | //////////////////////////////////////////////////////// 64 | ///Plots 65 | plot(high_rangeL, color=color.green, linewidth=2, title='H_RL') 66 | plot(low_rangeL, color=color.red, linewidth=2, title='L_RL') 67 | plot(r1_rl, color=color.olive, linewidth=1, title='R1_RL') 68 | plot(s1_rl, color=color.blue, linewidth=1, title='S1_RL') 69 | plot(r2_rl, color=color.orange, linewidth=1, title='R2_RL') 70 | plot(s2_rl, color=color.navy, linewidth=1, title='S2_RL') 71 | plot(r3_rl, color=color.orange, linewidth=1, title='R3_RL') 72 | plot(s3_rl, color=color.navy, linewidth=1, title='S3_RL') 73 | -------------------------------------------------------------------------------- /pinescript/ehlers_time_warp_laguerre_rsi_threshold_v1.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | strategy("Ehlers Laguerre RSI Threshold strategy v1", overlay=false) 3 | 4 | /////////////////////////////////////////////////// 5 | // Inputs 6 | filt_gamma = input(defval=0.8, title="Gamma", type=input.float, step=0.1) 7 | time_frame = input(defval='1D', title="Time Frame", type=input.resolution) 8 | src = input(defval=close, title="Sourc", type=input.source) 9 | ema_len = input(defval=0, title="RSI Smooth EMA Length", type=input.integer) 10 | threshold = input(defval=0.2, title="Crossover Threshold", type=input.float, step=0.01) 11 | stop_loss = input(defval=1.0, title="Stop loss", type=input.float) 12 | use_sperc = input(defval=false, title='Stop loss & tolerance are in %centage(s)', type=input.bool) 13 | use_tstop = input(defval=true, title='Use trailing stop', type=input.bool) 14 | 15 | /////////////////////////////////////////////////// 16 | // Calculate Signals 17 | LaguerreRSI_v1(gamma, src) => 18 | L0 = src 19 | L1 = src 20 | L2 = src 21 | L3 = src 22 | L0 := (1-gamma)*src + gamma*nz(L0[1]) 23 | L1 := -gamma*L0 + L0[1] + gamma*nz(L1[1]) 24 | L2 := -gamma*L1 + L1[1] + gamma*nz(L2[1]) 25 | L3 := -gamma*L2 + L2[1] + gamma*nz(L3[1]) 26 | CU = 0.0 27 | CD = 0.0 28 | if (L0>=L1) 29 | CU := (L0-L1) 30 | else 31 | CD := (L1-L0) 32 | // 33 | if (L1>=L2) 34 | CU := CU + (L1-L2) 35 | else 36 | CD := CD + (L2-L1) 37 | // 38 | if (L2>=L3) 39 | CU := CU + (L2-L3) 40 | else 41 | CD := CD + (L3-L2) 42 | // 43 | CU_new = CU 44 | CD_new = CD 45 | if ema_len > 0 46 | CU_new := ema(CU, ema_len) 47 | CD_new := ema(CD, ema_len) 48 | // 49 | RSI = CU_new/(CU_new+CD_new) 50 | // 51 | 52 | filt_ind_t = LaguerreRSI_v1(filt_gamma, src) 53 | filt_ind = security(syminfo.tickerid, time_frame, filt_ind_t) 54 | 55 | rsi_hi = 1.0 - threshold 56 | rsi_lo = threshold 57 | 58 | //////////////////////////////////////////////////////////// 59 | // Stop loss 60 | //stop_l = use_sperc ? close*(1-stop_loss/100.0) : (close - stop_loss) 61 | //stop_s = use_sperc ? close*(1+stop_loss/100.0) : (close + stop_loss) 62 | stop_l = use_sperc ? strategy.position_avg_price*(1-stop_loss/100.0) : (strategy.position_avg_price-stop_loss) 63 | stop_s = use_sperc ? strategy.position_avg_price*(1+stop_loss/100.0) : (strategy.position_avg_price+stop_loss) 64 | if use_tstop 65 | stop_l := use_sperc ? close*(1-stop_loss/100.0) : (close - stop_loss) 66 | stop_s := use_sperc ? close*(1+stop_loss/100.0) : (close + stop_loss) 67 | 68 | /////////////////////////////////////////////////////////// 69 | // Position signals and execution 70 | buy = crossover(filt_ind, rsi_lo) 71 | sell = crossunder(close, stop_l[1]) 72 | short = crossunder(filt_ind, rsi_hi) 73 | cover = crossover(close, stop_s[1]) 74 | 75 | strategy.entry("L", strategy.long, when=buy) 76 | strategy.close("L", when=sell) 77 | strategy.entry("S", strategy.short, when=short) 78 | strategy.close("S", when=cover) 79 | 80 | //////////////////////////////////////////////////////////// 81 | // Plots 82 | plot(filt_ind, title='Laguerre RSI') 83 | -------------------------------------------------------------------------------- /pinescript/pivot_levels_breakout_v1.psc: -------------------------------------------------------------------------------- 1 | //@version=4 2 | strategy(title="Pivot levels breakout v1", overlay=true) 3 | 4 | /////////////////////////////////////////////////////////////////////////////// 5 | // Inputs 6 | pivot_bars = input(defval=2, title='Pivot Bars Length', type=input.integer) 7 | pivot_tframe = input(defval='1D', title='Pivot Time Frame', type=input.resolution) 8 | use_pivot_tframe = input(defval=false, title='Use Diff Pivot Time Frame', type=input.bool) 9 | tolerance = input(defval=0.0, title="Tolerance", type=input.float) 10 | stop_loss = input(defval=1.0, title="Stop loss", type=input.float) 11 | use_sperc = input(defval=false, title='Stop loss & tolerance are in %centage(s)', type=input.bool) 12 | use_tstop = input(defval=true, title='Use trailing stop', type=input.bool) 13 | pivot_donch_len = input(defval=1, title='Dochian smoother length', type=input.integer) 14 | crossover_lag = input(defval=0, title='Crossover Lag', type=input.integer) 15 | 16 | /////////////////////////////////////////////////////////////////////////////// 17 | // Pivot signals 18 | phigh_t = pivothigh(pivot_bars, pivot_bars) 19 | plow_t = pivotlow(pivot_bars, pivot_bars) 20 | phigh = phigh_t 21 | plow = plow_t 22 | phigh_tf = security(syminfo.tickerid, pivot_tframe, phigh_t) 23 | plow_tf = security(syminfo.tickerid, pivot_tframe, plow_t) 24 | if use_pivot_tframe 25 | phigh := phigh_tf 26 | plow := plow_tf 27 | // 28 | phigh_san_t = phigh 29 | plow_san_t = plow 30 | phigh_san_t := nz(phigh[1]) ? phigh[1] : phigh_san_t[1] 31 | plow_san_t := nz(plow[1]) ? plow[1] : plow_san_t[1] 32 | phigh_san = highest(phigh_san_t, pivot_donch_len) 33 | plow_san = lowest(plow_san_t, pivot_donch_len) 34 | 35 | /////////////////////////////////////////////////////////////////////////////// 36 | // Stop losses 37 | stop_l = use_sperc ? strategy.position_avg_price*(1-stop_loss/100.0) : (strategy.position_avg_price-stop_loss) 38 | stop_s = use_sperc ? strategy.position_avg_price*(1+stop_loss/100.0) : (strategy.position_avg_price+stop_loss) 39 | if use_tstop 40 | stop_l := use_sperc ? close*(1-stop_loss/100.0) : (close - stop_loss) 41 | stop_s := use_sperc ? close*(1+stop_loss/100.0) : (close + stop_loss) 42 | // 43 | 44 | ////////////////////////////////////////////////////////////////////////// 45 | // Main signals 46 | tolbu = use_sperc ? phigh_san*(1+tolerance/100.0) : (phigh_san+tolerance) 47 | tolbl = use_sperc ? plow_san*(1-tolerance/100.0) : (plow_san-tolerance) 48 | close_t = close[crossover_lag] 49 | 50 | buy = crossover(close_t, tolbu) 51 | sell = crossunder(close, stop_l[1]) or crossunder(close_t, tolbl) 52 | short = crossunder(close_t, tolbl) 53 | cover = crossover(close, stop_s[1]) or crossover(close_t, tolbu) 54 | 55 | strategy.entry("L", strategy.long, when=buy) 56 | strategy.close("L", when=sell) 57 | strategy.entry("S", strategy.short, when=short) 58 | strategy.close("S", when=cover) 59 | 60 | //////////////////////////////////////////////////////////////////////////// 61 | // Plots 62 | plot(phigh_san, color=color.green, title='P+') 63 | plot(plow_san, color=color.red, title='P-') 64 | -------------------------------------------------------------------------------- /pinescript/fisher_transform_mtr_trend_trader.psc: -------------------------------------------------------------------------------- 1 | //@version=5 2 | // Fisher Transform trend trader strategy. 3 | // This applies Fisher transform on higher time frames to get long term cycles 4 | // and tries to trade them via trend following. 5 | strategy(title="Fisher Transform MTR Trend trader", overlay=false) 6 | 7 | ///////////////////////////////////// 8 | // Parameters 9 | fish_length = input.int(defval=10, title='Fisher Transform Period', minval=1) 10 | fish_threshold = input.float(defval=0.001, title='Fisher Transform crossover threshold', step=0.001) 11 | fish_timeframe = input.timeframe(defval="", title='Fisher Transform Timeframe') 12 | fish_lag = input.int(defval=1, title='Fisher Transform Lag Peridod for crossover signals') 13 | fish_use_ema = input.bool(defval=false, title='Use smoothed ema for Fisher Transform') 14 | fish_ema_len = input.int(defval=1, title='Fisher Transform EMA Length (only when enabled)') 15 | fish_alpha = input.float(defval=0.66, title='Fisher Alpha (Change with Caution)', step=0.01) 16 | fish_delta = input.float(defval=0.67, title='Fisher Delta (Change with Caution)', step=0.01) 17 | 18 | ////////////////////////////////////// 19 | // Function to get source for the fisher transform 20 | get_fisher_source(src, use_ema, ema_length) => 21 | fish_src = src 22 | if use_ema 23 | fish_src := ta.ema(src, ema_length) 24 | // 25 | [fish_src] 26 | 27 | //////////////////////////////////// 28 | // Function to calculate fisher transform 29 | get_fisher(f_src, f_length, f_alpha, f_delta) => 30 | fish_src = f_src 31 | fish_src_hi = ta.highest(fish_src, f_length) 32 | fish_src_lo = ta.lowest(fish_src, f_length) 33 | 34 | fish_nvalue1 = fish_src 35 | fish_nvalue2 = fish_src 36 | fish_ind = fish_src 37 | fish_nvalue1 := f_alpha * ((fish_src - fish_src_lo) / (fish_src_hi - fish_src_lo) - 0.5) + f_delta * nz(fish_nvalue1[1]) 38 | if fish_nvalue1 > 0.99 39 | fish_nvalue2 := 0.999 40 | else 41 | if fish_nvalue1 < -0.99 42 | fish_nvalue2 := -0.999 43 | else 44 | fish_nvalue2 := fish_nvalue1 45 | // 46 | // 47 | fish_ind := 0.5 * math.log((1 + fish_nvalue2) / (1 - fish_nvalue2)) + 0.5 * nz(fish_ind[1]) 48 | [fish_ind] 49 | // 50 | 51 | ////////////////////////////////////////////// 52 | // Get higher time frame data 53 | [fish_src] = get_fisher_source(hl2, fish_use_ema, fish_ema_len) 54 | [fish_ind] = get_fisher(fish_src, fish_length, fish_alpha, fish_delta) 55 | fish_ind_mtr = request.security(syminfo.tickerid, fish_timeframe, fish_ind[1], barmerge.gaps_off, barmerge.lookahead_on) 56 | fish_trig_mtr = nz(fish_ind_mtr[1+fish_lag]) 57 | 58 | ////////////////////////////////////////////// 59 | // Generate trading signals 60 | sig_buy = fish_ind_mtr > (fish_trig_mtr + fish_threshold) 61 | sig_sell = fish_ind_mtr < (fish_trig_mtr - fish_threshold) 62 | 63 | /////////////////////////////////////////////// 64 | // Execute positions 65 | strategy.entry("L", strategy.long, when=sig_buy) 66 | strategy.entry("S", strategy.short, when=sig_sell) 67 | 68 | /////////////////////////////////////////////// 69 | // Plotting 70 | plot(fish_ind_mtr, color=color.green, title="Fisher Indicator") 71 | plot(fish_trig_mtr, color=color.red, title="Fisher Trigger") 72 | --------------------------------------------------------------------------------