0

I'm writing a backtesting strategy to buy at certain time every day above that day's high so far.

I've written a pinescript strategy. See below. I've restricted it to August month of this year. This is applied on a 5m chart.

//@version=4
strategy("bnf2")

entryTime = year == 2019 and month == 8 and hour == 14 and minute == 0
exitTime = year == 2019 and month == 8 and hour == 15 and minute == 15

strategy.entry("bnf-long", strategy.long, 20, stop= high, when=entryTime)
strategy.close("bnf-long", when = exitTime)
plot(strategy.equity)

I suspect the "stop = high" is causing an issue as the high is not defined as that day's high so far.

What change do I need to make to the script to achieve this?

PineCoders-LucF
  • 8,288
  • 2
  • 12
  • 21
supernova
  • 43
  • 5

1 Answers1

0

This will buy after 14h00, but only when close breaches current day's high. There are markers plotted to help you see when conditions occur.

//@version=4
strategy("bnf2", overlay=true)

// Keep track of current day's high.
var dayHigh = 0.0
dayHigh := change(dayofweek) ? high : max(high, dayHigh)

entryTime = year == 2019 and month == 8 and hour == 14 and close > dayHigh[1]
exitTime = year == 2019 and month == 8 and hour == 15 and minute == 15
strategy.entry("bnf-long", strategy.long, 20, when = entryTime)
strategy.close("bnf-long", when = exitTime)
plot(dayHigh)
plotshape(entryTime, style = shape.triangleup, color = color.lime, location = location.belowbar, size = size.normal)
plotshape(exitTime, style = shape.triangledown, color = color.red, location = location.abovebar, size = size.normal)
// plot(strategy.equity)
PineCoders-LucF
  • 8,288
  • 2
  • 12
  • 21
  • Thanks.This seems to have taken me in right direction. But it still doesn't seem to do what I want it to do. It does enter at 2pm. But doesn't always close the order at 15:15 irrespective of loss/profit. It seems to be carrying position to next day. Is there something additional I need to do since it is an intraday trade? – supernova Aug 24 '19 at 19:32
  • You may try using `exitTime = year == 2019 and month == 8 and hour == 15 and minute >= 15` instead, but without a concrete example of an exit not triggering correctly, it's difficult to diagnose. Link to a chart would help. – PineCoders-LucF Aug 24 '19 at 23:40