如何计算以整数位为TP目标的交易盈利?附Pine Script脚本
整数位止盈目标计算方案及脚本优化
一、止盈目标与盈利计算逻辑
核心规则
以设置的整数位间隔(如黄金的5美元)为基准:
- 做多:取入场价上方第N个整数位作为止盈(TP),目标盈利=TP价格-入场价
- 做空:取入场价下方第N个整数位作为止盈,目标盈利=入场价-TP价格
分步计算示例(以黄金、间隔5美元、N=2为例)
做多场景(入场价1788.800)
- 找到入场价上方最近的整数位:
math.ceil(1788.8 / 5) * 5 = 358 * 5 = 1790.000 - 向上偏移1个间隔(第2个整数位):
1790.000 + (2-1)*5 = 1795.000 - 目标盈利:
1795.000 - 1788.800 = 6.200美元
做空场景(入场价1802.300)
- 找到入场价下方最近的整数位:
math.floor(1802.3 / 5) *5 = 360 *5 = 1800.000 - 向下偏移1个间隔(第2个整数位):
1800.000 - (2-1)*5 = 1795.000 - 目标盈利:
1802.300 -1795.000=7.300美元
二、整合计算逻辑的Pine Script脚本
以下是在你原有网格脚本基础上,添加止盈计算与标注功能的汉化版本:
//@version=5 indicator("整数位止盈网格", overlay=true) // 网格线基础设置 line_col = input(color.gray, title ="网格线颜色") line_width = input.int(2, title = "网格线宽度", minval = 1, maxval = 5) line_count = input.int(title="网格线数量", defval=10) line_count_2 = math.floor(line_count / 2) // 交易参数设置(手动输入) entry_price = input.float(title="入场价", defval=1788.8, tooltip="输入你的实际入场价格") tp_offset = input.int(title="止盈偏移整数位数量", defval=2, tooltip="做多取上方N个,做空取下方N个") trade_type = input.string(title="交易方向", options=["做多", "做空"], defval="做多") step_size = input.float(title="整数位间隔", defval=5, tooltip="黄金设5,EURUSD设0.001,US30设50") // 止盈价格计算函数 calculate_tp(entry, offset, step, is_long) => if is_long nearest_upper = math.ceil(entry / step) * step tp = nearest_upper + (offset - 1) * step else nearest_lower = math.floor(entry / step) * step tp = nearest_lower - (offset - 1) * step tp tp_price = calculate_tp(entry_price, tp_offset, step_size, trade_type == "做多") profit = trade_type == "做多" ? tp_price - entry_price : entry_price - tp_price // 在图表最后一根K线显示止盈与盈利 var label tp_label = na if barstate.islast tp_label := label.new(bar_index, tp_price, str.format("TP: {0}\n盈利: {1}", tp_price, profit), color=color.new(color.green, 0), textcolor=color.white, style=label.style_label_down) // 整数位网格绘制函数 draw_grid(sym, show, steps, use_custom, custom_price) => if sym == syminfo.prefix + ":" + syminfo.ticker and show for i = 0 to line_count - 1 price = use_custom ? custom_price : close grid_line = math.ceil(price / steps) * steps + (i * steps) - (line_count_2 * steps) line.new(bar_index, grid_line, bar_index - 1, grid_line, xloc=xloc.bar_index, extend=extend.both, color=line_col, width=line_width, style=line.style_dotted) // 品种1:EURUSD 默认设置 show_eurusd = input.bool(title="显示EURUSD网格", defval=true, inline="eurusd") eurusd_sym = input.symbol("EURUSD",title=" ", inline="eurusd") eurusd_step = input.float(title="间隔", defval=0.001, inline="eurusd") eurusd_use_custom = input.bool(title="自定义基准价", defval=false, inline="eurusd_custom") eurusd_custom_price = input.float(title="", defval=1.08, inline="eurusd_custom") // 品种2:黄金 默认设置 show_gold = input.bool(title="显示黄金网格", defval=true, inline="gold") gold_sym = input.symbol("GOLD",title=" ", inline="gold") gold_step = input.float(title="间隔", defval=5, inline="gold") gold_use_custom = input.bool(title="自定义基准价", defval=false, inline="gold_custom") gold_custom_price = input.float(title="", defval=1900, inline="gold_custom") // 品种3:道指US30 默认设置 show_us30 = input.bool(title="显示US30网格", defval=true, inline="us30") us30_sym = input.symbol("US30",title=" ", inline="us30") us30_step = input.float(title="间隔", defval=50, inline="us30") us30_use_custom = input.bool(title="自定义基准价", defval=false, inline="us30_custom") us30_custom_price = input.float(title="", defval=3500, inline="us30_custom") // 调用网格绘制函数 draw_grid(eurusd_sym, show_eurusd, eurusd_step, eurusd_use_custom, eurusd_custom_price) draw_grid(gold_sym, show_gold, gold_step, gold_use_custom, gold_custom_price) draw_grid(us30_sym, show_us30, us30_step, us30_use_custom, us30_custom_price)
脚本功能说明
- 保留原有的多品种整数位网格绘制功能
- 添加交易参数输入栏,支持手动录入入场价、止盈偏移数、交易方向和间隔
- 自动计算止盈价格与目标盈利,并在图表末端标注显示
三、手动计算步骤(无需脚本)
- 确定当前品种的整数位间隔(如黄金5、EURUSD0.001)
- 做多计算:
- 入场价 ÷ 间隔 → 向上取整 → 乘以间隔,得到最近上方整数位
- 最近上方整数位 + (N-1)×间隔 = 第N个上方止盈位
- 盈利=止盈位-入场价
- 做空计算:
- 入场价 ÷ 间隔 → 向下取整 → 乘以间隔,得到最近下方整数位
- 最近下方整数位 - (N-1)×间隔 = 第N个下方止盈位
- 盈利=入场价-止盈位
内容的提问来源于stack exchange,提问作者marcq
相关产品推荐
相关产品推荐

