## Best Algo Trading Tutorial For The Beginner 5 - RSI index

Algo trading is not as difficult as you think. There are four simple steps to lead you to how to code your own algorithmic trading strategy for the RSI index. All the coding is using pine script and based on the Tradingview platform.

The article will first display the full version of the strategy content at each step and will separate it into steps to explain.

The black part is a default code part, and the yellow part can be changed according to personal needs!

Next, the article will introduce the meaning of each step.

**Below is the full version code:**

//@version=4

strategy("Yaonology RSI Tutoring", overlay=false, 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)

days = input(title = "Ndays",defval=14)

RSI = rsi(close, days)

plot(RSI, color = color.black)

if(RSI < 20)

strategy.entry(id = "RSI_long",long = true)

if(RSI > 80)

strategy.close(id = "RSI_long")

**Step One: Strategy Setting**

//@version=4

strategy(“Yaonology RSI Tutoring“, overlay=false, 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 RSI Tutorial)

overlay: No – False

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: Input**

**Part 1:**

days = input(title = “Ndays“,defval=14)

The default length is 14 consecutive trading days.

The parameters can be modified in setting -> input.

**Part 2: Use built-in function**

RSI = rsi(close, days)

rsi: built-in function in pine script to calculate RSI index

**Step Three: Plot**

plot(RSI, color = color.black)-color for RSI is set as black

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

**if(RSI < 20)**

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

If this condition applies, this is the point to consider selling it.

Strategy.entry: The command to enter market position;

id: The order name. Yaonology sets as mass

long: This means long position, and here is true

**if(RSI > 80)**

** strategy.close(id = “RSI_long”)**

If this condition applies, this is the point to consider selling it.

Strategy.close: The command to close market position

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

**Total Closed Trades:** total number of failed trades

**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 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

#Algorithmtradingstrategy #codingtutorial #SPY500 #RSIindex