Reputation: 1
Hello there, I am trying to test my strategy with a pinescript and would need some help please.
If the price of the S&P500 is obove the 200-days-moving-average i'm in the market with all my capital. If it drops below, i'm selling everything. No short position. That's it, quite simple. But i don't get it how i can set the position size to my current equity.
// S&P 500 Strategie Test
strategy("S&P500 3x", initial_capital=10000)
// 200-day-moving-average
ema200 = sma(close, 200)
// conditions
long = close > ema200
short = close < ema200
positionSize = abs(strategy.equity)
strategy.entry("Long", strategy.long, qty=postionSize, when = long)
strategy.close("Long", when = short)
Script could not be translated from: |B|var initialCapital = strategy.equity|E|
|EMPTY_V1|
In a next step, i want to see what happens if i leverage my position. To do this i want to multiply the profit/loss of each trade with a factor of let's say 3. Anyone got an idea how to set this up? Maybe save the profit/loss, multiply it and then add it to the total equity, if such a thing is possible. Simply multiplying the initial capital by 3 doesn't work for me.
Thank you in advance!!!
Upvotes: 0
Views: 1569
Reputation: 1714
You can set default order size as 100% from equity in function strategy()
(https://www.tradingview.com/pine-script-reference/v4/#fun_strategy) with default_qty_type = strategy.percent_of_equity
(https://www.tradingview.com/pine-script-reference/v4/#var_strategy{dot}percent_of_equity) and default_qty_value = 100
Upvotes: 1