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

Strange Behaviour

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

    Strange Behaviour

    Hello Support,

    I have experienced some very strange and mysterious behaviour in my strategy.



    In this screenshot, the trade; Long Entry - 1000 should have been generated when the Close crosses and stays Above the EMA(X) (which is the line in blue) within the previous 5 bars. Here is the code for this:

    Code:
    bool condition = false;
    							
    for(int i = 1; i <= 5; i++)
    {
    	if(CrossAbove(Closes[1], EMA(BarsArray[1], X), i))
    	{
    		condition = true;
    		break;
    	}
    	if(CrossBelow(Closes[1], EMA(BarsArray[1], X), i))
    	{
    		condition = false;
    		break;
    	}
    }
    Now the above code returns true when it should not have had and triggered the order; Short Entry - 1000. The closes never crossed above the EMA and stayed above. Which as one can clearly see on the chart is not what should have happened. Can you please clarify why this happened? Our strategy logs also confirm that the cross above did happen when the loop variable value was 1 (i = 1 in above code).

    There have also been similar instances in other charts as well which we fail to comprehend. Another example is:



    Here the conditions are opposite i.e the Closes should have crossed below the EMA.

    Also, why are the bars being drawn so oddly? as you can see that the bar previous to where the order is generated is a green bar, and then the next bar (entry order bar) is also green but open even below the previous bar's open. Why is this happening? Please explain.

    Does this bar drawing have anything to do with the issue with the Cross Above/Below condition as well?

    Please advise.

    Thanks
    Umer

    #2
    Hello Master.Aurora,

    Thanks for your note.

    To gather a little more information I have a few questions.

    What is the primary data series added to the chart?

    What is the secondary data series added to the script?

    Also, please add prints to your script and let me know the outcome of the prints.

    For example:
    Code:
    bool condition = false;
    							
    for(int i = 1; i <= 5; i++)
    {
    Print("Previous: " + Closes[1][i+1] + " - " + EMA(BarsArray[1], X)[i+1] + " | Current: " + Closes[1][i] + " - " + EMA(BarsArray[1], X)[i]);
    	if(CrossAbove(Closes[1], EMA(BarsArray[1], X), i))
    	{
    		condition = true;
    		break;
    	}
    	if(CrossBelow(Closes[1], EMA(BarsArray[1], X), i))
    	{
    		condition = false;
    		break;
    	}
    }
    
    Print(" ");
    To address the question about the bars, typically the next bar will start with the next tick of data, this means that the open of the next bar is usually a tick different from the close of the previous bar.

    However, it is also possible for there to be gaps between ticks, meaning that the next tick that comes in is more than a tick different from the previous. This does cause gaps between the close of a bar and the open of the next.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Please find my comments below:

      What is the primary data series added to the chart?

      The primary data series is of a stock with a period of 1 Min.

      What is the secondary data series added to the script?

      The primary data series is of a stock with a period of 10 Mins.

      Also, please add prints to your script and let me know the outcome of the prints.

      We will need to add logging to our code and give it another try. That will take a little time. If you can share any opinion with the information available, it would be great. We will give it another try as well with detailed logging and let you know as well.

      Thanks
      Umer

      Comment


        #4
        Master.Aurora,

        Also, what is the period you are using for the SMA (value of X)?

        This is so that I may test this script on my end.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Period for EMA = 20.

          Comment


            #6
            Master.Aurora,

            Thanks for that information.

            I'm doing some testing now and will let you know what I find.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hi Master.Aurora,

              Thanks for your patience while I tested this.

              So to get a good idea of exactly what is happening I did this:

              First, I added your code to a script. Next, I added arrows to be drawn when the conditions are true.

              Then I opened a chart with AAPL with a 1 minute and 10 minute series, the 1 minute is the primary series shown in lighter colors, the 10 minute using darker colors.

              Next I added an EMA(20) directly to the chart using the 10 minute series as the input series for this indicator.

              Last I ran the script and took a screenshot of the results.


              The results do seem to be correct. The bar at 7:31 does dip below the EMA line of the 10 minute series, and the 7:40 bar rises above it.

              The for loop you have checks the previous 5 bars on the 10 minute series not including the current bar. So the basic logic is, if the 10 minute bars dip below the EMA(20) of the 10 minute series within the last 50 minutes (10 minute bars * 5 bars ago) then place a red down arrow above that bar. If the 10 minute bars rise above the EMA(20) for the 10 minutes series within the last 50 minutes, then place a green up arrow below the bar.


              The code for this is matching the actions.
              Attached Files
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                Thank you for this detailed analysis and report. Really appreciated.

                So this proves that the functionality i am trying to get out of the code is correct as per the requirements. So the problem MAY lie in the fact that the issues were encountered on relatively illiquid stocks. And in that the way that NinjaTrader maintains the price dataseries. I have updated the logging and we are testing it again. I will share my findings.

                Thanks
                Umer

                Comment


                  #9
                  Hi Umer,

                  I think the best thing to help you look into this would be to set up your chart similar to how my chart is setup.

                  This way you can see both data series, and the EMA line. Also, ensure your EMA is using the 10 minute series as its input series.

                  Let me know if I can be of any further assistance.
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    Run with Updated Logging

                    Hello Chelsea,

                    Thank you for the support so far.

                    We ran it again with updated logging last night and i would share the results with you now. But before i do, i would like to mention it here that last week when tested this strategy with FOREX pairs, it was working perfectly.



                    In the above instance of the strategy, the order Short Entry - 1000; should not have been triggered when it did because the Close did not cross below the EMA in the past 5 bars.

                    Order Time = 31/05/2013 20:30:43

                    As we check this condition on the first tick of the bar only and then retain it throughout the bar. This is because we have other conditions that need to be checked n every tick while this condition does not.

                    Condition Checking Time = 2013-05-31 20:30:08,147

                    Previous Values:
                    Close = 7
                    EMA = 6.98368112369338

                    Current Values:
                    Close = 7
                    EMA = 6.98523530238925

                    And it returned a true value after the very first iteration. Now what i dont understand is how the above values constitute a Cross Below. Can you? Also, why are the current & previous close values are the same when clearly on the chart they are not.

                    Code Snippet:

                    Code:
                    if(FirstTickOfBar)
                    {
                    	bool thisTickValue = false;
                    	
                    	for(int i = 1; i <= 5; i++)
                    	{
                    		Logger("Previous Values : Close = " + Closes[1][i+1] + " - EMA = " + EMA(BarsArray[1], 20)[i+1] + 
                    						  " | Current Values : Close = " + Closes[1][i] + " - EMA = " + EMA(BarsArray[1], 20)[i]);
                    		
                    		if(CrossBelow(Closes[1], EMA(BarsArray[1],20), i))
                    		{
                    			thisTickValue = true;
                    			break;
                    		}
                    		if(CrossAbove(Closes[1], EMA(BarsArray[1],20), i))
                    		{
                    			thisTickValue = false;
                    			break;
                    		}
                    	}
                    	
                    	conditionValue = thisTickValue;
                    }
                    And, just to re-iterate again; the above code will keep on checking whether a cross below has occurred in the previous i bars.

                    There were a lot of other instances but i will share just one more here.



                    Order Time = 31/05/2013 20:32:08

                    Condition Checking Time = 2013-05-31 20:30:31,696

                    Previous Values:
                    Close = 24.28
                    EMA = 24.2576371891216

                    Current Values:
                    Close = 24.26
                    EMA = 24.257862218729

                    Again the value seen on the chart and that in the logs do not match.

                    This is very confusing for us as well as our client. We would like to get to the bottom of this as soon as possible.

                    Please advise.

                    Thanks
                    Umer
                    Last edited by Master.Aurora; 06-01-2013, 03:32 AM.

                    Comment


                      #11
                      In the above instance of the strategy, the order Short Entry - 1000; should not have been triggered when it did because the Close did not cross below the EMA in the past 5 bars.
                      The region you have boxed here did have a cross above followed by a cross below on the very next bar.
                      As long as there is a close above/below this line it will constitute a cross above/below.

                      If I'm understanding correctly it sounds like the confusion is coming from the CrossAbove/Below method.

                      For example a lookback period of 5 does not simply check to see if there was a cross 5 bars ago. It will check this but it will also check to see if a cross happened anywhere in the past 5 bars.

                      Because of this you won't need to create your own loops to see if a cross in the past X bars occurred.



                      Let me know if I can be of further assistance.
                      LanceNinjaTrader Customer Service

                      Comment


                        #12
                        Hello Support,

                        I think i understand what the problem might be. To implement a solution i have thought of, i need some help. How can i access the series of close values in a Multi-Timeframe strategy? What i mean is that i do not want to access each element of the dataseries seaprately; i.e

                        Closes[1][0]

                        Rather, i want to access the whole of the Close series from the 1st secondary dataseries. Is this possible?

                        Please advise.

                        Thanks
                        Umer

                        Comment


                          #13
                          Umer, perhaps I'm not following you 100%, but Closes[1] would give you access to the series itself, as you don't request a specific double element with the index back.
                          BertrandNinjaTrader Customer Service

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by futtrader, 04-21-2024, 01:50 AM
                          4 responses
                          41 views
                          0 likes
                          Last Post futtrader  
                          Started by Option Whisperer, Today, 09:55 AM
                          1 response
                          11 views
                          0 likes
                          Last Post bltdavid  
                          Started by port119, Today, 02:43 PM
                          0 responses
                          3 views
                          0 likes
                          Last Post port119
                          by port119
                           
                          Started by Philippe56140, Today, 02:35 PM
                          0 responses
                          4 views
                          0 likes
                          Last Post Philippe56140  
                          Started by 00nevest, Today, 02:27 PM
                          0 responses
                          2 views
                          0 likes
                          Last Post 00nevest  
                          Working...
                          X