1

Can anyone tell me how to know if last trade closed was a long or short in strategy please.

Thank you.

Jakx
  • 13
  • 7

2 Answers2

1

There is a built-in variable for that that tells you what your open position is:
strategy.position_size

For closed trades first you need to find your trade (or trade ID). If it was the very last, it's as simple as that:
strategy.closedtrades.size(strategy.closedtrades - 1)

If the value is > 0, the market position was long. If the value is < 0, the market position was short.

elod008
  • 1,227
  • 1
  • 5
  • 15
  • Do you mean like this, lastclosedLong = strategy.closedtrades.size(strategy.closedtrades - 1) > 0 lastclosedShort = strategy.closedtrades.size(strategy.closedtrades - 1) < 0 – Jakx Sep 03 '22 at 14:12
  • Exactly. The exact result is irrelevant for you for now. Only the sign matters. +/- – elod008 Sep 03 '22 at 14:33
  • Iam using strategy.order and if my strategy.position_size is -7 and i just had a new long trade, Then my new position_size will be -6 right? So will the lastclosedLong is true or lastclosedLong is false? (Since position_size is -6) – Jakx Sep 03 '22 at 14:43
  • Well I think your way of thinking is right but you may be overthinking it a bit. "strategy.closedtrades" is a list of all your closed trades. So you're working with that. What's open? How many? We don't care right now. By the referred function you get the position that just has been closed. How many orders? Doesn't matter. Is it +/-? If + then you closed a long position, if - you closed a short position. What's your new position? We don't care. For what still open is, there is "strategy.opentrades" that takes care of all that. – elod008 Sep 03 '22 at 19:07
  • Thanks for clarification elod008. It is working as expected. Thanks a lot. – Jakx Sep 04 '22 at 09:46
  • You're welcome! There are great resources, make sure to check out the tradingview documentations already linked in my answer, furthermore kodify and tradingcode.net. You can learn a lot from there. – elod008 Sep 06 '22 at 09:28
0

Use the strategy.position_size built-in variable to figure out if a position is closed and its direction.

is_pos_closed = (strategy.position_size[1] != 0 and strategy.position_size == 0) or ((strategy.position_size[1] * strategy.position_size) < 0)
is_long = strategy.position_size > 0
is_short = strategy.position_size < 0

Then all you need to do is when a position is closed, check if the direction was long or short on the previous bar.

was_long = is_pos_closed and is_long[1]
was_short = is_pos_closed and is_short[1]
vitruvius
  • 15,740
  • 3
  • 16
  • 26
  • Why do you prefer it to "strategy.closedtrades.size()" ? Isn't that the "one-liner" tool for this purpose? It seems to me that your solution has the limitation that if I'm x bars after the trade close, it won't work anymore. Anyways, the "is_pos_closed" check is a very elegant solution! – elod008 Sep 04 '22 at 07:55
  • @elod008 you are right on both your points. It depends on his use case to be honest. I usually have bunch of macros defined already and combine them to create new ones :) – vitruvius Sep 04 '22 at 08:58