## Best Algo Trading Tutorial For The Beginner 4 - Mass Indicator

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 by mass indicator method. 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 Mass Index Tutorial", 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)

length = input(title = "MASS", defval =25)

mavglength = input(title = "MAvg", defval = 9)

ma_9 = sma(close, 9)

prc_rg = high - low

ahl = ema(prc_rg,9)

dhl = ema(ahl,9)

mass_idx = sum(ahl/dhl,length)

z = 0

if z[1] == 1

z := 1

if mass_idx > 27

z := 1

if mass_idx < 26.5

z := 0

plot(mass_idx, color = z == 0 ? color.black : color.red, linewidth = 2)

plot(27, color = color.green)

plot(26.5, color = color.red)

if z == 0 and z[1] == 1 and ma_9 < ma_9[1]

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

if z == 0 and z[1] == 1 and ma_9 > ma_9[1]

strategy.close(id = "mass")

**Step 1: Strategy Setting**

//@version=4

strategy(“Yaonology Mass Index Tutorial“, 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 Mass Index 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**

length = input(title = “MASS“, defval =25)

mavglength = input(title = “MAvg“, defval = 9)

The default length is 25 consecutive trading days.

The default moving average length is 9 trading days.

The parameters can be modified in setting -> input.

**Part 2: Strategy Exponential Moving Average**

**ma_9 = sma(close, 9)**

sma: Simple moving average(9 days)

**prc_rg = high – low**

high: The daily highest price

low: The daily lowest price

high – low: The daily stock price fluctuation.

**ahl = ema(prc_rg,9)**

ema: Exponential moving average(9 days stock price fluctuation)

**dhl = ema(ahl,9)**

ema: Exponential moving average(9 days of ahl)

**mass_idx = sum(ahl/dhl,length)**

sum: sum up the ratios of ahl and bhl within 9 days

**z = 0**

**if z[1] == 1**

** z := 1**

**if mass_idx > 27**

** z := 1**

**if mass_idx < 26.5**

** z := 0**

mass_index: If the previous z (one date before) equals to 1 or the mass index goes beyond 27, then z equals to 1; if the index below 26.5, then z equals to 0: (z[1])

**Step Three: Plot**

plot(mass_idx, color = z == 0 ? color.black : color.red, linewidth = 2)

plot(27, color = color.green)- color for mass index that greater than 27 is set as green

plot(26.5, color = color.red)-color for mass index that lower than 27 is set as red

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

In the end, we need to decide when to buy or sell based on Mass Index we plot:

if z == 0 and z[1] == 1 and ma_9 < ma_9[1]

strategy.entry(id = “mass“, long = true)

=> [1] means one day before;

if this condition applies, this is the point to consider to buy 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 z == 0 and z[1] == 1 and ma_9 > ma_9[1]

strategy.close(id = “mass“)

=> [1] means one day before

If this condition applies, this is the point to consider to sell 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

#Algorithmtrading #codingtutorial #SPY500 #Massindicator