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

File.AppendText causing severe hair loss

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

    File.AppendText causing severe hair loss

    Below is a section of code that writes to a file just fine so long as the else statement is remarked out. If it is left in, all I get is a 0 byte file. What am I doing wrong?

    Thanks a mil


    try
    {
    if(dExp.WCCIPattern[0] != 0)
    {
    sw = File.AppendText(@FilePath);
    // If file at 'path' doesn't exist it will create the file. If it does exist it will append the file.
    if(dExp.WCCIDirection[0] == 1)
    nextOpen = Close[0] + TickSize;
    else
    nextOpen = Close[0] - TickSize;

    sw.Write("\r\n"+Time[0].ToString("M-dd-yyy")+","+Time[0].ToString("HH:mm")+","+Pattern+","+dExp.WCCIDirect ion+","+","+nextOpen);
    sw.Flush();
    sw.Close();
    }
    else
    if(dExp.WCCIPattern[1] != 0)
    {
    sw = File.AppendText(@FilePath);
    if(dExp.WCCIDirection[1] == 1)
    {
    sw.Write(","+(Open[0] - Low[0])/TickSize+","+(High[0]-Open[0])/TickSize);
    sw.Flush();
    sw.Close();
    }
    else
    {
    sw.Write(","+(High[0] - Open[0])/TickSize+","+(Open[0]-Low[0])/TickSize);
    sw.Flush();
    sw.Close();
    }
    }
    }
    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 SampleStreamReader.", NinjaTrader.Cbi.LogLevel.Error);
    throw;
    }

    #2
    Shouldn't that be "else if (XXX)" since it looks like you have

    if ()
    {}
    else
    {}
    else
    {}
    RayNinjaTrader Customer Service

    Comment


      #3
      Found the problem. It was that blasted [1] look back. Grrrrrrrrrrrrrrrrrrrr.

      Comment


        #4
        Could someone please show me how to write it out so that FIRST it wipes out any data in the file, and then appends all the new data into it? Been on the C+ zone but just can't find this out.

        Comment


          #5
          Unfortunately I have no sample for this case available cclsys, couldn't you use FileAppendAllText, to create new file each and then point to it?

          Provides static methods for the creation, copying, deletion, moving, and opening of a single file, and aids in the creation of FileStream objects.
          BertrandNinjaTrader Customer Service

          Comment


            #6
            I'm far from an expert and am kinda guessing here, but using the File.AppendText(@FilePath) to add data, it would seem that File.Delete(@<filename>) (or something like it) would delete the file.

            See http://www.csharpfriends.com/Article...x?articleID=54

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by trilliantrader, 04-18-2024, 08:16 AM
            4 responses
            17 views
            0 likes
            Last Post trilliantrader  
            Started by mgco4you, Today, 09:46 PM
            1 response
            6 views
            0 likes
            Last Post NinjaTrader_Manfred  
            Started by wzgy0920, Today, 09:53 PM
            0 responses
            7 views
            0 likes
            Last Post wzgy0920  
            Started by Rapine Heihei, Today, 08:19 PM
            1 response
            9 views
            0 likes
            Last Post NinjaTrader_Manfred  
            Started by Rapine Heihei, Today, 08:25 PM
            0 responses
            9 views
            0 likes
            Last Post Rapine Heihei  
            Working...
            X