EXITING LONG AND SHORT POSITIONS

Viewing 15 posts - 1 through 15 (of 16 total)
  • Author
    Posts
  • #178002 quote
    egyptiantrader
    Participant
    Junior

    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.

    #178003 quote
    robertogozzi
    Moderator
    Master
    • BUY opens a Long position
    • SELL exits a Long position
    • SELLSHORT opens a Short position
    • EXITSHORT exits a Short position

    you have no choice.

    egyptiantrader thanked this post
    #178005 quote
    egyptiantrader
    Participant
    Junior

    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

    #178006 quote
    robertogozzi
    Moderator
    Master

    You can’t because each one of the above keywords is devoted to only one task.

    egyptiantrader thanked this post
    #178007 quote
    PeterSt
    Participant
    Master

    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

    egyptiantrader thanked this post
    #178009 quote
    robertogozzi
    Moderator
    Master

    @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:

    1. the open position is entirely closed
    2. the new position is entirely opened

    no math is done on the different positions.

    egyptiantrader thanked this post
    #178035 quote
    egyptiantrader
    Participant
    Junior

    Thanks guys. I’ve still  got a lot of learning to do.

    #178041 quote
    PeterSt
    Participant
    Master

    “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”.

    #178042 quote
    PeterSt
    Participant
    Master

    Thanks guys. I’ve still got a lot of learning to do.

    No worries. Me too. 🙂

    egyptiantrader thanked this post
    #178045 quote
    robertogozzi
    Moderator
    Master

    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).

    #178052 quote
    PeterSt
    Participant
    Master

    I don’t know where we talk passed each other, but the basis is

    • being long 1
    • sellshort 2

    –> Short 1.

    Apologies if it is my being not clear. 🙂

    #178054 quote
    robertogozzi
    Moderator
    Master

    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
    #178060 quote
    PeterSt
    Participant
    Master

    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
    image_2021-09-21_114105.png image_2021-09-21_114105.png image_2021-09-21_114151.png image_2021-09-21_114151.png image_2021-09-21_114355.png image_2021-09-21_114355.png
    #178065 quote
    egyptiantrader
    Participant
    Junior

    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?

    #178070 quote
    PeterSt
    Participant
    Master

    FYI, the result on IG is exactly the same (the previous was IB) :

    egyptiantrader thanked this post
    image_2021-09-21_162006.png image_2021-09-21_162006.png
Viewing 15 posts - 1 through 15 (of 16 total)
  • You must be logged in to reply to this topic.

EXITING LONG AND SHORT POSITIONS


ProOrder: Automated Strategies & Backtesting

New Reply
Author
Summary

This topic contains 15 replies,
has 3 voices, and was last updated by egyptiantrader
4 years, 5 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 09/20/2021
Status: Active
Attachments: 4 files
Logo Logo
Loading...