• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy Analyser vs. Live

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

    Strategy Analyser vs. Live

    Hi,

    When I took my strategy to live sim01 trading I had the following issue: For some reason it behaved differently to my backtests in a specific scenario.

    The strategy is short, and a long signal is triggerd at the same time as the short exit condition. In this case, the strategy reverses my position (one trade "close position" one "buy") but it also executes the "buy to cover" order and I end up 2 contracts long.

    If I backtest the same period there is only one contract, in market replay two. I read the help guide section on real-time vs. backtest but didn't find a solution (CalculateOnBar is set true, like in backtesting).

    If anyone has run into similar issues I would be happy if you could help me with this.

    Marco


    #2
    marco,

    You don't need to do an ExitShort() and EnterLong() at the same time. The EnterLong() will automatically reverse your position and close the Short position.

    Please let me know if I may assist further.
    Adam P.NinjaTrader Customer Service

    Comment


      #3
      Hi Adam,

      Thanks for your answer, I know that a EnterLong() order does exit my short position and goes long. But the ExitShort() is needed for the strategy. Like I said this is a special case where both, EnterLong() and ExitShort() are triggerd at the same close, although they follow completely different logic.

      Any idea how I can avoid getting long two contracts, with leaving the exit logic as is. I'm wondering why it executes since my position is 1L anyways.

      Comment


        #4
        marco,

        You could use a flag that is set to false whenever one or the other condition occurs, then only is set to true once an order is placed. Then, you just check for the flag before you allow an exit or a entershort. Essentially its like toggling a switch that says "Ok, I cannot exit a short now because I already entered a long", etc.

        Please let me know if I may assist further.
        Adam P.NinjaTrader Customer Service

        Comment


          #5
          Hi,

          I tried using 2 flags, one for longtrades one for shorttrades and tested the system live last week:

          Suppose the system enters long (setting Longflag = true), then it has 2 options to exit that trade:
          1. following exit condition set (including Longflag = true)
          2. to enter a short trade, because it has to close the trade before

          Now suppose I exit the trade throug a short trade this would set Longflag = false and invalidate the exit condition set.

          I use the same procedure for Shortflag. This is the only thing I changed in my script. Backtesting in strategy analyser follows my logic (as it did without flags). In live sim01 trading most trades now follow the corret logic. However I now have some erroneous trades entered at times that the system should ignore.

          For example: In 30 min timeframe (calculate on bar close = true) the system now enters at 17.40.46s and exits at 17.40.48s and executes the trade 7 times (in the live performance report). Can you give me any suggestions for debugging this? I only want the system to take the exactly same trades as in backtesting.

          I appreciate you help,

          Marco

          Comment


            #6
            Marco,

            You won't get the same exact trades in backtesting as in real time. Please see the following link.

            http://www.ninjatrader.com/support/h...ime_vs_bac.htm

            Here is a reference sample on getting intra-bar granularity : http://www.ninjatrader.com/support/f...ead.php?t=6652

            As far as debugging, without more details or seeing the code its difficult to determine exactly what is causing the issue. If you could explain more or post your code I would be happy to take a look, or comment further.
            Adam P.NinjaTrader Customer Service

            Comment


              #7
              Hi Adam,

              Thanks for the quick reply, you're really reactive in the forum. I read discrepanicies section in the help guide but I didn't find any solution. My code looks as follows (My indicator takes either 1 or -1):

              protected override void Initialize()
              {

              CalculateOnBarClose = true;
              }

              protected override void OnBarUpdate()
              {
              // Long condition set
              if (MyIndicator == 1
              && SMA(44)[0] < Close[0])
              {
              EnterLong(DefaultQuantity, "Long1");
              flagLong = true;
              flagShort = false;
              }

              // Exit long condition set
              if (CrossBelow(EMA(6), SMA(14), 1)
              && flagShort == false)
              {
              ExitLong("ExitLong", "Long1");

              }

              // Enter short condition set
              if (MyIndicator == -1
              && SMA(44)[0] > Close[0])
              {
              EnterShort(DefaultQuantity, "Short1");
              flagLong = false;
              flagShort = true;
              }

              // Exit long condition set
              if (CrossAbove(EMA(6), SMA(14), 1)
              && flagLong == false)
              {
              ExitShort("ExitShort", "Short1");

              }

              Comment


                #8
                Marco,

                I would suggest that the issue is the following two conditions could be true at the same time :

                MyIndicator == 1
                && SMA(44)[0] < Close[0]

                CrossBelow(EMA(6), SMA(14), 1)
                && flagShort == false

                Causing you to enter and exit a trade really quickly. I would suggest that you reset your flags in the exit sections as well and also check that you are in a long/short before you exit.

                Please let me know if I may assist further.
                Adam P.NinjaTrader Customer Service

                Comment


                  #9
                  Hi Adam,

                  just to tell that now everything works well. Finally I use position.marketposition to check if i'm long in the exit code.

                  Marco

                  Comment


                    #10
                    Marco, thanks for the update. The underlying issue here is that when you call ExitLong() and EnterShort() in the same code block (or even in different code blocks if it happens fast enough), the exit sends a close order and enter short doesn't yet know there isn't supposed to be a position (because the new flat position hasn't been reported to NinjaTrader yet), so it also sends a short order. This is what gives you the double position.
                    AustinNinjaTrader Customer Service

                    Comment


                      #11
                      Flages don't change il live trading

                      Hi,

                      Th system is now live and orders are executed on my brokerage account still using the flags to prevent that the two conditions come true at the same time. Today the system opened a long position in ZB, but the Longflag did not become true. It does perfectly in sim01 trading. I don't understand why?

                      the code is the following:

                      // Long condition set
                      if (MyIndicator == 1
                      && SMA(44)[0] < Close[0])
                      {
                      EnterLong(DefaultQuantity, "Long1");
                      flagLong = true;
                      flagShort = false;
                      }

                      Once the condition is met and I'm long if I look at parameters in the strategies tab, flagLong = false? do you have any idea why? it changes in sim01 testing.

                      Thanks in advance.

                      Marco

                      Comment


                        #12
                        marco,

                        I am not sure why this would be the case. I would suggest doing some walk-forward testing on a sim account with your data feed coming in live. Then you can make liberal use of print statements in the output window to follow the logic and variables of your strategy as it happens live.

                        Is this strategy CalculateOnBarClose = false?

                        If you post your strategy, we can possibly comment further.

                        Print statements can be used like following :

                        Print("string here"+Variable[0]+" another here");

                        Please let me know if I may assist further.
                        Adam P.NinjaTrader Customer Service

                        Comment


                          #13
                          Hi Adam,

                          I already id wald forward testing with live data and sim01 account. The parameters (flagLong and flagShort) changed as expected when the strategy enters a trade.

                          I recap for better understanding:

                          I use two parameters flagLong and flagShort to pervent two conditions to become true at the same time (cf. code below). I start the strategie with both parameters set to false, and expect them to become true when a trade (either long or short) is entered. In walk forward testing with live stream, I can see (control center strategies tab) how the parameters become true when the trade is entered. When I use a live account (being the only difference in the strategies setting tab) the parameters don't change when a trade is entered (at least not displayed in CC strategies tab). However, after I stopped the strategy (unchecked the box in strategies tab) the parameters changed and the expected values were displayed.

                          My question is whether this is just an issue of correct display, or whether I have to fear that the parameters are not set correctly while trading. If they are not this would be a great danger to my strategy because I would not exit the trades as expected.

                          I sincerly appreciate your help,

                          Marco

                          Code:
                          protected override void Initialize()
                                  {
                          
                                      CalculateOnBarClose = true;
                                  }
                          
                                  /// <summary>
                                  /// Called on each bar update event (incoming tick)
                                  /// </summary>
                                  protected override void OnBarUpdate()
                                  {
                                      // Condition set 1
                                      if (MyIndicator == 1
                                          && SMA(44)[0] < Close[0])
                                      {
                                          EnterLong(DefaultQuantity, "Long1");
                          				flagLong = true;
                          				flagShort = false;
                                      }
                          			
                          			            // Condition set 3
                                      if (MyIndicator == -1
                                          && SMA(44)[0] > Close[0])
                                      {
                                          EnterShort(DefaultQuantity, "Short1");
                          				flagLong = false;
                          				flagShort = true;
                                      }
                          
                                      // Condition set 2
                                      if (Position.MarketPosition == MarketPosition.Long)
                          			{
                          				if (CrossBelow(EMA(6), SMA(14), 1)
                          					&& flagLong == true)
                          				{
                          					ExitLong("ExitLong", "Long1");
                          					flagLong = false;
                          					
                          				}
                          			}
                                  // Condition set 4
                          			if (Position.MarketPosition == MarketPosition.Short)
                          			{
                          				if (CrossAbove(EMA(6), SMA(14), 1)
                          					&& flagShort == true)
                          				{
                          					ExitShort("ExitShort", "Short1");
                          					flagShort = false;
                          				}
                          			}
                          			
                                  }
                          Last edited by marcoheimann; 12-20-2011, 06:16 AM.

                          Comment


                            #14
                            Hello,

                            I'm not sure of the expected behavior. Did you try using Print() statements and looking at these values in real-time to see if they are resetting as expected?

                            Also keep in mind that if you're using CaculateOnBarClose = true, events in OnBarUpdate will not be modified until the next bar is processed. If you wish to immediately set this flag to false, I would recommend working in OnExecution() or OnOrderUpdate() to ensure this flag is in fact false once you are out of position.

                            You can see some examples of using OnExeuction or OnOrderUpdate from the following reference sample:

                            http://www.ninjatrader.com/support/f...ead.php?t=7499
                            MatthewNinjaTrader Product Management

                            Comment


                              #15
                              Hello Mathew,

                              Thanks for your answer. Before I start to code the OnExecutionUpdate() method could you please clarify something for me?

                              Can you tell me if within the OnBarUpdate method() Position.MarketPosition == MarketPosition.Long is still true when the longtrade was exited right before within the same OnBarUpdate()

                              I appreciate your help,

                              Marco

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by mreanuzlazx, Today, 01:30 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post mreanuzlazx  
                              Started by xinepev, Today, 12:33 AM
                              0 responses
                              4 views
                              0 likes
                              Last Post xinepev
                              by xinepev
                               
                              Started by sun0781, Yesterday, 05:02 PM
                              0 responses
                              9 views
                              0 likes
                              Last Post sun0781
                              by sun0781
                               
                              Started by ronhb107, 09-12-2019, 10:09 AM
                              4 responses
                              44 views
                              0 likes
                              Last Post Mr.Trader  
                              Started by rwilliam, Yesterday, 03:55 PM
                              0 responses
                              4 views
                              0 likes
                              Last Post rwilliam  
                              Working...
                              X