Reputation: 21
Going to be a slightly tricky one to explain. I'm in the process of writing my very first Pine script, its a very simple system using RSI, I'm not expecting it to be successful necessarily I'm just trying to get my head around writing a system using Pine.
In simple terms, the system executes a long position when the RSI reads at or below 25 and a short position when the RSI reads at or above 75. Take profit is 2% or if the RSI reaches overbought/oversold (the opposite of whatever the direction of the position is). Stop loss price will be set at 1.5x the current ATR (not yet coded into the script). My issue is calculating position size:
What I want is to code the system to use a position size where 1.5x the ATR = 1% of the account's value. When manually placing trades, my stop is placed at 1.5x the ATR and then my position size is chosen so that the stop loss is equal to (or as close as possible to) 1% of my total account size. Is there a combination of functions/inputs which would allow me to do this within a Pine script? Apologies if this is poorly explained/not enough info, this is also my first post on Stack Overflow!
Upvotes: 2
Views: 3070
Reputation: 63
So, trailing stop-loss wise, QuantProgram over on YouTube is nicely straightforward. He walks through a code for a fixed-percentage trail (which I've been able to mash it in to an ATR trailing stop-loss.)
This is his code:
//@version=5
strategy('Trailingstop', overlay=true)
if close > ta.sma(close, 50)
strategy.entry('long', strategy.long)
//Trailing stop loss
Trailperc = 0.20
price_stop = 0.0
if strategy.position_size > 0
stopValue = close * (1 - Trailperc)
price_stop := math.max(stopValue, price_stop[1])
price_stop
else
price_stop := 0
price_stop
if strategy.position_size > 0
strategy.exit(id='stoploss', stop=price_stop)
if close < ta.sma(close, 50)
strategy.entry('short', strategy.short)
//Trailing stop loss
price_stop_short = 0.0
if strategy.position_size < 0
stopValue = close * (1 + Trailperc)
price_stop_short := math.min(stopValue, price_stop_short[1])
price_stop_short
else
price_stop_short := 10000000000
price_stop_short
if strategy.position_size < 0
strategy.exit(id='stoploss', stop=price_stop_short)
Upvotes: 0
Reputation: 63
The other responses here are spot on, but since I've had difficulty trying to achieve the same thing recently, I thought I'd chip in.
What you want to do is calculate your position size using (i) your entry price and (ii) your stop price. Have you come across the formula online?
Entry price - stop price = cash at stake per 1 unit of position size.
E - S = DIFF
(Account balance / 100) * percentage account risk per-trade = cash risk per trade.
(BAL/100) * R = CR
Then finally:
CR / DIFF = position size
In Pine, I ended up with the following:
long_diff = entry_price - long_stop
long_tp = entry_price + (i_rewardmulti * long_diff)
balance = (strategy.initial_capital + strategy.netprofit)
bal_limited = (balance > 0 ? balance : 0)
bal_risked = (i_risk/100) * balance_limited
pos_size = (equityrisked / long_diff)
I notice that the strategy.percent_of_equity
function actually returns your initial capital plus your open profit - that is, unrealized profit from any open positions. I don't know about you, but I wanted to risk 1% of my actual account balance, so(strategy.initial_capital + strategy.netprofit)
was the better option.
The bal_limited
bit just makes sure that I don't calculate position size using a negative number if the account goes overdrawn. That'll crash the script.
I've also calculated a take-profit by multiplying the DIFF by the RR I'm looking for - in this case, i_rewardmulti
is a user-input set to a default of 1.5 - and adding that to the entry price.
The stop, profit and position-size values should be plugged in to your strategy.entry()
, strategy.exit()
or strategy.close()
functions. And yes, invert the calculation for shorts.
Let me know if this is of any help - I'm new to Pine too, and this stumped me for a good couple of days.
Best,
(Also) Matt.
Upvotes: 3
Reputation: 865
You can access the strategy equity with this variable: https://www.tradingview.com/pine-script-reference/v5/#var_strategy{dot}equity
Does that help?
Upvotes: 0