Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //* Ichimoku Signals *\\
- // == inputs ==
- conversionPeriods = input(10, minval=1, title="Conversion Line Periods"),
- basePeriods = input(30, minval=1, title="Base Line Periods")
- laggingSpan2Periods = input(60, minval=1, title="Lagging Span 2 Periods"),
- displacement = input(30, minval=1, title="Displacement")
- // == helpers ==
- donchian(len) => avg(lowest(len), highest(len))
- resolve(src, default) =>
- if na(src)
- default
- else
- src
- // == generate ichimoku data ==
- conversionLine = donchian(conversionPeriods)
- baseLine = donchian(basePeriods)
- // NOTE: these senkou spans are for signal generation; not for plotting. this is
- // due to limitations of pinescript
- //
- // i.e. Senkou Span A
- leadLine1 = offset(avg(conversionLine, baseLine), displacement)
- // i.e. Senkou Span B
- leadLine2 = offset(donchian(laggingSpan2Periods), displacement)
- // == plot ichimoku ==
- // // i.e. Tenkan Sen (turning line) (blue)
- // plot(conversionLine, color=#0496ff, title="Turning/Conversion Line", linewidth=3)
- // // i.e. Kijun Sen (base/standard line) (red)
- // plot(baseLine, color=#991515, title="Standard/Base Line", linewidth=3)
- // // i.g. Chikou Span (lagging line) (green)
- // plot(close, offset = -displacement, color=#459915, title="Lagging Span", linewidth=3)
- // plot(conversionLine, color=#0496ff, title="Conversion Line")
- // plot(baseLine, color=#991515, title="Base Line")
- // real_leadLine1 = avg(conversionLine, baseLine)
- // real_leadLine2 = donchian(laggingSpan2Periods)
- // // i.e. Senkou Span A
- // p1 = plot(real_leadLine1, offset = displacement, color=green, title="Lead 1")
- // // i.e. Senkou Span B
- // p2 = plot(real_leadLine2, offset = displacement, color=red, title="Lead 2")
- // // i.e. Kumo cloud colouring
- // fill(p1, p2, color = real_leadLine1 > real_leadLine2 ? green : red)
- // == ichimoku cloud signals ==
- // source: http://www.ichimokutrader.com/signals.html
- // == Tenkan Sen (turning line) / Kijun Sen (standard line) Cross ==
- // tk_cross_bull = (conversionLine[1] < baseLine[1] and conversionLine >= baseLine)
- // tk_cross_bear = (conversionLine[1] > baseLine[1] and conversionLine <= baseLine)
- tk_cross_bull = crossover(conversionLine, baseLine)
- tk_cross_bear = crossunder(conversionLine, baseLine)
- // get true y-coord of the cross
- cross_y = (conversionLine[1] * (baseLine - baseLine[1]) - baseLine[1] * (conversionLine - conversionLine[1])) / ((baseLine - baseLine[1]) - (conversionLine - conversionLine[1]))
- tk_cross_score = 0.0
- tk_cross_score := resolve(tk_cross_score[1], 0)
- tk_cross_below_kumo = cross_y <= leadLine2[1] and cross_y <= leadLine1[1] and cross_y <= leadLine2 and cross_y <= leadLine1
- tk_cross_above_kumo = cross_y >= leadLine2[1] and cross_y >= leadLine1[1] and cross_y >= leadLine2 and cross_y >= leadLine1
- tk_cross_inside_kumo = (not tk_cross_below_kumo) and (not tk_cross_above_kumo)
- use_tk_cross = input(true, title="TS Cross")
- tk_cross_weight = input(1.0, title="TS Cross Importance Weight", type=float, step=0.1)
- tk_cross_weak_bullish_points = input(0.25, title="TS Cross Weak Bullish Points", type=float, step=0.1)
- tk_cross_neutral_bullish_points = input(0.5, title="TS Cross Neutral Bullish Points", type=float, step=0.1)
- tk_cross_strong_bullish_points = input(1.0, title="TS Cross Strong Bullish Points", type=float, step=0.1)
- tk_cross_weak_bearish_points = input(-0.25, title="TS Cross Weak Bearish Points", type=float, step=0.1)
- tk_cross_neutral_bearish_points = input(-0.5, title="TS Cross Neutral Bearish Points", type=float, step=0.1)
- tk_cross_strong_bearish_points = input(-1.0, title="TS Cross Strong Bearish Points", type=float, step=0.1)
- // == TK Cross / weak bullish (below kumo) ==
- tk_cross_score := (tk_cross_bull and tk_cross_below_kumo) ? tk_cross_weak_bullish_points : tk_cross_score
- // == TK Cross / neutral bullish (inside kumo) ==
- tk_cross_score := (tk_cross_bull and tk_cross_inside_kumo) ? tk_cross_neutral_bullish_points : tk_cross_score
- // == TK Cross / strong bullish (above kumo) ==
- tk_cross_score := (tk_cross_bull and tk_cross_above_kumo) ? tk_cross_strong_bullish_points : tk_cross_score
- // == TK Cross / strong bearish (below kumo) ==
- tk_cross_score := (tk_cross_bear and tk_cross_below_kumo) ? tk_cross_strong_bearish_points : tk_cross_score
- // == TK Cross / neutral bearish (inside kumo) ==
- tk_cross_score := (tk_cross_bear and tk_cross_inside_kumo) ? tk_cross_neutral_bearish_points : tk_cross_score
- // == TK Cross / weak bearish (above kumo) ==
- tk_cross_score := (tk_cross_bear and tk_cross_above_kumo) ? tk_cross_weak_bearish_points : tk_cross_score
- // == Price and Kijun Sen (standard line) Cross ==
- //pk_cross_bull = (close[1] < baseLine[1] and close >= baseLine)
- //pk_cross_bear = (close[1] > baseLine[1] and close <= baseLine)
- pk_cross_bull = crossover(close, baseLine)
- pk_cross_bear = crossunder(close, baseLine)
- cross_pk_y = (close[1] * (baseLine - baseLine[1]) - baseLine[1] * (close - close[1])) / ((baseLine - baseLine[1]) - (close - close[1]))
- pk_cross_below_kumo = cross_pk_y <= leadLine2[1] and cross_pk_y <= leadLine1[1] and cross_pk_y <= leadLine2 and cross_pk_y <= leadLine1
- pk_cross_above_kumo = cross_pk_y >= leadLine2[1] and cross_pk_y >= leadLine1[1] and cross_pk_y >= leadLine2 and cross_pk_y >= leadLine1
- pk_cross_inside_kumo = (not pk_cross_below_kumo) and (not pk_cross_above_kumo)
- pk_cross_score = 0.0
- pk_cross_score := resolve(pk_cross_score[1], 0)
- use_pk_cross = input(true, title="PS Cross")
- pk_cross_weight = input(1.0, title="PS Cross Importance Weight", type=float, step=0.1)
- pk_cross_weak_bullish_points = input(0.25, title="PS Cross Weak Bullish Points", type=float, step=0.1)
- pk_cross_neutral_bullish_points = input(0.5, title="PS Cross Neutral Bullish Points", type=float, step=0.1)
- pk_cross_strong_bullish_points = input(1.0, title="PS Cross Strong Bullish Points", type=float, step=0.1)
- pk_cross_weak_bearish_points = input(-0.25, title="PS Cross Weak Bearish Points", type=float, step=0.1)
- pk_cross_neutral_bearish_points = input(-0.5, title="PS Cross Neutral Bearish Points", type=float, step=0.1)
- pk_cross_strong_bearish_points = input(-1.0, title="PS Cross Strong Bearish Points", type=float, step=0.1)
- // == PK Cross / weak bullish (below kumo) ==
- pk_cross_score := (pk_cross_bull and pk_cross_below_kumo) ? pk_cross_weak_bullish_points : pk_cross_score
- // == PK Cross / neutral bullish (inside kumo)
- pk_cross_score := (pk_cross_bull and pk_cross_inside_kumo) ? pk_cross_neutral_bullish_points : pk_cross_score
- // == PK Cross / strong bullish (above kumo)
- pk_cross_score := (pk_cross_bull and pk_cross_above_kumo) ? pk_cross_strong_bullish_points : pk_cross_score
- // == PK Cross / strong bearish (below kumo)
- pk_cross_score := (pk_cross_bear and pk_cross_below_kumo) ? pk_cross_strong_bearish_points : pk_cross_score
- // == PK Cross / neutral bearish (inside kumo)
- pk_cross_score := (pk_cross_bear and pk_cross_inside_kumo) ? pk_cross_neutral_bearish_points : pk_cross_score
- // == PK Cross / weak bearish (above kumo)
- pk_cross_score := (pk_cross_bear and pk_cross_above_kumo) ? pk_cross_weak_bearish_points : pk_cross_score
- //
- price_below_kumo = (close < leadLine2 and close < leadLine1)
- price_above_kumo = (close > leadLine2 and close > leadLine1)
- price_inside_kumo = (not price_below_kumo) and (not price_above_kumo)
- // == Kumo Breakouts ==
- kumo_breakout_score = 0.0
- kumo_breakout_score := resolve(kumo_breakout_score[1], 0)
- kumo_bull = (crossover(close, leadLine1) and leadLine1 > leadLine2) or (crossover(close, leadLine2) and leadLine2 > leadLine1)
- kumo_bear = (crossunder(close, leadLine2) and leadLine1 > leadLine2) or (crossunder(close, leadLine1) and leadLine2 > leadLine1)
- kumo_breakout_bullish_points = input(1.0, title="Kumo Breakout Bullish Points", type=float, step=0.1)
- kumo_breakout_bearish_points = input(-1.0, title="Kumo Breakout Bearish Points", type=float, step=0.1)
- kumo_breakout_score := (kumo_bull and price_above_kumo) ? kumo_breakout_bullish_points : kumo_breakout_score
- kumo_breakout_score := (kumo_bear and price_below_kumo) ? kumo_breakout_bearish_points : kumo_breakout_score
- use_kumo_breakout = input(true, title="Kumo Breakout")
- kumo_breakout_weight = input(1.0, title="Kumo Breakout Importance Weight", type=float, step=0.1)
- // == Senkou Span Cross ==
- // The Senkou Span Cross signal occurs when the Senkou Span A (1st leading line) crosses the Senkou Span B (2nd leading line).
- // NOTE: this cross occurs ahead of the price, since it's displaced to the right; this displacement must be removed
- // i.e. Senkou Span A (no displacement)
- no_dp_leadLine1 = avg(conversionLine, baseLine)
- // i.e. Senkou Span B (no displacement)
- no_dp_leadLine2 = donchian(laggingSpan2Periods)
- // TODO: debug;remove
- // plot(no_dp_leadLine1)
- // plot(no_dp_leadLine2)
- lead_line_cross_bull = crossover(no_dp_leadLine1, no_dp_leadLine2)
- lead_line_cross_bear = crossunder(no_dp_leadLine1, no_dp_leadLine2)
- price_below_kumo := (close < no_dp_leadLine2 and close < no_dp_leadLine1)
- price_above_kumo := (close > no_dp_leadLine2 and close > no_dp_leadLine1)
- price_inside_kumo := (not price_below_kumo) and (not price_above_kumo)
- // price_inside_kumo = (no_dp_leadLine2 < close and close < no_dp_leadLine1) and (no_dp_leadLine1 < close and close < no_dp_leadLine2)
- span_cross_score = 0.0
- span_cross_score := resolve(span_cross_score[1], 0)
- use_span_cross = input(true, title="Span Cross")
- span_cross_weight = input(1.0, title="Span Cross Importance Weight", type=float, step=0.1)
- span_cross_weak_bullish_points = input(0.25, title="Span Cross Weak Bullish Points", type=float, step=0.1)
- span_cross_neutral_bullish_points = input(0.5, title="Span Cross Neutral Bullish Points", type=float, step=0.1)
- span_cross_strong_bullish_points = input(1.0, title="Span Cross Strong Bullish Points", type=float, step=0.1)
- span_cross_weak_bearish_points = input(-0.25, title="Span Cross Weak Bearish Points", type=float, step=0.1)
- span_cross_neutral_bearish_points = input(-0.5, title="Span Cross Neutral Bearish Points", type=float, step=0.1)
- span_cross_strong_bearish_points = input(-1.0, title="Span Cross Strong Bearish Points", type=float, step=0.1)
- // == Senkou Span Cross / weak bullish (price below kumo) ==
- span_cross_score := (lead_line_cross_bull and price_below_kumo) ? span_cross_weak_bullish_points : span_cross_score
- // == Senkou Span Cross / neutral bullish (price inside kumo) ==
- span_cross_score := (lead_line_cross_bull and price_inside_kumo) ? span_cross_neutral_bullish_points : span_cross_score
- // == Senkou Span Cross / strong bullish (price above kumo) ==
- span_cross_score := (lead_line_cross_bull and price_above_kumo) ? span_cross_strong_bullish_points : span_cross_score
- // == Senkou Span Cross / weak bearish (price above kumo) ==
- span_cross_score := (lead_line_cross_bear and price_above_kumo) ? span_cross_weak_bearish_points : span_cross_score
- // == Senkou Span Cross / neutral bearish (price inside kumo) ==
- span_cross_score := (lead_line_cross_bear and price_inside_kumo) ? span_cross_neutral_bearish_points : span_cross_score
- // == Senkou Span Cross / strong bearish (price below kumo) ==
- span_cross_score := (lead_line_cross_bear and price_below_kumo) ? span_cross_strong_bearish_points : span_cross_score
- // == Chikou Span Cross ==
- // The Chikou Span Cross signal occurs when the Chikou Span (Lagging line) rises above or falls below the price.
- past_price = offset(close, displacement)
- lag_line_bull_cross = close > close[displacement]
- lag_line_bear_cross = close < close[displacement]
- // TODO: debug; remove
- // plot(close)
- // plot(close[displacement], linewidth=2)
- past_price_below_kumo = (past_price < leadLine2 and past_price < leadLine1)
- past_price_above_kumo = (past_price > leadLine2 and past_price > leadLine1)
- past_price_inside_kumo = (leadLine2 < past_price and past_price < leadLine1) and (leadLine1 < past_price and past_price < leadLine2)
- lag_line_cross_score = 0.0
- lag_line_cross_score := resolve(lag_line_cross_score[1], 0)
- use_lag_line = input(true, title="Lag Line Cross")
- lag_line_cross_weight = input(1.0, title="Lag Line Cross Importance Weight", type=float, step=0.1)
- lag_line_cross_weak_bullish_points = input(0.25, title="Lag Line Cross Weak Bullish Points", type=float, step=0.1)
- lag_line_cross_neutral_bullish_points = input(0.5, title="Lag Line Cross Neutral Bullish Points", type=float, step=0.1)
- lag_line_cross_strong_bullish_points = input(1.0, title="Lag Line Cross Strong Bullish Points", type=float, step=0.1)
- lag_line_cross_weak_bearish_points = input(-0.25, title="Lag Line Cross Weak Bearish Points", type=float, step=0.1)
- lag_line_cross_neutral_bearish_points = input(-0.5, title="Lag Line Cross Neutral Bearish Points", type=float, step=0.1)
- lag_line_cross_strong_bearish_points = input(-1.0, title="Lag Line Cross Strong Bearish Points", type=float, step=0.1)
- // == Chikou Span Cross / weak bullish (price below kumo)
- lag_line_cross_score := (lag_line_bull_cross and past_price_below_kumo) ? lag_line_cross_weak_bullish_points : lag_line_cross_score
- // == Chikou Span Cross / neutral bullish (price inside kumo)
- lag_line_cross_score := (lag_line_bull_cross and past_price_inside_kumo) ? lag_line_cross_neutral_bullish_points : lag_line_cross_score
- // == Chikou Span Cross / strong bullish (price above kumo)
- lag_line_cross_score := (lag_line_bull_cross and past_price_above_kumo) ? lag_line_cross_strong_bullish_points : lag_line_cross_score
- // == Chikou Span Cross / weak bearish (price above kumo)
- lag_line_cross_score := (lag_line_bear_cross and past_price_above_kumo) ? lag_line_cross_weak_bearish_points : lag_line_cross_score
- // == Chikou Span Cross / neutral bearish (price inside kumo)
- lag_line_cross_score := (lag_line_bear_cross and past_price_inside_kumo) ? lag_line_cross_neutral_bearish_points : lag_line_cross_score
- // == Chikou Span Cross / strong bearish (price below kumo)
- lag_line_cross_score := (lag_line_bear_cross and past_price_below_kumo) ? lag_line_cross_strong_bearish_points : lag_line_cross_score
- // == lag line releative to cloud ==
- use_lag_line_location = input(true, title="Lag Line Relative to Cloud")
- lag_line_location_weight = input(1.0, title="Lag Line Relative to Cloud Importance Weight", type=float, step=0.1)
- lag_line_location_above_points = input(1.0, title="Lag Line Above Cloud Points", type=float, step=0.1)
- lag_line_location_inside_points = input(0, title="Lag Line Inside Cloud Points", type=float, step=0.1)
- lag_line_location_below_points = input(-1.0, title="Lag Line Below Cloud Points", type=float, step=0.1)
- lag_line_placement_score = 0.0
- lag_line_placement_score := resolve(lag_line_placement_score[1], 0)
- lag_line_placement_score := past_price_above_kumo ? lag_line_location_above_points : lag_line_placement_score
- lag_line_placement_score := past_price_inside_kumo ? lag_line_location_inside_points : lag_line_placement_score
- lag_line_placement_score := past_price_below_kumo ? lag_line_location_below_points : lag_line_placement_score
- // == price relative to cloud ==
- price_placement_score = 0.0
- price_placement_score := resolve(price_placement_score[1], 0)
- use_price_location = input(true, title="Price Relative to Cloud")
- price_location_weight = input(1.0, title="Price Relative to Cloud Importance Weight", type=float, step=0.1)
- price_location_above_points = input(1.0, title="Price Above Cloud Points", type=float, step=0.1)
- price_location_inside_points = input(0, title="Price Inside Cloud Points", type=float, step=0.1)
- price_location_below_points = input(-1.0, title="Price Below Cloud Points", type=float, step=0.1)
- price_below_kumo := (close < leadLine2 and close < leadLine1)
- price_above_kumo := (close > leadLine2 and close > leadLine1)
- price_inside_kumo := (not price_below_kumo) and (not price_above_kumo)
- price_placement_score := price_above_kumo ? price_location_above_points : price_placement_score
- price_placement_score := price_inside_kumo ? price_location_inside_points : price_placement_score
- price_placement_score := price_below_kumo ? price_location_below_points : price_placement_score
- // == plot score ==
- resolve_bull_vote(vote) =>
- vote > 0 ? vote : 0
- resolve_bear_vote(vote) =>
- vote < 0 ? vote * -1 : 0
- number_of_metrics = 7
- bull_votes = 0.0
- bull_votes := resolve(bull_votes[1], 0) * 0.5 + resolve(bull_votes[2], 0) * 0.25 + resolve_bull_vote(tk_cross_score) + resolve_bull_vote(pk_cross_score) + resolve_bull_vote(kumo_breakout_score) + resolve_bull_vote(span_cross_score) + resolve_bull_vote(lag_line_cross_score) + resolve_bull_vote(lag_line_placement_score) + resolve_bull_vote(price_placement_score)
- bear_votes = 0.0
- bear_votes := resolve(bear_votes[1], 0) * 0.5 + resolve(bear_votes[2], 0) * 0.25 + resolve_bear_vote(tk_cross_score) + resolve_bear_vote(pk_cross_score) + resolve_bear_vote(kumo_breakout_score) + resolve_bear_vote(span_cross_score) + resolve_bear_vote(lag_line_cross_score) + resolve_bear_vote(lag_line_placement_score) + resolve_bear_vote(price_placement_score)
- raw_score = (bull_votes + 0.5) / (bull_votes + bear_votes + 1)
- // plot(total_score, linewidth=3, title="Total Score")
- plot(raw_score, linewidth=2, color=black)
- plot(0.75, style=line, title="Bull zone", color=green)
- plot(0.5, style=line, title="Base", color=black)
- plot(0.25, style=line, title="Bear zone", color=red)
- buy_signal = raw_score <= 0.2 and raw_score > raw_score[1] and raw_score[1] <= raw_score[2]
- sell_signal = raw_score >= 0.8 and raw_score < raw_score[1] and raw_score[1] >= raw_score[2]
- bg_color = buy_signal ? green : sell_signal ? red : white
- //bgcolor(bg_color, transp=70)
- icLong = crossover(raw_score, 0.5)
- icShort = crossunder(raw_score, 0.5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement