The loops are to generate the signal code and are necessary. The signal code is what is going to be eventually written to disk. Below is an example of a row ZZZZZZZZZZ-ZZZZZZZZZZ-ZZZZZZZZZZ-BBAAAABBBB-ZBAAAAAAZZ-CCBCCXYZZZ-XXCXXYYZZZ-BBBBBBBBCC 1.1748 1.1748 1.17475 1.17475 10/5/2020 0:02'
private void WriteToCSV() { if(CurrentBar <= BarsRequiredToPlot + LookBack) return; StringBuilder header = new StringBuilder(); StringBuilder logEntry = new StringBuilder(); StringBuilder signalCode = new StringBuilder(); string fullPath = System.IO.Path.Combine(filePath,fileName); header.Append("Index Key,"); header.Append("CurrentBar,"); header.Append("SignalCode,"); header.Append("Open,"); header.Append("High,"); header.Append("Low,"); header.Append("Close,"); header.Append("BarCloseTime"); //INPUT1 for(int i = 1; i <= LookBack; i++) { signalCode.Append(input1_1_trendchar[i]); if(i == LookBack) signalCode.Append("-"); } for(int i = 1; i <= LookBack; i++) { signalCode.Append(input1_2_trendchar[i]); if(i == LookBack) signalCode.Append("-"); } for(int i = 1; i <= LookBack; i++) { signalCode.Append(input1_3_trendchar[i]); if(i == LookBack) signalCode.Append("-"); } for(int i = 1; i <= LookBack; i++) { signalCode.Append(input1_4_trendchar[i]); if(i == LookBack) signalCode.Append("-"); } for(int i = 1; i <= LookBack; i++) { signalCode.Append(input2_1_trendchar[i]); if(i == LookBack) signalCode.Append("-"); } for(int i = 1; i <= LookBack; i++) { signalCode.Append(input2_2_trendchar[i]); if(i == LookBack) signalCode.Append("-"); } for(int i = 1; i <= LookBack; i++) { signalCode.Append(input2_3_trendchar[i]); if(i == LookBack) signalCode.Append("-"); } for(int i = 1; i <= LookBack; i++) { signalCode.Append(input2_4_trendchar[i]); } logEntry.Append(indexKey); logEntry.Append(","); logEntry.Append(CurrentBar-1); logEntry.Append(","); logEntry.Append(signalCode.ToString()); logEntry.Append(","); logEntry.Append(Open[1]); logEntry.Append(","); logEntry.Append(High[1]); logEntry.Append(","); logEntry.Append(Low[1]); logEntry.Append(","); logEntry.Append(Close[1]); logEntry.Append(","); logEntry.Append(Time[1]); indexKey++; try { if (File.Exists(fullPath) == false) { using (StreamWriter sw = new StreamWriter(fullPath, true)) { sw.WriteLine(header); // If file doesnt exist, create it and add the Header sw.WriteLine(logEntry); // Append a new line to the file sw.Close(); // Close the file to allow future calls to access the file again. } } else //File Does Exisit { if (IsFileLocked(fullPath) == false) //If file is not locked for editing { using (StreamWriter sw = new StreamWriter(fullPath, true)) { sw.WriteLine(logEntry); // Append a new line to the file sw.Close(); // Close the file to allow future calls to access the file again. } } } } catch (Exception e) { // Outputs the error to the log //Log(uniqueStrategy + ": ExecutionLog - cannot write and read at the same time.", NinjaTrader.Cbi.LogLevel.Error); Print("ERROR WRITTING SignalLog"); } }
Comment