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

reading out a file

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

    reading out a file

    i want nt to use information out of a file. i read the reading example and copy the relevant part to the strategy but it wont do as intended. whats wrong?

    Code:
    #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.Indicator;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Strategy;
    #endregion
    
    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
        /// <summary>
        /// Enter the description of your strategy here
        /// </summary>
        [Description("read the file")]
        public class MyCustomStrategy : Strategy
        {
            // Variables
            
            
            // Order
            private double long_entry     = 0;
            
            // This sets the path in which the text file will be created.
            private string path         = "d:\ninjatest\order.txt";
            
            
            // Creates a StreamReader object
            private System.IO.StreamReader sr;
            
            
            
            // This method is used to configure the strategy and is called once before any strategy method is called.
            protected override void Initialize()
            {
                CalculateOnBarClose = true;
            }
    
            
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
               if (Historical)
                    return;
                // Checks to see if the file exists
                else if (File.Exists(path))
                {
                    try
                    {
                        // Sets the file the StreamReader will read from
                        sr = new System.IO.StreamReader(path);
        
                        string line;
                        
                        // Read lines and calculate the current day's OHLC from the file. While loop will go through the whole file.
                        while ((line = sr.ReadLine()) != null) 
                        {
                            string [] split = line.Split(new Char [] {' '});
                            
                            if ( line.startsWith("long_entry") )
                                long_entry = double.Parse( split[1] );
                                         
                            
                        }
                        //Print(  "entry: " + long_entry  + " target: " + long_target + " stop: " + long_stop );
                    }
                    catch (Exception e)
                    {
                        // Outputs the error to the log
                        Log("You cannot write and read from the same file at  the same time. Please remove SampleStreamWriter.",  NinjaTrader.Cbi.LogLevel.Error);
                        Print(e.ToString());
                        throw;
                    }
                    
                    
                }
                // If file does not exist, let the user know
                else
                    Print("File does not exist.");
            
                EnterLongLimit(DefaultQuantity, long_entry, "");
                
                
            } //end OnBarUpdate
            
            
            // Necessary to call in order to clean up resources used by the StreamReader object
            protected override void OnTermination()
            {
                // Disposes resources used by the StreamReader
                if (sr != null)
                {
                    sr.Dispose();
                    sr = null;
                }
            }
        }
    }

    #2
    Hello lifetime,
    Welcome to the forum and I am happy to assist you.

    I suspect the path is not correct. Instead of writing the code as
    Code:
    private string path         = "d:\ninjatest\order.txt";
    please append it as

    Code:
    private string path         = @"d:\ninjatest\order.txt";
    Please refer to this sample code which demonstrates how to read from a text file.
    JoydeepNinjaTrader Customer Service

    Comment


      #3
      working. not totally as intended but working for a start. thx :-)

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by ETFVoyageur, 04-30-2024, 02:04 PM
      7 responses
      43 views
      0 likes
      Last Post eDanny
      by eDanny
       
      Started by f.saeidi, Today, 07:07 AM
      5 responses
      14 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by Creamers, 04-27-2024, 05:32 AM
      9 responses
      60 views
      0 likes
      Last Post Creamers  
      Started by cmtjoancolmenero, 04-29-2024, 03:40 PM
      19 responses
      59 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Started by samish18, 04-17-2024, 08:57 AM
      26 responses
      116 views
      0 likes
      Last Post samish18  
      Working...
      X