Reputation: 21
I want to test a strategy in Pinescript which uses arbitrary amount of cash to create orders. For example, buy 100 USD of S&P 500. Despite following the tutorial it doesn't seem to be working for me.
I'm using the following strategy definition:
strategy("BarUpDn Strategy", overlay=true, default_qty_type = strategy.cash, currency=currency.USD, default_qty_value = 50, initial_capital = 1000, pyramiding = 9999)
This means that when creating an order / going long, it should be using 50 USD as enter position. However, when if I have
strategy.order("BarUp", strategy.long)
Then when running the strategy I see no order being created in the Strategy Tester > List of Trades. Now if I add an explicit quantity, it uses the quantity as the Contract number, but not the cash number. e.g.
strategy.order("BarUp", strategy.long,1)
This results in an order getting created, but its 1 contract (4K usd for s&p 500) and not 1 USD, which is the quantity_type that my strategy defines.
This seems like an ultra basic use case but I'm not able to get it to work.
Full code snippet that should be able to reproduce the problem for any stock:
//@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()
Upvotes: 0
Views: 199
Reputation: 1961
If strategy.cash
is specified as the default_qty_type
, then the qty
value must be greater than the price of the security (close * syminfo.pointvalue).
Price of the security is more than 50$, so you should increase it.
qty
parameter of the strategy.order()
function specifies the number of contracts/shares/lots/units to trade (and redefines default strategy parameters) so it is not amout of cash, but number of contracts. If you want to override it, you can calculate number of contracts from the security price.
Upvotes: 1