/// <summary> /// Sends a batch of commands to the server and processes the results. /// </summary> private static void SendBatch() { // Create a new web client that will serve as the connection point to the server. WebClient webClient = new WebClient(); webClient.Timeout = TableLoader.timeout; // Call the web services to execute the command batch. remoteBatch.Merge(webClient.Execute(remoteBatch)); // Any error during the batch will terminate the execution of the remaining records in the data file. if (remoteBatch.HasExceptions) { // Display each error on the console. foreach (RemoteException remoteException in remoteBatch.Exceptions) { Console.WriteLine(remoteException.Message); } // This will signal the error exit from this loader. hasErrors = true; } // Clearing out the batch indicates that a new header should be created for the next set of commands. remoteBatch = null; remoteTransaction = null; remoteAssembly = null; remoteType = null; }
public void PrintTransaction(RemoteTransaction transaction) { Console.WriteLine("CarId : " + transaction.CarId); Console.WriteLine("GroupId : " + transaction.GroupId); Console.WriteLine("CarType : " + transaction.CarType); Console.WriteLine("Speed : " + transaction.Speed); Console.WriteLine("Timestamp: " + transaction.Timestamp); }
/// <summary> /// Load and execute a command batch. /// </summary> /// <param name="rootNode">The root node of the Xml Data structure that contains the command.</param> private static void LoadCommand(XmlNode rootNode) { // Read each of the transactions and send them to the server. foreach (XmlNode transactionNode in rootNode.SelectNodes("transaction")) { // Ignore Comment Nodes. if (transactionNode.NodeType == XmlNodeType.Comment) { continue; } // Each transaction in the batch is handled as a unit. That is, everything between the start and end <Transaction> // tags will be executed or rolled back as a unit. remoteBatch = new RemoteBatch(); remoteTransaction = remoteBatch.Transactions.Add(); remoteAssembly = null; remoteType = null; // The <Assembly> tag specifies the name of an assembly where the object and methods are found. foreach (XmlNode assemblyNode in transactionNode.SelectNodes("assembly")) { LoadAssembly(assemblyNode, 0); } // Once the entire transaction has been loaded, send it off to the server. SendBatch(); } // If the batch file doesn't contain explicit transactions, then implicit ones are assumed and the processing of the file // continues with the 'assembly' nodes. XmlNodeList assemblyNodes = rootNode.SelectNodes("assembly"); if (assemblyNodes.Count != 0) { // This will tell the 'LoadAssembly' to determine the size of the batch from the number of records processed. remoteBatch = null; remoteTransaction = null; remoteAssembly = null; remoteType = null; // If an assembly is included outside of a Transaction, then the transaction is implicit and the size of the // transaction is the 'batchSize' parameter. foreach (XmlNode assemblyNode in assemblyNodes) { // Load the assebly node ninto the batch. LoadAssembly(assemblyNode, batchSize); // There will be records in the batch when the above method returns (unless there are exactly as many records // in the batch as the batch size). This will send the remaining records to the server. if (remoteBatch != null) { SendBatch(); } } } }
/// <summary> /// Returns a set of orders that will achieve the targets specified by the model. /// </summary> /// <param name="accountRow">The account or parent account to be rebalanced.</param> /// <param name="modelRow">The target percentages to use for rebalancing.</param> /// <returns>A Dataset of new, updated and deleted orders.</returns> public static RemoteBatch Rebalance(ClientMarketData.AccountRow accountRow, ClientMarketData.ModelRow modelRow) { // The orders to insert, update and delete orders to achieve the target percentages will be put in this DataSet. RemoteBatch remoteBatch = new RemoteBatch(); RemoteTransaction remoteTransaction = remoteBatch.Transactions.Add(); // Rebalance the parent account and all it's children. SelectedSecurity.RecurseAccounts(remoteBatch, remoteTransaction, accountRow, modelRow); // This is the sucessful result of rebalancing. return(remoteBatch); }
/// <summary> /// Returns a set of orders that will achieve the targets specified by the model. /// </summary> /// <param name="accountRow">The account or parent account to be rebalanced.</param> /// <param name="modelRow">The target percentages to use for rebalancing.</param> /// <returns>A Dataset of new, updated and deleted orders.</returns> public static RemoteBatch Rebalance(ClientMarketData.AccountRow accountRow, ClientMarketData.ModelRow modelRow) { // Make sure the scheme still exists in the in-memory database. We need it to rebalance the appraisal. ClientMarketData.SchemeRow schemeRow; if ((schemeRow = ClientMarketData.Scheme.FindBySchemeId(modelRow.SchemeId)) == null) { throw new ArgumentException("Scheme doesn't exist in the ClientMarketData", modelRow.SchemeId.ToString()); } // The final result of this method is a command batch that can be sent to the server. RemoteBatch remoteBatch = new RemoteBatch(); RemoteTransaction remoteTransaction = remoteBatch.Transactions.Add(); // Rebalance the parent account and all it's children. RecurseAccounts(remoteBatch, remoteTransaction, accountRow, modelRow, schemeRow); // The sucessful result of rebalancing. return(remoteBatch); }
/// <summary> /// Returns a set of orders that will achieve the targets specified by the model. /// </summary> /// <param name="accountRow">The account or parent account to be rebalanced.</param> /// <param name="modelRow">The target percentages to use for rebalancing.</param> /// <returns>A Dataset of new, updated and deleted orders.</returns> public static RemoteBatch Rebalance(ClientMarketData.AccountRow accountRow, ClientMarketData.ModelRow modelRow) { // The orders to insert, update and delete orders to achieve the target percentages will be put in this // DataSet. RemoteBatch remoteBatch = new RemoteBatch(); RemoteTransaction remoteTransaction = remoteBatch.Transactions.Add(); // The outline of the appraisal will be needed to make calculations based on a position, that is a security, // account, position type combination. Note that we're also including all the model securities in the // outline. This triggers a rebalance if a security exists in the model, but doesn't exist yet in the // appraisal. AppraisalSet appraisalSet = new Appraisal(accountRow, modelRow, true); // Rebalance the parent account and all it's children. Security.RecurseAccounts(remoteBatch, remoteTransaction, appraisalSet, accountRow, modelRow); // This is the sucessful result of rebalancing. return(remoteBatch); }
public void Send(RemoteTransaction transaction, String group) { th = TrafficHandler.getInstance(); var factory = new ConnectionFactory(); factory.Uri = "amqp://*****:*****@rabbit.binna.eu/"; //Insert your own user and password using (var connection = factory.CreateConnection()) using (var channel = connection.CreateModel()) { var transactionString = JsonConvert.SerializeObject(transaction); //Awesome function var properties = channel.CreateBasicProperties(); properties.Persistent = true; channel.BasicPublish(exchange: "car.production", //Choose between bank.development and bank.production depending of the queue (e.g. 70 is production, 71 is development) routingKey: group, //This relates to the queue name of the receiver bank basicProperties: properties, //Set the properties to persistent, otherwise the messages will get lost if the server restarts body: GetBytes(transactionString)); } }
/// <summary> /// Recursively creates instructions to delete proposed order of the given position. /// </summary> /// <param name="remoteBatch">The object type containing the method to delete the order relationship.</param> /// <param name="remoteTransaction">Groups several commands into a unit for execution.</param> /// <param name="accountRow">An account record, used to select proposed order records.</param> private static void Delete(RemoteBatch remoteBatch, RemoteTransaction remoteTransaction, ClientMarketData.AccountRow accountRow, ClientMarketData.SecurityRow securityRow, int positionTypeCode) { // Run through each of the proposed orders in this account and create a record to have them deleted. object[] key = new object[] { accountRow.AccountId, securityRow.SecurityId, positionTypeCode }; foreach (DataRowView dataRowView in MarketData.ProposedOrder.UKProposedOrderAccountIdSecurityIdPositionTypeCode.FindRows(key)) { // This is used to reference the current proposed order that matches the position criteria. ClientMarketData.ProposedOrderRow proposedOrderRow = (ClientMarketData.ProposedOrderRow)dataRowView.Row; // Child proposed orders aren't deleted directly, they can only be deleted when the parent is deleted. The best // example of this is cash. An account can have both child cash (related to an equity trade) or parent cash (cash // added directly to the account with no offsetting trade). If a reqest is made to delete cash, only the parent // cash should be deleted. The account will appear to have a cash balance until the equity attached to the child // cash is deleted. if (!Relationship.IsChildProposedOrder(proposedOrderRow)) { Delete(remoteBatch, remoteTransaction, proposedOrderRow); } } }
/// <summary> /// Creates a batch command to delete a proposed order and it's links. /// </summary> /// <param name="remoteBatch"></param> /// <param name="remoteTransaction"></param> /// <param name="parentProposedOrder"></param> public static void Delete(RemoteBatch remoteBatch, RemoteTransaction remoteTransaction, ClientMarketData.ProposedOrderRow parentProposedOrder) { // These define the assembly and the types within those assemblies that will be used to create the proposed orders on // the middle tier. RemoteAssembly remoteAssembly = remoteBatch.Assemblies.Add("Service.Core"); RemoteType proposedOrderType = remoteAssembly.Types.Add("Shadows.WebService.Core.ProposedOrder"); RemoteType proposedOrderTreeType = remoteAssembly.Types.Add("Shadows.WebService.Core.ProposedOrderTree"); // Proposed orders have a hierarchy. For example, orders for equities will be linked to foreach (ClientMarketData.ProposedOrderTreeRow proposedOrderTree in parentProposedOrder.GetProposedOrderTreeRowsByFKProposedOrderProposedOrderTreeParentId()) { // Create a command to delete the relationship between the parent and child. RemoteMethod deleteRelation = proposedOrderTreeType.Methods.Add("Delete"); deleteRelation.Transaction = remoteTransaction; deleteRelation.Parameters.Add("rowVersion", proposedOrderTree.RowVersion); deleteRelation.Parameters.Add("parentId", proposedOrderTree.ParentId); deleteRelation.Parameters.Add("childId", proposedOrderTree.ChildId); // This relatioship will give us access to the child proposed order. ClientMarketData.ProposedOrderRow childProposedOrder = proposedOrderTree.ProposedOrderRowByFKProposedOrderProposedOrderTreeChildId; // Create a command to delete the child proposed order. RemoteMethod deleteChild = proposedOrderType.Methods.Add("Delete"); deleteChild.Transaction = remoteTransaction; deleteChild.Parameters.Add("rowVersion", childProposedOrder.RowVersion); deleteChild.Parameters.Add("proposedOrderId", childProposedOrder.ProposedOrderId); } // Create a command to delete the parent proposed order. RemoteMethod deleteParent = proposedOrderType.Methods.Add("Delete"); deleteParent.Transaction = remoteTransaction; deleteParent.Parameters.Add("rowVersion", parentProposedOrder.RowVersion); deleteParent.Parameters.Add("proposedOrderId", parentProposedOrder.ProposedOrderId); }
/// <summary> /// Load up an assembly section of a batch. /// </summary> /// <param name="assemblyNode"></param> private static void LoadAssembly(XmlNode assemblyNode, int batchSize) { // Ignore Comment Nodes. if (assemblyNode.NodeType == XmlNodeType.Comment) { return; } // Add an Assembly specifier to the batch. This essentially describes the DLL where the methods are found. string assemblyName = assemblyNode.Attributes["name"].Value; // Each assembly can have one or more Objects (or Types) which can be instantiated. The batch command // processing can call static methods belonging to the instantiated object. foreach (XmlNode typeNode in assemblyNode.SelectNodes("type")) { // Ignore Comment Nodes. if (typeNode.NodeType == XmlNodeType.Comment) { continue; } // Loading the database involves creating a batch of commands and sending them off as a transaction. // This gives the server a chance to pipeline a large chunk of processing, without completely locking // up the server for the entire set of data. This will construct a header for the command batch which // gives information about which assembly contains the class that is used to load the data. string typeName = typeNode.Attributes["name"].Value; // Attach each method and it's parameters to the command batch. foreach (XmlNode methodNode in typeNode.SelectNodes("method")) { // Ignore Comment Nodes. if (methodNode.NodeType == XmlNodeType.Comment) { continue; } // The 'remoteBatch' will be cleared after it is sent to the server. Make sure a batch exists before adding // methods and parameters to it. if (remoteBatch == null) { // Loading the database involves creating a batch of commands and sending them off as a transaction. This // gives the server a chance to pipeline a large chunk of processing, without completely locking up the // server for the entire set of data. This will create a batch for the next bunch of commands. remoteBatch = new RemoteBatch(); remoteTransaction = remoteBatch.Transactions.Add(); // This counts the number of records placed in the batch. batchCounter = 0; } if (remoteAssembly == null || remoteAssembly.Name != assemblyName) { remoteAssembly = remoteBatch.Assemblies.Add(assemblyName); } if (remoteType == null || remoteType.Name != typeName) { remoteType = remoteAssembly.Types.Add(typeName); } // Each method is part of the transaction defined by the tagged outline structure of the input // file. RemoteMethod remoteMethod = remoteType.Methods.Add(methodNode.Attributes["name"].Value); remoteMethod.Transaction = remoteTransaction; // Load each of the parameters into the method structure. foreach (XmlNode parameterNode in methodNode.ChildNodes) { // Ignore Comment Nodes. if (parameterNode.NodeType == XmlNodeType.Comment) { continue; } // Add the next parameter to the method. remoteMethod.Parameters.Add(parameterNode.Name, parameterNode.InnerText); } // One more record for the grand total, one more record for the number in the current batch. recordCounter++; batchCounter++; // This will check to see if it's time to send the batch. A batch is sent when the 'batchSize' has been // reached, or if the last record has just been converted into a command. if (batchSize != 0 && recordCounter % batchSize == 0) { SendBatch(); } } } }
private void DragDropHandler(params object[] arguments) { ObjectNode toNode = (ObjectNode)arguments[0]; ObjectNode fromNode = (ObjectNode)arguments[1]; ObjectNode childNode = (ObjectNode)arguments[2]; // Make sure the user has selected a valid source and destination for the operation. It's illegal to move the node // 1. To the root of the tree // 2. From the root of the tree if (toNode == null || fromNode == null) { return; } // Don't allow for circular references. try { // Lock the tables needed for this operation. Debug.Assert(!ClientMarketData.AreLocksHeld); ClientMarketData.ObjectLock.AcquireReaderLock(CommonTimeout.LockWait); // It's critical that circular references aren't created, either by accident or design. First, find the object // record associated with the destination node. ClientMarketData.ObjectRow parentRow = ClientMarketData.Object.FindByObjectId(toNode.ObjectId); if (parentRow == null) { throw new Exception("This object has been deleted"); } // This is the object that is being dragged. Find the row ClientMarketData.ObjectRow childRow = ClientMarketData.Object.FindByObjectId(childNode.ObjectId); if (childRow == null) { throw new Exception("This object has been deleted"); } if (Shadows.Quasar.Common.Relationship.IsChildObject(childRow, parentRow)) { MessageBox.Show(this.TopLevelControl, "This would create a circular references.", "Quasar Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } catch (Exception exception) { // Write the error and stack trace out to the debug listener Debug.WriteLine(String.Format("{0}, {1}", exception.Message, exception.StackTrace)); } finally { // Release table locks. if (ClientMarketData.ObjectLock.IsReaderLockHeld) { ClientMarketData.ObjectLock.ReleaseReaderLock(); } Debug.Assert(!ClientMarketData.AreLocksHeld); } // Any commands created below will be constructed in this object and sent to the server for execution. RemoteBatch remoteBatch = new RemoteBatch(); // Change the default model of an account. When the destination is an account group, account or sub account and the // source is a model, a command will be constructed to change the default model. if (toNode.ObjectTypeCode == ObjectType.Account && dragNode.ObjectTypeCode == ObjectType.Model) { try { // Lock the tables needed for this operation. Debug.Assert(!ClientMarketData.AreLocksHeld); ClientMarketData.AccountLock.AcquireReaderLock(CommonTimeout.LockWait); // Find the account used, throw an error if it's been deleted. ClientMarketData.AccountRow accountRow; if ((accountRow = ClientMarketData.Account.FindByAccountId(toNode.ObjectId)) == null) { throw new Exception("This account has been deleted"); } // Construct a command to change the default model associated with the account. RemoteAssembly remoteAssembly = remoteBatch.Assemblies.Add("Service.Core"); RemoteType remoteType = remoteAssembly.Types.Add("Shadows.WebService.Core.Account"); RemoteMethod remoteMethod = remoteType.Methods.Add("Update"); remoteMethod.Parameters.Add("rowVersion", accountRow.RowVersion); remoteMethod.Parameters.Add("accountId", accountRow.AccountId); remoteMethod.Parameters.Add("modelId", dragNode.ObjectId); } catch (Exception exception) { // Write the error and stack trace out to the debug listener Debug.WriteLine(String.Format("{0}, {1}", exception.Message, exception.StackTrace)); } finally { // Release table locks. if (ClientMarketData.AccountLock.IsReaderLockHeld) { ClientMarketData.AccountLock.ReleaseReaderLock(); } Debug.Assert(!ClientMarketData.AreLocksHeld); } } else { // If we made it here, the drag-and-drop is interpreted as a command to move a child from one parent to another. try { // Lock the tables needed for this operation. Debug.Assert(!ClientMarketData.AreLocksHeld); ClientMarketData.ObjectTreeLock.AcquireReaderLock(CommonTimeout.LockWait); // Extract the primary identifiers from the user interface nodes. int fromId = fromNode.ObjectId; int toId = toNode.ObjectId; int childId = dragNode.ObjectId; // Find the object in the data model and make sure it still exists. ClientMarketData.ObjectTreeRow objectTreeRow = ClientMarketData.ObjectTree.FindByParentIdChildId(fromNode.ObjectId, dragNode.ObjectId); if (objectTreeRow == null) { throw new Exception("This relationship has been deleted by someone else."); } // Moving a child object from one parent to another must be accomplished as a transaction. Otherwise, an // orhpan object will be created if the operation fails midway through. RemoteTransaction remoteTransaction = remoteBatch.Transactions.Add(); RemoteAssembly remoteAssembly = remoteBatch.Assemblies.Add("Service.Core"); RemoteType remoteType = remoteAssembly.Types.Add("Shadows.WebService.Core.ObjectTree"); // Construct a command delete the old parent relation. RemoteMethod deleteObjectTree = remoteType.Methods.Add("Delete"); deleteObjectTree.Transaction = remoteTransaction; deleteObjectTree.Parameters.Add("rowVersion", objectTreeRow.RowVersion); deleteObjectTree.Parameters.Add("parentId", fromId); deleteObjectTree.Parameters.Add("childId", childId); // Construct a command insert a new parent relation. RemoteMethod insertObjectTree = remoteType.Methods.Add("Insert"); insertObjectTree.Transaction = remoteTransaction; insertObjectTree.Parameters.Add("parentId", toId); insertObjectTree.Parameters.Add("childId", childId); } catch (Exception exception) { // Write the error and stack trace out to the debug listener Debug.WriteLine(String.Format("{0}, {1}", exception.Message, exception.StackTrace)); } finally { // Release table locks. if (ClientMarketData.ObjectTreeLock.IsReaderLockHeld) { ClientMarketData.ObjectTreeLock.ReleaseReaderLock(); } Debug.Assert(!ClientMarketData.AreLocksHeld); } } // If the command batch was built successfully, then execute it. if (remoteBatch != null) { try { // Call the web server to rename the object on the database. Note that this method must be called when there are // no locks to prevent deadlocking. That is why it appears in it's own 'try/catch' block. ClientMarketData.Execute(remoteBatch); } catch (BatchException batchException) { // Display each error in the batch. foreach (RemoteException remoteException in batchException.Exceptions) { MessageBox.Show(remoteException.Message, "Quasar Error"); } } } }
private static void Update(RemoteBatch remoteBatch, RemoteTransaction remoteTransaction, ClientMarketData.ProposedOrderRow parentProposedOrder, decimal quantityInstruction) { // These define the assembly and the types within those assemblies that will be used to create the proposed orders on // the middle tier. RemoteAssembly remoteAssembly = remoteBatch.Assemblies.Add("Service.Core"); RemoteType proposedOrderType = remoteAssembly.Types.Add("Shadows.WebService.Core.ProposedOrder"); ClientMarketData.AccountRow accountRow = parentProposedOrder.AccountRow; ClientMarketData.SecurityRow securityRow = parentProposedOrder.SecurityRowByFKSecurityProposedOrderSecurityId; // This will turn the signed quantity into an absolute quantity and a transaction code (e.g. -1000 is turned into a // SELL of 1000 shares). decimal parentQuantity = Math.Abs(quantityInstruction); int parentTransactionTypeCode = TransactionType.Calculate(securityRow.SecurityTypeCode, parentProposedOrder.PositionTypeCode, quantityInstruction); // The time in force first comes from the user preferences, next, account settings and finally defaults to a day // orders. int timeInForceCode = !ClientPreferences.IsTimeInForceCodeNull() ? ClientPreferences.TimeInForceCode : !accountRow.IsTimeInForceCodeNull() ? accountRow.TimeInForceCode : TimeInForce.DAY; // The destination blotter comes first from the user preferences, second from the account preferences, and finally uses // the auto-routing logic. int blotterId = !ClientPreferences.IsBlotterIdNull() ? ClientPreferences.BlotterId : !accountRow.IsBlotterIdNull() ? accountRow.BlotterId : TradingSupport.AutoRoute(securityRow, parentQuantity); // Create a command to update the proposed order. RemoteMethod updateParent = proposedOrderType.Methods.Add("Update"); updateParent.Transaction = remoteTransaction; updateParent.Parameters.Add("rowVersion", parentProposedOrder.RowVersion); updateParent.Parameters.Add("proposedOrderId", parentProposedOrder.ProposedOrderId); updateParent.Parameters.Add("accountId", parentProposedOrder.AccountId); updateParent.Parameters.Add("securityId", parentProposedOrder.SecurityId); updateParent.Parameters.Add("settlementId", parentProposedOrder.SettlementId); updateParent.Parameters.Add("blotterId", blotterId); updateParent.Parameters.Add("positionTypeCode", parentProposedOrder.PositionTypeCode); updateParent.Parameters.Add("transactionTypeCode", parentTransactionTypeCode); updateParent.Parameters.Add("timeInForceCode", timeInForceCode); updateParent.Parameters.Add("orderTypeCode", OrderType.Market); updateParent.Parameters.Add("quantity", parentQuantity); foreach (ClientMarketData.ProposedOrderTreeRow proposedOrderTree in parentProposedOrder.GetProposedOrderTreeRowsByFKProposedOrderProposedOrderTreeParentId()) { ClientMarketData.ProposedOrderRow childProposedOrder = proposedOrderTree.ProposedOrderRowByFKProposedOrderProposedOrderTreeChildId; // If this is the settlement part of the order, then adjust the quantity. if (childProposedOrder.SecurityId == parentProposedOrder.SettlementId) { // The settlement security is needed for the calculation of the cash impact of this trade. ClientMarketData.CurrencyRow currencyRow = MarketData.Currency.FindByCurrencyId(childProposedOrder.SettlementId); decimal marketValue = parentQuantity * securityRow.QuantityFactor * Price.Security(currencyRow, securityRow) * securityRow.PriceFactor * TransactionType.GetCashSign(parentTransactionTypeCode); decimal childQuantity = Math.Abs(marketValue); int childTransactionTypeCode = TransactionType.Calculate(securityRow.SecurityTypeCode, parentProposedOrder.PositionTypeCode, marketValue); // Create a command to update the proposed order. RemoteMethod updateChild = proposedOrderType.Methods.Add("Update"); updateChild.Transaction = remoteTransaction; updateChild.Parameters.Add("rowVersion", childProposedOrder.RowVersion); updateChild.Parameters.Add("proposedOrderId", childProposedOrder.ProposedOrderId); updateChild.Parameters.Add("accountId", childProposedOrder.AccountId); updateChild.Parameters.Add("securityId", childProposedOrder.SecurityId); updateChild.Parameters.Add("settlementId", childProposedOrder.SettlementId); updateChild.Parameters.Add("blotterId", blotterId); updateChild.Parameters.Add("positionTypeCode", parentProposedOrder.PositionTypeCode); updateChild.Parameters.Add("transactionTypeCode", childTransactionTypeCode); updateChild.Parameters.Add("timeInForceCode", timeInForceCode); updateChild.Parameters.Add("orderTypeCode", OrderType.Market); updateChild.Parameters.Add("quantity", childQuantity); } } }
/// <summary> /// Recursively rebalances an account and all it's children. /// </summary> /// <param name="accountRow">The parent account to be rebalanced.</param> private static void RecurseAccounts(RemoteBatch remoteBatch, RemoteTransaction remoteTransaction, AppraisalSet appraisalSet, ClientMarketData.AccountRow accountRow, ClientMarketData.ModelRow modelRow) { // The base currency of the account is used to cacluate market values. ClientMarketData.CurrencyRow currencyRow = ClientMarketData.Currency.FindByCurrencyId(accountRow.CurrencyId); // Calculate the total market value for the appraisal. This will be the denominator in all calculations involving // portfolio percentages. decimal accountMarketValue = MarketValue.Calculate(currencyRow, accountRow, MarketValueFlags.EntirePosition); // Cycle through all the positions of the appraisal using the current account and calculate the size and direction of // the trade needed to bring it to the model's target percent. foreach (AppraisalSet.SecurityRow driverSecurity in appraisalSet.Security) { // We need to reference the security row in the ClientMarketData to price this item. ClientMarketData.SecurityRow securityRow = ClientMarketData.Security.FindBySecurityId(driverSecurity.SecurityId); // In this rebalancing operation, the cash balance is dependant on the securities bought and sold. The assumption // is made that we won't implicitly add or remove cash to accomplish the reblancing operation. When stocks are // bought or sold below, they will impact the underlying currency. A cash target can be reached by setting all the // other percentages up properly. As long as the total percentage in a model is 100%, the proper cash target will // be calculated. We don't have to do anything with this asset type. if (securityRow.SecurityTypeCode == SecurityType.Currency) { continue; } // This section will calculate the difference in between the actual and target market values for each // position and create orders that will bring the account to the targeted percentages. foreach (AppraisalSet.PositionRow driverPosition in driverSecurity.GetPositionRows()) { // Calculate the proposed quantity needed to bring this asset/account combination to the percentage given by // the model. First, find the target percent. If it's not there, we assume a target of zero (meaning sell all // holdings). ClientMarketData.PositionTargetRow positionTargetRow = ClientMarketData.PositionTarget.FindByModelIdSecurityIdPositionTypeCode(modelRow.ModelId, securityRow.SecurityId, driverPosition.PositionTypeCode); decimal targetPositionPercent = positionTargetRow == null ? 0.0M : positionTargetRow.Percent; // The market value of this trade will be the target market value less the current market value of // this position (without including the existing proposed orders in the current market value // calculation). decimal targetPositionMarketValue = targetPositionPercent * accountMarketValue; decimal actualPositionMarketValue = MarketValue.Calculate(currencyRow, accountRow, securityRow, driverPosition.PositionTypeCode, MarketValueFlags.ExcludeProposedOrder); decimal proposedMarketValue = targetPositionMarketValue - actualPositionMarketValue; // Calculate the quantity needed to hit the target market value and round it according to the model. Note that // the market values and prices are all denominated in the currency of the parent account. Also note the // quantityFactor is needed for the proper quantity calculation. decimal price = Price.Security(currencyRow, securityRow); decimal proposedQuantity = price == 0.0M ? 0.0M : proposedMarketValue / (price * securityRow.QuantityFactor); // If we have an equity, round to the model's lot size. Common values are 100 and 1. if (securityRow.SecurityTypeCode == SecurityType.Equity) { proposedQuantity = Math.Round(proposedQuantity / modelRow.EquityRounding, 0) * modelRow.EquityRounding; } // A debt generally needs to be rounded to face. if (securityRow.SecurityTypeCode == SecurityType.Debt) { proposedQuantity = Math.Round(proposedQuantity / modelRow.DebtRounding, 0) * modelRow.DebtRounding; } // Have the Order Form Builder object construct an order based on the new proposed quantity. This method will // fill in the defaults needed for a complete Proposed Order. It will also create an deposit or widthdrawal // from an account to cover the transaction. ProposedOrder.Create(remoteBatch, remoteTransaction, accountRow, securityRow, driverPosition.PositionTypeCode, proposedQuantity); } } // Now that we've rebalanced the parent account, cycle through all the children accounts and rebalance them. foreach (ClientMarketData.ObjectTreeRow objectTreeRow in accountRow.ObjectRow.GetObjectTreeRowsByFKObjectObjectTreeParentId()) { foreach (ClientMarketData.AccountRow childAccount in objectTreeRow.ObjectRowByFKObjectObjectTreeChildId.GetAccountRows()) { Security.RecurseAccounts(remoteBatch, remoteTransaction, appraisalSet, childAccount, modelRow); } } }
/// <summary> /// Creates a temporary model based on the current sector level targets. /// </summary> /// <param name="accountRow">An account used as a basis for the targets.</param> /// <param name="schemeRow">The scheme used to select sector targets.</param> /// <returns>A batch of commands that will create a model containing the current sector weights of the account.</returns> private static ModelBatch CreateSectorSelfModel(ClientMarketData.AccountRow accountRow, ClientMarketData.SchemeRow schemeRow) { // This command batch will create a temporary model and populate it with the current position level percentages as the // target values. ModelBatch modelBatch = new ModelBatch(); RemoteTransaction remoteTransaction = modelBatch.Transactions.Add(); RemoteAssembly remoteAssembly = modelBatch.Assemblies.Add("Service.Core"); RemoteType remoteType = remoteAssembly.Types.Add("Shadows.WebService.Core.Model"); // Create the temporary model. RemoteMethod insertModel = remoteType.Methods.Add("Insert"); insertModel.Parameters.Add("modelId", DataType.Int, Direction.ReturnValue); insertModel.Parameters.Add("rowVersion", DataType.Long, Direction.Output); insertModel.Parameters.Add("modelTypeCode", ModelType.Sector); insertModel.Parameters.Add("name", "Untitled"); insertModel.Parameters.Add("schemeId", schemeRow.SchemeId); insertModel.Parameters.Add("algorithmId", Algorithm.SectorMergeRebalancer); insertModel.Parameters.Add("temporary", true); // The 'Self Sector' uses the market value of all the account and sub-account. decimal accountMarketValue = MarketValue.Calculate(accountRow.CurrencyRow, accountRow, MarketValueFlags.EntirePosition | MarketValueFlags.IncludeChildAccounts); // No need to construct a model if the account market value is zero. if (accountMarketValue != 0.0M) { // Create a new outline for the model to follow. This will collect the tax lots, proposed orders, orders // and allocations into industry classification sectors. Common.Appraisal appraisal = new Common.Appraisal(accountRow, schemeRow, true); // The object Type for this operation. RemoteType sectorTargetType = remoteAssembly.Types.Add("Shadows.WebService.Core.SectorTarget"); // Now that we have an outline to follow, we are going to run through each of the sectors, calculate the market // value, and create an entry in the temporary model for that sector and it's current weight of the overall market // value. foreach (AppraisalSet.SchemeRow driverScheme in appraisal.Scheme) { foreach (AppraisalSet.ObjectTreeRow driverTree in driverScheme.ObjectRow.GetObjectTreeRowsByFKObjectObjectTreeParentId()) { foreach (AppraisalSet.SectorRow driverSector in driverTree.ObjectRowByFKObjectObjectTreeChildId.GetSectorRows()) { // This sector is the destination for the market value calculation. ClientMarketData.SectorRow sectorRow = ClientMarketData.Sector.FindBySectorId(driverSector.SectorId); // Calculate the market value of all the securities held by all the accounts in the current sector. decimal sectorMarketValue = MarketValue.Calculate(accountRow.CurrencyRow, accountRow, sectorRow, MarketValueFlags.EntirePosition | MarketValueFlags.IncludeChildAccounts); // Add the position level target to the model. RemoteMethod insertSector = sectorTargetType.Methods.Add("Insert"); insertSector.Parameters.Add("modelId", insertModel.Parameters["modelId"]); insertSector.Parameters.Add("sectorId", sectorRow.SectorId); insertSector.Parameters.Add("percent", sectorMarketValue / accountMarketValue); } } } } // Save the reference to the 'modelId' return parameter. modelBatch.ModelIdParameter = insertModel.Parameters["modelId"]; // This batch will create a temporary model based on the sector totals of the original account. return(modelBatch); }
static int Main(string[] args) { // If this flag is set during the processing of the file, the program will exit with an error code. bool hasErrors = false; try { // Defaults batchSize = 100; assemblyName = "Service.External"; nameSpaceName = "Shadows.WebService.External"; // The command line parser is driven by different states that are triggered by the flags read. Unless a flag has been // read, the command line parser assumes that it's reading the file name from the command line. argumentState = ArgumentState.FileName; // Parse the command line for arguments. foreach (string argument in args) { // Decode the current argument into a state change (or some other action). if (argument == "-a") { argumentState = ArgumentState.Assembly; continue; } if (argument == "-b") { argumentState = ArgumentState.BatchSize; continue; } if (argument == "-n") { argumentState = ArgumentState.NameSpace; continue; } if (argument == "-i") { argumentState = ArgumentState.FileName; continue; } // The parsing state will determine which variable is read next. switch (argumentState) { case ArgumentState.Assembly: assemblyName = argument; break; case ArgumentState.BatchSize: batchSize = Convert.ToInt32(argument); break; case ArgumentState.FileName: fileName = argument; break; case ArgumentState.NameSpace: nameSpaceName = argument; break; } // The default state is to look for the input file name on the command line. argumentState = ArgumentState.FileName; } // Throw a usage message back at the user if no file name was given. if (fileName == null) { throw new Exception("Usage: Loader.Algorithm -i <FileName>"); } // Open up the file containing all the broker. BrokerReader brokerReader = new BrokerReader(fileName); // Create a new web client that will serve as the connection point to the server. WebClient webClient = new WebClient(); // Loading the database involves creating a batch of commands and sending them off as a transaction. This gives // the server a chance to pipeline a large chunk of processing, without completely locking up the server for the // entire set of data. This will construct a header for the command batch which gives information about which // assembly contains the class that is used to load the data. RemoteBatch remoteBatch = new RemoteBatch(); RemoteTransaction remoteTransaction = remoteBatch.Transactions.Add(); RemoteAssembly remoteAssembly = remoteBatch.Assemblies.Add(assemblyName); RemoteType remoteType = remoteAssembly.Types.Add(string.Format("{0}.{1}", nameSpaceName, "Broker")); // Read the file until an EOF is reached. while (true) { // This counter keeps track of the number of records sent. When the batch is full, it's sent to the server to be // executed as a single transaction. int batchCounter = 0; // Read the next broker from the input stream. A 'null' is returned when we've read past the end of file. Broker broker = brokerReader.ReadBroker(); if (broker != null) { // Construct a call to the 'Load' method to populate the broker record. RemoteMethod remoteMethod = remoteType.Methods.Add("Load"); remoteMethod.Transaction = remoteTransaction; remoteMethod.Parameters.Add("brokerId", broker.Symbol); remoteMethod.Parameters.Add("name", broker.Name); remoteMethod.Parameters.Add("symbol", broker.Symbol); } // This will check to see if it's time to send the batch. A batch is sent when the 'batchSize' has been // reached, or if the last record has just been converted into a command. if (++batchCounter % batchSize == 0 || broker == null) { // Call the web services to execute the command batch. remoteBatch.Merge(webClient.Execute(remoteBatch)); // Any error during the batch will terminate the execution of the remaining records in the data file. if (remoteBatch.HasExceptions) { // Display each error on the console. foreach (RemoteMethod remoteMethod in remoteBatch.Methods) { foreach (RemoteException remoteException in remoteMethod.Exceptions) { Console.WriteLine(String.Format("{0}: {1}", remoteMethod.Parameters["brokerId"].Value, remoteException.Message)); } } // This will signal the error exit from this loader. hasErrors = true; } // If the end of file was reached, break out of the loop and exit the application. if (broker == null) { break; } // After each batch has been send, check to see if there are additional records to be send. If so, // regenerate the remote batch and set up the header. remoteBatch = new RemoteBatch(); remoteTransaction = remoteBatch.Transactions.Add(); remoteAssembly = remoteBatch.Assemblies.Add(assemblyName); remoteType = remoteAssembly.Types.Add(string.Format("{0}.{1}", nameSpaceName, "Broker")); } } } catch (Exception exception) { // Show the system error and exit with an error. Console.WriteLine(exception.Message); hasErrors = true; } // Any errors will cause an abnormal exit. if (hasErrors) { return(1); } // Write a status message when a the file is loaded successfully. Console.WriteLine(String.Format("{0} Data: Brokers, Loaded", DateTime.Now.ToString("u"))); // If we reached here, the file was imported without issue. return(0); }
/// <summary> /// Fills the OrderForm table with instructions to create, delete or update proposed orders. /// </summary> /// <param name="accountId">Identifiers the destination account of the proposed order.</param> /// <param name="securityId">Identifies the security being trade.</param> /// <param name="positionTypeCode">Identifies the long or short position of the trade.</param> /// <param name="settlementId"></param> /// <param name="proposedQuantity">The signed (relative) quantity of the trade.</param> public static void Create(RemoteBatch remoteBatch, RemoteTransaction remoteTransaction, ClientMarketData.AccountRow accountRow, ClientMarketData.SecurityRow securityRow, int positionTypeCode, decimal proposedQuantity) { // If the proposed quantity is to be zero, we'll delete all proposed orders for this position in the parent and // descendant accounts. Otherwise, a command batch will be created to clear any child proposed orders and create or // update a proposed order for the parent account. if (proposedQuantity == 0.0M) { ProposedOrder.Delete(remoteBatch, remoteTransaction, accountRow, securityRow, positionTypeCode); } else { // The strategy here is to cycle through all the existing proposed orders looking for any that match the account // id, security id and position type of the new order. If none is found, we create a new order. If one is found, // we modify it for the new quantity. Any additional proposed orders are deleted. This flag lets us know if any // existing proposed orders match the position attributes. bool firstTime = true; // Cycle through each of the proposed orders in the given account looking for a matching position. object[] key = new object[] { accountRow.AccountId, securityRow.SecurityId, positionTypeCode }; foreach (DataRowView dataRowView in ClientMarketData.ProposedOrder.UKProposedOrderAccountIdSecurityIdPositionTypeCode.FindRows(key)) { // This is used to reference the current proposed order that matches the position criteria. ClientMarketData.ProposedOrderRow parentProposedOrderRow = (ClientMarketData.ProposedOrderRow)dataRowView.Row; // This check is provided for currency-like assets. There may be many proposed orders for currency // transactions that are used to settle other trades. The user can also enter currency orders directly into // the appraisal. Any manual deposits or withdrawls should not impact settlement orders. This check will skip // any trade that is linked to another order. if (Shadows.Quasar.Common.Relationship.IsChildProposedOrder(parentProposedOrderRow)) { continue; } // Recycle the first proposed order that matches the position criteria. Any additional proposed orders for the // same account, security, position type will be deleted. if (firstTime) { // Any proposed orders found after this one will be deleted. This variable will also indicate that an // existing proposed order was recycled. After the loop is run on this position, a new order will be // created if an existing order couldn't be recycled. firstTime = false; // Create the command to update this proposed order. Update(remoteBatch, remoteTransaction, parentProposedOrderRow, proposedQuantity); } else { // Any order that isn't recycled is considered to be redundant. That is, this order has been superceded by // the recycled order. Clearing any redundant orders makes the operation more intuitive: the user knows // that the only order on the books is the one they entered. They don't have to worry about artifacts from // other operations. Delete(remoteBatch, remoteTransaction, parentProposedOrderRow); } } // This will create a new proposed order if an existing one couldn't be found above for recycling. if (firstTime == true) { Insert(remoteBatch, remoteTransaction, accountRow, securityRow, positionTypeCode, proposedQuantity); } } }
/// <summary> /// Creates a temporary model based on the current position level targets. /// </summary> /// <param name="accountRow">An account used as a basis for the targets.</param> /// <param name="schemeRow">The scheme used to select sector targets.</param> /// <returns>A batch of commands that will create a model containing the current position weights of the account.</returns> private static ModelBatch CreatePositionSelfModel(ClientMarketData.AccountRow accountRow, ClientMarketData.SchemeRow schemeRow) { // Create the batch and fill it in with the assembly and type needed for this function. ModelBatch modelBatch = new ModelBatch(); RemoteTransaction remoteTransaction = modelBatch.Transactions.Add(); RemoteAssembly remoteAssembly = modelBatch.Assemblies.Add("Service.Core"); RemoteType remoteType = remoteAssembly.Types.Add("Shadows.WebService.Core.Model"); // Create the temporary, position model based on the scheme used by the original account. RemoteMethod insertModel = remoteType.Methods.Add("Insert"); insertModel.Parameters.Add("modelId", DataType.Int, Direction.ReturnValue); insertModel.Parameters.Add("rowVersion", DataType.Long, Direction.Output); insertModel.Parameters.Add("modelTypeCode", ModelType.Security); insertModel.Parameters.Add("name", "Untitled"); insertModel.Parameters.Add("schemeId", schemeRow.SchemeId); insertModel.Parameters.Add("algorithmId", Algorithm.SecurityRebalancer); insertModel.Parameters.Add("temporary", true); // The 'Self Security' model uses the market value of all the positions, regardless of account or sub-account, when // calculating the denominator for the percentages. decimal accountMarketValue = MarketValue.Calculate(accountRow.CurrencyRow, accountRow, MarketValueFlags.EntirePosition | MarketValueFlags.IncludeChildAccounts); // If the account market value is zero, we can't do much more to create a model. if (accountMarketValue != 0.0M) { // Create a new outline for the model to follow. This will collect the tax lots, proposed orders orders and // allocations into positions that can be used for calculating percentages. Common.Appraisal appraisal = new Common.Appraisal(accountRow, true); // Run through each of the positions, starting with the security. foreach (AppraisalSet.SecurityRow driverSecurity in appraisal.Security) { // This is a position is the destination for the market value calculation. ClientMarketData.SecurityRow securityRow = ClientMarketData.Security.FindBySecurityId(driverSecurity.SecurityId); // The object Type for this operation. RemoteType positionTargetType = remoteAssembly.Types.Add("Shadows.WebService.Core.PositionTarget"); // Run through each of the positions in the appraisal calculating the market value of each position. The ratio // of this market value to the account's market value is the model percentage. foreach (AppraisalSet.PositionRow positionRow in driverSecurity.GetPositionRows()) { // Calculate the market value of the given position. decimal securityMarketValue = MarketValue.Calculate(accountRow.CurrencyRow, accountRow, securityRow, positionRow.PositionTypeCode, MarketValueFlags.EntirePosition | MarketValueFlags.EntirePosition); // Add the position level target to the model. RemoteMethod insertPosition = positionTargetType.Methods.Add("Insert"); insertPosition.Parameters.Add("modelId", insertModel.Parameters["modelId"]); insertPosition.Parameters.Add("securityId", securityRow.SecurityId); insertPosition.Parameters.Add("positionTypeCode", positionRow.PositionTypeCode); insertPosition.Parameters.Add("percent", securityMarketValue / accountMarketValue); } } } // Save the reference to the 'modelId' return parameter. modelBatch.ModelIdParameter = insertModel.Parameters["modelId"]; // This batch will create a temporary model based on the position totals of the original account. return(modelBatch); }
/// <summary> /// Rebalances an AppraisalModelSet to sector targets. The model is applied to the aggregate market value of the /// account and it's children. /// </summary> /// <param name="accountId">The parent account to be rebalanced.</param> /// <param name="modelId">The sector model to be used.</param> /// <returns>A set of proposed orders.</returns> public static RemoteBatch Rebalance(ClientMarketData.AccountRow accountRow, ClientMarketData.ModelRow modelRow) { // Make sure the scheme still exists in the in-memory database. We need it to rebalance to calculate // sector totals. ClientMarketData.SchemeRow schemeRow; if ((schemeRow = ClientMarketData.Scheme.FindBySchemeId(modelRow.SchemeId)) == null) { throw new ArgumentException("Scheme doesn't exist in the ClientMarketData", modelRow.SchemeId.ToString()); } // All the market values need to be normalized to a single currency so the sectors can be aggregated. This // value is made available to all methods through a member rather than passed on the stack. ClientMarketData.CurrencyRow currencyRow = accountRow.CurrencyRow; // The final result of this method is a command batch that can be sent to the server. RemoteBatch remoteBatch = new RemoteBatch(); RemoteTransaction remoteTransaction = remoteBatch.Transactions.Add(); // Calculate the total market value for the appraisal and all the sub-accounts. This will be the denominator // in all calculations involving sector percentages. This feature makes a 'Merge' rebalancer different from a // 'Wrap' rebalance. The 'Wrap' uses the sub-account's market value as the denominator when calculating // sector market values. decimal accountMarketValue = MarketValue.Calculate(accountRow.CurrencyRow, accountRow, MarketValueFlags.EntirePosition | MarketValueFlags.IncludeChildAccounts); // The outline of the appraisal will be needed to make calculations based on a position, that is a security, // account, position type combination grouped by a security classification scheme. AppraisalSet appraisalSet = new Appraisal(accountRow, schemeRow, true); // By cycling through all the immediate children of the scheme record, we'll have covered the top-level // sectors in this appraisal. foreach (AppraisalSet.SchemeRow driverScheme in appraisalSet.Scheme) { foreach (AppraisalSet.ObjectTreeRow driverTree in driverScheme.ObjectRow.GetObjectTreeRowsByFKObjectObjectTreeParentId()) { foreach (AppraisalSet.SectorRow driverSector in driverTree.ObjectRowByFKObjectObjectTreeChildId.GetSectorRows()) { // The appraisal set collects the ids of the records used. We need to look up the actual sector // record from the ClientMarketData in order to search through it and aggregate sub-sectors and // securities. ClientMarketData.SectorRow sectorRow = ClientMarketData.Sector.FindBySectorId(driverSector.SectorId); // Get the market value of the top-level sector, including all subaccounts and all positions. decimal actualSectorMarketValue = MarketValue.Calculate(currencyRow, accountRow, sectorRow, MarketValueFlags.EntirePosition | MarketValueFlags.IncludeChildAccounts); // This will find the model percentage of the current top-level sector. If the sector wasn't // specified in the model, assume a value of zero, which would indicate that we're to sell the // entire sector. ClientMarketData.SectorTargetRow sectorTargetRow = ClientMarketData.SectorTarget.FindByModelIdSectorId(modelRow.ModelId, driverSector.SectorId); decimal targetPercent = sectorTargetRow == null ? 0.0M : sectorTargetRow.Percent; // The target market value is calculated from the model percentage and the actual aggregate // account market value. decimal targetSectorMarketValue = accountMarketValue * targetPercent; // Now that we have a target to shoot for, recursively descend into the structure calculating // propsed orders. RecurseSectors(remoteBatch, remoteTransaction, currencyRow, modelRow, driverSector.ObjectRow, actualSectorMarketValue, targetSectorMarketValue); } } } // This object holds a complete set of proposed orders to achieve the sector targets in the model. return(remoteBatch); }
/// <summary> /// Rebalances an account to the sector targets, then recursively rebalances the children accounts. /// </summary> /// <param name="orderFormBuilder">A collection of orders.</param> /// <param name="accountRow">The parent account to be rebalanced.</param> /// <param name="modelRow">The model containing the sector targets.</param> /// <param name="schemeRow">The outline scheme used to define the sector contents.</param> private static void RecurseAccounts(RemoteBatch remoteBatch, RemoteTransaction remoteTransaction, ClientMarketData.AccountRow accountRow, ClientMarketData.ModelRow modelRow, ClientMarketData.SchemeRow schemeRow) { // All the market values of all the securities in this account are normalized to a single currency so they can // be aggregated. ClientMarketData.CurrencyRow currencyRow = ClientMarketData.Currency.FindByCurrencyId(accountRow.CurrencyId); // Calculate the total market value for the appraisal without including child accounts. This is a 'Wrap' // rebalancing, so we're only concerned with what's in this account. The account's market value will be the // denominator in all calculations involving sector percentages. decimal accountMarketValue = MarketValue.Calculate(currencyRow, accountRow, MarketValueFlags.EntirePosition); // The outline of the appraisal will be needed to make market value calculations based on a sector. Note that // we're not including the child accounts in the outline. Wrap rebalancing works only on a single account at // a time. AppraisalSet appraisalSet = new Appraisal(accountRow, schemeRow, false); // By cycling through all the immediate children of the scheme record, we'll have covered the top-level // sectors in this appraisal. foreach (AppraisalSet.SchemeRow driverScheme in appraisalSet.Scheme) { foreach (AppraisalSet.ObjectTreeRow driverTree in driverScheme.ObjectRow.GetObjectTreeRowsByFKObjectObjectTreeParentId()) { foreach (AppraisalSet.SectorRow driverSector in driverTree.ObjectRowByFKObjectObjectTreeChildId.GetSectorRows()) { // Find the sectors row record that corresponds to the current sector in the appraisal set. ClientMarketData.SectorRow sectorRow = ClientMarketData.Sector.FindBySectorId(driverSector.SectorId); // Get the market value of the top-level sector, including all sub-sectors and all positions // belonging to only the current account. decimal actualSectorMarketValue = MarketValue.Calculate(currencyRow, accountRow, sectorRow, MarketValueFlags.EntirePosition); // This will find the model percentage of the current top-level sector. If the sector wasn't // specified in the model, assume a value of zero, which would indicate that we're to sell the // entire sector. ClientMarketData.SectorTargetRow sectorTargetRow = ClientMarketData.SectorTarget.FindByModelIdSectorId(modelRow.ModelId, driverSector.SectorId); decimal targetPercent = (sectorTargetRow == null) ? 0.0M : sectorTargetRow.Percent; // The sector's target market value is calculated from the model percentage and the current // account market value. This is placed in a member variable so it's available to the methods // when we recurse. decimal targetSectorMarketValue = accountMarketValue * targetPercent; // Now that we have a sector target to shoot for, recursively descend into the structure // calculating proposed orders. SectorWrap.RecurseSectors(remoteBatch, remoteTransaction, modelRow, driverSector, actualSectorMarketValue, targetSectorMarketValue); } } } // Now that we've rebalanced the parent account, cycle through all the children accounts and rebalance them. foreach (ClientMarketData.ObjectTreeRow objectTreeRow in accountRow.ObjectRow.GetObjectTreeRowsByFKObjectObjectTreeParentId()) { foreach (ClientMarketData.AccountRow childAccount in objectTreeRow.ObjectRowByFKObjectObjectTreeChildId.GetAccountRows()) { SectorWrap.RecurseAccounts(remoteBatch, remoteTransaction, childAccount, modelRow, schemeRow); } } }
/// <summary> /// Recursively calculates proposed orders for a sector. /// </summary> /// <param name="sector">Gives the current sector (sector) for the calculation.</param> private static void RecurseSectors(RemoteBatch remoteBatch, RemoteTransaction remoteTransaction, ClientMarketData.CurrencyRow currencyRow, ClientMarketData.ModelRow modelRow, AppraisalSet.ObjectRow driverObject, decimal actualSectorMarketValue, decimal targetSectorMarketValue) { // Run through each of the positions in the sector and calculate the current percentage of the position within // the sector. We're going to keep this percentage as we rebalance to the new sector market value. foreach (AppraisalSet.SecurityRow driverSecurity in driverObject.GetSecurityRows()) { foreach (AppraisalSet.PositionRow driverPosition in driverSecurity.GetPositionRows()) { // We need to know what kind of security we're dealing with when calculating market values and quantities // below. ClientMarketData.SecurityRow securityRow = ClientMarketData.Security.FindBySecurityId(driverSecurity.SecurityId); // In this rebalancing operation, the cash balance is dependant on the securities bought and sold. When // stocks are bought or sold below, they will impact the underlying currency. We can not balance to a // currency target directly. if (securityRow.SecurityTypeCode == SecurityType.Currency) { continue; } // Calculate the proposed orders for each account. The fraction of the security within the sector will // stay the same, even though the sector may increase or decrease with respect to the total market value. foreach (AppraisalSet.AccountRow driverAccount in driverPosition.GetAccountRows()) { // The underlying currency is needed for the market value calculations. ClientMarketData.AccountRow accountRow = ClientMarketData.Account.FindByAccountId(driverAccount.AccountId); // Sector rebalancing keeps the percentage of a security within the sector constant. Only the overall // percentage of the sector with respect to the NAV changes. To accomplish this, we first calculate // the percentage of the security within the sector before we rebalance the sector. decimal actualPositionMarketValue = MarketValue.Calculate(currencyRow, accountRow, securityRow, driverPosition.PositionTypeCode, MarketValueFlags.EntirePosition); // Calculate the target market value as a percentage of the entire sector (use zero if the sector has // no market value to prevent divide by zero errors). decimal targetPositionMarketValue = (actualSectorMarketValue == 0) ? 0.0M : actualPositionMarketValue * targetSectorMarketValue / actualSectorMarketValue; // The target proposed orders market value keeps the percentage of the position constant while // changing the overall sector percentage. decimal proposedMarketValue = targetPositionMarketValue - MarketValue.Calculate(currencyRow, accountRow, securityRow, driverPosition.PositionTypeCode, MarketValueFlags.ExcludeProposedOrder); // Calculate the quantity needed to hit the target market value and round it according to the // model. Note that the market values and prices are all denominated in the currency of the // parent account. Also note the quantityFactor is needed for the proper quantity calculation. decimal proposedQuantity = proposedMarketValue / (Price.Security(currencyRow, securityRow) * securityRow.PriceFactor * securityRow.QuantityFactor); // If we have an equity, round to the model's lot size. if (securityRow.SecurityTypeCode == SecurityType.Equity) { proposedQuantity = Math.Round(proposedQuantity / modelRow.EquityRounding, 0) * modelRow.EquityRounding; } // A debt generally needs to be rounded to face. if (securityRow.SecurityTypeCode == SecurityType.Debt) { proposedQuantity = Math.Round(proposedQuantity / modelRow.DebtRounding, 0) * modelRow.DebtRounding; } // Have the Order Form Builder object construct an order based on the quantity we've calcuated from // the market value. This method will fill in the defaults needed for a complete proposed order. ProposedOrder.Create(remoteBatch, remoteTransaction, accountRow, securityRow, driverAccount.PositionTypeCode, proposedQuantity); } } } // Recurse into each of the sub-sectors. This allows us to rebalance with any number of levels to the // hierarchy. Eventually, we will run across a sector with security positions in it and end up doing some // real work. foreach (AppraisalSet.ObjectTreeRow driverTree in driverObject.GetObjectTreeRowsByFKObjectObjectTreeParentId()) { SectorMerge.RecurseSectors(remoteBatch, remoteTransaction, currencyRow, modelRow, driverTree.ObjectRowByFKObjectObjectTreeChildId, actualSectorMarketValue, targetSectorMarketValue); } }
/// <summary> /// Recursively calculates proposed orders for a sector. /// </summary> /// <param name="sector">Gives the current sector (sector) for the calculation.</param> private static void RecurseSectors(RemoteBatch remoteBatch, RemoteTransaction remoteTransaction, ClientMarketData.ModelRow modelRow, AppraisalSet.SectorRow driverSector, decimal actualSectorMarketValue, decimal targetSectorMarketValue) { // The main idea here is to keep the ratio of the security to the sector constant, while changing the market // value of the sector. Scan each of the securities belonging to this sector. foreach (AppraisalSet.ObjectTreeRow objectTreeRow in driverSector.ObjectRow.GetObjectTreeRowsByFKObjectObjectTreeParentId()) { // Cycle through each of the securities in the sector. We're going to keep the ratio of the security the // same as we target a different sector total. foreach (AppraisalSet.SecurityRow driverSecurity in objectTreeRow.ObjectRowByFKObjectObjectTreeChildId.GetSecurityRows()) { foreach (AppraisalSet.PositionRow driverPosition in driverSecurity.GetPositionRows()) { // We need to reference the security record for calculating proposed orders and the market value // of the trade. ClientMarketData.SecurityRow securityRow = ClientMarketData.Security.FindBySecurityId(driverSecurity.SecurityId); // In this rebalancing operation, the cash balance is dependant on the securities bought and // sold. When stocks are bought or sold below, they will impact the underlying currency. A cash // target can be reached by setting all the other percentages up properly. As long as the total // percentage in a model is 100%, the proper cash target will be calculated. We don't have to do // anything with this asset type. if (securityRow.SecurityTypeCode == SecurityType.Currency) { continue; } // The ratio of the security within the sector will stay constant, even though the sector may // increase or decrease with the target in the model. Note that there's only one account in the // 'Accounts' table of the driver because this is a 'Wrap' operation. foreach (AppraisalSet.AccountRow driverAccount in driverPosition.GetAccountRows()) { // Find the account associated with the driver record. ClientMarketData.AccountRow accountRow = ClientMarketData.Account.FindByAccountId(driverAccount.AccountId); // The market value of all the securities are normalized to the base currency of the account // so they can be aggregated. ClientMarketData.CurrencyRow currencyRow = ClientMarketData.Currency.FindByCurrencyId(accountRow.CurrencyId); // Sector rebalancing keeps the percentage of a security within the sector constant. Only the // overall percentage of the sector with respect to the NAV changes. The first step in this // rebalancing operation is to calculate the market value of the given position. decimal actualPositionMarketValue = MarketValue.Calculate(currencyRow, accountRow, securityRow, driverPosition.PositionTypeCode, MarketValueFlags.EntirePosition); // The target market value operation keeps the percentage of the position constant while // changing the overall sector percentage. decimal targetPositionMarketValue = (actualSectorMarketValue == 0) ? 0.0M : actualPositionMarketValue * targetSectorMarketValue / actualSectorMarketValue; // Calculate the market value of an order that will achieve the target. Note that we're not // including the existing proposed orders in the market value, but we did include them when // calculating the account's market value. This allows us to put in what-if orders that will // impact the market value before we do the rebalancing. decimal proposedMarketValue = targetPositionMarketValue - MarketValue.Calculate(currencyRow, accountRow, securityRow, driverPosition.PositionTypeCode, MarketValueFlags.ExcludeProposedOrder); // Calculate the quantity needed to hit the target market value and round it according to the // model. Note that the market values and prices are all denominated in the currency of the // parent account. Also note the quantityFactor is needed for the proper quantity // calculation. decimal proposedQuantity = proposedMarketValue / (Price.Security(currencyRow, securityRow) * securityRow.QuantityFactor); // If we have an equity, round to the model's lot size. if (securityRow.SecurityTypeCode == SecurityType.Equity) { proposedQuantity = Math.Round(proposedQuantity / modelRow.EquityRounding, 0) * modelRow.EquityRounding; } // A debt generally needs to be rounded to face. if (securityRow.SecurityTypeCode == SecurityType.Debt) { proposedQuantity = Math.Round(proposedQuantity / modelRow.DebtRounding, 0) * modelRow.DebtRounding; } // Have the OrderForm object construct an order based on the quantity we've calcuated // from the market value. This will fill in the defaults for the order and translate the // signed quantities into transaction codes. ProposedOrder.Create(remoteBatch, remoteTransaction, accountRow, securityRow, driverAccount.PositionTypeCode, proposedQuantity); } } } // Recurse into each of the sub-sectors. This allows us to rebalance with any number of levels to the // hierarchy. Eventually, we will run across a sector with security positions in it and end up doing some // real work. foreach (AppraisalSet.SectorRow childSector in objectTreeRow.ObjectRowByFKObjectObjectTreeChildId.GetSectorRows()) { SectorWrap.RecurseSectors(remoteBatch, remoteTransaction, modelRow, childSector, actualSectorMarketValue, targetSectorMarketValue); } } }
/// <summary> /// Creates a command in a RemoteBatch structure to insert a proposed order. /// </summary> /// <param name="remoteBatch"></param> /// <param name="remoteTransaction"></param> /// <param name="accountRow"></param> /// <param name="securityRow"></param> /// <param name="positionTypeCode"></param> /// <param name="quantityInstruction"></param> private static void Insert(RemoteBatch remoteBatch, RemoteTransaction remoteTransaction, ClientMarketData.AccountRow accountRow, ClientMarketData.SecurityRow securityRow, int positionTypeCode, decimal quantityInstruction) { // These define the assembly and the types within those assemblies that will be used to create the proposed orders on // the middle tier. RemoteAssembly remoteAssembly = remoteBatch.Assemblies.Add("Service.Core"); RemoteType proposedOrderType = remoteAssembly.Types.Add("Shadows.WebService.Core.ProposedOrder"); RemoteType proposedOrderTreeType = remoteAssembly.Types.Add("Shadows.WebService.Core.ProposedOrderTree"); // Find the default settlement for this order. int settlementId = Shadows.Quasar.Common.Security.GetDefaultSettlementId(securityRow); // As a convention between the rebalancing section and the order generation, the parentQuantity passed into this method // is a signed value where the negative values are treated as 'Sell' instructions and the positive values meaning // 'Buy'. This will adjust the parentQuantity so the trading methods can deal with an unsigned value, which is more // natural for trading. decimal parentQuantity = Math.Abs(quantityInstruction); // This will turn the signed parentQuantity into an absolute parentQuantity and a transaction code (e.g. -1000 is // turned into a SELL of 1000 shares). int parentTransactionTypeCode = TransactionType.Calculate(securityRow.SecurityTypeCode, positionTypeCode, quantityInstruction); // The time in force first comes from the user preferences, next, account settings and finally defaults to a day // orders. int timeInForceCode = !ClientPreferences.IsTimeInForceCodeNull() ? ClientPreferences.TimeInForceCode : !accountRow.IsTimeInForceCodeNull() ? accountRow.TimeInForceCode : TimeInForce.DAY; // The destination blotter comes first from the user preferences, second from the account preferences, and finally uses // the auto-routing logic. int parentBlotterId = ClientPreferences.IsBlotterIdNull() ? (accountRow.IsBlotterIdNull() ? TradingSupport.AutoRoute(securityRow, parentQuantity) : accountRow.BlotterId) : ClientPreferences.BlotterId; // Create a command to delete the relationship between the parent and child. RemoteMethod insertParent = proposedOrderType.Methods.Add("Insert"); insertParent.Transaction = remoteTransaction; insertParent.Parameters.Add("proposedOrderId", DataType.Int, Direction.ReturnValue); insertParent.Parameters.Add("blotterId", parentBlotterId); insertParent.Parameters.Add("accountId", accountRow.AccountId); insertParent.Parameters.Add("securityId", securityRow.SecurityId); insertParent.Parameters.Add("settlementId", settlementId); insertParent.Parameters.Add("positionTypeCode", positionTypeCode); insertParent.Parameters.Add("transactionTypeCode", parentTransactionTypeCode); insertParent.Parameters.Add("timeInForceCode", timeInForceCode); insertParent.Parameters.Add("orderTypeCode", OrderType.Market); insertParent.Parameters.Add("quantity", parentQuantity); // Now it's time to create an order for the settlement currency. if (securityRow.SecurityTypeCode == SecurityType.Equity || securityRow.SecurityTypeCode == SecurityType.Debt) { // The underlying currency is needed for the market value calculations. ClientMarketData.CurrencyRow currencyRow = MarketData.Currency.FindByCurrencyId(settlementId); decimal marketValue = parentQuantity * securityRow.QuantityFactor * Price.Security(currencyRow, securityRow) * securityRow.PriceFactor * TransactionType.GetCashSign(parentTransactionTypeCode); // The stragegy for handling the settlement currency changes is to calculate the old market value, calculate the // new market value, and add the difference to the running total for the settlement currency of this security. The // new market value is the impact of the trade that was just entered. int childTransactionTypeCode = TransactionType.Calculate(securityRow.SecurityTypeCode, positionTypeCode, marketValue); decimal childQuantity = Math.Abs(marketValue); // The destination blotter comes first from the user preferences, second from the account preferences, and finally // uses the auto-routing logic. int childBlotterId = ClientPreferences.IsBlotterIdNull() ? (accountRow.IsBlotterIdNull() ? TradingSupport.AutoRoute(currencyRow.SecurityRow, childQuantity) : accountRow.BlotterId) : ClientPreferences.BlotterId; // Fill in the rest of the fields and the defaulted fields for this order. Create a command to delete the // relationship between the parent and child. RemoteMethod insertChild = proposedOrderType.Methods.Add("Insert"); insertChild.Transaction = remoteTransaction; insertChild.Parameters.Add("proposedOrderId", DataType.Int, Direction.ReturnValue); insertChild.Parameters.Add("blotterId", childBlotterId); insertChild.Parameters.Add("accountId", accountRow.AccountId); insertChild.Parameters.Add("securityId", settlementId); insertChild.Parameters.Add("settlementId", settlementId); insertChild.Parameters.Add("transactionTypeCode", childTransactionTypeCode); insertChild.Parameters.Add("positionTypeCode", positionTypeCode); insertChild.Parameters.Add("timeInForceCode", timeInForceCode); insertChild.Parameters.Add("orderTypeCode", OrderType.Market); insertChild.Parameters.Add("quantity", childQuantity); RemoteMethod insertRelation = proposedOrderTreeType.Methods.Add("Insert"); insertRelation.Transaction = remoteTransaction; insertRelation.Parameters.Add("parentId", insertParent.Parameters["proposedOrderId"]); insertRelation.Parameters.Add("childId", insertChild.Parameters["proposedOrderId"]); } }