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

Slowly running

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

    Slowly running

    Hello Josh,

    As you can see, I'm using your forum yet. Excuse me for my poor Engligh.

    I created a little indicator only to see that what I see in a txt file can be written in the Output window, but it is too slowly. That txt file has only 49 kb. It should be much faster.
    I think that perhaps it happens because it is completely read in every bar:

    protected
    overridevoid OnBarUpdate()
    {
    if (File.Exists(path))
    {
    {
    sr =
    new System.IO.StreamReader(path);
    string line;
    while ((line = sr.ReadLine())!=null)
    {
    string [] split = line.Split(new Char [] {' '});
    int splitCounter=0;
    foreach (string s in split)
    {
    splitCounter++;
    if (splitCounter==1)
    date =
    int.Parse(s);
    if (splitCounter==2)
    currentOpen=
    double.Parse(s);
    if (splitCounter==3)
    currentHigh=
    double.Parse(s);
    if (splitCounter==4)
    currentLow=
    double.Parse(s);
    if (splitCounter==5)
    currentClose=
    double.Parse(s);
    Print (date +
    " " + currentOpen);
    }
    }
    }
    }
    else
    Print ("File does not exist.");
    }

    #2
    Your assumption is correct. That is why in the reference sample we limited it to real-time bars only. Let's say your chart has 1000 bars. You are looping through hundreds of lines a thousand times before you even get to the first real-time bar. You should only read from your file when you absolutely need to.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      i need to read an external file

      thank you Josh, but i need to read from an external file.
      This is what i need to do:
      - read the date of the first column of my file
      - compare it with the date of each bar in the chart
      - plot the close value of my file in an indicator for that bar of the chart

      is there any other way without using "OnBarUpdate()"?

      thanks,
      jr.

      Comment


        #4
        If you are trying to compare it on every bar then you have to read it every bar. There is no two ways about this. How would you compare it to every bar without accessing every bar.

        What you are trying to do it is going to be slow. You are opening a file thousands of times. You are parsing hundreds of lines each time you open a file. Then you are adding thousands of draw text objects. It is not a mystery why it is slow.

        You can either change your approach to a less intensive requirement or bring down the number of bars on your chart drastically.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          another question with the same problem

          The "OnBarUpdate()" means then that if i don't have real-time (or for example it is saturday morning), i can't do anything with ninjascript because there is no bar "updating"?

          thanks again,
          jr.

          Comment


            #6
            i can do that with other programs in a different way: when i am in a bar, i look for the closest date of my file (it is ordered from oldest to newest date), plot the close like an indicator and then go to the next bar of the chart and go again to my file from the last position i was in.

            Comment


              #7
              OnBarUpdate() is called on every single bar. For historical bars OnBarUpdate() is processed once per bar. In realtime bars, depending on what you have CalculateOnBarClose set to, you will get OnBarUpdate() triggered either every single incoming tick or just once at the end of the bar.
              Josh P.NinjaTrader Customer Service

              Comment


                #8
                i'm now with yahoo. And i can't see anything in the output window. żis it happening because of the "OnBarUpdate()"?

                Comment


                  #9
                  Again, what you are trying to do is very calculation intensive. There is no two way about it. If you are opening and closing a file thousands of times. Parsing hundreds of lines each time of course it is going to be slow.
                  Josh P.NinjaTrader Customer Service

                  Comment


                    #10
                    jrbolsa,

                    You will ONLY see things when the code evaluates to true. Like I said, OnBarUpdate() is triggered once for EVERY single historical bar.
                    Josh P.NinjaTrader Customer Service

                    Comment


                      #11
                      This time it is not a question of speed. I doesn't run. I'll take a look to "CalculateOnBarClose"...

                      thanks,
                      jr

                      Comment


                        #12
                        JR,

                        I suggest you completely step back from trying to read a file at all. Just start simple. You need to grasp the concept of how NinjaScripts are processed before you should do anything else.

                        Just do a very basic indicator. In OnBarUpdate() just have one line:
                        Code:
                        Print(Time[0].ToString());
                        That will print out the timestamp of the bar it is processing. It is CRITICAL you understand this concept before proceeding to more advanced functionality.

                        Reading/writing from a file is complex C# on the fringe of being outside of the scope of what we can offer support for.
                        Josh P.NinjaTrader Customer Service

                        Comment


                          #13
                          Ok, i'll do it.
                          thanks,
                          jr.

                          Comment


                            #14
                            I know what i'm trying to do is not usual. But it's the only way to plot the cot report, like i did with other languages (VisualBasic). I am not an expert with C# although years ago I had to programm in C (when i was doing my m. degree).

                            I'll go on investigating,

                            thanks,
                            jr.

                            Comment


                              #15
                              I'll get it.
                              you gave me a great idea.
                              That's my problem: i don't know C#
                              But it will be resolved soon.
                              I only need how to scape from the while loop when i find the date of the bar of the chart.
                              For the last bars it will be take time, but not so much
                              thank you very much,
                              jr.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by judysamnt7, 03-13-2023, 09:11 AM
                              4 responses
                              57 views
                              0 likes
                              Last Post DynamicTest  
                              Started by ScottWalsh, Today, 06:52 PM
                              4 responses
                              36 views
                              0 likes
                              Last Post ScottWalsh  
                              Started by olisav57, Today, 07:39 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post olisav57  
                              Started by trilliantrader, Today, 03:01 PM
                              2 responses
                              19 views
                              0 likes
                              Last Post helpwanted  
                              Started by cre8able, Today, 07:24 PM
                              0 responses
                              9 views
                              0 likes
                              Last Post cre8able  
                              Working...
                              X