# [Beginner-8] Best Algo Trading Tutorial- SAR Indicator

## Best Algo Trading Tutorial For The Beginner 8- SAR Indicator

Algo trading is not as difficult as you think. In this tutorial article, there are four simple steps to lead the beginner to understand how to code your own best algorithm trading strategy for SAR indicator. All the coding is using pine script and based on the Tradingview platform.

#### Below is the full version code:

`//@version=4strategy("Yaonology SAR Tutoring", overlay=true, default_qty_type= strategy.percent_of_equity ,default_qty_value=100, currency=currency.USD, initial_capital=10000, commission_type=strategy.commission.percent, commission_value=0)s = sar(0.02,0.02,0.2)plot(s, color = close > s ? color.green : color.red, linewidth = 2, style = plot.style_circles)if crossunder(s, close)     strategy.entry(id = "SAR", long = true) if crossover(s, close)     strategy.close(id = "SAR")`

#### Step One: Strategy Setting

strategy(“Yaonology SAR Tutoring“, overlay=true, default_qty_type= strategy.percent_of_equity ,default_qty_value=100, currency=currency.USD, initial_capital=10000, commission_type=strategy.commission.percent, commission_value=0)

strategy: Name of strategy (Yaonology sets the name as Yaonology SAR Tutoring)

overlay: Yes – True

default_qty_type : Type of quantity – percent of equity

default_qty_value : Value of quantity – 100

currency:  USD

initial_capital: Initial trading price – 10000

commission_type : Type of commission – percent

commission_value: Value of commission ( there is no commission, so we set 0)

#### Step Two: Calculate the SAR indicator with the Built-in SAR Function Provided by TradingView

s = sar(0.02,0.02,0.2)

sar is the built-in function for calculating the underlying stock’s SAR indicator value. It has three parameters in numbers.

The first parameter is the initial acceleration factor. If an upward or downward trend appears, we set the coefficient to be 0.02

The second is the increment of acceleration factor. If we observe a trend to persist for more than one day, we add 0.02 to the current acceleration factor

The third is the maximum acceleration factor. We fix the acceleration factor at 0.2 even the trend continues.

#### Step Three: Plot

plot(s, color = close > s ? color.green : color.red, linewidth = 2, style = plot.style_circles)

If the price is larger than the SAR indicator of the day, we plot the indicator in green; otherwise, we plot the indicator in red. We also choose to plot the SAR indicator with linewidth of 2 and in the shape of a set of circles.

#### Step Four: Strategy.entry & Strategy.close

if crossunder(s, close)

strategy.entry(id = “SAR”, long = true)

If we observe SAR line crosses the close price line from below, we enter into long position of the underlying stock

id: The order name. Yaonology sets as SAR

long: This means long position, and here is true

if crossover(s, close)

strategy.close(id = “SAR”)

If we observe SAR line crosses the close price line from above, we exit the position by selling the stock

The id should set the same one as the entry one since it is the same order

#### Complete version:

Net Profit: total net profit by applying the strategy to the historical market

Percent profitable: percentage of trades with positive profit

Profit factor: total profit/ total loss

Max Drawdown: total loss by applying the strategy to the historical market (Using S&P 500 as an example)

Average Number of Days In Trades: the average number of days between entering and exiting a position by applying the strategy to the historical market

Yaonology.com