so I am trying to create a simple trading strategy based on RSI overbought and oversold levels (sell overbought levels and buy oversold levels). And I want to be able to sell 20% of the open trade and buy 40% e.g. (buy/sell at different percentages) with pyramiding. I tried it out in excel (manually inserted prices) and liked the results so i want to automate this in Tradingview.
So in the case that the RSI goes under, lets say 40, it opens a trade with a 40% position, and in the case it goes over 90 it closes a 20% position. Additionally, I am trying to make the trades in one direction cumulate if a trade in the opposite direction didn't happen (let's say a sell RSI 90 signal is triggered two times in succession).
In my code I have gotten it to act the way i want to on the strategy.entry() side with pyramiding. If RSI goes under 40 multiple times it triggers a buy multiple times, but with pyramiding and strategy.close() I'm only able to get it to work once. So if a RSI over 90 signal happens two times in succession, the sell signal is only triggered at the first occurrence.
If I change the .close to instead sell short, I imagine the shorts would cumulate, but it is not exactly what I'm after. How can I get this to work with pyramiding, or is there another way to get the same result? And I'm also having some difficulties with adding a separate position sizing for long and short trades. Thanks in advance.
// INPUTS
rsi_low = input(40, title ="RSI low", minval=5, step = 1)
rsi_high = input(90, title ="RSI top", minval=30, step = 1)
rsi_period = input(14, title="RSI period", minval = 1, step = 1)
// CALCULATIONS
myrsi = rsi(close, rsi_period)
strategy.entry("Long", true, when = crossover(myrsi,rsi_low))
strategy.close("Long", when = crossunder(myrsi,rsi_high))
plotchar(myrsi, title = "myrsi", char='+', color=color.black)