Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=3
- 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)
- whenLong = input(title= "Short", type=float, defval= 0.11, step= 0.01)
- whenShort = input(title= "Long", type=float, defval= -0.12, step= 0.01)
- whichFunding = input("Daily Funding", title="Type of Funding", options=[ "8h Funding", "Daily Funding" ] )
- contract = input("XBTUSD", title="Contract", options=[ "XBTUSD", "ETHUSD" ])
- capFunding = true
- useAlternateColorsWhenAccuracyReduced = true
- CONTRACT_XBTUSD = "XBTUSD"
- CONTRACT_ETHUSD = "ETHUSD"
- //----- HELPERS
- clamp (lowest, highest, subject) => max(lowest, min(highest, subject))
- minutesToBars (minutes) =>
- dailyMinutes = 1440
- weeklyMinutes = 10080
- monthlyMinutes = weeklyMinutes * 30
- resolutionBaseMinutes = ismonthly ? monthlyMinutes :
- isweekly ? weeklyMinutes :
- isdaily ? dailyMinutes :
- 1
- barMinutes = interval * resolutionBaseMinutes
- minutes / barMinutes
- total (src, length) =>
- result = 0.0
- for i = 0 to length - 1
- if (i < 0)
- result := na
- break
- result := result + src[i]
- result
- simpleMovingAverage (src, length) => total(src, length) / length
- // this seems to be faster than the built-in `barssince ` function, which was contributing to an error for being too slow
- barsSince (condition) =>
- bars = 0
- bars := condition ? 0 : bars[1] + 1
- twap (resetWhen, src) =>
- bars = barsSince(resetWhen) + 1
- simpleMovingAverage(src, bars)
- // Funding occurs every 8 hours at 04:00 UTC, 12:00 UTC and 20:00 UTC.
- isBitmexFundingBar = ((hour == 4 or hour == 12 or hour == 20) and (minute == 0)) or isdwm
- fundingTwap (src) => twap(isBitmexFundingBar, src)
- bitmexInterestRateTwap (asset) =>
- //----- Interest Rate Component
- // Every contract traded on BitMEX consists of two instruments: a Base currency and a Quote currency.
- // For example, on XBTUSD, the Base currency is XBT while the quote currency is USD.
- // The Interest Rate is a function of interest rates between these two currencies:
- interestBaseIndex = security("BITMEX:" + asset + "BON", period, close) // The Interest Rate for borrowing the Base currency
- interestQuoteIndex = security("BITMEX:USDBON", period, close) // The Interest Rate for borrowing the Quote currency
- fundingInterval = 3 // (Since funding occurs every 8 hours)
- interestRate = nz((interestQuoteIndex - interestBaseIndex) / fundingInterval, 0.0001)
- fundingTwap(interestRate)
- bitmexPremiumIndexTwap (asset) =>
- //----- Premium / Discount Component
- // The perpetual contract may trade at a significant premium or discount to the Mark Price.
- // 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.
- // Each contract’s Premium Index is available on the specific instrument’s Contract Specifications page and is calculated as follows:
- // 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
- premiumIndexTwap_fallback = security("BITMEX:" + asset + "USDPI", period, fundingTwap(ohlc4))
- premiumIndexTwap = security("BITMEX:" + asset + "USDPI", "1", fundingTwap(ohlc4))
- [ premiumIndexTwap, premiumIndexTwap_fallback ]
- bitmexPredictedFundingRate (asset, capFunding, fundingCapAmount, dampenerAmount, interestRateTwap, premiumIndexTwap) =>
- predictedBaseFundingRateRatio = premiumIndexTwap + clamp(-dampenerAmount, dampenerAmount, interestRateTwap - premiumIndexTwap)
- predictedFundingRateRatio = capFunding ? clamp(-fundingCapAmount, fundingCapAmount, predictedBaseFundingRateRatio) : predictedBaseFundingRateRatio
- predictedFundingRateRatio * 100
- bitmexNextFundingRate (predictedFundingRate) =>
- nextFundingRate = 0.01
- nextFundingRate := isBitmexFundingBar ? predictedFundingRate[1] : nextFundingRate[1]
- bitmexFundingBarValue (assetTicker, predictedFundingRate) =>
- fundingPeriodMinutes = 480
- fundingPeriodBars = max(1, floor(minutesToBars(fundingPeriodMinutes)))
- fundingOffset = fundingPeriodBars + 1
- dwmIntervalModifier = isdaily ? 3 :
- isweekly ? 3 * 7 :
- ismonthly ? 3 * 30 :
- na
- dwmModifier = interval * dwmIntervalModifier
- intradayFundingBarValue = predictedFundingRate[fundingOffset]
- // 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`
- nonIntradayFundingBarValue = security(assetTicker, "240", intradayFundingBarValue) * dwmModifier
- (not isBitmexFundingBar) ? na : (isintraday ? intradayFundingBarValue : nonIntradayFundingBarValue)
- //----- VALUES
- asset = contract == CONTRACT_XBTUSD ? "XBT" :
- contract == CONTRACT_ETHUSD ? "ETH" :
- "XBT"
- assetTicker = asset + "USD"
- [ premiumIndexTwap_ideal, premiumIndexTwap_fallback ] = bitmexPremiumIndexTwap(asset)
- premiumIndexTwap = na(premiumIndexTwap_ideal) ? premiumIndexTwap_fallback : premiumIndexTwap_ideal
- interestRateTwap = bitmexInterestRateTwap(asset)
- predictedFundingRate = bitmexPredictedFundingRate(asset, capFunding, 0.00375, 0.0005, interestRateTwap, premiumIndexTwap)
- nextFundingRate = bitmexNextFundingRate(predictedFundingRate)
- fundingBarValue = bitmexFundingBarValue(assetTicker, predictedFundingRate)
- accuracyReduced = na(premiumIndexTwap_ideal)
- theActualUsedFunding = whichFunding == "8h Funding" ? nextFundingRate : predictedFundingRate
- //----- RENDER
- longsColor = #53B987
- shortsColor = #EB4D5C
- longsAlternateColor = #247BA0
- shortsAlternateColor = orange
- useAlternateColors = useAlternateColorsWhenAccuracyReduced and accuracyReduced
- FundingColor (rate) => rate < 0 ? (useAlternateColors ? shortsAlternateColor : shortsColor) : (useAlternateColors ? longsAlternateColor : longsColor)
- nextFundingColor = FundingColor(nextFundingRate)
- fundingBarColor = FundingColor(fundingBarValue)
- longsPay= theActualUsedFunding > whenLong
- shortsPay= theActualUsedFunding < whenShort
- ema_0 = input(11, minval=1, title="EMA 0")
- ema_1 = input(60, minval=1, title="EMA 1")
- ema_2 = input(203, minval=1, title="EMA 2")
- //vwma_0 = input(113, minval=1, title="VWMA")
- emasrc = close
- ema0 = ema(emasrc, ema_0)
- ema1 = ema(emasrc, ema_1)
- ema2 = ema(emasrc, ema_2)
- //vwma0 = vwma(src, vwma_0)
- plot(ema0, color=red, title="EMA 0", style=line)
- plot(ema1, color=orange, title="EMA 1", style=line)
- plot(ema2, color=yellow, title="EMA 2", style=line)
- //plot(vwma0, color=green, title="VWMA", style=line)
- bgcolor(color=(longsPay ? #8b3030 : (shortsPay ? #0F1626 : na)))
- startingPeriod = n<500
- longCondition= (crossover(ema0,ema1) and (ema0 > ema2) and (ema1 > ema2)) and not startingPeriod
- //shortCondition=
- strategy.entry("Long", strategy.long, when=longCondition)
- //strategy.entry("Short", strategy.short, when=shortCondition)
- longClose= longsPay
- //shortClose=
- strategy.close("Long", when=longClose)
- //strategy.close("Short", when=shortClose)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement