Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.25 KB | None | 0 0
  1. //@version=3
  2. strategy("EMA+MexTops", shorttitle='EMA+MexTops', overlay=true, pyramiding=0, default_qty_type=strategy.percent_of_equity, default_qty_value=100, currency=currency.USD, initial_capital=1500, commission_type= strategy.commission.percent, commission_value= 0.1)
  3.  
  4. whenLong = input(title= "Short", type=float, defval= 0.11, step= 0.01)
  5. whenShort = input(title= "Long", type=float, defval= -0.12, step= 0.01)
  6. whichFunding = input("Daily Funding", title="Type of Funding", options=[ "8h Funding", "Daily Funding" ] )
  7. contract = input("XBTUSD", title="Contract", options=[ "XBTUSD", "ETHUSD" ])
  8. capFunding = true
  9. useAlternateColorsWhenAccuracyReduced = true
  10.  
  11. CONTRACT_XBTUSD = "XBTUSD"
  12. CONTRACT_ETHUSD = "ETHUSD"
  13.  
  14.  
  15. //----- HELPERS
  16.  
  17. clamp (lowest, highest, subject) => max(lowest, min(highest, subject))
  18.  
  19. minutesToBars (minutes) =>
  20. dailyMinutes = 1440
  21. weeklyMinutes = 10080
  22. monthlyMinutes = weeklyMinutes * 30
  23. resolutionBaseMinutes = ismonthly ? monthlyMinutes :
  24. isweekly ? weeklyMinutes :
  25. isdaily ? dailyMinutes :
  26. 1
  27. barMinutes = interval * resolutionBaseMinutes
  28. minutes / barMinutes
  29.  
  30. total (src, length) =>
  31. result = 0.0
  32. for i = 0 to length - 1
  33. if (i < 0)
  34. result := na
  35. break
  36. result := result + src[i]
  37. result
  38.  
  39. simpleMovingAverage (src, length) => total(src, length) / length
  40.  
  41. // this seems to be faster than the built-in `barssince ` function, which was contributing to an error for being too slow
  42. barsSince (condition) =>
  43. bars = 0
  44. bars := condition ? 0 : bars[1] + 1
  45.  
  46. twap (resetWhen, src) =>
  47. bars = barsSince(resetWhen) + 1
  48. simpleMovingAverage(src, bars)
  49.  
  50. // Funding occurs every 8 hours at 04:00 UTC, 12:00 UTC and 20:00 UTC.
  51. isBitmexFundingBar = ((hour == 4 or hour == 12 or hour == 20) and (minute == 0)) or isdwm
  52.  
  53. fundingTwap (src) => twap(isBitmexFundingBar, src)
  54.  
  55. bitmexInterestRateTwap (asset) =>
  56. //----- Interest Rate Component
  57.  
  58. // Every contract traded on BitMEX consists of two instruments: a Base currency and a Quote currency.
  59. // For example, on XBTUSD, the Base currency is XBT while the quote currency is USD.
  60. // The Interest Rate is a function of interest rates between these two currencies:
  61.  
  62. interestBaseIndex = security("BITMEX:" + asset + "BON", period, close) // The Interest Rate for borrowing the Base currency
  63. interestQuoteIndex = security("BITMEX:USDBON", period, close) // The Interest Rate for borrowing the Quote currency
  64. fundingInterval = 3 // (Since funding occurs every 8 hours)
  65. interestRate = nz((interestQuoteIndex - interestBaseIndex) / fundingInterval, 0.0001)
  66. fundingTwap(interestRate)
  67.  
  68. bitmexPremiumIndexTwap (asset) =>
  69. //----- Premium / Discount Component
  70.  
  71. // The perpetual contract may trade at a significant premium or discount to the Mark Price.
  72. // In those situations, a Premium Index will be used to raise or lower the next Funding Rate to levels consistent with where the contract is trading.
  73. // Each contract’s Premium Index is available on the specific instrument’s Contract Specifications page and is calculated as follows:
  74. // Premium Index (P) = (Max(0, Impact Bid Price - Mark Price) - Max(0, Mark Price - Impact Ask Price)) / Spot Price + Fair Basis used in Mark Price
  75.  
  76. premiumIndexTwap_fallback = security("BITMEX:" + asset + "USDPI", period, fundingTwap(ohlc4))
  77. premiumIndexTwap = security("BITMEX:" + asset + "USDPI", "1", fundingTwap(ohlc4))
  78. [ premiumIndexTwap, premiumIndexTwap_fallback ]
  79.  
  80. bitmexPredictedFundingRate (asset, capFunding, fundingCapAmount, dampenerAmount, interestRateTwap, premiumIndexTwap) =>
  81. predictedBaseFundingRateRatio = premiumIndexTwap + clamp(-dampenerAmount, dampenerAmount, interestRateTwap - premiumIndexTwap)
  82. predictedFundingRateRatio = capFunding ? clamp(-fundingCapAmount, fundingCapAmount, predictedBaseFundingRateRatio) : predictedBaseFundingRateRatio
  83. predictedFundingRateRatio * 100
  84.  
  85. bitmexNextFundingRate (predictedFundingRate) =>
  86. nextFundingRate = 0.01
  87. nextFundingRate := isBitmexFundingBar ? predictedFundingRate[1] : nextFundingRate[1]
  88.  
  89. bitmexFundingBarValue (assetTicker, predictedFundingRate) =>
  90. fundingPeriodMinutes = 480
  91. fundingPeriodBars = max(1, floor(minutesToBars(fundingPeriodMinutes)))
  92. fundingOffset = fundingPeriodBars + 1
  93.  
  94. dwmIntervalModifier = isdaily ? 3 :
  95. isweekly ? 3 * 7 :
  96. ismonthly ? 3 * 30 :
  97. na
  98. dwmModifier = interval * dwmIntervalModifier
  99.  
  100. intradayFundingBarValue = predictedFundingRate[fundingOffset]
  101. // any ticker will work fine here, but simply using `ticker` (the current chart) doesn't work on special charts like spread charts that can't access `ticker`
  102. nonIntradayFundingBarValue = security(assetTicker, "240", intradayFundingBarValue) * dwmModifier
  103. (not isBitmexFundingBar) ? na : (isintraday ? intradayFundingBarValue : nonIntradayFundingBarValue)
  104.  
  105.  
  106. //----- VALUES
  107.  
  108. asset = contract == CONTRACT_XBTUSD ? "XBT" :
  109. contract == CONTRACT_ETHUSD ? "ETH" :
  110. "XBT"
  111.  
  112. assetTicker = asset + "USD"
  113.  
  114. [ premiumIndexTwap_ideal, premiumIndexTwap_fallback ] = bitmexPremiumIndexTwap(asset)
  115. premiumIndexTwap = na(premiumIndexTwap_ideal) ? premiumIndexTwap_fallback : premiumIndexTwap_ideal
  116. interestRateTwap = bitmexInterestRateTwap(asset)
  117.  
  118. predictedFundingRate = bitmexPredictedFundingRate(asset, capFunding, 0.00375, 0.0005, interestRateTwap, premiumIndexTwap)
  119. nextFundingRate = bitmexNextFundingRate(predictedFundingRate)
  120. fundingBarValue = bitmexFundingBarValue(assetTicker, predictedFundingRate)
  121. accuracyReduced = na(premiumIndexTwap_ideal)
  122.  
  123. theActualUsedFunding = whichFunding == "8h Funding" ? nextFundingRate : predictedFundingRate
  124.  
  125. //----- RENDER
  126.  
  127. longsColor = #53B987
  128. shortsColor = #EB4D5C
  129. longsAlternateColor = #247BA0
  130. shortsAlternateColor = orange
  131. useAlternateColors = useAlternateColorsWhenAccuracyReduced and accuracyReduced
  132.  
  133. FundingColor (rate) => rate < 0 ? (useAlternateColors ? shortsAlternateColor : shortsColor) : (useAlternateColors ? longsAlternateColor : longsColor)
  134.  
  135. nextFundingColor = FundingColor(nextFundingRate)
  136. fundingBarColor = FundingColor(fundingBarValue)
  137.  
  138. longsPay= theActualUsedFunding > whenLong
  139. shortsPay= theActualUsedFunding < whenShort
  140.  
  141.  
  142. ema_0 = input(11, minval=1, title="EMA 0")
  143. ema_1 = input(60, minval=1, title="EMA 1")
  144. ema_2 = input(203, minval=1, title="EMA 2")
  145. //vwma_0 = input(113, minval=1, title="VWMA")
  146.  
  147. emasrc = close
  148.  
  149. ema0 = ema(emasrc, ema_0)
  150. ema1 = ema(emasrc, ema_1)
  151. ema2 = ema(emasrc, ema_2)
  152. //vwma0 = vwma(src, vwma_0)
  153.  
  154. plot(ema0, color=red, title="EMA 0", style=line)
  155. plot(ema1, color=orange, title="EMA 1", style=line)
  156. plot(ema2, color=yellow, title="EMA 2", style=line)
  157. //plot(vwma0, color=green, title="VWMA", style=line)
  158.  
  159. bgcolor(color=(longsPay ? #8b3030 : (shortsPay ? #0F1626 : na)))
  160.  
  161. startingPeriod = n<500
  162. longCondition= (crossover(ema0,ema1) and (ema0 > ema2) and (ema1 > ema2)) and not startingPeriod
  163. //shortCondition=
  164.  
  165. strategy.entry("Long", strategy.long, when=longCondition)
  166. //strategy.entry("Short", strategy.short, when=shortCondition)
  167.  
  168.  
  169. longClose= longsPay
  170. //shortClose=
  171.  
  172. strategy.close("Long", when=longClose)
  173. //strategy.close("Short", when=shortClose)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement