示例#1
0
        public static void addPreciousMetalSpots(EWrapperImpl ibClient)
        {
            string[] names = new string[] {
                "XAUUSD",
                "XAGUSD",
                "XPTUSD"
            };
            foreach (string symbol in names)
            {
                Contract contract = new Contract();
                contract.Symbol   = symbol;
                contract.SecType  = "CMDTY";
                contract.Currency = "USD";

                int reqId = TickerGenerator.instance.get();
                ibClient.IBClientSocket.reqContractDetails(reqId, contract);
                while (!ibClient.contractDetailsMap.ContainsKey(reqId))
                {
                    Thread.Sleep(100);
                }
                ContractDetails details = ibClient.contractDetailsMap[reqId];
                if (DBAccess.DBAccess.instance.setContract(details, "US", new TimeSpan(9, 30, 0), new TimeSpan(16, 0, 0), true))
                {
                    Console.WriteLine("Added " + symbol);
                }
            }
        }
示例#2
0
        public static void addFXSpots(EWrapperImpl ibClient)
        {
            string[][] names = new string[][] {
                new string[] { "NZD", "USD" },
                new string[] { "AUD", "USD" },
                new string[] { "GBP", "USD" },
                new string[] { "USD", "JPY" },
                new string[] { "EUR", "USD" },
                new string[] { "USD", "CAD" },
                new string[] { "USD", "CHF" }
            };
            foreach (string[] symbol in names)
            {
                Contract contract = new Contract();
                contract.Symbol   = symbol[0];
                contract.SecType  = "CASH";
                contract.Currency = symbol[1];

                int reqId = TickerGenerator.instance.get();
                ibClient.IBClientSocket.reqContractDetails(reqId, contract);
                while (!ibClient.contractDetailsMap.ContainsKey(reqId))
                {
                    Thread.Sleep(100);
                }
                ContractDetails details = ibClient.contractDetailsMap[reqId];
                if (DBAccess.DBAccess.instance.setContract(details, "CME", new TimeSpan(17, 15, 0), new TimeSpan(17, 0, 0), true))
                {
                    Console.WriteLine("Added " + symbol);
                }
            }
        }
示例#3
0
        public BacktestEngine(EWrapperImpl wrapper, DBAccess.DBAccess db, XmlNode config)
        {
            _wrapper = wrapper;
            _db      = db;
            _tm      = ThreadManager.instance;
            lock (_lock)
            {
                _wrapper.nextOrderId = DBAccess.DBAccess.instance.getMaxOrderId() + 1;
            }
            _config    = config;
            _account   = config["Account"].InnerText;
            _startDate = DateTime.Parse(config["StartDate"].InnerText);
            string endDateStr = config["EndDate"].InnerText;

            if (endDateStr == "Today")
            {
                _endDate = DateTime.Today;
            }
            else
            {
                _endDate = DateTime.Parse(endDateStr);
            }
            _barType            = config["BarType"].InnerText;
            _minBarSize         = Int32.Parse(config["MinBarSize"].InnerText);
            _slippage           = Int32.Parse(config["Slippage"].InnerText);
            _limitIncludeTouch  = Boolean.Parse(config["LimitIncludeTouch"].InnerText);
            _source             = config["MarketDataSource"].InnerText;
            _reportFolder       = config["ReportFolder"].InnerText;
            _interval           = new TimeSpan(0, 0, _minBarSize);
            _mktData            = new Dictionary <int, Bar[]>();
            _currMktIndices     = new Dictionary <int, int>();
            _barIds             = new List <int>();
            _barTickerMap       = new Dictionary <int, int>();
            _tickTickerMap      = new Dictionary <int, int>();
            _mktTimeZoneMap     = new Dictionary <int, TimeZoneInfo>();
            _contractBarMap     = new Dictionary <int, int>();
            _orderContractMap   = new Dictionary <int, Contract>();
            _barSlippageMap     = new Dictionary <int, double>();
            _currTime           = _startDate;
            _currd              = _currTime.Date;
            _liveOrders         = new SortedDictionary <int, Order>();
            _ocaGroups          = new Dictionary <string, List <int> >();
            _childOrders        = new Dictionary <int, List <int> >();
            _executions         = new SortedDictionary <int, Execution>();
            _timeEventHandlers  = new SortedDictionary <int, List <onTime> >();
            _timeEventRefreshed = new Dictionary <int, bool>();
            _isDone             = new Dictionary <int, bool>();
            _sleepId            = 0;
            _sleepEvents        = new Dictionary <int, AutoResetEvent>();
            _nextBarEvent       = new ManualResetEvent(true);
        }
示例#4
0
        //IBStrategy.exe --strategies=43,44,42 --run_config=E:\Temp\AccountConfig.xml
        static void Main(string[] args)
        {
            List <int> strategy_ids = new List <int>();
            string     run_config_file;
            Dictionary <string, string> argmap = new Dictionary <string, string>();

            if (args != null && args.Length > 0)
            {
                foreach (string arg in args)
                {
                    if (arg.StartsWith("--strategies"))
                    {
                        argmap.Add("strategies", arg.Substring(13));
                    }
                    if (arg.StartsWith("--run_config"))
                    {
                        argmap.Add("run_config", arg.Substring(13));
                    }
                }
            }
            if (!argmap.ContainsKey("run_config"))
            {
                Console.Write("Please input run config file: ");
                argmap.Add("run_config", Console.ReadLine());
            }
            if (!argmap.ContainsKey("strategies"))
            {
                Console.Write("Please input strategy ids: ");
                argmap.Add("strategies", Console.ReadLine());
            }
            string[] strats = argmap["strategies"].Split(',');
            int      ret;

            foreach (string strat in strats)
            {
                if (Int32.TryParse(strat, out ret))
                {
                    strategy_ids.Add(ret);
                    Console.WriteLine("Adding strategy " + ret.ToString());
                }
            }
            run_config_file = argmap["run_config"];
            Console.WriteLine("Run Config: " + run_config_file);
            string      run_config = File.ReadAllText(run_config_file);
            XmlDocument xml        = new XmlDocument();

            xml.LoadXml(run_config);
            XmlNode xn1      = xml.SelectSingleNode("/Config");
            string  server   = xn1["Server"].InnerText;
            string  database = xn1["Database"].InnerText;

            //string engine_type = xn1["EngineType"].InnerText;
            DBAccess.DBAccess.instance.connect(server, database);
            Thread.Sleep(1000);

            EWrapperImpl ibClient    = new EWrapperImpl(run_config, true);
            string       engine_type = xn1["EngineType"].InnerText;

            if (engine_type == "TWS")
            {
                XmlNode xnib      = xn1.SelectSingleNode("IBConfig");
                string  ip        = xnib["IP"].InnerText;
                int     port      = Int32.Parse(xnib["Port"].InnerText);
                bool    extraAuth = Boolean.Parse(xnib["ExtraAuth"].InnerText);
                ibClient.IBClientSocket.eConnect(ip, port, 0, extraAuth);
                var reader = new EReader(ibClient.IBClientSocket, ibClient.Signal);
                reader.Start();
                new Thread(() => { while (ibClient.IBClientSocket.IsConnected())
                                   {
                                       ibClient.Signal.waitForSignal(); reader.processMsgs();
                                   }
                           })
                {
                    IsBackground = true
                }.Start();
            }
            while (ibClient.nextOrderId <= 0)
            {
                Thread.Sleep(100);
            }
            OrderManager.instance.set(Math.Max(ibClient.nextOrderId - 1, DBAccess.DBAccess.instance.getMaxOrderId()));
            ibClient.initialize(strategy_ids);

            //Utilities.addSGXFutures(ibClient);
            //Utilities.addOilFutures(ibClient);
            //Utilities.addNYMEXFutures(ibClient);
            //Utilities.addUSLiquidETFs(ibClient);
            //Utilities.addUSHighVolStocks(ibClient);
            //Utilities.addPreciousMetalSpots(ibClient);
            //Utilities.addFXSpots(ibClient);
            if (engine_type == "TWS")
            {
                while (true)
                {
                }
                ;
            }
            else
            {
                ibClient.BacktestClientSocket.run();
            }
        }
示例#5
0
 public static void addSGXFutures(EWrapperImpl ibClient)
 {
     string[] expiries = new string[] {
         "201703",
         "201702",
         "201701",
         "201612",
         "201611",
         "201610",
         "201609",
         "201608",
         "201607",
         "201606",
         "201605",
         "201604",
         "201603",
         "201602",
         "201601",
         "201512"
     };
     string[] expiries_nk = new string[]
     {
         "201703",
         "201612",
         "201609",
         "201606",
         "201603",
         "201512"
     };
     Tuple <string, string, TimeSpan, TimeSpan, string[]>[] symbols = new Tuple <string, string, TimeSpan, TimeSpan, string[]>[] {
         new Tuple <string, string, TimeSpan, TimeSpan, string[]>("STW", "USD", new TimeSpan(8, 30, 0), new TimeSpan(13, 45, 0), expiries),
         new Tuple <string, string, TimeSpan, TimeSpan, string[]>("SSG", "SGD", new TimeSpan(8, 30, 0), new TimeSpan(17, 10, 0), expiries),
         new Tuple <string, string, TimeSpan, TimeSpan, string[]>("XINA50", "USD", new TimeSpan(9, 0, 0), new TimeSpan(16, 30, 0), expiries),
         new Tuple <string, string, TimeSpan, TimeSpan, string[]>("NIFTY", "USD", new TimeSpan(9, 0, 0), new TimeSpan(18, 15, 0), expiries),
         new Tuple <string, string, TimeSpan, TimeSpan, string[]>("SGXNK", "JPY", new TimeSpan(7, 30, 0), new TimeSpan(14, 30, 0), expiries_nk),
     };
     foreach (var info in symbols)
     {
         foreach (string expiry in info.Item5)
         {
             Contract stw = new Contract();
             stw.Symbol   = info.Item1;
             stw.SecType  = "FUT";
             stw.Currency = info.Item2;
             stw.Exchange = "SGX";
             stw.LastTradeDateOrContractMonth = expiry;
             stw.IncludeExpired = true;
             int reqId = TickerGenerator.instance.get();
             ibClient.IBClientSocket.reqContractDetails(reqId, stw);
             while (!ibClient.contractDetailsMap.ContainsKey(reqId))
             {
                 Thread.Sleep(100);
             }
             ContractDetails details = ibClient.contractDetailsMap[reqId];
             if (DBAccess.DBAccess.instance.setContract(details, "SGX", info.Item3, info.Item4, true))
             {
                 Console.WriteLine("Added " + stw.Symbol + " " + expiry);
             }
         }
     }
 }
示例#6
0
 public static void addNYMEXFutures(EWrapperImpl ibClient)
 {
     string[] names = new string[] {
         "CL",
         "RB",
         "HO",
         "NG"
     };
     string[] expiries = new string[] {
         "201503",
         "201504",
         "201505",
         "201506",
         "201507",
         "201508",
         "201509",
         "201510",
         "201511",
         "201512",
         "201601",
         "201602",
         "201603",
         "201604",
         "201605",
         "201606",
         "201607",
         "201608",
         "201609",
         "201610",
         "201611",
         "201612",
         "201701",
         "201702",
         "201703",
         "201704",
         "201705",
         "201706",
         "201707",
         "201708",
         "201709"
     };
     foreach (string symbol in names)
     {
         foreach (string expiry in expiries)
         {
             Contract contract = new Contract();
             contract.Symbol   = symbol;
             contract.SecType  = "FUT";
             contract.Currency = "USD";
             contract.Exchange = "NYMEX";
             contract.LastTradeDateOrContractMonth = expiry;
             contract.IncludeExpired = true;
             int reqId = TickerGenerator.instance.get();
             ibClient.IBClientSocket.reqContractDetails(reqId, contract);
             while (!ibClient.contractDetailsMap.ContainsKey(reqId))
             {
                 Thread.Sleep(100);
             }
             ContractDetails details = ibClient.contractDetailsMap[reqId];
             if (DBAccess.DBAccess.instance.setContract(details, "CME", new TimeSpan(18, 0, 0), new TimeSpan(17, 0, 0), true)) // EST
             {
                 Console.WriteLine("Added " + symbol);
             }
         }
     }
 }
示例#7
0
 public static void addUSHighVolStocks(EWrapperImpl ibClient)
 {
     //These names are of top intraday volatilities,
     //with daily volume > 2M and price in [20,200]
     string[] names = new string[] {
         "IONS",
         "MNK",
         "X",
         "ACAD",
         "CLVS",
         "CRC",
         "CC",
         "FRAC",
         "NVDA",
         "HTZ",
         "JUNO",
         "ATI",
         "SM",
         //"TCK",
         "SLW",
         "MOMO",
         "KSS",
         "NEM",
         "ALXN",
         "FNSR",
         "TECK",
         "GPOR",
         "FSLR",
         "AEM",
         "JBLU",
         "PBF",
         "M",
         "RICE",
         "GPS",
         "MU",
         "RRC",
         "AA",
         "STX",
         "HFC",
         "JWN",
         "HES",
         "MUR",
         "MBLY",
         "URBN",
         "CF",
         "XIV",
         "STLD",
         "QCOM",
         "LW",
         "PK",
         "PTEN",
         "CMC",
         "VRTX",
         "YELP",
         "MAT",
         "CLR",
         "MYL",
         "COG",
         "WDC",
         "TEVA",
         "NBL",
         "BAC",
         "CELG",
         "GME",
         "MPC",
         "YUMC",
         "AOBC",
         "BMY",
         "SWFT",
         "DKS",
         "DVN",
         "NFLX",
         "PE",
         "ETP",
         "SQQQ",
         "MOS",
         "WYNN",
         "BBBY",
         "CXW",
         "NFX",
         "KORS",
         "NUE",
         "AR",
         "TSO",
         "BBY",
         "UAL",
         "WMB",
         "UA",
         "AAL",
         "SWKS",
         "LVS",
         "JD",
         "ARNC",
         "TRIP",
         "MCK",
         "BX",
         "KMI",
         "DAL",
         "UAA",
         "DISH",
         "AET",
         "PAA",
         "ABC",
         "VLO",
         "CIEN",
         "SKX",
         "TRGP",
         "NOV",
         "APC",
         "GM",
         "APA",
         //"AWAY",
         "MS",
         "HPE",
         "PHM",
         "FITB",
         "C",
         "DLTR",
         "LUV",
         "KSU",
         "COP",
         "LB",
         "SUM",
         "CFG",
         "LNG",
         "ESRX",
         "WNR",
         "BHI",
         "ZAYO",
         "SXL",
         "DISCA",
         "MGM",
         "ZION",
         "EVHC",
         "KMX",
         "FTNT",
         "DHI",
         "JBL",
         "YNDX",
         "EOG",
         "LULU",
         "HBI",
         "XLNX",
         "TMUS",
         "OLN",
         "BABA",
         "MNST",
         "BIDU",
         "GGP",
         "HUN",
         "DB",
     };
     foreach (string symbol in names)
     {
         Contract contract = new Contract();
         contract.Symbol   = symbol;
         contract.SecType  = "STK";
         contract.Currency = "USD";
         contract.Exchange = "SMART";
         int reqId = TickerGenerator.instance.get();
         ibClient.IBClientSocket.reqContractDetails(reqId, contract);
         while (!ibClient.contractDetailsMap.ContainsKey(reqId))
         {
             Thread.Sleep(100);
         }
         ContractDetails details = ibClient.contractDetailsMap[reqId];
         if (DBAccess.DBAccess.instance.setContract(details, "US", new TimeSpan(9, 30, 0), new TimeSpan(16, 0, 0), true))
         {
             Console.WriteLine("Added " + symbol);
         }
     }
     Console.WriteLine("US High Vol stocks all loaded!");
 }
示例#8
0
        public static void addUSLiquidETFs(EWrapperImpl ibClient)
        {
            string[] names = new string[] {
                "IWM",
                "IWN",
                "IWO",
                "IWF",
                "IWD",
                "IWB",
                "IWV",
                "IUSV",
                "IUSG",
                "IWS",
                "IWC",
                "IWR",
                "IWP",

                "SPY",
                //"UWTI",
                "VXX",
                "UGAZ",
                "EEM",
                "XLF",
                "EWJ",
                "NUGT",
                "QQQ",
                "FXI",
                "USO",
                "EFA",
                "XIV",
                "EWZ",
                "TZA",
                "XLE",
                "UVXY",
                "XLU",
                "VWO",
                "DGAZ",
                "RSX",
                "TLT",
                "XOP",
                "SDS",
                "IYR",
                "XLI",
                "XLK",
                "JNK",
                "EWT",
                "XLV",
                "UCO",
                "XLP",
                "EZU",
                "OIH",
                "TVIX",
                "HYG",
                "SQQQ",
                "SLV",
                "EWG",
                "UNG",
                "DUST",
                "AMLP",
                "XLY",
                "EPI",
                "VGK",
                "DIA",
                "VEA",
                "TNA",
                "SPXU",
                "KRE",
                "XLB",
                "JDST",
                "DBEF",
                "VNQ",
                "SMH",
                "SSO",
                "JNUG",
                "OIL",
                "QID",
                "IVV",
                "SH",
                "TBT",
                "FAS",
                "EWH",
                "LQD",
                "ITB",
                "XHB",
                "TQQQ",
                "IAU",
                "FAZ",
                "EWU",
                "UUP",
                "FEZ",
                "IEMG",
                "UPRO",
                "VTI",
                "XME",
                "EWY",
                "BKLN",
                "EWI",
                "INDA",
                "EWW",
                "BND",
                "XRT",
                "EWA",
                "PFF",
                "EWC",
                "AGG",
                "VEU",
                //"DWTI",
                "AMJ",
                "DBC"
            };
            foreach (string symbol in names)
            {
                Contract contract = new Contract();
                contract.Symbol   = symbol;
                contract.SecType  = "STK";
                contract.Currency = "USD";
                contract.Exchange = "SMART";
                int reqId = TickerGenerator.instance.get();
                ibClient.IBClientSocket.reqContractDetails(reqId, contract);
                while (!ibClient.contractDetailsMap.ContainsKey(reqId))
                {
                    Thread.Sleep(100);
                }
                ContractDetails details = ibClient.contractDetailsMap[reqId];
                if (DBAccess.DBAccess.instance.setContract(details, "US", new TimeSpan(9, 30, 0), new TimeSpan(16, 0, 0), true))
                {
                    Console.WriteLine("Added " + symbol);
                }
            }
            Console.WriteLine("US Liquid ETFs all loaded!");
        }