SmartQuant Discussion

Automated Quantitative Strategy Development, SmartQuant Product Discussion and Technical Support Forums
It is currently Wed Nov 21, 2018 3:31 am

All times are UTC + 3 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Sun Feb 15, 2015 6:48 pm 
Offline

Joined: Thu Jan 22, 2015 6:23 pm
Posts: 11
I thought this code snippet can assist who ever need to sync between the broker and OQ when it comes to active positions.
I call this code in OnStrategyStart event.

private void TrySyncActivePositions()
{
var provider = this.ExecutionProvider.Name;
var accountInfo = DataManager.GetBrokerInfo(provider).Accounts.Cast<BrokerAccount>().FirstOrDefault();
if (accountInfo != null)
{
//get currently active positions
var accountPositions = accountInfo.Positions.Cast<BrokerPosition>();
foreach (var position in accountPositions)
{
//check if instrument exists in project instruments list
var instrument = this.Instruments.Cast<Instrument>().Where(i => i.Symbol == position.Symbol).FirstOrDefault();
if (instrument != null)
{
var positionInfo = position.Fields.Cast<BrokerPositionField>();
try
{
//add transactions to portfolio
var tranType = position.LongQty > 0 ? TransactionSide.Buy : TransactionSide.Sell;
var entryPrice = double.Parse(position.Fields["AverageCost"].Value);
this.Portfolio.Add(Clock.Now, tranType, position.Qty, instrument, entryPrice, "OnStart - Loaded from broker");
}
catch (Exception ex)
{
Console.WriteLine(string.Format("Can't add {0} symbol to portfolio --> {1}", position.Symbol, ex.StackTrace));
}
}
}
}
}

** This code will run only when running in Live mode
** This code assumes you only have one broker active account
** This code will not crash but it assumes that the active (symbols) positions in the broker are also defined in the executed project, i.e. if you're currently trading on the AAPL you cannot sync the strategy with active position on MSFT.

Enjoy.


Top
 Profile  
 
PostPosted: Mon Feb 16, 2015 9:31 pm 
Offline

Joined: Fri Feb 13, 2015 5:44 pm
Posts: 32
Thank you.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

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