SmartQuant Discussion
http://www.smartquant.com/forums/

Check if position open on a symbol across many strategies
http://www.smartquant.com/forums/viewtopic.php?f=42&t=10406
Page 1 of 1

Author:  ghfj555 [ Tue Dec 04, 2012 2:06 am ]
Post subject:  Check if position open on a symbol across many strategies

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

Author:  kkkwj [ Tue Dec 04, 2012 6:38 am ]
Post subject:  Re: Check if position open on a symbol across many strategie

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.

Author:  ghfj555 [ Tue Dec 04, 2012 7:49 pm ]
Post subject:  Re: Check if position open on a symbol across many strategie

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.

Author:  kkkwj [ Tue Dec 04, 2012 8:26 pm ]
Post subject:  Re: Check if position open on a symbol across many strategie

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.

Author:  ghfj555 [ Tue Dec 04, 2012 9:15 pm ]
Post subject:  Re: Check if position open on a symbol across many strategie

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

Author:  ghfj555 [ Wed Dec 05, 2012 6:59 pm ]
Post subject:  Re: Check if position open on a symbol across many strategie

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?

Author:  kkkwj [ Wed Dec 05, 2012 7:16 pm ]
Post subject:  Re: Check if position open on a symbol across many strategie

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) {
...
}

Author:  ghfj555 [ Wed Dec 05, 2012 8:38 pm ]
Post subject:  Re: Check if position open on a symbol across many strategie

Changed it the way you suggested and it does not give me that error any more. Thank you, again!

Page 1 of 1 All times are UTC + 3 hours
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/