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

backtesting, entering a position in a secondary bar sereies

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

    backtesting, entering a position in a secondary bar sereies

    Hi,

    I'm having trouble with executing code that enters a position in a secondary bar series while backtesting. The command I am trying to execute is: if the price high of the current bar of the secondary bar series is greater than the variable ORhigh + (the variable Buyticks * the tick size) then enter a long position in the secondary bar series for the the number of contracts represented by the variable Shares.

    The code I have is:

    if (Highs [1] [0] >= ORhigh + Buyticks * TickSize)
    {
    count = count + 1;
    EnterLong(1,Shares, "BLong");
    }

    The code has no compilation errors. However, when I back test the strategy no trades are executed.

    I am fairly certain that he problem lies with the lines of code mentioned above, because when I substitute with the code below, that does not have any references to a secondary bar series, I see the expected trades take place when back tested.


    if (High [0] >= ORhigh + Buyticks * TickSize)
    {
    count = count + 1;
    EnterLong(Shares, "BLong");
    }

    It seems that my problem is getting the trade to enter a position in a secondary bar series. Please help.

    Thanks in advance.

    Joe

    #2
    edit: I re-read advanced order handling, working with multi instrument strategy, and even if you had that, it should work.

    What is ORHigh?

    I would use Output Window and put Print statements in the code to see what the actual values are (and if it is ever reaching this code).

    -------------------------------------
    I don't know what the rest of your code says, (I think the problem is there)..

    do you by chance have?
    Code:
    if (BarsInProgress!=0) return;
    Originally posted by jperales View Post
    Hi,

    I'm having trouble with executing code that enters a position in a secondary bar series while backtesting. The command I am trying to execute is: if the price high of the current bar of the secondary bar series is greater than the variable ORhigh + (the variable Buyticks * the tick size) then enter a long position in the secondary bar series for the the number of contracts represented by the variable Shares.

    The code I have is:

    if (Highs [1] [0] >= ORhigh + Buyticks * TickSize)
    {
    count = count + 1;
    EnterLong(1,Shares, "BLong");
    }

    The code has no compilation errors. However, when I back test the strategy no trades are executed.

    I am fairly certain that he problem lies with the lines of code mentioned above, because when I substitute with the code below, that does not have any references to a secondary bar series, I see the expected trades take place when back tested.


    if (High [0] >= ORhigh + Buyticks * TickSize)
    {
    count = count + 1;
    EnterLong(Shares, "BLong");
    }

    It seems that my problem is getting the trade to enter a position in a secondary bar series. Please help.

    Thanks in advance.

    Joe
    Last edited by sledge; 07-21-2012, 04:25 PM. Reason: don't like my answer

    Comment


      #3
      Can you post the complete code, or at least the initialize section of it ?
      What timeframes and datatypes are your primary/secondary dataseries ? (like 5min or 4Renko)

      Comment


        #4
        BarsInProgress help

        Thank you for your reply.

        I do not have the suggested line:
        if (BarsInProgress!=0) return;
        in the code.

        I tried to insert the line into the code however I still end up with the same result of no trades being executed when back testing. I'm still a novice when it comes to coding in NinjaTrader so forgive me for my ignorance on the subject. Where would you suggest I insert the line of code suggested above?

        I'm confused as to how and when to use BarsInProgress in code. I understand BarsInProgress allows for the separation of trading logic for different bars events, I'm just not sure how and when to use it.

        For instance in my code below, in Condition 1, within the OnBarUpdate() method, I want to use the primary series (3 min bars) to define the variables ORhigh and ORlow. Would I need to use a BarsInProgress line of code to specify that the primary series needs to be used, or is that the default if BarsInProgress is not used.

        In Condition 2, I want to switch to the secondary (tick) bar series to enter a trade after 9:36:00 and after price breaks through n # of ticks above the variable ORhigh, defined in condition 1 that used the primary bar series. Exactly how would I do that?

        Conditions 3, 4, 5 and 6 represent the Stop loss commands and exit commands. Would I need to reference BarsInProgress again since BarsInProgress was used in condition 2 and possibly condition1, or is the primary series the default and there is no need for using BarsInProgress again?

        Clarification on these questions would greatly be appreciated, instructional and very useful for putting together this strategy as well as other strategies that I plan to write in the future. Sorry if it's a simple question, as I'm still a novice at writing code for strategies and feel like I'm in the early part of the learning curve.

        Hope this answers the questions posted back to me, and thanks again in advance for helping me out with my questions/problem and education.

        Joe

        public class ORangeBreakout1 : Strategy
        {
        #region Variables
        // Wizard generated variables
        private int buyticks = 2; // Default setting for Buyticks
        private int sellticks = 2; // Default setting for Sellticks
        private int stopticks4long = 2; // Default setting for Stopticks4long
        private int stopticks4short = 2; // Default setting for Stopticks4short
        private int exitbars = 1; // Default setting for Exitbars
        private int shares = 100; // Default setting for Shares
        // User defined variables (add any user defined variables below)
        #endregion

        // variable to keep count of how many trades are taken per day and for keeping track of each days opening range high/low
        int count = 1;
        double ORhigh;
        double ORlow;
        /// <summary>
        /// This method is used to configure the strategy and is called once before any strategy method is called.
        /// </summary>
        protected override void Initialize()
        {
        // Adding a tick Bars object to strategy
        Add(PeriodType.Tick, 1);
        CalculateOnBarClose = false;
        }

        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
        // Conditon 1 finds opening range high and low, in the primary bar series, and resets variable count to 0
        if (ToTime(Time[0]) == 93600)
        {
        count = 0;
        ORhigh = MAX(High, 2) [0];
        ORlow = MIN(Low,2) [0];
        }
        // Condition 2 enters trades in the secondary bar series only after 9:36 and only allows 1 trade per day
        if (ToTime(Time[0]) >= 93600 && count == 0)
        {
        if (Highs [1] [0] >= ORhigh + Buyticks * TickSize)
        {
        count = count + 1;
        EnterLong(1,Shares, "BLong");
        }
        else
        {
        if (Low [0] <= ORlow - Sellticks * TickSize)
        {
        count = count + 1;
        EnterShort(Shares, "SShort");
        }
        }
        }
        // Condition 3 stoploss for long position

        SetStopLoss("BLong", CalculationMode.Price, ORlow - stopticks4long * TickSize, false);

        // Condition 4 stoploss for short position

        SetStopLoss("SShort", CalculationMode.Price, ORhigh + stopticks4short * TickSize, false);

        // Condition 5 exit n bars from entry of long position
        if (BarsSinceEntry() == Exitbars - 1)
        {
        ExitLong("BLongExit", "BLong");
        }
        // Condition 6 exit n bars from entry of short position
        if (BarsSinceEntry() == Exitbars - 1)
        {
        ExitShort("SShortExit", "SShort");
        }
        }

        Comment


          #5
          I made some minor modifications to your strategy please see attachment.
          You have to copy this attachment into your strategy directory (C:\Users\......\Documents\NinjaTrader 7\bin\Custom\Strategy ) and compile.

          In the code (line 58,88) are 2 print statements which we can use for further debugging.You have to open the output screen to view the output.

          Probably it will still not work, but at least we now have the same code


          You have tick-based historical data do you ? Because the secondary timeframe is 1 Tick

          edit: for updated attachment see post#10

          Marco
          Last edited by marcow; 07-22-2012, 06:47 PM.

          Comment


            #6
            **NT** Error on calling 'OnBarUpdate' method for strategy 'ccc1/04ec68f05e5649318243d9bc7e6fdbb8': You must use the overload that has a 'BarsInProgress' parameter when calling the BarsSinceEntry() method in the context of a multi-time frame and instrument strategy.

            Comment


              #7
              BarsSinceEntry()



              Definition
              Returns the number of bars that have elapsed since the last specified entry.

              Method Return Value
              An int value that represents a number of bars.

              Syntax
              BarsSinceEntry()
              BarsSinceEntry(string signalName)

              The following method signature should be used when working with multi-time frame and instrument strategies:

              BarsSinceEntry(int barsInProgressIndex, string signalName, int entriesAgo)

              Comment


                #8
                I use this in market replay:
                Code:
                      protected override void OnBarUpdate()
                        {
                            if (Historical) return;
                ...

                Code:
                                    // Condition 5 exit n bars from entry of long position 
                                    if (BarsSinceEntry(1,"BLong",0) == Exitbars - 1)
                                        ExitLong("BLongExit", "BLong");
                                            
                                    // Condition 6 exit n bars from entry of short position
                                    if (BarsSinceEntry(1,"SShort",0) == Exitbars - 1)
                                        ExitShort("SShortExit", "SShort");

                Comment


                  #9
                  The short failed with some error about setting stop limit above entry or something......

                  Comment


                    #10
                    thank you sledge for the heads-up on the BarsSinceEntry !
                    please find attached the updated strategy.

                    Do you still get the error for the stoploss ?

                    Marco
                    Attached Files
                    Last edited by marcow; 07-22-2012, 06:36 PM.

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Stanfillirenfro, Yesterday, 09:19 AM
                    7 responses
                    51 views
                    0 likes
                    Last Post NinjaTrader_Gaby  
                    Started by TraderCro, 04-12-2024, 11:36 AM
                    4 responses
                    69 views
                    0 likes
                    Last Post Mindset
                    by Mindset
                     
                    Started by Mindset, Yesterday, 02:04 AM
                    1 response
                    15 views
                    0 likes
                    Last Post Mindset
                    by Mindset
                     
                    Started by agclub, 04-21-2024, 08:57 PM
                    4 responses
                    18 views
                    0 likes
                    Last Post NinjaTrader_Gaby  
                    Started by Irukandji, Today, 04:58 AM
                    0 responses
                    6 views
                    0 likes
                    Last Post Irukandji  
                    Working...
                    X