public void TransactionCreditDebit(Entry entry)
        {
            AccountToFrom accountToFrom = MapperAccount(entry);

            accountToFrom = ValidationAccounts(accountToFrom);
            SaveTransaction(accountToFrom, entry);
        }
        private void SaveTransaction(AccountToFrom accountToFrom, Entry entry)
        {
            Lancamentos lancamento = new Lancamentos()
            {
                AccountFromId = accountToFrom.AccountFrom.AccountId,
                AccountToId   = accountToFrom.AccountTo.AccountId,
                Status        = 0,
                Value         = entry.Value
            };

            _entryAppService.Add(lancamento);
        }
        private bool Transaction(AccountToFrom accountToFrom, Entry entry)
        {
            accountToFrom.AccountFrom.Balance = accountToFrom.AccountFrom.Balance - entry.Value;

            accountToFrom.AccountTo.Balance = accountToFrom.AccountTo.Balance + entry.Value;

            _accountAppService.Update(accountToFrom.AccountFrom);

            _accountAppService.Update(accountToFrom.AccountTo);


            return(true);
        }
        private AccountToFrom ValidationAccounts(AccountToFrom accountToFrom)
        {
            Account AccountTo = _accountAppService.GetAccouts(accountToFrom.AccountTo.NumberAccount, accountToFrom.AccountTo.NumberAgency, accountToFrom.AccountTo.NumberDigit);

            if (AccountTo == null)
            {
                throw new ArgumentException("Conta Origem inesistente");
            }

            Account AccountFrom = _accountAppService.GetAccouts(accountToFrom.AccountFrom.NumberAccount, accountToFrom.AccountFrom.NumberAgency, accountToFrom.AccountFrom.NumberDigit);

            if (AccountFrom == null)
            {
                throw new ArgumentException("Conta Destino inesistente");
            }

            accountToFrom.AccountFrom = AccountFrom;
            accountToFrom.AccountTo   = AccountTo;
            return(accountToFrom);
        }
        private AccountToFrom MapperAccount(Entry entry)
        {
            AccountToFrom accountToFrom = new AccountToFrom();
            Account       accountFrom   = new Account()
            {
                NumberAccount = entry.NumberAccountFrom,
                NumberAgency  = entry.NumberAgencyFrom,
                NumberDigit   = entry.NumberDigitFrom
            };

            Account accounTo = new Account()
            {
                NumberAccount = entry.NumberAccountTo,
                NumberAgency  = entry.NumberAgencyTo,
                NumberDigit   = entry.NumberDigitTo
            };

            accountToFrom.AccountFrom = accountFrom;
            accountToFrom.AccountTo   = accounTo;

            return(accountToFrom);
        }
コード例 #6
0
        /// <summary>
        /// Run for each day that the Tax Lot remains open / partially closed
        /// </summary>
        /// <param name="env"></param>
        /// <param name="element">Trade we aee interested in</param>
        public void DailyEvent(PostingEngineEnvironment env, Transaction element)
        {
            double fxrate = 1.0;

            // Lets get fx rate if needed
            if (!element.SettleCurrency.Equals(env.BaseCurrency))
            {
                fxrate = Convert.ToDouble(FxRates.Find(env.ValueDate, element.SettleCurrency).Rate);
            }

            // Calculate the unrealized PNL
            if (env.TaxLotStatus.ContainsKey(element.LpOrderId))
            {
                // Determine if we need to accumulate unrealized PNL
                var taxlot = env.TaxLotStatus[element.LpOrderId];

                // Check to see if the TaxLot is still open and it has a non zero Quantity
                if (!taxlot.Status.ToLowerInvariant().Equals("closed") && Math.Abs(taxlot.Quantity) > 0)
                {
                    var listOfTags = new List <Tag>
                    {
                        Tag.Find("SecurityType"),
                        Tag.Find("CustodianCode")
                    };

                    // We have an open / partially closed tax lot so now need to calculate unrealized Pnl
                    var quantity = taxlot.Quantity;

                    var prevEodPrice = 0.0;
                    var eodPrice     = 0.0;

                    if (env.ValueDate == element.TradeDate)
                    {
                        eodPrice     = MarketPrices.GetPrice(env, env.ValueDate, element).Price;
                        prevEodPrice = element.SettleNetPrice;
                    }
                    else
                    {
                        prevEodPrice = MarketPrices.GetPrice(env, env.PreviousValueDate, element).Price;
                        eodPrice     = MarketPrices.GetPrice(env, env.ValueDate, element).Price;
                    }

                    var unrealizedPnl = CommonRules.CalculateUnrealizedPnl(env, taxlot);

                    AccountToFrom fromToAccounts = null;

                    if (element.IsDerivative())
                    {
                        var originalAccount = AccountUtils.GetDerivativeAccountType(unrealizedPnl);
                        if (originalAccount.Contains("(Liabilities)"))
                        {
                            // This needs to be registered as a Credit to the Libabilities
                            unrealizedPnl *= -1;
                        }
                        fromToAccounts = new AccountUtils().GetAccounts(env, originalAccount, "Change in Unrealized Derivatives Contracts at Fair Value", listOfTags, taxlot.Trade);
                    }
                    else
                    {
                        var originalAccount = taxlot.Side == "SHORT" ? "Mark to Market Shorts" : "Mark to Market Longs";
                        fromToAccounts = new AccountUtils().GetAccounts(env, originalAccount, "CHANGE IN UNREALIZED GAIN/(LOSS)", listOfTags, taxlot.Trade);
                    }

                    var fund = env.GetFund(element);

                    var debit = new Journal(element)
                    {
                        Account     = fromToAccounts.From,
                        When        = env.ValueDate,
                        Symbol      = taxlot.Symbol,
                        Quantity    = quantity,
                        FxRate      = fxrate,
                        Value       = env.SignedValue(fromToAccounts.From, fromToAccounts.To, true, unrealizedPnl),
                        CreditDebit = env.DebitOrCredit(fromToAccounts.From, unrealizedPnl),
                        StartPrice  = prevEodPrice,
                        EndPrice    = eodPrice,
                        Event       = Event.DAILY_UNREALIZED_PNL,
                        Fund        = fund,
                    };

                    var credit = new Journal(element)
                    {
                        Account     = fromToAccounts.To,
                        When        = env.ValueDate,
                        FxRate      = fxrate,
                        Quantity    = quantity,
                        Value       = env.SignedValue(fromToAccounts.From, fromToAccounts.To, false, unrealizedPnl),
                        CreditDebit = env.DebitOrCredit(fromToAccounts.To, env.SignedValue(fromToAccounts.From, fromToAccounts.To, false, unrealizedPnl)),
                        Event       = Event.DAILY_UNREALIZED_PNL,
                        StartPrice  = prevEodPrice,
                        EndPrice    = eodPrice,
                        Fund        = fund,
                    };

                    Logger.Info($"[Journals] ==> From : {debit.CreditDebit}::{debit.Value}::{debit.Account.Type.Category.Name} --> To : {credit.CreditDebit}::{credit.Value}::{credit.Account.Type.Category.Name} ({unrealizedPnl})");

                    env.Journals.AddRange(new[] { debit, credit });

                    // For Derivatives this is un-necessary as we do not have an investment at cost, but we do have Fx on unsettled
                    if (taxlot.Quantity != 0.0)
                    {
                        if (element.TradeDate != env.ValueDate)
                        {
                            new FxPosting().CreateFxUnsettled(env, element);
                        }
                    }
                }
            }
            else
            {
                if (fxrate != 1.0)
                {
                    if (element.TradeDate != env.ValueDate && element.SettleDate >= env.ValueDate)
                    {
                        /*
                         * var fxJournals = FxPosting.CreateFx(
                         *  env,
                         *  "DUE FROM/(TO) PRIME BROKERS ( Unsettled Activity )",
                         *  "fx gain or loss on unsettled balance",
                         *  "daily",
                         *  element.Quantity, null, element);
                         * env.Journals.AddRange(fxJournals);
                         */
                    }
                }
            }
        }