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

Several proj. in 1 solution.How to access performance-equity
http://www.smartquant.com/forums/viewtopic.php?f=42&t=7366
Page 2 of 2

Author:  lpbwanga [ Wed Oct 07, 2009 9:45 pm ]
Post subject: 

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'

Author:  kkkwj [ Wed Oct 14, 2009 6:32 pm ]
Post subject: 

Good question. My code works fine with a boolean, but not with a double. Here is my code:
Code:
    // this boolean code works fine
    string key = "n";
    if (!Global.Contains (key))
        Global[key] = true;
    bool global_n = (bool) Global[key];
    Console.WriteLine ("N is {0}", global_n);

    // but this double code fails with the 'cast not valid' runtime error
    string key = "n";
    if (!Global.Contains (key))
        Global[key] = 100000;
    double global_n = (double) Global[key];
    Console.WriteLine ("N is {0}", global_n);

    // finally, this code works fine. I guess C# uses a different
    // storage representation for 100000 if you don't tell it to store
    // it as a 'double' bit pattern
    string key = "n";
    if (!Global.Contains (key))
        Global[key] = (double) 100000;
    double global_n = (double) Global[key];
    Console.WriteLine ("N is {0}", global_n);


I tried using no cast on the store operation, and then 'long' or 'float' on the retrieve operation, to see if long or float might have been the default storage rep going in. But neither one worked, so I don't know what storage rep is used going in. Anyhow, this particular problem is now solved. :-)

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