0

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)
  • What's your ticker id and timeframe? I don't see any long trades either but both lack of long and short trades seem to be related to your `start` date. – vitruvius Nov 12 '22 at 11:19
  • It could be anything...BTCUSDT (binance) to GOLD (currencycom), at any timeframe. the "same" code works with the long signals with AND without the "if time >= start and time <= end" line – Cero Melkonyan Nov 13 '22 at 12:26
  • Can you send the entire code so we can do the 'long' part ? – G.Lebret Nov 13 '22 at 16:34
  • added the long code to the main post. – Cero Melkonyan Nov 14 '22 at 13:31
  • 1
    Instead of ` if (longCondition and strategy.position_size > 0) ` should be `if (longCondition and strategy.position_size < 0)` – Starr Lucky Nov 17 '22 at 12:35
  • your ```strategy.exit``` should be pulled out of the local scope and inside the session local scope, they will not run the exits unless the short or long conditions are met again – John Baron Nov 18 '22 at 12:01
  • @StarrLucky (longCondition and strategy.position_size > 0) could be just (longCondition) it's not that important – Cero Melkonyan Nov 18 '22 at 14:33
  • @JohnBaron which one is the session local scope? do you mean take it out of the "if (shortCondition).."? or out of the "if time >= start.." loop? I use the strategy exit just fot the TP/SL – Cero Melkonyan Nov 18 '22 at 14:37
  • `if shortCondition` – John Baron Nov 18 '22 at 19:06
  • @JohnBaron If a position of entered the strategy.exit command which i use for tp/sl work fine. The problem with the SHORT code is that the positions are not entered when inside the "if (shortCondition)". and I know for sure the it enters the if loop, I have tried calling other commands. So it enters the if loop but doesnt enter a position is it is a short position, only a long position. – Cero Melkonyan Nov 21 '22 at 15:24
  • i do not see anything above in the script where a short order is entered. – John Baron Nov 21 '22 at 16:36
  • @JohnBaron You are looking at the LONG code. Look at the SHORT code, under the "if (shortCondition).." there are 2 commands: strategy.entry(for entering a short position) and strategy.exit(for tp/sl) – Cero Melkonyan Nov 22 '22 at 14:06
  • all i see is exit long under the shortCondition, i do not see a short entry – John Baron Nov 23 '22 at 02:23
  • @JohnBaron and again you are looking at the LONG code. look at the SHORT code. – Cero Melkonyan Nov 23 '22 at 18:39
  • ```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) ``` this is all i see above, there is no short entry – John Baron Nov 24 '22 at 11:23
  • @JohnBaron I dont know how you are not seeing it, it's half a page above the LONG code. Bute here you go `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) strategy.cancel("Exit Short") strategy.close("Short Entry 1H", comment = "Closed short", immediately = true)` – Cero Melkonyan Nov 25 '22 at 12:53
  • I will add the response in the answer field so the formatting for the script is ok – John Baron Nov 25 '22 at 14:24

1 Answers1

0

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()```
John Baron
  • 291
  • 2
  • 8