コード例 #1
0
ファイル: TraderService.cs プロジェクト: 810912015/option
 static void RaiseAccountChanged(Trader t, decimal delta, AccountChangeType type, bool isSuccess, string who, string oderdesc, decimal curTotal, Account account)
 {
     if (OnAccountChanged != null)
     {
         OnAccountChanged(t, delta, type, isSuccess, who, oderdesc, curTotal, account);
     }
 }
コード例 #2
0
ファイル: BacktestAccount.cs プロジェクト: afhacker/NetTrade
        public void ChangeBalance(double amount, DateTimeOffset time, string note, AccountChangeType type)
        {
            var change = new AccountChange(CurrentBalance, amount, time, note, type);

            _balanceChanges.Add(change);

            CurrentBalance = change.NewValue;
        }
コード例 #3
0
ファイル: BacktestAccount.cs プロジェクト: afhacker/NetTrade
        public void ChangeEquity(double amount, DateTimeOffset time, string note, AccountChangeType type)
        {
            var change = new AccountChange(Equity, amount, time, note, type);

            _equityChanges.Add(change);

            Equity = change.NewValue;

            CheckForMarginCall();
        }
コード例 #4
0
ファイル: BacktestAccount.cs プロジェクト: afhacker/NetTrade
        public void ChangeMargin(double amount, DateTimeOffset time, string note, AccountChangeType type)
        {
            var change = new AccountChange(UsedMargin, amount, time, note, type);

            _marginChanges.Add(change);

            UsedMargin = change.NewValue;

            CheckForMarginCall();
        }
コード例 #5
0
        public AccountChange(double previousValue, double amount, DateTimeOffset time, string note, AccountChangeType type)
        {
            PreviousValue = previousValue;

            Amount = amount;

            Time = time;

            Note = note;

            NewValue = PreviousValue + Amount;

            Type = type;
        }
コード例 #6
0
 public AccountUpdateMessageData(
     string currency,
     long accountId,
     decimal balance,
     decimal available,
     AccountChangeType changeType,
     AccountType accountType,
     DateTimeOffset changeTime)
 {
     Currency    = currency;
     AccountId   = accountId;
     Balance     = balance;
     Available   = available;
     ChangeType  = changeType;
     AccountType = accountType;
     ChangeTime  = changeTime;
 }
コード例 #7
0
 /// <summary>
 /// 如果是平仓,直接支付;否则检查保证金是否足够
 /// </summary>
 /// <param name="t"></param>
 /// <param name="delta"></param>
 /// <param name="m"></param>
 /// <param name="o"></param>
 /// <returns></returns>
 public static bool BailPay(this Trader t, decimal delta, Market m, Order o, AccountChangeType type)
 {
     lock (t.Account.BailAccount)
     {
         SystemAccount.Instance.Log.Info(string.Format("支付(可能借款):{0}-线程{1}-委托{2}", DateTime.Now.ToString("HH:mm:ss.fff"),
                                                       Thread.CurrentThread.ManagedThreadId, o == null?"":o.Id.ToString()));
         if (o != null && o.OrderType == OrderType.平仓)
         {
             //系统借款记录处理
             if (t.Account.BailAccount.Sum < delta)
             {
                 var needed       = delta * (TraderService.CommissionRatio + 1) - t.Account.BailAccount.Sum;
                 var borrowResult = SystemAccount.Instance.Borrow(needed, o);
             }
             var r = t.Account.BailAccount.Sub(delta);
             TraderService.OperateAccount(t, delta, type, "system", o, t.Account.BailAccount.Total);
             return(r);
         }
         else
         {
             var maintain = GetMaintain(t, m);
             var remain   = t.Account.BailAccount.Sum - maintain;
             if (t.Account.BailAccount.Sum < delta || remain < delta)
             {
                 //执行保证金自动转入
                 var d1 = delta - t.Account.BailAccount.Sum;
                 var d2 = delta - remain;
                 var d  = d1 >= d2 ? d1 : d2;
                 var tr = TraderService.OperateAddBailFromCache(t, d, o);
                 if (!tr)
                 {
                     return(false);
                 }
             }
             var r = t.Account.BailAccount.Sub(delta);//划转保证金
             TraderService.OperateAccount(t, delta, type, "system", o, t.Account.BailAccount.Total);
             return(r);
         }
     }
 }
コード例 #8
0
        public void Save(Trader arg1, decimal arg2, AccountChangeType arg3, bool arg4, string byWho, string orderDesc, decimal current, Account ac)
        {
            var rec = new AccountTradeRecord
            {
                Id          = IdService <AccountTradeRecord> .Instance.NewId(),
                ByWho       = byWho,
                Delta       = arg2,
                IsAddTo     = arg4,
                When        = DateTime.Now,
                Who         = arg1,
                OperateType = arg3,
                OrderDesc   = orderDesc,
                Current     = current,
                Frozen      =
                    arg3 == AccountChangeType.保证金冻结 || arg3 == AccountChangeType.保证金解冻 ?
                    arg1.Account.BailAccount.Frozen :
                    arg3 == AccountChangeType.现金冻结 || arg3 == AccountChangeType.现金解冻 ?
                    arg1.Account.CacheAccount.CnyAccount.Frozen : 0m,
                IsBail = ac is BailAccount,
                CoinId = ac.CacheType.Id
            };

            ars.Save(rec);
        }
コード例 #9
0
ファイル: TraderService.cs プロジェクト: 810912015/option
        /// <summary>
        /// 用户账户现金操作
        /// </summary>
        /// <param name="t"></param>
        /// <param name="delta"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static bool OperateAccount(Trader t, decimal delta, AccountChangeType type, string byWho, Order o, decimal tcur = 0)
        {
            if (delta <= 0)
            {
                return(false);
            }
            bool    r         = false;
            bool    needComm  = false;
            bool    needEvent = true;
            decimal cur       = 0;
            Account ac        = null;

            switch (type)
            {
            case AccountChangeType.BTC付款:
                r   = t.Account.CacheAccount.BtcAccount.Sub(delta);
                cur = t.Account.CacheAccount.BtcAccount.Total;
                ac  = t.Account.CacheAccount.BtcAccount;
                break;

            case AccountChangeType.BTC收款:
                r   = t.Account.CacheAccount.BtcAccount.Add(delta);
                cur = t.Account.CacheAccount.BtcAccount.Total;
                ac  = t.Account.CacheAccount.BtcAccount;
                break;

            case AccountChangeType.保证金冻结:
                r = t.Account.BailAccount.Freeze(delta);

                cur = t.Account.BailAccount.Total;
                ac  = t.Account.BailAccount;
                break;

            case AccountChangeType.保证金解冻:
                r   = t.Account.BailAccount.UnFreeze(delta);
                cur = t.Account.BailAccount.Total;
                ac  = t.Account.BailAccount;
                break;

            case AccountChangeType.保证金转现金:
                needEvent = false;
                r         = t.Account.BailAccount.Sub(delta);
                if (r)
                {
                    RaiseAccountChanged(t, delta, AccountChangeType.保证金转现金_保证金转出, r, byWho, "", t.Account.BailAccount.Total, t.Account.BailAccount);
                    r = t.Account.CacheAccount.CnyAccount.Add(delta);
                    RaiseAccountChanged(t, delta, AccountChangeType.保证金转现金_现金转入, r, byWho, "", t.Account.CacheAccount.CnyAccount.Total, t.Account.CacheAccount.CnyAccount);
                }
                cur = t.Account.CacheAccount.CnyAccount.Total;
                break;

            case AccountChangeType.现金转保证金:
                needEvent = false;
                r         = t.Account.CacheAccount.CnyAccount.Sub(delta);
                if (r)
                {
                    RaiseAccountChanged(t, delta, AccountChangeType.现金转保证金_现金转出, r, byWho, "", t.Account.CacheAccount.CnyAccount.Total, t.Account.CacheAccount.CnyAccount);
                    r = t.Account.BailAccount.Add(delta);
                    RaiseAccountChanged(t, delta, AccountChangeType.现金转保证金_保证金转入, r, byWho, "", t.Account.BailAccount.Total, t.Account.BailAccount);
                }

                cur = t.Account.BailAccount.Total;
                break;

            case AccountChangeType.现金收款:
                r   = t.Account.CacheAccount.CnyAccount.Add(delta);
                cur = t.Account.CacheAccount.CnyAccount.Total;
                ac  = t.Account.CacheAccount.CnyAccount;
                break;

            case AccountChangeType.CNY充值:
                r   = t.Account.CacheAccount.CnyAccount.Add(delta);
                cur = t.Account.CacheAccount.CnyAccount.Total;
                ac  = t.Account.CacheAccount.CnyAccount;
                SystemAccount.Instance.OperateExceptBorrowRepay(delta, t, SysAccountChangeType.充值, true);
                break;

            case AccountChangeType.现金付款:
                r   = t.Account.CacheAccount.CnyAccount.Sub(delta);
                cur = t.Account.CacheAccount.CnyAccount.Total;
                ac  = t.Account.CacheAccount.CnyAccount;
                break;

            case AccountChangeType.CNY提现:
                //Sub
                t.Account.CacheAccount.CnyAccount.UnFreeze(delta);
                r   = t.Account.CacheAccount.CnyAccount.Sub(delta);  //再扣除可用
                cur = t.Account.CacheAccount.CnyAccount.Total;
                ac  = t.Account.CacheAccount.CnyAccount;
                SystemAccount.Instance.OperateExceptBorrowRepay(delta, t, SysAccountChangeType.提现, false);
                break;

            case AccountChangeType.BTC充值:
                r   = t.Account.CacheAccount.BtcAccount.Add(delta);
                cur = t.Account.CacheAccount.BtcAccount.Total;
                ac  = t.Account.CacheAccount.BtcAccount;
                break;

            case AccountChangeType.BTC提现:
                t.Account.CacheAccount.BtcAccount.UnFreeze(delta);
                r   = t.Account.CacheAccount.BtcAccount.Sub(delta);
                cur = t.Account.CacheAccount.BtcAccount.Total;
                ac  = t.Account.CacheAccount.BtcAccount;

                break;

            case AccountChangeType.现金冻结:
                r   = t.Account.CacheAccount.CnyAccount.Freeze(delta);
                cur = t.Account.CacheAccount.CnyAccount.Total;
                ac  = t.Account.CacheAccount.CnyAccount;
                break;

            case AccountChangeType.BTC冻结:
                r   = t.Account.CacheAccount.BtcAccount.Freeze(delta);
                cur = t.Account.CacheAccount.BtcAccount.Total;
                ac  = t.Account.CacheAccount.BtcAccount;
                break;

            case AccountChangeType.BTC解冻:
                r   = t.Account.CacheAccount.BtcAccount.UnFreeze(delta);
                cur = t.Account.CacheAccount.BtcAccount.Total;
                ac  = t.Account.CacheAccount.BtcAccount;
                break;

            case AccountChangeType.现金解冻:
                r   = t.Account.CacheAccount.CnyAccount.UnFreeze(delta);
                cur = t.Account.CacheAccount.CnyAccount.Total;
                ac  = t.Account.CacheAccount.CnyAccount;
                break;

            case AccountChangeType.保证金收款:
                r        = t.Account.BailAccount.Collect(delta);
                needComm = true;
                cur      = t.Account.BailAccount.Total;
                ac       = t.Account.BailAccount;
                break;

            case AccountChangeType.保证金付款:
                r        = true;
                needComm = true;
                cur      = tcur;
                ac       = t.Account.BailAccount;
                break;

            case AccountChangeType.行权划入:
                r        = true;
                needComm = false;
                cur      = t.Account.BailAccount.Total;
                ac       = t.Account.BailAccount;
                break;

            case AccountChangeType.行权划出:
                r        = true;
                needComm = false;
                cur      = tcur;
                ac       = t.Account.BailAccount;
                break;

            default:
                break;
            }
            if (needEvent)
            {
                RaiseAccountChanged(t, delta, type, r,
                                    byWho, o == null?type.ToString(): o.ToShortString(), cur == 0?t.Account.BailAccount.Sum:cur, ac);
            }

            if (needComm)
            {
                PayCommission(delta, t.Account.BailAccount, t, o);
            }
            return(r);
        }
コード例 #10
0
ファイル: TraderService.cs プロジェクト: 810912015/option
 public static bool Manual(this Trader t, decimal delta, AccountChangeType type, string byWho, bool isAbountFreeze = false, bool isFreeze = false)
 {
     return(TraderService.ManualChange(t, delta, type, byWho, isAbountFreeze, isFreeze));
 }
コード例 #11
0
ファイル: TraderService.cs プロジェクト: 810912015/option
        public static bool ManualChange(Trader t, decimal delta, AccountChangeType type, string byWho, bool isAboutFreeze = false, bool isFreeze = false)
        {
            bool    result  = false;
            decimal cur     = 0;
            Account account = null;

            if (isAboutFreeze)
            {
                switch (type)
                {
                case AccountChangeType.手动减少BTC:
                    account = t.Account.CacheAccount.BtcAccount;
                    result  = isFreeze? account.Freeze(delta):account.UnFreeze(delta);
                    cur     = t.Account.CacheAccount.BtcAccount.Total;
                    break;

                case AccountChangeType.手动减少保证金:
                    account = t.Account.BailAccount;
                    result  = isFreeze ? account.Freeze(delta) : account.UnFreeze(delta);
                    cur     = t.Account.BailAccount.Total;
                    break;

                case AccountChangeType.手动减少现金:
                    account = t.Account.CacheAccount.CnyAccount;
                    result  = isFreeze ? account.Freeze(delta) : account.UnFreeze(delta);
                    cur     = t.Account.CacheAccount.CnyAccount.Total;
                    break;

                case AccountChangeType.手动增加BTC:
                    account = t.Account.CacheAccount.BtcAccount;
                    result  = isFreeze ? account.Freeze(delta) : account.UnFreeze(delta);
                    cur     = t.Account.CacheAccount.BtcAccount.Total;
                    break;

                case AccountChangeType.手动增加保证金:
                    account = t.Account.BailAccount;
                    result  = isFreeze ? account.Freeze(delta) : account.UnFreeze(delta);
                    cur     = t.Account.BailAccount.Total;
                    break;

                case AccountChangeType.手动增加现金:
                    account = t.Account.CacheAccount.CnyAccount;
                    result  = isFreeze ? account.Freeze(delta) : account.UnFreeze(delta);
                    cur     = t.Account.CacheAccount.CnyAccount.Total;
                    break;
                }
            }
            else
            {
                switch (type)
                {
                case AccountChangeType.手动减少BTC:
                    account = t.Account.CacheAccount.BtcAccount;
                    result  = t.Account.CacheAccount.BtcAccount.Sub(delta);
                    cur     = t.Account.CacheAccount.BtcAccount.Total;

                    break;

                case AccountChangeType.手动减少保证金:
                    account = t.Account.BailAccount;
                    result  = t.Account.BailAccount.Sub(delta);
                    cur     = t.Account.BailAccount.Total;
                    //手动操作资金要在系统私有总额中反方向操作,以保证总额恒定
                    SystemAccount.Instance.OperateExceptBorrowRepay(delta, t, SysAccountChangeType.手动调整_减少保证金, true);
                    break;

                case AccountChangeType.手动减少现金:
                    account = t.Account.CacheAccount.CnyAccount;
                    result  = t.Account.CacheAccount.CnyAccount.Sub(delta);
                    cur     = t.Account.CacheAccount.CnyAccount.Total;
                    SystemAccount.Instance.OperateExceptBorrowRepay(delta, t, SysAccountChangeType.手动调整_减少现金, true);
                    break;

                case AccountChangeType.手动增加BTC:
                    account = t.Account.CacheAccount.BtcAccount;
                    result  = t.Account.CacheAccount.BtcAccount.Add(delta);
                    cur     = t.Account.CacheAccount.BtcAccount.Total;
                    break;

                case AccountChangeType.手动增加保证金:
                    account = t.Account.BailAccount;
                    result  = t.Account.BailAccount.Add(delta);
                    cur     = t.Account.BailAccount.Total;
                    SystemAccount.Instance.OperateExceptBorrowRepay(delta, t, SysAccountChangeType.手动调整_增加保证金, false);
                    break;

                case AccountChangeType.手动增加现金:
                    account = t.Account.CacheAccount.CnyAccount;
                    result  = t.Account.CacheAccount.CnyAccount.Add(delta);
                    cur     = t.Account.CacheAccount.CnyAccount.Total;
                    SystemAccount.Instance.OperateExceptBorrowRepay(delta, t, SysAccountChangeType.手动调整_增加现金, false);
                    break;
                }
                RaiseAccountChanged(t, delta, type, result, byWho, "管理员手动调整", cur, account);
            }
            return(result);
        }
コード例 #12
0
ファイル: OptionModel.cs プロジェクト: 810912015/option
 /// <summary>
 /// 保持用户资金账户的更新
 /// </summary>
 /// <param name="arg1"></param>
 /// <param name="arg2"></param>
 /// <param name="arg3"></param>
 /// <param name="arg4"></param>
 void TraderService_OnAccountChanged(Trader arg1, decimal arg2, AccountChangeType arg3, bool arg4, string byWho, string orderDesc, decimal current, Account ac)
 {
     ars.Save(arg1, arg2, arg3, arg4, byWho, orderDesc, current, ac);
 }