Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Multi strategy variable sync

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

    Multi strategy variable sync

    Hello, I added two data series to a chart (attached). Each has the same strategy running against it. I'd like to stop the strategies when a cumulative PnL is reached for both of them. Is there a way to sync the variables or strategies to allow access either to the whole account PnL or somehow sync the vars? Or maybe create a singleton object that's shared by both strategies?
    Attached Files

    #2
    Hello debarr76,

    Thank you for your post.

    NinjaScript strategies would run independent of each other, so there would not be a means to have them compare against one another or calculate values together between the two in NinjaScript.
    You could however have the strategies write and read and external file in order to achieve the results you are looking for here.

    For an example on reading and writing in a NinjaScript object please visit the following link: http://ninjatrader.com/support/forum...ead.php?t=3477

    Please let me know if you have any questions.

    Comment


      #3
      I have noticed that when you run 2 strategies on the same instrument the order handling is done separately but the OnPositionUpdate is triggered in both strategies when going flat.
      Is this to be expected or a bug?

      This what I am seeing when printing the OnOrderUpdate, OnExecution and OnPositionUpdate events (1 is strategy 1 and 2 is for strategy 2):

      Code:
      **NT** Enabling NinjaScript strategy 'Strategy1/0b76ac84c0bc4af18cff04910cf4feb4' : On starting a real-time strategy - StrategySync=WaitUntilFlat SyncAccountPosition=False EntryHandling=AllEntries EntriesPerDirection=1 StopTargetHandling=PerEntryExecution ErrorHandling=StopStrategyCancelOrdersClosePositions ExitOnClose=False Set order quantity by=Strategy ConnectionLossHandling=KeepRunning DisconnectDelaySeconds=10 CancelEntryOrdersOnDisable=False CancelExitOrdersOnDisable=True CalculateOnBarClose=False MaxRestarts=4 in 5 minutes
      
      **NT** Enabling NinjaScript strategy 'Strategy2/ffe83e3c349047308e79eb273812a45f' : On starting a real-time strategy - StrategySync=WaitUntilFlat SyncAccountPosition=False EntryHandling=AllEntries EntriesPerDirection=1 StopTargetHandling=PerEntryExecution ErrorHandling=StopStrategyCancelOrdersClosePositions ExitOnClose=False Set order quantity by=Strategy ConnectionLossHandling=KeepRunning DisconnectDelaySeconds=10 CancelEntryOrdersOnDisable=False CancelExitOrdersOnDisable=True CalculateOnBarClose=False MaxRestarts=4 in 5 minutes
      
      1: OnOrderUpdate: Order='2d2e2a6c8e4147f7abe2a376ce133180/Sim101' Name='En1' State=PendingSubmit Instrument='ES 09-16' Action=Buy Limit price=0 Stop price=0 Quantity=1 Type=Market Tif=Gtc OverFill=False Oco='' Filled=0 Fill price=0 Token='2d2e2a6c8e4147f7abe2a376ce133180' Gtd='01/01/0001 00:00:00'
      1: OnOrderUpdate: Order='2d2e2a6c8e4147f7abe2a376ce133180/Sim101' Name='En1' State=Accepted Instrument='ES 09-16' Action=Buy Limit price=0 Stop price=0 Quantity=1 Type=Market Tif=Gtc OverFill=False Oco='' Filled=0 Fill price=0 Token='2d2e2a6c8e4147f7abe2a376ce133180' Gtd='01/01/0001 00:00:00'
      1: OnOrderUpdate: Order='2d2e2a6c8e4147f7abe2a376ce133180/Sim101' Name='En1' State=Working Instrument='ES 09-16' Action=Buy Limit price=0 Stop price=0 Quantity=1 Type=Market Tif=Gtc OverFill=False Oco='' Filled=0 Fill price=0 Token='2d2e2a6c8e4147f7abe2a376ce133180' Gtd='01/01/0001 00:00:00'
      1: OnOrderUpdate: Order='2d2e2a6c8e4147f7abe2a376ce133180/Sim101' Name='En1' State=Filled Instrument='ES 09-16' Action=Buy Limit price=0 Stop price=0 Quantity=1 Type=Market Tif=Gtc OverFill=False Oco='' Filled=1 Fill price=2154.5 Token='2d2e2a6c8e4147f7abe2a376ce133180' Gtd='01/01/0001 00:00:00'
      1: OnExecution: Execution='c436cd8d5e07445c8fa857932f6a1049' Instrument='ES 09-16' Account='Sim101' Name='En1' Exchange=Default Price=2154.5 Quantity=1 Market position=Long Commission=1.9 Order='2d2e2a6c8e4147f7abe2a376ce133180' Time='19/07/2016 11:13:05'
      1: OnPositionUpdate: Instrument='ES 09-16' Account='Sim101' Avg price=2154.5 Quantity=1 Market position=Long
      1: OnOrderUpdate: Order='8cf35f71866c4cfe9f8ea368c9fb1473/Sim101' Name='St1NoTarget' State=PendingSubmit Instrument='ES 09-16' Action=Sell Limit price=0 Stop price=2153.5 Quantity=1 Type=Stop Tif=Gtc OverFill=False Oco='OCO1 11:14:00.0000' Filled=0 Fill price=0 Token='8cf35f71866c4cfe9f8ea368c9fb1473' Gtd='01/01/0001 cube1 Ratio Short = 50, cube2 Ratio Short = 0.02, Cube1 Sum Bid = 31, Cube1 Sum Ask = 1244, Cube2 Sum Bid = 31, Cube2 Sum Ask = 1244
      00:00:00'
      1: OnOrderUpdate: Order='8cf35f71866c4cfe9f8ea368c9fb1473/Sim101' Name='St1NoTarget' State=Accepted Instrument='ES 09-16' Action=Sell Limit price=0 Stop price=2153.5 Quantity=1 Type=Stop Tif=Gtc OverFill=False Oco='OCO1 11:14:00.0000' Filled=0 Fill price=0 Token='8cf35f71866c4cfe9f8ea368c9fb1473' Gtd='01/01/0001 00:00:00'
      1: OnOrderUpdate: Order='8cf35f71866c4cfe9f8ea368c9fb1473/Sim101' Name='St1NoTarget' State=PendingCancel Instrument='ES 09-16' Action=Sell Limit price=0 Stop price=2153.5 Quantity=1 Type=Stop Tif=Gtc OverFill=False Oco='OCO1 11:14:00.0000' Filled=0 Fill price=0 Token='8cf35f71866c4cfe9f8ea368c9fb1473' Gtd='01/01/0001 00:00:00'
      1: OnOrderUpdate: Order='0b95c3bf5c12477fa93519246d05c30a/Sim101' Name='ExitRules' State=Accepted Instrument='ES 09-16' Action=Sell Limit price=0 Stop price=0 Quantity=1 Type=Market Tif=Gtc OverFill=False Oco='OCO1 11:14:00.0000' Filled=0 Fill price=0 Token='0b95c3bf5c12477fa93519246d05c30a' Gtd='01/01/0001 00:00:00'
      1: OnOrderUpdate: Order='0b95c3bf5c12477fa93519246d05c30a/Sim101' Name='ExitRules' State=Working Instrument='ES 09-16' Action=Sell Limit price=0 Stop price=0 Quantity=1 Type=Market Tif=Gtc OverFill=False Oco='OCO1 11:14:00.0000' Filled=0 Fill price=0 Token='0b95c3bf5c12477fa93519246d05c30a' Gtd='01/01/0001 00:00:00'
      1: OnOrderUpdate: Order='0b95c3bf5c12477fa93519246d05c30a/Sim101' Name='ExitRules' State=Filled Instrument='ES 09-16' Action=Sell Limit price=0 Stop price=0 Quantity=1 Type=Market Tif=Gtc OverFill=True Oco='OCO1 11:14:00.0000' Filled=1 Fill price=2154.25 Token='0b95c3bf5c12477fa93519246d05c30a' Gtd='01/01/0001 00:00:00'
      1: OnExecution: Execution='e8e4ef7259674319928708ee915960ef' Instrument='ES 09-16' Account='Sim101' Name='ExitRules' Exchange=Default Price=2154.25 Quantity=1 Market position=Short Commission=1.9 Order='0b95c3bf5c12477fa93519246d05c30a' Time='19/07/2016 11:13:06'
      1: OnPositionUpdate: Instrument='ES 09-16' Account='Sim101' Avg price=0 Quantity=0 Market position=Flat
      [COLOR="Red"]2: OnPositionUpdate: Instrument='ES 09-16' Account='Sim101' Avg price=0 Quantity=0 Market position=Flat[/COLOR]
      Edit:
      I noticed it happens only after the first trade (the redundant OnPositionUpdate)
      Last edited by ToMer_K; 07-20-2016, 12:01 AM.

      Comment


        #4
        Hello ToMer_K,

        Just to clarify, you are running two different strategies on the same instrument on the same account. The behavior you are seeing is: as the position changes on one strategy, this causes the OnPositionUpdate of both strategies to be triggered.

        I was not able to reproduce this behavior.
        Attached is a very simple set of strategies to test this behavior.
        Below is a link to a video I have made while testing this.
        http://screencast.com/t/hXRf1FuljE

        Are you able to reproduce this behavior using the strategies I have provided?
        Attached Files
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          I am sorry, I thought this was a thread for NT7. Should I start another one for NT7?

          Comment


            #6
            Hello ToMer_K,

            That was my mistake.

            I am retesting using NinjaTrader 7.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hello ToMer_K,

              Using NinjaTrader 7 I am able to reproduce this behavior.
              OnPositionUpdate will occur in a strategy even if no position was updated if a second strategy is running on that instrument and account


              However, having tested with NT8 Beta 12 first, I can confirm that this is resolved with NinjaTrader 8.
              Attached Files
              Last edited by NinjaTrader_ChelseaB; 07-21-2016, 10:28 AM.
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                Hi ChelseaB,
                Thanks for checking it out for me. This is exactly what I am seeing and it did interfere with one of my strategies that has code in OnPositionUpdate.

                Currently I solved it with keeping the last MarketPosition and disregarding a position update that has the same MarketPosition as the previous update. I am using a code that needs to run only once after each position update in a specific direction (= I don't need to run it twice if I have a consecutive position updates in the same direction).

                The problem is that if Strategy 1 enters Long and Strategy 2 enters Long before Strategy 1 exits, if Strategy 1 exits it may show MarketPosition = Flat in Strategy 2 which will be false.

                I know that NT8 is advancing but it has many unsolved bugs so it will take at least a few more months to a stable final version and a few more for all indicator vendors to make NT8 editions. That means that I, and many others, will still use NT7 for live trading for many more months.
                I do think bugs like that should be addressed and fixed because it can interfere with any strategy that relies on OnPositionUpdate

                Comment


                  #9
                  Hello ToMer_K,

                  I have forward your request to have this bug fixed with NinjaTrader 7 to our development deparment.

                  As this item has existed in NinjaTrader 7 since day one, and as NinjaTrader 7 is in code lockdown (meaning there will be no further bug fixes or feature request implementation) this item will not be corrected in NinjaTrader 7.

                  Our developers are currently directing all resources to completing NinjaTrader 8 so that a stable release can be pushed as soon as possible.

                  We appreciate your understanding with this matter.
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    Ok, understood. But if there will be a version update for some reason than I hope it will be fixed in it.

                    Comment


                      #11
                      Originally posted by ToMer_K View Post
                      Currently I solved it with keeping the last MarketPosition and disregarding a position update that has the same MarketPosition as the previous update. I am using a code that needs to run only once after each position update in a specific direction (= I don't need to run it twice if I have a consecutive position updates in the same direction).
                      Yep, I've seen this same behavior in my strategies. If I run a LONG only strategy against CL simultaneously with a SHORT only strategy in CL on the same account, I've seen OnPositionUpdate trigger in the idle strategy. Just like in Chelsea's video, my idle strategy (which already has a current strategy position of flat) receives an OnPositionUpdate with MarketPosition.Flat when the other active strategy's position becomes flat.

                      Anyhoo ...

                      About your comment:
                      OnPositionUpdate is sent to the strategy whenever there is a change in MarketPosition (LONG/SHORT/FLAT) or the current position's Quantity. Thus, if you scale-in or scale-out of a position (meaning MarketPosition hasn't changed) you will also get OnPositionUpdate with an updated Quantity only.

                      My point is:
                      I recommend you update your code logic to handle changes in both variables -- only then can you be sure to catch all spurious OnPositionUpdate's from the other strategy's activity.
                      Last edited by bltdavid; 07-22-2016, 04:02 PM.

                      Comment


                        #12
                        Thanks for the help. I am aware of the position update events in scaling in and out. My strategy needs to handle the code in OnPositionUpdate only once when going Long or Short no matter if it changes quantity so my solution fits me.

                        In order to fully control this odd behavior the inner strategy order tracking is the only way to know if the position change is legit.

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by funk10101, Today, 09:43 PM
                        0 responses
                        6 views
                        0 likes
                        Last Post funk10101  
                        Started by pkefal, 04-11-2024, 07:39 AM
                        11 responses
                        37 views
                        0 likes
                        Last Post jeronymite  
                        Started by bill2023, Yesterday, 08:51 AM
                        8 responses
                        44 views
                        0 likes
                        Last Post bill2023  
                        Started by yertle, Today, 08:38 AM
                        6 responses
                        26 views
                        0 likes
                        Last Post ryjoga
                        by ryjoga
                         
                        Started by algospoke, Yesterday, 06:40 PM
                        2 responses
                        24 views
                        0 likes
                        Last Post algospoke  
                        Working...
                        X