Vatsal Bindal
Vatsal Bindal

Reputation: 21

Custom position sizing(Trading view pine script)

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!

Upvotes: 1

Views: 1315

Answers (1)

sgdata
sgdata

Reputation: 2763

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.

Upvotes: 1

Related Questions