## Best Algo Trading Tutorial For The Beginner 6 - ADX index

Algo trading is not as difficult as you think. There are four simple steps to lead the beginner to understand how to code your own algorithm trading strategy for ADX 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

//STEP ONE: Initialization

strategy("Yaonology ADX 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)

period = input(title = "Period",defval = 14)

pos_dm = high - high[1]

neg_dm = low[1]-low

dm = 0.0

if pos_dm > neg_dm

neg_dm := 0

else

pos_dm := 0

TR = max(abs(high-low),abs(high-close[1]),abs(close[1]-low))

ATR = sma(TR,period)

pos_di = ema(pos_dm,period)*100/ATR

neg_di = ema(neg_dm,period)*100/ATR

DX = abs(pos_di-neg_di)/abs(pos_di+neg_di)

ADX = sma(DX,period)*100

plot(ADX, color = color.black)

plot(pos_di, color = color.green)

plot(neg_di, color = color.red)

if ADX > neg_di and pos_di > neg_di and ADX[1] < ADX

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

if ADX < neg_di and pos_di < neg_di and ADX > ADX[1]

strategy.close(id = "long")

**Step One: Strategy Setting**

**//@version=4**

**strategy(“Yaonology ADX 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 ADX 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:**

period = input(title = “Period”,defval = 14)

The default length is 14 consecutive trading days.

The parameters can be modified in setting -> input.

**Part 2: Calculate Directional Movement**

**pos_dm = high – high[1]**

high: highest price

high[1]: highest price 1 day before

**neg_dm = low[1]-low**

low: lowest price

low[1]: lowest price 1 day before

**dm = 0.0**

**if pos_dm > neg_dm**

** neg_dm := 0**

else

**pos_dm := 0**

pos_dm: positive directional movement index

neg_dm: negative directional movement index

if the positive movement is higher than negative, the negative index equals to zero; otherwise, the positive index equals to zero

**Part 3: Calculate Real Fluctuation**

TR = max(abs(high-low),abs(high-close[1]),abs(close[1]-low))

abs: absolute value

max: the maximum value among the three indicators

**Part 4: Calculate the Directional Movement Index**

**ATR = sma(TR,period)**

sma: simple moving average

**pos_di = ema(pos_dm,period)*100/ATR**

**neg_di = ema(neg_dm,period)*100/ATR**

ema: exponential moving average

DX = abs(pos_di-neg_di)/abs(pos_di+neg_di)

DX equals the absolute value of the difference between positive and negative movement divide by the sum of positive and negative movement

**ADX = sma(DX,period)*100 **

get the simple moving average of DX within 14 consecutive days

**Step Three: Plot**

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

plot(pos_di, color = color.green)-color for positive is set as green

plot(neg_di, color = color.red)-color for negative is set as red

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

**if ADX > neg_di and pos_di > neg_di and ADX[1] < ADX**

** strategy.entry(id = “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 ADX < neg_di and pos_di < neg_di and ADX > ADX[1]**

** strategy.close(id = “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

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

#Algorithmtradingstrategy #codingtutorial #SPY500 #ADXindex