Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

i need help to print numbers as strings, and with the correct number of decimals.

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

    i need help to print numbers as strings, and with the correct number of decimals.





    people with nt,


    i have been trying for a number of weeks now to get nt to generate text files as logs for events of interest. i received very little assistance from nt support but i have continued to work hard on this objective and have been able to make good progress. actually, i have been watching videos and going though microsoft's courses to learn to code in c# and it has been very helpful for this matter.


    at this point i have been able to get streamwriter to work acceptably in nt.

    Code:
    [INDENT]private void recordevent ( string filepath )     [/INDENT][INDENT]{[/INDENT][INDENT] [/INDENT][INDENT=2]filepath = string.Format(@"{0}ntlogs {1} {2} {3} {4} {5}.txt", Pridir, Symtic, DateTime.Now.ToString("yyyyMMdd HHmmss"), Castyp, Close[0]);[/INDENT][INDENT=2]
    Print(filepath);
    sw = new StreamWriter(filepath);  
    sw.AutoFlush = true;  
    sw.WriteLine(message);      
    sw.Close();   
    [/INDENT][INDENT]}[/INDENT]

    from everything i have studied, it seems to me that nt implicitly converts the number Close[0] to string in the fragment above as it has been working correctly for me.


    however, there are several other numerical inputs and variables (some are integer, some are decimal type) i want to be able to print to my text files using this same string.Format(@"...", method. in these cases, implicit conversion by c# will not be good enough. ¿how can i command nt to convert decimal or integer values to string? ¿and very importantly, how can i define the number of decimals that will be printed? ¿and, is it possible to define the number of decimals to be printed as a user defined input instead of hard coding it?




    very well, i hope i will receive far more effective assistance from nt support this time. thanks, regards.

    #2
    Hello rtwave,

    While the NinjaTrader staff cannot debug a script on your behalf or write code for you, we can detail how documented NinjaScript items work.

    I do have an example that writes bar information to file that you may use as an example.
    Citizens of the NinjaTrader Community, A common question we hear from clients is 'why are results from backtest different from real-time or from market replay?'. Live orders are filled on an exchange with a trading partner on an agreed upon price based on market dynamics. Backtest orders are not using these market dynamics.


    To convert an integer to a string use <int>.ToString().
    To convert a double to a string use <double.ToString().

    This would fall under general C# and is not specific to NinjaScript. Integers, doubles, and strings are all common to C#.
    Below are public links to the Microsoft documentation.
    https://docs.microsoft.com/en-us/dotnet/api/system.int32.tostring?view=netframework-4.8#System_Int32_ToString
    https://docs.microsoft.com/en-us/dotnet/api/system.double.tostring?view=netframework-4.8


    Also, below are public links to the Microsoft documentation on format strings.
    Learn how to create a custom numeric format string to format numeric data in .NET. A custom numeric format string has one or more custom numeric specifiers.

    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      I hope you understand that no one will write and compile code for you? On this forum there are a huge number of articles, which describe in detail how to solve this problem. I can't throw off the link, because I don't remember when I saw that post. I had a similar problem, I needed to display the numbers of 500 random numbers in the range from 0 to 10000. Each time the maximum number that was displayed was 244. I don't know what it was connected with, but now this is my least favorite number, although it has a very cool value which you can find out about here https://www.sunsigns.org/angel-number-244-meaning/. The only thing that helped me was to completely rewrite the entire code, slightly changing the algorithm ..
      Last edited by VictorNorth9; 08-22-2020, 12:26 AM.

      Comment


        #4
        Hello VictorNorth9,

        Welcome to the NinjaTrader forums!

        Let me know if you have any questions on this topic.
        Chelsea B.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Irukandji, Today, 09:34 AM
        1 response
        3 views
        0 likes
        Last Post NinjaTrader_Clayton  
        Started by RubenCazorla, Today, 09:07 AM
        1 response
        5 views
        0 likes
        Last Post RubenCazorla  
        Started by TraderBCL, Today, 04:38 AM
        3 responses
        25 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by WeyldFalcon, 08-07-2020, 06:13 AM
        11 responses
        1,423 views
        0 likes
        Last Post jculp
        by jculp
         
        Started by BarzTrading, Today, 07:25 AM
        2 responses
        29 views
        1 like
        Last Post BarzTrading  
        Working...
        X