0

I am barely a day old at PineScript and am trying to experiment by backtesting some simple strategies. I am trying to Short at a specific time (say 2:00 PM ) and Exit the position at say (3:00PM) How do I do this? I have read the documentation and have attempted at a code but I am getting Syntax Error. I am absolutely new to programming / scripts

//////

strategy("My strategy", initial_capital=100000, margin_long=100, margin_short=100)
start = timestamp(2001,1,1,0,0)
end = timestamp(2022,9,1,0,0)

if time > start and time < end
strategy.entry ("Short", strategy.short,1,when=time(12:00))
strategy.close("Short", when=time(14:00))
Usitha Indeewara
  • 870
  • 3
  • 10
  • 21
Zabx
  • 1
  • 3

1 Answers1

0
//@version=5
strategy("My strategy")

start = timestamp(2001,1,1,0,0,0)
end = timestamp(2022,9,1,0,0,0)

if time > start and time < end
    if hour == 1
        strategy.entry("Short", strategy.short)
    if hour == 13
        strategy.close("Short")

Note that the hour variable operates on exchange timezone that you may need to take into consideration.
Furthermore your script gets evaluated on each bar close, which means so that your backtesting can work, you need to run your strategy on an hourly chart and choose 1hour instead of 2 and 13 instead of 14 respectively. If the script runs on the 01:00 close and all conditions met, it sets you a strategy entry afterwards, beginning with the 02:00 hour candle.
You may find this guide helpful on how to set up and debug a strategy properly.

elod008
  • 1,227
  • 1
  • 5
  • 15