Reputation: 649
In the PineScript v5, I am trying to develop a strategy where I don't intent to take any trade after 3:00 pm (in GMT+5:30 timezone). So if the current time is less than 3:00 pm, then only I will "entry"/"exit" the trades. Otherwise at 3:00 PM, I want to exit all the open positions.
So I tried this:
endOfDay = input.int(defval=1500, title="Close all trades, default is 3:00 PM, 1500 hours (integer)")
if (hour(timenow) < endOfDay)
// Entry
float sl = na
if (sureBuyInTrend)
strategy.entry("enter long", strategy.long, lotSize, limit=na, stop=na, comment="Long")
sl = atrLow
if (sureSellInTrend)
strategy.entry("enter short", strategy.short, lotSize, limit=na, stop=na, comment="Short")
sl = atrHigh
// Exit: target or SL
longExitComment = (close < sl) ? "Long SL hit" : sureSellInTrend ? "Long target hit" : "Long close"
shortExitComment = (close > sl) ? "Short SL hit" : sureBuyInTrend ? "Short target hit" : "Short close"
if ((sureSellInTrend or (close < sl)))
strategy.close("enter long", comment=longExitComment)
if (sureBuyInTrend or (close > sl))
strategy.close("enter short", comment=shortExitComment)
lastBarOfDay = (hour(time_close)*60 + minute(time_close)==(60*(endOfDay/100)+endOfDay%100)) ? 1 : 0
if (lastBarOfDay)
// Close all open position at the end
strategy.close_all(comment = "Close all entries")
The 'close all entries' are working fine.
However I am seeing the hour(timenow) < endOfDay
check is not working.
In the List of Trades I am seeing order is issued at 3:15 PM.
What mistake I am doing here?
Upvotes: 2
Views: 3759
Reputation: 1368
We can calculate the hour in GMT+530 using below code
h=hour(time('1'),"Asia/Kolkata")
m=minute(time('1'),"Asia/Kolkata")
hour=h*100+m
Then you can use this hour in your if case instead of hour(timenow)
In below chart I have plotted both (my calculated hour (above) and hour(timenow) (below)) in GMT+530
Upvotes: 6