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 terofs, Yesterday, 04:18 PM
    1 response
    21 views
    0 likes
    Last Post terofs
    by terofs
     
    Started by CommonWhale, Today, 09:55 AM
    1 response
    3 views
    0 likes
    Last Post NinjaTrader_Erick  
    Started by Gerik, Today, 09:40 AM
    2 responses
    7 views
    0 likes
    Last Post Gerik
    by Gerik
     
    Started by RookieTrader, Today, 09:37 AM
    2 responses
    13 views
    0 likes
    Last Post RookieTrader  
    Started by alifarahani, Today, 09:40 AM
    1 response
    7 views
    0 likes
    Last Post NinjaTrader_Jesse  
    Working...
    X