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

Order handling from OnExecution in NT8

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

    Order handling from OnExecution in NT8

    Hello,

    I´m working for years in NT7 with handling setstoploss in onexecution. But now in NT8 I have issues because SetStopLoss is not moved to new price with fill of contract1. I have in the code (reading the reference samples before asking this question):

    protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
    {
    if (entryOrderS1a != null && entryOrderS1a == execution.Order) {
    if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
    {avgFillSa=execution.Order.AverageFillPrice;}
    }
    if(execution.Name=="Profit target" && execution.Order.FromEntrySignal=="S1a")
    {SetStopLoss("S1b", CalculationMode.Price, avgFillSa+2*TickSize, false);}}

    What I´m doing wrong in NT8 please?

    Thank you!
    Tony

    #2
    Hello tonynt,

    Thank you for the post.

    I don't see any debugging added to this, are you certain that your code is being executed?

    SetStopLoss can be called from OnExecutiion, I would suggest using Prints here to further identify the problem.


    Please let me know if I may be of additional assistance.
    Last edited by NinjaTrader_Jesse; 12-27-2018, 08:22 AM.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Hello,
      thank you for your reply. This is why I´m asking for help because it seems it does not. Do you see from this few lines whats wrong with it or missing?
      Can you please post one line what you mean with debugging here?

      Thank you!
      Tony

      Comment


        #4
        Hello tonynt,

        No, it would not be apparent what is wrong from what you provided at this point.

        I am unable to tell if the condition became true or not, I also cannot see if there are other issues surrounding the conditions or variables used as I cannot test this code in this state. You did not include any Print statements in the provided syntax to show how you have so far confirmed the condition is working or provided output to support your question. At this point, I could only suggest that you further explore the script to find the specific issue.

        Regarding debugging, we have talked about this previously in your other forum posts and emails about your NT7 scripts. This concept has not changed from NT7. Print statements and the output window are still used in NT8 and would be used for debugging your custom code. Here are the links to the NT8 resources for these topics:




        You can review this information to better understand the debugging tools you have available in the platform. Our support does not provide direct debugging services so this information is very important to learn if you will be doing custom coding. You will find that frequently you may need to use Prints or other tests to confirm what you are doing works as expected. This is a normal and part of the overall debugging process and most frequently Prints can be used to directly answer your questions.

        This question would be a good case to use Prints because you have a condition displayed and variables used. Prints can confirm everything is good to reach the SetStopLoss syntax. If your print confirms it reached that point but it did not submit an order, you could explore using TraceOrders or make a more simple sample to recreate the problem if it is not apparent why it fails. We can review a sample that you provide, but we would ask that it is a simple example that only demonstrates your question and also that we can test it. If we cannot test it, we would need to see how you tested it which generally require you to provide the syntax including the Print statements you used and output from your Print statements.

        Another debugging item you can look into using is TraceOrders. If you are certain your condition is becoming true and confirmed that it is and you are doing something which conflicts with the managed approach you would get a trace order error in the output window.




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

        Comment


          #5
          Should this header from the Reference Guide, Using OnOrderUpdate() and OnExecution() methods to submit protective orders, instead be this, Using OnOrderUpdate() and OnExecutionUpdate() methods to submit protective orders?
          eDanny
          NinjaTrader Ecosystem Vendor - Integrity Traders

          Comment


            #6
            Hello eDanny,

            In the NT8 help guide, yes the override name has changed. I will forward this to development to review this section.

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

            Comment


              #7
              Hello,

              I have added a print statement referring your reply and the sample in the helpguide and so I have in OnExecution now

              if (entryOrderS1a != null && entryOrderS1a == execution.Order)
              {if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
              {Print(execution.ToString()); avgFillSa=execution.Order.AverageFillPrice;}

              The entry is triggered correctly from conditons, but double "avgFillSa" is still zero and the print statement doesnt show anything in output window.
              What might be the reason please when I have in OnExecution the same logic as in the sample.

              Thank you!
              Tony

              Comment


                #8
                Hello tonynt,

                Thank you for your reply.

                I am a little confused about what you said in regard to the print statement. You noted:

                The entry is triggered correctly from conditons, but double "avgFillSa" is still zero and the print statement doesnt show anything in output window.
                I do not see that you printed avgFillSa to know it was 0, how do you know this was 0? Are you saying the execution shows its average fill price as 0? if so can you provide the output you saw?

                I see that you printed the execution object, but you said "and the print statement doesnt show anything in output window.". Do you mean that the print did not happen? If you don't see a print, that likely entails that this code was not reached. Otherwise, what was the specific output you saw? Seeing the code is only partially helpful, knowing the output from the variables used in the code or lack of output would be the most important detail which is not present here.



                I look forward to being of further assistance.



                JesseNinjaTrader Customer Service

                Comment


                  #9
                  Hello,

                  thank you for your reply. You are right, my explanation was not correct. Sorry. (I´m not a programmer and english is not my main-language, so 2 disadvantages caused this)
                  I added the print statement to a script because you told me to do so to debug. I do "usually" directly plotting on the chart, there I see immediately whats goin on and I do not miss any infomation that might happen because of sometimes many lines in output window. With my "plot statements" in chart I saw it. Attached is an exampel of my plottings.

                  But it seems that now it is plotting ok. I dont know what was the reason (what error of mine of course). Sorry for posting, thank you for your support. Maybe my sceenshot gives an hint to other traders to use it this way with the advantages for easier debugging.

                  Best regards
                  Tony
                  Attached Files
                  Last edited by tonynt; 02-12-2019, 10:39 AM.

                  Comment


                    #10
                    Hello tonynt,

                    Yes, the chart information would not be specifically helpful in this case because you are just outputting the value of a variable. The variable is 0, so the chart shows that.

                    The purpose of using Prints is to identify where the problem is in the code so you can fix it. If you are seeing a 0 on the chart for the drawing you are doing which is based on the value of a variable, you would need to go back through the code which sets that variable and find out why it is not being set.

                    If the following code is what you are using to set the variable

                    Code:
                    if (entryOrderS1a != null && entryOrderS1a == execution.Order)
                    {if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
                    {Print(execution.ToString()); avgFillSa=execution.Order.AverageFillPrice;}


                    some sample prints could be like the following:

                    Code:
                    Print("entryOrderS1a != null " + (entryOrderS1a != null));
                    Print("ntryOrderS1a == execution.Order " + (ntryOrderS1a == execution.Order));
                    
                    if (entryOrderS1a != null && entryOrderS1a == execution.Order)
                    {
                        Print("execution.Order:  " + execution.Order.ToString());
                        if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
                        {
                        avgFillSa=execution.Order.AverageFillPrice;}

                    The placement of the prints here is important, note how the print is not inside of the condition you are asking about. This is so that when the condition is run, no matter what the outcome is we know what the value was. If it does not become true, avgFillSa is not set which would likely mean its 0. Printing before this point will help you know if this part of your code is even being run, and if so should provide more details on why avgFillSa is 0.

                    You will need to do this anywhere you set avgFillSa to find where you are setting it to 0, or find where it is not being set to correct the problem.


                    I look forward to being of further assistnace.

                    JesseNinjaTrader Customer Service

                    Comment


                      #11
                      Hello,

                      what I found out now is that it works on live datafeed but it does not work with playback connection! Very interesting! Why?

                      And another question referring "realtime" please:
                      what is this " ... else if (State == State.Realtime)" for that I never saw in a NT7 script but I have here now from NT8 samples?

                      Thank you!
                      Tony

                      Comment


                        #12
                        Hello tonynt,

                        Thank you for your reply.

                        From the provided details I would not be certain, were you able to use prints to further identify what changes between the two use cases?

                        For the OnStateChange question, I would suggest reading about OnStateChange in the help guide as that will help clear up any questions surrounding this new state system. This is new to nt8, this is also documented in the code-breaking changes as replacing Initialize:





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

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by junkone, Today, 11:37 AM
                        2 responses
                        12 views
                        0 likes
                        Last Post junkone
                        by junkone
                         
                        Started by frankthearm, Yesterday, 09:08 AM
                        12 responses
                        43 views
                        0 likes
                        Last Post NinjaTrader_Clayton  
                        Started by quantismo, 04-17-2024, 05:13 PM
                        5 responses
                        35 views
                        0 likes
                        Last Post NinjaTrader_Gaby  
                        Started by proptrade13, Today, 11:06 AM
                        1 response
                        7 views
                        0 likes
                        Last Post NinjaTrader_Clayton  
                        Started by love2code2trade, 04-17-2024, 01:45 PM
                        4 responses
                        35 views
                        0 likes
                        Last Post love2code2trade  
                        Working...
                        X