private void ValidOrderFund(TradeOrder order)
        {
            var balance = new AccountBalanceBLL(_unit).GetAccountBalanceByAccount(order.AccountId);

            if (balance.AvailableFund < order.Reserve)
            {
                throw new ApplicationException("Not enough fund for the order.");
            }
        }
示例#2
0
 public TradeManager(IUnitWork unit) : base(unit)
 {
     aBll  = new AccountBLL(_unit);
     toBll = new TradeOrderBLL(_unit);
     abBll = new AccountBalanceBLL(_unit);
     trBll = new TransactionBLL(_unit);
     tpBll = new TradePositionBLL(_unit);
     tsBll = new TradeSetBLL(_unit);
 }
        public void RemoveOrder(int orderId)
        {
            TradeOrder        o     = null;
            AccountBalanceBLL abBll = new AccountBalanceBLL(_unit);
            int accountId           = GetByID(orderId).AccountId;

            o = base.GetByID(orderId);

            abBll.RemoveOrder(o);
            base.Delete(orderId);
        }
示例#4
0
        public OutAccount TransferFund(int accountId, string operation, double amount)
        {
            OutAccount        outAcc = null;
            Transaction       ts     = null;
            AccountBalanceBLL abBll  = new AccountBalanceBLL(_unit);
            TransactionBLL    tBLL   = new TransactionBLL(_unit);

            Account acc = GetByID(accountId);

            AccountBalance existAB = abBll.TransferFund(accountId, operation, amount);

            outAcc = GetOutAccountFromObjs(acc, existAB);

            return(outAcc);
        }
        public TradeOrder CreateOrder(TradeOrder order)
        {
            TradeOrder        to    = null;
            AccountBalanceBLL abBll = new AccountBalanceBLL(_unit);

            this.CalculateOrder(order);

            ValidOrderFund(order);

            base.Create(order);

            to = order;

            abBll.NewOrder(order);

            return(to);
        }
示例#6
0
        public OutAccount Create(OutAccount outAccount)
        {
            AccountBalanceBLL abb = new AccountBalanceBLL(_unit);
            Account           acc = this.GetAccountFromOut(outAccount);

            base.Create(acc);

            var balance = abb.InitCreate(acc.Id);

            if (outAccount.AvailableFund > 0)
            {
                balance = abb.TransferFund(acc.Id, "Deposit", outAccount.AvailableFund);
            }

            outAccount = GetOutAccountFromObjs(acc, balance);

            return(outAccount);
        }
        public TradeOrder CreateExitOrder(TradePosition position, Entity.Indicator ind, bool isStop)
        {
            TradeOrder     to     = new TradeOrder();
            Account        acc    = new AccountBLL(_unit).GetByID(position.AccountId);
            Broker         broker = new BrokerBLL(_unit).GetByID(acc.BrokerId);
            AccountBalance ab     = new AccountBalanceBLL(_unit).GetAccountBalanceByAccount(acc.Id);

            to.AccountId = position.AccountId;
            if (position.Size > 0)
            {
                to.Direction = "Short";
            }
            else
            {
                to.Direction = "Long";
            }
            to.Size = Math.Abs(position.Size);

            if (isStop)
            {
                to.OrderPrice = position.Stop.Value;
            }
            else
            {
                to.OrderPrice = position.Limit.Value;
            }

            to.Fee                  = this.GetFee(to.OrderValue, broker);
            to.ShareId              = position.ShareId;
            to.Source               = OrderSource.Stop.ToString();
            to.UpdateDate           = DateTime.Now;
            to.ProcessedTradingDate = ind.TradingDate;
            to.LatestTradingDate    = ind.TradingDate;
            to.Status               = "Fulfilled";
            to.UpdatedBy            = "System";
            to.TradingOrderDate     = ind.TradingDate;
            to.OrderType            = "Exit";

            this.Create(to);

            new AccountBalanceJourneyBLL(_unit).AddJourneyOrder(ab, "O." + to.Direction, to);

            return(to);
        }
示例#8
0
        public AccountSummary GetAccountSummary(int accountId)
        {
            AccountBLL        aBLL        = new AccountBLL(_unit);
            AccountBalanceBLL abBll       = new AccountBalanceBLL(_unit);
            TradeOrderBLL     orderBLL    = new TradeOrderBLL(_unit);
            TradePositionBLL  positionBLL = new TradePositionBLL(_unit);

            AccountSummary aSummary = new AccountSummary();

            aSummary.Account          = aBLL.GetByID(accountId);
            aSummary.Balance          = abBll.GetAccountBalanceByAccount(accountId);
            aSummary.OpenOrders       = orderBLL.GetListByAccountStatus(accountId, "open");
            aSummary.CurrentPositions = positionBLL.GetOutstandingPositions(accountId);

            aSummary.Balance.Margin        = aSummary.PositionMarginSum;
            aSummary.Balance.Reserve       = aSummary.OrderReserveSum;
            aSummary.Balance.PositionValue = aSummary.PositionValueSum;

            return(aSummary);
        }
        public void UpdateOrder(TradeOrder order)
        {
            bool isWithdraw = false;

            AccountBalanceBLL abBll = new AccountBalanceBLL(_unit);

            this.CalculateOrder(order);

            ValidOrderFund(order);

            var tmpOrder = base.GetByID(order.Id);

            if (tmpOrder.Status.Equals("Open", StringComparison.CurrentCultureIgnoreCase) &&
                order.Status.Equals("Withdrawn", StringComparison.CurrentCultureIgnoreCase))
            {
                isWithdraw = true;
            }

            TradeOrder oldOrder = new TradeOrder();

            this.CloneProperties(tmpOrder, oldOrder);

            this.CloneProperties(order, tmpOrder);

            //tmpOrder.ProcessedTradingDate = null;

            base.Update(tmpOrder);

            if (isWithdraw)
            {
                abBll.RemoveOrder(oldOrder, true);
            }
            else
            {
                abBll.UpdateOrder(oldOrder, order);
            }
        }