跳转到主要内容

OKX 长期 ETH 交易机器人

这是一款适用于 OKX 交易所的长期投资机器人,专为现货或保证金交易对设计,并始终跟随趋势运行。

作者:Jacob

该交易机器人适用于 OKX 的现货和保证金交易市场。

该策略使用 三重指数移动平均线(Triple Exponential Moving Average,TEMA)最小二乘移动平均线(Least Squares Moving Average,LSMA) 的交叉信号进行交易。

同时,该策略会在上涨趋势中通过基于 ATR(Average True Range,平均真实波幅) 指标的追踪止损来管理利润。

该策略仅适用于现货市场,主要面向长期投资者。

适用市场 - OKX:ETH/USDT(4小时周期)

参数

Parameters

What Trades Should Be Taken

LONG

First Trend Line

TEMA

Second Trend Line

LSMA

Length of the First Trend Line

18

Length of the Second Trend Line

57

Long Take Profit 1 (%)

9

Long Take Profit 1 (Qty)

20

Long Take Profit 2 (%)

20

Long Take Profit 2 (Qty)

20

Stop Loss (%)

3

SL Multiplier

3.3

ATR Period

4

Source

Close

Pine Script 代码

您可以复制 Pine Script 代码并粘贴到 TradingView 中使用。

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

//@version=5
strategy('Automated Ethereum (ETH) Investment Strategy', overlay=true, initial_capital=5000, pyramiding=0, currency='USD', default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_type=strategy.commission.percent, commission_value=0.1)

//////////// Functions

Atr(p) =>
atr = 0.
Tr = math.max(high - low, math.max(math.abs(high - close[1]), math.abs(low - close[1])))
atr := nz(atr[1] + (Tr - atr[1]) / p, Tr)
atr

//TEMA
TEMA(series, length) =>
if length > 0
ema1 = ta.ema(series, length)
ema2 = ta.ema(ema1, length)
ema3 = ta.ema(ema2, length)
3 * ema1 - 3 * ema2 + ema3
else
na
tradeType = input.string('LONG', title='What trades should be taken : ', options=['LONG', 'SHORT', 'BOTH', 'NONE'])

///////////////////////////////////////////////////
/// INDICATORS
source = close

/// TREND
trend_type1 = input.string('TEMA', title='First Trend Line : ', options=['LSMA', 'TEMA', 'EMA', 'SMA'])
trend_type2 = input.string('LSMA', title='First Trend Line : ', options=['LSMA', 'TEMA', 'EMA', 'SMA'])

trend_type1_length = input(18, 'Length of the First Trend Line')
trend_type2_length = input(57, 'Length of the Second Trend Line')

leadLine1 = if trend_type1 == 'LSMA'
ta.linreg(close, trend_type1_length, 0)
else if trend_type1 == 'TEMA'
TEMA(close, trend_type1_length)
else if trend_type1 == 'EMA'
ta.ema(close, trend_type1_length)
else
ta.sma(close, trend_type1_length)

leadLine2 = if trend_type2 == 'LSMA'
ta.linreg(close, trend_type2_length, 0)
else if trend_type2 == 'TEMA'
TEMA(close, trend_type2_length)
else if trend_type2 == 'EMA'
ta.ema(close, trend_type2_length)
else
ta.sma(close, trend_type2_length)

p3 = plot(leadLine1, color=color.new(#53b987, 50), title='EMA', linewidth=1)
p4 = plot(leadLine2, color=color.new(#eb4d5c, 50), title='SMA', linewidth=1)
fill(p3, p4, color=leadLine1 > leadLine2 ? #53b987 : #eb4d5c, transp=60)

//Upward Trend
UT = ta.crossover(leadLine1, leadLine2)
DT = ta.crossunder(leadLine1, leadLine2)

// TP/ SL/ FOR LONG
// TAKE PROFIT AND STOP LOSS
long_tp1_inp = input.float(9, title='Long Take Profit 1 %', step=0.1) / 100
long_tp1_qty = input.int(20, title='Long Take Profit 1 Qty', step=1)

long_tp2_inp = input.float(20, title='Long Take Profit 2%', step=0.1) / 100
long_tp2_qty = input.int(20, title='Long Take Profit 2 Qty', step=1)

long_take_level_1 = strategy.position_avg_price * (1 + long_tp1_inp)
long_take_level_2 = strategy.position_avg_price * (1 + long_tp2_inp)

long_sl_input = input.float(3, title='stop loss in %', step=0.1) / 100
long_sl_input_level = strategy.position_avg_price * (1 - long_sl_input)

// Stop Loss
multiplier = input.float(3.3, 'SL Mutiplier', minval=1, step=0.1)
ATR_period = input.int(4, 'ATR period', minval=1, step=1)

// Strategy
//LONG STRATEGY CONDITION

SC = input(close, 'Source')
SL1 = multiplier * Atr(ATR_period) // Stop Loss
Trail1 = 0.0
iff_1 = SC > nz(Trail1[1], 0) ? SC - SL1 : SC + SL1
Trail1 := SC < nz(Trail1[1], 0) and SC[1] < nz(Trail1[1], 0) ? math.min(nz(Trail1[1], 0), SC + SL1) : iff_1
Trail1_high = ta.highest(Trail1, 50)

// iff(SC > nz(Trail1[1], 0) and SC[1] > nz(Trail1[1], 0), max(nz(Trail1[1], 0), SC - SL1),

entry_long = ta.crossover(leadLine1, leadLine2) and Trail1_high < close
exit_long = close < Trail1_high or ta.crossover(leadLine2, leadLine1) or close < long_sl_input_level

///// BACKTEST PERIOD ///////
testStartYear = input(2016, 'Backtest Start Year')
testStartMonth = input(1, 'Backtest Start Month')
testStartDay = input(1, 'Backtest Start Day')
testPeriodStart = timestamp(testStartYear, testStartMonth, testStartDay, 0, 0)

testStopYear = input(9999, 'Backtest Stop Year')
testStopMonth = input(12, 'Backtest Stop Month')
testStopDay = input(31, 'Backtest Stop Day')
testPeriodStop = timestamp(testStopYear, testStopMonth, testStopDay, 0, 0)

testPeriod() =>
time >= testPeriodStart and time <= testPeriodStop ? true : false

if testPeriod()
if tradeType == 'LONG' or tradeType == 'BOTH'
if strategy.position_size == 0 or strategy.position_size > 0
strategy.entry('long', strategy.long, comment='b8f60da7_ENTER-LONG_BINANCE_BTC/USDT_b8f60da7-BTC-Investment_4H', when=entry_long)
strategy.exit('TP1', 'long', qty_percent=long_tp1_qty, limit=long_take_level_1)
strategy.exit('TP2', 'long', qty_percent=long_tp2_qty, limit=long_take_level_2)
strategy.close('long', when=exit_long, comment='b8f60da7_EXIT-LONG_BINANCE_BTC/USDT_b8f60da7-BTC-Investment_4H')


// LONG POSITION

plot(strategy.position_size > 0 ? long_take_level_1 : na, style=plot.style_linebr, color=color.new(color.green, 0), linewidth=1, title='1st Long Take Profit')
plot(strategy.position_size > 0 ? long_take_level_2 : na, style=plot.style_linebr, color=color.new(color.green, 0), linewidth=1, title='2nd Long Take Profit')
plot(strategy.position_size > 0 ? Trail1_high : na, style=plot.style_linebr, color=color.new(color.red, 0), linewidth=1, title='Long Stop Loss')
这是否解答了您的问题?