Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Exit, LastExit and multiple accounts

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

    Exit, LastExit and multiple accounts

    Hello,

    I had been using a snippet of code that was basically bulletproof - when I was using it on a single account. This code looked to see when the first exit (of many exits) in a trade occurred. When that happened, it would ratchet the trailing stop to the entry price. The snippet of code is as follows:

    Code:
    If(
    Performance.Executions[Performance.Executions.Count - 1].Exit &&
    !Performance.Executions[Performance.Executions.Count - 1].LastExit &&
    !TradeData[BarsInProgress].firstExit)
    {
    ...Code runs...
    }
    The "TradeData" object is simply a container for a bunch of custom variables I have; firstExit is a boolean and is set after I've ratcheted up my trailing stop.

    Now, here's the rub: After learning that NT does not support allocation to Interactive Brokers, I've gone to running identical scripts in parallel. The only change for each NinjaScript is the account. In other words, I have two separate IB accounts, and I'm running two identical NT scripts, each with a different account.

    In backtesting for single accounts, the above code is bullet proof. In live trading for single accounts, again it's bullet proof. However, when running identical scripts in parallel, it will not consistently resolve. I can't figure out the situations where it won't resolve, but I can say that it does not consistently resolve as it should. My solution has been to re-load each script when I know this code would have resolved, and thus everything is set correctly. However, this means that each script was run in serial (basically, backtesting conditions).

    Now, you can imagine that I don't want to debug my code in the middle of live trading. And in testing I can't think of ways to debug and run two simultaneous scripts. So I'm here to ask the devs.

    Please give me a rundown on how "Performance.Executions[Performance.Executions.Count - 1].Exit" and Performance.Executions[Performance.Executions.Count - 1].LastExit" work in the context of multiple instruments. I get the feeling that .Exit and .LastExit are somehow globally set variables and are not local to the script. Is this the case? If so, I can always re-build the exit logic, but I'd prefer not to.

    #2
    Hello,

    Thank you for the post.

    I wanted to better understand the question, by definition the data contained in the Performance collection would be for the strategy.

    Definition
    The Performance object holds all trades and trade performance data generated by a strategy.
    Can you clarify what you mean by:
    However, when running identical scripts in parallel, it will not consistently resolve
    What specifically do you mean by it will not consistently resolve, are you seeing a inconsistent value with a specific object used in the provided syntax?

    The mentioned NinjaScript properties are bool type which should have either a true or false value, are you seeing an incorrect value for the type of Execution?
    Code:
    Performance.Executions[Performance.Executions.Count - 1].Exit &&
    !Performance.Executions[Performance.Executions.Count - 1].LastExit
    If this is related to the TradeData object I could not really comment as that is a custom object being used.

    In the case you are seeing a value that is not correct for the Performance object or its sub objects, you could complete a more simple test of just that object in a new script. For example, make a script enter if flat only in realtime, and exit per your logic or in a simple way that is similar to produce the desired error condition. Allow it to do this one time and then check the Performance object to see if the value is what is expected or if it replicates what the other script is doing. This type of test could help eliminate any logic in the existing script that may be at fault, or display the problem in a clear manor if there is a problem.

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Update

      Jesse -

      There are a lot of moving parts here, but there was something I failed to mention earlier but recently came to mind.

      One of the first things I can tell you that I've been noticing is that I'll occasionally lose the market position for at least one of the accounts. Those positions will just "disappear." Restarting IB alone won't fix this; I've had to log out of both IB and NT and restart both to get the market positions to re-appear.

      Now, the next thing I've noticed is the behavior I defined in my original post. What I'd like to make clear (again) is that, when I was running this particular strategy in isolation - first in backtesting, later using the NT7 simulator account, then using an IB paper trading account and, finally, trading live in IB with real money, the code I defined for you was bulletproof. That code is here:

      So I looked at my log. I can see that after one of my most recent trades on 10/7/16, the following happened (I've substituted "Account1" and "Account2" for my actual account names:
      Code:
      10/7/2016 1:06:52 AM|1|32|Order='fb764ab3e190418d83c7d2de5f872ea5/Account2' Name='Account2_CL 11-16_Entry_10/7/2016 1:06:54 AM' New state=PendingSubmit Instrument='CL 11-16' Action=Buy Limit price=0 Stop price=0 Quantity=4 Type=Market Filled=0 Fill price=0 Error=NoError Native error=''
      10/7/2016 1:06:52 AM|1|32|Order='178ec018f12247d9b23ae580d3e58164/Account1' Name='Account1_CL 11-16_Entry_10/7/2016 1:06:54 AM' New state=PendingSubmit Instrument='CL 11-16' Action=Buy Limit price=0 Stop price=0 Quantity=4 Type=Market Filled=0 Fill price=0 Error=NoError Native error=''
      10/7/2016 1:06:52 AM|2|4|Order Event Warning:Attribute 'Outside Regular Trading Hours' is ignored based on the order type and destination. PlaceOrder is now being processed. 2109
      10/7/2016 1:06:52 AM|2|4|Order Event Warning:Attribute 'Outside Regular Trading Hours' is ignored based on the order type and destination. PlaceOrder is now being processed. 2109
      10/7/2016 1:06:53 AM|1|32|Order='1458126420/Account2' Name='Account2_CL 11-16_Entry_10/7/2016 1:06:54 AM' New state=Accepted Instrument='CL 11-16' Action=Buy Limit price=0 Stop price=0 Quantity=4 Type=Market Filled=0 Fill price=0 Error=NoError Native error=''
      10/7/2016 1:06:53 AM|1|32|Order='1458126420/Account2' Name='Account2_CL 11-16_Entry_10/7/2016 1:06:54 AM' New state=Working Instrument='CL 11-16' Action=Buy Limit price=0 Stop price=0 Quantity=4 Type=Market Filled=0 Fill price=0 Error=NoError Native error=''
      10/7/2016 1:06:53 AM|1|32|Order='1458126420/Account2' Name='Account2_CL 11-16_Entry_10/7/2016 1:06:54 AM' New state=Filled Instrument='CL 11-16' Action=Buy Limit price=0 Stop price=0 Quantity=4 Type=Market Filled=4 Fill price=50.52 Error=NoError Native error=''
      10/7/2016 1:06:53 AM|1|16|Execution='CL 11-16/0000e9a5.57f6d39e.01.01' Instrument='CL 11-16' Account='Account2' Exchange=Nymex Price=50.52 Quantity=2 Market position=Long Operation=Insert Order='1458126420' Time='10/7/2016 1:06:52 AM'
      10/7/2016 1:06:53 AM|1|16|Execution='CL 11-16/0000e9a5.57f6d39f.01.01' Instrument='CL 11-16' Account='Account2' Exchange=Nymex Price=50.52 Quantity=1 Market position=Long Operation=Insert Order='1458126420' Time='10/7/2016 1:06:53 AM'
      10/7/2016 1:06:53 AM|1|16|Execution='CL 11-16/0000e9a5.57f6d3a0.01.01' Instrument='CL 11-16' Account='Account2' Exchange=Nymex Price=50.52 Quantity=1 Market position=Long Operation=Insert Order='1458126420' Time='10/7/2016 1:06:53 AM'
      10/7/2016 1:06:53 AM|1|32|Order='1458126421/Account1' Name='Account1_CL 11-16_Entry_10/7/2016 1:06:54 AM' New state=Accepted Instrument='CL 11-16' Action=Buy Limit price=0 Stop price=0 Quantity=4 Type=Market Filled=0 Fill price=0 Error=NoError Native error=''
      10/7/2016 1:06:53 AM|1|32|Order='1458126421/Account1' Name='Account1_CL 11-16_Entry_10/7/2016 1:06:54 AM' New state=Working Instrument='CL 11-16' Action=Buy Limit price=0 Stop price=0 Quantity=4 Type=Market Filled=0 Fill price=0 Error=NoError Native error=''
      10/7/2016 1:06:53 AM|1|32|Order='1458126421/Account1' Name='Account1_CL 11-16_Entry_10/7/2016 1:06:54 AM' New state=Filled Instrument='CL 11-16' Action=Buy Limit price=0 Stop price=0 Quantity=4 Type=Market Filled=4 Fill price=50.52 Error=NoError Native error=''
      10/7/2016 1:06:53 AM|1|16|Execution='CL 11-16/0000e9a5.57f6d3a1.01.01' Instrument='CL 11-16' Account='Account1' Exchange=Nymex Price=50.52 Quantity=1 Market position=Long Operation=Insert Order='1458126421' Time='10/7/2016 1:06:53 AM'
      10/7/2016 1:06:53 AM|1|16|Execution='CL 11-16/0000e9a5.57f6d3a2.01.01' Instrument='CL 11-16' Account='Account1' Exchange=Nymex Price=50.52 Quantity=1 Market position=Long Operation=Insert Order='1458126421' Time='10/7/2016 1:06:53 AM'
      10/7/2016 1:06:53 AM|1|16|Execution='CL 11-16/0000e9a5.57f6d3a3.01.01' Instrument='CL 11-16' Account='Account1' Exchange=Nymex Price=50.52 Quantity=1 Market position=Long Operation=Insert Order='1458126421' Time='10/7/2016 1:06:53 AM'
      10/7/2016 1:06:53 AM|1|16|Execution='CL 11-16/0000e9a5.57f6d3a4.01.01' Instrument='CL 11-16' Account='Account1' Exchange=Nymex Price=50.52 Quantity=1 Market position=Long Operation=Insert Order='1458126421' Time='10/7/2016 1:06:53 AM'
      [B]10/7/2016 1:06:58 AM|1|64|Instrument='CL 11-16' Account='Account1' Avg price=50.52231 Quantity=4 Market position=Long Operation=Insert Currency=UsDollar[/B]
      10/7/2016 3:04:25 AM|1|2|Interactive Brokers (Master): Primary connection=Connected, Price feed=ConnectionLost
      Now I put the last line in bold because that was the only update I got for Account1. No updates happened for Account2. If I remember correctly, it was the market position for Account2 that stopped showing up in NT. Hard to say because I tried to fix this problem when I first encountered it (it was the middle of the night). I did, however, manage to snag the output I always create. There's something interesting:

      Code:
      10/7/2016 1:06:54 AM : Account1 : CL 11-16: Buy Signal Generated: New Long.
      10/7/2016 1:06:54 AM : Account1 : CL 11-16: Flat & submitting Market Buy Order.
      10/7/2016 1:06:54 AM : Account1 : CL 11-16: Order Update: PendingSubmit Buy 4 @ Market
      10/7/2016 1:06:54 AM : Account1 : CL 11-16: Order Update: Accepted Buy 4 @ Market
      10/7/2016 1:06:54 AM : Account1 : CL 11-16: Order Update: Working Buy 4 @ Market
      10/7/2016 1:06:54 AM : Account1 : CL 11-16: Order Update: Filled Buy 4 @ Market
      10/7/2016 1:06:54 AM : Account1 : CL 11-16: Execution: Bought 4 @ 50.53
      [B]10/7/2016 1:06:54 AM : Account1 : CL 11-16: Position Update: Long 4 @ 50.53[/B]
      10/7/2016 1:06:54 AM : Account1 : CL 11-16: Position Update (Trailing Stop): 50.2
      10/7/2016 1:26:33 AM : Account1 : CL 11-16: Trailing Stop (Initial): 50.21
      10/7/2016 1:26:33 AM : Account1 : CL 11-16: Trailing Stop Updated: 50.21
      10/7/2016 3:11:18 AM : Account2 : CL 11-16: Trailing Stop Updated: 50.23
      10/7/2016 3:11:18 AM : Account1 : CL 11-16: Trailing Stop Updated: 50.23
      10/7/2016 3:16:07 AM : Account2 : CL 11-16: Trailing Stop Updated: 50.31
      10/7/2016 3:16:07 AM : Account1 : CL 11-16: Trailing Stop Updated: 50.31
      10/7/2016 3:20:34 AM : Account2 : CL 11-16: Trailing Stop Updated: 50.36
      10/7/2016 3:20:34 AM : Account1 : CL 11-16: Trailing Stop Updated: 50.36
      10/7/2016 3:39:29 AM : Account2 : CL 11-16: Sell Signal Generated: Close Long.
      10/7/2016 3:39:29 AM : Account2 : CL 11-16: Closing 1 long contract(s).
      10/7/2016 3:39:29 AM : Account1 : CL 11-16: Sell Signal Generated: Close Long.
      10/7/2016 3:39:29 AM : Account1 : CL 11-16: Closing 1 long contract(s).
      10/7/2016 3:39:29 AM : Account2 : CL 11-16: Order Update: PendingSubmit Sell 1 @ Market
      10/7/2016 3:39:29 AM : Account1 : CL 11-16: Order Update: PendingSubmit Sell 1 @ Market
      10/7/2016 3:39:29 AM : Account2 : CL 11-16: Order Update: Accepted Sell 1 @ Market
      10/7/2016 3:39:29 AM : Account2 : CL 11-16: Order Update: Working Sell 1 @ Market
      10/7/2016 3:39:29 AM : Account2 : CL 11-16: Order Update: Filled Sell 1 @ Market
      10/7/2016 3:39:29 AM : Account2 : CL 11-16: Execution: Sold 1 @ 50.68
      10/7/2016 3:39:29 AM : Account1 : CL 11-16: Order Update: Accepted Sell 1 @ Market
      10/7/2016 3:39:29 AM : Account1 : CL 11-16: Order Update: Working Sell 1 @ Market
      10/7/2016 3:39:29 AM : Account1 : CL 11-16: Order Update: Filled Sell 1 @ Market
      10/7/2016 3:39:29 AM : Account1 : CL 11-16: Execution: Sold 1 @ 50.68
      [B]10/7/2016 3:39:29 AM : Account1 : CL 11-16: Position Update: Long 3 @ 50.53[/B]
      10/7/2016 3:39:29 AM : Account1 : CL 11-16: Position Update (Trailing Stop): 50.36
      10/7/2016 3:47:56 AM : Account2 : CL 11-16: Trailing Stop (Ratchet): 50.53
      10/7/2016 3:47:56 AM : Account2 : CL 11-16: Trailing Stop Updated: 50.53
      10/7/2016 3:47:56 AM : Account1 : CL 11-16: Trailing Stop (Ratchet): 50.53
      10/7/2016 3:47:56 AM : Account1 : CL 11-16: Trailing Stop Updated: 50.53
      10/7/2016 5:48:41 AM : Account2 : CL 11-16: Closing Long Position.
      10/7/2016 5:48:41 AM : Account1 : CL 11-16: Closing Long Position.
      Now, you'll see that I put in bold "Position Update". In my output, I will note when "OnPositionUpdate() is called (I also note OnOrderUpdate() and OnExecution()). That position update was called from OnPositionUpdate(), but only for Account1. It was never called for Account2. You'll see from the output that Account2 goes on as far as the strategy is concerned, but it doesn't actually know where it is. I didn't include any later log lines because this post is getting too long.

      But to sum up here - it wasn't the Performance object that's the problem - that was just the symptom. The problem appears to be that NT is losing track of its account position from IB.

      Thoughts are welcome.

      Comment


        #4
        Hello cgeorgan,

        NinjaTrader 7 does not support FA (Financial Advisor) or FF (Family and Friends) accounts at all with Interactive Brokers.
        There is a known issue that updates to orders will be updated to the wrong account. A round robin effect essentially causes position updates to be incorrect.


        May I confirm that you are trading to completely separate accounts and that you are not trading to sub accounts with a FA or FF account?
        (This would require that you have multiple instances of TWS running and NinjaTrader is connected via different ports)

        NinjaTrader 8, have corrected this behavior and the individual sub accounts within an FA account can be traded to directly. That said, it is still not supported to trade to the master FA account.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_ChelseaB View Post
          Hello cgeorgan,

          NinjaTrader 7 does not support FA (Financial Advisor) or FF (Family and Friends) accounts at all with Interactive Brokers.
          There is a known issue that updates to orders will be updated to the wrong account. A round robin effect essentially causes position updates to be incorrect.
          So, this would be one of those things where I wish NinjaTrader had a "known issues" thread for NT7. There's already a "Critical Issues" tab for NT8. I probably could have saved myself a lot of grief had I known any of this.

          I can imagine that IB is heavily represented among the NT user base. How many other people had this issue? NT has known of this since 2009 and it was only addressed through NT8. But in the meantime, only IB lets people know that NT doesn't support FA allocations through TWS, and I had to spend a week tearing my hair out, pouring over my code, to learn that this "issue" was around for 7 years.

          Please guys...NT7 is still widely used; there has to be some resource about "known bugs" so that I can just go there and make sure I'm not trying anything crazy.

          Comment


            #6
            Hello cgeorgan,

            Thank you for your response.

            The accounts are technically not supported so this would not be a bug in that aspect for NinjaTrader 7.

            We would not have a list of all unsupported items in NinjaTrader or scenarios such as what you ran into.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by cre8able, Yesterday, 01:16 PM
            3 responses
            11 views
            0 likes
            Last Post cre8able  
            Started by ChartTourist, Today, 08:22 AM
            0 responses
            4 views
            0 likes
            Last Post ChartTourist  
            Started by LiamTwine, Today, 08:10 AM
            0 responses
            2 views
            0 likes
            Last Post LiamTwine  
            Started by Balage0922, Today, 07:38 AM
            0 responses
            5 views
            0 likes
            Last Post Balage0922  
            Started by JoMoon2024, Today, 06:56 AM
            0 responses
            6 views
            0 likes
            Last Post JoMoon2024  
            Working...
            X