SmartQuant Discussion

Automated Quantitative Strategy Development, SmartQuant Product Discussion and Technical Support Forums
It is currently Mon Sep 16, 2019 7:47 am

All times are UTC + 3 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: Thu Apr 26, 2007 8:17 am 
Offline

Joined: Sun Apr 08, 2007 10:41 pm
Posts: 90
Location: Denver, CO
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.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 26, 2007 8:38 am 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6816
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


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 26, 2007 2:38 pm 
Offline

Joined: Sun Apr 08, 2007 10:41 pm
Posts: 90
Location: Denver, CO
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();
}
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 26, 2007 3:21 pm 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6816
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);

}


}


Top
 Profile  
 
PostPosted: Mon Jun 11, 2012 1:02 pm 
Offline

Joined: Sun Mar 25, 2012 2:01 am
Posts: 30
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 ? :)


Top
 Profile  
 
PostPosted: Mon Jun 11, 2012 1:07 pm 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6816
You should create a or declare a File object I guess... :)


Top
 Profile  
 
PostPosted: Mon Jun 11, 2012 2:10 pm 
Offline

Joined: Sun Mar 25, 2012 2:01 am
Posts: 30
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.


Top
 Profile  
 
PostPosted: Mon Jun 11, 2012 5:24 pm 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6816
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC + 3 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group