Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Reversing positions

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Reversing positions

    The following code backtests OK but in live simulated trading the initial 1 short contract gets reversed to 2 long contracts, whereas I expect that there to be only 1 long contract. The live simulation result is shown in attached graphic.

    The relevant reversing code segment is given below. I don't see what is wrong with my code (especially since it backtests OK).

    Code:
    [FONT=Courier New][SIZE=2][COLOR=#0000ff]else[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] (Position.MarketPosition == MarketPosition.Short)[/SIZE][/FONT]
    [SIZE=2][FONT=Courier New]{[/FONT][/SIZE]
    [SIZE=2][FONT=Courier New]sellsToday = [/FONT][/SIZE][FONT=Courier New][SIZE=2][COLOR=#0000ff]true[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2];[/SIZE][/FONT]
    [SIZE=2][FONT=Courier New]traded = [/FONT][/SIZE][FONT=Courier New][SIZE=2][COLOR=#0000ff]true[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2];[/SIZE][/FONT]
     
     
     
    [FONT=Courier New][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] (!buysToday)[/SIZE][/FONT]
    [SIZE=2][FONT=Courier New]{[/FONT][/SIZE]
    [FONT=Courier New][SIZE=2]bp = MIN(Low, BarsSinceEntry())[[/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2]] + diff; [/SIZE][/FONT]
    [FONT=Courier New][SIZE=2]ExitShortStop(bp); [/SIZE][/FONT]
    [FONT=Courier New][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] ( (ToTime(Time[[/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2]]) <= Time_RevEnd) && ShortRev )[/SIZE][/FONT]
    [SIZE=2][FONT=Courier New]{[/FONT][/SIZE]
    [SIZE=2][FONT=Courier New]EnterLongStop(bp, [/FONT][/SIZE][FONT=Courier New][SIZE=2][COLOR=#800000]"SRev"[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2]);[/SIZE][/FONT]
    [SIZE=2][FONT=Courier New]LongRev = [/FONT][/SIZE][FONT=Courier New][SIZE=2][COLOR=#0000ff]false[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2];[/SIZE][/FONT]
    [SIZE=2][FONT=Courier New]sellsToday = [/FONT][/SIZE][FONT=Courier New][SIZE=2][COLOR=#0000ff]false[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2];[/SIZE][/FONT]
    [SIZE=2][FONT=Courier New]}[/FONT][/SIZE]
    [SIZE=2][FONT=Courier New]}[/FONT][/SIZE]
    [SIZE=2][FONT=Courier New]}[/FONT][/SIZE]
    Attached Files

    #2
    Hi jonesr227,

    Are you using CalculateOnBarClose = false? I suspect the reason you are experiencing extra reversals is because your code is running through the logic more than once while your first reversal order is still in a pending/working state. This can happen because your check is only a Position check and even though you submitted an order to close this position you will still have the position until the order is completed.

    Please put in some Print() to see what exactly is happening in your code. A print right after the ExitShortStop(bp) will work. I suspect it will print twice. You can prevent this with a variable that goes true right after the first exit and only allows for the logic to run again if that variable is false.
    Last edited by NinjaTrader_JoshP; 09-12-2007, 11:50 AM.
    Josh P.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by samish18, Yesterday, 08:31 AM
    2 responses
    8 views
    0 likes
    Last Post elirion
    by elirion
     
    Started by Mestor, 03-10-2023, 01:50 AM
    16 responses
    389 views
    0 likes
    Last Post z.franck  
    Started by rtwave, 04-12-2024, 09:30 AM
    4 responses
    31 views
    0 likes
    Last Post rtwave
    by rtwave
     
    Started by yertle, Yesterday, 08:38 AM
    7 responses
    29 views
    0 likes
    Last Post yertle
    by yertle
     
    Started by bmartz, 03-12-2024, 06:12 AM
    2 responses
    23 views
    0 likes
    Last Post bmartz
    by bmartz
     
    Working...
    X