I want to print some values about my trades, (entryTime, exitTime, entryPrice, exitPrice etc) from my strategy to a textfile. This works for a single backtest. But I want to write multiple files for an optimization, one file for each combination of parameters. This was no problem in NT7, but in NT8 it's no longer working with my code since the optimizer runs in parallell/ asynchronous. One obvious problem is that State == State.DataLoaded is called at the beginning for all combination of parameters, that is the StreamWriter is "initialized" for all runs before it is disposed.
Is there a way to write multiple textfiles files from the optimizer in NT8 ? (Maybe with multiple StreamWriter objects?) A single textfile with all trades matched with the right parameter values would also be ok.
The StreamWriter is "initialized" when State = State.DataLoaded
else if (State == State.DataLoaded) { tradeNo = 0; // Reset if (!File.Exists(fileLocation)) { sw = File.AppendText(fileLocation ); } }
and disposed when State = State.Terminated.
The Writing is done in OnPositionUpdate()
protected override void OnPositionUpdate(....) { // WriteToFile if (SystemPerformance.AllTrades.Count > 0 && tradeNo != SystemPerformance.AllTrades.Count ) { Trade lastTrade = SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1]; tradeNo += 1; try { sw.WriteLine( parameter1 + ";" + parameter2 + ";" + lastTrade.Entry.Time + ";" + lastTrade.Exit.Time + ";" + lastTrade.Entry.Price + ";" + lastTrade.Exit.Price); } 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 StreamReader.", NinjaTrader.Cbi.LogLevel.Error); throw; } }
Comment