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

alerts

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

    alerts

    Is there any way to set an alert for when a strategy makes a trade without setting it from within the strategy itself?
    I saw theres a way to make an alert directly on a chart based and you can make it based on an indicator but it looks very limited.
    So maybe theres a way to go around it by creating an indicator that sets off the alert for when a strategy makes a trade? thanks

    #2
    Hello ezrollin,

    Thanks for your post.

    You could create an indicator that listens to Account.OrderUpdate events. From there, you could check for Order Updates where e.OrderState is OrderState.Filled, and have the indicator call Alert() to fire an alert for a filled order.

    Alert - https://ninjatrader.com/support/help...html?alert.htm

    Account.OrderUpdate - https://ninjatrader.com/support/help...rderupdate.htm

    I have attached an example that can demonstrate subscribing to Account.OrderUpdate events in an indicator.

    You could also consider using Order.GetOwnerStrategy() to get the Strategy object that submitted the order. For example:

    Code:
    private void OnOrderUpdate(object sender, OrderEventArgs e)
    {
        if (e.OrderState == OrderState.Filled && e.Order.GetOwnerStrategy() != null)
            Print(e.Order.GetOwnerStrategy().Name + " filled");
    }
    We look forward to assisting.
    Attached Files
    Last edited by NinjaTrader_Jim; 01-31-2022, 07:57 AM.
    JimNinjaTrader Customer Service

    Comment


      #3
      1) could this be used to send a twitter/email/text not just an audio alert?
      2) how do i create a new demo account since this subscribes to all strategies under the sim101 account at once?
      3) cant I have this set up to automatically buy and sell when a strategy makes a buy/sell? thanks

      Comment


        #4
        To get it to listen to a sim101 account strategy and fire back the same order to my live account, would I have to do something like this:

        Code:
        [B][COLOR=#0000ff]lock[/COLOR] (Account.All)[/B]
        [B]           mySimAccount = Account.All.FirstOrDefault(a => a.Name == [COLOR=#a31515][FONT=Consolas]"Sim101"[/FONT][/COLOR]);[/B]
        [B][COLOR=#0000ff]lock[/COLOR] (Account.All)[/B]
        [B]           myLiveAccount = Account.All.FirstOrDefault(a => a.Name == [COLOR=#a31515][FONT=Consolas]"ezrollinLIVE"[/FONT][/COLOR]);[/B]
        
        
        [COLOR=#0000ff]privatevoid[/COLOR]OnPositionUpdate([COLOR=#0000ff]object[/COLOR]sender,PositionEventArgse)
        {
        if sim101 bought
        [COLOR=#0000ff]          [/COLOR]       I guess I need to do it like: [COLOR=#0000ff]     foreach[/COLOR](Orderorder[COLOR=#0000ff]in[/COLOR]myAccount.Orders)    or   [COLOR=#0000ff]foreach[/COLOR](Executionexecution[COLOR=#0000ff]in[/COLOR]myAccount.Executions)
                                                             or  [COLOR=#ffffff]         [/COLOR][COLOR=#0000ff]if[/COLOR](e.MarketPosition==MarketPosition.Long)  but maybe this would only check my live account?
        {
        BuyOrder=myLiveAccount.CreateOrder(myInstrument, OrderAction.Buy, OrderType.BuyLimit, OrderEntry.Automated, TimeInForce.Day, 1, double limitPrice, double stopPrice, string oco, "buy", Core.Globals.MaxDate, CustomOrder customOrder)
        myLiveAccount.Submit(new[] { BuyOrder});
        }
        if sim101 sold
        {
        sellOrder=myLiveAccount.CreateOrder(myInstrument, OrderAction.Sell, OrderType.SellLimit, OrderEntry.Automated, TimeInForce.Day, 1, double limitPrice, double stopPrice, string oco, "sell", Core.Globals.MaxDate, CustomOrder customOrder)
        myLiveAccount.Submit(new[] { sellOrder});
        
        }
        }
        I dont really know what I'm doing. I dont know if it'll actually copy all that over and use the same arguments.
        I guess to copy over arguments, you have to do like "e.MarketPosition or e.Position.Instrument.FullName" etcera in the createorder?
        I can start figuring stuff out by looking in the help docs BUT there arent many examples in anything! I wish there was more examples!!!!
        Please help me fix my code thanks

        Comment


          #5
          Hello ezrollin,

          1) could this be used to send a twitter/email/text not just an audio alert?
          Yes, Share() could be used to send a twitter message, text message or to use any other share service.

          Share() - https://ninjatrader.com/support/help...html?share.htm

          2) how do i create a new demo account since this subscribes to all strategies under the sim101 account at once?
          The indicator subscribes to events from the account you select in the Indicators dialog when you set up the indicator. You can select whichever account you want to listen to. If you want to make a new SIM account, that can be done in the Accounts tab of the Control Center (Right Click > Add Simulation Account)

          3) cant I have this set up to automatically buy and sell when a strategy makes a buy/sell? thanks
          Yes, the snippet I included in post #2 can be tested with Account's OrderUpdate event. The print will only be seen when an order fills and that order belongs to a strategy.

          That said, I would suggest getting more familiar with listening to the events for what you need before trying to submit orders at the account level.

          What is demonstrated here involves listening to the account's order/execution/position feeds so you can have an alert message or Share message triggered using code that you would not have to implement in each strategy.

          While it is technically possible to listen to the events on one account and use AddOn Framework code (Account.CreateOrder()/Account.Submit()) to create and submit orders, these order submissions would be made outside of the context of the strategy. Since the main purpose of a strategy is so it can manage orders/position on an account, it would be recommended to implement order submission behaviors within the strategy itself, rather than using AddOn Framework code and submitting orders when listening to Account level events.

          I suggest to first focus on getting the code here to trigger your Alert and Share messages. You can try the code below in the example I posted in post #2 to see when a strategy order fills on the account. Just be sure to use change the account in the indicator to the account that the strategies are attached to. Once you are able to see the print that is fired when a strategy order fills on the account, consider then firing your alert.

          If you would like to look into account level order submissions, I would suggest saving those for a new thread after you have the Alerts/Share messages down.

          Let me know if there is something else specific you are having issue with when getting the prints to be seen.
          JimNinjaTrader Customer Service

          Comment


            #6
            Ok, I'm trying to go by your recommendation. I have SOO many questions.

            Code:
            [COLOR=#ffffff]        [/COLOR][COLOR=#008000]// Subscribe to static events. Remember to unsubscribe with -= when you are done[/COLOR]
            [COLOR=#ffffff]        [/COLOR]Account.AccountStatusUpdate+=OnAccountStatusUpdate;
            What is a static event? How is this different than just a regular event?
            Wouldnt an event always be dynamic not static?

            Comment


              #7
              Hello ezrollin,

              It sounds like we are getting into C# specific questions.

              We are not programming educators, so we would really suggest using publicly available materials on C# to learn more about the language. As you get deeper involved with NinjaScript code, a deeper background of C# will become necessary.

              A publicly available resource on subscribing to events in C# can be found here - https://docs.microsoft.com/en-us/dot...be-from-events
              JimNinjaTrader Customer Service

              Comment


                #8
                Maybe you can only subscribe to 1 account at a time even though the compiler isnt giving an error for it when I'm subscribed separately to 2?
                Maybe I should be using the accountSelector? IDK theres not much documentation on any of it.

                Comment


                  #9
                  Hello ezrollin,

                  Order objects have a property describing what account they are submitted to, Order.Account.

                  Order properties - https://ninjatrader.com/support/help...html?order.htm

                  You would not change the Account property of an order to "set" it to a different account. You would need to call Account.CreateOrder() and Account.Submit() to submit the order to that account, where Account is the account object for where you want to send and send a new order.

                  I.E.

                  MyAccount.CreateOrder()
                  MyAccount.Submit()

                  There is no limit to the number of events that may be subscribed.

                  [TypeConverter(typeof(NinjaTrader.NinjaScript.Accou ntNameConverter))] could be used on a User Input string so that string is a friendly name to create an account object.

                  I.E.

                  Code:
                   [TypeConverter(typeof(NinjaTrader.NinjaScript.Accou ntNameConverter))]
                  public string AccountName { get; set; }
                  Give a default name in State.SetDefaults:

                  Code:
                  AccountName                                 = "Sim101";
                  Then create the account object in State.DataLoaded

                  Code:
                   // Find our account
                  lock (Account.All)
                      myAccount = Account.All.FirstOrDefault(a => a.Name == AccountName);
                  Then subscribe to events from the account object in State.DataLoaded

                  Code:
                   // Subscribe to account item and order updates
                  if (myAccount != null)
                  {
                      myAccount.OrderUpdate += MyOnOrderUpdate;
                      myAccount.ExecutionUpdate += MyOnExecutionUpdate;
                      myAccount.PositionUpdate += MYOnPositionUpdate;
                  }
                  Finally, make sure to unsubscribe in State.Terminated:
                  Code:
                  else if (State == State.Terminated)
                  {
                      if (myAccount != null)
                      {
                          myAccount.OrderUpdate -= MyOnOrderUpdate;
                          myAccount.ExecutionUpdate -= MyOnExecutionUpdate;
                          myAccount.PositionUpdate -= MyOnPositionUpdate;
                      }
                  }



                  JimNinjaTrader Customer Service

                  Comment


                    #10
                    I have been using all that code already based on the AccountEventIndicator.zip file you told me to use.
                    I just didnt include the code for the whole thing so it would be easy for you to read and figure out.

                    In layman's terms, you are basically just telling me to 1) subscribe to the sim101 account to listen to

                    but 2) send orders through:
                    MyAccount.CreateOrder()
                    MyAccount.Submit()
                    and the code I sent already has these 2, so it was partially right, it just doesnt need to be subscribed to the destination account? thanks!
                    Last edited by ezrollin; 02-03-2022, 10:26 PM.

                    Comment


                      #11
                      Hello ezrollin,

                      Yes, in layman's terms: you can subscribe to the Sim101 account's events and from these events, you can create/submit orders to the 2nd account. You do not need to subscribe to the 2nd account's events if you just want to submit an order to it.
                      JimNinjaTrader Customer Service

                      Comment


                        #12
                        Tried adding this:
                        It still wont send an order to the 2nd account


                        Code:
                        [B]OnOrderUpdate:[/B]
                        myEntryOrder = liveAccount.CreateOrder(Cbi.Instrument.GetInstrument("MES 03-22"), OrderAction.Buy, OrderType.Market,
                        TimeInForce.Day, 1, 0, 0, string.Empty, "Entry", null);
                        
                        NinjaTrader.NinjaScript.AtmStrategy.StartAtmStrategy("my new order", myEntryOrder);

                        Comment


                          #13
                          Hello ezrollin,

                          I notice that you are locking the Account.All collection to get the Sim account, but you are not locking Account.All when you get the live account. Account.All should be locked when searching with FirstOrDefault, which would be done to find an account by account name.

                          You are also not unsubscribing from the events in State.Terminated.

                          I do not think your OnOrderUpdate code would work and your order submission code would ever be reached. You are checking if an order object is not null, and that order object is never set to anything in the script. It would always be null. I would suggest checking e.Order.Name in OnOrderUpdate to identify the order.

                          I suggest the following as a checklist and to set up in a separate script:
                          1. Subscribe to Sim101 account events and make sure you can see prints for those events when adding a print in the OnOrderUpdate method subscribed to the event. Be sure to unsubscribe in State.Terminated
                          2. Then add the code to add the liveAccount Account object.
                          3. Then add code to OnOrderUpdate to check for a specific order name, and also that e.Order.OrderState == OrderState.Filled. Use a print in this condition, and make sure you see the print for it when the order fills
                          4. Then at this point, you will have the "listening" portion set. You would then just have to add code for liveAccount.CreateOrder() and liveAccount.Submit() to be able to send an order to the live account. Test submitting a market order first.
                          Do steps 1-4 before trying StartAtmStrategy. This will give you a clear path to make sure the necessary parts are working to listen for filled orders on the source account, and making sure a market order can be sent to the live account. Once these are squared away, changing usage to use StartAtmStrategy and start an Atm strategy on the second account will be easier, because the other parts needed will be set up correctly.


                          JimNinjaTrader Customer Service

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by GussJ, 03-04-2020, 03:11 PM
                          16 responses
                          3,279 views
                          0 likes
                          Last Post Leafcutter  
                          Started by WHICKED, Today, 12:45 PM
                          2 responses
                          19 views
                          0 likes
                          Last Post WHICKED
                          by WHICKED
                           
                          Started by Tim-c, Today, 02:10 PM
                          1 response
                          8 views
                          0 likes
                          Last Post NinjaTrader_ChelseaB  
                          Started by Taddypole, Today, 02:47 PM
                          0 responses
                          5 views
                          0 likes
                          Last Post Taddypole  
                          Started by chbruno, 04-24-2024, 04:10 PM
                          4 responses
                          51 views
                          0 likes
                          Last Post chbruno
                          by chbruno
                           
                          Working...
                          X