Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=3
- //
- //This script combines two different strategies for combined force. Triggers can be modified to AND/OR statements.
- //Checks the HullMA on 3 correlated assets for signals. Checks Ichimoku cloud score to buy low and sell high.
- //Ichimoku score must be above Sell or below Buy threshold for HullMA event to trigger an alert.
- //Designed for Buy-Sell, not shorting. May need to be modified to fit a short strategy.
- //Benchmark defaults on BTCUSD 1H chart.
- //
- //Toggle these next 2 lines and comment out section at the bottom for study version.
- //study(title = "Triple HullMA + Ichimoku Alerts", shorttitle = "3x HullMA + Ichi AL", overlay = true)
- strategy(title="Triple HullMA + Ichimoku Cloud Signal Score", shorttitle="3x HullMA + Ichi", overlay=false, initial_capital=100000, precision=8, currency=currency.NONE, default_qty_value=100, default_qty_type=strategy.percent_of_equity, pyramiding=0, commission_value=0.2)
- // == inputs ==
- bst = input(-5.5, "Buy Threshold (Below Ichi)", type=float, step=0.1, minval=-13, maxval=100)
- sst = input(-1.9, "Sell Threshold (Above Ichi)", type=float, step=0.1, minval=-13, maxval=100)
- keh = input(title="HullMA Length (~7)", type=integer, defval=8, minval=1) //original default: 7
- secThree=input(title="Trading Pair (MA)", type=symbol, defval="COINBASE:BTCUSD")
- secOne=input(title="USD Strength (MA)", type=symbol, defval="DXY")
- secTwo=input(title="Gold Standard (MA)", type=symbol, defval="XAUUSD")
- tf = input(title="Resolution (MA)", type=resolution, defval="180") //1Week=W 1Day=D 4HR=240 3HR=180 2HR=120 (default 60)
- dt = input(0.000, title="Decision Threshold (MA)", type=float, step=0.001, minval=0.001) //Decision Threshold for confidence calculation.
- long_conf = input(0.006, title="(DT+) Long Confidence", type=float, step=0.001, minval=0.001) //Modifies dt on long trigger
- short_conf = input(0.004,title="(DT+) Short Confidence", type=float, step=0.001, minval=0.001) //Modifies dt on short trigger
- direction = input(title = "Strategy Direction", defval="Long", options=["Both", "Long", "Short"])// Strategy Direction Switch
- ot=1
- src = ohlc4[1]
- // === HullMA Triple Correlation Script Here ===
- //
- ////Currency Strength
- DXY=(security(secOne, tf, src)) //USDollar index... Quote currency strength.
- e2ma=2*wma(DXY,round(keh/2))
- ema=wma(DXY,keh)
- ediff=e2ma-ema
- esqn=round(sqrt(keh))
- e2ma1=2*wma(DXY[1],round(keh/2))
- ema1=wma(DXY[1],keh)
- ediff1=e2ma1-ema1
- esqn1=round(sqrt(keh))
- e1=wma(ediff,esqn)
- e2=wma(ediff1,esqn)
- ////Gold Strength
- XAU=(security(secTwo, tf, src)) //Gold:USD or other correlated asset.
- n2ma=2*wma(XAU,round(keh/2))
- nma=wma(XAU,keh)
- ndiff=n2ma-nma
- nsqn=round(sqrt(keh))
- n2ma1=2*wma(XAU[1],round(keh/2))
- nma1=wma(XAU[1],keh)
- ndiff1=n2ma1-nma1
- nsqn1=round(sqrt(keh))
- n1=wma(ndiff,nsqn)
- n2=wma(ndiff1,nsqn)
- ////Currency pair being traded on chart.
- CUR=(security(secThree, tf, src)) //Crypto:USD pair being traded.
- s2ma=2*wma(CUR,round(keh/2))
- sma=wma(CUR,keh)
- sdiff=s2ma-sma
- ssqn=round(sqrt(keh))
- s2ma1=2*wma(CUR[1],round(keh/2))
- sma1=wma(CUR[1],keh)
- sdiff1=s2ma1-sma1
- ssqn1=round(sqrt(keh))
- s1=wma(sdiff,ssqn)
- s2=wma(sdiff1,ssqn)
- confidence=(security(tickerid, 'D', src)-security(tickerid, 'D', src[1]))/security(tickerid, 'D', src[1])
- b=s1>s2?lime:black
- c=s1>s2?black:red
- // === Hull Ribbon Visual === Uncomment to see on chart
- //c1=plot(s1, color=b, title="hull1", linewidth=2, transp=0)
- //c2=plot(s2, color=c, title="hull1", linewidth=2, transp=0)
- //fill(c1, c2, color=c, transp=75)
- // === END ===
- // === Ichimoku Script Here ===
- //This strategy could be improved by having it check the score from another timeframe for multiple confirmations.
- //This should be improved with some way to determine if the 'bst' & 'sst' are being reached on the way up or down
- //to prevent buying at the beginning of the dip versus at the end...
- //The bst could trigger a trailing limit. The sst could trigger a Trailing Stop Loss.
- //fall = input(3, "Falling Score Take Profit", type=float, step=0.1, minval=-12, maxval=100)
- conversionPeriods = input(20, minval=1, title="Conversion Line Periods"),
- basePeriods = input(60, minval=1, title="Base Line Periods")
- laggingSpan2Periods = input(120, minval=1, title="Lagging Span 2 Periods"),
- displacement = input(60, minval=1, title="Displacement")
- //Used to plot lines on chart, should replace bst & sst. (update)
- usl = input(5, minval=1, title="Upper Line Plot", type=float, step=0.1),
- lsl = input(-5, maxval=-1, title="Lower Line Plot", type=float, step=0.1),
- // == 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 == Uncomment to see on chart.
- // // 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 = 0.0
- 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 = 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.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 = 0.0
- 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 = 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.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 = 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.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 = 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 ==
- use_tk_cross_bool = use_tk_cross ? 1 : 0
- use_pk_cross_bool = use_pk_cross ? 1 : 0
- use_kumo_breakout_bool = use_kumo_breakout ? 1 : 0
- use_span_cross_bool = use_span_cross ? 1 : 0
- use_lag_line_bool = use_lag_line ? 1 : 0
- use_price_location_bool = use_price_location ? 1 : 0
- use_lag_line_location_bool = use_lag_line_location ? 1 : 0
- total_score = (use_tk_cross_bool * tk_cross_weight * tk_cross_score) + (use_pk_cross_bool * pk_cross_weight * pk_cross_score) + (use_kumo_breakout_bool * kumo_breakout_weight * kumo_breakout_score) + (use_span_cross_bool * span_cross_weight * span_cross_score) + (use_lag_line_bool * lag_line_cross_weight * lag_line_cross_score) + (use_price_location_bool * price_location_weight * price_placement_score)
- total_score := total_score + (use_lag_line_location_bool * lag_line_location_weight * lag_line_placement_score)
- upperSignalLine = hline(usl, title="Upper Sig Line")
- lowerSignalLine = hline(lsl, title="Lower Sig Line")
- plotColor = total_score > 0 ? total_score > usl ? green : lime : total_score < lsl ? maroon : red
- plot(usl, title="Upper Signal Line", style=line, color=plotColor)
- plot(lsl, title="Lower Signal Line", style=line, color=plotColor)
- plot(0, style=line, title="Base")
- plot(total_score, linewidth=3, style=line, color=plotColor, title="Total Score")
- fill(upperSignalLine, lowerSignalLine, color=purple, transp=90)
- // ===Original Trigger Conditions===
- //
- //Change the "usl" variable to any fixed number to test sensitivity.
- //Ex.: buy @ -6, sell @ 8 to catch cycles.
- //Added bst & sst inputs at top of script.
- //longCondition = crossover(total_score, bst) //usl)
- //longClose = crossunder(total_score, sst) //usl)
- //take_profit = falling(total_score, 3) //optional Take Profit function
- //
- //if time>timestamp(2017, 11, 01, 00, 00)
- //strategy.entry("Long", strategy.long, when=longCondition)
- //
- //The two options below can be toggled or stacked.
- //strategy.close("Long", when = take_profit) //Take Profit option above.
- //strategy.close_all(when=longClose)
- //
- // === /END
- // === Conditions ===
- ////////////////////////////////////////////////////////////////////////////
- long_entry = (total_score>bst) and e1<e2 and n1>n2 and s1>s2 and confidence>dt+long_conf //and e1<e2 and n1>n2 and s1>s2 and confidence>dt+0.0 //crossover(total_score, bst) //and buy_tl //Crossover is for Ichimoku, the rest is HullMA
- short_entry = (total_score<sst) and e1>e2 and n1<n2 and s1<s2 and confidence>dt+short_conf //and e1>e2 and n1<n2 and s1<s2 and confidence<dt+0.0 //crossunder(total_score, sst) //Crossover is for Ichimoku, the rest is HullMA
- long_exit = short_entry //or (e1>e2 and confidence<dt+0.0) //or falling(total_score, fall) //and sell_tl //optional Take Profit function & trailing limit
- short_exit = long_entry //or (e1<e2 and confidence>dt+0.0)
- // === /END
- ///////////////////////////////////////////////////////////////////////////
- // init these values here, they will get updated later as more decisions are made
- last_long_close = na
- last_short_close = na
- // === Long position detection ===
- // longs open
- longo = 0
- longo := nz(longo[1])
- // longs closed
- longc = 0
- longc := nz(longc[1])
- if long_entry
- longo := longo + 1
- longc := 0
- if long_exit
- longc := longc + 1
- longo := 0
- // === /END
- // === Short position detection ===
- shorto = 0
- shorto := nz(shorto[1])
- shortc = 0
- shortc := nz(shortc[1])
- if short_entry
- shorto := shorto + 1
- shortc := 0
- if short_exit
- shortc := shortc + 1
- shorto := 0
- // === /END
- // === Pyramiding Settings ===
- //pyr = input(1, title="Pyramiding Setting")
- pyr = 1
- longCondition = long_entry and longo <= pyr
- longX = long_exit and longc <= pyr
- shortCondition = short_entry and shorto <=pyr
- shortX = short_exit and shortc <=pyr
- // === /END
- // === Get Last Position Price ===
- last_open_longCondition = na
- last_open_shortCondition = na
- // last open prices
- last_open_longCondition := longCondition ? close : nz(last_open_longCondition[1])
- last_open_shortCondition := shortCondition ? close : nz(last_open_shortCondition[1])
- // === /END
- // === Check For Long/Short ===
- last_longCondition = na
- last_shortCondition = na
- // last open times
- last_longCondition := longCondition ? time : nz(last_longCondition[1])
- last_shortCondition := shortCondition ? time : nz(last_shortCondition[1])
- last_longClose = longX ? time : nz(last_long_close[1])
- last_shortClose = shortX ? time : nz(last_short_close[1])
- in_longCondition = last_longCondition > last_shortCondition and last_longCondition >= last_longClose
- in_shortCondition = last_shortCondition > last_longCondition and last_shortCondition >= last_shortClose
- // === /END
- // === Stop Loss (Long) ===
- isSLl = input(false, "Stop Loss (Long)")
- sll = input(0, "Stop Loss %", type=float, step=0.2, minval=0, maxval=100) / 100
- long_call_sl = last_open_longCondition * (1 - sll)
- long_sl = isSLl and low <= long_call_sl and longCondition == 0
- // === /END
- // === Stop Loss (Short) ===
- isSLs = input(false, "Stop Loss (Short)")
- sls = input(0, "Stop Loss %", type=float, step=0.2, minval=0, maxval=100) / 100
- short_call_sl = last_open_shortCondition * (1 + sls)
- short_sl = isSLs and high >= short_call_sl and shortCondition == 0
- // === /END
- // === Trailing Stop ===
- last_high = na
- last_low = na
- last_high := in_longCondition ? (na(last_high[1]) or high > nz(last_high[1])) ? high : nz(last_high[1]) : na
- last_low := in_shortCondition ? (na(last_low[1]) or low < nz(last_low[1])) ? low : nz(last_low[1]) : na
- isTSl = input(false, "Trailing Stop Long")
- tsil = input(19, "Activate Trailing Stop % Long", type=float, step=1, minval=0, maxval=100) / 100
- tsl = input(2, "Trailing Stop % Long", type=float, step=1, minval=0, maxval=100) / 100
- long_call_ts = last_high * (1 - tsl)
- long_call_tsi = last_open_longCondition * (1 + tsil)
- long_ts = isTSl and not na(last_high) and low <= long_call_ts and longCondition == 0 and last_high >= long_call_tsi
- isTSs = input(false, "Trailing Stop Short")
- tsis = input(19, "Activate Trailing Stop % Short", type=float, step=1, minval=0, maxval=100) / 100
- tss = input(2, "Trailing Stop % Short", type=float, step=1, minval=0, maxval=100) / 100
- short_call_ts = last_low * (1 + tss)
- short_call_tsi = last_open_shortCondition * (1 - tsis)
- short_ts = isTSs and not na(last_low) and high >= short_call_ts and shortCondition == 0 and last_low <= short_call_tsi
- // === /END
- // === Trailing Limit ===
- tl_last_high = na
- tl_last_low = na
- tl_last_high := shortCondition ? (na(tl_last_high[1]) or high > nz(tl_last_high[1])) ? high : nz(tl_last_high[1]) : na //as of now script will only keep track of highest high/lowest low when short condition/long condition are satisfied
- tl_last_low := longCondition ? (na(tl_last_low[1]) or low < nz(tl_last_low[1])) ? low : nz(tl_last_low[1]) : na //that leaves the possibility that short/long condition with activate trailing limit, but short/long condition might become
- tl_isTSl = input(true, "Trailing Limit Long") //false before trailing limit is activated, in which case we do not end up trading...
- //tl_tsil = input(2, "Activate Trailing Limit % Long", type=float, step=1, minval=0, maxval=100) / 100
- tl_tsl = input(2, "Trailing Limit % Long", type=float, step=1, minval=0, maxval=100) / 100
- tl_long_call_ts = tl_last_low * (1 + tl_tsl)
- //tl_long_call_tsi = last_open_longCondition * (1 + tl_tsil)
- tl_long_ts = tl_isTSl and not na(tl_last_high) and high >= tl_long_call_ts
- tl_isTSs = input(true, "Trailing Limit Short")
- //tl_tsis = input(19, "Activate Trailing Stop % Short", type=float, step=1, minval=0, maxval=100) / 100
- tl_tss = input(2, "Trailing Limit % Short", type=float, step=1, minval=0, maxval=100) / 100
- tl_short_call_ts = tl_last_high * (1 - tl_tss)
- //tl_short_call_tsi = last_open_shortCondition * (1 - tl_tsis)
- tl_short_ts = tl_isTSs and not na(tl_last_low) and low <= tl_short_call_ts
- // === /END
- // === Create Single Close For All Closing Conditions ===
- closelong = long_sl or long_ts or longX or tl_short_ts //Add 'or sell_tl' for Trailing Limit? DEBUG
- closeshort = short_sl or short_ts or shortX or tl_long_ts
- // Get Last Close
- last_long_close := closelong ? time : nz(last_long_close[1])
- last_short_close := closeshort ? time : nz(last_short_close[1])
- // Check For Close Since Last Open
- if closelong and last_long_close[1] > last_longCondition
- closelong := 0
- if closeshort and last_short_close[1] > last_shortCondition
- closeshort := 0
- // === /END
- ////////////////////////////////////////////////////////////////////////////
- // === Alarm Settings ===
- alertcondition(tl_long_ts, title='LONG', message='LONG')
- alertcondition(closelong==1, title='EXIT LONG', message='EXIT LONG')
- alertcondition(tl_short_ts, title='SHORT', message='SHORT')
- alertcondition(closeshort==1, title='EXIT SHORT', message='EXIT SHORT')
- // === /END
- ////////////////////////////////////////////////////////////////////////////
- // === Debugs Here ===
- //Remove "//" To Check/Debug The Code Above
- // Signal Shapes
- //plotshape(longCondition[1]==1, title='LONG', style=shape.triangleup, size=size.large, color=#02CB80, location= location.belowbar)
- //plotshape(shortCondition[1]==1, title='SHORT', style=shape.triangledown, size=size.large, color=#DC143C, location=location.abovebar)
- //plotshape(shortCondition[1]==0 and closelong[1]==1, title='EXIT LONG', style=shape.xcross, color=#02CB80, location=location.belowbar, transp=0)
- //plotshape(longCondition[1]==0 and closeshort[1]==1, title='EXIT SHORT', style=shape.xcross, color=#DC143C, location=location.abovebar, transp=0)
- // SL Plot
- //slColor = (isSLl or isSLs) and (in_longCondition or in_shortCondition) ? red : white
- //plot(isSLl and in_longCondition ? long_call_sl : na, "Long SL", slColor, style=3, linewidth=2)
- //plot(isSLs and in_shortCondition ? short_call_sl : na, "Short SL", slColor, style=3, linewidth=2)
- // TP Plot
- //tpColor = isTP and (in_longCondition or in_shortCondition) ? purple : white
- //plot(isTP and in_longCondition ? long_call_tp : na, "Long TP", tpColor, style=3, linewidth=2)
- //plot(isTP and in_shortCondition ? short_call_tp : na, "Short TP", tpColor, style=3, linewidth=2)
- // TS Plot
- //tsColor = (isTSl or isTSs) and (in_longCondition or in_shortCondition) ? orange : white
- //tsiColor = (isTSl or isTSs) and (in_longCondition or in_shortCondition) ? white : orange
- //plot(isTSl and in_longCondition ? long_call_tsi : na, "Long Trailing", tsiColor, style=3, linewidth=2)
- //plot(isTSs and in_shortCondition ? short_call_tsi : na, "Short Trailing", tsiColor, style=3, linewidth=2)
- //plot(isTSl and in_longCondition and last_high > long_call_tsi ? long_call_ts : na, "Long Trailing", tsColor, style=2, linewidth=2)
- //plot(isTSs and in_shortCondition and last_low < short_call_tsi ? short_call_ts : na, "Short Trailing", tsColor, style=2, linewidth=2)
- // === /END
- ////////////////////////////////////////////////////////////////////////////
- // //
- // REMOVE THE CODE BELOW FOR STUDY CONVERSION //
- // //
- ////////////////////////////////////////////////////////////////////////////
- // === Backtesting Dates ===
- testPeriodSwitch = input(true, "Custom Backtesting Dates")
- testStartYear = input(2017, "Backtest Start Year")
- testStartMonth = input(6, "Backtest Start Month")
- testStartDay = input(1, "Backtest Start Day")
- testPeriodStart = timestamp(testStartYear,testStartMonth,testStartDay,0,0)
- testStopYear = input(2018, "Backtest Stop Year")
- testStopMonth = input(12, "Backtest Stop Month")
- testStopDay = input(31, "Backtest Stop Day")
- testPeriodStop = timestamp(testStopYear,testStopMonth,testStopDay,0,0)
- testPeriod() =>
- time >= testPeriodStart and time <= testPeriodStop ? true : false
- isPeriod = testPeriodSwitch == true ? testPeriod() : true
- // === /END
- // === Strategy ===
- if isPeriod and direction=="Both"
- if (tl_long_ts)
- strategy.entry("Long",strategy.long)
- if (closelong) and not shortCondition
- strategy.close("Long")
- if (tl_short_ts)
- strategy.entry("Short",strategy.short)
- if (closeshort) and not longCondition
- strategy.close("Short")
- if isPeriod and direction=="Long"
- if (tl_long_ts)
- strategy.entry("Long",strategy.long)
- if (closelong)
- strategy.close("Long")
- if isPeriod and direction=="Short"
- if (tl_short_ts)
- strategy.entry("Short",strategy.short)
- if (closeshort)
- strategy.close("Short")
- // === /END
- ////////////////////////////////////////////////////////////////////////////
- // //
- // ULTIMATE PINE INJECTOR V1.2 //
- // //
- //////////////////////===ANION=CODE=END====/////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement