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 gbourque, Today, 06:39 AM
            2 responses
            14 views
            0 likes
            Last Post gbourque  
            Started by rexsole, Today, 08:39 AM
            0 responses
            4 views
            0 likes
            Last Post rexsole
            by rexsole
             
            Started by trilliantrader, Yesterday, 03:01 PM
            3 responses
            31 views
            0 likes
            Last Post NinjaTrader_Clayton  
            Started by cmtjoancolmenero, Yesterday, 03:58 PM
            4 responses
            26 views
            0 likes
            Last Post cmtjoancolmenero  
            Started by Brevo, Today, 01:45 AM
            1 response
            14 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Working...
            X