Cero Melkonyan
Cero Melkonyan

Reputation: 1

Pine script runs long strategy but not the corresponding short strategy on tester

I am trying to run a strategy for short positions. I have the corresponding strategy running perfectly for the long positions, but for some reason I am blind to i do not see why it doesnt work. I have made sure the tp/sl make sense but cannot figure where I am wrong.

this is the SHORT code:

//@version=5
strategy("BTC 1H cross SHORT strategy", overlay=true, default_qty_type = strategy.percent_of_equity, margin_long=100, margin_short=100,
 initial_capital = 10000, pyramiding = 3, calc_on_order_fills = true, process_orders_on_close = true, calc_on_every_tick=true)

ema55 = ta.ema(close, 55)
ema200 = ta.ema(close, 200)

shortCondition = ta.crossunder(ema55,ema200)//and strategy.opentrades==0
longCondition = ta.crossover(ema55, ema500)

start = timestamp(2022,6,1,0,0)
end = timestamp(2022,12,31,23,59)

tp = 3500
sl = 2500

if time >= start and time <= end
    if (shortCondition)
        strategy.entry("Short Entry 1H", strategy.short, qty = 1, comment = "Opened Short")
        strategy.exit(id = "Exit Short", from_entry = "Short Entry 1H",  limit = close[0]-tp, stop = close[0]+sl, comment_profit = "HIT profit", comment_loss = "HIT loss")
    
    if (longCondition and strategy.position_size > 0)
        strategy.cancel("Exit Short")
        strategy.close("Short Entry 1H", comment = "Closed short abruptly", immediately = true)

Can someone help me with this?

Edit:

this is the LONG code:

    //@version=5
strategy("BTC 1H cross LONG strategy", overlay=true, default_qty_type = strategy.percent_of_equity, margin_long=100, margin_short=100,
 initial_capital = 10000, pyramiding = 3, calc_on_order_fills = true, process_orders_on_close = true, calc_on_every_tick=true)

ema55 = ta.ema(close, 55)
ema200 = ta.ema(close, 200)

longCondition = ta.crossover(ema55 ,ema200) and strategy.opentrades==0
shortCondition = ta.crossunder(ema55 ,ema200)

start = timestamp(2022,6,1,0,0)
end = timestamp(2022,12,31,23,59)

tp = 3500
sl = 2500

if time >= start and time <= end
    if (longCondition)
        strategy.entry("Long Entry 1H", strategy.long, qty = 1, comment = "Opened Long")
        strategy.exit(id = "Exit Long", from_entry = "Long Entry 1H", limit = close[0]+tp, stop = close[0]-sl, comment_profit = "HIT PROFIT", comment_loss = "HIT LOSS")

    if (shortCondition)
        strategy.cancel("Exit Long")
        strategy.close("Long Entry 1H", comment = "Closed long abruptly", immediately = true)

Upvotes: 0

Views: 287

Answers (1)

John Baron
John Baron

Reputation: 291

A few modifications to consider: use strategy.position_size as part of your entry condition. Use strategy.close_all when the user session ends, it's not clear how that is handled. strategy.exit should execute on every bar, think of it more like a command rather than an order. The same model should be used for the longCondition.

    if (shortCondition) and strategy.position_size>=0
        strategy.entry("Short Entry 1H", strategy.short, qty = 1, comment = "Opened Short")
        
    if (longCondition)
        strategy.close("Short Entry 1H", comment = "Closed short", immediately = true) 

if not(longCondition)
    strategy.exit(id = "Exit Short", from_entry = "Short Entry 1H",  limit = close[0]-tp, stop = close[0]+sl, comment_profit = "HIT profit", comment_loss = "HIT loss")

if time[1]<end and time>= end
    strategy.close_all()```

Upvotes: 0

Related Questions