0% found this document useful (0 votes)
320 views

ATR Mean Reversion Strategy Pine Script Code

This document outlines a trading strategy that uses the average true range (ATR) indicator to determine price bands and generate reversion signals. It takes long and short term EMAs as inputs to determine trend and uses the ATR value to calculate top, middle, and bottom price bands. It enters long positions when the close is below the bottom band and above the long EMA, placing a limit order a stretched amount below the low. It exits longs when the close or source price hits the determined sell band or crosses below the long EMA.

Uploaded by

GKMurthy
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
320 views

ATR Mean Reversion Strategy Pine Script Code

This document outlines a trading strategy that uses the average true range (ATR) indicator to determine price bands and generate reversion signals. It takes long and short term EMAs as inputs to determine trend and uses the ATR value to calculate top, middle, and bottom price bands. It enters long positions when the close is below the bottom band and above the long EMA, placing a limit order a stretched amount below the low. It exits longs when the close or source price hits the determined sell band or crosses below the long EMA.

Uploaded by

GKMurthy
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

// @version=5

strategy("ATR Reversion System",

overlay=true,

initial_capital=100000,

default_qty_type=strategy.percent_of_equity,

default_qty_value=100,

commission_type=strategy.commission.cash_per_order,

commission_value=25)

// Get user input

i_EmaLongLength = input.int(title="Long-term EMA", defval=200)

i_EmaShortLength = input.int(title="Short-term EMA Length", defval=20)

i_ATRPeriod = input.int(title="ATR Period", defval=5)

i_ATRBand = input.float(title="ATR Band Distance", defval=1)

i_ATRStretch = input.float(title="ATR Buy Stretch", defval=1)

i_SellBand = input.string(title="Sell At Band:", defval="Middle", options=["Top", "Middle",


"Bottom"])

i_SellSrc = input.source(title="Sell Price Source", defval=high)

// Get indicator values

emaLongTerm = ta.ema(close, i_EmaLongLength)

emaShortTerm = ta.ema(close, i_EmaShortLength)

atrValue = ta.atr(i_ATRPeriod)

// Get ATR bands

atrBandTop = emaShortTerm + (atrValue * i_ATRBand)

atrBandBot = emaShortTerm - (atrValue * i_ATRBand)

// Define price stretch


float buyLimitPrice = na

// Check setup conditions = bar close is below ATR band, above long-term EMA

setupCondition = close < atrBandBot and low > emaLongTerm

// Clear any pending limit orders

strategy.cancel_all()

// Enter trades on next bar after setup condition is met

if setupCondition

buyLimitPrice := low - (atrValue * i_ATRStretch)

strategy.entry("Long", strategy.long, limit=buyLimitPrice)

// Get sell price

sellPrice = switch i_SellBand

"Top" => atrBandTop

"Middle" => emaShortTerm

"Bottom" => atrBandBot

// Exit trades

if i_SellSrc >= sellPrice or close < emaLongTerm

strategy.close("Long", comment="Exit trade")

// Draw data to chart

plot(emaLongTerm, "EMA Filter", color.red, 2)

plot(emaShortTerm, "ATR Band Middle", color.blue)

plot(atrBandBot, "ATR Band Bottom", color=color.green)

plot(atrBandTop, "ATR Band Top", color=color.new(color.gray, 75))

plot(setupCondition ? buyLimitPrice : na, "Buy Limit", color.lime, 1, plot.style_cross)

You might also like