Ejemplo n.º 1
0
        public ICollection <IInstruction> Deserialize(byte[] delta)
        {
            var instructions = new List <IInstruction>();

            using (MemoryStream stream = new MemoryStream(delta))
                using (BinaryReader reader = new BinaryReader(stream))
                {
                    while (reader.BaseStream.Position < reader.BaseStream.Length)
                    {
                        InstructionTypes instructionType = (InstructionTypes)reader.ReadByte();

                        switch (instructionType)
                        {
                        case InstructionTypes.Add:
                            var add = new Add();
                            add.Index = reader.ReadInt32();
                            int length = reader.ReadInt32();
                            add.Data = reader.ReadBytes(length);

                            instructions.Add(add);
                            break;

                        case InstructionTypes.Copy:
                            var copy = new Copy();
                            copy.Index  = reader.ReadInt32();
                            copy.Length = reader.ReadInt32();

                            instructions.Add(copy);
                            break;

                        case InstructionTypes.Run:
                            var run = new Run();
                            run.Length = reader.ReadInt32();
                            run.Value  = reader.ReadByte();

                            instructions.Add(run);
                            break;

                        default:
                            throw new FormatException();
                        }
                    }
                }

            return(instructions);
        }
Ejemplo n.º 2
0
        internal static bool rebalanceAccount(int accountId, InstructionTypes instructionType, OrderActionTypes actionType, bool noCharges, decimal depositCashPositionDiff, bool includePrevCash)
        {
            bool success = false;
            Money diff = null;
            IDalSession session = NHSessionFactory.CreateSession();

            IAccountTypeCustomer account = (IAccountTypeCustomer)AccountMapper.GetAccount(session, accountId);
            if (account.ActiveRebalanceInstructions != null && account.ActiveRebalanceInstructions.Count > 0)
                throw new ApplicationException(string.Format("The account {0} already has an active rebalance instruction.", account.Number));

            IList<IJournalEntryLine> cashTransfers = JournalEntryMapper.GetUnProcessedCashTransfers(session, account);
            if (cashTransfers == null || cashTransfers.Count == 0)
                throw new ApplicationException("It is not possible to do this rebalance without cash transfers");
            else
                diff = new Money(depositCashPositionDiff, cashTransfers[0].Currency);

            if (account.ModelPortfolio == null)
                throw new ApplicationException(string.Format("The account {0} does not have a model attached.", account.Number));

            IInstruction instruction = account.CreateInstruction(instructionType, actionType, DateTime.Now.Date, noCharges, cashTransfers);
            if (instruction != null)
            {
                // check total value of the transfers
                if (instructionType == InstructionTypes.BuyModel)
                {
                    IBuyModelInstruction bmi = (IBuyModelInstruction)instruction;
                    if (!(bmi.CashTransfers.TotalTransferAmount.IsGreaterThanZero && account.TotalCash.IsGreaterThanZero))
                        throw new ApplicationException("It is not possible to do this rebalance with a zero/negative cash transfer amount");

                    if (!(account.ActiveWithdrawalInstructions.Count == 0 && account.ActiveMoneyTransferOrders.Count == 0))
                        throw new ApplicationException(string.Format("It is not possible to do a buy model for account {0} since there are active money transfers/withdrawals.", account.Number));

                    if (account.OpenOrdersForAccount.NewCollection(x => x.Side == Side.Sell).Count > 0)
                        throw new ApplicationException(string.Format("It is not possible to do a buy model for account {0} since ther are sell orders.", account.Number));

                    if (account.OpenOrdersForAccount.Count > 0)
                    {
                        if (diff.IsLessThanZero)
                            throw new ApplicationException(string.Format("It is not possible to do a buy model for account {0} since the cash difference is negative.", account.Number));
                        if (account.TotalCash - account.OpenOrderAmount() < bmi.CashTransfers.TotalTransferAmount)
                            throw new ApplicationException(string.Format("It is not possible to do a buy model for account {0} since the cash is already spent.", account.Number));
                    }
                    else
                    {
                        if (diff != null && diff.IsNotZero)
                        {
                            // When there is a cash difference (max 15% of the deposit) -> clear it away
                            if (includePrevCash)
                            {
                                if ((bmi.CashTransfers.TotalTransferAmount + diff).IsLessThanZero)
                                    throw new ApplicationException(string.Format("It is not possible to do a buy model for account {0} since there is not enough cash.", account.Number));
                                bmi.DepositCashPositionDifference = diff;
                            }
                        }
                    }
                }

                ICurrency underlying = account.AccountOwner.StichtingDetails.BaseCurrency;
                InstructionEngineParameters engineParams = InstructionEngineParametersMapper.GetParameters(session);
                InstructionEngine engine = new InstructionEngine(engineParams);

                if (engine.ProcessInstruction(instruction))
                {
                    session.BeginTransaction();
                    AccountMapper.Update(session, account);
                    if (instruction.UpdateableOrders != null && instruction.UpdateableOrders.Count > 0)
                        OrderMapper.Insert(session, instruction.UpdateableOrders);
                    success = session.CommitTransaction();
                }
            }
            session.Close();
            return success;
        }
Ejemplo n.º 3
0
 public Instruction(InstructionTypes toggleInsType, int value)
 {
     InstructionType = toggleInsType;
     Value           = value;
 }
Ejemplo n.º 4
0
 private Instruction(InstructionTypes type, int first, int second)
 {
     InstructionType = type;
     First           = first;
     Second          = second;
 }
Ejemplo n.º 5
0
 private Stream getStateTable(InstructionTypes instructionType)
 {
     Assembly assembly = Assembly.GetExecutingAssembly();
     string assemblyname = assembly.ToString().Substring(0, assembly.ToString().IndexOf(","));
     const string subPath = ".Instructions.Engine.StateMachineSchemas.";
     Stream stateTable = null;
     switch (instructionType)
     {
         case InstructionTypes.Rebalance:
             stateTable = assembly.GetManifestResourceStream(assemblyname + subPath + "RebalanceInstructionStates.xml");
             break;
         case InstructionTypes.BuyModel:
             stateTable = assembly.GetManifestResourceStream(assemblyname + subPath + "BuyModelInstructionStates.xml");
             break;
         case InstructionTypes.CashWithdrawal:
             stateTable = assembly.GetManifestResourceStream(assemblyname + subPath + "CashWithdrawalInstructionStates.xml");
             break;
         case InstructionTypes.ClientDeparture:
             stateTable = assembly.GetManifestResourceStream(assemblyname + subPath + "ClientDepartureInstructionStates.xml");
             break;
     }
     return stateTable;
 }
Ejemplo n.º 6
0
        /// <summary>
        /// This is the method where a new rebalance instruction is created for the account.
        /// </summary>
        /// <param name="instructionType">The type of instruction, currently we only support rebalance instructions</param>
        /// <param name="orderActionType">The type of instruction that is placed on the orders</param>
        /// <param name="executionDate">The date that the rebalance should be executed</param>
        /// <param name="doNotChargeCommission">Determines whether commission should be charged</param>
        /// <param name="cashTransfers">The cash transfers involved</param>
        /// <returns>A new <see cref="T:B4F.TotalGiro.Accounts.Instructions.Instruction">instruction</see></returns>
        public IInstructionTypeRebalance CreateInstruction(InstructionTypes instructionType, OrderActionTypes orderActionType, DateTime executionDate, bool doNotChargeCommission, IList<IJournalEntryLine> cashTransfers)
        {
            IInstructionTypeRebalance instruct = null;
            switch (instructionType)
            {
                case InstructionTypes.Rebalance:
                    instruct = new RebalanceInstruction(this, executionDate, orderActionType, doNotChargeCommission, cashTransfers);
                    break;
                case InstructionTypes.BuyModel:
                    instruct = new BuyModelInstruction(this, executionDate, orderActionType, doNotChargeCommission, cashTransfers);
                    break;
                default:
                    throw new ApplicationException("Wrong instruction type for this method");
            }

            if (instruct != null)
            {
                AccountInstructions.Add(instruct);
                ActiveAccountInstructions.Add(instruct);
            }
            return instruct;
        }
Ejemplo n.º 7
0
 /// <summary>
 /// This is the method where a new rebalance instruction is created for the account.
 /// </summary>
 /// <param name="instructionType">The type of instruction, currently we only support rebalance instructions</param>
 /// <param name="orderActionType">The type of instruction that is placed on the orders</param>
 /// <param name="executionDate">The date that the rebalance should be executed</param>
 /// <returns>A new <see cref="T:B4F.TotalGiro.Accounts.Instructions.Instruction">instruction</see></returns>
 public IInstructionTypeRebalance CreateInstruction(InstructionTypes instructionType, OrderActionTypes orderActionType, DateTime executionDate, bool doNotChargeCommission)
 {
     return CreateInstruction(instructionType, orderActionType, executionDate, doNotChargeCommission, null);
 }