# Algo Trading Tutorial – Bollinger Bands

## Algo Trading Tutorial - Bollinger Bands

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 Bollinger Bands. 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.

```//Bollinger Bands

//@version = 4

strategy("Yaonology Bollinger Bands Tutorial", 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)

length = input(20, minval = 1, title="MA period")

clp = input(close, title = "Closing Price")

mtp = input(2.0, minval = 0.001, maxval = 50.0)

mdl = sma(clp, length)

dev = mtp * stdev(clp, length)

upr = mdl + dev

lwr = mdl - dev

plot(mdl, color=color.red)

p1 = plot(upr, color=color.blue)

p2 = plot(lwr, color=color.purple)

fill(p1, p2)

if clp < lwr

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

if clp > upr

strategy.close(id = "BOLL1")```

#### Step One: Initial Setting

strategy(“Yaonology KDJ Indicators Tutorial“, 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 KDJ Indicators Tutorial)

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

length = input(20, minval = 1, title=”MA period“)

length: days of simple moving average

20: 20 days simple moving average (In general, the market uses 20 days as simple moving average, but you can edit with different conditions)

minval: Minimum interval is 1

title: The name could be set by yourself. Yaonology sets as MA period

clp = input(close, title = “Closing Price“)

close: closing price

title : The name could be set by yourself. Yaonology sets as Closing Price

mtp = input(2.0, minval = 0.001, maxval = 50.0)

mtp : multiple

minval: Minimum interval is 0.001

maxval: Max interval is 50.0

mdl = sma(clp, length)

mdl : middle band (simple moving average)

sma(clp, length) : simple moving average (closing price, average days)

dev = mtp * stdev(clp, length)

dev : deviation

mtp * stdev(clp, length) : mtp mutiple the standard deviation of closing price and length

upr = mdl + dev (formula of upper band)

upr : upper band (In general, it is 2 positive standard deviations from the middle average)

lwr = mdl – dev (formula of the lower band)

lwr : lower band (in general, it is 2 negative standard deviations from the middle average)

#### Step Three: Plot

plot(mdl, color=color.red): Set middle band curve as color red

p1 = plot(upr, color=color.blue): Set upper band curve as color blue

p2 = plot(lwr, color=color.purple): Set lower band curve as color purple

fill(p1, p2): It means filling p1 and p2 with shadows to highlight the Bollinger Channel

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

if clp < lwr

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

=>If this condition applies, this is the point to consider buying it.

Strategy.entry: The command to enter the market position

id: The order name. Yaonology sets as BOLL1

long: This means long position, and here is true

if clp > upr

strategy.close(id = “BOLL1“)

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

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

Yaonology.com