Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
- // © imjoy4u
- //@version=4
- strategy("TSI-Bnf", overlay=true, initial_capital = 500000, commission_value=0.0116, commission_type = strategy.commission.percent )
- amplitude = input(title="Amplitude", defval=3)
- channelDeviation = input(title="Channel Deviation", defval=2)
- showArrows = input(title="Show Arrows", defval=true)
- showChannels = input(title="Show Channels", defval=true)
- startDate = input(title="Start Date", type=input.integer,
- defval=1, minval=1, maxval=31)
- startMonth = input(title="Start Month", type=input.integer,
- defval=1, minval=1, maxval=12)
- startYear = input(title="Start Year", type=input.integer,
- defval=2018, minval=2018, maxval=2100)
- endDate = input(title="End Date", type=input.integer,
- defval=31, minval=1, maxval=31)
- endMonth = input(title="End Month", type=input.integer,
- defval=12, minval=1, maxval=12)
- endYear = input(title="End Year", type=input.integer,
- defval=2021, minval=1800, maxval=2100)
- rLong = input(title="RSI Long", defval=30)
- rShort = input(title="RSI Short", defval=70)
- // study("True Strength Indicator", shorttitle="TSI", format=format.price, precision=4, resolution="")
- long = input(title="Long Length", type=input.integer, defval=25)
- short = input(title="Short Length", type=input.integer, defval=13)
- signal = input(title="Signal Length", type=input.integer, defval=13)
- // TradeTime = input(title="Trade Timings",defval="0930-1515", type=input.session)
- // SqoffTime = input(title="Squareoff Timings",defval="1520-1530", type=input.session)
- // Barsinsession(TradeTime) => time(timeframe.period,TradeTime) != 0
- // Insession = Barsinsession(TradeTime) ? 1 : 0
- // endofsession = Insession == 0 and Insession[1] == 1
- // Sqsession = Barsinsession(SqoffTime) ? 1 : 0
- // SqTime = Sqsession == 1 and Sqsession[1] ==0
- afterStartDate = (time >= timestamp(syminfo.timezone, startYear, startMonth, startDate, 0, 0)) and (time <= timestamp(syminfo.timezone, endYear, endMonth, endDate, 0, 0))
- // TLong = input(title="Trail Long %", defval=0.10, minval=0.00)
- // TShort = input(title="Trail Short%", defval=0.10, minval=0.00)
- var int trend = 0
- var int nextTrend = 0
- var float maxLowPrice = nz(low[1], low)
- var float minHighPrice = nz(high[1], high)
- var int trig = 0
- var float up = 0.0
- var float down = 0.0
- float atrHigh = 0.0
- float atrLow = 0.0
- float arrowUp = na
- float arrowDown = na
- atr2 = atr(100) / 2
- dev = channelDeviation * atr2
- highPrice = high[abs(highestbars(amplitude))]
- lowPrice = low[abs(lowestbars(amplitude))]
- highma = sma(high, amplitude)
- lowma = sma(low, amplitude)
- if nextTrend == 1
- maxLowPrice := max(lowPrice, maxLowPrice)
- if highma < maxLowPrice and close < nz(low[1], low)
- trend := 1
- nextTrend := 0
- minHighPrice := highPrice
- else
- minHighPrice := min(highPrice, minHighPrice)
- if lowma > minHighPrice and close > nz(high[1], high)
- trend := 0
- nextTrend := 1
- maxLowPrice := lowPrice
- if trend == 0
- if not na(trend[1]) and trend[1] != 0
- up := na(down[1]) ? down : down[1]
- arrowUp := up - atr2
- else
- up := na(up[1]) ? maxLowPrice : max(maxLowPrice, up[1])
- atrHigh := up + dev
- atrLow := up - dev
- else
- if not na(trend[1]) and trend[1] != 1
- down := na(up[1]) ? up : up[1]
- arrowDown := down + atr2
- else
- down := na(down[1]) ? minHighPrice : min(minHighPrice, down[1])
- atrHigh := down + dev
- atrLow := down - dev
- ht = trend == 0 ? up : down
- var color buyColor = color.blue
- var color sellColor = color.red
- htColor = trend == 0 ? buyColor : sellColor
- htPlot = plot(ht, title="TrendSwitch", linewidth=2, color=htColor)
- atrHighPlot = plot(showChannels ? atrHigh : na, title="ATR High", style=plot.style_circles, color=sellColor)
- atrLowPlot = plot(showChannels ? atrLow : na, title="ATR Low", style=plot.style_circles, color=buyColor)
- fill(htPlot, atrHighPlot, title="ATR High Ribbon", color=sellColor)
- fill(htPlot, atrLowPlot, title="ATR Low Ribbon", color=buyColor)
- buySignal = not na(arrowUp) and (trend == 0 and trend[1] == 1)
- sellSignal = not na(arrowDown) and (trend == 1 and trend[1] == 0)
- plotshape(showArrows and buySignal ? atrLow : na, title="Arrow Up", style=shape.triangleup, location=location.belowbar, size=size.small, color=buyColor)
- plotshape(showArrows and sellSignal ? atrHigh : na, title="Arrow Down", style=shape.triangledown, location=location.abovebar, size=size.small, color=sellColor)
- stopL= strategy.position_size >0 ? strategy.position_avg_price - 60 : atrLow
- stopS= strategy.position_size <0 ? strategy.position_avg_price + 60 : atrHigh
- price = close
- double_smooth(src, long, short) =>
- fist_smooth = ema(src, long)
- ema(fist_smooth, short)
- pc = change(price)
- double_smoothed_pc = double_smooth(pc, long, short)
- double_smoothed_abs_pc = double_smooth(abs(pc), long, short)
- tsi_value = 100 * (double_smoothed_pc / double_smoothed_abs_pc)
- sign = ema(tsi_value, signal)
- sign1 = tsi_value[4]
- sS= (tsi_value < sign)
- bS= (tsi_value > sign)
- buyT = (bS[1] == 0 and bS == 1)
- sellT = (sS[1] == 0 and sS == 1)
- trig := buyT == 1 ? 1 : sellT == 1 ? -1 : 0
- plot(trig)
- plot(vwap, title="VWAP", color=color.purple, linewidth=2)
- // plot(bS, title="buyT")
- plot(pc/close*100,title="pc")
- // plot(tsi_value, color=#2962FF)
- // plot(ema(tsi_value, signal), color=#E91E63)
- // hline(0, title="Zero", color=#787B86)
- longCondition = (trig== 1) and afterStartDate
- shortCondition= (trig==-1) and afterStartDate
- //if (longCondition)
- // if (afterStartDate and longCondition)
- strategy.entry("Long", strategy.long, qty = 2, when= longCondition , comment="B")
- strategy.exit("LE1", from_entry="Long", qty_percent = 50, trail_points = strategy.position_avg_price * 0.01/syminfo.mintick)
- strategy.entry("Short", strategy.short, qty =2, when=shortCondition, comment="S")
- strategy.exit("SE1", from_entry="Short", qty_percent = 50, trail_points = strategy.position_avg_price * 0.01/syminfo.mintick)
- // strategy.exit("SE", from_entry="Short", trail_price = strategy.position_avg_price * 0.99)
- // strategy.exit("Exit", profit=(close*0.01/syminfo.mintick), loss=(150/syminfo.mintick), comment="Trail" )
- // strategy.exit("LX", "LE", profit = (close * 0.005/syminfo.mintick), stop= (close * 0.005/syminfo.mintick) , comment="S" )
- // shortCondition = (trig[1] == -1)
- // if (afterStartDate and shortCondition)
- // strategy.entry("SE", strategy.short, comment="S")
- // strategy.exit("SX", "SE", profit = (close * 0.01/syminfo.mintick), stop= strategy.position_avg_price* 0.01/syminfo.mintick, comment="B")
- // plot(close*0.005/syminfo.mintick)
- //barOpenTime = time
- //tt=timestamp(year(time), month(time), dayofmonth(time), hour(time), 2)
- //if ( hour(time) == 15 and minute(time) > 26 )
- // strategy.close("LE", comment="SCM")
- //if ( hour(time) == 15 and minute(time) > 26 )
- // strategy.close("SE", comment="SCM")
- EMA20 = ema(close, 20)
- plot(EMA20, title="EMA", color=color.white, linewidth=1)
Advertisement
Add Comment
Please, Sign In to add comment