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

Saving drawings to .csv using StreamWriter

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

    Saving drawings to .csv using StreamWriter

    Hi, i am trying save all rectangles drawings from the user to a csv file using StreamWriter sample, i get the foreach (DrawingTool draw in DrawObjects.ToList()) code in help sample but when i save the file i get repeated data, i cant find the problem.

    Here is the code



    namespace NinjaTrader.NinjaScript.Indicators
    {
    public class SampleStreamWriter : Indicator
    {
    private string path;
    private StreamWriter sw; // a variable for the StreamWriter that will be used
    private bool UserDrawing;
    private string RectangleTag;
    private double RectangleStartPrice;
    private double RectangleEndPrice;
    private DateTime RectangleStartTime;
    private DateTime RectangleEndTime;


    protected override void OnStateChange()
    {
    if(State == State.SetDefaults)
    {
    Calculate = Calculate.OnBarClose;
    Name = "Sample stream writer";
    path = NinjaTrader.Core.Globals.UserDataDir + "MyTestFile.txt"; // Define the Path to our test file
    }
    // Necessary to call in order to clean up resources used by the StreamWriter object
    else if(State == State.Terminated)
    {
    if (sw != null)
    {
    sw.Close();
    sw.Dispose();
    sw = null;
    }
    }
    }

    protected override void OnBarUpdate()
    {

    // Loops through the DrawObjects collection via a threadsafe list copy
    sw = File.AppendText(path); // Open the path for writing
    foreach (DrawingTool draw in DrawObjects.ToList())
    {

    UserDrawing = draw.IsUserDrawn;
    //Print("******************" + UserDrawing);


    if (UserDrawing == true && draw is DrawingTools.Rectangle)
    {
    // Indicates if this is a manually drawn or script generated line
    Print("Line Object: " + draw.Tag + " Manually Drawn: " + draw.IsUserDrawn);
    DrawingTools.Rectangle temp = draw as DrawingTools.Rectangle;
    RectangleTag = draw.Tag;
    RectangleStartPrice = temp.StartAnchor.Price;
    RectangleEndPrice = temp.EndAnchor.Price;
    RectangleStartTime = temp.StartAnchor.Time;
    RectangleEndTime = temp.EndAnchor.Time;
    sw.WriteLine(RectangleTag + "," + RectangleStartPrice + "," + RectangleEndPrice + "," + RectangleStartTime + "," + RectangleEndTime); // Append a new line to the file
    }
    }


    sw.Close(); // Close the file to allow future calls to access the file again.
    }
    }
    }​


    Thanks a lot
    Attached Files

    #2
    Hi MJGutierrez, thanks for posting. The code is running in OnBarUpdate, so its running each time a bar closes. You can wrap the drawing code within a CurrentBar check so it only runs this code at the second to last bar on the chart:

    if ((Calculate == Calculate.OnBarClose) && (CurrentBar == Bars.Count-2))
    {
    //write to csv here.
    }
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Thanks a lot for your help i made the changes but i have a problem, the file is not saving the draw objects, the test line is saved but the draw objects not






      protected override void OnBarUpdate()
      {

      if ((Calculate == Calculate.OnBarClose) && (CurrentBar == Bars.Count - 2))
      {
      ChartPeriod = Convert.ToString(BarsPeriod);

      if (ChartPeriod.Contains("Daily"))
      {
      ChartPeriod = "24";
      FileName = "1D";
      }

      if (ChartPeriod.Contains("240"))
      {
      ChartPeriod = "4";
      FileName = "4H";
      }

      if (ChartPeriod.Contains("60"))
      {
      ChartPeriod = "60";
      FileName = "1H";
      }

      if (ChartPeriod.Contains("15"))
      {

      ChartPeriod = "15";
      FileName = "15M";
      }

      if (ChartPeriod.Contains("5"))
      {
      ChartPeriod = "5";
      FileName = "5M";
      }

      if (ChartPeriod.Contains("1"))
      {
      ChartPeriod = "1";
      FileName = "1M";
      }

      FileStream f = new FileStream("c:\\WebDrive\" + FileName + ".csv", FileMode.Create);
      StreamWriter s = new StreamWriter(f);

      s.WriteLine("WRITE TEST");


      foreach (DrawingTool draw in DrawObjects.ToList())
      {

      if (draw is DrawingTools.Rectangle && draw.IsUserDrawn)
      {
      // Indicates if this is a manually drawn or script generated line
      Print("Line Object: " + draw.Tag + " Manually Drawn: " + draw.IsUserDrawn);
      DrawingTools.Rectangle temp = draw as DrawingTools.Rectangle;
      RectangleTag = draw.Tag;
      RectangleStartPrice = temp.StartAnchor.Price;
      RectangleEndPrice = temp.EndAnchor.Price;
      RectangleStartTime = temp.StartAnchor.Time;
      RectangleEndTime = temp.EndAnchor.Time;
      s.WriteLine("@NQ#BOX" + ";" + RectangleStartTime + ";" + RectangleStartPrice + ";" + RectangleEndTime + ";" + RectangleEndPrice + ";" + ChartPeriod); // Append a new line to the file
      }
      }


      s.Close();
      f.Close();
      Print("File created successfully...");

      }

      }​
      Attached Files

      Comment


        #4
        Hi, thanks for the follow up. The time I can spend verifying custom code will be limited. The first suggestion that may work is to switch the drive to local hard drive and not a web drive. There is also an example indicator that takes chart data and saves it to a CSV file here:

        This indicator will write all of the chart’s historical bar data and indicator data to a CSV type file that can then be imported into a spreadsheet. To use this indicator, add it to a chart and (critical) wait until all indicators have finished reloading (no longer shows (Calculating…)). Click the green button in the […]


        Kind regards,
        -ChrisL
        Chris L.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by helpwanted, Today, 03:06 AM
        0 responses
        4 views
        0 likes
        Last Post sarafuenonly123  
        Started by Brevo, Today, 01:45 AM
        0 responses
        7 views
        0 likes
        Last Post Brevo
        by Brevo
         
        Started by aussugardefender, Today, 01:07 AM
        0 responses
        5 views
        0 likes
        Last Post aussugardefender  
        Started by pvincent, 06-23-2022, 12:53 PM
        14 responses
        242 views
        0 likes
        Last Post Nyman
        by Nyman
         
        Started by TraderG23, 12-08-2023, 07:56 AM
        9 responses
        384 views
        1 like
        Last Post Gavini
        by Gavini
         
        Working...
        X