Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Holding values in IF statements.

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

    Holding values in IF statements.

    Hi I have a newbie question.


    if (condition1)
    {
    value1=1;
    }

    Sets value1 to 1, but whenever condition1=false value1 is automatically set to 0.

    I did not write

    if (condition1)
    {
    value1=1;
    }
    else
    {
    value1=0;
    }

    So why is value1 always 0 when condition1=false?

    I can get value1 to remain 1 until I change it in easylanguage, but can't seem to do it in ninjascript.

    Thanks in advance.

    #2
    Welcome to our forums iansane, how do you initialize the value of your Value1 variable?

    Comment


      #3
      Originally posted by NinjaTrader_Bertrand View Post
      Welcome to our forums iansane, how do you initialize the value of your Value1 variable?
      Thanks Bertrand.

      By initialize i hope you mean :

      int value1 = 0;

      Comment


        #4
        Correct, so the value is 0 if your condition to change it does not trigger.

        Comment


          #5
          when my condition changes it, does it remain changed or revert back to 0.

          i would like it to remain changed and store the new value until later

          Edit: have uploaded an example


          ideally the entire dotted line should be green until value1 is reverted back to 0 through another condition.
          Attached Files
          Last edited by iansane; 02-22-2012, 05:27 AM.

          Comment


            #6
            If you have no other command to reset, it would hold the value once set.

            Do you have the declaration globally under Variables?

            Comment


              #7
              value1 i refer to is trend in the code below
              also, since i am new, i have not touched anything other than this code and another moving average one.

              #region Using declarations
              using System;
              using System.ComponentModel;
              using System.Diagnostics;
              using System.Drawing;
              using System.Drawing.Drawing2D;
              using System.Xml.Serialization;
              using NinjaTrader.Cbi;
              using NinjaTrader.Data;
              using NinjaTrader.Gui.Chart;
              #endregion
              // This namespace holds all indicators and is required. Do not change it.
              namespace NinjaTrader.Indicator
              {
              /// <summary>
              /// Enter the description of your new custom indicator here
              /// </summary>
              [Description("Enter the description of your new custom indicator here")]
              public class Testtrend : Indicator
              {
              #region Variables
              // Wizard generated variables
              private int strength = 5; // Default setting for Strength
              // User defined variables (add any user defined variables below)
              #endregion
              /// <summary>
              /// This method is used to configure the indicator and is called once before any bar data is loaded.
              /// </summary>
              protected override void Initialize()
              {
              Add(new Plot(Color.FromKnownColor(KnownColor.Gray), PlotStyle.Dot, "Phigh"));
              Add(new Plot(Color.FromKnownColor(KnownColor.Gray), PlotStyle.Dot, "Plow"));
              Overlay = true;
              }
              /// <summary>
              /// Called on each bar update event (incoming tick)
              /// </summary>
              protected override void OnBarUpdate()
              {
              int barsAgo=0;
              int barsAgo2=0;
              double swinghigh=0;
              double swinglow=0;

              Swing(Strength).SwingHighBar(barsAgo,1,Strength+1) ;
              Swing(Strength).SwingLowBar(barsAgo2,1,Strength+1) ;


              swinghigh=Math.Max(High[HighestBar(High,Strength+1)],Swing(Strength).SwingHigh[0]);
              swinglow=Math.Min(Low[LowestBar(Low,Strength+1)],Swing(Strength).SwingLow[0]);
              Phigh.Set(swinghigh);
              Plow.Set(swinglow);


              ///trend section
              bool uptrend=false;
              double downtrend=0;
              double chigh= High[HighestBar(High,Strength+1)];
              double clow= Low[LowestBar(Low,Strength+1)];

              int high1bar=Swing(Strength).SwingHighBar(barsAgo,1,10 0);
              int high2bar=Swing(Strength).SwingHighBar(barsAgo,2,10 0);
              int high3bar=Swing(Strength).SwingHighBar(barsAgo,3,10 0);
              int high4bar=Swing(Strength).SwingHighBar(barsAgo,4,10 0);

              int low1bar=Swing(Strength).SwingLowBar(barsAgo,1,100) ;
              int low2bar=Swing(Strength).SwingLowBar(barsAgo,2,100) ;
              int low3bar=Swing(Strength).SwingLowBar(barsAgo,3,100) ;
              int low4bar=Swing(Strength).SwingLowBar(barsAgo,4,100) ;

              double high1=High[high1bar];
              double high2=High[high2bar];
              double high3=High[high3bar];
              double high4=High[high4bar];

              double low1=Low[low1bar];
              double low2=Low[low2bar];
              double low3=Low[low3bar];
              double low4=Low[low4bar];


              //uptrend conditions
              if ((chigh>high1 && low1>low2 && low1bar<high1bar && high1bar<low2bar && clow>low1) || (high1>high2 && low1>low2 && clow>low1 && high1bar<low1bar && low1bar<high2bar && high2bar<low2bar))
              {
              trend=1;

              }

              if (trend==1)
              {
              PlotColors[0][0] =Color.Green;
              PlotColors[1][0] =Color.Green;
              }



              }
              #region Properties
              [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
              [XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
              public DataSeries Phigh
              {
              get { return Values[0]; }
              }
              [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
              [XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
              public DataSeries Plow
              {
              get { return Values[1]; }
              }
              [Description("")]
              [GridCategory("Parameters")]
              public int Strength
              {
              get { return strength; }
              set { strength = Math.Max(1, value); }
              }
              #endregion
              }
              }
              #region NinjaScript generated code. Neither change nor remove.
              // This namespace holds all indicators and is required. Do not change it.
              namespace NinjaTrader.Indicator
              {
              public partial class Indicator : IndicatorBase
              {
              private Testtrend[] cacheTesttrend = null;
              private static Testtrend checkTesttrend = new Testtrend();
              /// <summary>
              /// Enter the description of your new custom indicator here
              /// </summary>
              /// <returns></returns>
              public Testtrend Testtrend(int strength)
              {
              return Testtrend(Input, strength);
              }
              /// <summary>
              /// Enter the description of your new custom indicator here
              /// </summary>
              /// <returns></returns>
              public Testtrend Testtrend(Data.IDataSeries input, int strength)
              {
              if (cacheTesttrend != null)
              for (int idx = 0; idx < cacheTesttrend.Length; idx++)
              if (cacheTesttrend[idx].Strength == strength && cacheTesttrend[idx].EqualsInput(input))
              return cacheTesttrend[idx];
              lock (checkTesttrend)
              {
              checkTesttrend.Strength = strength;
              strength = checkTesttrend.Strength;
              if (cacheTesttrend != null)
              for (int idx = 0; idx < cacheTesttrend.Length; idx++)
              if (cacheTesttrend[idx].Strength == strength && cacheTesttrend[idx].EqualsInput(input))
              return cacheTesttrend[idx];
              Testtrend indicator = new Testtrend();
              indicator.BarsRequired = BarsRequired;
              indicator.CalculateOnBarClose = CalculateOnBarClose;
              #if NT7
              indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
              indicator.MaximumBarsLookBack = MaximumBarsLookBack;
              #endif
              indicator.Input = input;
              indicator.Strength = strength;
              Indicators.Add(indicator);
              indicator.SetUp();
              Testtrend[] tmp = new Testtrend[cacheTesttrend == null ? 1 : cacheTesttrend.Length + 1];
              if (cacheTesttrend != null)
              cacheTesttrend.CopyTo(tmp, 0);
              tmp[tmp.Length - 1] = indicator;
              cacheTesttrend = tmp;
              return indicator;
              }
              }
              }
              }
              // This namespace holds all market analyzer column definitions and is required. Do not change it.
              namespace NinjaTrader.MarketAnalyzer
              {
              public partial class Column : ColumnBase
              {
              /// <summary>
              /// Enter the description of your new custom indicator here
              /// </summary>
              /// <returns></returns>
              [Gui.Design.WizardCondition("Indicator")]
              public Indicator.Testtrend Testtrend(int strength)
              {
              return _indicator.Testtrend(Input, strength);
              }
              /// <summary>
              /// Enter the description of your new custom indicator here
              /// </summary>
              /// <returns></returns>
              public Indicator.Testtrend Testtrend(Data.IDataSeries input, int strength)
              {
              return _indicator.Testtrend(input, strength);
              }
              }
              }
              // This namespace holds all strategies and is required. Do not change it.
              namespace NinjaTrader.Strategy
              {
              public partial class Strategy : StrategyBase
              {
              /// <summary>
              /// Enter the description of your new custom indicator here
              /// </summary>
              /// <returns></returns>
              [Gui.Design.WizardCondition("Indicator")]
              public Indicator.Testtrend Testtrend(int strength)
              {
              return _indicator.Testtrend(Input, strength);
              }
              /// <summary>
              /// Enter the description of your new custom indicator here
              /// </summary>
              /// <returns></returns>
              public Indicator.Testtrend Testtrend(Data.IDataSeries input, int strength)
              {
              if (InInitialize && input == null)
              throw new ArgumentException("You only can access an indicator with the default input/bar series from within the 'Initialize()' method");
              return _indicator.Testtrend(input, strength);
              }
              }
              }
              #endregion

              Comment


                #8
                iansane, sorry I don't exactly follow you, this script would not even compile without trend as variable being declared first under variables for example :

                private int trend = 0;

                Comment


                  #9
                  Originally posted by iansane View Post
                  Thanks Bertrand.

                  By initialize i hope you mean :

                  int value1 = 0;
                  Where is the statement located?

                  Comment


                    #10
                    Thanks everyone, sorry about the confusion i pasted an edited version that had not been compiled.

                    the problem was i did not initialize properly.

                    variables:
                    private int trend=0;

                    fixed the problem

                    Thanks again!

                    Comment


                      #11
                      Instead of using a variable to set the value to x if a certain condition is met, can you put in say the number 1? I can't seem to get it to work for my statement. See the following code. Thank you.

                      Code:
                      			oneUnit 			= 	if(Math.Floor(((AccountRiskPercent/100) * currentAccountSize)/Close[0]) < 1)
                      									1
                      									else Math.Floor(((AccountRiskPercent/100) * currentAccountSize)/Close[0]);

                      Comment


                        #12
                        cfree5119, please post your new issue in a separate and new thread on our forums.

                        Thanks,

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                        0 responses
                        579 views
                        0 likes
                        Last Post Geovanny Suaza  
                        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                        0 responses
                        334 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by Mindset, 02-09-2026, 11:44 AM
                        0 responses
                        101 views
                        0 likes
                        Last Post Mindset
                        by Mindset
                         
                        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                        0 responses
                        554 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by RFrosty, 01-28-2026, 06:49 PM
                        0 responses
                        551 views
                        1 like
                        Last Post RFrosty
                        by RFrosty
                         
                        Working...
                        X