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

BarsSinceExit in Multi Time Frame

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

    BarsSinceExit in Multi Time Frame

    Hello,

    i have one question regarding the BarsSinceExit function in Multi Time Frame strategies.

    My problem is: i only get the exits for the main bar object. i dont get any kind of information about the added bar objects

    What i have done:
    i have a script with two different short entry rules in two different time frames (0 and 2).
    for the exit rule i have a third time frame (1).

    the script looks like that
    --> Main time frame = 15min

    protected override void Initialize()
    {
    Add(PeriodType.Minute , 3);
    Add(PeriodType.Minute , 5);

    CalculateOnBarClose = false;
    ....
    }
    protected override void OnBarUpdate()
    {
    // Short 15min
    if (BarsInProgress == 0)
    {
    if (Position.MarketPosition == MarketPosition.Flat
    && ....

    {
    EnterShort( 0 , 1 ,"S15"); // --> Enter short in bar object 0 ( the 15min time frame)
    Print(BarsSinceExit(0,"S15",0));// --> works fine, i get the information about the last exit for this rule
    }
    }

    // Short Entry 5min BB
    if (BarsInProgress == 2)
    {
    if (Position.MarketPosition == MarketPosition.Flat
    && ...
    {
    EnterShort(2,1,"S5"); // enter short in bar object 2 (the 5 min time frame)
    Print(BarsSinceExit(2,"S5",0)) // !!! doesn't work. he says there is no exit in bar object 2 with the signal name "S5" (result =-1)
    Print(BarsSinceExit(0,"S5",0)) // also doesn't work. but seems to be okay, because there was no bar oject with entry signal "S5"
    Print(BarsSinceExit(2,"",0)) // !!! the result is the exit bar of bar object 0....hm.
    }
    }

    // ######################
    // Exit
    if (BarsInProgress == 1)
    {
    if (Position.MarketPosition == MarketPosition.Short
    && ...
    {
    ExitShort("Target", "");
    //ExitShort(2,1,"Target",""); --> the result is the same, i don't get any information about the last exit of my barsinprogress=2 entry-exit pair
    }
    }

    ----------------

    Finally one "simple" question. What do i have to do, to get the information about the last exit of any existing trade, no matter what time frame or bar object the trade was?

    Sorry for the first post, little mistake.
    Thx for your answers
    Last edited by SoGehtDas; 08-13-2008, 02:34 PM.

    #2
    Please provide all code and comments in English, since we run our support in English only.

    Also: what exactly is your issue?

    Comment


      #3
      post changed.
      sorry my mistake

      Comment


        #4
        Sorry I don't follow. I don't see any exit for barsInProgress=2 in your code. What problem do you see?

        Comment


          #5
          BarsSinceExit(int barsInProgressIndex, string signalName, int exitsAgo)
          barsInProgressIndex
          The index of the Bars object the entry order was submitted against

          - my entry order was submitted against barsInProgress=2 (& the exit order was submitted against barsInProgress=1 --> you are correct, there is no exit for barsInProgress=2, but doesn't matter. or?)
          --> what i want to know is the BarsSinceExit for this entry bar object.

          Print(BarsSinceExit(2,"S5",0)) --> this leads to all exits for bar object 0. But not for bar object 2. Why?
          if i understand the definition right, then the result should be the following: BarsSinceExit(2,"S5",0) = the BarsSinceExit for BarObject 2 with the entry signal name "S5" and 0 exits ago, means the last exit for object 2 instead of the last exit for object 0.

          Thanks.

          Comment


            #6
            >> Print(BarsSinceExit(2,"S5",0)) --> this leads to all exits for bar object 0
            expected

            In your scenario (multiple series on SAME instrument) the position for that instrument (there only is one) is manged on barsInProgress=0, meaning there is no position for barsInProgress=2.

            Thus any method related to that position would be executed like barsInProgress=0.

            Comment


              #7
              hmmm. to be honest, i don't get it.
              you say: there is no position for barsInProgress=2.
              okay, but if all my positions are managed in bar object 0. why do i only get the information about the "real" bar object 0 trades and not about all trades, even the trades entered after my if (BarsInProgress == 2) condition?

              May be we can shorten this dicussion.
              What do i have to do, to get the information about my last exit?

              Comment


                #8
                Positions[0] = Positions[1] = Positions[2] in your case

                BarsSinceExit() should work

                Comment


                  #9
                  your solution isn't working.
                  sorry for my late reply, but i was testing a lot.
                  i took your "SampleIntrabarBacktest" and modified it a bit. the result is the following
                  BarsSinceEntry and BarsSinceExit aren't working as expected.
                  Please have a look and tell me what i am doing wrong. i really don't find the mistake.
                  protected override void Initialize()
                  {
                  Add(PeriodType.Minute, 5);

                  CalculateOnBarClose = false;
                  ExitOnClose = false;
                  }

                  protected override void OnBarUpdate()
                  {
                  // --> everything around BarsInProgress == 0 works fine
                  if (BarsInProgress == 0)
                  {
                  // --- only for testing ---
                  //Print(BarsSinceEntry( 1 , "Entry 5" , 0)); // no result!!! always = 0!!!
                  //Print(BarsSinceEntry(0,"",0)); // no result!!! always = 0
                  //Print(BarsSinceEntry()); // the system doesn't work, he is doing nothing

                  if (CrossAbove(EMA(Fast15), EMA(Slow15), 1))
                  {
                  EnterLong( 0 , 1 , "Entry 15");
                  }

                  if (BarsSinceEntry( 0 , "Entry 15" , 0) > 5)
                  {
                  ExitLong( 0 , 1 , "Exit 15er","Entry 15");
                  }
                  }

                  // --> the entry trade for BarsInProgress == 1 is correct
                  if (BarsInProgress == 1)
                  {
                  if (CrossAbove(EMA(Fast15), EMA(Slow15), 1))
                  {
                  EnterLong( 1 , 1 , "Entry 5");
                  }

                  // no matter what condition i use, BarSinceEntry is allways 0! --> it is impossible to exit the initial entry!
                  if (BarsSinceEntry( 1 , "Entry 5" , 0) > 5) // doesn't work!!!
                  //if (BarsSinceEntry( 0 , "Entry 5" , 0) > 5) // doesn't work!!!
                  //if (BarsSinceEntry( ) > 5) // the hole system doesn't work anymore!!! no single entry trade
                  {
                  ExitLong(1 , 1 , "Exit 5er","Entry 5");
                  }
                  }
                  }

                  --> if i deactivate the BarsInProgress == 1 condition, the system works really fine. after six 15min bars he leaves the trade ...
                  But, if i activate the BIP = 1, then he is doing the first entry, but no more exit. ---> WHERE IS THE VALUE FOR THE BARSSINCEENTRY FOR THE BAR OBJECT 1??? and how can i work with this value? ... it's the same with BarsSinceExit for different bar objects! not instruments!

                  THX for your HELP! I am a little bit desperate in the meantime.

                  Comment


                    #10
                    We'll look into and let you know.

                    Comment


                      #11
                      i am for a few days in holiday. don't be surprised if i don't answer wihtin the next few days.
                      caused by some problems with the export function i have added the script text as a textfile. maybe you can use this for further analysis.

                      Thx again!
                      Attached Files

                      Comment


                        #12
                        This is a bug which will be fixed with next update (2-3 weeks from now). Thanks for pointing that out.

                        Comment


                          #13
                          I am also facing the same problem.
                          BarsSinceExit is not working as soon as I add next time period.

                          Comment


                            #14
                            Thought someone may use the temp workaround I have in my code for this issue
                            May be it will solve your problem.. may be not.. I hope it does. Good luck

                            In the variable declaration

                            //Following the workaround variable
                            //BarsArray[0].CurrentBar
                            private int bar_Num_of_Long_Exit = 0; // have to be careful if a strategy is runing too long. it might cross the INT's defined limit
                            private int bar_Num_of_Short_Exit = 0; // have to be careful if a strategy is runing too long. it might cross the INT's defined limit
                            //End of Workaround variable till the BarsSinceExit is fixed


                            protected override void Initialize()
                            {
                            // 1 tick bar added to simulate the real time action and is pretty accurate approximation
                            // to make is work real well. now I need to find a full tick by tick historical data
                            // If this produces good results.. then in fact the realtime coding doesnt really need to use Calculateonbarclose = false
                            Add(PeriodType.Tick, 1);

                            }

                            protected override void OnBarUpdate()
                            {

                            //return everytime when the onbarupdate is called from main bar. i.e BarsArray[0]
                            if (BarsInProgress != 1)
                            return;




                            if (my entry condition) && (bar_Num_of_Short_Exit!=BarsArray[0].CurrentBar)
                            {
                            EnterShort("ShortRule4");

                            }


                            if( my exit condition)
                            {

                            ExitShort("ExitShortNextPerdHiBreach", "ShortRule4");
                            bar_Num_of_Short_Exit = BarsArray[0].CurrentBar;
                            }


                            }

                            The above example I am running on 10 min bar. and my problem was that the trade was being entered again and again since
                            the entry condition was getting true on every incoming tick bar.. and exit condition too (dont ask why :P yes I am that stupid)
                            but this solution worked for me to not allow reentry using same rule on the same 10MIN bar.

                            Comment


                              #15
                              Originally posted by NinjaTrader_Dierk View Post
                              This is a bug which will be fixed with next update (2-3 weeks from now). Thanks for pointing that out.
                              Can you confirm this has been fixed in the current release? I am experiencing problems with BarsSinceExit in a multi-time frame strategy, and am not sure if it is related.

                              Mike

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by mattbsea, Today, 05:44 PM
                              0 responses
                              5 views
                              0 likes
                              Last Post mattbsea  
                              Started by RideMe, 04-07-2024, 04:54 PM
                              6 responses
                              31 views
                              0 likes
                              Last Post RideMe
                              by RideMe
                               
                              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,282 views
                              0 likes
                              Last Post Leafcutter  
                              Started by WHICKED, Today, 12:45 PM
                              2 responses
                              20 views
                              0 likes
                              Last Post WHICKED
                              by WHICKED
                               
                              Working...
                              X