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

Triggering orders to be submitted by addon via indicator

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

    Triggering orders to be submitted by addon via indicator

    Ok, im in the home stretch of completing my auto trader, this is my first ninja script and i haven't coded in 10+ years i relearned as i went 2k+ lines later its submitting orders through the indicator class and working like a charm, but now im stuck at adding the last bit of functionality, i have an addon where i can access the static members that give values to the chart trader selected options like qty account instrument atm, but i just realized i can only submit atm orders through the addon, can someone guide me as to how i can send something to the addon to submit the order im a bit lost this is what im using now to trigger orders, is there a way to send a trigger to have the addon place orders in place of the where i have them now
    Code:
                                    if(marketPosition != 2)
                                    {
                                        Print("short");
                                        Order mar****rder = account.CreateOrder(Instrument, OrderAction.Sell, OrderType.Market, OrderEntry.Automated,
                                        TimeInForce.Day, 1, 0, 0, string.Empty, "Bearish Diamond Short Confirmed", new DateTime(), null);
                                        account.Submit(new Order[] { mar****rder });
                                    }
                                    else if(marketPosition == 1)
                                    {
                                        Print("Sell to cover");
                                        Order mar****rder = account.CreateOrder(Instrument, OrderAction.SellShort, OrderType.Market, OrderEntry.Automated,
                                        TimeInForce.Day, 1, 0, 0, string.Empty, "Bearish Diamond Short Confirmed SellShort", new DateTime(), null);
                                        account.Submit(new Order[] { mar****rder });
                                    }
                                    else
                                    {
                                        if (!semiButtonToggled)
                                        {
                                            Dispatcher.InvokeAsync(() =>
                                            {
                                                semiButton.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));                                            
                                                semiButtonToggled = true;
                                            });
                                        }
                                    }​

    Click image for larger version  Name:	image.png Views:	0 Size:	69.1 KB ID:	1232996


    #2
    Hello balltrader,

    To submit an ATM using the addon framework you can see the following link:



    Your addon or indicator can make use of the account class and its methods like StartATMStrategy.

    JesseNinjaTrader Customer Service

    Comment


      #3
      Thank you for your response but ive seen that reference already, my question is more in line of how can i communicate to the addon class that the conditions were met in the indicator and it should execute that example

      Comment


        #4
        Hello Kylemcfar57x,

        There is no specific means for scripts to communicate like that. Either the addon would need to monitor for conditions to be true and then submit the order or the indicator would need to monitor and submit the order.

        Addons are not intended to serve as a way to communicate between scripts but instead are a way to interact with windows in the platform or design your own custom tools. The underlying addon framework can be used by any type to implement account level access and have the ability to submit orders like other tools in the platform. An indicator for example can implement addon framework account class to submit orders.




        JesseNinjaTrader Customer Service

        Comment


          #5
          so is there any way to submit an order utilizing an atm through the indicator im able to submit regular market orders but when i tried it with the atm it never placed an order

          Comment


            #6
            Hello Kylemcfar57x,

            Yes it would be the same way that you would do it from an addon, you can use StartAtmStrategy. https://ninjatrader.com/support/help...tmstrategy.htm
            JesseNinjaTrader Customer Service

            Comment


              #7
              I'll try playing around with it maybe i was entering the parameters wrong, it will still work with sim or market replay right?

              Comment


                #8
                Hello Kylemcfar57x,

                In what you provided in post 1 you are submitting orders to an account using account.Submit. Instead of that you would create the order like shown in that help guide sample and use account.CreateOrder and StartAtmStrategy. Starting an ATM order vs a regular order are two different methods in the addon framework.
                JesseNinjaTrader Customer Service

                Comment


                  #9
                  Ok it worked thankyou, i missed where string must say Entry
                  Last edited by Kylemcfar57x; 01-27-2023, 06:43 PM.

                  Comment


                    #10
                    ok new can of worms opened, the leftover brackets from the atm are not closed when position is reversed, i seen a post about get atm id and cancel that atm but it seems like thats strat only, or am i mistaken, i dont want to use flatten can i cancel the oco leftover from the atm before a new entry?Click image for larger version

Name:	image.png
Views:	385
Size:	90.0 KB
ID:	1233061

                    Comment


                      #11
                      Well i found a way to cancel the atms on reverse orders it works good, but im getting this exception, i didn't want to use flatten bc it closes all orders but i seen that you can add individual instruments to the list, i could use the click event in instrument to also add the current one to the list but since its been so long since ive coded im not sure how to remove it then seems like it could get janky, if im trading 2 instruments it could cancel the others, unless i could create a unique list for each instance of the indicator so its not shared idk...

                      Click image for larger version

Name:	image.png
Views:	362
Size:	29.7 KB
ID:	1233063

                      Comment


                        #12
                        You can ignore my previous responses i solved it everything is working great now and no exceptions, i tied the flatten method to gather the account id and put it in a list to be flattend, i was also able to eliminate the addon i used to pull the chart trader info by accessing the info through the thread dispatcher

                        Comment


                          #13
                          well i may have been a bit too optimistic, i finished on Saturday so the market was closed, long story short it works flawlessly on market reply, but live i get an occasional double entry on reversal, so i figured it was flipping too fast before the ui thread returned that the pos was not long yet so it entered again, i tried to use await but every time i try pass orderers through the thread dispatcher it just spams orders instead of doing one even without the dispatcher just an async method with await does the same thing, even tried executing the code to simulate clicks on chart trader for entry same result not sure what im messing up i was never good with threading, commented stuff is what i tried before, it all executes but i think its not letting market position get to the argument, thats also passed through a thread, the instrument to flatten data is also passed through a thread but held in a list so i dont think its that, it runs as expected in market replay in this form but i cant get it to work using await Click image for larger version  Name:	image.png Views:	0 Size:	23.2 KB ID:	1233222
                          Last edited by Kylemcfar57x; 01-30-2023, 01:58 AM.

                          Comment


                            #14
                            Hello Kylemcfar57x,

                            From the image it looks like you have problematic logic. You cannot check a position like that directly after calling an method such as Flatten, you would have to wait for the position update event which means the positions were flattened before running that market Position check code. That code likely needs to be removed from the BuyOrder method and placed into its own method being called from OnOrderUpdate/OnPositionUpdate/OnBarUpdate at a separate time.

                            When working with normal NinjaScript strategies you have to wait at a minimum of 1 bar event before doing a position check so the position is updated. When working directly with the account you instead have to wait for the actual account events for whatever you are trying to do. For example submitting targets for an entry would be done in OnExecutionUpdate based on the entries filled event. If you are trying to reverse a position you would need to cancel any open orders and wait for confirmation they were cancelled, clean up those orders variables and then close the position and wait for its confirmation. Once the orders are canceled and the position is flat you can reset any associated logic before doing the entry for the reversal.

                            You never need async/dispatching for NinjaScript based code. Using async and dispatchers is specific for UI related logic or cross threaded logic, your script wont run into that just by submitting orders and trying to do logic based on the position. You just need to re structure your code to listen to the account events to drive your logic. Adding dispatching or async for your logic or order events will take them out of the scripts context and do those changes at a random later time.

                            I also see using await/Tasks and Delay there, you never want to use tasks or any kind of delay/waiting/while looping in a script to avoid locking it up. Pausing your codes execution in NinjaScript is not good for performance, you should always let the script execute its actions as fast as possible to avoid UI freezing.








                            JesseNinjaTrader Customer Service

                            Comment


                              #15
                              those are only available through strategy class right? i used all these janky workarounds to make it work as an indicator class, i might be able to keep it as an indicator, if i can access these events as an indicator. can you have shared methods between strat and indicator? i would need to do some major restructuring

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Lumbeezl, 01-11-2022, 06:50 PM
                              31 responses
                              816 views
                              1 like
                              Last Post NinjaTrader_Adrian  
                              Started by xiinteractive, 04-09-2024, 08:08 AM
                              5 responses
                              14 views
                              0 likes
                              Last Post NinjaTrader_Erick  
                              Started by swestendorf, Today, 11:14 AM
                              2 responses
                              6 views
                              0 likes
                              Last Post NinjaTrader_Kimberly  
                              Started by Mupulen, Today, 11:26 AM
                              0 responses
                              6 views
                              0 likes
                              Last Post Mupulen
                              by Mupulen
                               
                              Started by Sparkyboy, Today, 10:57 AM
                              1 response
                              6 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Working...
                              X