Пример #1
0
        static void testMarket1()
        {
            AMarket   market = AMarket.GetInstance(MarketVendor.Exchange);
            ASecurity s1     = new ETF("510050", Exchange.SHE);   //50ETF
            ASecurity s2     = new ETF("510180", Exchange.SHE);   //100ETF
            ASecurity s3     = new Stock("601318", Exchange.SHE); //中国平安
            ASecurity s4     = new Stock("600104", Exchange.SHE); //上汽集团

            List <ASecurity> seclist = new List <ASecurity>();

            seclist.Add(s1);
            seclist.Add(s2);
            seclist.Add(s3);
            seclist.Add(s4);

            List <Option> optionlist = market.GetOptionSet(seclist);

            //ASecurity s = (ASecurity)market.htUnderlyingSets["510050"];
            //s.name = "更改";

            if (optionlist != null)
            {
                foreach (Option o in optionlist)
                {
                    o.DebugPrint();
                }
            }
            market.Dispose();
        }
Пример #2
0
        static void testMarket4()
        {
            AMarket market = AMarket.GetInstance(MarketVendor.Exchange);
            ETF     etf    = new ETF("510050", Exchange.SHE);
            Stock   s      = new Stock("601318", Exchange.SHE);

            market.SubscribeBidAskBook(etf, 2);
            market.SubscribeBidAskBook(s, 2);
            market.Dispose();
        }
        public AssetBase CreateAsset(AssetClass assetClass, string ticker, OptionClass?optionClass = null, string underlyingTicker = null, DateTime?expiration = null, double?strike = null)
        {
            switch (assetClass)
            {
            case AssetClass.Stock:
                Stock stock = new Stock(ticker);
                SaveAsset(stock);
                return(stock);

            case AssetClass.ETF:
                ETF etf = new ETF(ticker);
                SaveAsset(etf);
                return(etf);

            case AssetClass.Bond:
                Bond bond = new Bond(ticker);
                SaveAsset(bond);
                return(bond);

            case AssetClass.Option:
                if (optionClass.HasValue)
                {
                    Option option          = new Option(optionClass.Value, ticker);
                    Stock  unserlyingAsset = (Stock)GetAssetyBy(AssetClass.Stock, underlyingTicker);
                    if (unserlyingAsset == null)
                    {
                        unserlyingAsset = new Stock(underlyingTicker);
                        SaveAsset(unserlyingAsset);
                    }
                    option.UnderlyingAsset = unserlyingAsset;
                    option.ExpirationDate  = expiration.Value;
                    option.Strike          = strike.Value;
                    SaveAsset(option);
                    return(option);
                }
                else
                {
                    throw new ArgumentException("Must provide option class (call other put), underlying asset, strike and expiration when asset is an option");
                }

            case AssetClass.Cash:
                Cash cash = new Cash(ticker);
                SaveAsset(cash);
                return(cash);

            default:
                throw new NotImplementedException($"There is no DBSet AssetClass={assetClass.ToString()}");
            }
        }
Пример #4
0
    private static void Main()
    {
        var output             = new StringBuilder();
        int remainingTestCases = int.Parse(Console.ReadLine());

        while (remainingTestCases-- > 0)
        {
            int n = int.Parse(Console.ReadLine());
            output.Append(
                ETF.Solve(n));
            output.AppendLine();
        }

        Console.Write(output);
    }
Пример #5
0
        public async Task <bool> CreateETF(ETFCreate model)
        {
            var entity = new ETF()
            {
                AuthorId = _userId,
                Name     = model.Name,
                Ticker   = model.Ticker
            };

            using (var ctx = new ApplicationDbContext())
            {
                var query = ctx.ETFs.Add(entity);
                return(await ctx.SaveChangesAsync() == 1);
            }
        }
Пример #6
0
        static void Main(string[] args)
        {
            CB cb = new CB();

            Console.WriteLine("== test CB =====");
            Console.WriteLine("give price 149.9, idx = {0}, tick_size={1:n}", cb.inPriceRange(149.9m), cb.getTickSize(149.9m));
            Console.WriteLine("give price 100 and 101, ticks  = {0}", cb.TicksBetween(100m, 101m));
            Console.WriteLine("give price 153.5 and 155, ticks  = {0}", cb.TicksBetween(153.5m, 155m));

            Console.WriteLine("give price 149.9 and 150, ticks  = {0}", cb.TicksBetween(149.9m, 150m));
            Console.WriteLine("give price 150 and 999, ticks  = {0}", cb.TicksBetween(150m, 999m));
            Console.WriteLine("give price 150 and 1000, ticks  = {0}", cb.TicksBetween(150m, 1000m));
            Console.WriteLine("give price 149.9 and 1005, ticks  = {0}", cb.TicksBetween(149.9m, 1005m));
            Console.WriteLine("give price 149.9 and 1003, ticks  = {0}", cb.TicksBetween(149.9m, 1003m));

            Console.WriteLine("price 100.5 move 3 ticks  = {0:n}", cb.TickMove(100.5m, 3));
            Console.WriteLine("price 149.9 move 5 ticks  = {0:n}", cb.TickMove(149.9m, 5));
            Console.WriteLine("price 998 move 5 ticks  = {0:n}", cb.TickMove(998m, 5));


            Console.WriteLine("== test ETF =====");
            ETF etf = new ETF();

            Console.WriteLine("give price 40 and 41, ticks  = {0}", etf.TicksBetween(40m, 41m));
            Console.WriteLine("give price 60 and 62, ticks  = {0}", etf.TicksBetween(60m, 62m));
            Console.WriteLine("price 50.2 move -5 ticks  = {0:n}", etf.TickMove(50.2m, -5));
            Console.WriteLine("price 49.98 move 5 ticks  = {0:n}", etf.TickMove(49.98m, 5));


            Console.WriteLine("== test X =====");
            TX tx = new TX();

            Console.WriteLine("give price 49.5 and 51, ticks  = {0}", tx.TicksBetween(49.5m, 51m));
            Console.WriteLine("give price 498.5 and 501, ticks  = {0}", tx.TicksBetween(498.5m, 501m));
            Console.WriteLine("price 49.6 move 8 ticks  = {0:n}", tx.TickMove(49.6m, 8));
            Console.WriteLine("price 498.5 move 6 ticks  = {0:n}", tx.TickMove(498.5m, 6));
            Console.WriteLine("price 51.5 move -6 ticks  = {0:n}", tx.TickMove(51.5m, -6));
            Console.WriteLine("price 505 move -6 ticks  = {0:n}", tx.TickMove(505m, -6));

            Console.Read();
        }
Пример #7
0
        private void readOptionContractFile()
        {
            try
            {
                if (this.htOptionSets.Count == 0)
                {
                    #region 读取文件
                    //复制到本地
                    File.Copy(filepath_option_contract_source, filepath_option_contract_target, true);

                    //读取该文件
                    string filecontent = "";
                    using (StreamReader sReader = new StreamReader(filepath_option_contract_target, Encoding.Default))
                    {
                        filecontent = sReader.ReadToEnd();
                        sReader.Close();
                    }
                    #endregion

                    #region 解析合约
                    string[] lines = filecontent.Split("\n".ToCharArray());
                    if (lines == null || lines.Length == 0)
                    {
                        MessageManager.GetInstance().Add(MessageType.Error, string.Format("期权合约文件为空"));
                        return;
                    }

                    List <Option> optionlist;
                    foreach (string line in lines)
                    {
                        if (line != null && line.Trim().Length > 0)
                        {
                            string[] fields = line.Split("|".ToCharArray());

                            if (fields.Length >= 34)
                            {
                                //代码
                                Option o = new Option(fields[1].Trim(), Exchange.SHE);
                                o.contractcode    = fields[2].Trim();
                                o.name            = fields[3].Trim();
                                o.underlyingunits = Convert.ToInt16(fields[9].Trim());
                                o.strike          = Convert.ToDouble(fields[10].Trim());
                                o.exercisedate    = Utility.ConvertToDateTime(fields[13].Trim(), "yyyyMMdd");
                                o.priceuplimit    = Convert.ToDouble(fields[22].Trim());
                                o.pricedownlimit  = Convert.ToDouble(fields[23].Trim());
                                o.marginunit      = Convert.ToDouble(fields[24].Trim());
                                o.lmtordmaxfloor  = Convert.ToInt16(fields[29].Trim());
                                o.ticksize        = Convert.ToDouble(fields[32].Trim());
                                o.daystoexercise  = (o.exercisedate - DateTime.Today).Days;
                                o.exercisemonth   = o.exercisedate.Month;

                                #region 标的
                                string underlyingtype = fields[6].Trim().ToUpper();
                                string underlyingcode = fields[4].Trim();
                                if (this.htUnderlyingSets.Contains(underlyingcode))
                                {
                                    //标的已存在
                                    o.underlying = (ASecurity)htUnderlyingSets[underlyingcode];
                                }
                                else
                                {
                                    //新建标的
                                    ASecurity s = null;
                                    switch (underlyingtype)
                                    {
                                    case "ASH":     //股票
                                        s = new Stock(underlyingcode, Exchange.SHE);
                                        break;

                                    case "EBS":     //ETF
                                        s = new ETF(underlyingcode, Exchange.SHE);
                                        break;

                                    default:
                                        break;
                                    }

                                    if (s != null)
                                    {
                                        s.name = fields[5].Trim();
                                        htUnderlyingSets.Add(underlyingcode, s);
                                        o.underlying = s;
                                    }
                                }
                                #endregion

                                //类型:call or put
                                string type = fields[8].Trim().ToUpper();
                                if (type == "C")
                                {
                                    o.type = OptionType.CALL;
                                }
                                else
                                {
                                    o.type = OptionType.PUT;
                                }

                                //交易标志
                                char[] flag = fields[33].Trim().ToUpper().ToCharArray();
                                o.islimitopening = (flag[0] == '0') ? false : true;     //第1位:‘0’表示可开仓,‘1’表示限制卖出开仓(不.包括备兑开仓)和买入开仓。
                                o.istrading      = (flag[1] == '0') ? true : false;     //第2位:‘0’表示未连续停牌,‘1’表示连续停牌。
                                o.isnew          = (flag[4] == 'A') ? true : false;     //第5位:‘A’表示当日新挂牌的合约,‘E’表示存续的合约

                                //加入列表
                                if (this.htOptionSets.Contains(o.underlying.code))
                                {
                                    optionlist = (List <Option>) this.htOptionSets[o.underlying.code];
                                    optionlist.Add(o);
                                }
                                else
                                {
                                    optionlist = new List <Option>();
                                    optionlist.Add(o);
                                    this.htOptionSets.Add(o.underlying.code, optionlist);
                                }
                            }
                        }
                    }
                    #endregion
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 private void SaveAsset(ETF etf)
 {
     ETFs.Add(etf);
     SaveChanges();
 }