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

Program shuts down after if statement

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

    Program shuts down after if statement

    In OnBarUpdate() im am doint the folowing
    else{
    if(Close[1]>Close[2] && Open[0]>Close[1])//++
    Sum(0);
    if(Close[1]<Close[2] && Open[0]>Close[1])//-+
    Sum(1);
    if(Close[1]>Close[2] && Open[0]<Close[1])//+-
    Sum(2);
    if(Close[1]<Close[2] && Open[0]<Close[1])//--
    Sum(3);
    if(Close[2]<Close[3] && Close[1]<Close[2] && Open[0]<Close[1])//---
    Sum(4);
    if(Close[2]<Close[3] && Close[1]<Close[2] && Open[0]>Close[1])//--+
    Sum(5);
    if(Close[2]<Close[3] && Close[1]>Close[2] && Open[0]<Close[1])//-+-
    Sum(6);
    if(Close[2]<Close[3] && Close[1]>Close[2] && Open[0]>Close[1])//-++
    Sum(7);
    if(Close[2]>Close[3] && Close[1]<Close[2] && Open[0]<Close[1])//+--
    Sum(8);
    if(Close[2]>Close[3] && Close[1]<Close[2] && Open[0]>Close[1])//+-+
    Sum(9);
    if(Close[2]>Close[3] && Close[1]>Close[2] && Open[0]<Close[1])//++-
    Sum(10);
    if(Close[2]>Close[3] && Close[1]>Close[2] && Open[0]>Close[1])//+++
    Sum(11);
    if(Close[3]>Close[4] && Close[2]>Close[3] && Close[1]>Close[2] && Open[0]>Close[1])//++++
    Sum(12);

    Everything works fine until i add the last row that has 4 conditions. I dont get any errors when compiling but the strategy is not starting and when i try to see what is wrong it seems like OnBarUpdate() dont gets called at all when i ad this row. (came to this conclution by using a Print("test") in OnBarUpdate() and nothing got printed on the screen)

    #2
    zwoop,

    I suspect you may have ran into the concept discussed here: http://www.ninjatrader-support2.com/...ead.php?t=3170
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Thanks that was the error.

      Now i have a new error with a new strategy.
      First i read that the Initialize() method is always only run once. But when i put a Print() statement in i can see two outputs in the same window. And i do use ClearOutputWindow(); first.

      Secound in this program i only want to run code in the Initialize() function to print some output while im decoding some math functions. This is the code:
      protected override void Initialize()
      {
      CalculateOnBarClose = true;
      ClearOutputWindow();

      bars = 10;

      range.Add(102.54);
      range.Add(103.78);
      range.Add(99.98);
      range.Add(98.28);
      range.Add(99.26);
      range.Add(103.59);
      range.Add(100.76);
      range.Add(100.39);
      range.Add(99.07);
      range.Add(102.26);
      range.Add(101.35);

      for(int i = bars; i>-1; i--){
      range[0] = Math.Log(range[i]/range[i-1]);
      Print(range[i].ToString());
      }
      for(int i = bars; i>-1; i--){

      Print(range[i].ToString());
      }

      }

      This code compiles fine but it has huge problems running. While i pick it in StrategyAnalyzer the dropdown changes to this strategy but non of the value fields update. And when i run a different strategy in run. The strategy that is run is the one that was highlighted before i pich this one. The label fild is not updating so something is very wrong here.

      Comment


        #4
        zwoop,

        Initialize() is not only ran once. It is guaranteed to run at least once prior to your strategy run. If you have code that you only want to run once I suggest you put it into OnBarUpdate() with a filter like if (CurrentBar == 0) to do the calculation once.

        Please see your Control Center logs for errors in regards to your Strategy Analyzer run.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Josh View Post
          ...Initialize() is not only ran once...
          Josh,

          your statement above is confusing, I would call it incorrect. Initialize() is called once only per Strategy/Indicator-instance. But a strategy/indicator is instantiated several times before one single instance reaches the "active" mode and processes bar-data.

          Regards
          Ralph

          Comment


            #6
            I didn't read the entire thread but maybe try OnStartUp()?:

            Comment


              #7
              You are right, mountainclimber. I don't want to stress this isue too much because it has been improved in NT7.

              Regards
              Ralph

              Comment


                #8
                Thanks all it is working fine now.

                Comment


                  #9
                  Finaly got started on my historical vol indicator but the pan is empty. Also when i try to do simpe output to debug nothing happens so must be somthing wrong. Also the new Line... syntax is not even shown in the pan. Below is the relevant code.

                  #region Variables
                  // Wizard generated variables
                  private int range = 100; // Default setting for Range
                  // User defined variables (add any user defined variables below)
                  List<double> priceList = new List<double>();
                  double mean1 = 0.0;
                  double mean2 = 0.0;
                  double stdDEV = 0.0;
                  double hv = 0.0;
                  #endregion

                  protected override void Initialize()
                  {
                  Add(new Plot(Color.FromKnownColor(KnownColor.SpringGreen), PlotStyle.Line, "HistVol"));
                  Add(new Line(Color.FromKnownColor(KnownColor.DarkOliveGree n), 0.5, "50%"));
                  CalculateOnBarClose = true;
                  Overlay = false;
                  PriceTypeSupported = true;
                  }

                  protected override void OnBarUpdate()
                  {
                  for(int i = range; i>0; i--){
                  priceList.Add(Math.Log(Close[i-1]/Close[i]));
                  mean1 = mean1+priceList[range-i];
                  }
                  mean1 = mean1/range;

                  for(int i = 0; i<range; i++){
                  priceList[i] = priceList[i]-mean1;
                  priceList[i] = Math.Pow(priceList[i],2);
                  mean2 = mean2+priceList[i];
                  }
                  stdDEV = Math.Sqrt(mean2/(range-1));
                  hv = stdDEV*Math.Sqrt(252);
                  HistVol.Set(hv);
                  priceList.Clear();
                  mean1 = 0.0;
                  mean2 = 0.0;
                  }

                  Comment


                    #10
                    zwoop,

                    Please check your Control Center logs for errors. I suspect you are likely running into this: http://www.ninjatrader-support2.com/...ead.php?t=3170
                    Josh P.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Segwin, 05-07-2018, 02:15 PM
                    14 responses
                    1,789 views
                    0 likes
                    Last Post aligator  
                    Started by Jimmyk, 01-26-2018, 05:19 AM
                    6 responses
                    837 views
                    0 likes
                    Last Post emuns
                    by emuns
                     
                    Started by jxs_xrj, 01-12-2020, 09:49 AM
                    6 responses
                    3,293 views
                    1 like
                    Last Post jgualdronc  
                    Started by Touch-Ups, Today, 10:36 AM
                    0 responses
                    13 views
                    0 likes
                    Last Post Touch-Ups  
                    Started by geddyisodin, 04-25-2024, 05:20 AM
                    11 responses
                    63 views
                    0 likes
                    Last Post halgo_boulder  
                    Working...
                    X