Allen9538

前單若止損,新開倉就提高槓桿,加大投資倍數,以提高獲利減少投資風險

BITSTAMP:BTCUSD   比特幣
這份程式碼主要是一個使用超級趨勢指標以及RSI指標的交易策略,以下是對程式碼的檢視與解釋:

在程式碼的開頭,@version=5 表示使用 Pine Script v5。

margin_long 變數是保證金比例的設置,默認值為 0.1。

default_qty_value 變數是設置默認的交易數量,默認值為 120。

在代碼中,如果策略的頭寸大小為0且已經有過交易,則會根據當前的保證金比例進行調整。如果當前的保證金比例為0.1,則將保證金比例更改為0.05,否則將保證金比例更改為0.1。

接下來根據新的保證金比例以及設置的止損比例,計算每筆交易的交易數量。

使用超級趨勢指標進行交易判斷,如果超級趨勢向下變化、RSI(21)小於70,RSI(3)大於80,以及 ADX 大於20,則進行多頭進場。如果超級趨勢向上變化、RSI(21)大於30,RSI(3)小於20,則平多頭倉位。

如果超級趨勢向上變化、RSI(21)大於70,RSI(3)小於20,以及 ADX 小於80,則進行空頭進場。如果超級趨勢向下變化、RSI(21)小於30,RSI(3)大於80,則平空頭倉位。

以上的交易操作均使用 strategy.entry 和 strategy.close 函數來進行下單操作,設置了交易數量、止損價格、限價價格等等參數。

在程式碼的最後,設置了策略的名稱、交易數量的默認值以及一些基本的參數,例如初始資本金和設置的保證金比例

備註投入1000u,使用10倍

//@version=5

// 设置初始保证金比例为 0.1
margin_long = 0.1
default_qty_value=120

// 判断是否需要降低保证金比例
if strategy.position_size == 0 and strategy.closed_trades > 0:
if strategy.margin_rate == 0.1:
strategy.margin_rate := 0.05
else:
strategy.margin_rate := 0.1

// 根据新的保证金比例计算建仓数量
quantity = strategy.equity * strategy.margin_rate / (close * stop_loss_pct)
stop_loss_pct = 0.3
stop_loss_price = strategy.position_avg_price * (1 - stop_loss_pct)

strategy.exit("Stop Loss", "Buy", stop=stop_loss_price)
strategy.exit("Stop Loss", "Sell", stop=stop_loss_price)
// 使用新的保证金比例进行建仓,设置止损为买入价的stop_loss_pct比例
strategy.entry("Buy", strategy.long, qty=quantity, limit=close + squeeze_range, stop=close * (1 - stop_loss_pct/100))
strategy.entry("Sell", strategy.short, qty=quantity, limit=close - squeeze_range, stop=close * (1 + stop_loss_pct/100))

// 自动调整建仓数量
if strategy.position_size > 0 and strategy.margin_rate != margin_long:
margin_diff = margin_long / strategy.margin_rate
default_qty_value := default_qty_value * margin_diff

strategy("Supertrend Strategy with Stop Loss", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=default_qty_value,
initial_capital=1000, margin_long=margin_long )
atrPeriod = input(10, "ATR Length")
factor = input.float(3.0, "Factor", step = 0.01)

= ta.supertrend(factor, atrPeriod)

adxlen = input(7, title="ADX Smoothing")
dilen = input(7, title="DI Length")
dirmov(len) =>
up = ta.change(high)
down = -ta.change(low)
plusDM = na(up) ? na : (up > down and up > 0 ? up : 0)
minusDM = na(down) ? na : (down > up and down > 0 ? down : 0)
truerange = ta.rma(ta.tr, len)
plus = fixnan(100 * ta.rma(plusDM, len) / truerange)
minus = fixnan(100 * ta.rma(minusDM, len) / truerange)

adx(dilen, adxlen) =>
= dirmov(dilen)
sum = plus + minus
adx = 100 * ta.rma(math.abs(plus - minus) / (sum == 0 ? 1 : sum), adxlen)
sig = adx(dilen, adxlen)
plot(sig, color=color.red, title="ADX")
// 多头进场条件:超级趋势向下变化,RSI(21)小于70,RSI(3)大于80
if ta.change(direction) < 0 and ta.rsi(close, 21) < 70 and ta.rsi(close,3) > 80 and sig > 20
strategy.entry("My Long Entry Id", strategy.long)

// 平仓多头条件:超级趋势向上变化,RSI(21)大于30,RSI(3)小于20
if ta.change(direction) > 0 and ta.rsi(close, 21) > 30 and ta.rsi(close,3) < 20
strategy.close("My Long Entry Id")

// 空头进场条件:超级趋势向上变化,RSI(21)大于70,RSI(3)小于20
if ta.change(direction) > 0 and ta.rsi(close, 21) > 70 and ta.rsi(close,3) < 20 and sig < 80
strategy.entry("My Short Entry Id", strategy.short)

// 平仓空头条件:超级趋势向下变化,RSI(21)小于30,RSI(3)大于80
if ta.change(direction) < 0 and ta.rsi(close, 21) < 30 and ta.rsi(close,3) > 80
strategy.close("My Short Entry Id")

以上代碼,tradingview 居然無法提供數據,以致以無法新增的圖表,求各位大神幫忙優化,感謝 !
免責聲明

這些資訊和出版物並不意味著也不構成TradingView提供或認可的金融、投資、交易或其他類型的意見或建議。請在使用條款閱讀更多資訊。