SmartQuant Discussion

Automated Quantitative Strategy Development, SmartQuant Product Discussion and Technical Support Forums
It is currently Thu Oct 01, 2020 9:45 am

All times are UTC + 3 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Sun Nov 25, 2012 7:34 pm 
Offline

Joined: Wed Nov 21, 2007 8:29 pm
Posts: 47
Hi Anton and Alexei,

It is essential for my code to be able to review its instruments during the start of a strategy and make changes to the IM. To do this, I use your very nice features to remove instruments from the IM and restart the strategy. This is easy to do, but unfortunately, the following code causes OQ to crash every time:

scenario.cs:
Code:
using System;

using OpenQuant.API;
using OpenQuant.API.Engine;

public class MyScenario : Scenario
{
   public override void Run()
   {
      foreach (Instrument imInstrument in InstrumentManager.Instruments)
      {
         if (!Solution.Projects[0].Instruments.Contains(imInstrument.Symbol))
         Solution.Projects[0].Instruments.Add(imInstrument);
      }

      StartOver = false;
      Start();

      StartOver = true;
      return;
   }
}


project code:
Code:
using System;
using System.Drawing;
using System.Collections;

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

public class MyStrategy : Strategy
{
   bool reEntryToStart = false;
   
   public override void OnStrategyStart()
   {
      // OnStrategyStart() is called for each instrument in the strategy, but we only need to execute once.
      if (reEntryToStart == true)
      {
         return;
      }
      else
      {
         reEntryToStart = true;
      }
      
      // The following code simply deletes an instrument from the InstrumentManager
      IEnumerator iEnum = InstrumentManager.Instruments.GetEnumerator();
      iEnum.MoveNext();
      if (iEnum.Current == null) throw new InvalidOperationException("No Instrument to remove for test");
      InstrumentManager.Remove((Instrument)iEnum.Current);
      return;      
   }
}


To demonstrate this, create a solution with the solution code and project code. Ensure that many Instruments are available in the InstrumentManager. Add at least one instrument to the strategy so that it will start. Now run the program. All instruments not present in the strategy will be added, then a random one will be deleted, all during the onStrategyStart event. OpenQuant crashes with this exception:

Code:
System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous)
   at System.Windows.Forms.Control.Invoke(Delegate method, Object[] args)
   at OpenQuant.Runner.SendRequests()
   at OpenQuant.Runner.Do()
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()


I have tried calling DataManager.Flush(), and setting ResetOnStart to both true and false, nothing makes it work. Can you please help?

Thanks,

David


Top
 Profile  
 
PostPosted: Mon Nov 26, 2012 1:17 pm 
Offline

Joined: Wed Oct 08, 2003 1:06 pm
Posts: 833
Hi,

The problem is that you are removing an instrument from the InstrumentManager, while this instrument stays as one of the instruments of you project. That's why it throws an exception.

Please describe why you need to remove an instrument and we will try to choose the best place in the code for it.

Regards,
Sergey.


Top
 Profile  
 
PostPosted: Mon Nov 26, 2012 6:05 pm 
Offline

Joined: Wed Nov 21, 2007 8:29 pm
Posts: 47
Sergey, thanks for the reply.

I don't think that's the problem: in my actual code, at each restart cycle, the code is this:

Code:
Solution.Projects[0].Instruments.Clear();
   
foreach (Instrument imInstrument in InstrumentManager.Instruments)
{
   Solution.Projects[0].Instruments.Add(imInstrument);
}


...so there should never be an instrument that remains in the strategy that isn't in the InstrumentManager.

I have a fully automated system that discovers new equity and option issues and manages the contents of the IM and the strategy automatically. Leaving obsolete instruments in the IM just slows everything down. Clearly, you intended for it to be possible to remove instruments -- since the API call is provided -- perhaps you can let me know how to use it such that it won't crash?

Thanks,

David


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 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