SmartQuant Discussion

Automated Quantitative Strategy Development, SmartQuant Product Discussion and Technical Support Forums
It is currently Wed Aug 15, 2018 3:49 am

All times are UTC + 3 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: Tue Dec 04, 2012 2:06 am 
Offline

Joined: Fri Nov 30, 2012 11:16 pm
Posts: 10
Hi,

I have 3 strategies trading same symbol within same solution. I don't want a strategy to open position on this symbol if there is already position opened by another strategy. How do I check that? If I understand it right "if !HasPosition" would not work here since it checks positions opened by one strategy only?

Thank you


Top
 Profile  
 
PostPosted: Tue Dec 04, 2012 6:38 am 
Offline

Joined: Thu Jun 08, 2006 3:56 pm
Posts: 537
Location: BC Canada
Yes, HasPosition is a per-strategy check.

To do any kind of communication between strategies, you must use the global hashtable. In your particular scenario, each strategy would keep its current positions recorded in (your) custom datastructure in the global hashtable, where all the other strategies could check that info before they opened their own positions, and would of course update their own info in the global place after they changed their own position.


Top
 Profile  
 
PostPosted: Tue Dec 04, 2012 7:49 pm 
Offline

Joined: Fri Nov 30, 2012 11:16 pm
Posts: 10
Thank you for reply! Do you have any sample code on those global hashtables or any link to where I could get more info about them? I dont remember anything like that covered in OpenQuant pdf documentation.


Top
 Profile  
 
PostPosted: Tue Dec 04, 2012 8:26 pm 
Offline

Joined: Thu Jun 08, 2006 3:56 pm
Posts: 537
Location: BC Canada
Here's a link to a small example. The global hashtable is just a typical C# datastructure (probably a Dictionary), so you code it in a normal way. You can search the net for many examples of C# Dictionary usage examples.

http://www.smartquant.com/forums/viewtopic.php?f=61&t=7861&p=31595&hilit=global+hashtable+code#p31595

The biggest issue that you might have with the Global table is casting types that you fetch out of the table into specific data types so you can use them again. The table stores "objects" (so you can store anything in there). So when you fetch something out of the table, you need to know what type of thing you are fetching, so you can cast it back to the right kind of object with the "is" operator, the "as" operator, or with the "(cast-to-type)" format.


Top
 Profile  
 
PostPosted: Tue Dec 04, 2012 9:15 pm 
Offline

Joined: Fri Nov 30, 2012 11:16 pm
Posts: 10
Thank you kkwj. I did some more search on this forum and found other link with example on hashtable global variable. Will try it out.

viewtopic.php?f=60&t=6527&hilit=global+variable


Top
 Profile  
 
PostPosted: Wed Dec 05, 2012 6:59 pm 
Offline

Joined: Fri Nov 30, 2012 11:16 pm
Posts: 10
Ok, I found sample code and trying to do the following:

1) On strategy start set global variable to 0 allowing position entry:

public override void OnStrategyStart()

Global["SymbolFlag"] = 0;


Then within a strategy I am checking status of that variable to see if we can open position:

public override void OnQuote(Quote quote)

if ( !HasPosition && (double)Global["SymbolFlag"] == 0 )


The code compiles fine but then when solution starts returns error:


System.InvalidCastException: Specified cast is not valid.
at MyStrategy.OnQuote(Quote quote) in c:\Users\Administrator\Documents\OpenQuant\Projects\mystrat\code.cs:line 120
at OpenQuant.Trading.StrategyRunner.SetNewQuote(Instrument instrument, Quote quote)

Line 120 is exactly that:

if ( !HasPosition && (double)Global["SymbolFlag"] == 0 )

What might be causing error?


Top
 Profile  
 
PostPosted: Wed Dec 05, 2012 7:16 pm 
Offline

Joined: Thu Jun 08, 2006 3:56 pm
Posts: 537
Location: BC Canada
Why are you trying to cast the expression (Global[..] == 0), which returns a bool value T/F?

I'd say remove the (double) cast, because you need a bool value in the "if(bool expressions only)" clause.

For my taste, I would also not put the Global[] expression in the if. You'd have nicer, clearer code with no data type issues if you wrote it this way:

Code:
int myposition = Global[key];
if (!HasPosition && myposition == 0) {
...
}


Top
 Profile  
 
PostPosted: Wed Dec 05, 2012 8:38 pm 
Offline

Joined: Fri Nov 30, 2012 11:16 pm
Posts: 10
Changed it the way you suggested and it does not give me that error any more. Thank you, again!


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