Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Custom Strategy with backtesting

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

    Custom Strategy with backtesting

    Hi,

    We're trying to write a strategy that would allow us to use a similar algorithm found in the ATM Strategy but with the ability to backtest on historical data. Writing the code (i.e. C#) is not an issue, what's an issue is understanding how it all should tie in. Here's what I would like to have:

    1) If some condition is met then go long. Set some variable to the low and high value to exit. For example -2 ticks to cut our loss and +4 to make a profit.
    a) If already long and we reached our profit target we should raise the profit bar and change the loss bar. For example -1 tick for a loss and +2 for a profit.
    b) If already long but below or above these limits we should exit the long.

    We would have something similar for the short part. The problem we're having is figuring out where to put all this logic (OnBarUpdate or OnExecution) and what methods we should use for enter/exit trades. There are 4 variations only on exiting the long alone (ExitLong, ExitLongLimit, ExitStop, ExitStopLimit). Which one do we need to use?

    The way I understand the system so far, we should be able to control the whole process in bar-by-bar. Ideally, from my point of view, we should decide manually when to enter or exit a trade, without relying on the xxxLimit magic

    I realize this is all vague, and it's because we're missing many pieces of the puzzle. Any help is appreciated.

    #2
    pbz1111, first of all welcome to the forums at NinjaTrader.

    You're correct, as NinjaTrader is event based you could control the code behavior bar by bar if it's placed in the OnBarUpdate -



    For a more granular order placement / modification approach, please see this related reference sample - http://www.ninjatrader-support2.com/...ead.php?t=7499

    As far as which order type you want to use, this is entirely up to you and should be the same as the type you manually use.

    Here's a little background on the order methods used in NinjaTrader - http://www.ninjatrader-support.com/H...verview36.html
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Hi Bertrand,

      Thanks for the welcome message

      I've looked at the SampleOnOrderUpdate strategy. My problem has more to do with the conceptual model of how all this should work. Looking at that sample, here's what I think I understand:

      * The order is placed in OnBarUpdate, the order is sent to the broker and it may take a while until it's filled. Until the order is filled, or partially filled, it's not really "active" and we could receive several OnBarUpdates until the order is filled (at least that's what happens when backtesting).
      * From OnExecute, if we notice that the order has been filled, we go ahead and set some limits for that order, a low price where we should get out and a high price that's our profit target. Why do we need to do this in OnExecute, and why only after the order is filled?
      * Back in OnBarUpdate, if our position in the market is long (BTW does that happen after the order is filled or right away after an EnterLong call?) we can adjust the stop loss (the low range we get out) by calling another ExitLongStop (the first one was called OnExecute).
      * We're not directly controlling when we get out of the current position, but rather set low and high boundaries for the current position. This works in some situations, but what if we need to control the exit based on other factors, like using some indicator data? Do you know of sample that we could use to control the entry and exit points, rather than just give it an "arbitrary" low and profit target?

      Thank you for your help!

      Comment


        #4
        pbz1111,

        1. Correct.

        2. You do it in OnExecution() because that is the earliest time possible to submit those protective orders at. You could wait till the next OnBarUpdate(), but who knows when that will come along. Placing in the protective orders immediately after receiving fill confirmation ensures you the most protection possible. The reason you place it after a fill is because there is no point putting in these orders if you have no position to exit.

        3. Happens after the position is part filled/filled. You can adjust your stop orders by calling them again with the new price you want, correct.

        4. If you want to exit based on other factors you need to then program the logic to place market orders when your indicator readings are so and so.

        Something like this
        Code:
        if (Position.MarketPosition == MarketPosition.Long && Close[0] > SMA(20)[0])
            ExitLong();
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Thanks Josh, it seems to work so far.

          When we ran the strategy in simulation mode we decided to set CalculateOnBarClose to false to be able to react to real-time per-tick data rather than what happens on bar-close. My understanding is that doing this would affect the calculations made by various indicators. The idea is to develop a strategy and be able to backtest it and have it run in simulation mode (and hopefully in live mode) in a similar way. What's the recommended way to make these two (backtest and live mode) and have somewhat similar results? Would we have to confine ourselves to act on OnBarUpdate only on bar close or is there another way?

          Ideally we would use the CalculateOnBarClose == true route until we decide for a position (long or short) and after that, once we're in a position, to somehow receive and be able to react to real-time data (not on bar close). Would OnMarketData help us with this?

          Thank you!

          Comment


            #6
            pbz1111, per default CalculateOnBarClose = true is the way to go in backtesting (as the intrabar tick formation is not known). However you can try simulating your conditions on a lower timeframe and thus achieven reacting quicker (as you would in realtime with CalculateOnBarClose set to false). However it can get involved getting this into in alignment...

            I suggest checking into this sample for your second question - http://www.ninjatrader-support2.com/...ad.php?t=19387
            BertrandNinjaTrader Customer Service

            Comment


              #7
              Thank you Bertrand! It looks like checking for FirstTickOfBar and shifting back to the previous bar would do the trick. Any reason why there isn't a LastTickOfBar?

              Just to make sure I understand: From the indicator's point of view, whether I set CalculateOnBarClose to false or true does not matter, right? Either way, if I use the FirstTickOfBar property when CalculateOnBarClose is false, I would get the same results? If this is the case then the FirstTickOfBar "trick", where I look back at the previous bar, would be the solution.

              Again, thanks for all your help and patience!

              Comment


                #8
                There's no LastTickOfBar as NinjaTrader is event driven, which means the if FirstTickOfBar returns true, you have a new bar and thus just reference one bar back more to make up for calling logic then on the open tick of the next bar.
                BertrandNinjaTrader Customer Service

                Comment


                  #9
                  I understand that, I meant it as a joke (hence the smiley) Is my understanding of how it works correct (second paragraph) ? Thanks.

                  Comment


                    #10
                    ok got it - just don't set any CalculateOnBarClose reference in the called indicator Initialize() for NinjaTrader 6.5 - and then direct this from the calling strategy directly and work with the FirstTickOfBar workaround to access 'on bar close' logic.
                    BertrandNinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by tkaboris, Today, 05:13 PM
                    0 responses
                    2 views
                    0 likes
                    Last Post tkaboris  
                    Started by GussJ, 03-04-2020, 03:11 PM
                    16 responses
                    3,281 views
                    0 likes
                    Last Post Leafcutter  
                    Started by WHICKED, Today, 12:45 PM
                    2 responses
                    19 views
                    0 likes
                    Last Post WHICKED
                    by WHICKED
                     
                    Started by Tim-c, Today, 02:10 PM
                    1 response
                    10 views
                    0 likes
                    Last Post NinjaTrader_ChelseaB  
                    Started by Taddypole, Today, 02:47 PM
                    0 responses
                    5 views
                    0 likes
                    Last Post Taddypole  
                    Working...
                    X