• 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

Most recent swing value as soon as condition appears?

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

    Most recent swing value as soon as condition appears?

    Hello, I have a condition:


    if ((CountIf(delegate {return conditionA;}, 100) >=1) && Open[0]>= Swing(5).SwingHigh[0])
    {
    EnterLong(DefaultQuantity, "entry1");

    By words: "If during last 100 bars conditionA appears, Enter Long everytime Open[0] >= last SwingHigh."

    Problem is, that I do not want to enter long above every new SwingHigh made, since conditionA appeared, but I want to enter above the concrete value of only one SwingHigh, which is last Swinghigh before conditionA appeareaed.

    Example:

    1.Lets say last SwingHigh before conditionA was met was =50.
    2. condition A appearred and Open[0] >= 50 -> EnterLong
    3. Price goes above value of 50 and makes new Swinghighs ( for example 60) ....But I want my Entry signal only if Open[0] >= 50 (not if Open [0] >= 60)

    I was thinking about using function Most recent occurrence, but do not know how to make this condition working. Can you please help me with this problem, how to combine function CounIf and MRO? Or any other way? Thanks a lot... I read about Countif, I red about MRO, but do not know how to combine it.

    #2
    Hello Tomass,

    Thanks for your post.

    If I understand your logic correctly, one approach you can take is to save the swinghigh value while ConditionA is not true.
    For example:

    if (!ConditionA) // if ConditionA is false
    {
    myEntrylevel = Swing(5).SwingHigh[0]; // save the current swing high value
    }

    Then when ConditionA becomes true you can use the last value saved while ConditionA was false.

    if (ConditionA && Open[0] >= myEntrylevel)
    {
    // Enterlong
    }
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Thank you for your asnwer, I will try to play with that, but maybe there will be problem from what I see.

      If we look at the code:

      if ((CountIf(delegate {return conditionA;}, 100) >=1) && Open[0]>= Swing(5).SwingHigh[0])
      {
      EnterLong(DefaultQuantity, "entry1");

      I need to save the most recent Swing(5).SwingHigh[0], if conditionA appears( at least once), but entries should work even if conditionA is false again...

      that is why in my eyes your condition " if (ConditionA && Open[0] >= myEntrylevel)" will not work...

      (For entries I do not need ConditionA&& Open[0] at the same time. I need to know that there was a conditionA in last X bars and since that looking only for Open >=Swing(5).SwingHigh[0](But SwingHigh with value of most recent Swinghigh in time when ConditionA appeared)...

      Hope it makes sense

      Comment


        #4
        Hello Tomass,

        Thanks for your reply.

        It is somewhat difficult to follow but I think I understand.

        I think we can agree that this code provides what you have specified, that is to save the swinghigh level before ConditionA becomes true:

        if (!ConditionA) // if ConditionA is false
        {
        myEntrylevel = Swing(5).SwingHigh[0]; // save the current swing high value
        }

        If ConditionA is not needed at the time of the entry then we can save the bar number when ConditionA occurred and compare that later to current bar to see if Condition A occured with X bars where you define what X is

        if (ConditionA)
        {
        lastCondABar = CurrentBar; // lastCondABar is an integer
        }

        Your entry then is

        if (Open[0] >= myEntryLevel && (CurrentBar - lastCondABar) < X)
        {
        // enter order
        }

        Hope this helps.

        edit: had to change from > to <
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Thanks a lot. Your advices directed me the right way and I sorted it out. Great!!

          I played with the conditions and as an EntryLevel I used :

          ( MAX(Swing(5).SwingHigh, 1000)[0]);.

          But it brings another question:

          Lets say I want to trade only between 15:30 till 21:30 -> so for all my conditions I did

          ToTime(Time[0]) >= ToTime(15, 30, 0) && ToTime(Time[0]) <= ToTime(21, 15, 0).

          Problem is, I am using Renko bars and functions MAX and CounIf...Because I can not predict number of bars, and I want to be sure, that these functions cover whole trading session from 15:30 to 21:15, I setup looking back period to 1000, just to be sure.

          so all together I have:

          ToTime(Time[0]) >= ToTime(15, 30, 0) && ToTime(Time[0]) <= ToTime(21, 15, 0).

          (CountIf(delegate {return conditionA;}, 1000) >=1)

          ( MAX(Swing(5).SwingHigh, 1000)[0]);

          But my question is: Does it mean, that these functions start counting since 15:30, or function Time to Time has no effect on them and they are looking way before 15:30 (in my example 1000 bars) back?.


          How to get to the point, that I have two functions MAX, Countif, which cover whole session, but they do not lookback before 15:30 and ?

          (lets say if todays session has only 100 bars, function Countif or MAX will give me values of previous days, because 1000 bars should be 3 days for example... and that is not what I am looking for).

          I know about function

          (ToDay(Time[0]) == ToDay (DateTime.Now)

          but problem is that with this-> strategy works only concrete date (date of my computer), which should work in live trading, but it does not work in market replay, which I need to?

          Is there any solution for both? Market replay + live?

          Thanks a lot.It is a last problem of my strategy. If I sort this out, it will be finally done . Will be glad for any advices.

          Thanks Tomas

          Comment


            #6
            Originally posted by Tomass View Post
            Thanks a lot. Your advices directed me the right way and I sorted it out. Great!!

            I played with the conditions and as an EntryLevel I used :

            ( MAX(Swing(5).SwingHigh, 1000)[0]);.

            But it brings another question:

            Lets say I want to trade only between 15:30 till 21:30 -> so for all my conditions I did

            ToTime(Time[0]) >= ToTime(15, 30, 0) && ToTime(Time[0]) <= ToTime(21, 15, 0).

            Problem is, I am using Renko bars and functions MAX and CounIf...Because I can not predict number of bars, and I want to be sure, that these functions cover whole trading session from 15:30 to 21:15, I setup looking back period to 1000, just to be sure.

            so all together I have:

            ToTime(Time[0]) >= ToTime(15, 30, 0) && ToTime(Time[0]) <= ToTime(21, 15, 0).

            (CountIf(delegate {return conditionA;}, 1000) >=1)

            ( MAX(Swing(5).SwingHigh, 1000)[0]);

            But my question is: Does it mean, that these functions start counting since 15:30, or function Time to Time has no effect on them and they are looking way before 15:30 (in my example 1000 bars) back?.


            How to get to the point, that I have two functions MAX, Countif, which cover whole session, but they do not lookback before 15:30 and ?

            (lets say if todays session has only 100 bars, function Countif or MAX will give me values of previous days, because 1000 bars should be 3 days for example... and that is not what I am looking for).

            I know about function

            (ToDay(Time[0]) == ToDay (DateTime.Now)

            but problem is that with this-> strategy works only concrete date (date of my computer), which should work in live trading, but it does not work in market replay, which I need to?

            Is there any solution for both? Market replay + live?

            Thanks a lot.It is a last problem of my strategy. If I sort this out, it will be finally done . Will be glad for any advices.

            Thanks Tomas
            Save the barNumber of when your starting time constraint is met. Then, on each bar, you can always calculate the exact number of bars since that time, by simply subtracting said barNumber from CurrentBar.

            Comment


              #7
              Thank you for advice:

              I wrote it like this:

              //into variables region:

              private int starttime

              private bool starttimecondition { get { return (ToTime(Time[0]) == ToTime(15, 30, 0)); } }


              // OnBarUpdate:

              if (starttimecondition)

              starttime = (CurrentBar);


              into MAX and Countif conditions I wrote (start time - CurrentBar) period to look back...

              However, it works for first day opened perfectly, but if I start Market replay, second day it still uses saved bar from previous day...So my conditions are still running on values of previous day not current day. I can not find what I am doing wrong that value from 15:30 is not updated as soon as condition is met. Will be glad for any advice.

              Thank you Tomas

              Comment


                #8
                Originally posted by Tomass View Post
                Thank you for advice:

                I wrote it like this:

                //into variables region:

                private int starttime

                private bool starttimecondition { get { return (ToTime(Time[0]) == ToTime(15, 30, 0)); } }


                // OnBarUpdate:

                if (starttimecondition)

                starttime = (CurrentBar);


                into MAX and Countif conditions I wrote (start time - CurrentBar) period to look back...

                However, it works for first day opened perfectly, but if I start Market replay, second day it still uses saved bar from previous day...So my conditions are still running on values of previous day not current day. I can not find what I am doing wrong that value from 15:30 is not updated as soon as condition is met. Will be glad for any advice.

                Thank you Tomas
                The number of bars that have passed would be (CurrentBar - starttime), which you have the wrong way around.

                I should think that what you want to do could be more easily coded that using CountIf, but that is just a general opinion.

                Comment


                  #9
                  Thank for your post,
                  you are right, it was my bad. It should be (CurrentBar - starttime). However, problem is still there.

                  Lets say I start market replay and leave it running for 2 days... First day it works good during my session 15:30-21:15...After that it runs overnight to hit 15:30 again next day, which should be another bar saved...(Startime should be updated). But it did not happen and my strategy works second day on values of previous day....
                  private int starttime still keeps values of first day and does not get updated when it meets its conditions next day...

                  Any idea why?

                  Second problem is - since I am using this private int condition, loading of chart takes ages... I tried to open todays YM Renko bar (looking 4 days back, to see how it was before weekend) and if I try to start my strategy, it took me 2 hours of loading of chart. I gave up and closed it after this...Before I started saving bar number it was ok?

                  Thank for all your advices. I am new to Ninjascript, but willing to learn and every problem is a new experience for me.

                  Tomas

                  Comment


                    #10
                    Originally posted by Tomass View Post
                    Thank for your post,
                    you are right, it was my bad. It should be (CurrentBar - starttime). However, problem is still there.

                    Lets say I start market replay and leave it running for 2 days... First day it works good during my session 15:30-21:15...After that it runs overnight to hit 15:30 again next day, which should be another bar saved...(Startime should be updated). But it did not happen and my strategy works second day on values of previous day....
                    private int starttime still keeps values of first day and does not get updated when it meets its conditions next day...

                    Any idea why?
                    What happens depends a lot on what you are processing and where.
                    • Is this in realtime/Replay, historical chart, Strategy Analyzer?
                    • What kind of bars are you processing on?

                    Those would be the major 2 considerations as to how your code gets executed.
                    Second problem is - since I am using this private int condition, loading of chart takes ages... I tried to open todays YM Renko bar (looking 4 days back, to see how it was before weekend) and if I try to start my strategy, it took me 2 hours of loading of chart. I gave up and closed it after this...Before I started saving bar number it was ok?

                    Thank for all your advices. I am new to Ninjascript, but willing to learn and every problem is a new experience for me.

                    Tomas
                    2 hours sounds excessive. I should think that you have a different problem than merely using an int to record a barCondition.

                    Comment


                      #11
                      I use Market Replay connection and using "play forward" button. I use Renko (UniRenko) charts.

                      With slow speed - I was thinking this function CountIf maybe slows it down? I will try to rewrite is somehow, not to use Countif, but maybe setup my conditions using this "save bar" integer...We will see how it goes...

                      Comment


                        #12
                        Hello Tomass,

                        Thanks for your posts.

                        For your time condition, instead of looking at a very specific exact time, allow yourself to use a time range.

                        For example, in the OnBarUpdate() method:

                        if (ToTime(Time[0]) >= ToTime(15, 30, 0) && ToTime(Time[0]) <= ToTime(21, 15, 0))
                        {
                        // execute all your code here when the time is between 15:30 and 21:15
                        }

                        Here are other references to time filtering: http://ninjatrader.com/support/forum...ead.php?t=3226
                        Paul H.NinjaTrader Customer Service

                        Comment


                          #13
                          I think I found the problem.

                          //into variables region:

                          private int starttime

                          private bool starttimecondition { get { return (ToTime(Time[0]) == ToTime(15, 30, 0)); } }


                          // OnBarUpdate:

                          if (starttimecondition)

                          starttime = (CurrentBar);

                          Problem is, I am using Renko Bars. And sometimes there is a situation, when there is no bar at 15:30, which seems there is no new start time values (start time does not update) and my conditions still works with values of previous day....

                          Is there any other way how to save Current bar (bar at 15:30) but with Renko bars?

                          I tried to use session manager and Bars.FirstBarOfSession... But it is not a solution, because in that case, there is always gap in chart and my indicators need to be counted without that gap....

                          All I am looking for is a way how to save current bar in current time (15:30) with Renko bars, without using session manager... It does not have to be exactly 15:30, but lets say first bar which appears after 15:30....
                          ?

                          Any advice please?

                          Comment


                            #14
                            Sorry for last comment, it was my bad.
                            I realized session manager works...

                            If I do setup for example:

                            start session: Monday 15:30
                            end session : Tuesday 15:30

                            it works now finally . Time to test it to be sure strategy works...
                            Thank you for all your advices and help...
                            There is always something new to learn.

                            Tomas

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by newuser, Today, 08:37 AM
                            0 responses
                            5 views
                            0 likes
                            Last Post newuser
                            by newuser
                             
                            Started by bc24fl, Today, 06:51 AM
                            1 response
                            4 views
                            0 likes
                            Last Post NinjaTrader_Jim  
                            Started by U0000999, Yesterday, 05:17 AM
                            1 response
                            15 views
                            0 likes
                            Last Post NinjaTrader_Kate  
                            Started by asmmbillah, Today, 05:03 AM
                            5 responses
                            31 views
                            0 likes
                            Last Post NinjaTrader_PaulH  
                            Started by lordvader34, Today, 06:18 AM
                            1 response
                            9 views
                            0 likes
                            Last Post NinjaTrader_PaulH  
                            Working...
                            X