• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Add-on for ATM execution

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

    Add-on for ATM execution

    Currently I manually use an ATM (to set profit and stop-loss targets), and then manually add more contracts to reverse the order (at the stop-loss). For example, if I have a 1-contract position, my stop-loss is 1 contract; I would manually add 5 more contracts to reverse my position to a 5-contract position.

    First, is it possible for an Add-on to automatically add the additional contracts at the stop-loss (to effect a reversal of position)? That is, can a manually entered ATM be tracked by an Add-on, or are we limited to only automatic entries?

    And second, could this be implemented within the ATM directly (to avoid programming an Add-on)? Is it possible to modify the ATM?

    Also, can you (or anyone) provide FULL CODE examples for an Add-on that does something similar? This would be helpful in getting up to speed quickly in NinjaScript.

    Thanks, Ron.
    Last edited by ronhb107; 11-12-2017, 08:21 AM. Reason: to emphasize that the ATM is manually entered

    #2
    Hello ronhb107,

    Thank you for your note.

    Yes, an active ATM can be tracked by an Addon. I have provided a strategy example which finds an active ATM Strategy and manages it. You could use similar logic in an Addon. In the sample you’ll see I loop through the Account and Orders working on that account. I find the ATM by template name then the order “Stop1” associated with that ATM.

    At this point you could find the price that stop was working and then submit the desired order for your reversal.

    I also attached an Addon which if you open and press the DoSomething Button, working orders as well as ATMs working on the account will be printed.

    Please see the attached two examples.

    Please let us know if you need further assistance.
    Attached Files
    Alan P.NinjaTrader Customer Service

    Comment


      #3
      Thanks, Alan. This is very helpful.

      I am working thru the code and making modifications as an Addon.

      Perhaps you can answer this: I only want to modify the StopLoss.Quantity (number of contracts) when the ATM actually fires (adds a position). At this point, I will add to the StopLoss.Quantity) by a multiple of 5x to both the StopLoss (at its current position) and a Target location 3-ticks from the StopLoss price with a limit order of the same quantity (of the multiple).

      A code snippet would be helpful.

      Again, thanks so much.

      Ron

      Comment


        #4
        Hello ronhb107,

        I modified the previous strategy script so that when the strategy is enabled, the strategy will modify the quantity of the stop by 2 contracts.

        I used,

        Code:
        stopOrder.QuantityChanged =stopOrder.Quantity+2;
        MainAccount.Change(new[] { stopOrder });  //Submits change order call.
        Please let us know if you need further assistance.
        Attached Files
        Alan P.NinjaTrader Customer Service

        Comment


          #5
          This is great. Thanks so much.

          One last request. Is there a way for the Addon to recognize the active Account and Selected ATM from the Chart? I'd like to avoid hard-coding the two since I switch between my live account and Sim as well as ATMs.

          Also, when the Stop-Loss quantity is increased by the code, will this be displayed (and modifiable) on the Chart?

          Thanks again.
          Ron
          Last edited by ronhb107; 11-13-2017, 11:08 PM.

          Comment


            #6
            Hello ronhb107,

            I put together a sample indicator which when applied to a chart with chart trader open, will print the selected ATM strategy as well as the chart trader account.

            Regarding your second question, did you test the script I provided for whether the stop loss will be displayed and modifiable on the chart?

            I look forward to your reply.
            Attached Files
            Alan P.NinjaTrader Customer Service

            Comment


              #7
              Hi Alan:

              Ok. I installed the indicator on my Chart (Pt&Fig).

              I went into Playback mode to test and tried Bar, Tick, and Price change parameters. Nothing printed.

              I've attached a pic so you can see the charting environment (which might help to explain why it's not working).

              With regard to your question, I have not tested the prior codes yet and I am only considering prior posts of others where there is nothing displayed on the chart. When we resolve this, I will test the modified code that I am creating.

              Thanks,
              Ron
              Attached Files
              Last edited by ronhb107; 11-14-2017, 03:22 PM.

              Comment


                #8
                Hello ronhb107,

                I am unable to replicate and its not clear from your screen shot whether you have an output window open. Do you have an output window open?

                Regarding your question on the behavior of the stop quantity, I suggest testing the script I provided before asking questions of its behavior.

                Please let us know if you need further assistance.
                Alan P.NinjaTrader Customer Service

                Comment


                  #9
                  My error, Alan. I did not have a NinjaScript Output window open.

                  Yes, it is working. I will now try to modify this code to only respond when the position is opened (and not pending).

                  Thanks,
                  Ron

                  Comment


                    #10
                    Compile errors

                    This code segment won't compile and I can't determine why.

                    else if (State == State.DataLoaded)
                    {
                    OrderState orderState;
                    if(orderState == OrderState.Filled) //This will print all filled orders on the account
                    // if (OrderState.Filled)
                    {
                    Print("For Account: " + xAlselector.SelectedAccount.ToString());
                    Print("For Strategy: " + xAtmStrategySelector.SelectedAtmStrategy.ToString( ));

                    Account MainAccount; // = Account.All.FirstOrDefault(a => a.Name == xAlselector.SelectedAccount.ToString());

                    // foreach(Order order in MainAccount.Orders) //First foreach loop, to get working orders
                    foreach(Order order in MainAccount.Orders) //First foreach loop, to get working orders
                    {

                    if(order.OrderState == OrderState.Filled) //This will print all working orders on the account
                    Print("This order is working"+order.ToString());
                    }

                    }
                    }

                    Comment


                      #11
                      Hello ronhb107,

                      Thank you for reaching back. I am responding to you on behalf of my colleague Alan who is out of the office today.

                      Please be sure to include the compile errors you are receiving so we can provide further input.

                      I took a guess and added that snippet to Alan's indicator and received the following errors:

                      NinjaScript File Error Code Line Column
                      aaPrintSelectedAccountAndATMStrategy.cs Use of unassigned local variable 'orderState' CS0165 64 7
                      NinjaScript File Error Code Line Column
                      aaPrintSelectedAccountAndATMStrategy.cs Use of unassigned local variable 'MainAccount' CS0165 73 27
                      This is because these objects have been created, but they are being referenced when they do not have an assigned value. The code you have commented out assigning a value to MainAccount would have resolved the second compiler error.

                      I am unsure what you are trying to accomplish with your code. State.DataLoaded occurs after all data series are loaded and would not happen for an AddOn. (AddOn's reach State.Active instead of Data Loading States.) Also, your 1st check for an order state being filled before having an order to reference would be incorrect. Later on in the code you reference each Order in MainAccount.Orders, which would have valid references and would be correct.

                      I've included links to publicly available documentation on OnStateChange() and the NinjaScript life cycle for further reference on how these states are iterated for AddOn's Indicators and Strategies.

                      NinjaScript Lifecycle - https://ninjatrader.com/support/help...fecycle_of.htm

                      OnStateChange() - https://ninjatrader.com/support/help...tatechange.htm

                      If you have any additional questions, please don't hesitate to write back.
                      JimNinjaTrader Customer Service

                      Comment


                        #12
                        Thanks for the feedback, Jim.

                        I am trying to determine the StopPrice when a manually entered ATM has been filled. By adding this Indicator to the Chart, I can determine the Account and ATM Strategy selected at fill time.

                        As you can see, I'm getting a little confused with this language.

                        Unfortunately, the code is not firing when the position is Filled.

                        Below is a code segment is provided below...

                        OrderState orderState = OrderState.Unknown;
                        if(orderState.Equals(OrderState.Filled)) //This will print all filled orders on the account
                        // if (OrderState.Filled)
                        {
                        Print("For Account: " + xAlselector.SelectedAccount.ToString());
                        Print("For Strategy: " + xAtmStrategySelector.SelectedAtmStrategy.ToString( ));

                        Account MainAccount; //
                        MainAccount = Account.All.FirstOrDefault(a => a.Name == xAlselector.SelectedAccount.ToString());
                        // MainAccount.Orders order;

                        // foreach(Order order in MainAccount.Orders) //First foreach loop, to get working orders
                        // foreach(stopOrder == order) //First foreach loop, to get working orders
                        // {

                        if(stopOrder.OrderState == OrderState.Filled) //This will print all working orders on the account
                        Print("This order is working"+stopOrder.StopPrice.ToString());
                        // }

                        }
                        }


                        }


                        protected override void OnBarUpdate()
                        {
                        ChartControl.Dispatcher.InvokeAsync((Action)(() =>
                        {

                        xAlselector = Window.GetWindow(ChartControl.Parent).FindFirst("C hartTraderControlAccountSelector") as NinjaTrader.Gui.Tools.AccountSelector;
                        Print(xAlselector.SelectedAccount.ToString());

                        xAtmStrategySelector = Window.GetWindow(ChartControl.Parent).FindFirst("C hartTraderControlATMStrategySelector") as NinjaTrader.Gui.NinjaScript.AtmStrategy.AtmStrateg ySelector;
                        Print(xAtmStrategySelector.SelectedAtmStrategy.ToS tring());


                        }));
                        Last edited by ronhb107; 11-17-2017, 02:26 PM.

                        Comment


                          #13
                          Hello ronhb107,

                          Thanks for your reply.

                          Variables have their values assigned by using the "=" operator.

                          I've included a link to some educational programming material that is included with the NinjaTrader 7 help guide. Programming education escapes the scope of services that we can reasonably offer to our clients in the Support Department. If you are looking for more comprehensive educational materials on C# than what we have, I would suggest to browse the internet for free materials or to register for programming classes.

                          NT7 Programming Concepts - https://ninjatrader.com/support/help...g_concepts.htm

                          Compile Error Codes - https://ninjatrader.com/support/help...rror_codes.htm
                          (Additional C# compiler errors can be looked up through a Google search.)

                          We also have a large EcoSystem of NinjaScript Consultants that would be happy to write this or any other script for you at your request. If this is something that interests you, please let me know and I will have a member of our Business Development team reach out to you with additional information.
                          JimNinjaTrader Customer Service

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by JohnAymin, Today, 02:38 PM
                          0 responses
                          2 views
                          0 likes
                          Last Post JohnAymin  
                          Started by Rmos22, Today, 02:20 PM
                          0 responses
                          8 views
                          0 likes
                          Last Post Rmos22
                          by Rmos22
                           
                          Started by sawtooth500, Today, 12:59 PM
                          1 response
                          3 views
                          0 likes
                          Last Post NinjaTrader_Spencer  
                          Started by Steve L, Yesterday, 09:36 PM
                          1 response
                          19 views
                          0 likes
                          Last Post NinjaTrader_Kate  
                          Started by MichaelAlexander, 04-25-2017, 03:28 AM
                          9 responses
                          1,170 views
                          0 likes
                          Last Post dani neira  
                          Working...
                          X