pine 策略交易显示label
编辑
0
2025-07-21

背景
pine 策略交易显示label
代码
pine脚本记录
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=6
strategy("我的策略", overlay=true, fill_orders_on_standard_ohlc = true)
longCondition = ta.crossover(ta.sma(close, 14), ta.sma(close, 28))
if (longCondition)
strategy.entry("My Long Entry Id", strategy.long)
shortCondition = ta.crossunder(ta.sma(close, 14), ta.sma(close, 28))
if (shortCondition)
strategy.entry("My Short Entry Id", strategy.short)
f_tradeAnalysis(_tradeNum, _closedTrades = true, _lblSze = size.normal) =>
if barstate.islastconfirmedhistory or barstate.isrealtime
_entryPrice = _closedTrades ? strategy.closedtrades.entry_price(_tradeNum) : strategy.opentrades.entry_price(_tradeNum)
_entryBarindex = _closedTrades ? strategy.closedtrades.entry_bar_index(_tradeNum) : strategy.opentrades.entry_bar_index(_tradeNum)
_entryTime = _closedTrades ? strategy.closedtrades.entry_time(_tradeNum) : strategy.opentrades.entry_time(_tradeNum)
_positionSize = _closedTrades ? strategy.closedtrades.size(_tradeNum) : strategy.opentrades.size(_tradeNum)
_profit = _closedTrades ? strategy.closedtrades.profit(_tradeNum) : strategy.opentrades.profit(_tradeNum)
_maxRunUp = _closedTrades ? strategy.closedtrades.max_runup(_tradeNum) : strategy.opentrades.max_runup(_tradeNum)
_maxDrawdown = _closedTrades ? strategy.closedtrades.max_drawdown(_tradeNum) : strategy.opentrades.max_drawdown(_tradeNum)
_exitPrice = _closedTrades ? strategy.closedtrades.exit_price(_tradeNum) : na
_exitBarindex = _closedTrades ? strategy.closedtrades.exit_bar_index(_tradeNum) : na
_exitTime = _closedTrades ? strategy.closedtrades.exit_time(_tradeNum) : na
_entryText = "Entry # {0} @ {1}\n{2} {3} {4}"
_entryText := str.format(_entryText, _tradeNum, _entryPrice, _positionSize > 0 ? "Long" : "Short", _positionSize, syminfo.basecurrency)
_l_entry = label.new(_entryTime, _entryPrice, _entryText, xloc.bar_time, color = color.black, textcolor = color.white, style = _positionSize > 0 ? label.style_label_upper_right : label.style_label_upper_right, size = _lblSze, textalign = text.align_right)
_exitText = "Exit # {0} @ {1}\nProfit:{2, number, #.#########}\nMax Run Up: {3}\n Max Drawdown: -{4}"
_exitText := str.format(_exitText, _tradeNum, _entryPrice, strategy.convert_to_account(_profit), _maxRunUp, _maxDrawdown)
_l_exit = label.new(_exitTime, _exitPrice, _exitText, xloc.bar_time, color = _profit > 0 ? color.green : color.red, textcolor = color.black, style = _positionSize > 0 ? label.style_label_upper_left : label.style_label_upper_left, size = _lblSze, textalign = text.align_left)
i_tradeNum = input.int(1, "Trade #", minval =1 ) -1
i_openTrades = not input.bool(false, "Show Open Trades")
f_tradeAnalysis(i_tradeNum, _lblSze = size.large)
默认的sma策略,14个bar上穿28个bar就做多,下穿就做空。
增加了f_tradeAnalysis自定义函数,画上label,记录开仓时间,仓位,价格,平仓时间,收益,最大涨幅,最大回撤等。
设置输入24,指的显示第24次的交易记录,可以看到对应 开空,4月28日到6月23日出场,亏损7210刀
图中也有对应的label#23(第一次是#0),亏损为红,可以修改为其他顺序的交易,盈利则是绿色。
- 0
- 0
-
赞助
支付宝
微信
-
分享