SmartQuant Discussion

Automated Quantitative Strategy Development, SmartQuant Product Discussion and Technical Support Forums
It is currently Sun Sep 15, 2019 9:39 pm

All times are UTC + 3 hours




Post new topic Reply to topic  [ 45 posts ]  Go to page 1, 2, 3  Next
Author Message
PostPosted: Wed Feb 27, 2008 2:28 am 
Offline

Joined: Mon Jan 07, 2008 6:18 am
Posts: 58
I want to check the account for a postion at strategy start and get it synched up. If I use

if(Position.Qty !0) it builds ok but I get runtime errors "Object reference not set to an instance of an object"

Obviously there is something I have to do to initialize things, Position.Qty is returning null, how is that done? I want to use Position.Qty other places in the code to keep the strategy synched up to the account as well. Maybe there is a way to tell the strategy that it has a position, by using a variable based on account info rather than HasPosition?

ty again

Max


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 27, 2008 2:04 pm 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6816
Hi,

a Position is created when a strategy receives an execution report from a broker, thus you don't have a Position (i.e. Strategy.Position == null) until your strategy receives a fill from a broker. You should always check

Code:
if (HasPosition)
  WriteLine("Position qty =  " + Position.Qty);


Regards,
Anton


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 27, 2008 6:15 pm 
Offline

Joined: Mon Jan 07, 2008 6:18 am
Posts: 58
HasPosition is from the viewpoint of the strategy. It does not check the account for a position, no? It's more like a flag in the code, no? I really would like to replace all instances of HasPosition in my code with Position.Qty. Maybe at startup of the strategy that is not doable. I have read the forums regarding the Null Exception error and did not find specific enough examples of the code involved to avoid it...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 27, 2008 6:34 pm 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6816
Hmm, perhaps I don't follow, but if you don't have any Qty, you don't have any position, i.e. Position == null .

It's a kind of logical since if you have an instrument in a strategy, i.e. you have Strategy object for this instrument, but you have not traded this instrument yet, you don't have any position in this instrument. If you don't have a position, then you don't have Strategy.Position object, which means that Strategy.Position == null

HasPosition is just a simplification for

Code:
if (Position == null)
  return false;
else
  return true;

Regards,
Anton


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 27, 2008 7:07 pm 
Offline

Joined: Mon Jan 07, 2008 6:18 am
Posts: 58
So HasPosition gets it info from the account? I have the following code to get bar history and close positions and cancel brackets at startup, it never closes the position that exists at start up, that led me to assume that HasPosition did not get it's info from the account:

public override void OnStrategyStart()
{
datetime2 = DateTime.Now;
datetime1 = datetime2.AddDays(-5);

foreach (Bar bar in GetHistoricalBars(datetime1, datetime2, BarType.Time, 60))
Bars.Add(bar);
// set up the moving averages, based on closing prices
ema1 = new EMA(Bars, AvgLength1, Color1);
Draw(ema1, 0);
ema2 = new EMA(Bars, AvgLength2, Color2);
Draw(ema2, 0);
atr1 = new ATR(Bars, AvgRangeLength, Color2);

if (HasPosition)

{
ExitTargetOrder.Cancel();
ExitStopOrder.Cancel();
ClosePosition();
}

}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 27, 2008 10:14 pm 
Offline

Joined: Tue May 03, 2005 9:36 pm
Posts: 136
Location: Sydney
I think maxpi is saying that he is trying to synchronize OQ with his broker account when he starts OQ. So he wants to check his broker account and if it has a position to have OQ know about it. For example if OQ had to shut down mid way through a session when there was an open position, when he restarts OQ he would want to get the account position from his broker and start running his strategy again - all synched up.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 27, 2008 10:24 pm 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6816
Ok, this is possible with a new BrokerInfo class that we have recently included in the Strategy. I will ask Alex to write some code examples tomorrow. It's not synched with Position since we don't know if we should do it at all and what is the best way to do this to not confuse a user / strategy.

Regards,
Anton


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 27, 2008 11:34 pm 
Offline

Joined: Mon Jan 07, 2008 6:18 am
Posts: 58
Thanks all... I consider it to be nearly critical that I can control code and order flow with account info rather than with flags set in the code. The issue is synchronization and error handling. I had that capability with TS2000 and Dynaorder or Hyperorder dll's and it was the elegant solution, but I can't go back to TS2000, it's not the elegant solution at all.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 28, 2008 6:54 pm 
Offline

Joined: Mon Jan 07, 2008 6:18 am
Posts: 58
Dr. Anton Fokin wrote:
Ok, this is possible with a new BrokerInfo class that we have recently included in the Strategy. I will ask Alex to write some code examples tomorrow. It's not synched with Position since we don't know if we should do it at all and what is the best way to do this to not confuse a user / strategy.

Regards,
Anton


The guy trading one system on one future contract could use some broker info at startup and maybe to set an account stop loss he could use the BrokerAccount class but the guy trading a few strategies on multiple stocks that wants to maximize use of his buying power is going to need to know instantaneous buying power and status of all his positions often... I could use examples of all the above really. I could do all of that years ago with the third party dll's and TS2k and it was never confusing actually....


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 28, 2008 8:27 pm 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6816
As I have mentioned, you can do all this, including buying power information request from a broker (currently IB), with BrokerInfo class. Alex will set up an example tomorrow.

Regards,
Anton


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 28, 2008 10:12 pm 
Offline

Joined: Mon Jan 07, 2008 6:18 am
Posts: 58
ty


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 04, 2008 12:40 am 
Offline

Joined: Fri Feb 08, 2008 5:46 pm
Posts: 26
I would love to see this example as well. Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 04, 2008 3:35 pm 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6816
Code:
BrokerAccount[] accounts = DataManager.GetBrokerInfo("IB").Accounts;
      
foreach (BrokerAccount account in accounts)
     Console.WriteLine(account.Name + " " + account.BuyingPower);


Regards,
Anton


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 05, 2008 2:02 am 
Offline

Joined: Mon Feb 27, 2006 9:07 pm
Posts: 52
Thank you Anton for posting that sample code. I tried it out and it worked fine for an IB individual account. I saw the account.Name and account.BuyingPower. I also was able to get the positions for this account.

I then tried it on an FA account. I saw the account.Name for each account, but the account.BuyingPower displayed as 0 and the positions were empty when there are positions. I tested to see if I could place a live order in one of the sub-accounts and I could. Is there anything else that needs to be done in order to make this work with sub-accounts?

Thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 05, 2008 5:57 am 
Offline

Joined: Wed Feb 27, 2008 7:37 am
Posts: 28
How do we get the Positions for this account in IB.

I get the following when I WriteLine account.Positions

Positions --> OpenQuant.API.BrokerPosition[]


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 45 posts ]  Go to page 1, 2, 3  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