Exemplo n.º 1
0
 public void RemoveContract(int c, decimal d)
 {
     try
     {
         var q = Contracts.Where(a => a.Id == c).FirstOrDefault();
         if (q == null)
         {
             return;
         }
         if (Contracts.Contains(q))
         {
             initor.contracts.Remove(q);
             using (OptionDbCtx db = new OptionDbCtx())
             {
                 var con = db.Contracts.Find(c);
                 con.IsNotInUse      = true;
                 con.ExcuteBasePrice = d;
                 db.SaveChanges();
             }
         }
     }
     catch (Exception e)
     {
         Singleton <TextLog> .Instance.Error(e, "OptionModel.RemoveContract");
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// 将编号写入数据库,系统停止时使用
        /// </summary>
        public void Flush()
        {
            using (OptionDbCtx db = new OptionDbCtx())
            {
                var q = db.GlobalPrms.Find("DutyIdOfThisYear");

                if (q != null)
                {
                    q.Value = DutyIdOfThisYear.ToString();
                }
                else
                {
                    db.GlobalPrms.Add(new Core.Infra.GlobalPrm {
                        Value = DutyIdOfThisYear.ToString(), Name = "DutyIdOfThisYear"
                    });
                }
                var p = db.GlobalPrms.Find("RightIdOfThisYear");
                if (p != null)
                {
                    p.Value = RightIdOfThisYear.ToString();
                }
                else
                {
                    db.GlobalPrms.Add(new Core.Infra.GlobalPrm {
                        Value = RightIdOfThisYear.ToString(), Name = "RightIdOfThisYear"
                    });
                }
                db.SaveChanges();
            }
        }
Exemplo n.º 3
0
 public HomeController()
 {
     UserManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(new ApplicationDbContext()));
     db          = new OptionDbCtx();
     odr         = new OrderDealReader(db);
     adb         = new ApplicationDbContext();
 }
Exemplo n.º 4
0
        public List <OrderDto> GetMyOrdersByTime_State(int tid, DateTime timeTop, DateTime timeEnd, string state, int pageIndex = 1)
        {
            OptionDbCtx db = new OptionDbCtx();

            using (BaseRepository <Trader> dr = new BaseRepository <Trader>())
            {
                var os = db.Traders.Where(a => a.Id == tid).FirstOrDefault();
                if (os == null)
                {
                    return(new List <OrderDto>());
                }
                if (state == "全部")
                {
                    return(os.Orders().Items.Select(a => new OrderDto(a)).Where(b => Convert.ToDateTime(b.Time) > timeTop && Convert.ToDateTime(b.Time) < timeEnd).OrderBy(a => a.Id)
                           .Skip((pageIndex - 1) * countPerPage)
                           .Take(countPerPage).ToList());
                }
                else
                {
                    return(os.Orders().Items.Select(a => new OrderDto(a)).Where(b => b.State == state && Convert.ToDateTime(b.Time) > timeTop && Convert.ToDateTime(b.Time) < timeEnd).OrderBy(a => a.Id)
                           .Skip((pageIndex - 1) * countPerPage)
                           .Take(countPerPage).ToList());
                }
            }
        }
Exemplo n.º 5
0
        public void RestoreOrders()
        {
            FlushSpotOrder fso = new FlushSpotOrder();
            var            ok  = fso.GetOldGeneration();

            if (ok == null)
            {
                return;
            }
            string sql = string.Format(@"select * from tempspotorders where id in 
(select id from tempspotorders where detail='{0}' except select id from spotorders)", (int)ok);

            using (var db = new OptionDbCtx())
            {
                var q = db.Database.SqlQuery <SpotOrder>(sql);
                foreach (var v in q)
                {
                    var coin   = Model.Coins.Where(a => a.Id == v.CoinId).FirstOrDefault();
                    var trader = Model.Traders.Where(a => a.Id == v.TraderId).FirstOrDefault();
                    var so     = new SpotOrder
                    {
                        Id            = v.Id, CoinId = v.CoinId, Count = v.Count, Detail = v.Detail,
                        Direction     = v.Direction, DoneCount = v.DoneCount, DonePrice = v.DonePrice, IsBySystem = v.IsBySystem,
                        OrderPolicy   = v.OrderPolicy, OrderTime = v.OrderTime, Price = v.Price, ReportCount = v.ReportCount,
                        RequestStatus = v.RequestStatus, State = v.State, TotalDoneCount = v.TotalDoneCount, TotalDoneSum = v.TotalDoneSum,
                        TraderId      = v.TraderId, Coin = coin, Trader = trader
                    };
                    SpotOrders.Add(so);
                }
            }
        }
Exemplo n.º 6
0
 protected override void Dispose(bool disposing)
 {
     if (disposing)
     {
         if (UserManager != null)
         {
             UserManager.Dispose();
             UserManager = null;
         }
         if (db != null)
         {
             db.Dispose();
             db = null;
         }
         if (adb != null)
         {
             adb.Dispose();
             adb = null;
         }
         if (dbm != null)
         {
             dbm.Dispose();
             dbm = null;
         }
     }
     base.Dispose(disposing);
 }
Exemplo n.º 7
0
        void RestorePositionSummary(Dictionary <int, Trader> td, Dictionary <int, Contract> cond)
        {
            foreach (var v in td.Values)
            {
                v.Positions = new List <UserPosition>();
            }
            var sql = @"select * from PositionSummaryDatas where Id in
(select MAX(Id) from PositionSummaryDatas group by ContractId,TraderId)";

            using (var db = new OptionDbCtx())
            {
                var rs = db.Database.SqlQuery <PositionSummaryData>(sql);
                foreach (var v in rs)
                {
                    if (!cond.ContainsKey(v.ContractId))
                    {
                        continue;
                    }
                    if (!td.ContainsKey(v.TraderId))
                    {
                        continue;
                    }
                    v.Contract = cond[v.ContractId];
                    v.Trader   = td[v.TraderId];
                    v.Trader.InitPosition(v);
                }
            }
        }
Exemplo n.º 8
0
        void RestoreAccount(Dictionary <int, Trader> td)
        {
            var sql = @"select * from AccountTradeRecords where id in(
select  max(id) from AccountTradeRecords
group by WhoId )";

            using (var db = new OptionDbCtx())
            {
                var rs = db.Database.SqlQuery <AccountTradeRecord>(sql);
                foreach (var v in rs)
                {
                    if (!td.ContainsKey(v.WhoId))
                    {
                        continue;
                    }
                    var t = td[v.WhoId];
                    t.Account.BailAccount.Sum                = v.BailSum;
                    t.Account.BailAccount.Frozen             = v.BailFrozen;
                    t.Account.CacheAccount.CnyAccount.Sum    = v.CnySum;
                    t.Account.CacheAccount.CnyAccount.Frozen = v.CnyFrozen;
                    t.Account.CacheAccount.BtcAccount.Frozen = v.BtcFrozen;
                    t.Account.CacheAccount.BtcAccount.Sum    = v.BtcSum;
                }
            }
        }
Exemplo n.º 9
0
        public void TestMethod2()
        {
            OptionDbCtx db = new OptionDbCtx(@"Data Source=.\sqlexpress;Initial Catalog=Option-dev;Integrated Security=True");
            var         r  = db.Database.SqlQuery <Deal>("select * from deals");
            var         rr = r.ToList <Deal>();

            Assert.IsNotNull(r);
        }
Exemplo n.º 10
0
 public void Flush()
 {
     using (var db = new OptionDbCtx())
     {
         db.GlobalPrms.AddOrUpdate(Prms.Values.ToArray());
         db.SaveChanges();
     }
 }
Exemplo n.º 11
0
        public void poco()
        {
            var db = new OptionDbCtx();
            var r  = db.Set <Order>().Select(a => new Coin2 {
                Id  = a.Id, Contract
                    = a.Contract, Count = a.Count
            }).ToList <Coin2>();

            Assert.IsNotNull(r);
        }
Exemplo n.º 12
0
 public void Init()
 {
     using (var db = new OptionDbCtx())
     {
         foreach (var v in db.GlobalPrms)
         {
             Prms.Add(v.Name, v);
         }
     }
 }
Exemplo n.º 13
0
        public void Add_test()
        {
            var db = new OptionDbCtx();
            var v  = new ContractRepo(db);
            var r  = v.Add("btc", DateTime.Now.AddMonths(1), 100, OptionType.认购期权, "比特币");

            SingletonWithInit <ContractService> .Instance.Flush();

            Assert.AreEqual(true, r);
        }
Exemplo n.º 14
0
 public static void InitSpot(KlineDataPool kdp)
 {
     using (OptionDbCtx db = new OptionDbCtx())
     {
         var l = db.Ohlcs.Where(a => a.WhatId < 0).OrderByDescending(a => a.Id).Take(1000);
         foreach (var v in l)
         {
             kdp.Add(v);
         }
     }
 }
Exemplo n.º 15
0
        public void TestMethod1()
        {
            List <string> l  = new List <string>();
            OptionDbCtx   db = new OptionDbCtx();

            db.Database.Log = (a) => { l.Add(a); };
            var c = db.Set <AccountTradeRecord>().Count();
            var q = db.Set <AccountTradeRecord>().Where(a => true).OrderBy(a => a.Id).Take(10).AsQueryable().ToList();

            Assert.AreEqual(10, q.Count);
        }
Exemplo n.º 16
0
        static decimal OperateInvitorFee(InvitorFeeTrans trans, Trader to, OptionDbCtx db, Action <string> log)
        {
            try
            {
                if (trans.Trader == null)
                {
                    return(0);
                }
                var start = trans.LastTransferTime ?? DateTime.Now.Date;
                var ls    = db.SysAccountRecords.Where(a => a.ChangedType == SysAccountChangeType.收取手续费 &&
                                                       a.Who != null &&
                                                       a.Who.Id == trans.Trader.Id &&
                                                       a.When > start &&
                                                       a.When < DateTime.Now
                                                       ).Select(a => new { Delta = a.Delta });
                if (ls == null)
                {
                    return(0);
                }
                var count = ls.Count();
                if (count == 0)
                {
                    return(0);
                }
                var total = ls.Sum(a => a.Delta);
                if (total == 0)
                {
                    return(0);
                }
                var r = total * trans.Ratio;
                if (r == 0)
                {
                    return(0);
                }
                string detal  = string.Format("{0}从{1}到{2}共交易{3}笔,手续费总额{4},返还比例{5},返还总额{6}", trans.Trader.Name, start, DateTime.Now, count, total, trans.Ratio, r);
                bool   result = TraderService.OperateInvitorFee(to, r, detal);
                if (result)
                {
                    trans.UpdateSumAction(r);
                }
                if (log != null)
                {
                    log(detal);
                }
                return(r);
            }
            catch (Exception ex)
            {
                Singleton <TextLog> .Instance.Error(ex);

                return(0);
            }
        }
Exemplo n.º 17
0
 public static void InitOption(KlineDataPool kdp, List <int> cids)
 {
     using (OptionDbCtx db = new OptionDbCtx())
     {
         var l = db.Ohlcs.Where(a => a.WhatId > 0 && cids.Contains(a.WhatId)).OrderByDescending(a => a.Id).Take(1000).ToList();
         l.Reverse();
         foreach (var v in l)
         {
             kdp.Add(v);
         }
     }
 }
Exemplo n.º 18
0
 public static void TransFee(List <InvitorFeeTrans> l, Trader to)
 {
     using (var db = new OptionDbCtx())
     {
         foreach (var v in l)
         {
             if (v.Trader == null)
             {
                 continue;
             }
             OperateInvitorFee(v, to, db, (s) => log.Info(s));
         }
     }
 }
Exemplo n.º 19
0
        public void Init()
        {
            if (CurrentOrderId > 0)
            {
                return;
            }
            if (typeof(T) == typeof(Order))
            {
                //因为order和temporders中存储的是相同的东西,所以应该是两个表中较大的id值
                int fmax;
                using (var db = new OptionDbCtx())
                {
                    string sql = "select MAX(id) from TempOrders";
                    int?   r1  = 0;
                    var    r3  = db.Database.SqlQuery <int?>(sql);
                    if (r3 != null)
                    {
                        r1 = r3.FirstOrDefault();
                    }
                    int r2 = 0;
                    var c  = db.Set <T>();
                    var t  = c.Count();
                    if (t > 0)
                    {
                        var max = db.Set <T>().Max(a => a.Id);
                        r2 = max;
                    }

                    fmax = r1 > r2 ? (r1 ?? 0) : r2;
                }
                SetCurrentOrderId(fmax + 1);
            }
            else
            {
                using (var db = new OptionDbCtx())
                {
                    var c = db.Set <T>();
                    var t = c.Count();
                    if (t > 0)
                    {
                        var max = db.Set <T>().Max(a => a.Id);
                        SetCurrentOrderId(max);
                    }
                    else
                    {
                        SetCurrentOrderId(0);
                    }
                }
            }
        }
Exemplo n.º 20
0
        void RestoreDeals(Dictionary <int, Contract> cond)
        {
            LatestDeals = new List <Deal>();
            var ks = cond.Keys.ToList();

            using (OptionDbCtx db = new OptionDbCtx())
            {
                var q = db.Deals.Where(a => ks.Contains(a.ContractId)).Take(1000);
                foreach (var v in q)
                {
                    LatestDeals.Add(v);
                }
            }
        }
Exemplo n.º 21
0
 protected override void Dispose(bool disposing)
 {
     if (!disposing && db != null)
     {
         db.Dispose();
         db = null;
     }
     if (!disposing && adb != null)
     {
         adb.Dispose();
         adb = null;
     }
     base.Dispose(disposing);
 }
Exemplo n.º 22
0
        public void TestMethod1()
        {
            using (OptionDbCtx db = new OptionDbCtx(@"Data Source=.\sqlexpress;Initial Catalog=Option;Integrated Security=True"))
            {
                OrderDealReader odr = new OrderDealReader(db);

                var r = odr.Query("hello1", 1);

                Assert.AreEqual(true, r.Item2);

                SpotOrderDealReader sor = new SpotOrderDealReader(db);
                var r2 = sor.Query("hello1", 1);
            }
        }
Exemplo n.º 23
0
        public List <MarketRecord> Read()
        {
            string sql            = "select * from marketrecords where id in (select MAX(id) from marketrecords group by contractId)";
            List <MarketRecord> l = new List <MarketRecord>();

            using (OptionDbCtx db = new OptionDbCtx())
            {
                var q = db.Database.SqlQuery <MarketRecord>(sql);
                foreach (var v in q)
                {
                    l.Add(v);
                }
            }
            return(l);
        }
Exemplo n.º 24
0
        /// <summary>
        /// 从数据库中读取系统账号金额
        /// </summary>
        public void Init()
        {
            using (var db = new OptionDbCtx())
            {
                var conn = db.Database.Connection.Database;
                var sql  = string.Format("SELECT TOP 1 *  FROM [{0}].[dbo].[SysAccountRecords] order by Id desc", conn);

                var sar = db.Database.SqlQuery <SysAccountRecord>(sql).FirstOrDefault();
                if (sar != null)
                {
                    PrivateSum = sar.PrivateSum;
                    PublicSum  = sar.PublicSum;
                }
            }
        }
Exemplo n.º 25
0
        public void T2()
        {
            List <string> l  = new List <string>();
            OptionDbCtx   db = new OptionDbCtx();

            db.Database.Log = (a) => {
                l.Add(a);
            };
            QueryEngine aqa = new QueryEngine();
            BaseRepository <AccountTradeRecord> cerRepo = new BaseRepository <AccountTradeRecord>();

            cerRepo.SetCtx(db);
            var r = QueryEngine.Query <AccountTradeRecord, int>(ref aqa, cerRepo, "/TradeData/CerQuery", a => a.Id);

            Assert.AreEqual(20, r.Count);
        }
Exemplo n.º 26
0
 public void AddContract(Contract c)
 {
     Task.Factory.StartNew(() =>
     {
         using (OptionDbCtx db = new OptionDbCtx())
         {
             var coin = db.Set <Coin>().Find(c.Coin.Id);
             if (coin != null)
             {
                 c.Coin = coin;
             }
             db.Contracts.Add(c);
             db.SaveChanges();
         }
     });
 }
Exemplo n.º 27
0
        public void Add_Multi()
        {
            var db = new OptionDbCtx();
            var v  = new ContractRepo(db);

            for (int i = 0; i < 10; i++)
            {
                var r = v.Add("btc", DateTime.Now.AddMonths(i + 1), 100, OptionType.认购期权, "比特币");
            }

            for (int i = 0; i < 10; i++)
            {
                var r = v.Add("btc", DateTime.Now.AddMonths(i + 1), 100, OptionType.认沽期权, "比特币");
            }
            Assert.IsNotNull(v);
        }
Exemplo n.º 28
0
 protected override void Dispose(bool disposing)
 {
     if (disposing)
     {
         if (db != null)
         {
             db.Dispose();
             db = null;
         }
         if (odc != null)
         {
             odc.Dispose();
             odc = null;
         }
     }
     base.Dispose(disposing);
 }
Exemplo n.º 29
0
        public void flush_test()
        {
            var om = new OptionModel();

            om.Init();
            var q = om.Traders.Where(a => a.Name == "hello0").FirstOrDefault();

            q.Account.BailAccount.Sum    = 9;
            q.Account.BailAccount.Frozen = 8;
            //q.Account.BailAccount.MaintainCount = 7;
            om.Flush();
            var db = new OptionDbCtx();
            var t  = db.Set <BailAccount>().Where(a => a.Id == q.Id).FirstOrDefault();

            Assert.AreEqual(t.Sum, 9);
            Assert.AreEqual(t.Frozen, 8);
            //Assert.AreEqual(t.MaintainCount, 7);
        }
Exemplo n.º 30
0
 /// <summary>
 /// 从数据库总读取并设置编号
 /// </summary>
 public void Init()
 {
     RightIdOfThisYear = 19; DutyIdOfThisYear = 20;
     using (OptionDbCtx db = new OptionDbCtx())
     {
         var q = db.GlobalPrms.Find("DutyIdOfThisYear");
         if (q != null)
         {
             int.TryParse(q.Value, out DutyIdOfThisYear);
         }
         var p = db.GlobalPrms.Find("RightIdOfThisYear");
         if (p != null)
         {
             RightIdOfThisYear = 0;
             int.TryParse(p.Value, out RightIdOfThisYear);
         }
     }
 }