Guest User

Untitled

a guest
Dec 11th, 2023
265
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.34 KB | None | 0 0
  1. //@version=5
  2. strategy("TPI LIKE BTC STRAT", initial_capital=10000, slippage=1, default_qty_value=100, pyramiding=0, default_qty_type=strategy.percent_of_equity, process_orders_on_close=true, shorttitle="TPI STRAT BTC", overlay=true)
  3.  
  4. // Backtest Code
  5. useDateFilter = input.bool(true, title="Filter Date Range of Backtest",
  6. group="Backtest Time Period")
  7. backtestStartDate = input.time(timestamp("1 Jan 2018"),
  8. title="Start Date", group="Backtest Time Period")
  9. backtestEndDate = input.time(timestamp("1 Jan 2092"),
  10. title="End Date", group="Backtest Time Period")
  11. inDateRange = not useDateFilter or (time >= backtestStartDate and
  12. time < backtestEndDate)
  13.  
  14.  
  15. // BOLLINGER BANDS %
  16.  
  17. //Inputs
  18. timeframebb = input.string("2D", "Days", ["1D", "2D", "3D", "4D", "5D", "6D", "7D"],group = "BB%")
  19. length_bb = input.int(20, title="BB Length",group = "BB%")
  20. src_bb = input(close, title="BB Source",group = "BB%")
  21.  
  22. //Calculations
  23. mult = 2
  24. basis = ta.sma(src_bb, length_bb)
  25. dev = mult * ta.stdev(src_bb, length_bb)
  26. upper_bb = basis + dev
  27. lower_bb = basis - dev
  28. bb_percent = (src_bb - lower_bb) / (upper_bb - lower_bb)
  29.  
  30. //Conditions
  31. bb_Long = bb_percent > 0.5
  32. bb_Short = bb_percent < 0.5
  33. bb_Long_cn = request.security(syminfo.tickerid,timeframebb,bb_Long)
  34. bb_Short_cn = request.security(syminfo.tickerid,timeframebb,bb_Short)
  35.  
  36. var vbb = 0
  37. if bb_Long_cn
  38. vbb := 1
  39. if bb_Short_cn
  40. vbb := -1
  41.  
  42. ////////////////////////// RSI ///////////////////////////////////
  43. timeframersi = input.string("2D", "Days", ["1D", "2D", "3D", "4D", "5D", "6D", "7D"],group = "RSI")
  44. ma(source, length, type) =>
  45. switch type
  46. "SMA" => ta.sma(source, length)
  47. "EMA" => ta.ema(source, length)
  48.  
  49. rsiLengthInput = input.int(14, minval=1, title="RSI Length", group="RSI")
  50. rsiSourceInput = input.source(close, "Source", group="RSI")
  51. maTypeInput = input.string("SMA", title="MA Type", options=["SMA", "Bollinger Bands", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="RSI")
  52. maLengthInput = input.int(14, title="MA Length", group="RSI")
  53. upRSI = ta.rma(math.max(ta.change(rsiSourceInput), 0), rsiLengthInput)
  54. downRSI = ta.rma(-math.min(ta.change(rsiSourceInput), 0), rsiLengthInput)
  55. rsi = downRSI == 0 ? 100 : upRSI == 0 ? 0 : 100 - (100 / (1 + upRSI / downRSI))
  56. rsiMA = ma(rsi, maLengthInput, maTypeInput)
  57. RSILong = rsi > rsiMA
  58. RSIShort = rsi < rsiMA
  59. RSILong1 = ta.crossover(rsi,rsiMA)
  60. RSIShort1 = ta.crossunder(rsi,rsiMA)
  61. RSILong2 = rsiMA > 50
  62. RSIShort2 = rsiMA < 50
  63. RSILONG_cn1 = request.security(syminfo.tickerid,timeframersi,RSILong)
  64. RSISHORT_cn1 = request.security(syminfo.tickerid,timeframersi,RSIShort)
  65. RSILONG_cn2 = request.security(syminfo.tickerid,timeframersi,RSILong1)
  66. RSISHORT_cn2 = request.security(syminfo.tickerid,timeframersi,RSIShort1)
  67. RSILONG_cn3 = request.security(syminfo.tickerid,timeframersi,RSILong2)
  68. RSISHORT_cn3 = request.security(syminfo.tickerid,timeframersi,RSIShort2)
  69.  
  70. var vrsi = 0
  71. if RSILONG_cn1 or RSILONG_cn2 or RSILONG_cn3
  72. vrsi := 1
  73. if RSISHORT_cn1 or RSISHORT_cn2 or RSISHORT_cn3
  74. vrsi := -1
  75.  
  76.  
  77. ///////////////////////////////////Supertrend ///////////////////////////////////////
  78. atrPeriod = input.int(10, "ATR Length", minval = 1, group = "Supertrend")
  79. factor = input.float(3.0, "Factor", minval = 0.01, step = 0.01, group = "Supertrend")
  80. [supertrend, direction] = ta.supertrend(factor, atrPeriod)
  81. supertrend := barstate.isfirst ? na : supertrend
  82. bodyMiddle = plot(barstate.isfirst ? na : (open + close) / 2, "Body Middle",display = display.none)
  83. superon = direction < 0
  84. superoff = direction > 0
  85.  
  86. var vsuper = 0
  87. if superon
  88. vsuper := 1
  89. if superoff
  90. vsuper := -1
  91.  
  92. ///////////////////////////////////////// STC ////////////////////////////////////////////
  93. import TradingView/ta/5
  94. STC_len = input.int(12, "STC Length", group = "STC")
  95. STC_fast = input.int(26, "Fast Length", group = "STC")
  96. STC_slow = input.int(50, "Slow Length", group = "STC")
  97. STC = ta.stc(close, STC_fast, STC_slow, STC_len, 3, 3)
  98.  
  99. STC_Long = STC > STC[1]
  100. STC_Short = STC < STC[1]
  101.  
  102. STC_Long1 = ta.crossover(STC, STC[1])
  103. STC_Short1 = ta.crossunder(STC, STC[1])
  104.  
  105. var vstc = 0
  106. if STC_Long or STC_Long1
  107. vstc := 1
  108. if STC_Short or STC_Short1
  109. vstc := -1
  110.  
  111. ////////////////////////Stochastic /////////////////////
  112. smoothK = input.int(3, "K", minval=1, group = "S RSI" )
  113. smoothD = input.int(3, "D", minval=1, group = "S RSI")
  114. lengthRSI = input.int(14, "RSI Length", minval=1, group = "S RSI")
  115. lengthStoch = input.int(14, "Stochastic Length", minval=1, group = "S RSI")
  116. src = input(close, title="RSI Source" )
  117. rsi1 = ta.rsi(src, lengthRSI)
  118. k = ta.sma(ta.stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)
  119. d = ta.sma(k, smoothD)
  120. timeframestoch = input.string("2D", "Days", ["1D", "2D", "3D", "4D", "5D", "6D", "7D"],group = "Stochastic")
  121. stchlongcn = request.security(syminfo.tickerid,timeframestoch,ta.crossover(k, d))
  122. stchshortcn = request.security(syminfo.tickerid,timeframestoch,ta.crossunder(k, d))
  123.  
  124. var vstch = 0
  125. if stchlongcn
  126. vstch := 1
  127. if stchshortcn
  128. vstch := -1
  129. ///////////////////////DMI////////////////////////////
  130. timeframedmi = input.string("2D", "Times", ["1D", "2D", "3D", "4D", "5D", "6D", "7D"],group = "DMI")
  131. lensig = input.int(14, title="ADX Smoothing", minval=1, maxval=50,group = "DMI")
  132. lendmi = input.int(14, minval=1, title="DI Length",group = "DMI")
  133. up = ta.change(high)
  134. down = -ta.change(low)
  135. plusDM = na(up) ? na : (up > down and up > 0 ? up : 0)
  136. minusDM = na(down) ? na : (down > up and down > 0 ? down : 0)
  137. trur = ta.rma(ta.tr, lendmi)
  138. plus = fixnan(100 * ta.rma(plusDM, lendmi) / trur)
  139. minus = fixnan(100 * ta.rma(minusDM, lendmi) / trur)
  140. sum = plus + minus
  141. adx = 100 * ta.rma(math.abs(plus - minus) / (sum == 0 ? 1 : sum), lensig)
  142.  
  143. dmilong = ta.crossover(plus,minus) and ta.crossover(plus,adx)
  144. dmishort = ta.crossunder(plus,minus) and ta.crossover (minus,adx)
  145.  
  146. dmilongcn = request.security(syminfo.tickerid,timeframedmi,dmilong)
  147. dmishortcn =request.security(syminfo.tickerid,timeframedmi,dmishort)
  148.  
  149. var vdmi = 0
  150. if dmilongcn
  151. vdmi := 1
  152. if dmishort
  153. vdmi := -1
  154.  
  155. ///////////////////////FISHER/////////////////////////
  156. len = input.int(9, minval=1, title="Length",group = "Fisher")
  157. timeframefish = input.string("2D", "Times", ["1D", "2D", "3D", "4D", "5D", "6D", "7D"],group = "Fisher")
  158. high_ = ta.highest(hl2, len)
  159. low_ = ta.lowest(hl2, len)
  160. round_(val) => val > .99 ? .999 : val < -.99 ? -.999 : val
  161. value = 0.0
  162. value := round_(.66 * ((hl2 - low_) / (high_ - low_) - .5) + .67 * nz(value[1]))
  163. fish1 = 0.0
  164. fish1 := .5 * math.log((1 + value) / (1 - value)) + .5 * nz(fish1[1])
  165. fish2 = fish1[1]
  166. fishl= fish1 > fish2
  167. fishs= fish1 < fish2
  168.  
  169. fishlong = request.security(syminfo.tickerid,timeframefish,fishl)
  170. fishshort = request.security(syminfo.tickerid,timeframefish,fishs)
  171.  
  172. var vfish = 0
  173. if fishlong
  174. vfish := 1
  175. if fishshort
  176. vfish :=-1
  177.  
  178.  
  179. //Sum of values
  180. //Trend = math.avg(vbb,vrsi,vsuper,vstc,vstch,vfish,vcmf,vdmi)
  181. Trend = math.avg (vrsi,vstc,vbb,vstc,vsuper)
  182. Trend_Bull = ta.crossover(Trend, 0)
  183. Trend_Bear = ta.crossunder(Trend, 0)
  184. //Define Trade Conditions
  185. trendlong = Trend_Bull
  186. trendshort =Trend_Bear
  187.  
  188. if trendlong and inDateRange
  189. strategy.entry("VALHALLA", strategy.long)
  190.  
  191. if trendshort and inDateRange
  192. strategy.entry("TARTAROS", strategy.short)
  193.  
  194. import EliCobra/CobraMetrics/4 as cobra
  195. //// PLOT DATA
  196.  
  197. disp_ind = input.string ("None" , title = "Display Curve" , tooltip = "Choose which data you would like to display", options=["Strategy", "Equity", "Open Profit", "Gross Profit", "Net Profit", "None"], group = "๐Ÿ ๐“’๐“ธ๐“ซ๐“ป๐“ช ๐“œ๐“ฎ๐“ฝ๐“ป๐“ฒ๐“ฌ๐“ผ ๐Ÿ")
  198. pos_table = input.string("Middle Left", "Table Position", options = ["Top Left", "Middle Left", "Bottom Left", "Top Right", "Middle Right", "Bottom Right", "Top Center", "Bottom Center"], group = "๐Ÿ ๐“’๐“ธ๐“ซ๐“ป๐“ช ๐“œ๐“ฎ๐“ฝ๐“ป๐“ฒ๐“ฌ๐“ผ ๐Ÿ")
  199. type_table = input.string("Full", "Table Type", options = ["Full", "Simple", "None"], group = "๐Ÿ ๐“’๐“ธ๐“ซ๐“ป๐“ช ๐“œ๐“ฎ๐“ฝ๐“ป๐“ฒ๐“ฌ๐“ผ ๐Ÿ")
  200.  
  201. plot(cobra.curve(disp_ind))
  202. cobra.cobraTable(type_table, pos_table)
  203.  
  204. // Determining the color based on the trend value
  205. trendColor = Trend > 0 ? color.green : color.red
  206.  
  207. // Plotting the oscillator with dynamic color
  208. plot(Trend, title="Trend Oscillator", color=trendColor, linewidth=2)
  209. hline(0, title="Zero Line", color=color.red, linestyle=hline.style_dashed)
Advertisement
Comments
  • # text 0.12 KB | 0 0
    1. download all types of premium tradingview indicators codes available on telegram - https://t.me/tradingview_premium_indicator
Add Comment
Please, Sign In to add comment