简体   繁体   中英

Pinescript — Find the current day low within time range

I would like to get statistical data about how many time the lowest point of a day is made during a specific time range. The only way I thought about to obtain this data is to code a Pinescript strategy.

I currently use entryTime = input.session('0700-0900') to specify when the value should be searched for (green zone one below pictures); and tdayLow = request.security(tickername, 'D', low[0], lookahead=barmerge.lookahead_on) to specify what value should be searched for.

I intended to have a result that looks like this (one or no entry per day, since there can only be one lowest point). Unfortunately, it looks like this (several entries, taken I don't know how, which vary depending the timeframe I am on, not a single one taken on the current day low).

Since I'd like to find the low of the day, the timeframe I am on shouldn't change anything to the results I get, unlike what I currently obtain with the code below.

//@version=5
strategy("Day Low", overlay=true, margin_long=100, margin_short=100)


// === CURRENT DAY LOW ===
var tickername = ticker.new(syminfo.prefix, syminfo.ticker)
tlow = request.security(tickername, 'D', low[0], lookahead=barmerge.lookahead_on)


// === INPUT DATE RANGE ===
fromMonth  = input.int(defval = 7,       title = "From Month",      minval = 1, maxval = 12)
fromDay    = input.int(defval = 1,       title = "From Day",        minval = 1, maxval = 31)
fromYear   = input.int(defval = 2022,    title = "From Year",       minval = 2019)
thruMonth  = input.int(defval = 1,       title = "Thru Month",      minval = 1, maxval = 12)
thruDay    = input.int(defval = 1,       title = "Thru Day",        minval = 1, maxval = 31)
thruYear   = input.int(defval = 2112,    title = "Thru Year",       minval = 2019)


// === INPUT TIME RANGE ===
entryTime  = input.session('0700-0900',  title = "Entry Time")   // '0700-0900' is anytime to enter
exitTime   = input.session('0800-2000',  title = "Exit Time")    // '0700-2000' is anytime to exit


// === DATE & TIME RANGE FUNCTIONS ===
isDate() =>                                                               
    start  = timestamp(fromYear, fromMonth, fromDay, 00, 00)              
    finish = timestamp(thruYear, thruMonth, thruDay, 23, 59)              
    isDate = time >= start and time <= finish                             

isTime(_position) =>                                                      
    isTime = time(timeframe.period, _position + ':1234567')               

// === LOGIC ===
enterLong  = tlow 
exitLong   = tlow + 20

// === EXECUTION ===
strategy.entry("L", strategy.long, when = isDate() and isTime(entryTime) and enterLong)  
strategy.close("L", when = isDate() and isTime(exitTime) and exitLong)

Could somebody help me to get a result similar to the one first picture? Thanks

PS: I'm obviously not trying to find a strategy that would find the lowest point of the day, but simply to track when it is made based on historical data.

Since you are writing a condition (which should be boolean) as

enterLong  = tlow 
exitLong   = tlow + 20

It is becoming true by default. you will have to change them to comparison like

enterLong  = (low<=tlow)
exitLong   = (high>=tlow + 20)

Final code

//@version=5
strategy("Day Low", overlay=true, margin_long=100, margin_short=100)


// === CURRENT DAY LOW ===
var tickername = ticker.new(syminfo.prefix, syminfo.ticker)
tlow = request.security(tickername, 'D', low[0], lookahead=barmerge.lookahead_on)
plot(tlow)

// === INPUT DATE RANGE ===
fromMonth  = input.int(defval = 7,       title = "From Month",      minval = 1, maxval = 12)
fromDay    = input.int(defval = 1,       title = "From Day",        minval = 1, maxval = 31)
fromYear   = input.int(defval = 2022,    title = "From Year",       minval = 2019)
thruMonth  = input.int(defval = 1,       title = "Thru Month",      minval = 1, maxval = 12)
thruDay    = input.int(defval = 1,       title = "Thru Day",        minval = 1, maxval = 31)
thruYear   = input.int(defval = 2112,    title = "Thru Year",       minval = 2019)


// === INPUT TIME RANGE ===
entryTime  = input.session('0700-0900',  title = "Entry Time")   // '0700-0900' is anytime to enter
exitTime   = input.session('0800-2000',  title = "Exit Time")    // '0700-2000' is anytime to exit


// === DATE & TIME RANGE FUNCTIONS ===
isDate() =>                                                               
    start  = timestamp(fromYear, fromMonth, fromDay, 00, 00)              
    finish = timestamp(thruYear, thruMonth, thruDay, 23, 59)              
    isDate = time >= start and time <= finish                             

isTime(_position) =>                                                      
    isTime = time(timeframe.period, _position + ':1234567')               

// === LOGIC ===
enterLong  = (low<=tlow)
exitLong   = (high>=tlow + 20)

// === EXECUTION ===
strategy.entry("L", strategy.long, when = isDate() and isTime(entryTime) and enterLong)  
strategy.close("L", when = isDate() and isTime(exitTime) and exitLong)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM