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

Help needed with this coding problem

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

    Help needed with this coding problem

    Hi guys,

    I am trying to code a strategy so it can read from a text file which contains date and time for entry signals.

    This is the content of the text file that contains entry signal's date and time:

    2008-10-7 8:18:32
    2008-10-7 8:22:03
    2008-10-7 8:50:17
    2008-10-7 9:41:41
    2008-10-7 9:58:14
    2008-10-7 10:16:15
    2008-10-7 10:46:51


    When I tested it, it looks like only read the even lines from the text file while skipping the odd lines, so only the following signals were entered.


    2008-10-7 8:22:03

    2008-10-7 9:41:41

    2008-10-7 10:16:15


    My programming knowledge is very limited, so can anyone help me to find out what's the cause?

    Thank you!





    Code:

    #region Using declarations
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.ComponentModel;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using NinjaTrader.Indicator;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Strategy;
    #endregion
    using System.IO;

    namespace NinjaTrader.Strategy
    {
    public class MyCustomStrategy : Strategy
    {
    #region Variables
    private string myInput0 = @"0"; // Default setting for MyInput0
    private string path = Cbi.Core.UserDataDir.ToString() + "test.txt";
    private System.IO.StreamReader sr;
    #endregion
    protected override void Initialize()
    {
    CalculateOnBarClose = true;
    }
    protected override void OnBarUpdate()
    { if (File.Exists(path))
    {try
    {
    sr = new System.IO.StreamReader(path);
    string line;
    while ((line = sr.ReadLine()) != null)
    {
    myInput0 = sr.ReadLine();
    if (myInput0==(Time[0]).ToString())
    {
    DrawArrowUp("My up arrow" + CurrentBar, false, 0, Close[0] + -3 * TickSize, Color.Yellow);
    }
    }
    }
    catch (Exception e)
    {
    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;
    }
    }
    else
    Print("File does not exist.");
    }
    public override void Dispose()
    {
    if(sr != null)
    {
    sr.Dispose();
    sr = null;
    }
    base.Dispose();
    }

    #2
    while ((line = sr.ReadLine()) != null) <----- you read line 1,3,5,... into variable line
    {
    myInput0 = sr.ReadLine(); <---- you read line 2,4,6,... into myInput0
    if (myInput0==(Time[0]).ToString())

    Comment


      #3
      Thank you Ralph for your help, really appreciate it!

      I've a new problem, I have to seperate long entry signals and short entry signals into two text files, is there any way to make the ninjascript read a singal text file like the following and determine long or short itself?

      2008-10-7 8:06:27 Long
      2008-10-7 8:18:32 Short
      2008-10-7 8:22:03 Long
      2008-10-7 8:35:23 Long
      2008-10-7 8:50:17 Short
      2008-10-7 9:07:25 Long
      2008-10-7 9:41:41 Short
      2008-10-7 10:06:01 Short



      My new code is here:

      private string path = Cbi.Core.UserDataDir.ToString() + "test.txt";
      private string path1 = Cbi.Core.UserDataDir.ToString() + "test1.txt";
      private System.IO.StreamReader sr;
      private System.IO.StreamReader sr1;

      protected override void Initialize()
      {
      CalculateOnBarClose = true;
      }
      protected override void OnBarUpdate()
      {

      sr = new System.IO.StreamReader(path);
      string line;
      while ((line = sr.ReadLine()) != null)
      {
      if (line==(Time[1]).ToString())
      {
      DrawArrowUp("My up arrow" + CurrentBar, false, 0, Low[0] + -3 * TickSize, Color.Yellow);
      }
      }
      sr1 = new System.IO.StreamReader(path1);
      string line1;
      while ((line1 = sr1.ReadLine()) != null)
      {
      if (line1==(Time[1]).ToString())
      {
      DrawArrowDown("My up arrow" + CurrentBar, false, 0, High[0] + 3 * TickSize, Color.Red);
      }
      }

      }
      Last edited by rp088; 12-11-2009, 10:18 AM.

      Comment


        #4
        rp088, you need to parse your lines in this case. You could try something like this (not tested):

        string[] tokens;
        DateTime myDT;
        ...
        tokens = line.Split(' ');
        if (DateTime.TryParse(tokens[0] + ' ' + tokens[1], out myDT))
        {Process your comparison with myDT here}
        else
        {Print("Sytax Error");}
        if (tokens[2] == "Long")
        {Process your Long entry here}
        else if (tokens[2] == "Short")
        {Process your short here}
        else
        {Print("Sytax Error");}

        Regards
        Ralph

        Comment


          #5
          Hi Ralph,

          I used your code and it works, thank you once again!

          Best regards

          rp088

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by rtwave, 04-12-2024, 09:30 AM
          2 responses
          20 views
          0 likes
          Last Post rtwave
          by rtwave
           
          Started by tsantospinto, 04-12-2024, 07:04 PM
          5 responses
          68 views
          0 likes
          Last Post tsantospinto  
          Started by cre8able, Today, 03:20 PM
          0 responses
          7 views
          0 likes
          Last Post cre8able  
          Started by Fran888, 02-16-2024, 10:48 AM
          3 responses
          49 views
          0 likes
          Last Post Sam2515
          by Sam2515
           
          Started by martin70, 03-24-2023, 04:58 AM
          15 responses
          115 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Working...
          X