ATM Daily Breakout Strategy Code (For TradingView)

You might also like

Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 4

//@version=5

strategy("Options Option Daily Breakout Scalp", overlay=true, max_boxes_count=500,


initial_capital=5000, use_bar_magnifier=true)

//tool tip setup


SessionToolTip = "These are the trades I'm seeking in a day. To understand which
Timeframe to use, you can see the spread between the beginning and ending time."
StopLossInTicksToolTip = "If you're seeking different stop than the high or low change,
use this to restrict your trades to this stop. It's great for optimizing."
ProfitInTicksToolTip = "This is profit target. For example each tick on NQ is worth $5
USD"
MaxRiskToolTip = "This helps calculate the max contracts to be traded on each trade.
Enter the maximum dollar amount you're willing to lose"
BarRangeToolTip = "Use this to filter out extremly high or extremely low volatility
sessions from being a valid trade"

//drawing the boxes


AUTO_TIMEFRAME_TOOLTIP = "When the Auto option is selected, the timeframe of
the indicator is chosen automatically based on the chart timeframe. The Timeframe
dropdown is ignored. The automated timeframes are: \n

'1 day' for any chart timeframes below '1 day' \n

'1 week' for any timeframes starting from '1 day' up to '1 week' \n

'1 month' for any timeframes starting from '1 week' up to '1 month' \n

'3 months' for any timeframes starting from '1 month' up to '3 months' \n

'12 months' for any timeframes above '3 months'"

HLR = "High/Low Range"


TR = "True Range"
HAR = "Heikin Ashi Range"

DEFAULT_RESOLUTION = "1D"
DEFAULT_COLOR_GROWTH = color.rgb(0, 150, 136, 80)
DEFAULT_COLOR_FALL = color.rgb(244, 67, 54, 80)
DEFAULT_CALCULATION_METHOD = HLR
DEFAULT_AUTO_RESOLUTION = false

var prevBarIndex = bar_index


drawBox(left, right, top, bottom, diff, growthColorInput, fallColorInput, borderGrowth,
borderFall) =>
drawnBox = box.new(left, top, right, bottom)
if diff < 0
box.set_border_color(drawnBox, borderFall)
box.set_bgcolor(drawnBox, fallColorInput)
else
box.set_border_color(drawnBox, borderGrowth)
box.set_bgcolor(drawnBox, growthColorInput)
drawnBox

selectAutoTimeframe() =>
timeframe.isseconds ? '120':
timeframe.isminutes ? '1440':
timeframe.isdaily ? 'W':
timeframe.isweekly ? 'M':
timeframe.ismonthly and timeframe.multiplier < 3 ? '3M': '12M'

autoTimeframeInput = input(DEFAULT_AUTO_RESOLUTION,
title="AutoTimeframe", tooltip=AUTO_TIMEFRAME_TOOLTIP)
timeframeInput = input.timeframe(DEFAULT_RESOLUTION, title="Timeframe")
timeframe = autoTimeframeInput ? selectAutoTimeframe() : timeframeInput

calculationMethodInput = input.string(DEFAULT_CALCULATION_METHOD,
"Calculation", options = [HLR, TR, HAR])

growthColorInput = input(DEFAULT_COLOR_GROWTH, title="",inline="Body")


fallColorInput = input(DEFAULT_COLOR_FALL, title="Body", inline="Body")

growthBorderColor = color.new(growthColorInput, 0)
fallBorderColor = color.new(fallColorInput, 0)

[previousHigh, previousLow, previousClose, previousOpen, prePreviousClose] =


request.security(syminfo.tickerid, timeframe, [high[1], low[1], close[1], open[1],
close[2]], lookahead=barmerge.lookahead_on)
[currentHigh, currentLow, currentClose, currentOpen, prevClose] =
request.security(syminfo.tickerid, timeframe, [high, low, close, open, close[1]])

isNewPeriod = ta.change(time(timeframe))

drawCurrent = barstate.islast and not isNewPeriod


secHigh = drawCurrent ? currentHigh : previousHigh
secPrevClose = drawCurrent ? prevClose : prePreviousClose
secLow = drawCurrent ? currentLow : previousLow

diff = drawCurrent ? currentClose - currentOpen : previousClose - previousOpen


left = prevBarIndex
right = drawCurrent ? bar_index : bar_index[1]

[ashiHigh, ashiLow] = request.security(ticker.heikinashi(syminfo.tickerid), timeframe,


[high, low])
float top = na
float bottom = na

if calculationMethodInput == HLR
top := secHigh
bottom := secLow
else if calculationMethodInput == TR
top := math.max(secHigh, secPrevClose)
bottom := math.min(secLow, secPrevClose)
else if calculationMethodInput == HAR
top := ashiHigh
bottom := ashiLow

if isNewPeriod
prevBarIndex := bar_index
drawBox(left, right, top, bottom, diff, growthColorInput, fallColorInput,
growthBorderColor, fallBorderColor)

var box prevBox = na

if barstate.islast
if isNewPeriod
prevBox := na
else
box.delete(prevBox)
prevBox := drawBox(left, right, top, bottom, diff, growthColorInput,
fallColorInput, growthBorderColor, fallBorderColor)

MaxRisk = input(defval=5000, title="Max Risk: Trailing Drawdown Limit", tooltip =


MaxRiskToolTip)
pT = input(defval=2, title="Profit in Ticks", tooltip = ProfitInTicksToolTip)
sL = input(defval=40, title="Stop Loss in Ticks", tooltip = StopLossInTicksToolTip)
timeSession = input.session("0840-0850", "Session", tooltip=SessionToolTip,
options=["0730-0740","0730-0731", "0700-0800", "0800-0900", "0830-0833","0800-
1200", "0830-0840", "0833-0836","0836-0839","0840-0850", "0850-0900", "0900-
0910", "0900-1000","1000-1100", "1100-1200" , "1400-1500", "0900-1100", "0900-
1200", "1300-1500"])
t = time(timeframe.period, timeSession)

insession(sess) => na(time(timeframe.period, sess + ":1234567")) == false

//filter out extremely low volatility bars


bar_range = top-bottom
bar_size_filter = input(defval=3, title="Bar Range for Filtering Trades", tooltip =
BarRangeToolTip)

//setup to only take 1 trade within a single period


reset = false
if(isNewPeriod)
reset := true

//check to see if conditions are met for new trade


longCondition = high < top //and insession(timeSession) and bar_range >= bar_size_filter
shortCondition = low > bottom //and insession(timeSession) and bar_range >=
bar_size_filter

//order entry logic


if(reset)
if (longCondition)
strategy.entry("long", strategy.long, stop=top)

strategy.exit("exit long","long", profit = pT, loss = sL)


//strategy.exit("exit long lower","shortlong", stop=bottom)

if(reset)
if (shortCondition)
strategy.entry("short", strategy.short, stop=bottom)

strategy.exit("exit short","short", profit = pT, loss = sL)


//strategy.exit("exit short upper","short", stop=top)

You might also like