StockFetcher Forums · Filter Exchange · who can can make a code for this strategy will get paid if correct<< >>Post Follow-up
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 percentage-based 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 E-mini 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,


plot(series=lowerBand, title="LB", color=red,


We show the strategy's values on the chart with TradingView's plot() function. The first plot() statement shows the 350-day 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 built-in 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.

955 posts
msg #156881
Ignore nibor100
5/29/2021 12:08:17 PM

As written your strategy can't be coded in StockFetcher due to several identified TradingView capabilities that SF doesn't have.

Ed S.

304 posts
msg #157127
Ignore fotchstecker
6/30/2021 4:18:18 PM

Paid how much, however?

1 posts
msg #157908
Ignore pookiesrevenge
10/20/2021 6:28:39 PM

So basically you're looking for an SMA version of the Keltner Channel indicator combined with an ATR-based position size strategy, yes?

StockFetcher Forums · Filter Exchange · who can can make a code for this strategy will get paid if correct<< >>Post Follow-up

*** Disclaimer *** does not endorse or suggest any of the securities which are returned in any of the searches or filters. They are provided purely for informational and research purposes. does not recommend particular securities., Vestyl Software, L.L.C. and involved content providers shall not be liable for any errors or delays in the content, or for any actions taken based on the content.

Copyright 2022 - Vestyl Software L.L.C.Terms of Service | License | Questions or comments? Contact Us
EOD Data sources: DDFPlus & CSI Data Quotes delayed during active market hours. Delay times are at least 15 mins for NASDAQ, 20 mins for NYSE and Amex. Delayed intraday data provided by DDFPlus

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.