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

Can a live account strategy follow Sim account?

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

    Can a live account strategy follow Sim account?

    Hi NT!

    My Q is: can I have a strategy that can enter live trades based on the trade activity of a Sim account? Looking for something like the following:

    IF ( Sim101 account = Market Position: Long 1 Contract)
    THEN (EnterLong for Live account 1 Contract)


    Thank you,



    #2
    Hello Austiner87,

    Thank you for your post.

    You could use the Account class <Account>.Positions collection to get the position of an account, like the Sim101 account.

    See this help guide page for information about the Account class and sample code: https://ninjatrader.com/support/help...ount_class.htm

    Looping through the Account.Orders collection for the account could be done to find active orders.

    Subscribe to Account OrderUpdate events to look for any newly opened orders. Subscribe to Account PositionUpdate events to see position updates on the account.

    Account.Positions - https://ninjatrader.com/support/help...ns_account.htm
    Account.PositionUpdate - https://ninjatrader.com/support/help...tionupdate.htm
    Account.Orders - https://ninjatrader.com/support/helpguides/nt8/orders_account.htm
    Account.OrderUpdate - https://ninjatrader.com/support/helpguides/nt8/orderupdate.htm

    Please let me know if you have any other questions.
    Gaby V.NinjaTrader Customer Service

    Comment


      #3
      Hello, I followed these guides and am having success, I hit a snag on following the Exits. Please see below picture for the relevant code block - I am checking to see IF the "Sim101" is Long/Short and how many contracts, and If true, then I enter Long/Short on the other account that is attempting to follow the trades (this part of the code works perfectly). I coded the opposite binary for exits: IF "Sim101" is Flat, and position size == 0, then submit an exit order for Long and Short. The entries are working, but the exits are not, can you please help me understand how I can follow the exits? I have the entire code included as an attachment in a note file.

      Click image for larger version

Name:	Pic of Exit Code not working.png
Views:	123
Size:	29.5 KB
ID:	1275538

      Comment


        #4
        Hello Austiner87,

        Using exit methods these will only exit a position open in the strategy.

        Place the order to the account with the addon approach with Account.CreateOrder() and Account.Submit().

        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hi Chelsea and thank you!

          I am still having trouble, can you please let me know what might be wrong with this code line - it is not entering where the << EnterLong(1,"") >> worked before:

          myAccount.CreateOrder(Instrument.GetInstrument("MN Q 12-23"), OrderAction.Buy, OrderType.Market, OrderEntry.Automated, TimeInForce.Day, 1, 0, 0, "myOCO", "1", Core.Globals.MaxDate, null);​


          Used from the weblink provided:

          Syntax
          CreateOrder(Instrument instrument, OrderAction action, OrderType orderType, OrderEntry orderEntry, TimeInForce timeInForce, int quantity, double limitPrice, double stopPrice, string oco, string name, DateTime gtd, CustomOrder customOrder​

          Click image for larger version  Name:	Entry not working.png Views:	0 Size:	39.2 KB ID:	1275829
          Last edited by Austiner87; 11-01-2023, 08:50 AM.

          Comment


            #6
            Hello,

            After creating the order, you will need to Submit() them.

            Submit() - https://ninjatrader.com/support/help...nt8/submit.htm

            Please let me know if you have any other questions.
            Gaby V.NinjaTrader Customer Service

            Comment


              #7
              Hello Gaby and ChelseaB,

              you've been extremely helpful, I still can't get this order to submit: in either case below. just need a bit more direction as to what I need to fix, thank you! full code included as notepad file

              Click image for larger version

Name:	2.png
Views:	84
Size:	13.7 KB
ID:	1276086

              OR

              Click image for larger version

Name:	1.png
Views:	82
Size:	16.6 KB
ID:	1276087


              Comment


                #8
                Hello,

                Did you send the latest version of your strategy? The one posted is still using EnterLong() and EnterShort() to submit orders.

                That said, if the strategy is applied to a chart, is there data with new bars appearing on the chart the Strategy is applied to?

                Is the strategy showing as enabled on the Strategies tab of the Control Center?

                If the strategy is in the Strategy Analyzer, is there data appearing on the Chart Display of the Strategy Analyzer?

                Importantly, are there errors appearing on the Log tab of the Control Center?

                Try testing the Sample MA Crossover strategy included with NinjaTrader on the same chart or Strategy Analyzer using the same instrument, bar type, interval, and date range.

                Do you see results with the Sample MA Crossover strategy? (This would confirm for us data is available and the issue is with the logic in the custom script)


                If the strategy is getting data and the strategy is enabled or a backtest is run with no errors in the Log tab of the Control Center, and the Sample MA Crossover is returning results, then would likely indicate the logic conditions in the custom strategy did not evaluate as true or orders are being ignored or cancelled.

                In order to better understand how the code is working, it will be necessary to use Print to see how the conditions are evaluating and enable TraceOrders to see if orders are being submitted, ignored, rejected, or cancelled.

                Below is a link to a forum post that demonstrates using prints to understand behavior and includes a link to a video recorded using the Strategy Builder to add prints.

                https://ninjatrader.com/support/foru...121#post791121

                Enable TraceOrders, print the time of the bar and all values used in the conditions that submit entry orders. Include labels for all values and comparison operators.

                Let me know if you need any assistance creating a print or enabling TraceOrders.

                Save the output from the output window to a text file and provide this with your reply.

                I'll be happy to assist with analyzing the output.
                Gaby V.NinjaTrader Customer Service

                Comment


                  #9
                  Hi Gaby and ChesleaB,

                  Please see this short video on this private youtube link: I am connected to live data, you see contract data populating on the chart windows, the bar charts show bards updating, the strategy is enabled, and there are no errors in the log tab (picture included below). I am not attempting to use the strategy in the analyzer.

                  Youtube Link: https://www.youtube.com/watch?v=96pU...PersonalVideos

                  I've attached the code for reference that I review in the video,
                  I also attached the Log tab, executions tab, and a notepad of the output window from the video.

                  Thank you!

                  FollowTradesCode.txt

                  Attached Files

                  Comment


                    #10
                    Hello,

                    Looking at the saved output, it looks like the order is being submitted:

                    11/6/2023 1:20:01 PM Strategy 'FollowTradesMarket/310312960': Entered internal SubmitOrderManaged() method at 11/6/2023 1:20:01 PM: BarsInProgress=0 Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='' FromEntrySignal=''

                    Position: Long at 1

                    Your strategy then attempts to keep resubmitting the same order, but it is subsequently ignored:

                    11/6/2023 1:20:02 PM Strategy 'FollowTradesMarket/310312960': Ignored SubmitOrderManaged() method at 11/6/2023 1:20:02 PM: BarsInProgress=0 Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Buy' FromEntrySignal='' Reason='There already is a matching order with same prices and quantity'

                    Position: Long at 1

                    The order is also being ignored due to the EntryHandling properties of your script:


                    11/6/2023 1:20:02 PM Strategy 'FollowTradesMarket/310312960': Ignored SubmitOrderManaged() method at 11/6/2023 1:20:02 PM: BarsInProgress=0 Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Buy' FromEntrySignal='' Reason='Exceeded entry signals limit based on EntryHandling and EntriesPerDirection properties'

                    Position: Long at 1


                    This can come up when our code is calling an Enter method with a Signal Name that was already entered, or when we have exceeded the maximum allowed number of entries for the strategy in general.


                    It is important to understand how EntryHandling and EntriesPerDirection come into play.

                    EntryHandling controls what EntriesPerDirection limits. Should we limit every single entry that gets called, or should we limit the number of times each unique entry signal can (re-)enter?

                    EntriesPerDirection controls how many times we can enter in that direction.


                    For example, if EntryHandling is set to UniqueEntries, and EntriesPerDirection is set to 1,
                    • EnterLong(“MySignal”); may only be entered once. If “MySignal” is in position and this Enter method gets called again, we will see our error. the position opened by “MySignal” will need to be closed before the next entry can be made with that signal name
                    • However, we can call: 1. EnterLong(“MySignal1”); → 2. EnterLong(“MySignal2”); → 3. EnterLong(“MySignal3”); and these will all be allowed.


                    If EntryHandling is set to AllEntries, and EntriesPerDirection is set to 1,
                    • Calling 1. EnterLong(“MySignal1”); → 2. EnterLong(“MySignal2”); → 3. EnterLong(“MySignal3”); will only allow “MySignal1” to be submitted.

                    Please let me know if you have any questions.
                    Gaby V.NinjaTrader Customer Service

                    Comment


                      #11
                      Hello Gaby,

                      I understand your reply, and now much better understand AllEntries versus UniqueEntries. Thank you! However, I don't believe this is the issue. The output you reference is ignoring further orders to EnterLong because I have EntriesPerDirection set to 1. I tried changing it to Unique Entries and also naming the entries and exits, and I get the same result - entries work OK, the output prints that other entry orders are being ignored, and when I exit on Sim101, the strategy does not exit on the other account.

                      I am still having this issue: when I exit on Sim101 and the account is flat, the strategy stops submitting orders, and as a result also doesn't submit the exit order I am trying to submit. The exit order is the part of the code that is not happening that I am trying to make functional.

                      Now, I may not be seeing how your response connects to this - as it may be that the exits are not being submitted because of the ignored orders you called out in your reply above, but I need help understanding how they relate so I can fix it.


                      Comment


                        #12
                        Hello Austiner87,

                        These messages would only be appearing if using order methods like EnterLong() and not through the account with <Account>.CreateOrder() and <Account>.Submit.

                        Just to confirm, you are using the strategy to place orders to the strategy selected account and then using <Account>.Submit() to submit the orders to a different account when the strategy order fills, is this correct?



                        With the ignored orders, i am not showing you are using unique entries as you have stated.

                        Enabling NinjaScript strategy 'FollowTradesMarket/310312960' : On starting a real-time strategy - StartBehavior=ImmediatelySubmit EntryHandling=All entries EntriesPerDirection=1 StopTargetHandling=Per entry execution ErrorHandling=Stop strategy, cancel orders, close positions ExitOnSessionClose=True / triggering 30 seconds before close SetOrderQuantityBy=Strategy ConnectionLossHandling=Recalculate DisconnectDelaySeconds=10 CancelEntriesOnStrategyDisable=False CancelExitsOnStrategyDisable=False Calculate=On each tick IsUnmanaged=False MaxRestarts=4 in 5 minutes

                        The strategy was enabled with EntryHandling=All entries and the EntriesPerDirection=1. (If you've changed this in State.SetDefaults, then you will need to remove the instance and add a new instance to pull the new instance. Or alternatively, just set this in State.Configure)

                        I am seeing there was an order with an empty string as the signal name.

                        11/6/2023 1:20:01 PM Strategy 'FollowTradesMarket/310312960': Entered internal SubmitOrderManaged() method at 11/6/2023 1:20:01 PM: BarsInProgress=0 Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='' FromEntrySignal=''

                        After this order fills, no further orders can be submitted as the Entry handling with all entries has met 1 in that direction.


                        The output is matching an existing order with the name Buy. This is not Buy1 or Buy2 or Buy3, it's just Buy over and over.

                        11/6/2023 1:20:02 PM Strategy 'FollowTradesMarket/310312960': Ignored SubmitOrderManaged() method at 11/6/2023 1:20:02 PM: BarsInProgress=0 Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Buy' FromEntrySignal='' Reason='There already is a matching order with same prices and quantity'

                        11/6/2023 1:20:02 PM Strategy 'FollowTradesMarket/310312960': Ignored SubmitOrderManaged() method at 11/6/2023 1:20:02 PM: BarsInProgress=0 Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Buy' FromEntrySignal='' Reason='Exceeded entry signals limit based on EntryHandling and EntriesPerDirection properties'


                        Have you made changes and not provided the new output from the output window?

                        You could also choose to add to the entry condition that the position is flat or less than a certain quantity to prevent the ignored order.



                        "when I exit on Sim101 and the account is flat, the strategy stops submitting orders, and as a result also doesn't submit the exit order I am trying to submit."

                        The strategy order methods can only place exit orders to exit the position strategy. If the strategy is flat (even if the account is not), exit orders will be ignored as there is no position to exit.
                        You will need to submit orders through the Account.Submit() if you submit an order to the account.
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          Hi Gaby,

                          Thank you very very much for these messages!! I've made changes, but I am still stuck on trying to get the strategy to exit / track when Sim101 is flat so I can submit an order to exit my position. Please see this video, it shows it in detail:



                          attached is the code I review in the video, as well as the output window and log.

                          Thank you!!
                          Attached Files

                          Comment


                            #14
                            Hello,

                            This would likely indicate the logic conditions in the custom strategy did not evaluate as true in order for your logic to exit when Sim101 is flat to execute.

                            In order to better understand how the code is working, it will be necessary to use Print to see how the conditions are evaluating

                            Use a Print() statement to see if your condition is evaluating as true. Right now you are just printing the Market Position and Position quantity, but your if statements are also checking your ContractsLong variable. I would include this in your print statement.

                            Also, your print statement should be one line above and outside of the condition. If the print is inside of the condition, it will only print when the condition is true. We want to print when the condition is false to understand why it is evaluating as false
                            Last edited by NinjaTrader_Gaby; 11-07-2023, 07:10 AM.
                            Gaby V.NinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by llanqui, Today, 03:53 AM
                            0 responses
                            5 views
                            0 likes
                            Last Post llanqui
                            by llanqui
                             
                            Started by burtoninlondon, Today, 12:38 AM
                            0 responses
                            10 views
                            0 likes
                            Last Post burtoninlondon  
                            Started by AaronKoRn, Yesterday, 09:49 PM
                            0 responses
                            15 views
                            0 likes
                            Last Post AaronKoRn  
                            Started by carnitron, Yesterday, 08:42 PM
                            0 responses
                            11 views
                            0 likes
                            Last Post carnitron  
                            Started by strategist007, Yesterday, 07:51 PM
                            0 responses
                            14 views
                            0 likes
                            Last Post strategist007  
                            Working...
                            X