Reputation: 63
I've a finished strategy in Pine that backtests well, and I'd like to automate it by sending alerts to Wundertrading. The strategy calculates a custom position-size for each order, based on the current equity and a percentage per-order to risk. The position-size is passed to the 'qty' parameter of the 'strategy.entry' function.
My issue is that WunderTrading interprets all 'qty' values as cash, rather than contracts, and that (as far as I know) the 'strategy.entry' function does the opposite.
I can calculate the cash-value of orders in TV, but as soon as I enter that in to the 'strategy.entry' function, the strategy's calculations are thrown off, affecting the instructions it sends on to Wundertrading.
Consider a long trade calculated at a quantity of 1 ETH by my strategy. As it is, Wundertrading would read this as an instruction to purchase $1 of Ethereum. So I calculate the cash value - say, $1600 - and enter that in to the 'qty' parameter of the relevant functions. Tradingview now reads the order as purchasing 1600 ETH!
That would be fine if the same orders were executed in both cases. But, clearly, whether Tradingview records a successful long for 1 ETH or for 1600 ETH has a drastic effect on the next order it sends out.
It seems there's no way of getting WunderTrading to accept custom orders in a contract format. Is there a way to get Tradingview to accept a custom cash-value amount for each order?
Update 1
Mr Statler below has suggested altering default_qty_type
to strategy.cash
, but doing so doesn't solve the problem. Here's an example of the strategy running with positions in contract format:
And here's an example of the same strategy with the strategy.cash
alteration, plus a few minor necessities:
I may need to post the scripts - I suspect there's something fundamentally wrong with the way I've handled position size.
Update 2
Here's an example of some of the relevant code for the script that calculates position size in contracts:
long_position_opened = strategy.position_size == 0 and long_condition
short_position_opened = strategy.position_size == 0 and short_condition
entry_price = ta.valuewhen(long_condition or short_condition, close, 0)
long_diff = (ta.valuewhen(long_position_opened, math.abs(stopvalue - entry_price), 0))
short_diff = (ta.valuewhen(short_position_opened, math.abs(entry_price - short_stopvalue),0))
balance = (strategy.initial_capital + strategy.netprofit)
balance_limited = (balance > 0 ? balance : 0)
equityrisked = (i_risk/100) * balance_limited
pos_size = (equityrisked / long_diff)
short_pos_size = (equityrisked / short_diff)
And the code for position size in cash is the same, except for (1) default_qty_type
and (2) the following:
pos_size = (equityrisked / long_diff) * entry_price
short_pos_size = (equityrisked / short_diff) * entry_price
Upvotes: 0
Views: 902
Reputation: 2171
You can change the default_qty_type
argument in your strategy()
:
default_qty_type (const string) Specifies the units used for
default_qty_value
. Possible values are: strategy.fixed for contracts/shares/lots, strategy.cash for currency amounts, or strategy.percent_of_equity for a percentage of available equity. This setting can also be changed in the strategy's "Settings/Properties" tab. Optional. The default is strategy.fixed.
Upvotes: 1