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

Comparing one value with the previous and aplying IF

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

    Comparing one value with the previous and aplying IF

    Hello,

    I am having a lot of problems with a Stop Loss indicator I am trying to develop.

    I have been able to plot the calculations (It just calculates the minimum using MIN function of the last X bars, and draws that line.

    However, I want the plot NOT to go down until I set a reset condition, so I need to compare each calculated data with the previous days data, and if it is smaller, use the previous day value. And this applies for everyday of course

    Eg.

    if (stoplosstoday<stoplossyesterday)
    then
    stoploss=stoplosstoday
    else
    stoploss=stoplossyesterday

    I had a lot of problems, specially because double ans series variables get mixed.

    Can you give me some guidance on how to work this out?

    Thank you

    #2
    Originally posted by user1986 View Post
    Hello,

    I am having a lot of problems with a Stop Loss indicator I am trying to develop.

    I have been able to plot the calculations (It just calculates the minimum using MIN function of the last X bars, and draws that line.

    However, I want the plot NOT to go down until I set a reset condition, so I need to compare each calculated data with the previous days data, and if it is smaller, use the previous day value. And this applies for everyday of course

    Eg.

    if (stoplosstoday<stoplossyesterday)
    then
    stoploss=stoplosstoday
    else
    stoploss=stoplossyesterday

    I had a lot of problems, specially because double ans series variables get mixed.

    Can you give me some guidance on how to work this out?

    Thank you
    Your pseudocode does not match your verbal description. Which one is it?

    Comment


      #3
      Hello User1986,

      Thank you for your post.

      You stated that if the current day is less than the previous than use the previous day, however your logic suggests that you use the current day for stoploss when this occurs and the opposite when this isn't true.
      Cal H.NinjaTrader Customer Service

      Comment


        #4
        You are right, should be something like this

        if (stoplosstoday<stoplossyesterday)
        then
        stoploss=stoplossyesterday
        else
        stoploss=stoplosstoday

        Anyhow, the problem is quite generic, as it seems I am not able to figure out how to compare the values at all and then plot the whole string of values generated through the comparison.

        Thanks for the quick reply.

        Comment


          #5
          I have even tryied to plot new variables created by displaced closed prices, and I have only been abple to do it looking at the future rather than the past.

          oput1=MAX(10)[-1];
          Plot0.Set(oput1);
          that works


          oput1=MAX(10)[1];
          Plot0.Set(oput1);
          Doesn't

          EDIT:::
          I just found this post!!!


          This helps fix the problem of not being able to call for the bar.
          Still I need some help on the comparison shown below.
          Last edited by user1986; 02-06-2015, 02:04 PM.

          Comment


            #6
            Originally posted by user1986 View Post
            You are right, should be something like this

            if (stoplosstoday<stoplossyesterday)
            then
            stoploss=stoplossyesterday
            else
            stoploss=stoplosstoday

            Anyhow, the problem is quite generic, as it seems I am not able to figure out how to compare the values at all and then plot the whole string of values generated through the comparison.

            Thanks for the quick reply.
            Code:
            double StopValue = Math.Max(stoplosstoday, stoplossyesterday);

            Comment


              #7
              Originally posted by koganam View Post
              Code:
              double StopValue = Math.Max(stoplosstoday, stoplossyesterday);

              Thank you koganam, but perhaps I am not being clear.

              I need that to happen to every day in the data series, so
              if day n is smaller than day n-1 it takes the value of n-1
              at the same time if n-1 is smaller than n-2, it takes the value of n-2

              and so on till the start of the string.

              The final objective is similar to a trailing stop that never goes down, only up.

              I hope this is more clear.

              Comment


                #8
                Originally posted by user1986 View Post
                Thank you koganam, but perhaps I am not being clear.

                I need that to happen to every day in the data series, so
                if day n is smaller than day n-1 it takes the value of n-1
                at the same time if n-1 is smaller than n-2, it takes the value of n-2

                and so on till the start of the string.

                The final objective is similar to a trailing stop that never goes down, only up.

                I hope this is more clear.
                That is what that does. You just have to do it everyday, which it will, because you are editing a data stream, not a static sample space.

                Comment


                  #9
                  Oh ok, If a new value comes in, it will run thorugh that logic.

                  but for historical data, it does not work when I open a chart and try to apply the indicator.

                  Comment


                    #10
                    Originally posted by user1986 View Post
                    Oh ok, If a new value comes in, it will run thorugh that logic.

                    but for historical data, it does not work when I open a chart and try to apply the indicator.
                    Historicity has nothing to do with the matter. An OnBarUpdate is an OnBarUpdate. Granted there is only one per bar on historical bars, but why should that matter? Especially, as you seem to be talking about daily bars anyway.

                    If your indicator does not do what you want, that is because your indicator is not coded in accordance with what you want to do, not because Math.Max() does not provide the correct value.

                    You asked a specific question, and that specific question was answered. Why your indicator seems to not be doing what you expect, is a completely different kettle of fish.
                    Last edited by koganam; 02-06-2015, 03:26 PM.

                    Comment


                      #11
                      I misunderstood your previous post.
                      I guess the real question here is: "How can I make it so that the operation is performed on every daily data, and that change impacts the next data calculation"

                      I was trying to work on something of this fashion,creating a new dataseries by going through the first one sequentially, but still nothing

                      for (int i=50;i>0;i--)
                      if (MAX(5)[i]<MAX(5)[i+1])
                      { maxim1[i]=maxim1[(i+1)]; }
                      else
                      { maxim1[i]=MAX(5)[i]; }

                      Originally posted by koganam View Post
                      If your indicator does not do what you want, that is because your indicator is not coded in accordance with what you want to do,
                      Of that I am sure and that is what I am trying to fix
                      Last edited by user1986; 02-06-2015, 03:52 PM.

                      Comment


                        #12
                        Well I think after this weekend that the forum was not accessible, some posts got lost...

                        Anyhow, I wanted to thank Koganam who patiently tried to understand my posts and questions an collaborate on the answer, even when sometimes my post where just me thinking out loud.

                        So far this is what I got. Does pretty much what I want, but I am still working on some tweaks. First thing is to hide all values before "buy date" because they mess up with the Y-scale (by setting minimum to zero).

                        Any suggestions on improving the code are of course welcome.

                        Originally posted by Code
                        //+++++// Protection for crashing when on the first value of the chart and wants to look back to compare previous point.
                        if (CurrentBar < 2)
                        return;

                        //+++++// Calculation of SL by local minimum method

                        double slMin = MIN(minPeriod)[0];

                        //+++++// Calculation of SL by Chandelier exit method

                        double slChand = (MAX(chandPeriod)[0]) - ((ATR(22)[0])*chandMult);

                        //+++++// Calculation of SL by Manual input

                        double slMan = manValue;



                        //+++++// Calculation of FINAL SL

                        Value[0] = Math.Max((Math.Max(slMin,slChand)),slMan);

                        if (ToDay(Time[0])>buyDate)
                        {
                        if (Value[0]<Value[1])
                        {
                        slFinal=Value[1];
                        }
                        else
                        {
                        slFinal=Value[0];
                        }
                        }
                        else
                        {
                        Value [0] = 0;
                        }



                        //+++++// Fill dataseries for graph
                        MySLL.Set(slFinal);

                        Comment


                          #13
                          else
                          {
                          Value [0] = 0;
                          }
                          You are explicitly setting that to zero. Yet you seem to be saying that that is a problem. If so, then why are you setting it to zero?

                          Comment


                            #14
                            Yes, I set it to zero as I could have set it to any other value, but the idea would be to make it hidden if possible.

                            Due to the way the code is designed now, it has to be a lower value than whatever the value of the indicator will be at the buy date. And that value will not be lower than zero .

                            I do not know if ninjascript will allow for an "empty" or null value, or if that would create a problem, or how to hide all portion in the plot where date<buydate.


                            EDIT:
                            Apparently this does the trick, still need to test it a little bit.
                            Code:
                            			if ((ToDay(Time[0]))>buyDate)
                            				{
                            					if (Value[0]>Value[1])
                            						{
                            						slFinal=Value[0];
                            						}
                            					else
                            						{
                            						slFinal=Value[1];
                            						}
                            				}
                            			else
                            				{
                            				slFinal = Close[0];
                            				PlotColors[0][0] = Color.Transparent;
                            				}
                            Last edited by user1986; 02-09-2015, 11:35 AM.

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by mgco4you, Today, 09:46 PM
                            1 response
                            2 views
                            0 likes
                            Last Post NinjaTrader_Manfred  
                            Started by wzgy0920, Today, 09:53 PM
                            0 responses
                            3 views
                            0 likes
                            Last Post wzgy0920  
                            Started by Rapine Heihei, Today, 08:19 PM
                            1 response
                            8 views
                            0 likes
                            Last Post NinjaTrader_Manfred  
                            Started by Rapine Heihei, Today, 08:25 PM
                            0 responses
                            6 views
                            0 likes
                            Last Post Rapine Heihei  
                            Started by f.saeidi, Today, 08:01 PM
                            1 response
                            9 views
                            0 likes
                            Last Post NinjaTrader_Manfred  
                            Working...
                            X