Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

test from Visual studio to NT

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

    test from Visual studio to NT

    Hello,

    I am trying to make a test to implement in NT.

    Code:
    public class Program
    {
    public static void Main()
    {
    double number1 = 0;
    
    var listQ = new List<double>() {1, 3, 3.2, 4, 4.1 ,5};
    
    double i = 3.5;
    
    int loops = listQ.Count();
    double previousNumber = i;
    for(int iterator = 0; iterator<loops; iterator++){
    number1 = FindClosest(listQ, previousNumber); //this is what you have
    listQ = RemoveFromList(listQ, number1);
    Print("item " +number1);
    previousNumber = number1;
    
    }
    }
    
    public static double FindClosest(IEnumerable<double> listQ, double number)
    {
    return listQ.Aggregate((x, y) => Math.Abs(x - number) < Math.Abs(y - number) ? x : y);
    }
    
    public static List<double> RemoveFromList(List<double> listQ, double number)
    {
    return listQ.Where(a=>a!=number).ToList();
    }
    }

    Would like to replace
    Code:
    Console.WriteLine("item " +number1);
    for
    Code:
    Print("item " +number1);
    In the first case it return nada in the Output window, in the second case error:


    NinjaScript File Error Code Line Column NEWSDBA.cs Object reference is required for non-static property, method or field 'NinjaTrader.NinjaScript.NinjaScript.Print (object)' CS0120 732 4

    Any pointers?

    Thank you

    #2
    Hello frankduc,

    The code you have shown would be something you would run in visual studio and not in NinjaScript.

    If you are trying to use one of the concepts shown in that example you would need to only use that example as a reference and not copy/paste the whole thing.

    What you have shown is the basic structure of a console app in C#, I would highly suggest to stop here and use the following link to go over a console app. That is so you can see in the code you copied what would not be needed in NinjaScript.

    Create a C# console application in Visual Studio and explore some features of the Visual Studio integrated development environment (IDE).


    The void Main() is called the entry point, you could roughly equate that to one of the NinjaScript state in OnStateChange or OnBarUpdate. It depends on what your intended goal was with that sample

    The other structure like the class you have shown and static methods would need to be removed or adjusted to work in your scripts class. You already have a class, that's your Indicator or Strategy class. You already have a Main, thats your OnStateChange or OnBarUpdate. If you need the methods you had shown, you can likely copy those in however they don't need to be static. Static would be needed for the console app example because the class is static and that's how console apps work.

    MSDN has a large number of getting started type tutorials to get you well versed in making a C# application on your own. That will greatly help in NinjaScript specifically if you are finding external C# examples for console apps or other C# use cases. When looking at general C# examples you will find that in most cases its not going to be something you can just copy and paste, that's due to the context of what it was being used for. More often you will just want to look at that example and what its doing and then write code in NinjaScript where you need it making any adjustments as you go.

    Free courses, tutorials, videos, and more to learn to program in C#. Resources from the .NET team, .NET community, and training companies.



    I look forward to being of further assistance.

    Comment


      #3
      Hello,

      I did that test to produce a result where it runs multiple times the same calculation in circle. The code is running but nothing is printing. Is it the class void and private double that cause trouble?
      Of course variables are declared above in public class indicator.

      Code:
      public void Whatever()
      {
      var nearClose = 8;
      var barCount = 3;
      var results = new List<double> { nearClose };
      
      for(int i = 0; i < barCount; i++)
      {
      var result = RunCalculation(results[i]); 
      results.Add(result);
      
      Print("R"+result);
      }
      }
      
      
      
      private double RunCalculation(double nearClose)
      {
      
      return (nearClose - lowPrice0) / (Ncma - lowPrice0);
      }
      Thank you

      Comment


        #4
        Hello frankduc,

        Did you call the Whatever method from OnBarUpdate or use them in some way?

        The Print not showing up would hint that the code was not called or not run, you mentioned it was running how do you know that or how were you confirming it was running?

        I look forward to being of further assistance.

        Comment


          #5
          Jesse,

          I just removed the Whatever method and kept in OnRender the rest of script.
          Now i try to insert in OnRender the
          Code:
          private double RunCalculation(double nearClose) { return (nearClose - lowPrice0) / (Ncma - lowPrice0); }
          Any idea what could look that part in OR?

          In a small test indicator it is working but if i try to make it work in my project indicator i cant reach the private double RunCal.

          Thanks

          Comment


            #6
            Hello frankduc,

            The code you have shown would not go within OnRender, that is a private method. You can use the MSDN resources that I previously provided if you are unsure where methods go inside a C# class, that is one of the core concepts of C#.

            You mentioned it works in one indicator but not another, if that's the case you would need to compare the two scripts to see how it was used and what is different. If you don't provide the full context of how you used the code along with your question I won't know how to help here. The method alone is not enough to know what may be wrong, The context here would be the whole file because I would need to see where you put the method in the file and where you used it in the other areas of the script.

            Please let me know if I may be of additional assistance.



            Comment


              #7
              Jesse,

              The way it is presented here https://docs.microsoft.com/en-us/dot...eywords/return , its like i have no choice to use C# class to make the
              private double RunCalculation part working?

              That's the code where the sample test is working:

              Code:
              protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
              {
              var nearClose = 20;
              var barCount = 10;
              var results = new List<double> { nearClose };
              
              for(int i = 0; i < barCount; i++)
              {
              var result = RunCalculation(results[i]); 
              results.Add(result);
              
              Print("R"+result);
              }
              
              
              
              var numbers = new List<double> {};
              numbers.Add(nearclose);
              
              foreach (var item in results)
              {
              
              Print(item);
              
              }
              }
              
              
              
              
              private double RunCalculation(double nearClose)
              {
              
              return (nearClose - lowPrice0) / (Ncma - lowPrice0);
              }
              My main project is a 800 lines code. The logic is a bit different and i am asking myself if i have to move a part of that logic in the return part to make it work. Is there anyway i can move inside OnRender that part private double RunCalculation(double nearClose)?

              Thanks

              Comment


                #8
                Hello frankduc,

                That's the code where the sample test is working:
                Without knowing what you did in the other script that's not working this is not really helpful. You could use this to compare with your other script to gather whats different. If you are then unsure why we could look at that knowing specifically what you did differently.

                Is there anyway i can move inside OnRender that part private double RunCalculation(double nearClose)?
                You can do that same calculation inside OnRender without the method if you wanted to, you would just use the same math that you used inside the method. There is still code missing from what you provided so I couldnt really say what that may end up looking like, the math would be the same though (value1 - low) / (othervalue - low)

                Please let me know if I may be of additional assistance.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                563 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                329 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
                547 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                548 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X