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

Trail stop that doesn't trail

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

    Trail stop that doesn't trail

    Howdy,

    I am using NT in simulation mode. I have encoutered a peculiar problem with SetTrailStop(). It behaves exactly like SetStopLoss(), i.e. the stop does not trail. Verified this from SuperDOM. But could this relate to the fact that the entry orders are not place within OnBarUpdate()?? Code:

    #region Variables

    // Parameters
    private bool useTrailStop = true;

    private int maxLoss = 8;
    private int lotsPerTrade = 1;

    #endregion

    protected override void Initialize()
    {

    if
    (UseTrailStop)
    {
    SetTrailStop(CalculationMode.Ticks, MaxLoss);
    }
    else
    {
    SetStopLoss(CalculationMode.Ticks, MaxLoss);
    }

    }

    protected override void OnMarketData(MarketDataEventArgs e)
    {
    // Code

    ...

    Main();
    }

    protected override void OnMarketDepth(MarketDepthEventArgs e)
    {
    // Code

    ...

    Main();
    }

    private void Main()
    {
    // Code
    ...


    EnterLong(0, LotsPerTrade, "ENTER_LONG");

    }
    Last edited by jp_kettunen; 12-19-2009, 10:27 AM. Reason: Poor layout

    #2
    Hello,

    That may be the issue. Try it within OnBarUpdate and let us know if it is still not working.
    DenNinjaTrader Customer Service

    Comment


      #3
      Hi Ben,

      You guessed right: SetTrailStop() works if EnterLong() is called inside OnBarUpdate(). Have I found a bug? Note that both SetStopLoss() and SetProfitTarget() function perfectly well if I call EnterLong() inside Main().

      JP

      Comment


        #4
        Hello,

        No, that is was I suspected because you have to place orders on a bar and a bar only exists in OnBarUpdate and BarsInProgress and the like.
        DenNinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Ben View Post
          Hello,

          No, that is was I suspected because you have to place orders on a bar and a bar only exists in OnBarUpdate and BarsInProgress and the like.
          Question: If I also wanted to generate orders as a response to level 2 changes, not only on the basis of actual trades in the market, then what should I do? Have carried out a lot of tests with that structure (i.e. all trading logic in one method that can be called from all other methods and routines, e.g OnBarUpdate() and OnMarketDepth(), and it seems to have been working except with Trail Stop. The problem is that level 2 changes do not wake up OnBarUpdate(), and if level 2 is active whiles trades are at short supply, e.g. because of wide spreads, having to wait for trades to be able to do something would be frustrating. How would you do that?

          JP

          Comment


            #6
            Hello,

            I believe you will need to trigger something from OnMarketDepth then wait for an OnBarUpdate event/tick in order to place an order. I will have someone confirm this on Monday though. This link may help:
            DenNinjaTrader Customer Service

            Comment


              #7
              Ok, waiting for further instructions, thanks!

              Btw: have already created the level II books and have noticed an interesting difference between Zen-Fire (based on Mirus demo) and Patsystems (MF Global demo):

              Zen-Fire works as expected: In case of a new lowest ask or a new highest bid (MarketDepthEventArgs.Position == 0) Operation == Operation.Insert; and if the lowest ask araises or the highest bid retreats Operation == Operation.Remove. But with Patsystems (it seems, this is still preliminary) Operation == Operation.Insert only when the corresponding 'slot' is created (i.e. once), and after that it is always Operation.Update, e.g. if bid volume is updated but also when we have a new highest bid or if the highest bid retreats (is removed). COMMENTS?!?!

              Comment


                #8
                You should only place trades within the context of NinjaScript methods and not in your own Main() calls.

                As far as event updates go, we pass along data just as the data provider offers it to us as. If they give it as an update event, then you will receive it as an update event, so you will just have to be mindful of such nuances between your feeds if this is occurring for your two feeds.
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  Josh:

                  Are there some established guidelines on dos/dont's on where we can and can not place orders using custom NinjaScripts?

                  This has to do with the sequence in which the internal engine processes user input when it relates to call backs (e.g. Order functions) after it is had done with the Update Calls.

                  It seems that the NT engine needs to be in a state where it can accept and process orders, and the Order callbacks are processed synchronously and do not trigger new asynchronous order processing control flow, before coming back to the Update call.

                  i.e.

                  Code:
                  OnBarUpdate{
                  
                   ...
                  
                   Some Order Function 1
                  
                  ...
                  
                   Some Order Function 2
                  
                  }
                  It seems that NT waits for the end of the call OnBarUpdate before processing the tasks requested in the order functions #1 and #2 above.

                  In a purely asynchronous event driven system. The SomeOrderFunction1 above will complete before the control flow reaches SomeOrderFunction2.

                  Please do advise, especially if NT7 will have some changes.

                  Comment


                    #10
                    aviat72, we highly recommend you do not place orders outside of the NinjaTrader main methods. You can run calculations inside your own methods, but do not place Enter() methods inside your own custom methods.
                    AustinNinjaTrader Customer Service

                    Comment


                      #11
                      Presuming then that I can place trades also within OnMarketDepth(), and not only within OnBarUpdate(), right? Concluding that if I place trades within my own functions/methods, such as Main(), the end result is arbitrary?

                      Comment


                        #12
                        JP, yes, you can place trades in OnMarketDepth(), OnExecution(), OnOrderUpdate(), OnMarketData(), etc.

                        Could you please clarify what you mean by the second question?
                        AustinNinjaTrader Customer Service

                        Comment


                          #13
                          Austin:

                          I think what he wants is a confirmation of what you said.

                          Essentially, the NT order processing engine is active only in the NT's market data driven functions (e.g. On[xyz] functions). If you make these calls in your custom code which could be triggered by some custom event trigger, then NT's order processing engine will not handle it.

                          In most cases it may not be an issue since an event which triggers an ON method call from NT will occur soon enough. But there are times when you to do order handling right away and DO NOT want to wait for an event to trigger an On call and process your order.

                          For example, you may want to go flat or cancel existing orders 10seconds before a market moving event. If the instrument you are trading is not moving (or is perhaps halted), you will have to wait till there is some price movement before NT processes your order.
                          Last edited by aviat72; 12-21-2009, 11:48 AM.

                          Comment


                            #14
                            aviat72, if I wanted to generate new orders (or cancel pending ones) on the basis of level II changes for a contract that does not currently trade e.g. due to a wide spread (typical of RMB and many others), I can do this inside OnMarketDepth(): it gets called in response to each change in the bid/ask price ladder. If there is any activity in the market, OnMarketDepth() is an effective way of getitng access to CPU time (whether you actually want to use level II data or not).

                            The problem is, however, that instead of being able to maintain the part of my code that generates market entry and exit orders in one place - i.e. inside a user-defined method that could be called from OnBarUpdate(), OnMarketDepth() or any other main NT method - I am now being instructed to copy paste that code to several places, resulting in future maintenance challenges (integrity etc.).

                            I am still a bit surprised since my approach seems to have been working well so far, though in a simulated environment, with the exception of that SetTrailStop(). My wild guess is that the risk our official NinjaTraders are implicitly referring to relates to the precedence of the NT's 'protected override' methods; perhaps an incoming tick activating such a method can halt the execution of a user-defined method, and if that method deals with orders, the end result is arbitrary. This is just reasoning..

                            Anyways, thanks a lot for advice and interesting views!
                            JP

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by Mathias79, Today, 03:44 PM
                            0 responses
                            9 views
                            0 likes
                            Last Post Mathias79  
                            Started by Austiner87, Today, 03:42 PM
                            0 responses
                            8 views
                            0 likes
                            Last Post Austiner87  
                            Started by lorem, 04-25-2024, 09:18 AM
                            19 responses
                            81 views
                            0 likes
                            Last Post NinjaTrader_ChelseaB  
                            Started by joselube001, 05-10-2024, 12:17 PM
                            6 responses
                            29 views
                            0 likes
                            Last Post joselube001  
                            Started by bigc0220, 09-18-2018, 09:16 AM
                            6 responses
                            2,587 views
                            0 likes
                            Last Post NinjaTrader_ChelseaB  
                            Working...
                            X