TradingView 策略测试程序交易列表按执行顺序显示已平仓交易

发布于 2025-01-16 01:26:20 字数 1407 浏览 1 评论 0原文

我试图了解如何制定一个可以使用金字塔并拥有多个订单 ID 的策略。这样我就可以使用指定的 ID 和正确的注释退出交易。

有没有办法以不同的顺序进入和退出交易?

在此处输入图像描述

我编写了这段代码来使用 unqiue id 进行测试。我想看看我是否可以在限价和止损的情况下退出交易。但在贸易列表中,一切看起来都是按时间排序的。因此,进入是有序的,退出也是按时间排序的。因此,当我不按顺序退出交易时,交易列表中将会有不同类型的交易混合在一起。我预计只有 A 入口和 A 出口。

//@version=5
strategy("My strategy", overlay=true, precision=2, pyramiding=100, commission_type=strategy.commission.percent, commission_value=0.02, initial_capital=100, currency=currency.USD, default_qty_type=strategy.cash, calc_on_order_fills=false, calc_on_every_tick=false, process_orders_on_close=false, slippage=0, backtest_fill_limits_assumption=200)

s(value) => str.tostring(value)
p(value, percentage) => value * (1 + (percentage / 100))

id(id) => id + ' ' + s(bar_index)

limitA=p(close, 2)
stopA=p(close, -2)
limitB=p(close, 4)
stopB=p(close, -4)
    
if (bar_index % 20 == 0)
    strategy.entry(id('A'), strategy.long, qty=0.001, comment=id('A') + ' ' + s(close))
    strategy.exit(id('Exit-A'), id('A'), limit=limitA, stop=stopA, comment=id('A') + ' ' + s(limitA) + "/" + s(stopA))

if (bar_index % 20 == 10)
    strategy.entry(id('B'), strategy.long, qty=0.002, comment=id('B') + ' ' + s(close))
    strategy.exit(id('Exit-B'), id('B'), limit=limitB, stop=stopB, comment=id('B') + ' ' + s(limitB) + "/" + s(stopB))

I'm trying to understand how i can make a strategy where i can use pyramiding and have multiple order ids. So that i can exit a trade with a specified ID and correct comments.

Is there a way to enter and exit trades in different order ?

enter image description here

I have writen this piece of code to test with unqiue ids. I am trying to see, if i can exit a trade with limit and stoploss. But in the Tradelist everything looks ordered in time. Thus the entries are ordered and the exit`s are ordered in time. Thus when i exit a trade out of sequense, the trade list will have different types of trades mixed together. I expected only A entries with A exits.

//@version=5
strategy("My strategy", overlay=true, precision=2, pyramiding=100, commission_type=strategy.commission.percent, commission_value=0.02, initial_capital=100, currency=currency.USD, default_qty_type=strategy.cash, calc_on_order_fills=false, calc_on_every_tick=false, process_orders_on_close=false, slippage=0, backtest_fill_limits_assumption=200)

s(value) => str.tostring(value)
p(value, percentage) => value * (1 + (percentage / 100))

id(id) => id + ' ' + s(bar_index)

limitA=p(close, 2)
stopA=p(close, -2)
limitB=p(close, 4)
stopB=p(close, -4)
    
if (bar_index % 20 == 0)
    strategy.entry(id('A'), strategy.long, qty=0.001, comment=id('A') + ' ' + s(close))
    strategy.exit(id('Exit-A'), id('A'), limit=limitA, stop=stopA, comment=id('A') + ' ' + s(limitA) + "/" + s(stopA))

if (bar_index % 20 == 10)
    strategy.entry(id('B'), strategy.long, qty=0.002, comment=id('B') + ' ' + s(close))
    strategy.exit(id('Exit-B'), id('B'), limit=limitB, stop=stopB, comment=id('B') + ' ' + s(limitB) + "/" + s(stopB))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧话新听 2025-01-23 01:26:20

该问题可以通过声明 strategy() 函数的 close_entries_rule= 参数来解决。默认情况下,它是“先进先出”(FIFO),因此无论触发哪个退出信号 - 它将从第一个条目关闭指定的金额,如果还有剩余 - 它将移动 通过声明 close_entries_rule='ANY'

您可以强制策略以任何顺序平仓,这样它们就会链接到指定的 ID。

strategy("My strategy", ... , close_entries_rule = 'ANY')

输入图片此处描述

The issue could be resolved by declaring the close_entries_rule= parameter of the strategy() function. By default, it's 'First-In-First-Out' (FIFO), so whichever exit signal is triggered - it will close the specified amount from the first entry, if anything is left - it will move to the second entry etc.

By declaring close_entries_rule='ANY' you can force the strategy to close the positions in any order, that way they will be linked to the specified ID.

strategy("My strategy", ... , close_entries_rule = 'ANY')

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文