Tradingview里的strategy.entry()、strategy.order、strategy.exit()总结说明

在进行TV回测过程中遇到复杂的入场与离场挂单指令处理难题。为解决这一问题,作者通过制作思维导图的方式梳理了挂单指令的工作流程及其实现细节。

写TV回测的过程中,遇到 了多个入场、离场挂单指令,实在头疼,于是花一上午做了思维导图 。
过程匆忙,难免有错。
在这里插入图片描述

//@version=5 strategy("激进马丁策略", overlay=true, margin_long=100, margin_short=100) // 参数设置 初始手数 = input.float(1.0, "初始手数") 加仓倍数 = input.float(2.0, "加仓倍数", minval=1.1) 最大加仓次数 = input.int(10, "最大加仓次数", minval=1) 基准止盈 = input.float(1.0, "基准止盈(%)") / 100 止盈递减 = input.float(0.2, "止盈递减(%)") / 100 // 系统变量 var float 当前手数 = 初始手数 var int 加仓次数 = 0 var float 持仓均价 = na // 交易信号(示例用简单均线) fastMA = ta.sma(close, 5) slowMA = ta.sma(close, 20) 做多信号 = ta.crossover(fastMA, slowMA) 做空信号 = ta.crossunder(fastMA, slowMA) // 仓位管理 if strategy.position_size == 0 当前手数 := 初始手数 加仓次数 := 0 持仓均价 := na // 交易执行 if 做多信号 strategy.entry("Buy", strategy.long, qty=当前手数) 持仓均价 := strategy.position_avg_price if 做空信号 strategy.entry("Sell", strategy.short, qty=当前手数) 持仓均价 := strategy.position_avg_price // 加仓逻辑 if strategy.position_size > 0 and strategy.closedtrades.profit(0) < 0 加仓次数 := 加仓次数 + 1 if 加仓次数 <= 最大加仓次数 当前手数 := 当前手数 * 加仓倍数 strategy.entry("加仓"+str.tostring(加仓次数), strategy.long, qty=当前手数) // 动态止盈 止盈点 = 持仓均价 * (1 + 基准止盈 - 止盈递减*加仓次数) if strategy.position_size > 0 strategy.exit("止盈", "Buy", limit=止盈点)以上代码增加以下的内容 增加波动率过滤:使用ATR指标在市场高波动时暂停加仓 时间衰减机制:超过设定时间强制平仓 盈利重启:累计盈利达到阈值后重置仓位 跨品种对冲:在关联品种上建立对冲头寸 建议先用模拟账户进行至少3个月不同市场环境测试,再考虑实盘应用。实际使用时应根据账户规模动态调整初始手数,建议单次初始风险不超过总资金的0.5%。
03-19
//@version=5 strategy("增强型马丁策略", overlay=true, margin_long=100, margin_short=100, pyramiding=10) //======= 基础参数 ======= 初始手数 = input.float(1.0, "初始手数", minval=0.1) 加仓倍数 = input.float(2.0, "加仓倍数", minval=1.1) 最大加仓次数 = input.int(10, "最大加仓次数", minval=1) 基准止盈 = input.float(1.0, "基准止盈(%)") / 100 止盈递减 = input.float(0.2, "止盈递减(%)") / 100 //======= 新增风控参数 ======= atr长度 = input(14, "ATR周期") atr阈值 = input(2.0, "ATR阈值(点)") 最大持仓小时 = input(48, "最大持仓时间(小时)") 盈利目标 = input(5.0, "重启盈利(%)") / 100 对冲品种 = input.string("BINANCE:BTCUSDT", "对冲品种") 对冲比例 = input.float(0.5, "对冲比例", step=0.1) //======= 系统变量 ======= var float 当前手数 = 初始手数 var int 加仓次数 = 0 var float 持仓均价 = na var int 开仓时间 = na var bool 允许加仓 = true //======= 指标计算 ======= fastMA = ta.sma(close, 5) slowMA = ta.sma(close, 20) 做多信号 = ta.crossover(fastMA, slowMA) 做空信号 = ta.crossunder(fastMA, slowMA) atrValue = ta.atr(atr长度) 允许加仓 := atrValue <= atr阈值 //======= 仓位管理 ======= if strategy.position_size == 0 当前手数 := 初始手数 加仓次数 := 0 持仓均价 := na 开仓时间 := time 允许加仓 := true //======= 交易执行 ======= if 做多信号 and strategy.position_size == 0 strategy.entry("Buy", strategy.long, qty=当前手数) 持仓均价 := strategy.position_avg_price if 做空信号 and strategy.position_size == 0 strategy.entry("Sell", strategy.short, qty=当前手数) 持仓均价 := strategy.position_avg_price //======= 增强型加仓逻辑 ======= if strategy.position_size > 0 if 允许加仓 and (time - 开仓时间) < (最大持仓小时 * 60 * 60 * 1000) if strategy.closedtrades.profit(0) < 0 加仓次数 := 加仓次数 + 1 if 加仓次数 <= 最大加仓次数 当前手数 := 当前手数 * 加仓倍数 strategy.entry("加仓"+str.tostring(加仓次数), strategy.long, qty=当前手数) if (time - 开仓时间) >= (最大持仓小时 * 60 * 60 * 1000) strategy.close_all() alert("达到最大持仓时间,强制平仓") //======= 动态止盈 ======= 止盈点 = 持仓均价 * (1 + 基准止盈 - 止盈递减*加仓次数) if strategy.position_size > 0 strategy.exit("止盈", "Buy", limit=止盈点) //======= 盈利重启机制 ======= if strategy.netprofit >= strategy.initial_capital * 盈利目标 strategy.close_all() alert("达到盈利目标,重启系统") 当前手数 := 初始手数 加仓次数 := 0 //======= 跨品种对冲 ======= 对冲品种价格 = request.security(对冲品种, timeframe.period, close) 对冲手数 = strategy.position_size * 对冲比例 if strategy.position_size != 0 strategy.order("对冲", strategy.short, qty=对冲手数, symbol=对冲品种)错误于 5:1 no viable alternative at character '初'
03-20
请把下面的代码尾部都带上分号//@version=5 strategy("Optimized MACD+KDJ Strategy", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_type=strategy.commission.percent, commission_value=0.1) // 1. 参数优化 - 使用input()函数替代已弃用的study() fastLen = input.int(12, "MACD Fast Length", minval=5, maxval=26) slowLen = input.int(26, "MACD Slow Length", minval=12, maxval=50) sigLen = input.int(9, "Signal Length", minval=5, maxval=20) kdjLen = input.int(14, "KDJ Length", minval=5, maxval=30) smoothK = input.int(3, "K Smoothing", minval=1, maxval=10) smoothD = input.int(3, "D Smoothing", minval=1, maxval=10) // 2. 指标计算 - 使用ta前缀函数提高效率 [macdLine, signalLine, _] = ta.macd(close, fastLen, slowLen, sigLen) macdBull = ta.crossover(macdLine, signalLine) macdBear = ta.crossunder(macdLine, signalLine) // 3. KDJ计算优化 - 避免重复计算 lowestLow = ta.lowest(low, kdjLen) highestHigh = ta.highest(high, kdjLen) rsv = 100 * (close - lowestLow) / (highestHigh - lowestLow) k = ta.sma(rsv, smoothK) d = ta.sma(k, smoothD) j = 3 * k - 2 * d kdjBull = ta.crossover(k, d) and k < 80 kdjBear = ta.crossunder(k, d) and k > 20 // 4. 复合信号条件 - 使用布尔变量简化逻辑 longCondition = macdBull and kdjBull shortCondition = macdBear and kdjBear // 5. 交易执行 - 使用strategy.entry替代已弃用的order函数 if (longCondition) strategy.entry("Long", strategy.long) if (shortCondition) strategy.entry("Short", strategy.short) // 6. 可视化优化 - 使用plotcandle替代plot plotcandle(open, high, low, close, color = longCondition ? color.green : shortCondition ? color.red : na)
08-16
//@version=5 strategy("Hull+HalfTrend+CCI Strategy", overlay=true, pyramiding=3) // 1. 指标计算 // Hull Suite(以Hull移动平均为例) hullLength = input(14, "Hull周期") hullMA = ta.hma(close, hullLength) // HalfTrend指标(需导入社区脚本或自定义) [trendDir, buySignal, sellSignal] = request.security("", "D", ta.halfTrend(close, 5, 4, 0)) // 参数需调整 // CCI指标 cciLength = input(20, "CCI周期") cciValue = ta.cci(close, cciLength) // 2. 开仓条件 longCondition = (trendDir == 1) and // HalfTrend显示上升趋势 (close > hullMA) and // 价格在Hull均线上方 (cciValue > -100) and // CCI突破超卖区 (ta.crossover(cciValue, -100)) // CCI上穿关键阈值 shortCondition = (trendDir == -1) and // HalfTrend显示下降趋势 (close < hullMA) and // 价格在Hull均线下方 (cciValue < 100) and // CCI突破超买区 (ta.crossunder(cciValue, 100)) // CCI下穿关键阈值 // 3. 分批止盈策略 entryPrice = strategy.position_avg_price profitTarget1 = entryPrice * 1.015 // +1.5% profitTarget2 = entryPrice * 1.03 // +3% profitTarget3 = entryPrice * 1.05 // +5% // 4. 执行交易 if (longCondition) strategy.entry("Long1", strategy.long, qty=40) // 40%仓位 if (shortCondition) strategy.entry("Short1", strategy.short, qty=40) // 分批止盈逻辑 strategy.exit("TP1", "Long1", qty_percent=30, profit=profitTarget1) strategy.exit("TP2", "Long1", qty_percent=30, profit=profitTarget2) strategy.exit("TP3", "Long1", profit=profitTarget3) // 剩余40%将上述策略转换成MT4EA
最新发布
10-18
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值