I’m trying to work out the best wat to exit from long and short positions, there seems to be multiple ways to do this and when I back test they produce wildly different results.
To enter a short position I can use SELL or SELLSHORT (which one is best)
Once I’m in a short position I can use BUY OR EXIT SHORT
IF c4 THEN
EXITSHORT AT MARKET
ENDIF
or
IF c4 THEN
BUY AT MARKET
ENDIF
Also when I add SHORTONMARKET when using the BUY command to exit a short position this changes the back test result massively when I’d expect it to be the same?
IF c4 AND SHORTONMARKET THEN
BUY AT MARKET
ENDIF
I must be be missing something about these commands? Please can someone explain.
Thank you.
Why can’t I use BUY to exit a short position? Isn’t this the same as EXIT SHORT? I now it produces some very different results
You can’t because each one of the above keywords is devoted to only one task.
Why can’t I use BUY to exit a short position? Isn’t this the same as EXIT SHORT? I now it produces some very different results
Hi – You sure can, but you’d have to know what you are doing, hence keep track of your current positions very well. Thus, if you are 1 Long, you can SellShort 1 and end up with 0. But if the same SellShort 1 command runs into the situation that you don’t have a position, the result will be 1 short.
Similarly, if you have 1 Long and SellShort 2, you end up with 1 short.
IF c4 AND SHORTONMARKET THEN
Now you’ll understand why this makes a vast difference, and why you should always precede your Buy / Sell commands with determining the current situation (If LongOnMarket – If ShortOnMarket).
Also notice that
Sell at Market
and
ExitShort at Market
thus without a quantity, exit the whatever position regardless. You would normally use those, but still first would need to wonder (appropriate If) whether you have the position of concern (Long vs Short respectively).
Peter
@PeterSt
“if you have 1 Long and SellShort 2, you end up with 1 short“
no, you will end up with 2 short positions. Sellshort when a long position is open (as well as Buy when a short position is open) will do a Stop & Reverse, so:
- the open position is entirely closed
- the new position is entirely opened
no math is done on the different positions.
Thanks guys. I’ve still got a lot of learning to do.
“if you have 1 Long and SellShort 2, you end up with 1 short“ no, you will end up with 2 short positions. Sellshort when a long position is open (as well as Buy when a short position is open) will do a Stop & Reverse, […]
Hi Roberto – Interesting. So you are saying that if I have 1 Long and virtually sell 2, I end up with 2 Short ? Thus my transaction of 2 ends up as a transaction with 3 ? It would be a largest bug ever, so that can’t be what you mean.
You are correct on your #1 and #2 and about the “no math”.
If you are Long with 1 position and want to go short with 2, then you’ll end up being short with 2.
Why 3?
If you are already Short 1 position and SELLSHORT 2 positions, then you will end up with 3 positions (provided DEFPARAM CumulateOrders = TRUE).
I don’t know where we talk passed each other, but the basis is
–> Short 1.
Apologies if it is my being not clear. 🙂
I think this is what IG enables you to do with multiple strategies with opposite positions opened, you can enable to group them (for a given instrument), thus they will do some math.
This is not the case with PRT, as opposite positions cannot be opened.
Try this:
a = 1 + (Day MOD 4 = 0)
b = 1 + (Day MOD 5 <> 0)
Sma = average[100,0](close)
IF close CROSSES OVER Sma THEN
BUY a CONTRACTS AT Market
ELSIF close CROSSES UNDER Sma THEN
SELLSHORT b CONTRACTS AT Market
ENDIF
graph CountOfPosition
graph a
graph b
This is not the case with PRT, as opposite positions cannot be opened.
Correct, but opposite positions are not in order (they are not the subject).
I made an example too, which is hopefully the most clear for everybody.
// Demonstration of Long 1 vs SellShort 2.
Defparam Preloadbars = 20 // Otherwise the first trade is outside of the chart.
If BarIndex > 20 then // For better visibility in the left hand side of the chart.
Once BoughtOnce = 0 // We do it one time only, for clarity.
IF Not BoughtOnce and NOT LongOnMarket then
BUY 100 CONTRACTS AT MARKET
// This one (in green) shows one bar only because at the next bar we'll sell it via Short 2.
BoughtOnce = 1
ENDIF
If LongOnMarket then // So this too will occur one time only.
SELLShort 2 Contracts AT MARKET
// These two (in red) stay Short forever because we don't Exit them.
ENDIF
Graph countoflongshares coloured(0,255,0,255) // Green.
Graph countofshortshares coloured(255,0,0,255) // Red.
Endif
I’m on IG so maybe this was causing some of my problems with results all over with small changes to the code that I expected not to make any difference?
FYI, the result on IG is exactly the same (the previous was IB) :