SmartQuant Discussion

Automated Quantitative Strategy Development, SmartQuant Product Discussion and Technical Support Forums
It is currently Sun Oct 25, 2020 11:23 pm

All times are UTC + 3 hours




Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: Sat Nov 07, 2009 2:05 pm 
Offline

Joined: Sat Sep 26, 2009 12:08 am
Posts: 62
Location: Ireland
Simple question: How can I interact with OQ while it is running (change a value, etc)?

Please provide a sample solution to:
1. Intergrate a windows form and controls into an OQ strategy (for example, a check box to enable automatic trading, text boxes to enter quantity and instrument symbols, control button to manually buy, sell or close all positions, a list box to display error or broker messages, etc.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 11, 2009 3:50 pm 
Offline
Site Admin

Joined: Thu Jul 17, 2003 10:39 am
Posts: 1478
It is not possible to integrate any window form into OQ UI, but you are able to develop the form in msvs, compile it to dll, refer this dll to the strategy and then create an instance of your form in the code, show it and interact with it.

_________________
SmartQuant Development Team


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 11, 2009 5:23 pm 
Offline

Joined: Sat Sep 26, 2009 12:08 am
Posts: 62
Location: Ireland
Sounds a little too complicated for a newbie. Is there a sample anywhere? If OQ is a .NET thing then forms should be easy.
Any other simple way to interact with OQ? Can I change a property in the property window?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 11, 2009 6:19 pm 
Offline
Site Admin

Joined: Thu Jul 17, 2003 10:39 am
Posts: 1478
Hmm...
There is an example how to do it...
Attached zip is MSVS project which contains win form with Qty field.
1. put MyControlLib.dll to OQ binary folder
(usually C:\Program Files\SmartQuant Ltd\OpenQuant 2\Bin)
2. refer this dll to the strategy
(in OQ menu->Tools->Options->Projects and Solutions->Build tab)
3. use the following code to get the value of Qty field in form at every bar.
Code:
using System;
using System.Drawing;
using System.Windows.Forms;

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

using MyControlLib;

public class MyStrategy : Strategy
{
   private QtyForm form;
   
   public override void OnStrategyStart()
   {
      form = new QtyForm();
      
      System.Threading.ThreadPool.QueueUserWorkItem(delegate(object state)
         {
            Application.Run(form);
         });
   }

   public override void OnBar(Bar bar)
   {
      Console.WriteLine(form.GetQty());
   }
}



Attachments:
MyControlLib.zip [15.67 KiB]
Downloaded 545 times

_________________
SmartQuant Development Team
Top
 Profile  
 
 Post subject: How to use forms with OQ
PostPosted: Wed Nov 11, 2009 9:20 pm 
Offline

Joined: Sat Sep 26, 2009 12:08 am
Posts: 62
Location: Ireland
That works great! Thanks a lot!

Just a minor change needed to correct a MSVS load error:
In the MyControlLib.csproj file change the line
Code:
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
to
Quote:
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />


Top
 Profile  
 
PostPosted: Fri Sep 14, 2012 8:28 am 
Offline

Joined: Thu Jun 08, 2006 3:56 pm
Posts: 537
Location: BC Canada
Some helpful comments for others who might try out this example zip file in the future.

First, I had problems (unhandled exceptions in the OQ solution file) when I was too lazy to copy the sample dll to the OQ/bin installation directory. I don't know why. OQ said it couldn't reference or load the dll in the Debug/bin directory where I built it. Maybe VStudio had a file handle to it or something, I don't know.

I eventually shut down the VS and OQ apps (OQ died, of course, and wouldn't load it's start page afterwards (sigh), and reverted the solution file (thank heavens for source code control!), copied the dll to the OQ installation bin directory, and then ran an OQ strategy with the little MyControl form code in place.

Presto! 3 of the forms popped up, and I couldn't figure out why. The reason is that I had 3 instruments defined in the strategy I was using, and OQ creates an instance of OnStrategyStart for each instrument... so I get three windows when the simulation starts up.

I was running trades for input, so my Print statement was in OnTrade, which caused a very looooong and slow sequence (thousands) of print statements in my output window. Spinning the number box on the form for the instrument that was running worked great. You could see the changes instantly. But spinning the others had no apparent effect, since their instruments had not yet started to run, so their OnTrade event handlers were not yet being called, and their Print statements were not being run.

I solved this by adding a bit of code to only print a number every few hundred trades, so my 3-instrument strategy finished in reasonable time, showing 3 different numbers that I had set in the 3 different spin controls on the 3 different forms.

Finally, the way the sample code is written, the forms on the separate threads are never reclaimed when the OQ simulation finishes. So I ended up with a pile of little zombie forms that I had to manually close to get rid of them. Maybe using a sequence like the following would be helpful to overcome the small issues I had with the example windows hanging around.

Code:
// in OnStrategyStart
Thread f = new Thread (the form)
f.Start()
...
// in OnStrategyStop
f.Abort()  // reclaim the form


Having said all this, the little MyControl example does definitely show that it's possible to get a separate Windows Form to interact with running simulations (or live trading runs). The flexibility of OQ is very nice, no question. Thanks Alex!


Top
 Profile  
 
PostPosted: Fri Sep 14, 2012 9:29 am 
Offline

Joined: Thu Jun 08, 2006 3:56 pm
Posts: 537
Location: BC Canada
This link shows how to move data from OQ to the external Form, using delegates, which are required for the OQ thread to WRITE to the form that is on a separate thread.

http://www.smartquant.com/forums/viewtopic.php?f=37&t=4749&p=33791#p33791

At this point, this thread shows how to READ from controls on the form (using the MyControl.zip file above), and provides a link to an example of how OQ can WRITE data to the form thread using delegates, for display on the form.

Now I'm wondering if and how it would be possible for the form to be the boss initiating process, to READ from an OQ object on demand of the form (eg click a button to fetch a data value from some OQ variable), and if/how the form could WRITE to the OQ (parent) thread on demand (click a button to WRITE something into an OQ object). I don't know enough C# yet to do that sort of magic, but I imagine it would look a lot like the solutions above, except in reverse, with the form somehow having references to variables and delegates in the parent OQ code.


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

All times are UTC + 3 hours


Who is online

Users browsing this forum: No registered users and 1 guest


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