Calculate Risk Exposure to Bitcoin Pinescript (Tradingview)

Graybox Trading

Jan 02

For traders and investors who believe that Bitcoin may offer a long term value store but who also seek to take advantage of timing opportunities, and manage their risk: the question of risk needs to be addressed.

If a trader is looking at it as a portfolio trade, I have found that adding as little as 7% exposure to Bitcoin can double the returns over holding the S&P 500 index. And, some small amount of exposure from .5% to 10% may be appropriate for some investors.

Others may allocate a trivial amount of savings to Bitcoin that they are willing to take maximum risk on.

However, the other way of looking at it is a trader who has a more significant amount of capital that they want to allocate to Bitcoin while still managing risk. The question is how to know how much to allocate to Bitcoin at any given time. A part of that is question of timing and trading edge which I won’t address here. But, the other part is purely a question of risk or volatility.

I created the Pinescript below for Tradingview that allows the user to set a multiple of the ATR to calculate a dollar risk and translate that into a maximum exposure risk for a given account size. The objective is to reduce the probability of having a large adverse excursion greater then a given percentage or dollar risk. Importantly, it is a quantitative measure that can inform regardless of emotion and sentiment. Of course, if the range explodes then it is very possible to experience a greater risk. It is only suggesting a given level of exposure for a given ATR threshold, i.e. assuming all things constant.

The user must tune the percent risk, account size, max bitcoins, and ATR to match their risk tolerance exposure level. The objective is to tune your percent risk to some multiple of the ATR risk.

//BeyondBacktesting ATR RISK Bitcoin
study(title="ATR Risk Bitcoin", shorttitle="AtrRiskBitCoin", overlay=false, resolution="")
length = input(title="Length", defval=7, minval=1)
smoothing = input(title="Smoothing", defval="RMA", options=["RMA", "SMA", "EMA", "WMA"])
maxriskpercent = input(title="PercentRisk", defval=5.00, minval=1)
accountsize = input(title="AccountSize", defval=100000, minval=1)
maxbitcoins = input(title="MaxBitcoins", defval=5, minval=1)
atrmult = input(title="AtrMult", defval=2.0, minval=0.1)
ma_function(source, length) =>
	if smoothing == "RMA"
		rma(source, length)
		if smoothing == "SMA"
			sma(source, length)
			if smoothing == "EMA"
				ema(source, length)
				wma(source, length)

var maxriskaccount = (maxriskpercent/100)*accountsize

plot( iff( (maxriskaccount/ (ma_function(tr(true), length) * atrmult) ) > maxbitcoins, maxbitcoins,maxriskaccount/ (ma_function(tr(true), length)*atrmult)) , title = "ATR",,  style=plot.style_histogram,  transp=0)
plot(1, "1 btc",
plot(2, "2 btc",
plot(0, "0 btc",

About the Author

The author is passionate about markets. He has developed top ranked futures strategies. His core focus is (1) applying machine learning and developing systematic strategies, and (2) solving the toughest problems of discretionary trading by applying quantitative tools, machine learning, and performance discipline. You can contact the author at