SmartQuant Discussion

Automated Quantitative Strategy Development, SmartQuant Product Discussion and Technical Support Forums
It is currently Thu Sep 19, 2019 8:19 am

All times are UTC + 3 hours




Post new topic Reply to topic  [ 17 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: Tue Apr 07, 2009 10:17 am 
Offline

Joined: Sat Jun 07, 2008 1:41 pm
Posts: 47
Location: Munich, Germany
I have several projects stacked in one solution. Every project-code includes a MaxDailyloss I access through Portfolio.GetValue(). One system stops without disturbing other systems.

What I now need is an additional project "MasterRiskOfWholeSolution" which accesses the overall equity of all projects together to get a maximum loss for my whole trading-system-portfolio to stop everything.

The overall equity I can watch at solution -> view performance. There the equity of the whole project-portfolio is shown. My question now is how can I access it to use it as maximum daily loss for the whole portfolio? Or is there a proper way to define a maximimum loss (not for one project) for all projects within my solution?

It would be good if somebody can help
Jens


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 07, 2009 5:08 pm 
Offline

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

Currently there is no way to get access to the Solution portfolio.

You can try to calculate statistics you need by accessing portfolios of all projects. But you should put the portfolios in the Global hashtable in the OnStrategyStart method first.

Hope this helps.
Sergey.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 11, 2009 6:26 pm 
Offline

Joined: Thu Jun 08, 2006 3:56 pm
Posts: 537
Location: BC Canada
Hi Sergey, what is the purpose of the Global hashtable that you speak of? Also could you please post a code example of how to use the "Global hashtable" as the designers (you SQ guys) intended? Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 13, 2009 4:27 pm 
Offline

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

Please see the following example:

Add the following lines in the OnStrategyStart method:
Code:
if (!Global.ContainsKey("portfolios"))
         Global["portfolios"] = new System.Collections.ArrayList();
      
      (Global["portfolios"] as System.Collections.ArrayList).Add(Portfolio);


this code adds an array list to the Global hashtable and fills it with Portfolios of all your projects. After that you can access all your portfolios in any other project. Put the following lines in OnStrategyStop method if you like to print CoreEquity of all your portfolios:
Code:
   foreach (Portfolio portfolio in Global["portfolios"] as System.Collections.ArrayList)
         Console.WriteLine(portfolio.GetTotalEquity());


Regards,
Sergey.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 17, 2009 5:21 pm 
Offline

Joined: Sat Jun 07, 2008 1:41 pm
Posts: 47
Location: Munich, Germany
Thanks Sergey,

but how can I give this value back to my project (to switch it off)?

foreach (Portfolio portfolio in Global["portfolios"] as System.Collections.ArrayList)
Console.WriteLine(portfolio.GetTotalEquity());

That's fine. But then I want to set a variable e.g. from zero to one. This variable should now be accessable in my projects I use to stop trading.

I'm not familiar with system.*, so how can I do this?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 17, 2009 5:49 pm 
Offline

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

You can access Global hashtable in every project. So you can put some value to let's say Global["MyVar"] in one project and get it (and set again if needed) in another.

The example above puts an array (of type System.Collections.ArrayList) into Global["portfolios"]. Each project adds its own porfolio to this array.

After that you can get any project portfolio by accessing the array in Global["portfolios"]. If you want to turn off a project - i would suggest you to put let's say "1" in Global[NameOfProjectToStop] in MasterRiskOfWholeSolution and check the Global[[NameOfProjectToStop]in every project before sending orders (to see if the project is still enabled).

Regards,
Sergey.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 23, 2009 5:08 pm 
Offline

Joined: Sat Jun 07, 2008 1:41 pm
Posts: 47
Location: Munich, Germany
I do not make it from the synthax. Can somebody please help me? It is a problem of typecasting/new instances where I stuck in 3 cases. What I want to do is:


This is the trading-system as one project.
Code:
   public override void OnStrategyStart()
   {
      PuL = PuL_old = 0;
      Global ["portfolios"] += Portfolio.GetValue();  //This is the first, how to add the P&L of this project
   }

   public override void OnBar(Bar bar)
   {
      //------------------ bar-calculation (every bar) --------------------------
      //something else
      PuL = Portfolio.GetValue();       //Get current P&L of this system/project
      if (PuL != PuL_old)
      {
         double tmp2 = PuL - PuL_old;      //Calculate change in P&L
         //Global ["portfolios"] += tmp2;    //This needs to be changed to C# and leads me to problems. But this is what I want to do.
         PuL_old = PuL;                   
      }



In my MasterRiskModule I do something like this:
Code:
   public override void OnStrategyStart()
   {
      Global ["StopAllSystems"] = 0;
   }

   public override void OnBar(Bar bar)
   {
      if (DoOnlyOnce == 0)
      {
         DoOnlyOnce = 1;
         MaxEquity = Start = CurrentWertEquity = (double) Global ["portfolios"];
      }
      else
      {   
         CurrentWertEquity = (double) Global ["portfolios"];
      

         if (CurrentWertEquity < Start- MaxDailyLossPortfolio)
            Global ["StopAllSystems"] = 1;

         if (CurrentWertEquity > MaxEquity)
            MaxEquity = CurrentWertEquity;      
      
      }
   }


Back in the project above I have a variable when set to 1 disable this system/project.
Code:
   public override void OnBar(Bar bar)
   {
      //------------------ bar-calculation (every bar) --------------------------
      // something else
      MaxDailyLossReached = (double) Global ["StopAllSystems"];  // This is a problem, too.


Maybe someone can give the right synthax?

Another question would be if I run into realtime-problems: When I run e.g. 8 projects with one MastRiskProject, I always add all equities together in OnStrategyStart. Can it happen that OnStrategyStart in one project can come later then the first OnBar of my MasterRiskProject?

Thanks
Jens


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 24, 2009 11:02 am 
Offline

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

Please use:
Quote:
Global ["portfolios"] += Portfolio.GetValue(); //This is the first, how to add the P&L of this project
Global ["portfolios"] = (double)Global ["portfolios"] + Portfolio.GetValue();
Quote:
//Global ["portfolios"] += tmp2; //This needs to be changed to C# and leads me to problems. But this is what I want to do.
Global ["portfolios"] = (double)Global ["portfolios"] + tmp2;
Quote:
MaxDailyLossReached = (double) Global ["StopAllSystems"]; // This is a problem, too.
What kind of problems do you have here? this code should be compiled successfully (if the MaxDailyLossReached declared correctly)


Quote:
Another question would be if I run into realtime-problems: When I run e.g. 8 projects with one MastRiskProject, I always add all equities together in OnStrategyStart. Can it happen that OnStrategyStart in one project can come later then the first OnBar of my MasterRiskProject?
You can't be sure which OnStrategyStart method is called last. I would suggest to create a static method and call it after all your OnStrategyStart methods get called. You can use something like:
Code:
public override void OnStrategyStart()
{
...

if (!Global.Contains("CallNumber"))
    Global.Contains("CallNumber") = 0;

Global["CallNumber"] = (int)Global["CallNumber"] + 1;

if (Global["CallNumber"] == NumberOfProjects)
   InitializeMyMasterRiskProject();
}

...

public static void InitializeMyMasterRiskProject()
{
...
}
}


Regards,
Sergey.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 24, 2009 11:33 am 
Offline

Joined: Sat Jun 07, 2008 1:41 pm
Posts: 47
Location: Munich, Germany
Thanks Sergey,

I can compile it but when I start the solution in live-mode I get an "Strategy Error - System.NullReferenceException" (at this lines of code).

I don't really know why this happens.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 24, 2009 4:42 pm 
Offline

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

It is hard to say why it happens. Maybe I can help if you post a full source code of the projects you are trying to run.

Regards,
Sergey.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 28, 2009 5:11 am 
Offline

Joined: Thu Jun 08, 2006 3:56 pm
Posts: 537
Location: BC Canada
The error might be caused by this code:

Code:
if (!Global.Contains("CallNumber"))
    Global.Contains("CallNumber") = 0;


which should probably be this instead:

Code:
if (!Global.Contains("CallNumber"))
    Global["CallNumber"] = 0;


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 21, 2009 8:53 pm 
Offline

Joined: Thu Jun 08, 2006 3:56 pm
Posts: 537
Location: BC Canada
Just a warning for anyone who reads this thread--I do not believe that using Portfolio.GetValue() (or any of the Portfolio.GetTotalEquity() etc calls) is a reliable way for an individual strategy to monitor PnL progress, when using multiple strategy projects within one solution.

Here is a link that explains what is happening, and why the Portfolio.XXX methods are only valid if you have only 1 strategy in 1 solution. This is what I call "the daily profit target problem."

http://www.smartquant.com/forums/viewtopic.php?t=7631


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 27, 2009 11:36 am 
Offline

Joined: Sat Jun 07, 2008 1:41 pm
Posts: 47
Location: Munich, Germany
Thanks Kevin, you spend a lot of time on this.

Has anybody another way how to get it?


I mean: You trade a few trading-strategies (in Openquant naming: 1 solution with several projects included) and you want to define a maximum stop loss (or position sizing or whatever) for all trading-strategies together.

I think this is something everybody needs for live-trading. Is it better to read out somehow the broker-p&l?

Is there anybody with an idea around?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 28, 2009 8:27 am 
Offline

Joined: Thu Jun 08, 2006 3:56 pm
Posts: 537
Location: BC Canada
It seems to me that the best way is to have each strategy calculate its own running PnL, and to update it in the Global space on each bar/trade/quote.

After updating its own PnL in the global space, then each strategy must immediately check the global stop loss (or whatever risk limit you're monitoring), and must set a global flag for all strategies to shut down the next time they check in.

Then the first strategy that just updated its PnL, and that just detected and flagged the global risk overrun, must shut itself down. Other strategies will follow as they try to update their PnL.

I could be wrong, but it seems to me that trying to monitor the broker PnL for a global risk limit is (1) too limited, (2) probably too risky, and (3) maybe too complex. I say too limited because you might not be able to monitor the risk info that you're interested in (if it needs something beyond a PnL value, for example). It's probably too risky (at least for me), because what if the broker updates the info too slow in a fast market, or just too slow at any time? Then you're monitoring inaccurate info. And I say maybe too complex, because I don't know much about fetching and monitoring broker info. I hardly trust anything from the broker end any more---order statuses don't always update, connectivity goes down, and so on.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 07, 2009 9:43 pm 
Offline

Joined: Mon Aug 10, 2009 11:36 am
Posts: 6
Hi,

I am trying to write a risk management function in a solution containing different strategies using the code you provided above, but for some reason it does not work.
I have tried many variations without success.
Does any one of you know how to fix this?

Many Thanks,
lp

Code:
public override void OnStrategyStart()
   {
      if (!Global.Contains("n"))
         Global["n"] = 100000;
   }

// I have an error with the below condition

   public override void OnBar(Bar bar)
   {
      if (Equity >= (double)Global["n"])    // The error is 'Specified cast is not valid'


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

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