SmartQuant Discussion
http://www.smartquant.com/forums/

Can??™t write to file if have more than one active instrumen
http://www.smartquant.com/forums/viewtopic.php?f=44&t=5520
Page 1 of 1

Author:  Gary99 [ Thu Apr 26, 2007 8:17 am ]
Post subject:  Can’t write to file if have more than one active instrumen

I am able to log status to the Output window without difficulty. However, when I write to a file, it works if I have a single instrument. But if I have more than one instrument, I get the following error:

The process cannot access the file 'c:\notes111.txt' because it is being used by another process.

I imagine the other process is the first instrument. How do I produce a single file that contains my status for all of the instruments I am statusing?

Hope this makes sense.

Author:  Dr. Anton Fokin [ Thu Apr 26, 2007 8:38 am ]
Post subject: 

I suggest you make your file static, so that you have only one instance of this file. Otherwise what happens is that OpenQuant creates an instance of strategy class per instrument and, consequently, an instance of your file per instrument.

Regards,
Anton

Author:  Gary99 [ Thu Apr 26, 2007 2:38 pm ]
Post subject: 

I believe I had the variables declared static. It works as expected with one instrument, biut not more than one. Here is the code snippet:

using System;
using System.Drawing;

using OpenQuant.API;
using OpenQuant.API.Indicators;

using System.Collections.Generic;
using System.Text;
using System.IO;



public class MyStrategy : Strategy
{

static string NotePath = @"c:\notes79.txt";
static FileStream NoteFileStream;
static string previous;
static StreamReader sr;
static StreamWriter sw;



public override void OnStrategyStart()
{

if (!File.Exists(NotePath))
NoteFileStream = File.Create(NotePath);
else
NoteFileStream = File.Open(NotePath, FileMode.Open, FileAccess.ReadWrite);
sr = new StreamReader(NoteFileStream);
sw = new StreamWriter(NoteFileStream);

}

public override void OnBar(Bar bar)
{
sw.WriteLine("But " + Instrument.Symbol + " at " + bar.Median.ToString ("N2") + " on " + bar.DateTime.ToString ("MM/dd/yy hh:mm:ss tt") +
" now " + System.DateTime.Now.ToString ("MM/dd/yy hh:mm:ss tt"));


}

public override void OnStrategyStop()
{
sw.Close();
}
}

Author:  Dr. Anton Fokin [ Thu Apr 26, 2007 3:21 pm ]
Post subject: 

Think that OnStrategyStart() is called for every instrument. What you want to do is to open the file (as well as streams) only once.

Thus I suggest to add

if (File != null)
{

if (!File.Exists(NotePath))
NoteFileStream = File.Create(NotePath);
else
NoteFileStream = File.Open(NotePath, FileMode.Open, FileAccess.ReadWrite);
sr = new StreamReader(NoteFileStream);
sw = new StreamWriter(NoteFileStream);

}


}

Author:  linuxpower [ Mon Jun 11, 2012 1:02 pm ]
Post subject:  Re: Can??™t write to file if have more than one active instr

Anton,

I try to follow your code but.. doesn't works :(

public class Strat : Strategy
{
static string NotePath = @"c:\Quotes.csv";
static FileStream NoteFileStream;
static string previous;
static StreamReader sr;
static StreamWriter sw;

public override void OnStrategyStart()
{
if (File != null)
{

if (!File.Exists(NotePath))
NoteFileStream = File.Create(NotePath);
else
NoteFileStream = File.Open(NotePath, FileMode.Open, FileAccess.ReadWrite);
sr = new StreamReader(NoteFileStream);
sw = new StreamWriter(NoteFileStream);

}
}

But i have a error :

System.IO.File is a type but is used like a variable..

Have you some idea for fix ? :)

Author:  Dr. Anton Fokin [ Mon Jun 11, 2012 1:07 pm ]
Post subject:  Re: Can??™t write to file if have more than one active instr

You should create a or declare a File object I guess... :)

Author:  linuxpower [ Mon Jun 11, 2012 2:10 pm ]
Post subject:  Re: Can??™t write to file if have more than one active instr

I try :

public override void OnStrategyStart()
{
if (NotePath != null)
{

if (!File.Exists(NotePath))
NoteFileStream = File.Create(NotePath);
else
NoteFileStream = File.Open(NotePath, FileMode.Open, FileAccess.ReadWrite);
sr = new StreamReader(NoteFileStream);
sw = new StreamWriter(NoteFileStream);

}

But i have no error on the code but if i start strategy ( multi instrument) i have a problem :

The process cannot access the file 'c:\Quotes.csv' because it is being used by another process.

Author:  Dr. Anton Fokin [ Mon Jun 11, 2012 5:24 pm ]
Post subject:  Re: Can??™t write to file if have more than one active instr

Indeed, because you are trying to access the same file from several strategy instancies. Remember that OQ creates one instance of a strategy per instrument.

Regards,
Anton

Page 1 of 1 All times are UTC + 3 hours
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/