1
votes

Statement : My strategy requires custom position sizing depending on the market conditions.

Example: It should enter a long with 50% of equity when 2/3 conditions are true and enter with 100% of equity when 3/3 conditions are true.

Problem : I found the "qty" function in strategy.entry but can't figure out a way using it with % of equity.

Any leads over how to do this is appreciated.

Thanks!

1
There is an example on the HOWTOs related to something else that shows this: strategy("BarUpDn Strategy", overlay=true, default_qty_type = strategy.percent_of_equity, default_qty_value = 10) Is that what you're looking for? I think when you use it this way qty will then be used for % of equity. - bajaco
Hey! Thanks for the help, I tried incorporating a similar thing in my code but started facing another set of issues I didn't expect. It'll be really helpful if you could have a look at this: stackoverflow.com/questions/66957626/… - Vatsal Bindal

1 Answers

1
votes

Adapting this script from Backtest Rookies (great resource) a bit, here's how you might be able to do this:

//@version=4
strategy("Conditional Entry", overlay=true,default_qty_type=strategy.percent_of_equity, default_qty_value=20)

longCondition = crossover(sma(close, 14), sma(close, 28))
longTRUE = true
longTRUE2 = true
longTRUE3 = false
currentSize = strategy.position_size[0]


if (longCondition and longTRUE == true and longTRUE2 == true and longTRUE3 == false and currentSize == 0)
    strategy.entry(id="50% Long", long=true, qty=50)
    
if (longCondition and longTRUE == true and longTRUE2 == true and longTRUE3 == true and currentSize == 0)
    strategy.entry(id="100% Long", long=true, qty=100)


strategy.exit('L-SLTP1', '50% Long', stop=10, limit=100, qty_percent=100)
strategy.exit('L-SLTP2', '100% Long', stop=20, limit=100, qty_percent=100)

Note: This uses pure limits and stop losses for exits but maybe you have an indicator that you'd like to follow for that instead. Also - I'd highly recommend including current position size as a condition if you haven't already.