Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

SampleAddChartTraderButtons script

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

    SampleAddChartTraderButtons script

    I picked this SampleAddChartTraderButtons script from here. Could I please be guided (with examples) on how to make the script complete with the required order entry/exit script portion(s).

    Thanks for the anticipated assistance and guidance.

    Omololu

    #2
    Hello Omololu,

    In the button .Click event handlers you can call order methods.

    Below is a link to an example that demonstrates.
    https://ninjatraderecosystem.com/use...bar-buttons-2/
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChelseaB View Post
      Hello Omololu,

      In the button .Click event handlers you can call order methods.

      Below is a link to an example that demonstrates.
      https://ninjatraderecosystem.com/use...bar-buttons-2/
      Thanks for the link, Chelsea. Meanwhile, that example is a Strategy ... I want an Indicator. Please, which scripts in the link example are also valid for an indicator. I want to implement a tool similar to the various commercial "Chart Clicker" indicator tools available. I also want to implement ATM calls in the indicator.

      Regards.

      Omololu
      Last edited by omololu; 08-14-2020, 12:52 PM.

      Comment


        #4
        Hello omololu,

        You could use what that demonstrates from an indicator also, that sample would still apply here. The UserControlCollection is being used to add WPF elements. You can do that from both an indicator or a strategy.

        . I also want to implement ATM calls in the indicator.
        You would have to use the Addon methods to achieve that, you can research that topic in the following link: https://ninjatrader.com/support/help...nt8/add_on.htm

        Please let me know if I may be of further assistance.

        JesseNinjaTrader Customer Service

        Comment


          #5
          Hi Jesse,

          I want to restrict myself to NOT implementing the ATM now.The first challenge I have is that the EnterLong() and EnterShort() as used in the NinjaTraderEcosystem Strategy example, do not work in my indicator. Are these commands for Strategies ONLY ? In any case, I suspect that I can use the CreateOrder command, so I used it as in the example below, but I'm having error CS0103 "The name myAccount does not exit in the current context"

          Order stopOrder;
          stopOrder = myAccount.CreateOrder(myInstrument, OrderAction.Sell, OrderType.StopMarket, OrderEntry.Automated, TimeInForce.Day, 1, 0, 1400, "myOCO", "stopOrder", Core.Globals.MaxDate, null);

          myAccount.Submit(new[] { stopOrder });


          Please, I shall appreciate your assistance once again. How do I define myAccount (e. g. Sim101).

          Regards.

          Omololu

          Comment


            #6
            Hello omololu,

            Yes those methods are only for strategies, you need to use the Addon section of the help guide if you want to work on Orders or other account related tasks from an indicator.

            The myAccount problem is that the sample you are looking at assumes that you first reviewed the Account main sample here (the whole Account sub section makes this assumption): https://ninjatrader.com/support/help...ount_class.htm

            You need to find the account and make it into a variable before you can use it, the main Account sample demonstrates that concept.

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

            Comment


              #7
              Originally posted by NinjaTrader_Jesse View Post
              Hello omololu,

              Yes those methods are only for strategies, you need to use the Addon section of the help guide if you want to work on Orders or other account related tasks from an indicator.

              The myAccount problem is that the sample you are looking at assumes that you first reviewed the Account main sample here (the whole Account sub section makes this assumption): https://ninjatrader.com/support/help...ount_class.htm

              You need to find the account and make it into a variable before you can use it, the main Account sample demonstrates that concept.

              I look forward to being of further assistance.
              Hi Jesse,

              I'm back at this my "SampleAddChartTraderButton" project.

              Following your tips and guide, I picked the scripts below from the Guide link on Account;

              private Account myAccount;

              protected override void OnStateChange()
              {
              if (State == State.SetDefaults)
              {
              // Find our Sim101 account
              lock (Account.All)
              myAccount = Account.All.FirstOrDefault(a => a.Na me == "Sim101");

              // Subscribe to static events. Remember to unsubscribe with -= when you are done
              Account.AccountStatusUpdate += OnAccountStatusUp date;

              if (myAccount != null)
              {
              // Print some information about our account using the AccountItem indexer
              Print(string.Format("Account Name: {0} Connection Name: {1} Cash Value {2}",
              myAccount.Name,
              myAccount.Connection.Options.Name,
              myAccount.Get(AccountItem.CashValue, Currency.Us Dollar)
              ));

              // Print the prices of the executions on our account
              lock (myAccount.Executions)
              foreach (Execution execution in myAccount.Execut ions)
              Print("Price: " + execution.Price);

              // Subscribe to events. Remember to unsubscribe with -= when you are done
              myAccount.AccountItemUpdate += OnAccountItemUpda te;
              myAccount.ExecutionUpdate += OnExecutionUpdate;
              }
              }
              else if (State == State.Terminated)
              {
              // Unsubscribe to events
              myAccount.AccountItemUpdate -= OnAccountItemUpdate;
              myAccount.ExecutionUpdate -= OnExecutionUpdate;

              Account.AccountStatusUpdate -= OnAccountStatusUpdate;
              }
              }

              private void OnAccountStatusUpdate(object sender, AccountStatusEventArgs e)
              {
              // Do something with the account status update
              }

              private void OnAccountItemUpdate(object sender, Ac countItemEventArgs e)
              {
              // Do something with the account item update
              }

              private void OnExecutionUpdate(object sender, Exec utionEventArgs e)
              {
              // Do something with the execution update
              }



              I also picked the scripts below on NTTPage also from the Guide link on Add-On;


              /* Example of subscribing/unsubscribing to market data from an Add On. The concept can be carried over

              to any NinjaScript object you may be working on. */

              public class MyAddOnTab : NTTabPage

              {

              private AccountSelector accountSelector



              public MyAddOnTab()

              {

              // Note: pageContent (not demonstrated in this example) is the page content of the XAML

              // Find account selector

              accountSelector = LogicalTreeHelper.FindLogicalNode(pageContent, "accountSelector") as AccountSelector;



              // When the account selector's selection changes, unsubscribe and resubscribe

              accountSelector.SelectionChanged += (o, args) =>

              {

              if (accountSelector.SelectedAccount != null)

              {

              // Unsubscribe to any prior account subscriptions

              accountSelector.SelectedAccount.AccountItemUpdate -= OnAccountItemUpdate;

              accountSelector.SelectedAccount.ExecutionUpdate -= OnExecutionUpdate;

              accountSelector.SelectedAccount.OrderUpdate -= OnOrderUpdate;

              accountSelector.SelectedAccount.PositionUpdate -= OnPositionUpdate;



              // Subscribe to new account subscriptions

              accountSelector.SelectedAccount.AccountItemUpdate += OnAccountItemUpdate;

              accountSelector.SelectedAccount.ExecutionUpdate += OnExecutionUpdate;

              accountSelector.SelectedAccount.OrderUpdate += OnOrderUpdate;

              accountSelector.SelectedAccount.PositionUpdate += OnPositionUpdate;

              }

              };

              }



              // Called by TabControl when tab is being removed or window is closed

              public override void Cleanup()

              {

              // Clean up our resources

              base.Cleanup();

              }







              // Other required NTTabPage members left out for demonstration purposes. Be sure to add them in your own code.

              }



              My challenge now is that I'm lost on where to place the above scripts inside the "SampleAddChartTraderButton" indicator. I shall appreciates your guide and tips once again.

              Regards.

              Omololu

              Comment


                #8
                Hello omololu,

                The two samples you have linked are from two different sections of the addon helpguide. These are not at all the same concept, they only are alike in the sense that they both pick an account.

                The first sample is how you can access an account through code while the second sample demonstrates creating a GUI in XAML that has an account selector.

                The link I provided in post #6 would be what you need from a indicator, you are not creating a whole user interface or addon with XAML so the second sample is not relevant here. You just are creating a WPF control and appending it to an existing control with the button sample.

                If the intention is to do something with the account on the button press you could do that with the standard account sample from post #6. Keep in mind if you are trying to use any NinjaScript code like Close[0] from a button event you need a TriggerCustomEvent surrounding your code: https://ninjatrader.com/support/help...ghtsub=trigger

                Please let me know if I may be of further assistance.

                JesseNinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_Jesse View Post
                  Hello omololu,

                  The two samples you have linked are from two different sections of the addon helpguide. These are not at all the same concept, they only are alike in the sense that they both pick an account.

                  The first sample is how you can access an account through code while the second sample demonstrates creating a GUI in XAML that has an account selector.

                  The link I provided in post #6 would be what you need from a indicator, you are not creating a whole user interface or addon with XAML so the second sample is not relevant here. You just are creating a WPF control and appending it to an existing control with the button sample.

                  If the intention is to do something with the account on the button press you could do that with the standard account sample from post #6. Keep in mind if you are trying to use any NinjaScript code like Close[0] from a button event you need a TriggerCustomEvent surrounding your code: https://ninjatrader.com/support/help...ghtsub=trigger

                  Please let me know if I may be of further assistance.
                  Hi Jesse,

                  Thanks for your reply.

                  I seem to have the Account portion sorted out now (at least, I don't have compilation errors), but I'm stuck on where to locate my "buttons click events/actions and CreateOrder()/CancelOrder()/SubmitOrder() actions" ... that is, where to locate the scripts responsible for "buttons clicks" and their associated events/actions. Please confirm if and where these "buttons clicks" events/actions scripts should be located within the different scripts portions below, you may wish to give me an example of the "Long or Short" buttons click events/actions, for example. In the case that the below scripts portions are not the correct place to locate my "buttons clicks" events/actions, please guide me to an example of a typical location for the "buttons clicks" events/actions.


                  private void OnAccountStatusUpdate(object sender, AccountStatusEventArgs e)
                  {
                  // Do something with the account status update
                  }

                  private void OnAccountItemUpdate(object sender, AccountItemEventArgs e)
                  {
                  // Do something with the account item update
                  }

                  private void OnExecutionUpdate(object sender, ExecutionEventArgs e)
                  {
                  // Do something with the execution update
                  }

                  private void SampleButton_Click(object sender, RoutedEventArgs e)
                  {
                  Print("Sample Button Clicked");
                  }

                  protected override void OnBarUpdate()
                  {
                  //Add your custom indicator logic here.
                  }



                  Regards.

                  Omololu

                  Comment


                    #10
                    Hello omololu,

                    that is, where to locate the scripts responsible for "buttons clicks" and their associated events/actions.
                    I am not certain what you mean by scripts here, this code would go in the file you are editing.

                    This would go inside the class, the OnBarUpdate method you provided in your latest sample is a good example of where this code goes. You can look in any other file that uses OnBarUpdate to see how OnBarUpdate was placed in that file to replicate that in your file. If the other methods you have shown from the sample are around OnBarUpdate then you would use it as you have shown it in your script.

                    The button click events and also the account events have been provided the past replies. In post #7 you are using the account events and show their placement, the help guide also has a full sample that shows the placement. The button click event is also shown in the sample linked in post #2, the sample you are using SampleAddChartTraderButtons also demonstrates that placement.

                    if you view the samples and where the button method is placed in the file you would do the same thing in your code.



                    Please let me know if I may be of further assistance.
                    JesseNinjaTrader Customer Service

                    Comment


                      #11
                      Hi Jesse,

                      I thank you for your prompt response.

                      The sample linked in post #2 is a Strategy and not an Indicator. The SampleAddChartTraderButtons in my post #1 is an indicator and relevant to my case.

                      The SampleAddChartTraderButtons script example has the below portions of its scripts/codes that contain what I believe you referred to in your response, and which is very "scanty". Could you please guide me to "buttons click events/actions and CreateOrder()/CancelOrder()/SubmitOrder() actions" code examples that I can insert within each of the below example.


                      private void SampleButton_Click(object sender, RoutedEventArgs e)
                      {
                      Print("Sample Button Clicked");
                      }

                      protected override void OnBarUpdate()
                      {
                      //Add your custom indicator logic here.
                      }



                      Also, the sample linked in post #2 (which is a Strategy) has the below portions of its scripts/codes that contain another reference of yours, and which is a good example. I suppose that I can get a little guidance from this example but with the insight that this is a Strategy case. Right ?


                      protected override void OnBarUpdate()
                      {
                      if (longButtonClicked
                      && Close[1] >= MAX(High, 2)[2] + 0 * TickSize
                      && High[1] < CurrentDayOHL().CurrentHigh[1]
                      && High[0] > High[1])
                      EnterLong();

                      if (shortButtonClicked
                      && Low[1] > CurrentDayOHL().CurrentLow[1]
                      && Low[0] < Low[1]
                      && Close[1] < MIN(Low, 2)[2] - 0 * TickSize)
                      EnterShort();

                      if (!longButtonClicked
                      && Position.MarketPosition == MarketPosition.Long)
                      ExitLong();

                      if (!shortButtonClicked
                      && Position.MarketPosition == MarketPosition.Short)
                      ExitShort();
                      }

                      private void OnButtonClick(object sender, RoutedEventArgs rea)
                      {
                      System.Windows.Controls.Button button = sender as System.Windows.Controls.Button;
                      if (button == longButton && button.Name == "LongButton" && button.Content == "LONG")
                      {
                      button.Content = "Exit L";
                      button.Name = "ExitLongButton";
                      longButtonClicked = true;
                      return;
                      }

                      if (button == shortButton && button.Name == "ShortButton" && button.Content == "SHORT")
                      {
                      button.Content = "Exit S";
                      button.Name = "ExitShortButton";
                      shortButtonClicked = true;
                      return;
                      }

                      if (button == longButton && button.Name == "ExitLongButton" && button.Content == "Exit L")
                      {
                      button.Content = "LONG";
                      button.Name = "LongButton";
                      longButtonClicked = false;
                      return;
                      }

                      if (button == shortButton && button.Name == "ExitShortButton" && button.Content == "Exit S")
                      {
                      button.Content = "SHORT";
                      button.Name = "ShortButton";
                      shortButtonClicked = false;
                      return;
                      }
                      }
                      }


                      Meanwhile, I'm still not clear on the relevance of the below scripts/codes (also posted in my post #9) in this Indicator project of mine.


                      private void OnAccountStatusUpdate(object sender, AccountStatusEventArgs e)
                      {
                      // Do something with the account status update
                      }

                      private void OnAccountItemUpdate(object sender, AccountItemEventArgs e)
                      {
                      // Do something with the account item update
                      }

                      private void OnExecutionUpdate(object sender, ExecutionEventArgs e)
                      {
                      // Do something with the execution update
                      }




                      Regards.

                      Omololu

                      Comment


                        #12
                        Hello omololu

                        The sample linked in post #2 is a Strategy and not an Indicator. The SampleAddChartTraderButtons in my post #1 is an indicator and relevant to my case.
                        This really does not matter here, both are going to be structured identically and we are not dealing with a specific NinjaScript concept with the button. The help guide page you linked to is also not specific to Indicators, that is in the Common section for charts which a variety of types can use which include strategies: https://ninjatrader.com/support/help...collection.htm

                        The SampleAddChartTraderButtons script example has the below portions of its scripts/codes that contain what I believe you referred to in your response, and which is very "scanty". Could you please guide me to "buttons click events/actions and CreateOrder()/CancelOrder()/SubmitOrder() actions" code examples that I can insert within each of the below example.
                        That sample specifically uses OnBarUpdate because it is a strategy and uses the managed approach to submit orders, you provided the code for that in your post. Submitting orders from an indicator would be done using the addon account methods. https://ninjatrader.com/support/help...ount_class.htm

                        In both samples the button is subscribed to and something happens in the buttons event handler, the strategy uses that to control orders and the indicator uses that to simply print the buttons name. If you wanted to submit an order you could do that from the buttons event assuming you have an account object like the account example above, and then use the account.Submit() method to submit an order. https://ninjatrader.com/support/help...nt8/submit.htm




                        Please let me know if I may be of further assistance.
                        JesseNinjaTrader Customer Service

                        Comment


                          #13
                          Originally posted by NinjaTrader_Jesse View Post
                          Hello omololu


                          1. That sample specifically uses OnBarUpdate because it is a strategy ...

                          2. ... and then use the account.Submit() method to submit an order.
                          Hi Jesse,

                          1. No, no, no ... that was not a strategy, it is an indicator.

                          2. Please, where do I insert the following example ... and other such codes/scripts ?

                          Order stopOrder = null;

                          stopOrder = myAccount.CreateOrder(myInstrument, OrderAction.Sell, OrderType.StopMarket, TimeInForce.Day, 1, 0, 1400, "myOCO", "stopOrder", null);

                          myAccount.Submit(new[] { stopOrder });


                          Regards and thanks.

                          Omololu


                          Comment


                            #14
                            Hello omololu,

                            It seems that having multiple samples is causing some confusion here. I would suggest to just go with whichever sample makes more sense to you to learn the button click event concept from it. The button part is the same in both samples, you can ignore the other parts of each sample like how it submits orders or changes the button text. Those are just actions the buttons accomplish in those samples.

                            The code you provided would go where you want it to be executed. If you want that to be called for the button event place it within your buttons click event method. If you are trying to trigger the order based on OnBarUpdate you could look at how the strategy submits orders from OnBarUpdate and replicate that but submitting the order with the syntax you provided instead.

                            If you are not certain where to place this code I would suggest to just place where you believe it should go and compile. If it doesn't work it will likely give you an error so you can understand that is not the right location. You can also undo changes you make so its easy to test placement of code, this is part of the script creation process and learning C#. If you cant figure it out post what you tried and we can go from there.


                            Please let me know if I may be of further assistance.

                            JesseNinjaTrader Customer Service

                            Comment


                              #15
                              Originally posted by NinjaTrader_Jesse View Post
                              Hello omololu,

                              It seems that having multiple samples is causing some confusion here. I would suggest to just go with whichever sample makes more sense to you to learn the button click event concept from it. The button part is the same in both samples, you can ignore the other parts of each sample like how it submits orders or changes the button text. Those are just actions the buttons accomplish in those samples.

                              The code you provided would go where you want it to be executed. If you want that to be called for the button event place it within your buttons click event method. If you are trying to trigger the order based on OnBarUpdate you could look at how the strategy submits orders from OnBarUpdate and replicate that but submitting the order with the syntax you provided instead.

                              If you are not certain where to place this code I would suggest to just place where you believe it should go and compile. If it doesn't work it will likely give you an error so you can understand that is not the right location. You can also undo changes you make so its easy to test placement of code, this is part of the script creation process and learning C#. If you cant figure it out post what you tried and we can go from there.


                              Please let me know if I may be of further assistance.
                              Hi Jesse,

                              I'm focusing on using Indicator instead of Strategy and I can differentiate between the codes relevant to each of these two approaches. In any case, I'll proceed as you have advised.

                              Regards.

                              Omololu

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by NeverDownMoney, Today, 02:32 PM
                              1 response
                              6 views
                              0 likes
                              Last Post NinjaTrader_Tyler  
                              Started by ErikY, Today, 02:14 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post ErikY
                              by ErikY
                               
                              Started by redart1021, Today, 01:14 PM
                              2 responses
                              10 views
                              0 likes
                              Last Post redart1021  
                              Started by auto112112, Today, 12:36 PM
                              3 responses
                              12 views
                              0 likes
                              Last Post NinjaTrader_PatrickG  
                              Started by UltraNIX, Today, 11:03 AM
                              1 response
                              12 views
                              0 likes
                              Last Post NinjaTrader_ChrisL  
                              Working...
                              X