这是一个专为加密货币市场设计的量化交易策略,充分利用加密货币市场的高波动性特征,通过智能的成本平均法(DCA)在价格回调时动态加仓。策略在15分钟时间框架上运行,能够有效应对加密货币市场的快速波动,同时规避过度交易带来的风险。
策略主要包含四个核心模块: 1. 智能入场系统:基于OHLC4加权均价进行首次建仓,适应加密货币市场的高波动特性 2. 动态补仓机制:在价格回调时触发安全订单,补仓量随深度增加而放大,充分利用市场波动 3. 风险管理系统:通过金字塔式加仓和灵活杠杆调整来优化风险收益比 4. 快速止盈控制:针对加密货币市场的快速波动特点设计的止盈机制,包含手续费优化
该策略通过创新的DCA方法和动态风险管理,为加密货币交易提供了一个全面的自动化解决方案。虽然加密货币市场存在较高风险,但通过精心设计的风控机制和市场适应性优化,策略能够在大多数市场环境下保持稳定性。未来优化将着重于提升策略对加密货币市场特殊性的适应能力。
/*backtest
start: 2020-08-29 15:00:00
end: 2025-02-18 17:22:45
period: 1h
basePeriod: 1h
exchanges: [{"eid":"Binance","currency":"TRB_USDT"}]
*/
//@version=5
strategy('Autotrade.it DCA', overlay=true, pyramiding=999, default_qty_type=strategy.cash, initial_capital=10000, commission_value=0.02)
// Date Ranges
from_month = 1
from_day = 1
from_year = 2021
to_month = 1
to_day = 1
to_year = 9999
start = timestamp(from_year, from_month, from_day, 00, 00) // backtest start window
finish = timestamp(to_year, to_month, to_day, 23, 59) // backtest finish window
window = time >= start and time <= finish ? true : false // create function "within window of time"
source_type = 'OHLC4'
source_function(type) =>
if type == 'Close'
close
else if type == 'Open'
open
else if type == 'High'
high
else if type == 'Low'
low
else if type == 'HL2'
hl2
else if type == 'HL3'
hlc3
else if type == 'OHLC4'
ohlc4
else if type == 'Median Body'
(open + close) / 2
else if type == 'Weighted Close'
(high + low + 2 * close) / 4
else if type == 'Trend Biased'
close > open ? (high + close) / 2 : (low + close) / 2
else if type == 'Trend Biased Extreme'
close > open ? high : low
truncate(number, decimals) =>
factor = math.pow(10, decimals)
int(number * factor) / factor
// Strategy Inputs
price_deviation = input.float(1.0, title='Price deviation to open safety orders (%)', minval=0.0) / 100
take_profit = 1.0 / 100
base_order = 10.0
safe_order = 10.0
safe_order_volume_scale = 1.1
safe_order_step_scale = 1.1
max_safe_order = 30
var current_so = 0
var initial_order = 0.0
var previous_high_value = 0.0
var original_ttp_value = 0.0
// Calculate our key levels
take_profit_level = strategy.position_avg_price * (1 + take_profit)
startTrade = input.int(defval=1, title='Trade Start')
margin = input.float(title='Margin', defval=1, step=1, tooltip='USDT')
leverage = input.int(title='Leverage', defval=50, tooltip='it only used on futures trade')
multi = 1.125
var float multiplier = 1
symbol = str.replace_all(syminfo.ticker, '.P', '')
var float totalMargin = 0.0
var bool isTrade =false
var float totalPrice = 0.0
var int totalTrade = 0
var float totalQtys = 0
var float sellPrice = 0
var float sellQty = 0
// // First Position
if strategy.position_size == 0 and window and source_function(source_type) > 0 and previous_high_value == 0.0
strategy.entry('No Position', strategy.long, qty=base_order / source_function(source_type))
initial_order := source_function(source_type)
current_so := 1
previous_high_value := 0.0
original_ttp_value := 0
original_ttp_value
threshold = 0.0
if safe_order_step_scale == 1.0
threshold := initial_order - initial_order * price_deviation * safe_order_step_scale * current_so
threshold
else
threshold := initial_order - initial_order * ((price_deviation * math.pow(safe_order_step_scale, current_so) - price_deviation) / (safe_order_step_scale - 1))
threshold
// Average Down
if current_so > 0 and source_function(source_type) <= threshold and current_so <= max_safe_order and previous_high_value == 0.0
if(startTrade<=current_so)
margin := math.round(margin * multiplier * 100) / 100
multiplier *= multi
totalMargin += margin
avePrice = (totalPrice/totalTrade)
qty = margin*leverage/close
isTrade := true
totalPrice+=close
totalTrade+=1
totalQtys+=qty
alert('{"category": "linear", "mode": 3, "tradeMode": 0, "symbol": "' + str.tostring(symbol) + '", "leverage": "' + str.tostring(leverage) + '", "side": "Buy", "orderType": "Market", "marketUnit": "quoteCoin", "qty": "' + str.tostring(margin) + '", "reduceOnly": false, "positionIdx": 1 }')
strategy.entry('Trade # ' + str.tostring(current_so) +"---Margin: $" + str.tostring(margin), direction=strategy.long, qty=safe_order * math.pow(safe_order_volume_scale, current_so - 1) / source_function(source_type))
else
strategy.entry('Trade # ' + str.tostring(current_so) +" No position", direction=strategy.long, qty=safe_order * math.pow(safe_order_volume_scale, current_so - 1) / source_function(source_type))
current_so += 1
current_so
// Take Profit!
if take_profit_level <= source_function(source_type) and strategy.position_size > 0 or previous_high_value > 0.0
if(isTrade)
avePrice = totalMargin * leverage / totalQtys * 1.002 // Include fee directly
percentGain = math.round((close - avePrice) / avePrice * 100 * 100) / 100
gain = math.round(percentGain * leverage * totalMargin / 100 * 100) / 100
isTrade := false
sellPrice := avePrice*0.95
sellQty := totalMargin * leverage/sellPrice
loop = current_so-1
testQty = sellQty/loop
strategy.close_all(comment= "Take Profit: $" + str.tostring(gain))
alert('{"category": "linear", "mode": 3, "tradeMode": 0, "symbol": "' + str.tostring(symbol) + '", "leverage": "' + str.tostring(testQty) + '", "side": "Sell", "orderType": "Market", "marketUnit": "baseCoin", "qty": "' + str.tostring(sellQty) + '", "reduceOnly": true, "positionIdx": 1, "loop": "' + str.tostring(loop) + '" }')
else
strategy.close_all(comment='No Position')
current_so := 0
previous_high_value := 0
original_ttp_value := 0
multiplier:=1
totalMargin:=0.0
totalPrice:=0
totalTrade:=0
totalQtys:=0