private void addTransferToPosition(AssetTransferData transferRequest) { ClientPositionData ClientPosition = new ClientPositionData(); TokenRateData TokenRate = new TokenRateData(); ClientPosition clientPositionRecord; lock (ClientPositionLock) { if (ClientPositions.TryGetValue(transferRequest.clientId, out clientPositionRecord) == false) { clientPositionRecord = new ClientPosition(transferRequest.clientId); ClientPositions.TryAdd(transferRequest.clientId, clientPositionRecord); } if (clientPositionRecord == null) { throw new Exception("Unable to find/create client position record"); } if (transferRequest.transferType == TransferTypeEnum.Deposit) { if (clientPositionRecord.ClientPositions.TryGetValue(transferRequest.tokenId, out ClientPosition) == true) { ClientPosition.TokenAmount += transferRequest.tokenAmount; ClientPosition.TokenValue = ClientPosition.TokenAmount * ClientPosition.TokenRate; } else { ClientPosition = new ClientPositionData(); ClientPosition.TokenId = transferRequest.tokenId; ClientPosition.TokenAmount = transferRequest.tokenAmount; ClientPosition.TokenRate = 0.0m; ClientPosition.TokenRateIn = "USD"; if (TokenRates.TryGetValue(ClientPosition.TokenId, out TokenRate) == true) { ClientPosition.TokenRate = (decimal)TokenRate.AskRate; ClientPosition.TokenRateIn = TokenRate.PriceCurrency; } ClientPosition.TokenValue = ClientPosition.TokenAmount * ClientPosition.TokenRate; clientPositionRecord.ClientPositions.TryAdd(ClientPosition.TokenId, ClientPosition); } } else { if (clientPositionRecord.ClientPositions.TryGetValue(transferRequest.tokenId, out ClientPosition) == true) { ClientPosition.TokenAmount -= transferRequest.tokenAmount; ClientPosition.TokenValue = ClientPosition.TokenAmount * ClientPosition.TokenRate; } } } }
private void addFilledOrderToPosition(OrderLeg orderLegRecord, OrderFilledData orderFilledDataRecord) { ClientPosition clientPositionRecord; ClientPositionData ClientPosition = new ClientPositionData(); TokenRateData TokenRate = new TokenRateData(); int clientId = orderLegRecord.Order.ClientId; if (ClientPositions.TryGetValue(clientId, out clientPositionRecord) == false) { clientPositionRecord = new ClientPosition(clientId); ClientPositions.TryAdd(clientId, clientPositionRecord); } if (clientPositionRecord == null) { throw new Exception("Unable to find/generate client position record"); } if (orderLegRecord.BuySellType == OrderLegBuySellEnum.Buy) { if (clientPositionRecord.ClientPositions.TryGetValue(orderFilledDataRecord.Token1Id, out ClientPosition) == true) { ClientPosition.TokenAmount += orderFilledDataRecord.Token1Amount; ClientPosition.TokenValue = ClientPosition.TokenAmount * ClientPosition.TokenRate; } else { ClientPosition = new ClientPositionData(); ClientPosition.TokenId = orderFilledDataRecord.Token1Id; ClientPosition.TokenAmount = orderFilledDataRecord.Token1Amount; ClientPosition.TokenRate = 0.0m; ClientPosition.TokenRateIn = "USD"; if (TokenRates.TryGetValue(ClientPosition.TokenId, out TokenRate) == true) { ClientPosition.TokenRate = (decimal)TokenRate.AskRate; ClientPosition.TokenRateIn = TokenRate.PriceCurrency; } ClientPosition.TokenValue = ClientPosition.TokenAmount * ClientPosition.TokenRate; clientPositionRecord.ClientPositions.TryAdd(ClientPosition.TokenId, ClientPosition); } if (clientPositionRecord.ClientPositions.TryGetValue(orderFilledDataRecord.Token2Id, out ClientPosition) == true) { ClientPosition.TokenAmount -= orderFilledDataRecord.Token2Amount; ClientPosition.TokenValue = ClientPosition.TokenAmount * ClientPosition.TokenRate; } else { ClientPosition = new ClientPositionData(); ClientPosition.TokenId = orderFilledDataRecord.Token2Id; ClientPosition.TokenAmount -= orderFilledDataRecord.Token2Amount; ClientPosition.TokenRate = 0.0m; ClientPosition.TokenRateIn = "USD"; if (TokenRates.TryGetValue(ClientPosition.TokenId, out TokenRate) == true) { ClientPosition.TokenRate = (decimal)TokenRate.AskRate; ClientPosition.TokenRateIn = TokenRate.PriceCurrency; } ClientPosition.TokenValue = ClientPosition.TokenAmount * ClientPosition.TokenRate; clientPositionRecord.ClientPositions.TryAdd(ClientPosition.TokenId, ClientPosition); } } else { if (clientPositionRecord.ClientPositions.TryGetValue(orderFilledDataRecord.Token2Id, out ClientPosition) == true) { ClientPosition.TokenAmount += orderFilledDataRecord.Token2Amount; ClientPosition.TokenValue = ClientPosition.TokenAmount * ClientPosition.TokenRate; } else { ClientPosition = new ClientPositionData(); ClientPosition.TokenId = orderFilledDataRecord.Token2Id; ClientPosition.TokenAmount = orderFilledDataRecord.Token2Amount; ClientPosition.TokenRate = 0.0m; ClientPosition.TokenRateIn = "USD"; if (TokenRates.TryGetValue(ClientPosition.TokenId, out TokenRate) == true) { ClientPosition.TokenRate = (decimal)TokenRate.AskRate; ClientPosition.TokenRateIn = TokenRate.PriceCurrency; } ClientPosition.TokenValue = ClientPosition.TokenAmount * ClientPosition.TokenRate; clientPositionRecord.ClientPositions.TryAdd(ClientPosition.TokenId, ClientPosition); } if (clientPositionRecord.ClientPositions.TryGetValue(orderFilledDataRecord.Token1Id, out ClientPosition) == true) { ClientPosition.TokenAmount -= orderFilledDataRecord.Token1Amount; ClientPosition.TokenValue = ClientPosition.TokenAmount * ClientPosition.TokenRate; } else { ClientPosition = new ClientPositionData(); ClientPosition.TokenId = orderFilledDataRecord.Token1Id; ClientPosition.TokenAmount -= orderFilledDataRecord.Token1Amount; ClientPosition.TokenRate = 0.0m; ClientPosition.TokenRateIn = "USD"; if (TokenRates.TryGetValue(ClientPosition.TokenId, out TokenRate) == true) { ClientPosition.TokenRate = (decimal)TokenRate.AskRate; ClientPosition.TokenRateIn = TokenRate.PriceCurrency; } ClientPosition.TokenValue = ClientPosition.TokenAmount * ClientPosition.TokenRate; clientPositionRecord.ClientPositions.TryAdd(ClientPosition.TokenId, ClientPosition); } } }