Ichimoku + CDC Action Zone +ATR Trailing stop

Fukuiz · study · 286 行 · 点赞 492 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=4
 
 
study(title="Ichimoku + CDC Action Zone by fukuiz" , shorttitle="FKS",overlay=true)
 
 
//ichimoku##################################################
 
//color
colorblue=#3300CC
colorred=#993300
colorwhite=#FFFFFF
colorgreen=#CCCC33
colorpink=#CC6699
colorpurple=#6633FF
 
//switch
switch1=input(false, title="Chikou")
switch2=input(false, title="Tenkan")
switch3=input(false, title="Kijun")
 
middleDonchian(Length) =>
    lower = lowest(Length)
    upper = highest(Length)
    avg(upper, lower)
    
conversionPeriods = input(9, minval=1),
basePeriods = input(26, minval=1)
laggingSpan2Periods = input(52, minval=1),
displacement = input(26, minval=1)
Tenkan = middleDonchian(conversionPeriods)
Kijun =  middleDonchian(basePeriods)
xChikou = close 
SenkouA = middleDonchian(laggingSpan2Periods)
SenkouB = (Tenkan[basePeriods] + Kijun[basePeriods]) / 2
 
A = plot(SenkouA[displacement], color=colorpurple, title="SenkouA")
B = plot(SenkouB, color=colorgreen, title="SenkouB")
plot(switch1?xChikou:na, color=colorpink , title="Chikou", offset = -displacement )
plot(switch2?Tenkan:na, color=colorred, title="Tenkan")
plot(switch3?Kijun:na, color=colorblue, title="Kijun")
fill(A, B, color=colorgreen)
 
//CDC action zone#################################################
xsrc = input(title="Source Data",type=input.source, defval=close)
xprd1 = input(title="Fast EMA period", type=input.integer, defval=12)
xprd2 = input(title="Slow EMA period", type=input.integer, defval=26)
xsmooth = input(title="Smoothing period (1 = no smoothing)", type=input.integer, defval=1)
fillSW = input(title="Paint Bar Colors", type=input.bool, defval=true)
fastSW = input(title="Show fast moving average line", type=input.bool, defval=true)
slowSW = input(title="Show slow moving average line", type=input.bool, defval=true)
labelSwitch =  input(title="Turn on assistive text", type=input.bool, defval=true)
plotSigsw = input(title="Plot Buy/Sell Signals? ", type=input.bool, defval=true)
plotRibsw = input(title="Plot Buy/Sell Ribbon", type=input.bool, defval=false)
plotRibbonPos = input(title="Ribbon Position", options=["Top", "Bottom"], defval="Top")
 
xfixtf = input(title="** Use Fixed time frame Mode (advanced) **", type=input.bool, defval=false)
xtf = input(title="** Fix chart to which time frame ? **)", type=input.resolution, defval="D")
 
plotSig2sw = input(title="Plot momentum based Buy/Sell Signals? ", type=input.bool, defval=false)
plotSig2lv = input(title="Set signal threshold (higher = stricter)", type=input.integer, defval=1, minval=0, maxval=1)
 
//****************************************************************************//
//Calculate Indicators
 
xPrice = ema(xsrc,xsmooth)
FastMA = xfixtf ? 
     ema(security(syminfo.tickerid, xtf, ema(xsrc,xprd1), 
         gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_on), xsmooth)
     :
     ema(xPrice,xprd1)
SlowMA = xfixtf ? 
     ema(security(syminfo.tickerid, xtf, ema(xsrc,xprd2), 
         gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_on), xsmooth)
     :
     ema(xPrice,xprd2)
 
Bull = FastMA > SlowMA
Bear = FastMA < SlowMA
 
//****************************************************************************//
// Define Color Zones
 
Green = Bull and xPrice>FastMA // Buy
Blue = Bear and xPrice>FastMA and xPrice>SlowMA //Pre Buy 2
LBlue = Bear and xPrice>FastMA and xPrice<SlowMA //Pre Buy 1
 
Red = Bear and xPrice < FastMA // Sell
Orange = Bull and xPrice < FastMA and xPrice < SlowMA // Pre Sell 2
Yellow = Bull and xPrice < FastMA and xPrice > SlowMA // Pre Sell 1
 
//****************************************************************************//
// Display color on chart
 
 
bColor = Green ? color.green : 
     Blue  ? color.blue : 
     LBlue ? color.aqua : 
     Red ? color.red : 
     Orange ? color.orange : 
     Yellow ? color.yellow : 
     color.black
barcolor(color=fillSW? bColor : na)
 
//****************************************************************************//
// Display MA lines
 
//FastL = plot(fastSW ? FastMA : na,"Fast EMA",color=color.red)
//SlowL = plot(slowSW ? SlowMA : na,"Slow EMA",color=color.blue)
//fillcolor = Bull ? color.green : Bear ? color.red : color.black
//fill(FastL,SlowL,fillcolor)
 
//****************************************************************************//
// Define Buy and Sell condition
// This is only for thebasic usage of CDC Actionzone (EMA Crossover) 
// ie. Buy on first green bar and sell on first red bar
 
buycond = Green and Green[1]==0
sellcond = Red and Red[1]==0
 
bullish = barssince(buycond) < barssince(sellcond)
bearish = barssince(sellcond) < barssince(buycond)
 
buy= bearish[1]and buycond
sell= bullish[1] and sellcond
 
bColor_BullBear = bullish ? color.green : bearish ? color.red : color.black
 
//****************************************************************************//
// Plot Buy and Sell point on chart
 
plotshape(plotSigsw ? buy : na,
     style=shape.circle,
     title = "Buy Signal",
     location = location.belowbar,
     //size = size.small,
     color = color.green)
plotshape(plotSigsw ? sell : na,
     style=shape.circle,
     title = "Sell Signal",
     location=location.abovebar,
     //size = size.small,
     color = color.red)
 
// Display Buy/Sell Ribbon
 
 
plotshape(plotRibsw ? plotRibbonPos == "Top" ? close : na : na,
     style=shape.square,
     title = "Buy/Sell Ribbon", 
     location = location.top, 
     color = bColor_BullBear)
     
plotshape(plotRibsw ? plotRibbonPos == "Bottom" ? close : na : na,
     style=shape.square,
     title = "Buy/Sell Ribbon", 
     location = location.bottom, 
     color = bColor_BullBear)
    
 
//****************************************************************************//
// Label
 
labelstyle = close>SlowMA ? label.style_labeldown : label.style_labelup
labelyloc = close>SlowMA ? yloc.abovebar : yloc.belowbar
labeltcolor = buy? color.black : 
     sell ? color.white :
     close > close[1] ? color.green : 
     color.red
labelbgcolor = buy ? color.green : sell ? color.red : color.silver
labeltext = buy ? "BUY next bar\n" : sell ? "SELL next bar\n" : " "
trendText = bullish ? "bullish" : bearish ? "bearish" : "sideways"
 
 
l1 = label.new(bar_index,na,
     text=labeltext+syminfo.ticker + " "+tostring(close) + " " + syminfo.currency + "\n currently in a " + trendText + " trend \n" ,
     color = labelbgcolor, 
     textcolor=labeltcolor, 
     yloc=labelyloc, 
     style=labelstyle)
 
label.delete(labelSwitch ? l1[1] : l1)
 
// Momentum Signal using StochRSI
 
// Adds a momentum based signal following trends to the script
// Default is hidden, only use with caution
// Parameters for STOCH RSI is hard-coded to avoid cluttering the input screen further
// If you need to change anything, make a copy of the code and change it.
// Inputs are commented out, to enable them comment out the hard coded variables first!
 
// fixed inputs //
 
smoothK = 3
smoothD = 3
RSIlen = 14
STOlen = 14
SRsrc = close
OSlevel = 30
OBlevel = 70
 
// User inputs // // COMMENT ABOVE VARIABLES FIRST!! 
 
// smoothK = input(3,"StochRSI smooth K",type=input.integer,minval=1)
// smoothD = input(3,"StochRSI smooth D",type=input.integer,minval=1)
// RSIlen = input(14,"RSI length",type=input.integer,minval=1)
// STOlen = input(14,"Stochastic length",type=input.integer,minval=1)
// SRsrc = input(close,"Source for StochasticRSI",type=input.source)
// OSlevel = input(30,"Oversold Threshold",type=input.float,minval=0.00)
// OBlevel = input(70,"Oversold Threshold",type=input.float,minval=0.00)
 
// calculations //
rsi1 = rsi(SRsrc,RSIlen)
k = sma(stoch(rsi1,rsi1,rsi1,STOlen),smoothK)
d = sma(k,smoothD)
 
// storsiBuySig =  if bullish
//     if (d < OSlevel and crossover(k,d))
//         3
//     else if crossover(k,OSlevel)
//         2
//     else if d > OSlevel and crossover(k,d)
//         1
//     else
//         0
// else
//     0
 
storsiBuySig = iff(bullish,
                     iff(d < OSlevel and crossover(k,d),
                         2,
                         iff(d > OSlevel and crossover(k,d),
                             1,
                             0)),
                     0)
 
storsiSellSig = iff(bearish,
                     iff(d > OBlevel and crossunder(k,d),
                         2,
                         iff(d < OBlevel and crossunder(k,d),
                             1,
                             0)),
                     0)
 
plotshape(plotSig2sw ? storsiBuySig > plotSig2lv ? storsiBuySig : na : na,
     "Buy more signals", style=shape.triangleup,
     location=location.belowbar, color = color.teal)
plotshape(plotSig2sw ? storsiSellSig > plotSig2lv ? storsiSellSig : na : na,
     "Sell more signals", style=shape.triangledown,
     location=location.abovebar, color = color.orange)
 
 
//****************************************************************************//
// Alert conditions
 
alertcondition(buy, 
     title="*Buy Alert",
     message= "Buy {{exchange}}:{{ticker}}")
     
alertcondition(sell, 
     title="*Sell Alert",
     message= "Sell {{exchange}}:{{ticker}}")
     
alertcondition(bullish, 
     title="is Bullish")
    
alertcondition(bearish, 
     title="is Bearish")
     
alertcondition(Green, 
     title="is Green")
     
alertcondition(Blue, 
     title="is Blue (Strong Rally)")
     
alertcondition(LBlue, 
     title="is Light Blue (Rally)")
     
alertcondition(Red, 
     title="is Red")
     
alertcondition(Orange, 
     title="is Orange (Strong Dip)")
     
alertcondition(Yellow, 
     title="is Yellow (Dip)")

← 返回列表