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

SampleATMStrategy

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

    SampleATMStrategy

    Hello,

    because of ongoing error-messages in log when trading with script strategy (when target or stop is hit then the oco-order causes the messages because one order is still in process ...) I´m thinking if it would be maybe better to use ATM to handle the stops and targets in a trade. After going over the SampleATMStragegy and before trying to modify my scripts to use ATM I have 2 questions please:

    a.) in the SampleATMStrategy there is in line 98 "you can change the stop price". Its clear to me what this means but I do not understand why this is in the script, from my understanding this will work from the ATM to use the stopmove as set in the ATM parameters. Or does the stopmove from ATM parameters not work when the ATM is called from a script strategy. If this does not work then there is the derived question what is the sense of calling an ATM from a script?
    b.) my error messages in my current "normal" script-strategies (with SetStopLoss and SetProfitTarget in script and not with an ATM) occur - from my understanding from replies of NT-support - when eg in fast markets the target2 is filled and same moment the stop2 should be moved to breakeven (error message because the position is closed and nothing there anymore to move). My question is now when I have the same settings in the ATM parameters of the ATM that is called by a script will then be the same error-messages as I have with a script-strategy with setstoploss and setprofittarget or is there a difference in handling of stop and target orders from a script calling an ATM (with stopmoves in ATM). Because if its the same then I dont need to modify my scripts by adding ATM.

    Thank you!
    Tony
    Last edited by tonynt; 03-09-2020, 05:21 PM. Reason: clearifying question

    #2
    Hello tonynt,



    A:
    The purpose of the ATM is for it to control the targets, however there is also a method that lets you change their values if you wanted to. You don't need to do that, it is just shown in the sample so you know it exists if you had a reason to need that. https://ninjatrader.com/support/help...angeStopTarget

    B:
    If you are doing a breakeven in your logic that could see an error if the target was closed and you tried to move it. The ATM uses OCO for the targets similar to your script is now so it could potentially run into this type of situation depending on the variables involved. In your script you can try to mitigate this as best you can by checking the state of the orders before doing any changes and using OnExecutionUpdate/OnOrderUpdate to monitor changes.

    Are you currently using any conditions to check order objects before doing the change or have you added any logic to try and prevent changing when the position was closed?

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

    Comment


      #3
      Hello Jesse,

      thank you for your reply.
      A: still not clear for me 100%, sorry. Let me ask different: it is not necessary when calling an ATM from a script to handle the stop from conditions in the script when this is in the ATM, eg when 10 ticks in profit then move stop to BE, this works from the ATM, no code necessary in the script to do so. yes?

      B: first I had the move of stop2 in onexecution immediately when target1 was filled, but because of error messages I have now stopmove in onbarupdate and checking a lot (but still error-messages)...
      else if (Positions[4].Quantity==1)
      {if(CurrentBars[3]>entrybarshort1b && avgFillSd1b>0 && GetCurrentBid(3)>=target2d1short+2*TickSize && GetCurrentAsk(3)<=avgFillSd1b-2*TickSize)
      {SetStopLoss("Sd1b", CalculationMode.Price, avgFillSd1b+1*TickSize, false);
      // entrybarshort I set the int from currentbars[3], avgFillSd1b (entryprice) and target2d1short I all set in onexecution with entry.
      // so I do only "allow" to move stop when there is a certain distance of current price from target and entryprice to avoid that stop is moved while target is hit with error-messages.

      I think from this snippets you see the intention. What is a better way to avoid the problem described?

      Thank you!
      Tony
      Last edited by tonynt; 03-10-2020, 10:55 AM. Reason: translation error

      Comment


        #4
        Hello tonynt,

        A, that script just shows you the options that are available when you use ATM's in NinjaScript. It is up to you if you want to use that method or not to change the target, you don't have to use that for the ATM to work as an ATM.

        B. Thanks for clarifying, I just wanted to get a better idea of what checking you were doing. The only item you may want to try and incorporate into that would be to use the actual order object and its current state. We have a sample that describes monitoring SetStopLoss orders, you could get the order from OnOrderUpdate and in your break even check that it is Working and Not Null. That may help in some cases if the order state is updated that may avoid the problem. This of course is not for ATM's as you cannot track the targets in the same way.


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

        Comment


          #5
          Hello,

          thank you for your reply and the sample. I have the sample already in my scripts but I do not understand this.
          I do understand from you reply one simply needs to check if its working and not null. I also have the SampleOnOrderUpdate, wouldnt it work somehow as in the sample?

          Thank you!
          Tony

          Comment


            #6
            Hello tonynt,


            In what you provided you are using Price information and Position information but not specifically the Order information. One area which may be able to help would be to additionally use the orders information such as the State to make sure its Working before trying to update it. If its not in the right state at that time a condition could filter that out and avoid trying to change it.

            I also have the SampleOnOrderUpdate, wouldnt it work somehow as in the sample?
            I am not certain I fully understand this question in how it was worded. Are you asking if you can apply the same concepts from the sample?

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

            Comment


              #7
              Hello Jesse,

              thank you for your reply. I did it this way because I had no idea how it could be done better. Therefore I was using the position.quantity (when it is less then the original tradequantity then this indicates first one is off already..) this was my "muddling through".

              First I had done it in onexecution with fill of target1, but causing issues - because not checking there. Then I thought to do in onbarupdate as posted and calculating the distance to avoid the problem. Your aproach makes more sense of course. Can you please give me an idea or snippet how to do it with checking the order information?

              Thank you!
              Tony

              Comment


                #8
                Hello tonynt,

                That sample has a List which makes it a little more complex to understand, really that sample is only good to see how to find a SetStopLoss order. That specific order has a name "Stop loss" that needs to be used to find it.

                The following sample is more aligned with what you are trying to do: https://ninjatrader.com/support/help...and_onexec.htm

                This sample actually has a BreakEven in it already so you could likely just use the sample overall as a starting point if you wanted to go with this type of logic. To check the status of the order that would look like the following:

                Code:
                if (stopOrder != null && stopOrder.OrderState == OrderState.Working)
                In the sample it is only checking for not null, you could add other conditions in like the state if needed.

                In general using the Order objects like this is less error prone because you are awaiting status updates before doing actions and making sure the orders were successful. The exception in this sample is the OnBarUpdate logic to break even, that is still going to relate to your OnBarUpdate calls but the order object is being used which will generally provide a more accurate picture of whats happening than just the price conditions/position.


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

                Comment


                  #9
                  Hello Jesse,

                  thank you for your reply. The SampleOnOrderUpdate is using Iorders/private Order.... and not SetStopLoss and SetProfitTarget. And this is not working OCO!

                  Best regards
                  Tony

                  Comment


                    #10
                    Hello tonynt,

                    Right, the second sample shows a more complete picture of using order objects in the logic and how to find/assign them. The first sample is a little more confusing on how you would actually use the found SetStopLoss orders, that is why I wanted to provide the second sample with some description.

                    The first sample would be good to understand how to find your SetStopLoss Order objects, the second sample would be how to make variables of the order objects and use the found order objects in some way. That sample demonstrates a breakeven which you could also do with the SetStopLoss order instead.

                    Keep in mind that our samples are not complete scripts, they only display specific concepts so we will generally link a few different samples that relate to what you are trying to do. In this case the two samples could be leveraged to understand how to find SetStopLoss orders and then use their Order object logically.

                    I look forward to being of further assistance.


                    JesseNinjaTrader Customer Service

                    Comment


                      #11
                      Hello Jesse,

                      thank you for your reply. First its difficult for me because I´m not a programmer, but OK I work hard to do what I need and after 10 years I could do almost everything I need, but now I have this final issues with the error-messages. Then its even more difficult for me to understand because english is not my first language. OK. And then you reply me with "first sample" and "other sample". lol. I have no idea which one you are referring to. This causes me ae headache. If I would know from your reply and I could assign this to a script then I would not have the thread as I would know how to do everything. I have to find from your reply which sample you would mean.

                      I know that your samples are not complete scripts! They helped me a lot, also the great support from you guys of NinjaTrader.

                      The sampleOnorderupdate I know, I used it untill I found out that this is not OCO by default. And I have no idea what this sample helps me for the setstoploss and setprofittarget to check.

                      Thank you!
                      Tony
                      Last edited by tonynt; 03-13-2020, 06:56 AM. Reason: add info for more accurate reply

                      Comment


                        #12
                        Hello tonynt,

                        If you need further clarification about something it is not problem just let me know.

                        The first sample would be the one I had provided or post #4 (SampleMonitorStopProfit_NT8.zip), the second sample would be the second or post #8 (SampleOnOrderUpdate_NT8.zip).

                        The SampleOnOrderUpdate_NT8.zip is to learn how to use orders in your logic. You can still use SetStopLoss instead of what that sample shows. What that sample is explaining is just how to use Order objects.

                        The SampleMonitorStopProfit_NT8 shows how to find SetStopLoss orders.

                        If you combine the two samples you can find a stop loss order and save it to an Order for later use.


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


                        JesseNinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by The_Sec, Yesterday, 03:37 PM
                        1 response
                        11 views
                        0 likes
                        Last Post NinjaTrader_Gaby  
                        Started by vecnopus, Today, 06:15 AM
                        0 responses
                        1 view
                        0 likes
                        Last Post vecnopus  
                        Started by Aviram Y, Today, 05:29 AM
                        0 responses
                        5 views
                        0 likes
                        Last Post Aviram Y  
                        Started by quantismo, 04-17-2024, 05:13 PM
                        3 responses
                        27 views
                        0 likes
                        Last Post NinjaTrader_Gaby  
                        Started by ScottWalsh, 04-16-2024, 04:29 PM
                        7 responses
                        36 views
                        0 likes
                        Last Post NinjaTrader_Gaby  
                        Working...
                        X