Beispiel #1
0
 static void Main(string [] args)
 {
     try
     {
         bool reporterror = true;
         if (args.Length >= 2)
         {
             pcmdsupport.ParseDBCMDLine       dbcmdline       = new pcmdsupport.ParseDBCMDLine(args [0]);
             pcmdsupport.ParseAccountsCMDLine accountscmdline = new pcmdsupport.ParseAccountsCMDLine(args [1]);
             if (!dbcmdline.InError && !accountscmdline.InError)
             {
                 pshare ps = new pshare();
                 ps.Go(dbcmdline, accountscmdline, 2, args);
                 Console.Write("Finished");
                 reporterror = false;
             }
         }
         if (reporterror)
         {
             Console.WriteLine("Command Line should be:-");
             Console.Write("pshare " + pcmdsupport.ParseDBCMDLine.SupportedFormatString() + " " +
                           pcmdsupport.ParseAccountsCMDLine.SupportedFormatString() + " [ticker,displayname,exchange,maxoi,maxvalue]");
         }
     }
     catch (Exception e)
     {
         Console.Write(e.Message);
     }
 }
Beispiel #2
0
 static void Main(string [] args)
 {
     try
     {
         bool reporterror = true;
         if (args.Length >= 2)
         {
             pcmdsupport.ParseDBCMDLine       dbcmdline       = new pcmdsupport.ParseDBCMDLine(args [0]);
             pcmdsupport.ParseAccountsCMDLine accountscmdline = new pcmdsupport.ParseAccountsCMDLine(args [1]);
             if (!dbcmdline.InError && !accountscmdline.InError)
             {
                 pshare pg = new pshare();
                 pg.Go(dbcmdline, accountscmdline, 2, args);
                 Console.Write("Finished");
                 reporterror = false;
             }
         }
         if (reporterror)
         {
             Console.WriteLine("Command Line should be:-");
             Console.Write("pgen " + pcmdsupport.ParseDBCMDLine.SupportedFormatString() + " " +
                           pcmdsupport.ParseAccountsCMDLine.SupportedFormatString() +
                           " [tickerid,sharestartprice,shareendprice,gap,selloffset,numberbuyshares,pgenfilename]");
         }
     }
     catch (Exception e)
     {
         Console.Write(e.Message);
     }
 }
Beispiel #3
0
 static void Main(string [] args)
 {
     try
     {
         bool reporterror = true;
         if (args.Length == 3)
         {
             pcmdsupport.ParseDBCMDLine       dbcmdline       = new pcmdsupport.ParseDBCMDLine(args [0]);
             pcmdsupport.ParseAccountsCMDLine accountscmdline = new pcmdsupport.ParseAccountsCMDLine(args [1]);
             if (!dbcmdline.InError && !accountscmdline.InError)
             {
                 pshare pg = new pshare();
                 reporterror = pg.Go(dbcmdline, accountscmdline, args [2]);
                 Console.Write("Finished");
             }
         }
         if (reporterror)
         {
             Console.WriteLine("Command Line should be:-");
             Console.Write("pgendel " + pcmdsupport.ParseDBCMDLine.SupportedFormatString() + " " +
                           pcmdsupport.ParseAccountsCMDLine.SupportedFormatString() +
                           " tickerid");
         }
     }
     catch (Exception e)
     {
         Console.WriteLine("Exception Thrown:-");
         Console.Write(e.Message);
     }
 }
Beispiel #4
0
        void CreatePGen(pcmdsupport.ParseAccountsCMDLine account, ShareEntry shareentry,
                        string filename,
                        params PGenEntry [] pgens)
        {
            string username = account.UserName;

            Utility.CreateDirectory(Path.GetDirectoryName(filename));

            FileStream   filestream = null;
            StreamWriter writer     = null;

            try
            {
                filestream = new FileStream(filename, FileMode.Create);
                writer     = new StreamWriter(filestream);

                writer.WriteLine("<orders>");

                foreach (PGenEntry pgenentry in pgens)
                {
                    pgenentry._shareentry._pgenfilename = filename;

                    writer.Write(" <order ");

                    WritePGenAttr(writer, "ticker", pgenentry._shareentry._ticker);
                    WritePGenAttr(writer, "tickerid", pgenentry._shareentry._tickerid);
                    WritePGenAttr(writer, "buyprice", pgenentry._buyprice);
                    WritePGenAttr(writer, "numberbuyshares", pgenentry._numbersharestobuy);
                    WritePGenAttr(writer, "sellprice", pgenentry._sellprice);
                    WritePGenAttr(writer, "numbersellshares", pgenentry._numbersharestosell);
                    WritePGenAttr(writer, "sectype", pgenentry._sectype);
                    WritePGenAttr(writer, "exchange", pgenentry._exchange);
                    WritePGenAttr(writer, "primaryexchange", pgenentry._primaryexchange);
                    WritePGenAttr(writer, "currency", pgenentry._currency);
                    WritePGenAttr(writer, "localsymbol", pgenentry._shareentry._ticker);
                    WritePGenAttr(writer, "secidtype", pgenentry._secidtype);
                    WritePGenAttr(writer, "secid", pgenentry._secid);
                    WritePGenAttr(writer, "genericticks", pgenentry._genericticks);
                    WritePGenAttr(writer, "displayname", pgenentry._shareentry._displayname);

                    writer.WriteLine("></order>");
                }

                writer.WriteLine("</orders>");
            }
            finally
            {
                if (writer != null)
                {
                    writer.Close();
                }
                if (filestream != null)
                {
                    filestream.Close();
                }
            }
        }
Beispiel #5
0
        void AddShares(pcmdsupport.ParseAccountsCMDLine account, bool updateshares)
        {
            _shareentries.Reset();

            ReadDatabase_shares(account);

            if (updateshares)
            {
                UpdateDatabase_shares(account);
            }
        }
Beispiel #6
0
        void ReadDatabase_shares(pcmdsupport.ParseAccountsCMDLine account)
        {
            /*
             *  ibrokers.shares (Table)
             *  ========================
             *  idshares: INT UNSIGNED
             *  tickerid: TEXT
             *  ticker: TEXT
             *  displayname: TEXT
             *  primaryexchange: TEXT
             *  sectype: TEXT
             *  minoi: TEXT
             *  maxoi: TEXT
             *  maxvalue: TEXT
             *  username: TEXT
             *  accountnumber: TEXT
             */
            string        username      = account.UserName;
            string        accountnumber = account.AccountNumber;
            StringBuilder sb            = new StringBuilder("select tickerid, ticker, displayname, primaryexchange, sectype, minoi, maxoi, shares.maxvalue, username, accountnumber from shares where ");

            sb.Append(" username = '******' and accountnumber = '");
            sb.Append(accountnumber);
            sb.Append("'");
            MySqlCommand mysqlcommand = new MySqlCommand(sb.ToString(), _mysqlconnection);

            mysqlcommand.CommandTimeout = dbconnection.DBConnectionManager.DATABASE_TIMEOUT;
            MySqlDataReader mysqlreader = mysqlcommand.ExecuteReader();

            while (mysqlreader.Read())
            {
                _shareentries.AddShareEntry(mysqlreader.GetString(0), mysqlreader.GetString(1), mysqlreader.GetString(2),
                                            mysqlreader.GetString(3), mysqlreader.GetString(4), mysqlreader.GetString(5),
                                            mysqlreader.GetString(6), mysqlreader.GetString(7), mysqlreader.GetString(8),
                                            mysqlreader.GetString(9));
            }
            mysqlreader.Close();
        }
Beispiel #7
0
        void Go(pcmdsupport.ParseDBCMDLine dbcmdline, pcmdsupport.ParseAccountsCMDLine accountscmdline,
                int startindex, string [] args)
        {
            try
            {
                _mysqlconnection = dbmanager.GetDBConnection(dbcmdline.UserName, dbcmdline.Pwd, dbcmdline.Schema, dbcmdline.Hostname);
                _shareentries.Reset();
                ReadDatabase_shares(accountscmdline);

                List <string> fields = Fields.CollateFields(startindex, true, args);
                if (fields == null || fields.Count == 0)
                {
                    return;
                }

                // format is [ticker,displayname,exchange,maxoi,maxvalue]
                foreach (string input in fields)
                {
                    string    newinput = input;
                    string [] splits   = newinput.Split(',');
                    if (splits.Length == 5)
                    {
                        ShareEntry shareentry = new ShareEntry(splits [0], splits [1], splits [2], "STK", "0",
                                                               splits [3], splits [4], accountscmdline.UserName,
                                                               accountscmdline.AccountNumber);
                        bool alreadyadded = _shareentries.AddShareEntry(shareentry, true);
                        Console.WriteLine((alreadyadded ? "Existing Ticker " : "New Ticker ") + shareentry._ticker + " has a ticker id of " + shareentry._tickerid);
                    }
                }

                Console.WriteLine("Updating Database");
                UpdateDatabase_shares(accountscmdline);
            }
            finally
            {
                dbmanager.CloseConnections();
            }
        }
Beispiel #8
0
        void Go(pcmdsupport.ParseDBCMDLine dbcmdline, pcmdsupport.ParseAccountsCMDLine accountscmdline, int startindex, string [] args)
        {
            try
            {
                _mysqlconnection = dbmanager.GetDBConnection(dbcmdline.UserName, dbcmdline.Pwd, dbcmdline.Schema, dbcmdline.Hostname);
                _shareentries.Reset();
                ReadDatabase_shares(accountscmdline);

                const string GENERICTICKS = "100,101,104,105,106,107,165,221,225,233,236,258";

                _usdmarket_isl    = new IBSpecifics("STK", "SMART", EXCHANGE_ISLAND, "USD", "", "", "", GENERICTICKS);
                _usdmarket_arca   = new IBSpecifics("STK", "SMART", EXCHANGE_ARCA, "USD", "", "", "", GENERICTICKS);
                _usdmarket_nyse   = new IBSpecifics("STK", "SMART", EXCHANGE_NYSE, "USD", "", "", "", GENERICTICKS);
                _cadmarket_tsx    = new IBSpecifics("STK", "SMART", EXCHANGE_TSE, "CAD", "", "", "", GENERICTICKS);
                _cadmarket_ven    = new IBSpecifics("STK", "SMART", EXCHANGE_VENTURE, "CAD", "", "", "", GENERICTICKS);
                _gbpmarket        = new IBSpecifics("STK", "SMART", EXCHANGE_LONDON, "GBP", "", "", "", GENERICTICKS);
                _usdmarket_nasdaq = new IBSpecifics("STK", "SMART", EXCHANGE_NASDAQ, "USD", "", "", "", GENERICTICKS);

                Dictionary <string, bool> _donetickers = new Dictionary <string, bool> ();
                List <string>             fields       = Fields.CollateFields(startindex, true, args);
                if (fields == null || fields.Count == 0)
                {
                    return;
                }

                // format is [tickerid,sharestartprice,shareendprice,gap,selloffset,numberbuyshares,pgenfilename] or [delete]
                bool deletepgenfields = false;
                foreach (string input in fields)
                {
                    string [] splits = input.Split(',');
                    if (splits.Length == 7)
                    {
                        #region "pgen"
                        string tickerid = splits [0];
                        if (!_donetickers.ContainsKey(tickerid))
                        {
                            List <string> allfields = new List <string> ();
                            _donetickers [tickerid] = true;
                            for (int i = 0; i < fields.Count; i++)
                            {
                                splits = fields [i].Split(',');
                                if (splits.Length == 7)
                                {
                                    if (splits [0] == tickerid)
                                    {
                                        allfields.Add(fields [i]);
                                    }
                                }
                            }
                            ShareEntry shareentry;
                            if (_shareentries._shareentries.TryGetValue(tickerid, out shareentry))
                            {
                                string xmlfile = null;
                                List <PGenEntry []> pgenentries = new List <PGenEntry []> ();
                                foreach (string allfield in allfields)
                                {
                                    try
                                    {
                                        splits = allfield.Split(',');
                                        IBSpecifics ibspecs = null;
                                        if (String.Compare(shareentry._primaryexchange, EXCHANGE_ISLAND, true) == 0)
                                        {
                                            ibspecs = _usdmarket_isl;
                                        }
                                        else if (String.Compare(shareentry._primaryexchange, EXCHANGE_ARCA, true) == 0)
                                        {
                                            ibspecs = _usdmarket_arca;
                                        }
                                        else if (String.Compare(shareentry._primaryexchange, EXCHANGE_NYSE, true) == 0)
                                        {
                                            ibspecs = _usdmarket_nyse;
                                        }
                                        else if (String.Compare(shareentry._primaryexchange, EXCHANGE_TSE, true) == 0)
                                        {
                                            ibspecs = _cadmarket_tsx;
                                        }
                                        else if (String.Compare(shareentry._primaryexchange, EXCHANGE_VENTURE, true) == 0)
                                        {
                                            ibspecs = _cadmarket_ven;
                                        }
                                        else if (String.Compare(shareentry._primaryexchange, EXCHANGE_LONDON, true) == 0)
                                        {
                                            ibspecs = _gbpmarket;
                                        }
                                        else if (String.Compare(shareentry._primaryexchange, EXCHANGE_NASDAQ, true) == 0)
                                        {
                                            ibspecs = _usdmarket_nasdaq;
                                        }
                                        if (ibspecs != null)
                                        {
                                            decimal      lower        = decimal.Parse(splits [1]);
                                            decimal      upper        = decimal.Parse(splits [2]);
                                            decimal      gap          = decimal.Parse(splits [3]);
                                            decimal      offset       = decimal.Parse(splits [4]);
                                            int          numbershares = int.Parse(splits [5]);
                                            PGenEntry [] pgenentry    = CreatePGenSetRange(shareentry, lower, upper, gap, offset, numbershares, numbershares, ibspecs);
                                            if (pgenentry != null)
                                            {
                                                pgenentries.Add(pgenentry);
                                                if (xmlfile == null)
                                                {
                                                    xmlfile = splits [6];
                                                }
                                            }
                                        }
                                    }
                                    catch (Exception)
                                    {
                                    }
                                }
                                if (xmlfile != null)
                                {
                                    shareentry._pgenfilename = xmlfile;
                                    CreatePGen(accountscmdline, shareentry, xmlfile,
                                               MergePGenSets(pgenentries.ToArray())
                                               );
                                }
                            }
                        }
                        #endregion
                    }
                    else if (splits.Length == 1)
                    {
                        #region "delete flag?"
                        if (string.Compare(splits [0], "delete", true) == 0)
                        {
                            deletepgenfields = true;
                        }
                        #endregion
                    }
                }

                UpdateDatabase_pgens(accountscmdline, deletepgenfields);
            }
            finally
            {
                dbmanager.CloseConnections();
            }
        }
Beispiel #9
0
        void UpdateDatabase_pgens(pcmdsupport.ParseAccountsCMDLine account, bool deletepgenfields)
        {
            /*
             *  ibrokers.pgens (Table)
             *  =======================
             *  idpgens: INT UNSIGNED
             *  pgenxml: TEXT
             *  username: TEXT
             *  accountnumber: TEXT
             */
            MySqlTransaction mysqltransaction = null;
            MySqlCommand     mysqlcommand     = null;

            try
            {
                string username      = account.UserName;
                string accountnumber = account.AccountNumber;

                List <string> existingpgensxmls = new List <string> ();
                if (!deletepgenfields)
                {
                    StringBuilder sb = new StringBuilder("select pgenxml from pgens where ");
                    sb.Append(" username = '******' and accountnumber = '");
                    sb.Append(accountnumber);
                    sb.Append("'");
                    mysqlcommand = new MySqlCommand(sb.ToString(), _mysqlconnection);
                    mysqlcommand.CommandTimeout = dbconnection.DBConnectionManager.DATABASE_TIMEOUT;
                    MySqlDataReader mysqlreader = mysqlcommand.ExecuteReader();
                    while (mysqlreader.Read())
                    {
                        string pgenxml = mysqlreader.GetString(0);
                        existingpgensxmls.Add(pgenxml);
                    }
                    mysqlreader.Close();
                }

                mysqltransaction         = _mysqlconnection.TheDBConnection.BeginTransaction();
                mysqlcommand             = _mysqlconnection.TheDBConnection.CreateCommand();
                mysqlcommand.Connection  = _mysqlconnection.TheDBConnection;
                mysqlcommand.Transaction = mysqltransaction;

                if (deletepgenfields)
                {
                    mysqlcommand.CommandText = "delete from pgens";
                    mysqlcommand.Parameters.Clear();
                    mysqlcommand.ExecuteNonQuery();
                }

                foreach (ShareEntry entry in _shareentries._linearshareentries)
                {
                    if (entry._pgenfilename != null)
                    {
                        bool matchfound = false;
                        foreach (string searchfile in existingpgensxmls)
                        {
                            if (CompareFileName(entry._pgenfilename, searchfile))
                            {
                                matchfound = true;
                                break;
                            }
                        }
                        if (!matchfound)
                        {
                            mysqlcommand.CommandText = "insert into pgens (pgenxml, username, accountnumber) VALUES (?field1, ?field2, ?field3)";
                            mysqlcommand.Parameters.Clear();
                            mysqlcommand.Parameters.AddWithValue("?field1", entry._pgenfilename);
                            mysqlcommand.Parameters.AddWithValue("?field2", username);
                            mysqlcommand.Parameters.AddWithValue("?field3", accountnumber);
                            mysqlcommand.ExecuteNonQuery();
                        }
                    }
                }

                mysqltransaction.Commit();
            }
            catch (Exception e)
            {
                try
                {
                    if (mysqltransaction != null)
                    {
                        mysqltransaction.Rollback();
                    }
                }
                catch
                {
                }
                if (e is System.Threading.ThreadAbortException)
                {
                    throw;
                }
            }
        }
Beispiel #10
0
        void UpdateDatabase_shares(pcmdsupport.ParseAccountsCMDLine account)
        {
            /*
             *  ibrokers.shares (Table)
             *  ========================
             *  idshares: INT UNSIGNED
             *  tickerid: TEXT
             *  ticker: TEXT
             *  displayname: TEXT
             *  primaryexchange: TEXT
             *  sectype: TEXT
             *  minoi: TEXT
             *  maxoi: TEXT
             *  maxvalue: TEXT
             *  username: TEXT
             *  accountnumber: TEXT
             */
            if (_shareentries._newshareentries.Count > 0)
            {
                MySqlTransaction mysqltransaction = null;
                MySqlCommand     mysqlcommand     = null;
                try
                {
                    mysqltransaction         = _mysqlconnection.TheDBConnection.BeginTransaction();
                    mysqlcommand             = _mysqlconnection.TheDBConnection.CreateCommand();
                    mysqlcommand.Connection  = _mysqlconnection.TheDBConnection;
                    mysqlcommand.Transaction = mysqltransaction;

                    foreach (ShareEntry entry in _shareentries._newshareentries)
                    {
                        mysqlcommand.CommandText = "insert into shares (tickerid, ticker, displayname, primaryexchange, sectype, minoi, maxoi, shares.maxvalue, username, accountnumber) VALUES (?field1, ?field2, ?field3, ?field4, ?field5, ?field6, ?field7, ?field8, ?field9, ?field10)";
                        mysqlcommand.Parameters.Clear();
                        mysqlcommand.Parameters.AddWithValue("?field1", entry._tickerid);
                        mysqlcommand.Parameters.AddWithValue("?field2", entry._ticker);
                        mysqlcommand.Parameters.AddWithValue("?field3", entry._displayname);
                        mysqlcommand.Parameters.AddWithValue("?field4", entry._primaryexchange);
                        mysqlcommand.Parameters.AddWithValue("?field5", entry._sectype);
                        mysqlcommand.Parameters.AddWithValue("?field6", entry._minoi);
                        mysqlcommand.Parameters.AddWithValue("?field7", entry._maxoi);
                        mysqlcommand.Parameters.AddWithValue("?field8", entry._maxvalue);
                        mysqlcommand.Parameters.AddWithValue("?field9", entry._username);
                        mysqlcommand.Parameters.AddWithValue("?field10", entry._accountnumber);
                        mysqlcommand.ExecuteNonQuery();
                    }

                    mysqltransaction.Commit();
                }
                catch (Exception e)
                {
                    try
                    {
                        if (mysqltransaction != null)
                        {
                            mysqltransaction.Rollback();
                        }
                    }
                    catch
                    {
                    }
                    if (e is System.Threading.ThreadAbortException)
                    {
                        throw;
                    }
                }
            }
        }
Beispiel #11
0
        bool Go(pcmdsupport.ParseDBCMDLine dbcmdline, pcmdsupport.ParseAccountsCMDLine accountscmdline, string tickeridstr)
        {
            try
            {
                int tickerid;
                if (!int.TryParse(tickeridstr, out tickerid))
                {
                    return(true);
                }

                _mysqlconnection = dbmanager.GetDBConnection(dbcmdline.UserName, dbcmdline.Pwd, dbcmdline.Schema, dbcmdline.Hostname);

                /*
                 *  ibrokers.deals (Table)
                 *  =======================
                 *  iddeals             int(10) unsigned PK
                 *  tickerid            int(11)
                 *  ticker              text
                 *  oi                  int(11)
                 *  maxoi               int(11)
                 *  buyprice            double
                 *  numberbuyshares     int(11)
                 *  sellprice           double
                 *  numbersellshares    int(11)
                 *  sectype             text
                 *  exchange            text
                 *  primaryexchange     text
                 *  currency            text
                 *  localsymbol         text
                 *  secidtype           text
                 *  secid               text
                 *  genericticks        text
                 *  displayname         text
                 *  username            text
                 *  accountnumber       text
                 *  hitcount            int(11)
                 *  lastavgfillbuyprice double
                 *  lastbuycommission   double
                 *  lastbuydate         datetime
                 *  updatetimestamp     timestamp
                 */
                string        username      = accountscmdline.UserName;
                string        accountnumber = accountscmdline.AccountNumber;
                StringBuilder sb            = new StringBuilder("select iddeals,tickerid,ticker,oi from deals where tickerid = ");
                sb.Append(tickerid.ToString());
                sb.Append(" and username = '******' and accountnumber = '");
                sb.Append(accountnumber);
                sb.Append("'");
                MySqlCommand mysqlcommand = new MySqlCommand(sb.ToString(), _mysqlconnection);
                mysqlcommand.CommandTimeout = dbconnection.DBConnectionManager.DATABASE_TIMEOUT;
                MySqlDataReader mysqlreader      = mysqlcommand.ExecuteReader();
                int             numberofrows     = 0;
                List <ulong>    rowswith_open_oi = new List <ulong> ();
                string          assumed_ticker   = null;
                while (mysqlreader.Read())
                {
                    ulong  iddeals     = mysqlreader.GetUInt64(0);
                    int    rowtickerid = mysqlreader.GetInt32(1);
                    string rowticker   = mysqlreader.GetString(2);
                    int    rowoi       = mysqlreader.GetInt32(3);
                    if (rowoi > 0)
                    {
                        rowswith_open_oi.Add(iddeals);
                    }
                    if (numberofrows == 0)
                    {
                        assumed_ticker = rowticker;
                    }
                    numberofrows++;
                }
                mysqlreader.Close();
                if (rowswith_open_oi.Count > 0)
                {
                    Console.WriteLine(assumed_ticker + " has " + rowswith_open_oi.Count.ToString() + " rows with Open Interest");
                }
                Console.WriteLine("Do you want to delete ticker " + assumed_ticker + "?");
                string reply = Console.ReadLine();
                if (reply == "y" || reply == "Y")
                {
                    Console.WriteLine("Answer is Yes, deleting...");

                    MySqlTransaction mysqltransaction = null;
                    mysqlcommand = null;
                    try
                    {
                        mysqltransaction         = _mysqlconnection.TheDBConnection.BeginTransaction();
                        mysqlcommand             = _mysqlconnection.TheDBConnection.CreateCommand();
                        mysqlcommand.Connection  = _mysqlconnection.TheDBConnection;
                        mysqlcommand.Transaction = mysqltransaction;

                        sb = new StringBuilder("delete from deals where tickerid = ");
                        sb.Append(tickerid.ToString());
                        sb.Append(" and username = '******' and accountnumber = '");
                        sb.Append(accountnumber);
                        sb.Append("'");

                        mysqlcommand.CommandText = sb.ToString();
                        mysqlcommand.Parameters.Clear();
                        mysqlcommand.ExecuteNonQuery();

                        mysqltransaction.Commit();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Delete failed with " + e.Message);
                        try
                        {
                            if (mysqltransaction != null)
                            {
                                mysqltransaction.Rollback();
                            }
                        }
                        catch
                        {
                        }
                        if (e is System.Threading.ThreadAbortException)
                        {
                            throw;
                        }
                    }
                    Console.WriteLine("Deleted");
                }
                else
                {
                    Console.WriteLine("Answer is NO");
                }
            }
            finally
            {
                dbmanager.CloseConnections();
            }
            return(false);
        }