0

I am writing a simple pine script to go Long before 10 mins hourly candle closure and close short after 10 mins after candle closure.

But the script does not apply to previous date / time but only applied from the time I add it to the chart.

I would like to back testing of this simple strategy. Can some one help please?

I am very new to scripting. Appreciate any help.

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // © jayaguru
//@version=4 
strategy("My Strategy", overlay=true)

longCondition = minute(timenow)

if (longCondition == 55)
    strategy.entry("Long", strategy.long, comment="Long")
if (longCondition == 14 or strategy.openprofit < -15 or strategy.openprofit > 40)
    strategy.close_all()
Bjorn Mistiaen
  • 6,459
  • 3
  • 18
  • 42
Jay
  • 1
  • 2

1 Answers1

0

You're using the timenow variable, which is the current date/time (as in 'now').
Instead, you need to use time which returns the time of when the bar opens.
Like this:

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // © jayaguru
//@version=4 
strategy("My Strategy", overlay=true)

longCondition = minute(time)

if (longCondition == 55)
    strategy.entry("Long", strategy.long, comment="Long")
if (longCondition == 14 or strategy.openprofit < -15 or strategy.openprofit > 40)
    strategy.close_all()

See Access bar times programmatically in TradingView Pine for more information.

Bjorn Mistiaen
  • 6,459
  • 3
  • 18
  • 42
  • Thank you so much Bjorn. appreciate your help. Could you please help if the below statement is wrong. it does not work. i am trying to be in the position until either it hits the netloss or profit but does not seem to be working. if (longCondition == 14 or strategy.netprofit < -100 or strategy.netprofit > 60) strategy.close_all() – Jay Jun 11 '20 at 21:23