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

Add Profit Target and Stop Loss

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

    #61
    Add Profit Target and Stop Loss

    Jesse:

    First, I have certainly learned a lot about how NT8 works (and certainly have more to learn). So, thanks for the help.

    Second, I have attached the cleaned up version and moved my control to OnAccountExecutionUpdate() from OnOrderUpdate() (based on further reading of the Guide).

    Third, I've modified the code to accommodate changes in the Account Selection (during Entry working and filled orders).

    And fourth, a curious observation while testing using Playback. In the NinjaScript Output window, execution events seem to be revisited twice (this does not occur for Sim101 testing). While this does not affect proper operation of the Strategy, I am curious as to why this is occurring. Any thoughts?

    One last point. Removing Dispatcher.InvokeAsync() resulted in double Entry orders (the 2nd with a blank name); leaving it in seems to work better. Given the info in the Guide, I would have expected no change when removed (perhaps the double executions in Playback is playing a role here).

    Thanks,
    Ron
    Attached Files
    Last edited by ronhb107; 02-02-2018, 02:32 PM.

    Comment


      #62
      Hello,

      Thank you for the post.

      Regarding the double events, are you certain you are unsubscribing from all events before changing to the Playback account? Are you also certain that the subscribe is only occurring once? This sounds like a subscription had occurred twice, without testing in that specific situation I would not be certain. you can likely add prints to your logic to find if this is happening.

      Regarding the dispatcher, I still wouldn't advice using this here, I would suggest that you read about the intended use of using InvokeAsync: https://ninjatrader.com/support/help...sub=dispatcher

      Dispatching should only be used for its intended use of multithreaded access in NinjaTrader. if you are seeing double entries as the result of removing this, I would suggest you debug that situation further while not using the dispatcher. Again the sample you provided previously has nothing to do with how you are using the dispatcher now, in that sample it was required because UI elements on a different thread were being accessed where in your current use it would be not advised to be used. If you are not accessing the UI or a different thread where you would get a specific threading error, the dispatcher would be not advised to be used as that is not its purpose.

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

      Comment


        #63
        Thanks for the response, Jesse.

        I will look further into this. The double prints in the NinjaScript Output window only occurs when in Playback; the double entries occur very infrequently, at this point (if at all).

        My latest addition to the strategy is to: Add a Trailing Stop once Stop1 is hit.

        Is SetTrailStop() the proper method? If so, what should the parameters be for a 3-tick trailing stop off of Stop1?

        Thanks, Jesse.

        PS: trying to use SetTrailStop() in State.Configure results in an error msg: The name 'SetTrailStop' does not exist in the current context.
        Last edited by ronhb107; 02-07-2018, 11:31 AM.

        Comment


          #64
          Hello,

          Thank you for the reply.

          If this in reference to the last sample then you would be unable to use the Set methods in that script. The Set methods are part of the Managed approach which is only relevant for a strategy. As you are using the Addon methods for submitting orders from an indicator, you can only use the addon methods to manage or submit orders.

          Please ignore any of the Strategy specific syntax in the help guide Strategy section as it would no longer be relevant to this item.

          To create a trailing stop, you would need to submit the correct type of stop order for the entry in question and logically update that order as you see price changes.

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

          Comment


            #65
            In other words, I would have to go back to a Strategy.

            I guess I could see my orders on a Chart that doesn't include the Strategy (have the Strategy included on a separate Chart). That would negate the need to draw the fills.

            Would the above work? Or, is there a better approach? What I don't want to do is manage the Trail Stop in code.

            Thanks,
            Ron

            Comment


              #66
              Hello,

              Really whatever works for your intended goal, the platform will work a certain way as you have noted in this thread. You would need to find how you can utilize the platform for your goals in a way that benefits you. If going back to a strategy so you can use SetTrailStop is what you need, that would be the approach I suggest.

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

              Comment


                #67
                Add Profit Target and Stop Loss

                Jesse:

                Everything works exactly as it did in the Indicator version, except the Trail Stop.

                I'm using this code snippet (below) first in State.Configure, and then I added it to OnBarUpdate(). There was no Trail Stop generated.

                What am I doing wrong?

                Thanks,
                Ron

                Code:
                SetTrailStop("Stop1", CalculationMode.Ticks, 3, false);		// sets Trailing Stop on Stop1
                Attached Files

                Comment


                  #68
                  Hello,

                  The comment I had mad previously would still stand in a Strategy, you are using the Addon methods with Account.Submit which means you cannot mix the managed approach with your orders.
                  The Set methods are part of the Managed approach which is only relevant for a strategy. As you are using the Addon methods for submitting orders from an indicator, you can only use the addon methods to manage or submit orders.
                  You need to use the Addon methods through and through if you use Account.Submit. If you want to use the Managed Approach from a strategy, you would need to use the Managed Approach and only the Strategy methods. The Addon methods submit orders similar to how you would manually and manual trading and Strategy trading cannot be combined.

                  If you need to utilize the SetTrailStop, you will need to use the managed approach in a strategy specifically omitting any use of the Addon orders.

                  If you need to use the addon methods, you need to drop the use of the managed approach and make logic to make a trailing stop, there is no way to combine these two approaches that I could suggest.

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

                  Comment


                    #69
                    So we've come full circle, Jesse. If I want a fully automated approach, then the Strategy methods is the direction; if I want to have some automation to manage the manually entered trades, then the AddOn methods will work. (When you consider how volatile some indices are, including now the S&P e-mini, it becomes imperative to automate the management of an order).

                    The ATM is one approach to providing automation for manually entered trades, but it cannot address the unique requirements of every trader; hence, the use of scripts (programs). With the scripts you would expect that nearly every capability in managing entries would be addressed; but, unfortunately, not Trail Stops.

                    So, I am 'requesting' that a Trail Stop method be added to the AddOn group of methods, similar to SetTrailStop(). One line of code to save numerous lines and hours of work. This seems a reasonable request since the code already exists in the Strategy area; simply port if over to the AddOn area.

                    Btw, simply creating a TrailStop Order without submitting it creates an 'Initialize' OrderState. This is fine, except upon exiting the trade and the TrailStop is cancelled, it places a Stop flag on the Chart at a zero price level. This obviously negatively impacts the Chart and requires I turn off (then on) the Strategy to remove it. How can this be avoided?

                    UPDATE: having put everything back into an Indicator, I get this error msg when trying to Submit the TrailStop order (the current market position is Short): "Buy stop or buy stop limit order can't be placed below the market affected Order: Buy 5 StopMarket @ 0"

                    Going thru the Output window shows the price for the TrailStop order has a proper price (which is not zero) above the Stop1 price (which, when filled, created a market Short position and triggered the Submit of the TrailStop order). What am I doing wrong here?

                    Thanks, Jesse.

                    PS: is it possible to use a second ATM (that has a Trailing Stop) when Stop1 of the first ATM is triggered (within an AddOn)? If so, this might be a work-around to the problem (it's not elegant, but it could work). In this scenario, the script would call the second ATM using...

                    Code:
                    NinjaTrader.NinjaScript.AtmStrategy.StartAtmStrategy(AtmStrategy atmStrategyTemplate, Order entryOrder)
                    Attached Files
                    Last edited by ronhb107; 02-08-2018, 11:56 AM.

                    Comment


                      #70
                      Hello,

                      In regard to the trail stop, I would be unsure by the description on how to avoid that other than to not do that process or to somehow handle it with logic. Perhaps don't place the trailing stop into initialize and instead use logic to execute it at the appropriate time when it should actually be submitted?

                      Looking at your syntax it looks like you are supplying a 0 for the stop price and have used a stop market order, is this potentially the reason for the error?

                      You can submit a second atm strategy from the Addon framework but I am unsure if this would be a solution to the problem as they would be separate atms. If you can find a solution in using this I would certainly suggest you test it and see if it works for your purposes.

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

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by sightcareclickhere, Today, 01:55 PM
                      0 responses
                      1 view
                      0 likes
                      Last Post sightcareclickhere  
                      Started by Mindset, 05-06-2023, 09:03 PM
                      9 responses
                      258 views
                      0 likes
                      Last Post ender_wiggum  
                      Started by Mizzouman1, Today, 07:35 AM
                      4 responses
                      18 views
                      0 likes
                      Last Post Mizzouman1  
                      Started by philmg, Today, 01:17 PM
                      1 response
                      6 views
                      0 likes
                      Last Post NinjaTrader_ChristopherJ  
                      Started by cre8able, Today, 01:01 PM
                      1 response
                      9 views
                      0 likes
                      Last Post NinjaTrader_ChelseaB  
                      Working...
                      X