我想在Pinescript中测试一个策略,它使用任意数量的现金来创建订单。例如,买入100美元的标普500。尽管遵循了教程,但它似乎不适合我。
我使用以下策略定义:
strategy("BarUpDn Strategy", overlay=true, default_qty_type = strategy.cash, currency=currency.USD, default_qty_value = 50, initial_capital = 1000, pyramiding = 9999)
这意味着当创建订单/做多时,应该使用50美元作为进场头寸。但是,如果我有
strategy.order("BarUp", strategy.long)
然后,当运行策略时,我看到在策略测试器中没有创建订单>行业清单。现在,如果我添加一个显式数量,它会使用这个数量作为合同号,而不是现金号。例如
strategy.order("BarUp", strategy.long,1)
这导致创建了一个订单,但它是1个合约(s&p 500的4K美元)而不是1美元,这是我的策略定义的quantity_type。
这似乎是一个超级基本的用例,但我不能让它工作。
完整的代码片段,应该能够重现任何股票的问题:
//@version=5
strategy("BarUpDn Strategy", overlay=true, default_qty_type = strategy.cash, currency=currency.USD, default_qty_value = 50, initial_capital = 1000, pyramiding = 9999)
// STEP 1. Create inputs that configure the backtest's date range
useDateFilter = input.bool(true, title="Filter Date Range of Backtest",
group="Backtest Time Period")
backtestStartDate = input.time(timestamp("1 Jan 2021"),
title="Start Date", group="Backtest Time Period",
tooltip="This start date is in the time zone of the exchange " +
"where the chart's instrument trades. It doesn't use the time " +
"zone of the chart or of your computer.")
backtestEndDate = input.time(timestamp("14 Nov 2022"),
title="End Date", group="Backtest Time Period",
tooltip="This end date is in the time zone of the exchange " +
"where the chart's instrument trades. It doesn't use the time " +
"zone of the chart or of your computer.")
// STEP 2. See if current bar falls inside the date range
inTradeWindow = not useDateFilter or (time >= backtestStartDate and
time < backtestEndDate)
var currMonth = -1
var prevMonth = 0
if inTradeWindow
strategy.order("BarUp", strategy.long)
else
strategy.close_all()
如果strategy.cash
指定为default_qty_type
,则qty
的值必须大于证券的价格(close * syminfo.pointvalue)。这证券的价格已经超过50美元了,你应该加价。
strategy.order()
函数的qty
参数指定要交易的合约/股份/手数/单位的数量(并重新定义默认策略参数),因此它不是现金数量,而是合约数量。如果你想重写它,你可以从证券价格中计算合约的数量。