Ichimoku Score

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 7

//@version=2

study(title="Ichimoku Cloud Signal Score", shorttitle="Ichimoku (score)",


overlay=false)

// == inputs ==

conversionPeriods = input(9, minval=1, title="Conversion Line Periods"),


basePeriods = input(26, minval=1, title="Base Line Periods")
laggingSpan2Periods = input(52, minval=1, title="Lagging Span 2 Periods"),
displacement = input(26, 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_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)

tk_cross_score = resolve(tk_cross_score[1], 0)

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.5, title="TS Cross Weak Bullish Points",


type=float, step=0.1)
tk_cross_neutral_bullish_points = input(1.0, title="TS Cross Neutral Bullish
Points", type=float, step=0.1)
tk_cross_strong_bullish_points = input(2.0, title="TS Cross Strong Bullish Points",
type=float, step=0.1)

tk_cross_weak_bearish_points = input(-0.5, title="TS Cross Weak Bearish Points",


type=float, step=0.1)
tk_cross_neutral_bearish_points = input(-1.0, title="TS Cross Neutral Bearish
Points", type=float, step=0.1)
tk_cross_strong_bearish_points = input(-2.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 = 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.5, title="PS Cross Weak Bullish Points",


type=float, step=0.1)
pk_cross_neutral_bullish_points = input(1.0, title="PS Cross Neutral Bullish
Points", type=float, step=0.1)
pk_cross_strong_bullish_points = input(2.0, title="PS Cross Strong Bullish Points",
type=float, step=0.1)

pk_cross_weak_bearish_points = input(-0.5, title="PS Cross Weak Bearish Points",


type=float, step=0.1)
pk_cross_neutral_bearish_points = input(-1.0, title="PS Cross Neutral Bearish
Points", type=float, step=0.1)
pk_cross_strong_bearish_points = input(-2.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

// == Kumo Breakouts ==

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_score = resolve(kumo_breakout_score[1], 0)

use_kumo_breakout = input(true, title="Kumo Breakout")


kumo_breakout_weight = input(1.0, title="Kumo Breakout Importance Weight",
type=float, step=0.1)

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)

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_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

// == 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 = 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.5, title="Span Cross Weak Bullish Points",


type=float, step=0.1)
span_cross_neutral_bullish_points = input(1.0, title="Span Cross Neutral Bullish
Points", type=float, step=0.1)
span_cross_strong_bullish_points = input(2.0, title="Span Cross Strong Bullish
Points", type=float, step=0.1)

span_cross_weak_bearish_points = input(-0.5, title="Span Cross Weak Bearish


Points", type=float, step=0.1)
span_cross_neutral_bearish_points = input(-1.0, title="Span Cross Neutral Bearish
Points", type=float, step=0.1)
span_cross_strong_bearish_points = input(-2.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 = 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.5, title="Lag Line Cross Weak Bullish


Points", type=float, step=0.1)
lag_line_cross_neutral_bullish_points = input(1.0, title="Lag Line Cross Neutral
Bullish Points", type=float, step=0.1)
lag_line_cross_strong_bullish_points = input(2.0, title="Lag Line Cross Strong
Bullish Points", type=float, step=0.1)

lag_line_cross_weak_bearish_points = input(-0.5, title="Lag Line Cross Weak Bearish


Points", type=float, step=0.1)
lag_line_cross_neutral_bearish_points = input(-1.0, title="Lag Line Cross Neutral
Bearish Points", type=float, step=0.1)
lag_line_cross_strong_bearish_points = input(-2.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 = 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 = 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 ==

total_score = (use_tk_cross * tk_cross_weight * tk_cross_score) + (use_pk_cross *


pk_cross_weight * pk_cross_score) + (use_kumo_breakout * kumo_breakout_weight *
kumo_breakout_score) + (use_span_cross * span_cross_weight * span_cross_score) +
(use_lag_line * lag_line_cross_weight * lag_line_cross_score) + (use_price_location
* price_location_weight * price_placement_score)
total_score := total_score + (use_lag_line_location * lag_line_location_weight *
lag_line_placement_score)

plot(0, style=line, title="Base")


plot(total_score, linewidth=3, title="Total Score")

// debug
//plotshape((should_plot_tk_nb_inside_kumo) ? leadLine1 : na, color=black,
style=shape.arrowup, location=location.belowbar, size=size.huge)

You might also like