SmartQuant Discussion

Automated Quantitative Strategy Development, SmartQuant Product Discussion and Technical Support Forums
It is currently Sun Aug 18, 2019 7:06 pm

All times are UTC + 3 hours




Post new topic Reply to topic  [ 17 posts ]  Go to page Previous  1, 2
Author Message
PostPosted: Tue Feb 14, 2012 11:55 pm 
Offline

Joined: Thu Mar 16, 2006 12:15 pm
Posts: 184
I would like to have that functionality, too.

Could you give an example how to do it the way you did it? Maybe its just me becaus I am not a good programer. Haven't used the global hashtable yet.

_________________
Expect the unexpected. May your MM/RM be with you.


Top
 Profile  
 
PostPosted: Wed Feb 15, 2012 4:51 pm 
Offline

Joined: Sun Oct 30, 2011 12:12 am
Posts: 220
I have the same need: executing some logic for a strategy (ie 1x), not for each instrument in a strategy (ie 8x if you have 8 instruments). Let's call 'N' the number of instruments associated to a strategy and say you have only 1 strategy.

Some events such as OnProviderError or OnConnected will be called N times but should conceptually be called only once. I guess other platforms manage that by adding a layer on top of the strategy (MetaStrategy or Portfolio but portfolio would be a confusing name in OQ). The Scenario level in OQ could have filled that role if it were to receive events and would give access to the Strategy instances directly. Is SmartQuant working on that?

Currently, there are complex and/or unelegant workarounds to address such issues.
If the logic can or must be executed before the strategy starts (outside of it), you can put your code in Scenario.Run(), before calling Start(). This is the case for synchronizing positions with IB for example as explained in another post.
If it must be executed within a Strategy context, then change your code so that it executes only for a given instrument (as a strategy is always associated with at least 1 instrument). To do that, I created an ugly property in my strategy:
Code:
        public bool ExecuteOnce
        {
            get
            {
                var key = "ExecuteOnceKey";

                if (!Global.ContainsKey(key))
                    Global[key] = Instrument;

                return Global[key] == Instrument;
            }
        }


This is what sals3r0 suggests in his previous post about the Global hashtable. Initially, Global is an empty collection of key/pair values.
What this code does is add a key arbitrarily named "ExecuteOnceKey" with a value equal to the current instrument.

Usage is very simple:

Code:
        public override void OnStrategyStart()
        {
            // do stuff
            // ...

            if (ExecuteOnce)
               TWSSync();

            // do stuff
            // ...
        }


Say your strategy has 2 instruments: AAPL and GOOG (added in this order).
When OnStrategyStart is called for the strategy's instance associated to AAPL, the ExecuteOnce call will set Global["ExecuteOnceKey"] to Instrument (ie AAPL) and return true. So TWSSync() will be called.
Then when OnStrategyStart is called for the strategy's instance associated to GOOG, the ExecuteOnce call will see that Global["ExecuteOnceKey"] is set to a value (APPL) different from the current Instrument (GOOG), and return false. TWSSync() won't be called.
Net result: TWSSync() is called only once per strategy, whatever N is (2, 8, 500, 2000...).

NOTE: you can use ExecuteOnce anywhere in your strategy, for example in OnBar(). You are not required to initialize anything nor are you forced to use it in OnStrategyStart: it is self-initialized the first time it is called.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 17 posts ]  Go to page Previous  1, 2

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