• 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 Profit Target and Stop Loss

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

    Add Profit Target and Stop Loss

    Not sure if this is an Add-on or not.

    I want to manually place an OCO trade. Once a position is filled, I would like to automatically add both the Profit Target and the Stop Loss using NinjaScript.

    The size of the Stop Loss will differ from the initial size of the entry.

    Can this be done using NinjaScript, and can both the Profit Target and Stop Loss be seen on the charts without affecting the Initial Entry (as seen on the charts)?

    Thanks.

    #2
    Hello ronhb107,

    Yes, this would need to be an addon (or use the addon style in an indicator or NinjaScript strategy).

    A NinjaScript Strategy is not able to detect manually placed orders using the methods available to strategies.

    However, with the addon approach it is possible to use a timer that loops through the account looking for changes in the orders collection or changes with the position.

    You would then need to submit a stop market order and limit order with OCO through the addon approach.

    Below are a few publicly available links to the help guide, as well examples that demonstrates placing an order from an addon.
    https://ninjatrader.com/support/help...rs_account.htm
    https://ninjatrader.com/support/help...rderupdate.htm
    https://ninjatrader.com/support/help...ns_account.htm
    https://ninjatrader.com/support/help...tionupdate.htm
    https://ninjatrader.com/support/help...-us/submit.htm

    https://ninjatrader.com/support/foru...555#post515555
    http://ninjatrader.com/support/helpG...ript_Basic.zip
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Add Profit Target and Stop Loss

      Thanks for the response, Chelsea.

      A few questions. First, while the manual Order Entry was OCO, why would the Target and StopLoss orders need to be OCO as well once the Order Entry is filled? If I don't set them to OCO, what would be the impact (that the Target or StopLoss order remains persistent after the other is filled)?

      Just to add a wrinkle, if the StopLoss is fired (filled), I will need to add a Target for the StopLoss (which has a quantity larger than the Order Entry size). Can this be done when the StopLoss is fired, or is it better to have this entered once the Entry Order is filled (to avoid using a loop)?

      Second, if I set the Data Series -> Base period type to 'Ticks', can I then use the OnBarUpdate() method to call one of the Event handlers to deterimine if an Order has been 'Filled'?

      And third, I suspect that the best place to attach the AddOn would be the Chart Trader window. This would allow me to monitor changes to the Account. Having reviewed the Guide, I am not sure just how to attach the AddOn (especially since I do not want to add a button, etc.). Can you provide a simple approach?

      Btw, the reason I am pursuing this approach is that the ATM does not allow setting the StopLoss quantity (which, by default, is set to the size of the Order Entry quantity)
      Last edited by ronhb107; 01-16-2018, 12:44 PM.

      Comment


        #4
        Hello ronhb107,

        That is correct. If OCO is not used and the limit fills, the stop market will still be working and when it also fills, will place you in a position. (or vice versa)

        The orders can have any quantity you would like as these are submitted as is.

        An addon will not have data automatically added. You would need to do a BarsRequest and assign an event handler to the request Update event.

        Below is a publicly available link to the help guide.
        https://ninjatrader.com/support/help...us/request.htm

        To find when an order has filled you would need an event handler assigned to the account OrderUpdate event.

        Below is a publicly available link to the help guide.
        https://ninjatrader.com/support/help...rderupdate.htm


        As far as indicators that modify the chart I do have examples of these.
        https://ninjatrader.com/support/foru...327#post499327

        As well as an example addon in its own window.
        https://ninjatrader.com/support/foru...555#post515555
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Add Profit Target and Stop Loss

          Thanks, Chelsea.

          To make things simple, let's take this one step at a time.

          First, how do I add the AddOn to Chart Trader without requiring a button, tab, etc.?

          From there I can experiment with Print statements how the flow will work.

          Thanks.

          Comment


            #6
            Hello ronhb107,

            When you say "how do I add the AddOn to Chart Trader" what are you trying to add?
            If you are not adding buttons, what are you adding?

            Chart Trader is part of chart. This is built with WPF objects. You can add buttons to this, or modify it with WPF controls..

            Chart Trader creates buttons that allow you to place orders to an account (using the selected instrument). It also monitors that account and instrument to show any orders that have been placed to that account and instrument and displays these on the chart.

            Are you trying to monitor an account?

            If you are using an indicator, you can add the indicator to the chart and it will immediately become active and will immediately start performing the actions in the code.

            If you are using an Addon, you would need to activate the Addon with a button or with a menu item in the New menu. Once the Addon is activated, it will begin performing actions.
            If you want a separate Addon window to find a chart, attach to the chart, and start modifying the WPF elements, then you would need to dig through all of the windows and try and identify that chart.

            Below is a link to an example of looping through all windows, finding a chart, and getting the ChartControl.
            https://ninjatrader.com/support/foru...541#post445541
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Add Profit Target and Stop Loss

              Great. Then I should start with an Indicator (not an AddOn).

              The next step, then, is to track the Orders collection for a 'Filled' event.

              From there, to add to the Orders collection an OCO for the StopLoss and Target.

              When creating the StopLoss, I should enter a non-OCO order for the StopLoss target.

              I will let you know of my progress.

              Thanks, Chelsea.

              Comment


                #8
                Hello ronhb107,

                I'm not clear on what you are trying to add to Chart Trader..

                Are you actually trying to add WPF elements to Chart Trader on a chart?
                (Are you trying to visually modify Chart Trader)

                Or are you trying to capture manually placed traded to an account?
                (Are you trying to find when manual orders have been placed or filled to particular account?)
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Add Profit Target and Stop Loss

                  Chelsea, the latter. I am trying to simply manage a manually entered OCO order that has been filled; not trying to change Chart Trader.

                  I will need to track changes to the Account (from Sim101 to my active account) so that the submitted Target and StopLoss (and its target) are attached to the active (correct) Account.

                  Btw, I suspect the StopLoss Target will NOT be an OCO entry.

                  I will be making trades and changes to the Account throughout the trading day, so OnStateChange() methods will not suffice (correct me if I'm wrong).

                  Nothing very complex.
                  Last edited by ronhb107; 01-16-2018, 01:59 PM.

                  Comment


                    #10
                    Hello ronhb107,

                    In this case, you wouldn't be adding the addon to chart trader itself. Your addon, when running would hook into the account. When the position updates or orders are updated from the account, this would trigger methods within the addon.

                    This would universally be able to find orders placed from any chart or any superdom, similar to the way that chart trader also reflects account items from any chart or superdom connected to the same account and instrument.

                    (This could be an indicator, strategy, or an addon. With an indicator, you would be using the addon methods (which are available to all scripts), but the Instrument provided to the indicator would save the trouble of having to choose an instrument..)

                    It is possible to make addons that start running immediately when launching NinjaTrader (by triggering events in the OnWindowCreated() method) or they could start running when a menu item is clicked in the New menu. This wouldn't have to be added to a chart and would start monitoring immediately after launch.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Add Profit Target and Stop Loss

                      Ok, using an Indicator, below is the code (zipped) I have so far.

                      With everything commented out, I am able to get the Account data.

                      If I uncomment the State.DataLoaded (and State.Terminated), I do not get the Account data and there are some errors during runtime dealing with objects not initiated.

                      Btw, if I want (later) to create an AddOn, will this be connected to the Control Center? Just curious.
                      Attached Files
                      Last edited by ronhb107; 01-16-2018, 04:40 PM.

                      Comment


                        #12
                        Hello ronhb107,

                        I am showing that you have
                        private Account account = null;

                        However, I am not seeing in this script where the account variable is being assigned to an account.

                        In OnBarUpdate you get the account from the ChartTrader account selector of the chart the indicator is assigned to and you assign this to and print this without ever assigning the account object to a variable.

                        However, the account variable (lowercase) is never assigned and is always null.

                        Likely the error is a null error that you attempted to use an object that was not set to an instance of an object, because your variable is null.

                        Below is a publicly available link to the help guide that shows assigning an account object to a variable.
                        https://ninjatrader.com/support/help...ount_class.htm
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          Add Profit Target and Stop Loss

                          Thanks, Chelsea.

                          I have added the line for setting account in OnBarUpdate().

                          After uncommenting State.DataLoaded and State.Terminated, I find that the OnBarUpdate() is not being executed, so I am receiving the same error msg:
                          Indicator 'RBTestATMorders': Error on calling 'OnStateChange' method: Object reference not set to an instance of an object.

                          The question is: why is OnBarUpdate() not firing? I have Set up -> Calculate at 'On each tick'.

                          Also, changing the Account does not fire any Account methods: OnAccountStatusUpdate; OnAccountItemUpdate; OnAccountExecutionUpdate.

                          Nor does the filling of an order.

                          See attached code.
                          Attached Files
                          Last edited by ronhb107; 01-17-2018, 09:17 AM.

                          Comment


                            #14
                            Hello ronhb107,

                            In the file included with your last post you have not corrected the account (lowercase) variable being null issue I have described in post #12.

                            The account variable is never set to anything. It is empty. It is null. It does not contain an account as you have not assigned an Account object to the variable you have named account.

                            You cannot use a null variable.

                            Please set the account variable to an Account object.


                            In OnStateChange of your script in the State.Terminated there is no check for null.

                            Try changing:
                            Code:
                            account.AccountItemUpdate -= OnAccountItemUpdate;
                            account.ExecutionUpdate -= OnAccountExecutionUpdate;
                            account.OrderUpdate -= OnOrderUpdate;
                            To:
                            Code:
                            if (account != null)
                            {
                            	account.AccountItemUpdate -= OnAccountItemUpdate;
                            	account.ExecutionUpdate -= OnAccountExecutionUpdate;
                            	account.OrderUpdate -= OnOrderUpdate;
                            }
                            Last edited by NinjaTrader_ChelseaB; 01-17-2018, 11:18 AM.
                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              #15
                              Add Profit Target and Stop Loss

                              Actually, Chelsea, I did set the account variable to a value within the OnBarUpdate(). (See below).

                              However, I moved its location outside of the ChartControl.xxx within OnBarUpdate and I have implemented the check for a null value for account on both Terminate and DataLoaded.

                              Still, the error msg persists, and there is no OnBarUpdate() triggering.
                              ================================================== ===============

                              protected override void OnBarUpdate()
                              {
                              ChartControl.Dispatcher.InvokeAsync((Action)(() =>
                              {
                              accountSelector = Window.GetWindow(ChartControl.Parent).FindFirst("C hartTraderControlAccountSelector") as NinjaTrader.Gui.Tools.AccountSelector;
                              Print("SelectedAccount: " + accountSelector.SelectedAccount.ToString());

                              account = accountSelector.SelectedAccount;

                              // strategySelector = Window.GetWindow(ChartControl.Parent).FindFirst("C hartTraderControlATMStrategySelector") as NinjaTrader.Gui.NinjaScript.AtmStrategy.AtmStrateg ySelector;
                              // { Print("SelectedATMStrategy: " + strategySelector.SelectedAtmStrategy.ToString()); }
                              }));
                              Attached Files
                              Last edited by ronhb107; 01-17-2018, 12:19 PM.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by bubblegum, Today, 01:13 AM
                              1 response
                              4 views
                              0 likes
                              Last Post NinjaTrader_ChrisL  
                              Started by TraderYoda, Today, 03:33 AM
                              1 response
                              5 views
                              0 likes
                              Last Post NinjaTrader_EricB  
                              Started by fredfred, Today, 03:41 AM
                              4 responses
                              15 views
                              0 likes
                              Last Post fredfred  
                              Started by pjsmith, Today, 02:56 AM
                              0 responses
                              6 views
                              0 likes
                              Last Post pjsmith
                              by pjsmith
                               
                              Started by ntbone, Yesterday, 08:10 PM
                              0 responses
                              13 views
                              0 likes
                              Last Post ntbone
                              by ntbone
                               
                              Working...
                              X