Advertisement
GevinMG

Untitled

Feb 17th, 2024
140
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.29 KB | None | 0 0
  1. // This Pine Scriptโ„ข code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2. // ยฉ GuufyKing
  3.  
  4. //@version=5
  5. strategy("Strat Dev Experimental Strategy (BTC)", initial_capital=10000, slippage=1, default_qty_value=100,
  6. pyramiding=0, default_qty_type=strategy.percent_of_equity, process_orders_on_close=true,
  7. shorttitle="SD101", overlay=true)
  8.  
  9. //Date Range:
  10. useDateFilter = input.bool(true, title="Range of Backtest",
  11. group="Backtest")
  12. backtestStartDate = input.time(timestamp("1 Jan 2018"),
  13. title="Start Date", group="Backtest Time Period")
  14.  
  15. //Range Conditions:
  16. inDateRange = not useDateFilter or (time >= backtestStartDate)
  17.  
  18. //Cobra Metrics Table:
  19. import EliCobra/CobraMetrics/4 as cobra
  20. //// PLOT DATA
  21. disp_ind = input.string ("Equity" , title = "Display Curve" , tooltip = "Choose which data you would like to display", options=["Strategy", "Equity", "Open Profit", "Gross Profit", "Net Profit", "None"], group = "๐Ÿ ๐“’๐“ธ๐“ซ๐“ป๐“ช ๐“œ๐“ฎ๐“ฝ๐“ป๐“ฒ๐“ฌ๐“ผ ๐Ÿ")
  22. pos_table = input.string("Middle Right", "Table Position", options = ["Top Left", "Middle Left", "Bottom Left", "Top Right", "Middle Right", "Bottom Right", "Top Center", "Bottom Center"], group = "๐Ÿ ๐“’๐“ธ๐“ซ๐“ป๐“ช ๐“œ๐“ฎ๐“ฝ๐“ป๐“ฒ๐“ฌ๐“ผ ๐Ÿ")
  23. type_table = input.string("Full", "Table Type", options = ["Full", "Simple", "None"], group = "๐Ÿ ๐“’๐“ธ๐“ซ๐“ป๐“ช ๐“œ๐“ฎ๐“ฝ๐“ป๐“ฒ๐“ฌ๐“ผ ๐Ÿ")
  24. plot(cobra.curve(disp_ind))
  25. cobra.cobraTable(type_table, pos_table)
  26.  
  27. //Exponential Moving Average (EMA) Indicator:
  28. EMA = ta.ema(close, 12)
  29. SMA = ta.sma(close, 12)
  30. plot(EMA, title = "EMA", color = color.orange)
  31. longEMA = EMA >= close[1]
  32. shortEMA = EMA < close [1]
  33.  
  34. //Relative Strength Index (RSI) Indicator:
  35. ma(source, length, type) =>
  36. switch type
  37. "SMA" => ta.sma(source, length)
  38. "Bollinger Bands" => ta.sma(source, length)
  39. "EMA" => ta.ema(source, length)
  40. "SMMA (RMA)" => ta.rma(source, length)
  41. "WMA" => ta.wma(source, length)
  42. "VWMA" => ta.vwma(source, length)
  43.  
  44. // RSI Inputs:
  45. rsiLengthInput = input.int(14, minval=1, title="RSI Length", group="RSI Settings")
  46. rsiSourceInput = input.source(close, "Source", group="RSI Settings")
  47. maTypeInput = input.string("SMA", title="MA Type", options=["SMA", "Bollinger Bands", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="MA Settings", display = display.data_window)
  48. maLengthInput = input.int(14, title="MA Length", group="MA Settings", display = display.data_window)
  49. bbMultInput = input.float(2.0, minval=0.001, maxval=50, title="BB StdDev", group="MA Settings", display = display.data_window)
  50. showDivergence = input.bool(false, title="Show Divergence", group="RSI Settings", display = display.data_window)
  51.  
  52. // RSI Moves:
  53. up = ta.rma(math.max(ta.change(rsiSourceInput), 0), rsiLengthInput)
  54. down = ta.rma(-math.min(ta.change(rsiSourceInput), 0), rsiLengthInput)
  55. rsiDaily = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
  56. rsiMA = ma(rsiDaily, maLengthInput, maTypeInput)
  57. isBB = maTypeInput == "Bollinger Bands"
  58.  
  59. // RSI Plots:
  60. //rsiPlot = plot(rsi, "RSI", color=#7E57C2)
  61. //plot(rsiMA, "RSI-based MA", color=color.yellow)
  62. //rsiUpperBand = hline(70, "RSI Upper Band", color=#787B86)
  63. //midline = hline(50, "RSI Middle Band", color=color.new(#787B86, 50))
  64. //rsiLowerBand = hline(30, "RSI Lower Band", color=#787B86)
  65. //fill(rsiUpperBand, rsiLowerBand, color=color.rgb(126, 87, 194, 90), title="RSI Background Fill")
  66. //bbUpperBand = plot(isBB ? rsiMA + ta.stdev(rsi, maLengthInput) * bbMultInput : na, title = "Upper Bollinger Band", color=color.green)
  67. //bbLowerBand = plot(isBB ? rsiMA - ta.stdev(rsi, maLengthInput) * bbMultInput : na, title = "Lower Bollinger Band", color=color.green)
  68. //fill(bbUpperBand, bbLowerBand, color= isBB ? color.new(color.green, 90) : na, title="Bollinger Bands Background Fill")
  69.  
  70. //midLinePlot = plot(50, color = na, editable = false, display = display.none)
  71. //fill(rsiPlot, midLinePlot, 100, 70, top_color = color.new(color.green, 0), bottom_color = color.new(color.green, 100), title = "Overbought Gradient Fill")
  72. //fill(rsiPlot, midLinePlot, 30, 0, top_color = color.new(color.red, 100), bottom_color = color.new(color.red, 0), title = "Oversold Gradient Fill")
  73.  
  74. // Divergence
  75. lookbackRight = 5
  76. lookbackLeft = 5
  77. rangeUpper = 60
  78. rangeLower = 5
  79. bearColor = color.red
  80. bullColor = color.green
  81. textColor = color.white
  82. noneColor = color.new(color.white, 100)
  83.  
  84. plFound = na(ta.pivotlow(rsiDaily, lookbackLeft, lookbackRight)) ? false : true
  85. phFound = na(ta.pivothigh(rsiDaily, lookbackLeft, lookbackRight)) ? false : true
  86. _inRange(cond) =>
  87. bars = ta.barssince(cond == true)
  88. rangeLower <= bars and bars <= rangeUpper
  89.  
  90. //------------------------------------------------------------------------------
  91. // Regular Bullish
  92. // rsi: Higher Low
  93.  
  94. rsiHL = rsiDaily[lookbackRight] > ta.valuewhen(plFound, rsiDaily[lookbackRight], 1) and _inRange(plFound[1])
  95.  
  96. // Price: Lower Low
  97.  
  98. priceLL = low[lookbackRight] < ta.valuewhen(plFound, low[lookbackRight], 1)
  99. bullCondAlert = priceLL and rsiHL and plFound
  100. bullCond = showDivergence and bullCondAlert
  101.  
  102. //plot(
  103. // plFound ? rsiDaily[lookbackRight] : na,
  104. // offset=-lookbackRight,
  105. // title="Regular Bullish",
  106. // linewidth=2,
  107. // color=(bullCond ? bullColor : noneColor)
  108. // )
  109. //
  110. //plotshape(
  111. // bullCond ? rsiDaily[lookbackRight] : na,
  112. // offset=-lookbackRight,
  113. // title="Regular Bullish Label",
  114. // text=" Bull ",
  115. // style=shape.labelup,
  116. // location=location.absolute,
  117. // color=bullColor,
  118. // textcolor=textColor
  119. // )
  120. //
  121. //------------------------------------------------------------------------------
  122. // Regular Bearish
  123. // rsi: Lower High
  124.  
  125. rsiLH = rsiDaily[lookbackRight] < ta.valuewhen(phFound, rsiDaily[lookbackRight], 1) and _inRange(phFound[1])
  126.  
  127. // Price: Higher High
  128.  
  129. priceHH = high[lookbackRight] > ta.valuewhen(phFound, high[lookbackRight], 1)
  130.  
  131. bearCondAlert = priceHH and rsiLH and phFound
  132. bearCond = showDivergence and bearCondAlert
  133.  
  134. //plot(
  135. // phFound ? rsiDaily[lookbackRight] : na,
  136. // offset=-lookbackRight,
  137. // title="Regular Bearish",
  138. // linewidth=2,
  139. // color=(bearCond ? bearColor : noneColor)
  140. // )
  141. //
  142. //plotshape(
  143. // bearCond ? rsiDaily[lookbackRight] : na,
  144. // offset=-lookbackRight,
  145. // title="Regular Bearish Label",
  146. // text=" Bear ",
  147. // style=shape.labeldown,
  148. // location=location.absolute,
  149. // color=bearColor,
  150. // textcolor=textColor
  151. // )
  152. //
  153. //alertcondition(bullCondAlert, title='Regular Bullish Divergence', message="Found a new Regular Bullish Divergence, `Pivot Lookback Right` number of bars to the left of the current bar.")
  154. //alertcondition(bearCondAlert, title='Regular Bearish Divergence', message='Found a new Regular Bearish Divergence, `Pivot Lookback Right` number of bars to the left of the current bar.')
  155.  
  156. //Standard Deviation Indicator:
  157. length = input.int(20, minval=1)
  158. src = input(close, title='Source')
  159. stdev = ta.stdev(src, length)
  160. plot(stdev, color=color.new(color.blue, 0))
  161.  
  162. longCon = rsiDaily > 50 and stdev >= close and longEMA
  163.  
  164. shortCon = rsiDaily < 50 and stdev < close and shortEMA
  165.  
  166. if longCon and inDateRange and barstate.isconfirmed
  167. strategy.entry("Long State", strategy.long)
  168.  
  169. if shortCon and inDateRange and barstate.isconfirmed
  170. strategy.entry("Short State", strategy.short)
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
Advertisement