Example #1
0
    public override void Init()
    {
        sb = base.StrategyBase;
        //mm = strat.MarketManager;
        //ms = sb.MetaStrategy;

        Console.WriteLine("Mode {0}", sb.MetaStrategyBase.MetaStrategyMode);

        TradeDB db = new TradeDB();

        db.Open();

        SqlCommand    cmd;
        SqlDataReader dr;
        //DateTime inc;
        Instrument instrument;
        string     sSymbol;

        string s = "select symbol from ToTrade"
                   + " where tradesystem = @tradesystem"
                   + " and sixmonpossd <  0.97 * sixmonposmean"
                   + " and sixmonnegsd < -0.97 * sixmonnegmean"
                   + " and daycl > 10"
                   + " and daycl < 125"
                   + " and sixmonposmean >  0.12"
                   + " and sixmonnegmean < -0.12"
                   + " order by symbol";

        cmd = new SqlCommand(s, db.Connection);
        cmd.Parameters.Add("@tradesystem", SqlDbType.VarChar);
        cmd.Parameters["@tradesystem"].Value = sTradeSystem;

        int cnt = 0;

        dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            cnt++;
            sSymbol    = dr.GetString(0);
            instrument = InstrumentManager.Instruments[sSymbol];
            AddInstrument(instrument);
            //Console.WriteLine( "{0}.MarketManager", sSymbol );
        }

        db.Close();
        //Console.WriteLine( "{0} symbols added.", cnt );
        //dtCurrentDay = new DateTime( 0 );
        //bRecordDayStartEquity = false;

        //Portfolio.CompositionChanged += new EventHandler(Portfolio_CompositionChanged);
        //Portfolio.ValueChanged += new PositionEventHandler(Portfolio_ValueChanged);
        //Portfolio.TransactionAdded += new TransactionEventHandler(Portfolio_TransactionAdded);
    }
Example #2
0
        public void requestNextWatch()
        {
            string sSymbol;

            if (bMore)
            {
                if (drSelect.Read())
                {
                    sSymbol = drSelect.GetString(0);
                    //Console.WriteLine( "get {0}:{1}", cnt, sSymbol );
                    htSymbols.Add(sSymbol, 1);
                    iq5009.startWatch(sSymbol);
                    cnt++;
                }
                else
                {
                    // close up and finish up
                    bMore = false;
                }
            }
            else
            {
                Console.WriteLine("Finishing counts:" + cnt + ", " + cntQ);
                //if ( 1 < cnt ) {
                //	foreach ( string s in htSymbols.Keys ) Console.WriteLine( "  remaining {0}", s );
                //}
            }
            if (0 == cnt)
            {
                //foreach ( string s in htSymbolsNotFound.Keys ) Console.WriteLine( "  Didn't find {0}", s );
                Console.WriteLine("Done ");

                iq5009.HandleFundamentalMessage -= new FundamentalMessageHandler(iq5009_HandleFundamentalMessage);
                //iq5009.HandleSummaryMessage -= new SummaryMessageHandler(iq5009_HandleSummaryMessage);
                iq5009.HandleWatchSymbolNotFound -= new WatchSymbolNotFoundHandler(iq5009_HandleWatchSymbolNotFound);
                //iq5009.HandleFDel = new Buffer.LineHandler(processF);
                //iq5009.HandleQDel = new Buffer.LineHandler( processQ );
                drSelect.Close();
                db1.Close();
                db2.Close();
                iq5009.Disconnect();

                Pulse();
            }
        }
Example #3
0
        public Pivots(string Symbol, string TradeSystem)
        {
            TradeDB db1 = new TradeDB();

            db1.Open();

            string sql = "select dayhi, daylo, daycl, day3hi, day3lo, weekhi, weeklo, weekcl, monhi, monlo, moncl, "
                         + " sma20day, sma200day, sixmonposmean, sixmonpossd, sixmonnegmean, sixmonnegsd"
                         + " from totrade where tradesystem='" + TradeSystem + "' and symbol = '" + Symbol + "'";
            SqlCommand cmd = new SqlCommand(sql, db1.Connection);

            SqlDataReader dr;

            dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    dayhi         = dr.GetDouble(0);
                    daylo         = dr.GetDouble(1);
                    daycl         = dr.GetDouble(2);
                    day3hi        = dr.GetDouble(3);
                    day3lo        = dr.GetDouble(4);
                    weekhi        = dr.GetDouble(5);
                    weeklo        = dr.GetDouble(6);
                    weekcl        = dr.GetDouble(7);
                    monhi         = dr.GetDouble(8);
                    monlo         = dr.GetDouble(9);
                    moncl         = dr.GetDouble(10);
                    sma20day      = dr.GetDouble(11);
                    sma200day     = dr.GetDouble(12);
                    sixmonposmean = dr.GetDouble(13);
                    sixmonpossd   = dr.GetDouble(14);
                    sixmonnegmean = dr.GetDouble(15);
                    sixmonnegsd   = dr.GetDouble(16);
                }
            }

            dr.Close();
            db1.Close();

            dayPv = (dayhi + daylo + daycl) / 3;
            dayR1 = 2 * dayPv - daylo;
            dayR2 = dayPv + (dayhi - daylo);
            dayR3 = dayR1 + (dayhi - daylo);
            dayS1 = 2 * dayPv - dayhi;
            dayS2 = dayPv - (dayhi - daylo);
            dayS3 = dayS1 - (dayhi - daylo);
            psDay = new PivotSet("day", dayS3, dayS2, dayS1, dayPv, dayR1, dayR2, dayR3);

            day3Pv = (day3hi + day3lo + daycl) / 3;
            day3R1 = 2 * day3Pv - day3lo;
            day3R2 = day3Pv + (day3hi - day3lo);
            day3R3 = day3R1 + (day3hi - day3lo);
            day3S1 = 2 * day3Pv - day3hi;
            day3S2 = day3Pv - (day3hi - day3lo);
            day3S3 = day3S1 - (day3hi - day3lo);
            psDay3 = new PivotSet("day3", day3S3, day3S2, day3S1, day3Pv, day3R1, day3R2, day3R3);

            weekPv = (weekhi + weeklo + weekcl) / 3;
            weekR1 = 2 * weekPv - weeklo;
            weekR2 = weekPv + (weekhi - weeklo);
            weekR3 = weekR1 + (weekhi - weeklo);
            weekS1 = 2 * weekPv - weekhi;
            weekS2 = weekPv - (weekhi - weeklo);
            weekS3 = weekS1 - (weekhi - weeklo);
            psWeek = new PivotSet("week", weekS3, weekS2, weekS1, weekPv, weekR1, weekR2, weekR3);

            monPv   = (monhi + monlo + moncl) / 3;
            monR1   = 2 * monPv - monlo;
            monR2   = monPv + (monhi - monlo);
            monR3   = monR1 + (monhi - monlo);
            monS1   = 2 * monPv - monhi;
            monS2   = monPv - (monhi - monlo);
            monS3   = monS1 - (monhi - monlo);
            psMonth = new PivotSet("mon", monS3, monS2, monS1, monPv, monR1, monR2, monR3);

            // Pivot Information
            if (pivotDraw)
            {
                pivot = (pivotHigh + pivotLow + pivotClose) / 3.0;
                //dsPivot = new DoubleSeries( "Pivot" );
                pivotR1 = 2.0 * pivot - pivotLow;
                pivotS1 = 2.0 * pivot - pivotHigh;
                pivotR2 = pivotR1 + pivot - pivotS1;
                pivotS2 = pivot - pivotR1 - pivotS1;
                Console.WriteLine("pivot {0} r1 {1} r2 {2} s1 {3} s2 {4}",
                                  pivot, pivotR1, pivotR2, pivotS1, pivotS2);
            }
        }
Example #4
0
        public void Run(DateTime dtEnd, bool bDebug)
        {
            TradeDB db1;
            TradeDB db2;

            string sDel = "delete from SymbolStats";
            //string sSelect = "select distinct top 5 symbol from iqRootOptionSymbols";
            string sSelect = "select distinct symbol from iqRootOptionSymbols";
            string sInsert = "insert into SymbolStats "
                             + "( symbol, hi52wk, hi52wkdate, lo52wk, lo52wkdate, "
                             + " hi26wk, hi26wkdate, lo26wk, lo26wkdate, "
                             + " volatility, volume, calcdate ) values ( "
                             + " @symbol, @hi52wk, @hi52wkdate, @lo52wk, @lo52wkdate, "
                             + " @hi26wk, @hi26wkdate, @lo26wk, @lo26wkdate, "
                             + " @volatility, @volume, @calcdate )";

            SqlCommand    cmdDel, cmdSelect, cmdInsert;
            SqlDataReader dr;

            //DateTime dtEnd = new DateTime( 2005, 08, 4 );
            ///DateTime dtEnd = DateTime.Today;
            //string s= dtEnd.ToShortDateString(
            TimeSpan ts52wk       = new TimeSpan(52 * 7, 0, 0, 0);
            DateTime dtStart52wk  = dtEnd.Subtract(ts52wk);
            TimeSpan ts26wk       = new TimeSpan(26 * 7, 0, 0, 0);
            DateTime dtStart26wk  = dtEnd.Subtract(ts26wk);
            TimeSpan ts20day      = new TimeSpan(20, 0, 0, 0);
            DateTime dtStart20day = dtEnd.Subtract(ts20day);

            //DataManager.Init();

            db1 = new TradeDB();
            db1.Open();

            db2 = new TradeDB();
            db2.Open();

            cmdDel    = new SqlCommand(sDel, db1.Connection);
            cmdSelect = new SqlCommand(sSelect, db1.Connection);
            cmdInsert = new SqlCommand(sInsert, db2.Connection);

            cmdInsert.Parameters.Add("@symbol", SqlDbType.VarChar, 12);
            cmdInsert.Parameters.Add("@hi52wk", SqlDbType.Float);
            cmdInsert.Parameters.Add("@hi52wkdate", SqlDbType.DateTime);
            cmdInsert.Parameters.Add("@lo52wk", SqlDbType.Float);
            cmdInsert.Parameters.Add("@lo52wkdate", SqlDbType.DateTime);
            cmdInsert.Parameters.Add("@hi26wk", SqlDbType.Float);
            cmdInsert.Parameters.Add("@hi26wkdate", SqlDbType.DateTime);
            cmdInsert.Parameters.Add("@lo26wk", SqlDbType.Float);
            cmdInsert.Parameters.Add("@lo26wkdate", SqlDbType.DateTime);
            cmdInsert.Parameters.Add("@volatility", SqlDbType.Float);
            cmdInsert.Parameters.Add("@volume", SqlDbType.Float);
            cmdInsert.Parameters.Add("@calcdate", SqlDbType.DateTime);

            cmdDel.ExecuteNonQuery();

            dr = cmdSelect.ExecuteReader();

            string      sSymbol;
            Instrument  instrument;
            DailySeries ds52;
            DailySeries ds26;

            DateTime dtNewHigh52;
            DateTime dtNewHigh26;
            //DateTime dtNewLow52;
            //DateTime dtNewLow26;

            int cntNewHigh52 = 0;
            int cntNewHigh26 = 0;
            int cntNewLow52  = 0;
            int cntNewLow26  = 0;

            while (dr.Read())
            {
                sSymbol = dr.GetString(0);
                if (bDebug)
                {
                    Console.Write("Starting {0}", sSymbol);
                }

                instrument = InstrumentManager.Instruments[sSymbol];
                if (null != instrument)
                {
                    try {
                        ds52 = DataManager.GetDailySeries(instrument, dtStart52wk, dtEnd);
                        ds26 = DataManager.GetDailySeries(instrument, dtStart26wk, dtEnd);
                        if (null != ds26)
                        {
                            dtNewHigh52 = ds52.HighestHighBar(dtStart52wk, dtEnd).DateTime;
                            dtNewHigh26 = ds26.HighestHighBar(dtStart26wk, dtEnd).DateTime;
                            //dtNewLow52  = ds52.LowestLowBar( dtStart52wk, dtEnd ).DateTime;
                            //dtNewLow26  = ds52.LowestLowBar( dtStart26wk, dtEnd ).DateTime;

                            if (dtNewHigh52 == dtEnd)
                            {
                                cntNewHigh52++;
                            }
                            if (dtNewHigh26 == dtEnd)
                            {
                                cntNewHigh26++;
                            }
                            //if ( dtNewLow52  == dtEnd ) cntNewLow52++;
                            //if ( dtNewLow26  == dtEnd ) cntNewLow26++;

                            cmdInsert.Parameters["@symbol"].Value = sSymbol;
                            cmdInsert.Parameters["@hi52wk"].Value = ds52.HighestHigh(dtStart52wk, dtEnd);
                            //cmdInsert.Parameters[ "@hi52wk" ].Value = 0.0;
                            cmdInsert.Parameters["@hi52wkdate"].Value = dtNewHigh52;
                            //cmdInsert.Parameters[ "@hi52wkdate" ].Value = dtEnd;
                            //cmdInsert.Parameters[ "@lo52wk" ].Value = ds52.LowestLow( dtStart52, dtEnd );
                            cmdInsert.Parameters["@lo52wk"].Value = 0.0;
                            //cmdInsert.Parameters[ "@lo52wkdate" ].Value = dtNewLow52;
                            cmdInsert.Parameters["@lo52wkdate"].Value = dtEnd;
                            cmdInsert.Parameters["@hi26wk"].Value     = ds26.HighestHigh(dtStart26wk, dtEnd);
                            cmdInsert.Parameters["@hi26wkdate"].Value = dtNewHigh26;
                            //cmdInsert.Parameters[ "@lo26wk" ].Value = ds52.LowestLow( dtStart26, dtEnd );
                            cmdInsert.Parameters["@lo26wk"].Value = 0.0;
                            //cmdInsert.Parameters[ "@lo26wkdate" ].Value = dtNewLow26;
                            cmdInsert.Parameters["@lo26wkdate"].Value = dtEnd;
                            //try {
                            //	cmdInsert.Parameters[ "@volatility" ].Value = ds52.GetVariance( dtStart20, dtEnd );
                            //}
                            //catch {
                            cmdInsert.Parameters["@volatility"].Value = 0.0;
                            //cmdInsert.Parameters[ "@volatility" ].Value = instrument.Volatility();
                            //}
                            cmdInsert.Parameters["@volume"].Value   = ds26.GetVolumeSeries(dtStart20day, dtEnd).GetMean();
                            cmdInsert.Parameters["@calcdate"].Value = dtEnd;

                            cmdInsert.ExecuteNonQuery();
                        }
                    }
                    catch (Exception e) {
                        if (bDebug)
                        {
                            Console.WriteLine();
                            Console.WriteLine("Error:  {0} {1}", e.Message, e.StackTrace);
                        }
                    }
                }
                if (bDebug)
                {
                    Console.WriteLine();
                }
            }

            db1.Close();
            db2.Close();
            //DataManager.Close();
            Console.WriteLine("{0} 52 High={1}, 52 Low={2}, 26 High={3}, 26 Low={4}",
                              dtEnd, cntNewHigh52, cntNewLow52, cntNewHigh26, cntNewLow26);
            dr.Close();
        }
Example #5
0
        private void startSymbol()
        {
            string     sSymbol;
            Instrument instrument = null;

            if (bSymbolsDone)
            {
                // do nothing
            }
            else
            {
                if (!bAbort && sdr.Read())
                {
                    sSymbol = sdr.GetString(0); // get next symbol in list
                    cntSymbols++;

                    try {
                        instrument                  = new Instrument(sSymbol, st);
                        instrument.Currency         = "USD";
                        instrument.SecurityExchange = "SMART";
                        //instrument.Exchange = "SMART";

                        FIXSecurityAltIDGroup altid;

                        altid = new FIXSecurityAltIDGroup();
                        altid.SecurityAltID       = sSymbol;
                        altid.SecurityAltIDSource = "IB";
                        instrument.AddGroup(altid);
                        altid = null;

                        altid = new FIXSecurityAltIDGroup();
                        altid.SecurityAltID       = sSymbol;
                        altid.SecurityAltIDSource = "IQFeed";
                        instrument.AddGroup(altid);
                        altid = null;

                        instrument.Save();
                    }
                    catch {
                        //Console.WriteLine( "Caught:  Symbol {0} already exists", sSymbol );
                        instrument = InstrumentManager.Instruments[sSymbol];
                    }

                    Console.WriteLine("Starting {0}, {1}", sSymbol, cntSymbols);
                    //Console.WriteLine("Starting {0}, {1} {2} {3}", sSymbol, cntSymbols, hs.intUseCount, hs.intPlaceInQ);

                    if (bTick)
                    {
                        Send(sSymbol, "Trade", instrument, "HT," + sSymbol + "," + cntTick + ";", cntTick);
                    }

                    if (b1min)
                    {
                        Send(sSymbol, "Bar", instrument, "HM," + sSymbol + "," + cnt1min + ",1;", 60, cnt1min);
                    }

                    if (b3min)
                    {
                        Send(sSymbol, "Bar", instrument, "HM," + sSymbol + "," + cnt3min + ",3;", 180, cnt3min);
                    }

                    if (b10min)
                    {
                        Send(sSymbol, "Bar", instrument, "HM," + sSymbol + "," + cnt10min + ",10;", 600, cnt10min);
                    }

                    if (b30min)
                    {
                        Send(sSymbol, "Bar", instrument, "HM," + sSymbol + "," + cnt30min + ",30;", 1800, cnt30min);
                    }

                    if (b2hr)
                    {
                        Send(sSymbol, "Bar", instrument, "HM," + sSymbol + "," + cnt2hr + ",120;", 7200, cnt2hr);
                    }

                    if (bDaily)
                    {
                        //Console.WriteLine( "Starting {0}, {1} {2} {3}", sSymbol, cntSymbols, hs.intUseCount, hs.intPlaceInQ );
                        Send(sSymbol, "Daily", instrument, "HD," + sSymbol + "," + cntDaily + ";", cntDaily);
                    }
                }
                else
                {
                    Console.WriteLine("All {0} symbols requested", cntSymbols);
                    bSymbolsDone = true;
                    sdr.Close();
                    db.Close();
                }
            }
        }
Example #6
0
        public void Run()
        {
            //
            // TODO: Add constructor logic here
            //
            // http://www.dtniq.com/product/mktsymbols.zip

            //			string sConn0 = "Integrated Security=SSPI;" +
            //				"Persist Security Info=False;Initial Catalog=Trade;" +
            //				"Data Source=localhost;" +
            //				"Packet Size=4096;" +
            //				"";
            //			string sConn = "Integrated Security=SSPI;" +
            //				"Persist Security Info=False;Initial Catalog=Trade;" +
            //				"Data Source=localhost;" +
            //				"Packet Size=4096;Workstation ID=I9100;" +
            //				"";

            TradeDB db1;

            db1 = new TradeDB();
            db1.Open();

            //string sConn = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Trade;Data Source=ARMOR01;Packet Size=4096;";
            //SqlConnection connTrade = new SqlConnection();
            //connTrade.ConnectionString = sConn;
            //connTrade.ConnectionTimeout= 14400;
            //connTrade.Open();

            string     sDelete;
            SqlCommand cmdDelete;

            sDelete                  = "delete from iqSymbols";
            cmdDelete                = new SqlCommand(sDelete, db1.Connection);
            cmdDelete.CommandType    = System.Data.CommandType.Text;
            cmdDelete.CommandTimeout = 14400;
            cmdDelete.ExecuteNonQuery();

            sDelete                  = "delete from iqRootOptionSymbols";
            cmdDelete                = new SqlCommand(sDelete, db1.Connection);
            cmdDelete.CommandType    = System.Data.CommandType.Text;
            cmdDelete.CommandTimeout = 14400;
            cmdDelete.ExecuteNonQuery();

            string sInsert = "insert into iqSymbols ( symbol, descr, exchange, isindex, iscboeindex, isindicator, "
                             + "ismutualfund, ismoneymarketfund ) "
                             + "values ( @symbol, @descr, @exchange, @isindex, @iscboeindex, @isindicator, "
                             + "@ismutualfund, @ismoneymarketfund )";
            SqlCommand cmdInsert = new SqlCommand(sInsert, db1.Connection);

            cmdInsert.CommandType = System.Data.CommandType.Text;

            cmdInsert.Parameters.Add("@symbol", SqlDbType.VarChar);
            cmdInsert.Parameters.Add("@descr", SqlDbType.VarChar);
            cmdInsert.Parameters.Add("@exchange", SqlDbType.VarChar);
            cmdInsert.Parameters.Add("@isindex", SqlDbType.Bit);
            cmdInsert.Parameters.Add("@iscboeindex", SqlDbType.Bit);
            cmdInsert.Parameters.Add("@isindicator", SqlDbType.Bit);
            cmdInsert.Parameters.Add("@ismutualfund", SqlDbType.Bit);
            cmdInsert.Parameters.Add("@ismoneymarketfund", SqlDbType.Bit);

            Stream       fsSymbols = new FileStream(dir, FileMode.Open);
            StreamReader srSymbols = new StreamReader(fsSymbols, Encoding.ASCII);

            string l;
            int    i = 0;

            string [] items  = null;
            string    sDelim = "\t";

            char [] chDelim = sDelim.ToCharArray();
            //string t;
            string sSymbol;
            string sExchange;

            bool bMutual;
            bool bMoneyMkt;
            bool bIndex;
            bool bCboe;
            bool bIndicator;

            Hashtable htExchange = new Hashtable();

            //int cntLines = 0;

            srSymbols.ReadLine();
            //&& cntLines++ < 34
            while (srSymbols.Peek() >= 0)
            {
                l = srSymbols.ReadLine();
                //Console.WriteLine( l );
                items = l.Split(chDelim);
                //foreach ( string t in items ) {
                //Console.Write( "'" + t + "'," );
                //}
                if (3 != items.GetLength(0))
                {
                    Console.WriteLine("Missing an element: {0}", l);
                    continue;
                }
                sSymbol    = items[0];
                sExchange  = items[2];
                bMutual    = Regex.IsMatch(sSymbol, ".{3}[^\\.]X$", RegexOptions.None) && 5 == sSymbol.Length;
                bMoneyMkt  = Regex.IsMatch(sSymbol, ".{3}XX$", RegexOptions.None) && 5 == sSymbol.Length;
                bIndex     = Regex.IsMatch(sSymbol, ".+\\.X$", RegexOptions.None);
                bCboe      = Regex.IsMatch(sSymbol, ".+\\.XO$", RegexOptions.None);
                bIndicator = Regex.IsMatch(sSymbol, ".+\\.Z$", RegexOptions.None);

                cmdInsert.Parameters[0].Value = sSymbol;
                cmdInsert.Parameters[1].Value = items[1];
                cmdInsert.Parameters[2].Value = items[2];
                cmdInsert.Parameters[3].Value = bIndex;
                cmdInsert.Parameters[4].Value = bCboe;
                cmdInsert.Parameters[5].Value = bIndicator;
                cmdInsert.Parameters[6].Value = bMutual;
                cmdInsert.Parameters[7].Value = bMoneyMkt;

                cmdInsert.ExecuteNonQuery();

                //Console.WriteLine( htExchange[ sExchange ].GetType() );
                if (htExchange.ContainsKey(sExchange))
                {
                }
                else
                {
                    htExchange[sExchange] = 0;
                }
                //htExchange[ sExchange ]++;
                i = (int)htExchange[sExchange];
                i++;
                htExchange[sExchange] = i;
                //i = (int) htExchange[ sExchange ];
                //i++;
                //htExchange[ sExchange ] = i;


                //Console.WriteLine();
            }

            Console.WriteLine("{0}", DateTime.Now);
            ArrayList aKeys = new ArrayList(htExchange.Keys);

            aKeys.Sort();
            foreach (string s in aKeys)
            {
                Console.WriteLine("{0} {1}", s, htExchange[s]);
            }

            //foreach ( string s in args ) {
            //Console.WriteLine( s );
            //}


            srSymbols.Close();
            fsSymbols.Close();

            db1.Close();
        }