'Wrong ENtry and Exits marked

I am trying to mark high and low of third candle of day

Long Entry - if price breaks the marked high , risk to reward 2:1 Long Exit - if price reaches marked low

Short Entry - if price breaks down the marked low , risk to reward 2:1 Short Exit - if price reaches marked high

Exit all positions at 3 PM for the day

This does not seem to work in code as it is only taking long trades and also at wrong prices and stoploss

// © ajaymshr42

//@version=4
strategy("Intraday 15 min third candle")
high_third = 0
low_third = 0
target = 0
stoploss = 0

LongEntry=false
LongExit=false

ShortEntry=false
ShortExit=false

if time >= timestamp(2021,01,01,0,0,0)
    
    if hour(time) == 9 and minute(time) == 45
        high_third = high[0]
        low_thrid = low[0]
        
        
    // Entry after 10 AM and upto 1 PM 
    if hour(time) >= 10 and hour(time) < 13
        if close[0] > high_third and not(ShortEntry) and not(LongExit)
            LongEntry=true
            strategy.entry("Long",strategy.long,100,when=LongEntry)
            target = high[0] + 2*(high[0] - low[0])
            stoploss= low_third
        if close[0] > low_third and not(LongEntry) and not(ShortExit)
            ShortEnrty=true
            strategy.entry("Short",strategy.short,100,when=LongExit)
            target = low[0] - 2*(high[0] - low[0])
            stoploss = high_third
    
    // Exit on Target
    if hour(time) >= 10 and hour(time) < 15
        if close[0] >= target and LongEntry 
            LongExit=true
            strategy.close("Long",when=LongExit)
        if close[0] <= target and ShortEntry
            ShortExit=true
            strategy.close("Short",when=ShortExit)
    
            
    // Exit on StopLoss
    if hour(time) >= 10 and hour(time) < 15
        if close[0] <= stoploss and LongEntry 
            LongExit=true
            strategy.close("Long",when=LongExit)
        if close[0] >= stoploss and ShortEntry
            ShortExit=true
            strategy.close("Short",when=ShortExit)
            
    if hour(time) == 15 
        strategy.close_all()


Solution 1:[1]

When you are declaring stops and limits you need to use var in the declaration so the variables hold their values between bar updates, otherwise they will recalculate on each bar. example:

var target = 0.0 

Additionally, when re-assigning variables later we can not use = again.

Variables are first defined with =, then re-assignment is done with :=. Using these two methods we can "set and save" our exit levels. Example:

if close[0] > low_third and not(LongEntry) and not(ShortExit)
    target := low - 2 * (high - low)

lastly, if we want to exit at those defined levels, we can use strategy.exit() and use the limit and stop arguments. strategy.close() will wait for the close of the bar, which can result in your levels being exceeded.

Cheers, and best of luck

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bjorgum