//@version=4
strategy("Ichimoku Cloud Strategy", shorttitle = "ICS", overlay=true)
// Ichimoku Cloud Settings
conversionLinePeriods = input(9, title="Conversion Line Period" , minval=1)
baseLinePeriods = input(26, title="Base Line Period" , minval=1)
leadingSpanPeriods = input(52, title="Leading Span B Period" , minval=1)
displacement = input(26, title="Displacement" , type=input.integer, minval=1)
// Ichimoku Cloud Calculations
conversionLine = (highest(high, conversionLinePeriods) + lowest(low, conversionLinePeriods)) / 2
baseLine = (highest(high, baseLinePeriods) + lowest(low, baseLinePeriods)) / 2
leadingSpanA = (conversionLine + baseLine) / 2
leadingSpanB = (highest(high, leadingSpanPeriods) + lowest(low, leadingSpanPeriods)) / 2
// Create Chikou Span (Lagging line)
chikouSpan = close[displacement]
plot(chikouSpan, title="Chikou Span", color=color.red, linewidth=1)
// Plot the Cloud
plot(leadingSpanA[displacement], title="Leading Span A", color=color.blue, linewidth=1, style=plot.style_line)
plot(leadingSpanB[displacement], title="Leading Span B", color=color.red, linewidth=1, style=plot.style_line)
fill(plot(leadingSpanA[displacement], color=color.blue, transp=0), plot(leadingSpanB[displacement], color=color.red, transp=0), color=(leadingSpanA[displacement] > leadingSpanB[displacement] ? color.green : color.red), transp=80)
// Entry Conditions
longCondition = crossover(conversionLine, baseLine) and close > leadingSpanA[displacement] and close > leadingSpanB[displacement] and chikouSpan > close
shortCondition = crossunder(conversionLine, baseLine) and close < leadingSpanA[displacement] and close < leadingSpanB[displacement] and chikouSpan < close
// Open Long and Short Positions
strategy.entry("Long", strategy.long, when=longCondition)
strategy.entry("Short", strategy.short, when=shortCondition)
// Exit Rules
strategy.exit("Exit Long", "Long", trail_price=baseLine, trail_offset=0)
strategy.exit("Exit Short", "Short", trail_price=baseLine, trail_offset=0)
Explain this code and what it does: //@version=3
strategy("Hull-MA_X + Ichimoku Kinko Hyo + MACD", shorttitle="HIM", overlay=true, default_qty_type=strategy.percent_of_equity, max_bars_back=1000, default_qty_value=100, calc_on_order_fills= true, calc_on_every_tick=true, pyramiding=0)
keh=input(title="Double HullMA",type=integer,defval=12, minval=1)
n2ma=2wma(close,round(keh/2))
nma=wma(close,keh)
diff=n2ma-nma
sqn=round(sqrt(keh))
n2ma1=2wma(close[1],round(keh/2))
nma1=wma(close[1],keh)
diff1=n2ma1-nma1
sqn1=round(sqrt(keh))
n1=wma(diff,sqn)
n2=wma(diff1,sqn)
b=n1>n2?lime:red
c=n1>n2?green:red
d=n1>n2?red:green
TenkanSenPeriods = input(9, minval=1, title="Tenkan Sen Periods")
KijunSenPeriods = input(24, minval=1, title="Kijun Sen Periods")
SenkouSpanBPeriods = input(51, minval=1, title="Senkou Span B Periods")
displacement = input(24, minval=1, title="Displacement")
donchian(len) => avg(lowest(len), highest(len))
TenkanSen = donchian(TenkanSenPeriods)
KijunSen = donchian(KijunSenPeriods)
SenkouSpanA = avg(TenkanSen, KijunSen)
SenkouSpanB = donchian(SenkouSpanBPeriods)
LS=close, offset = -displacement
SenkouSpanH = max(SenkouSpanA[displacement - 1], SenkouSpanB[displacement - 1])
SenkouSpanL = min(SenkouSpanA[displacement - 1], SenkouSpanB[displacement - 1])
MACD_Length = input(9)
MACD_fastLength = input(12)
MACD_slowLength = input(24)
MACD = ema(close, MACD_fastLength) - ema(close, MACD_slowLength)
aMACD = ema(MACD, MACD_Length)
Hullfast=plot(n1,color=c)
Hullslow=plot(n2,color=c)
plot(cross(n1, n2) ? n1:na, style = circles, color=b, linewidth = 4)
plot(cross(n1, n2) ? n1:na, style = line, color=d, linewidth = 3)
plot(TenkanSen, color=blue, title="Tenkan Sen", linewidth = 2)
plot(KijunSen, color=maroon, title="Kijun Sen", linewidth = 3)
plot(close, offset = -displacement, color=orange, title="Chikou Span", linewidth = 2)
sa=plot (SenkouSpanA, offset = displacement, color=green, title="Senkou Span A", linewidth = 2)
sb=plot (SenkouSpanB, offset = displacement, color=red, title="Senkou Span B", linewidth = 3)
fill(sa, sb, color = SenkouSpanA > SenkouSpanB ? green : red)
longCondition = n1>n2 and close>n2 and close>SenkouSpanH and MACD>aMACD and (TenkanSen>KijunSen or close>KijunSen)
if (longCondition)
strategy.entry("Long",strategy.long)
shortCondition = n1<n2 and close<n2 and close<SenkouSpanL and (MACD<aMACD or TenkanSen<KijunSen or close<TenkanSen or close<KijunSen)
if (shortCondition)
strategy.entry("Short",strategy.short)
closelong = n1<n2 and close<n2 and (MACD<aMACD or TenkanSen<KijunSen or close<TenkanSen or close<KijunSen or close<SenkouSpanL)
if (closelong)
strategy.close("Long")
closeshort = n1>n2 and close>n2 and (MACD>aMACD or TenkanSen>KijunSen or close>TenkanSen or close>KijunSen or close>SenkouSpanH)
if (closeshort)
strategy.close("Short")
To add a maximum stop loss of 2% to the existing strategy, you can modify the code by adding the stop loss condition in the strategy.entry() function for both long and short positions. Here's the modified Pine Script code:
//@version=3
strategy("Hull-MA_X + Ichimoku Kinko Hyo + MACD", shorttitle="HIM", overlay=true, default_qty_type=strategy.percent_of_equity, max_bars_back=1000, default_qty_value=100, calc_on_order_fills= true, calc_on_every_tick=true, pyramiding=0)
keh=input(title="Double HullMA",type=integer,defval=12, minval=1)
n2ma=2*wma(close,round(keh/2))
nma=wma(close,keh)
diff=n2ma-nma
sqn=round(sqrt(keh))
n2ma1=2*wma(close[1],round(keh/2))
nma1=wma(close[1],keh)
diff1=n2ma1-nma1
sqn1=round(sqrt(keh))
n1=wma(diff,sqn)
n2=wma(diff1,sqn)
b=n1>n2?lime:red
c=n1>n2?green:red
d=n1>n2?red:green
TenkanSenPeriods = input(9, minval=1, title="Tenkan Sen Periods")
KijunSenPeriods = input(24, minval=1, title="Kijun Sen Periods")
SenkouSpanBPeriods = input(51, minval=1, title="Senkou Span B Periods")
displacement = input(24, minval=1, title="Displacement")
donchian(len) => avg(lowest(len), highest(len))
TenkanSen = donchian(TenkanSenPeriods)
KijunSen = donchian(KijunSenPeriods)
SenkouSpanA = avg(TenkanSen, KijunSen)
SenkouSpanB = donchian(SenkouSpanBPeriods)
LS=close, offset = -displacement
SenkouSpanH = max(SenkouSpanA[displacement - 1], SenkouSpanB[displacement - 1])
SenkouSpanL = min(SenkouSpanA[displacement - 1], SenkouSpanB[displacement - 1])
MACD_Length = input(9)
MACD_fastLength = input(12)
MACD_slowLength = input(24)
MACD = ema(close, MACD_fastLength) - ema(close, MACD_slowLength)
aMACD = ema(MACD, MACD_Length)
Hullfast=plot(n1,color=c)
Hullslow=plot(n2,color=c)
plot(cross(n1, n2) ? n1:na, style = circles, color=b, linewidth = 4)
plot(cross(n1, n2) ? n1:na, style = line, color=d, linewidth = 3)
plot(TenkanSen, color=blue, title="Tenkan Sen", linewidth = 2)
plot(KijunSen, color=maroon, title="Kijun Sen", linewidth = 3)
plot(close, offset = -displacement, color=orange, title="Chikou Span", linewidth = 2)
sa=plot (SenkouSpanA, offset = displacement, color=green, title="Senkou Span A", linewidth = 2)
sb=plot (SenkouSpanB, offset = displacement, color=red, title="Senkou Span B", linewidth = 3)
fill(sa, sb, color = SenkouSpanA > SenkouSpanB ? green : red)
longCondition = n1>n2 and close>n2 and close>SenkouSpanH and MACD>aMACD and (TenkanSen>KijunSen or close>KijunSen)
if (longCondition)
strategy.entry("Long",strategy.long, stop=close*(1-0.02))
shortCondition = n1<n2 and close<n2 and close<SenkouSpanL and (MACD<aMACD or TenkanSen<KijunSen or close<TenkanSen or close<KijunSen)
if (shortCondition)
strategy.entry("Short",strategy.short, stop=close*(1+0.02))
closelong = n1<n2 and close<n2 and (MACD<aMACD or TenkanSen<KijunSen or close<TenkanSen or close<KijunSen or close<SenkouSpanL)
if (closelong)
strategy.close("Long")
closeshort = n1>n2 and close>n2 and (MACD>aMACD or TenkanSen>KijunSen or close>TenkanSen or close>KijunSen or close>SenkouSpanH)
if (closeshort)
strategy.close("Short")
In the code above, I added stop=close*(1-0.02) for long positions and stop=close*(1+0.02) for short positions in the strategy.entry() function. These lines set a stop loss of 2% below the current close price for long positions and 2% above the current close price for short positions.