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 AccountBalance ApplyEntryTransaction(Transaction tr, TradeOrder order, TradePosition tp) { AccountBalance ab = null; TradeOrderBLL toBll = new TradeOrderBLL(_unit); ab = this.GetAccountBalanceByAccount(order.AccountId); if (ab != null) { ab.FeeSum += tr.Fee; ab.TradingDate = tr.TradingDate; ab.UpdateDT = DateTime.Now; } this.Update(ab); return(ab); }
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 AccountBalance NewOrder(TradeOrder order) { AccountBalance ab = null; TradeOrderBLL toBll = new TradeOrderBLL(_unit); ab = this.GetAccountBalanceByAccount(order.AccountId); if (ab != null) { //ab.Reserve = ab.Reserve + order.Reserve; ab.AvailableFund = ab.AvailableFund - order.Reserve; ab.TradingDate = order.TradingOrderDate; ab.UpdateDT = DateTime.Now; } base.Update(ab); new AccountBalanceJourneyBLL(_unit).AddJourneyOrder(ab, "O." + order.Direction, order); return(ab); }
public AccountBalance ApplyExitTransaction(Transaction tr, TradeOrder order, TradePosition tp) { AccountBalance ab = null; TradeOrderBLL toBll = new TradeOrderBLL(_unit); ab = this.GetAccountBalanceByAccount(order.AccountId); if (ab != null) { ab.AvailableFund += tp.Margin - tr.Fee; ab.FeeSum += tr.Fee; ab.TradingDate = tr.TradingDate; ab.UpdateDT = DateTime.Now; } this.Update(ab); new AccountBalanceJourneyBLL(_unit).AddJourneyTransaction(ab, "T." + tr.Direction, tr); return(ab); }
/// <summary> /// Creates the trade position. /// </summary> /// <param name="order">The order.</param> /// <param name="ts">The ts.</param> /// <returns>is position closed </returns> public TradePosition CreateTradePosition(Transaction trans, TradeOrder order, TradeSet ts, Entity.Indicator ind, out bool isReverse) { TradePosition tp = null; TradeOrderBLL toBll = new TradeOrderBLL(_unit); //TradeOrder order = toBll.GetByID(trans.TradeOrderId); isReverse = false; tp = _unit.DataContext.TradePosition.Where(p => p.AccountId == order.AccountId && p.ShareId == order.ShareId).SingleOrDefault(); if (tp == null) { tp = new TradePosition { ShareId = order.ShareId, AccountId = order.AccountId, Size = trans.Size * order.Flag, EntryPrice = (trans.Size * trans.Price + trans.Fee * trans.Flag) / trans.Size, UpdateDT = DateTime.Now, CurrentPrice = ind.Close.Value, CurrentTradingDate = ind.TradingDate, }; this.Create(tp); } else { // update the position size if (toBll.IsReverse(order, tp)) { isReverse = true; tp.Size += order.Size * order.Flag; if (tp.Size != 0) { tp.EntryPrice = tp.EntryCost / Math.Abs(tp.Size); } tp.UpdateDT = DateTime.Now; tp.CurrentPrice = ind.Close.Value; tp.CurrentTradingDate = ind.TradingDate; this.Update(tp); } else { tp.Size += order.Size * order.Flag; if (tp.Size != 0) { tp.EntryPrice = tp.EntryCost / Math.Abs(tp.Size); } tp.UpdateDT = DateTime.Now; tp.CurrentPrice = ind.Close.Value; tp.CurrentTradingDate = ind.TradingDate; this.Update(tp); } } return(tp); }