I used this website the last couple of weeks for inspiration and help. First of all, thanks to all the active members and the great support that this page offers.
Currently im trying to code a strategy which opens a Short Trade if the high of a bar of the current session (in my case one session is equal to one day) > as the max high of the previous day. Same goes with Short Trades. If the low of a bar of the current session < as the min low of the previous day.
My code currently looks like this: see below
I got the inspiration from this article https://www.tradingcode.net/tradingview/session-high-low-box/.
However, the strategy doesnt work like I want it.
Can someone help me with that?
The entry should only be triggered once per day. Don't worry about the exit. I will set up a take profit and stopp loss for that. Thanks in advance!
strategy(title="Session high/low boxes", overlay=true)
// InSession() returns 'true' when the current bar happens inside
// the specified session, corrected for the given time zone (optional).
// Returns 'false' when the bar doesn't happen in that time period,
// or when the chart's time frame is 1 day or higher.
InSession(sessionTime, sessionTimeZone=syminfo.timezone) =>
not na(time(timeframe.period, sessionTime, sessionTimeZone))
// STEP 1:
// Define custom session trading times with inputs
sessionTime = input.session("0015-2245", title="Session time")
sessionZone = input.string("GMT+1", title="Session time zone")
// Other inputs for the boxes' visual appearance
boxBorderSize = input.int(2, title="Box border size", minval=0)
upBoxColor = input.color(color.new(color.green, 85), title="Up box")
upBorderColor = input.color(color.green, title="Up border")
downBoxColor = input.color(color.new(color.red, 85), title="Down box")
downBorderColor = input.color(color.red, title="Down border")
// Create variables
var sessionHighPrice = 0.0
var sessionLowPrice = 0.0
var sessionOpenPrice = 0.0
var box sessionBox = na
// STEP 2:
// See if the session is currently active and just started
inSession = InSession(sessionTime, sessionZone) and timeframe.isintraday
sessionStart = inSession and not inSession[1]
// STEP 3:
// When a new session starts, set the session high and low to the data
// of the bar in the session.
if sessionStart
sessionHighPrice := high
sessionLowPrice := low
sessionOpenPrice := open
// Else, during the session, track the highest high and lowest low
else if inSession
sessionHighPrice := math.max(sessionHighPrice, high)
sessionLowPrice := math.min(sessionLowPrice, low)
// STEP 4:
// When a session begins, make a new box for that session
if sessionStart
sessionBox := box.new(left=bar_index, top=na, right=na, bottom=na,
border_width=boxBorderSize)
// STEP 5:
// During the session, update that session's existing box
if inSession
box.set_top(sessionBox, sessionHighPrice)
box.set_bottom(sessionBox, sessionLowPrice)
box.set_right(sessionBox, bar_index + 1)
// See if bar closed higher than session open. When it did, make
// box green (and use red otherwise).
if close > sessionOpenPrice
box.set_bgcolor(sessionBox, upBoxColor)
box.set_border_color(sessionBox, upBorderColor)
else
box.set_bgcolor(sessionBox, downBoxColor)
box.set_border_color(sessionBox, downBorderColor)
//Entry Strategie
EntryLong = if (low < sessionBox[1])
1
else
0
EntryShort = if (high > sessionHighPrice[1])
1
else
0
//Exit
barssinceEntryLong = ta.barssince(EntryLong)
barssinceEntryShort = ta.barssince(EntryShort)
// Test Strategie
if (EntryLong == 1)
strategy.entry("long", strategy.long, comment="entry long")
if (barssinceEntryLong > 10)
strategy.close("long", comment="close long")
if (EntryShort == 1 and not inSession)
strategy.entry("short", strategy.short, comment="entry short")
if (barssinceEntryShort > 10)
strategy.close("short", comment="close short")
Ok I solved it by myself. For everyone who is interessted:
// Save Previous Max & Min
Minpreday = ta.valuewhen(SessionEnd,sessionLowPrice,0)
Maxpreday = ta.valuewhen(SessionEnd,sessionHighPrice,0)
//Entry Strategie
EntryLong = if (low < Minpreday)
1
else
0
EntryShort = if (high > Maxpreday)
1
else
0