Strategy · Pine Script v4 · Own pane

Eth Strategy

by samliu0206· code credited to zxzxcool

This script aggregates spot and perpetual futures price data across multiple cryptocurrency exchanges to calculate a volume-weighted futures premium index.

View on GitHub

GitHub stars1on the repository
Pine versionv4older, still runs
Lines of code110
Settings6inputs
LicenceNone statedfile: MPL-2.0
Last update4 years ago15 May 2022

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new strategy, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart. The trades and results appear in the Strategy Tester tab; adjust the settings with the gear icon.

Source Code

110 lines of Pine Script v4 from ethPremium.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

ethPremium.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © zxzxcool

//@version=4
strategy("Eth Strategy", overlay=false)

symbol_in = input("ETH", "Symbol", type=input.string, options=["BTC", "ETH"], group='price source')
src = input(hlc3, title="Source", type=input.source, group='price source')
smoothing = input("SMA", "Type", type=input.string, options=["SMA", "EMA", "RMA", "WMA"], group='Moving Average')
length = input(title='Length', defval=7, group='Moving Average')

upper = input(2, "Upper", type=input.float, step=0.1, minval=0, group='Threshold')
lower = input(0, "Lower", type=input.float, step=0.1, maxval=0, group='Threshold')
mid = (upper + lower) / 2

// get datas
f() => [src, volume]

f_price_spot() =>
    [p1, v1] = security("BINANCE:" + symbol_in + "USDT", timeframe.period, f())
    [p2, v2] = security("HUOBI:" + symbol_in + "USDT", timeframe.period, f())
    [p3, v3] = security("OKEX:" + symbol_in + "USDT", timeframe.period, f())
    [p4, v4] = security("COINBASE:" + symbol_in + "USD", timeframe.period, f())
    [p5, v5] = security("FTX:" + symbol_in + "USD", timeframe.period, f())
    price_spot = (p1*v1+p2*v2+p3*v3+p4*v4+p5*v5)/(v1+v2+v3+v4+v5)

f_price_future() =>
    [p1, v1] = security("BINANCE:" + symbol_in + "USDTPERP", timeframe.period, f())
    [p2, v2] = security("BYBIT:" + symbol_in + "USD", timeframe.period, f())
    [p3, v3] = security("HUOBI:" + symbol_in + "PERP", timeframe.period, f())
    [p4, v4] = security("BITMEX:" + symbol_in=="BTC"?"XBT":"ETH" + "USD", timeframe.period, f())
    [p5, v5] = security("FTX:" + symbol_in+"PERP", timeframe.period, f())
    price_future = (p1*v1+p2*v2+p3*v3+p4*v4+p5*v5)/(v1+v2+v3+v4+v5)

price_spot = f_price_spot()
price_future = f_price_future()


// BTC perp futures premium
premium = log(price_future / price_spot) * 1000


// ma_function
ma_function(source, length) =>
    if smoothing == "RMA"
        rma(source, length)
    else
        if smoothing == "SMA"
            sma(source, length)
        else
            if smoothing == "EMA"
                ema(source, length)
            else
                wma(source, length)
                

// calc moving average
ma = ma_function(premium, length)



// plot lines
//palette = ma>upper?color.red:ma<lower?color.green:color.gray
//plot(ma, linewidth=3, color=palette)
plot(ma, "premium", ma <= upper ? color.gray : na, linewidth=1, transp=50)
plot(ma, "premium", ma > upper ? color.red : na, linewidth=3, transp=0)
plot(ma, "premium", ma < lower ? color.green : na, linewidth=3, transp=0)


h0 = hline(0, "0")



////------Slope-------

ema = ema(src, 14)
old_ema = nz(ema[1],0)
slope = ema - old_ema
slope := slope < 0 ? slope * -1 : slope
//plot(slope, "slp", color.orange, linewidth=3)





spread1 = security("BITFINEX:ETHUSD", timeframe.period, ohlc4)-security("COINBASE:ETHUSD", timeframe.period, ohlc4) 
spread2 = security("COINBASE:ETHUSD", timeframe.period, ohlc4)-security("BINANCE:ETHUSDT", timeframe.period, ohlc4) 
spread3 = security("COINBASE:ETHUSD", timeframe.period, ohlc4)-security("BITMEX:ETHUSD", timeframe.period, ohlc4) 
spread = spread1 + spread2 + spread3
spread_p = spread / security("BITFINEX:ETHUSD", timeframe.period, ohlc4) 
ma_p = sma(spread_p, 7) * 1000

upper_p = 0.5
lower_p = -2

plot(ma_p, "premium", ma_p <= upper_p ? color.gray : na, linewidth=1, transp=50)
plot(ma_p, "premium", ma_p > upper_p ? color.green : na, linewidth=3, transp=50)
plot(ma_p, "premium", ma_p < lower_p ? color.red : na, linewidth=3, transp=50)


slope_bond = 30
goLong = ma < lower and ma_p > upper_p and slope < slope_bond
goShort = ma > upper and ma_p < lower_p and slope < slope_bond
if goLong
    alert("UMI says: 💵 ETH BUY 💵", alert.freq_once_per_bar)
if goShort
    alert("UMI says: 🚨 ETH SELL 🚨", alert.freq_once_per_bar)
//alert("WARNING: Test", alert.freq_once_per_bar)
strategy.order("buy", true, 1, when = goLong)
strategy.order("sell", false, 1, when = goShort)

Overview

Eth Strategy computes the log ratio between perpetual futures and spot prices, applies a user-selected moving average (SMA, EMA, RMA, or WMA) for smoothing, and plots the premium curve in a separate pane with dynamic threshold coloring. Despite being declared as a strategy, it lacks execution rules or stop losses and relies on legacy exchange feeds like FTX.

Details

Published by

samliu0206 on GitHub

Code credit

zxzxcool, as written in the script's header

Type

Strategy, Pine Script v4

Draws

Separate pane below the chart

Category

Moving averages, Mean reversion

Markets

Crypto

Timeframes

Any timeframe

Risk Notes

  • The strategy enters trades without a stop loss

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
SymbolstringETHsymbol_in
Sourcesourcehlc3src
TypestringSMAsmoothing
Lengthint7length
Upperfloat2upper
Lowerfloat0lower
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

10PreBuyer

by hasnocool
106 stars

10PreBuyer is a dip-buying strategy that submits limit orders below recent price action using a discounted Hull Moving Average (HMA). It calculates the highest HMA value of high prices over a selected period, multiplied by a percentage discount factor, and pla

Strategy v4 Moving averagesMean reversion
Updated 2 years ago · GPL-3.0 GitHub repository of 10PreBuyer

Greeffer BB Strategy

by SammyEnigma
18 stars

This strategy combines Bollinger Bands, Bollinger Band Width, and the Relative Strength Index (RSI) for mean-reversion trades. It enters long when price crosses below the lower band with oversold RSI, or when band width contracts under low volatility. Short en

Strategy v4 Mean reversionOscillators & momentum
Updated 6 years ago · None stated GitHub repository of Greeffer BB Strategy

VT-Lite

by algoboy-kevin
4 stars

VT-Lite is a Pine Script v5 mean-reversion strategy that draws in a separate pane. It sizes equity-percent entries from a short-term EMA and ATR band distance, with a volatility EMA threshold and a choice of close, low, or ATR-band source. It can add reentries

Strategy v5 Mean reversionVolatility & bands
Updated 2 years ago · None stated GitHub repository of VT-Lite

Alpy VIX

by avatar-lavventura
2 stars

This strategy combines the Williams Vix Fix oscillator with volume-weighted moving average (VWMA) bands to identify reversal entries. It plots the VIX Fix histogram and optional Bollinger and percentile bands in a separate pane. Long entries trigger when the o

Strategy v4 Volatility & bandsMoving averages Repaints
Updated 7 months ago · None stated GitHub repository of Alpy VIX
2 stars

This strategy plots in a separate pane and tests a selected oscillator with one of six entry logics: moving average, moving-average crossover, zero cross, extremes, mean reversion, or take profit. Users scale and time-adjust one of three oscillators and can li

Strategy v4 Oscillators & momentumMoving averages
Updated 8 months ago · None stated GitHub repository of Oscillator Evaluator (Analysis tool)

Oversold

by kat3samsin
2 stars

The oversold strategy identifies oversold conditions using the relative strength index with a 14 period. It applies an arithmetic linear moving average with a 20 period for trend filtering and requires the price to stay within 15 percent of this average. Stoch

Strategy v4 Mean reversionOscillators & momentum
Updated 5 years ago · MIT GitHub repository of Oversold

More strategys

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers