# [Beginner -1] Best Algo Trading Tutorial-Moving Average The algo trading is not difficult as you think. There are four simple steps to lead you how to code your own algorithmic trading strategy for moving average method. All the coding is using pine script and based on 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. This code is to teach how to use moving average tutoring to determine entry and close time.

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

```//@version=4
strategy("Yaonology Moving Average 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
fst = input(title = "MA_Fast", defval = 20)
slw = input(title = "MA_Slow", defval = 40)
ma_fast = sma(close, fst)
ma_slow = sma(close, slw)
plot(ma_fast, color = color.blue)
plot(ma_slow, color = color.green)
if ma_fast > ma_slow
strategy.entry(id = "ma_long", long = true)
if ma_slow > ma_fast
strategy.close(id = "ma_long")```

## The following will be detailed explanation for each step !

Step 1: Initial Setting

Below is the basic setting of the strategy session. The black part is the default code part, and the yellow part can be changed according to personal needs! The meaning of each setting will be explain as following.

//@version=4

strategy(“Yaonology Moving Average 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 Moving Average Tutoring)

overlay : Yes – True

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: Parameter Setting

fst = input(20, “MA_Fast”) – Yaonology sets fast moving average day as 20

slw = input(40, “MA_Slow”) – Yaonology sets slow moving average day as 40

The advantage of setting input is that you can directly change the “input” in the settings when changing the number of days without re-coding.

Moving Average Parameter

ma_fast = sma(close, fst)

ma_slow = sma(close, slw)

EX:

ma_20 = sma(close, 20)

ma_40 = sma(close, 40)

This step is to set the average number of days. Yaonology uses 20 days and 40 days as the average days.

ma_20 and ma_40 can be named according to personal preference, and then set the number of days in the yellow part.

Step Three: Plot

plot(ma_fast, color = color.blue)

plot(ma_slow, color = color.green)

This step is to set the color of the average day curve. The black part is a default part. You can change your favorite color in the yellow part.

Yaonology sets fst as blue, slw one as green.

Step Four: Strategy.entry & Strategy.close

if ma_fast > ma_slow

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

if ma_slow > ma_fast

strategy.close(id = “ma_long”)

if ma_fast > ma_slow: This means that if the fast one is greater than the slow one, it is an entry state and the command is on

Strategy.entry: The command to enter market position

id: The order name. Yaonology sets as ma_long

long: This means long position, and here is true

if ma_slow > ma_fast: This means that if the slow one is greater than the fast one, it is a close state and the command is off

strategy.close: The command to close the market

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

#### *Complete version 