zmalnajjar 1 posts msg #156878  Ignore zmalnajjar 
5/29/2021 7:10:56 AM
First we determine the SMA with TradingView's sma() function. We have that function run on closing prices (close) for the number of bars set by the smaLength input variable. Then we calculate the ATR with the atr() function and the atrLength input variable. We store these two computed values in the smaValue and atrValue variables.
Next we calculate the ATR channel's upper and lower band. For the upper band we increase the SMA with the ATR value multiplied by ubOffset, the input variable we gave a default of 7. To arrive at the lower band's value we decrease the SMA with lbOffset times the ATR. We store these band values in the upperBand and lowerBand variables.
Then we calculate the position size. The algorithm that Faith (2007) uses has two components: a percentage of equity and the ATR in currency value. The first part has us divide riskPerc with 100. That translates the percentagebased input option into a decimal value (so 0.5% becomes 0.005). Then we multiply with strategy.equity, a variable that returns the strategy's current equity (that is, initial capital + accumulated net profit + open position profit) (TradingView, n.d.).
To determine the currency value of the ATR we multiply atrValue with syminfo.pointvalue. That TradingView variable returns the point value for the current instrument (TradingView, n.d.). For instance, for the Emini S&P this variable returns 50 since one point of price movement in ES is worth $50. For crude oil futures, which have an underlying of 1,000 barrels, syminfo.pointvalue returns 1,000.
The posSize variable we make next holds the strategy's position size. Here we have the conditional operator (?:) check if the usePosSize input variable is true. When it is, we divide the risk equity with the ATR currency value. Then we use the floor() function to round the position size down to the nearest full integer. Should that input variable be false, however, we simply set posSize to 1. That has us always trade one contract when the position sizing input is turned off.
# Step 3: Output the strategy's data and visualise signals
To make it easier to validate the strategy, we plot its data on the chart next:
// 3. Output strategy data
plot(series=smaValue, title="SMA", color=orange)
plot(series=upperBand, title="UB", color=green,
linewidth=2)
plot(series=lowerBand, title="LB", color=red,
linewidth=2)
We show the strategy's values on the chart with TradingView's plot() function. The first plot() statement shows the 350day moving average (smaValue). We name that plot ‘SMA’ and colour it orange.
The next two plot() function calls show the ATR channel's upper and lower band on the chart. We show upperBand values in the green colour and have lowerBand values show with the red colour value. With the linewidth argument set to 2 these line plots are a bit thicker than normal.
# Step 4: Code the long trading rules
Next up are the conditions that determine when we open and exit long positions. From the trading rules above we know that the strategy goes long when the close breaks above the channel's upper band. And long positions are closed when prices go below the simple moving average.
Here's how we translate that into code:
// 4. Determine long trading conditions
enterLong = crossover(close, upperBand)
exitLong = crossunder(close, smaValue)
The first Boolean variable we make here, enterLong, gets its true/false value from crossover(). That builtin function returns true when the value of its first argument crossed above that of the second argument (TradingView, n.d.). Here we have the function check if the instrument's closing prices (close) cross above the ATR channel's upper band (upperBand). This makes enterLong true when prices break above the channel and false when they don't.
