SmartQuant Discussion

Automated Quantitative Strategy Development, SmartQuant Product Discussion and Technical Support Forums
It is currently Fri Oct 19, 2018 10:23 am

All times are UTC + 3 hours




Post new topic Reply to topic  [ 17 posts ]  Go to page Previous  1, 2
Author Message
 Post subject:
PostPosted: Wed Oct 07, 2009 9:45 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  
 
 Post subject:
PostPosted: Wed Oct 14, 2009 6:32 pm 
Offline

Joined: Thu Jun 08, 2006 3:56 pm
Posts: 537
Location: BC Canada
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. :-)


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 3 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