Pine量化交易策略:止盈改进与代码实现
创作时间:
作者:
@小白创作中心
Pine量化交易策略:止盈改进与代码实现
引用
1
来源
1.
https://www.cnblogs.com/Mephostopheles/p/18408719
本文将介绍如何通过改进止盈策略来优化Pine量化交易系统。通过对比改进前后的策略效果,我们将展示如何通过代码实现3.0%的止盈百分比,从而在确保一定盈利的情况下退出市场,避免市场行情逆转带来的风险。
什么是止盈
止盈的核心思想是:当市场价格达到设定的目标后,投资者会卖出资产,防止市场波动将已经取得的利润变为损失。通过止盈,投资者在确保一定盈利的情况下退出市场,而不是继续持有以追求更高的收益,从而避免市场行情逆转带来的风险。
止盈的常见方式包括:
- 固定价格止盈:设定一个具体的价格作为目标,一旦价格触及该水平就卖出资产。例如,某只股票买入价是 100,当价格上涨到 120 时卖出,锁定 20% 的利润。
- 百分比止盈:设定一个百分比目标,当资产价格上涨到预期百分比时卖出。例如,设定止盈目标为 10%,当资产价格涨幅达到 10% 时卖出。
- 技术指标止盈:使用技术分析工具(如移动平均线、相对强弱指数 RSI、布林带等)来判断市场趋势的变化,从而选择合适的时机卖出。例如,当 RSI 显示超买信号时,选择止盈。
- 动态止盈(追踪止盈):设定一个价格回撤的比例或金额,随着价格上涨,止盈价格也同步上移。当价格从高点回落到设定的比例时触发卖出。例如,设定 5% 的回撤止盈,当价格从最高点下跌 5% 时卖出。
改进前
定义了三个 EMA,分别为 9、26 和 55 周期的指数移动平均线,三种不同的 Supertrend,分别基于 7、14 和 21 个周期(length1, length2, length3)和不同的乘数(factor1, factor2, factor3)。策略结果如下:
改进后
首先把7和21个周期的Supertrend删去,然后加入止盈,这里止盈的百分比默认为 3.0%,例如,如果入场价是 100,止盈百分比为 3%,那么止盈价格就是 100 * 1.03 = 103。
改进原因
减少因为多信号导致的反应迟缓,3.0%的止盈可以防止在强趋势出现反转之前利润被侵蚀。结果如下图,可以看到各项指数均得到一定幅度的优化,最大回撤是年化收益率的六倍,2023-6-30到2024-7-30有72笔交易,k线周期为1h,符合金星策略。
源代码
/*backtest
start: 2023-06-30 00:00:00
end: 2024-07-30 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("Simplified Golden Crossover with Supertrend and Take Profit", overlay=true)
// Define EMA lengths
ema9_length = 9
ema26_length = 26
ema55_length = 55
// Input parameters for different timeframes
timeFrame9 = input.timeframe('', 'Time Frame - EMA 9')
timeFrame26 = input.timeframe('', 'Time Frame - EMA 26')
timeFrame55 = input.timeframe('', 'Time Frame - EMA 55')
// Request EMA data from specified time frames
ema9 = request.security(syminfo.tickerid, timeFrame9, ta.ema(close, ema9_length))
ema26 = request.security(syminfo.tickerid, timeFrame26, ta.ema(close, ema26_length))
ema55 = request.security(syminfo.tickerid, timeFrame55, ta.ema(close, ema55_length))
// Supertrend function (single Supertrend)
supertrend(length, factor) =>
[superTrend, direction] = ta.supertrend(factor, length)
superTrend
// Supertrend parameters (only one Supertrend for simplicity)
length_supertrend = 14
factor_supertrend = 2
// Supertrend calculation
superTrend = supertrend(length_supertrend, factor_supertrend)
// Plot EMAs on the chart
plot(ema9, color=color.black, title="EMA 9")
plot(ema26, color=color.green, title="EMA 26")
plot(ema55, color=color.red, title="EMA 55")
// Plot Supertrend line on the chart
plot(superTrend, color=color.blue, title="Supertrend")
// Define buy and sell conditions combining EMA crossover and Supertrend
buy_condition_ema = ta.crossover(ema9, ema26) and ema26 > ema55
sell_condition_ema = ta.crossunder(ema9, ema26) and ema26 < ema55
buy_condition_supertrend = close > superTrend
sell_condition_supertrend = close < superTrend
// Combine the conditions (one Supertrend + EMA crossover)
buy_condition = buy_condition_ema and buy_condition_supertrend
sell_condition = sell_condition_ema or sell_condition_supertrend
// Input for Take Profit percentage
take_profit_percent = input.float(3.0, title="Take Profit %", minval=0.1) / 100
// Calculate Take Profit level based on the entry price
take_profit_level = strategy.position_avg_price * (1 + take_profit_percent)
// Execute buy and sell orders
if (buy_condition)
strategy.entry("Buy", strategy.long)
// Close position with Take Profit and normal exit conditions
strategy.exit("Take Profit", "Buy", limit=take_profit_level)
// Sell condition to close the order
if (sell_condition)
strategy.close("Buy")
// Plot buy and sell signals on the chart
plotshape(series=buy_condition, location=location.belowbar, color=color.green, style=shape.arrowup, title="Buy Signal")
plotshape(series=sell_condition, location=location.abovebar, color=color.red, style=shape.arrowdown, title="Sell Signal")
多次测试
(注:原文中“多次测试”部分未完成,因此省略)
热门推荐
如何成为谣言“克星”?这场舆情培训全是干货!
狙击团队如何胜利
全红婵以为不用上学才去的跳水,四跳成绩比别人五跳还高
怎麼和朋友相處?建立深厚友誼的8個技巧
一文解读美联储降息如何影响A股?
医生提示:化疗前后最全饮食营养建议,值得收藏!
高效职场沟通:倾听、表达、反馈全攻略
医疗费用上涨已成全球普遍趋势,该如何应对?
初音未来游戏:融合音乐、偶像与科技的多维体验
坚决清除“害群之马”!财政部:对231家会计师事务所,作出处罚!
Unity3D UGUI工作原理详解:从输入处理到事件响应
助听器芯片解决方案实现了助听与蓝牙的深度整合
痘痘问题全解析:有效改善青春痘的10个方法
“古鼎:历史与文化传承的载体,心灵和谐的源泉”
奶粉和牛奶的区别:从成分到适用人群的全面解析
胃炎患者能吃甜食吗?胃炎患者的饮食注意事项
甘油三酯:你的健康密码,你知道吗?
麸炒白术的功效与作用、禁忌和食用方法
如何准确辨别不同种类的外币?这类外币的识别方法在实际应用中有哪些难点?
人社部官网首页功能详解:六大模块助力企业与个人高效办理业务
从复出道歉到自我救赎,还是那个熟悉的郭艾伦
鲈鱼检测指标都有什么?鲈鱼检测执行什么标准?
鲈鱼苗为什么会跳缸呢?怎么回事?
《白鹿原》:一部跨越时空的史诗巨著
快速支付系统(FPS)全解析:如何实现便捷转账?
电话客服系统平台如何提升呼叫效率?智能分配策略揭秘!
手部疼痛缓解及预防策略:守护手部健康,告别疼痛困扰
如何画蝴蝶:从工具准备到完成作品的详细步骤与技巧
专家:如何看待首付比例下降,房贷利率下调影响多大?
甜菜碱:心脏守护神还是运动表现加速器?揭秘它的多重健康功效!