示例#1
0
        private static bool ConstructTaxlotsFile(CommandLineParameters parms)
        {
            bool bResult = false;

            try
            {
                if (parms.AccountId < 0)
                {
                    s_logger.Error("Cannot construct taxlots file without specifying an acctid", null);
                }
                else
                {
                    ReconciliationLib.Utilities.ConstructTaxlots((short)parms.AccountId);
                    bResult = true;

                    if (s_logger != null)
                    {
                        s_logger.Info(String.Format("Constructed taxlots file for acctid {0}", parms.AccountId));
                    }
                }
            }
            catch (Exception ex)
            {
                s_lastErrorMessage = ex.Message;
                if (s_logger != null)
                {
                    s_logger.Error("Error constructing taxlots file", ex);
                }
                bResult = false;
            }

            return(bResult);
        }
示例#2
0
 private static void EndTask(CommandLineParameters parms, bool succeeded)
 {
     if (succeeded)
     {
         s_taskUtilities.TaskCompleted(parms.TaskName, s_completionMessage);
     }
     else
     {
         s_taskUtilities.TaskFailed(parms.TaskName, s_lastErrorMessage);
     }
 }
示例#3
0
 // returns 0 if task is successfully started
 // returns 1 if we should not start the task because it should not be run today (because this task should not run on a holiday, for example)
 // returns 4 if we cannot find the task name
 // returns some number higher than 4 on an unexpected failure
 private static int StartTask(CommandLineParameters parms)
 {
     if (String.IsNullOrEmpty(parms.TaskName))
     {
         return(4);
     }
     else
     {
         return(s_taskUtilities.TaskStarted(parms.TaskName, ReconciliationLib.Utilities.ImportDate));
     }
 }
示例#4
0
        static void Main(string[] args)
        {
            CommandLineParameters parms = new CommandLineParameters();

            try
            {
                // initialize log4net via app.config
                log4net.Config.XmlConfigurator.Configure();
                s_logger = LogManager.GetLogger(typeof(Program));

                if (Utility.ParseCommandLineArguments(args, parms))
                {
                    if (Run(parms))
                    {
                        System.Diagnostics.EventLog.WriteEntry(AppDomain.CurrentDomain.FriendlyName.Split('.')[0], "Gargoyle Reconciliation Console completed", System.Diagnostics.EventLogEntryType.Information);
                    }
                    else
                    {
                        System.Diagnostics.EventLog.WriteEntry(AppDomain.CurrentDomain.FriendlyName.Split('.')[0], "Gargoyle Reconciliation Console failed - see error log", System.Diagnostics.EventLogEntryType.Error);
                    }
                }
                else
                {
                    // display usage message
                    string errorMessage = Utility.CommandLineArgumentsUsage(typeof(CommandLineParameters));

                    Console.WriteLine(errorMessage);
                    System.Diagnostics.EventLog.WriteEntry(AppDomain.CurrentDomain.FriendlyName.Split('.')[0], errorMessage, System.Diagnostics.EventLogEntryType.Error);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                System.Diagnostics.EventLog.WriteEntry(AppDomain.CurrentDomain.FriendlyName.Split('.')[0], ex.Source + ": " + ex.ToString(), System.Diagnostics.EventLogEntryType.Error);
            }
            finally
            {
                ReconciliationLib.Utilities.Dispose();
            }
        }
示例#5
0
        private static bool ConsolidateTrades(CommandLineParameters parms, bool consolidateOptions, bool consolidateStocks)
        {
            bool bResult = false;

            try
            {
                if (String.IsNullOrEmpty(parms.AccountGroupName))
                {
                    s_logger.Error("Cannot consolidate trades without specifying an account group", null);
                }
                else
                {
                    if (consolidateOptions || consolidateStocks)
                    {
                        ReconciliationLib.Utilities.ConsolidateTrades(consolidateOptions, consolidateStocks, null);
                        bResult = true;

                        if (s_logger != null)
                        {
                            string consolidationOptions = consolidateOptions ?
                                                          (consolidateStocks ? "stock and option" : "option") : "stock";
                            s_logger.Info(String.Format("Consolidated {0} trades for {1}", consolidationOptions, parms.AccountGroupName));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                s_lastErrorMessage = ex.Message;
                if (s_logger != null)
                {
                    s_logger.Error("Error consolidating trades", ex);
                }
                bResult = false;
            }

            return(bResult);
        }
示例#6
0
        private static void InitializeUtilities(CommandLineParameters parms)
        {
            ReconciliationLib.Utilities.Init();

            // wire events
            ReconciliationLib.Utilities.OnError += new EventHandler <ErrorEventArgs>(Utilities_OnError);
            ReconciliationLib.Utilities.OnInfo  += new EventHandler <InfoEventArgs>(Utilities_OnInfo);
            TaskUtilities.OnError += new EventHandler <ErrorEventArgs>(Utilities_OnError);
            TaskUtilities.OnInfo  += new EventHandler <InfoEventArgs>(Utilities_OnInfo);

            // get Hugo connection
            Gargoyle.Utils.DBAccess.DBAccess dbAccess = Gargoyle.Utils.DBAccess.DBAccess.GetDBAccessOfTheCurrentUser(applicationName);
            ReconciliationLib.Utilities.Connection = dbAccess.GetConnection(databaseName);
            s_taskUtilities = new TaskUtilities(ReconciliationLib.Utilities.Connection);


            if (s_logger != null)
            {
                s_logger.Info("Connecting to " + ReconciliationLib.Utilities.Connection.ConnectionString);
            }

            // set account group
            if (!String.IsNullOrEmpty(parms.AccountGroupName))
            {
                ReconciliationLib.Utilities.AccountGroupName = parms.AccountGroupName;

                // if we have not specified a clearing house, use the one associated with the account group
                if (parms.ClearingHouse == ClearingHouse.None)
                {
                    parms.ClearingHouse = ReconciliationLib.Utilities.ClearingHouse;
                }
            }

            // set dates
            ReconciliationLib.Utilities.ImportDate   = parms.GetImportDateOverride() ?? DateTime.Today;
            ReconciliationLib.Utilities.PreviousDate = ReconciliationLib.Utilities.CalculatePreviousBusinessDay(ReconciliationLib.Utilities.ImportDate.Value);
        }
示例#7
0
        private static bool AddTrades(CommandLineParameters parms)
        {
            bool bResult = true;

            try
            {
                if (bResult)
                {
                    int nAddedOptionTrades = AddOptionTrades();
                    if ((s_logger != null) && (nAddedOptionTrades > 0))
                    {
                        s_logger.Info(String.Format("Added {0} option trade(s) to Hugo", nAddedOptionTrades));
                    }
                    int nAddedStockTrades = AddStockTrades();
                    if ((s_logger != null) && (nAddedStockTrades > 0))
                    {
                        s_logger.Info(String.Format("Added {0} stock trade(s) to Hugo", nAddedStockTrades));
                    }
                    if (CanFinalize())
                    {
                        ReconciliationLib.Utilities.FinalizeImport();
                    }
                }
            }
            catch (Exception ex)
            {
                s_lastErrorMessage = ex.Message;
                if (s_logger != null)
                {
                    s_logger.Error("Error adding trades", ex);
                }
                bResult = false;
            }

            return(bResult);
        }
示例#8
0
 private static void DoImports(CommandLineParameters parms, ref int deletedPositions, ref int insertedPositions, ref int deletedConfirmations, ref int insertedConfirmations,
                               ref int deletedTaxlots, ref int insertedTaxlots, ref int deletedBookkeeping, ref int insertedBookkeeping, ref int deletedDividends, ref int insertedDividends, ref int insertedTrades,
                               bool bDeletePositions, bool bInsertPositions, bool bDeleteConfirmations, bool bInsertConfirmations,
                               bool bDeleteTaxlots, bool bInsertTaxlots, bool bDeleteBookkeeping, bool bInsertBookkeeping, bool bDeleteDividends, bool bInsertDividends, bool bInsertTrades)
 {
     s_completionMessage = "Imported ";
     if (bDeletePositions)
     {
         deletedPositions = ReconciliationLib.Utilities.DeleteTodaysPositions(parms.ClearingHouse);
     }
     if (bInsertPositions)
     {
         s_softFailOnFileNotFound = IsFullyPaidLendingAccount(parms.PositionFileName, parms.ClearingHouse) || IsFuturesAccount(parms.PositionFileName, parms.ClearingHouse);
         insertedPositions        = ReconciliationLib.Utilities.ImportPositions(parms.Directory + "\\" + parms.PositionFileName, parms.ClearingHouse);
         s_completionMessage     += String.Format("{0} positions ", insertedPositions);
     }
     if (bDeleteConfirmations)
     {
         deletedConfirmations = ReconciliationLib.Utilities.DeleteTodaysConfirmations(parms.ClearingHouse);
     }
     if (bInsertConfirmations)
     {
         s_softFailOnFileNotFound = true;
         insertedConfirmations    = ReconciliationLib.Utilities.ImportConfirmations(parms.Directory + "\\" + parms.ConfirmationFileName, parms.ClearingHouse);
         s_completionMessage     += String.Format("{0} confirmations ", insertedConfirmations);
     }
     if (bDeleteTaxlots)
     {
         deletedTaxlots = ReconciliationLib.Utilities.DeleteTodaysTaxlots(parms.ClearingHouse);
     }
     if (bInsertTaxlots)
     {
         s_softFailOnFileNotFound = false;
         insertedTaxlots          = ReconciliationLib.Utilities.ImportTaxlots(parms.Directory + "\\" + parms.TaxlotsFileName, parms.ClearingHouse);
         s_completionMessage     += String.Format("{0} taxlots ", insertedTaxlots);
     }
     if (bDeleteDividends)
     {
         deletedDividends = ReconciliationLib.Utilities.DeleteTodaysDividends(parms.ClearingHouse);
     }
     if (bInsertDividends)
     {
         s_softFailOnFileNotFound = true;
         //              s_softFailOnFileNotFound = IsFullyPaidLendingAccount(parms.DividendsFileName, parms.ClearingHouse);
         insertedDividends    = ReconciliationLib.Utilities.ImportDividends(parms.Directory + "\\" + parms.DividendsFileName, parms.ClearingHouse);
         s_completionMessage += String.Format("{0} dividends ", insertedDividends);
     }
     if (bDeleteBookkeeping)
     {
         deletedBookkeeping = ReconciliationLib.Utilities.DeleteTodaysBookkeeping(parms.ClearingHouse);
     }
     if (bInsertBookkeeping)
     {
         s_softFailOnFileNotFound = false;
         insertedBookkeeping      = ReconciliationLib.Utilities.ImportBookkeeping(parms.Directory + "\\" + parms.BookkeepingFileName, parms.ClearingHouse);
         s_completionMessage     += String.Format("{0} bookkeeping records ", insertedBookkeeping);
     }
     if (bInsertTrades)
     {
         s_softFailOnFileNotFound = false;
         int?numberTradesInserted = 0;
         int?numberTradesRejected = 0;
         ReconciliationLib.Utilities.ImportTrades(parms.Directory + "\\" + parms.TradeFileName, parms.ClearingHouse, ref numberTradesInserted, ref numberTradesRejected);
         insertedTrades       = numberTradesInserted.Value;
         s_completionMessage += String.Format("{0} trades  ", insertedTrades);
         if (numberTradesRejected.Value > 0)
         {
             s_completionMessage += String.Format("(rejected {0})  ", numberTradesRejected.Value);
         }
     }
     if (s_completionMessage.Length <= 9)
     {
         s_completionMessage += "0 records ";
     }
     if (!String.IsNullOrEmpty(s_lastErrorMessage))
     {
         s_completionMessage = s_lastErrorMessage + "; " + s_completionMessage;
     }
 }
示例#9
0
        private static bool ImportFiles(CommandLineParameters parms)
        {
            int  deletedPositions      = 0;
            int  insertedPositions     = 0;
            int  deletedConfirmations  = 0;
            int  insertedConfirmations = 0;
            int  deletedTaxlots        = 0;
            int  insertedTaxlots       = 0;
            int  deletedBookkeeping    = 0;
            int  insertedBookkeeping   = 0;
            int  deletedDividends      = 0;
            int  insertedDividends     = 0;
            int  insertedTrades        = 0;
            bool bResult = true;

            try
            {
                // see if we have already done any imports today
                int numPositions, numConfirmations, numTaxlots, numBookkeepingEntries, numDividends;
                ReconciliationLib.Utilities.CheckImportState(out numPositions, out numConfirmations, out numTaxlots, out numBookkeepingEntries, out numDividends, parms.ClearingHouse);

                // decide what tasks we need to do based on state and user input
                bool bDeletePositions     = false;
                bool bInsertPositions     = !String.IsNullOrEmpty(parms.PositionFileName);
                bool bDeleteConfirmations = false;
                bool bInsertConfirmations = !String.IsNullOrEmpty(parms.ConfirmationFileName);
                bool bDeleteTaxlots       = false;
                bool bInsertTaxlots       = !String.IsNullOrEmpty(parms.TaxlotsFileName);
                bool bDeleteDividends     = false;
                bool bInsertDividends     = !String.IsNullOrEmpty(parms.DividendsFileName);
                bool bInsertTrades        = !String.IsNullOrEmpty(parms.TradeFileName);
                bool bDeleteBookkeeping   = false;
                bool bInsertBookkeeping   = !String.IsNullOrEmpty(parms.BookkeepingFileName);

                if (bInsertPositions && (numPositions != 0))
                {
                    HandleImportState(parms.Overwrite, ref bDeletePositions, ref bInsertPositions, "position");
                }
                if (bInsertConfirmations && (numConfirmations != 0))
                {
                    HandleImportState(parms.Overwrite, ref bDeleteConfirmations, ref bInsertConfirmations, "confirmation");
                }
                if (bInsertTaxlots && (numTaxlots != 0))
                {
                    HandleImportState(parms.Overwrite, ref bDeleteTaxlots, ref bInsertTaxlots, "taxlots");
                }
                if (bInsertBookkeeping && (numBookkeepingEntries != 0))
                {
                    HandleImportState(parms.Overwrite, ref bDeleteBookkeeping, ref bInsertBookkeeping, "bookkeeping");
                }
                if (bInsertDividends && (numDividends != 0))
                {
                    HandleImportState(parms.Overwrite, ref bDeleteDividends, ref bInsertDividends, "dividend");
                }

                // do tasks we have decided upon
                DoImports(parms, ref deletedPositions, ref insertedPositions, ref deletedConfirmations, ref insertedConfirmations,
                          ref deletedTaxlots, ref insertedTaxlots, ref deletedBookkeeping, ref insertedBookkeeping, ref deletedDividends, ref insertedDividends, ref insertedTrades,
                          bDeletePositions, bInsertPositions, bDeleteConfirmations, bInsertConfirmations,
                          bDeleteTaxlots, bInsertTaxlots, bDeleteBookkeeping, bInsertBookkeeping, bDeleteDividends, bInsertDividends, bInsertTrades);

                ReconciliationLib.Utilities.FixOptionIds(parms.ClearingHouse.ToString());
                //           ReconciliationLib.Utilities.SyncTotalCosts();
            }
            catch (Exception ex)
            {
                s_lastErrorMessage = ex.Message;
                if (s_logger != null)
                {
                    s_logger.Error("Error during import", ex);
                }
                bResult = false;
            }

            ShowImportResults(deletedPositions, insertedPositions, deletedConfirmations, insertedConfirmations,
                              deletedTaxlots, insertedTaxlots, deletedDividends, insertedDividends, deletedBookkeeping, insertedBookkeeping, insertedTrades);
            return(bResult);
        }
示例#10
0
        private static bool Run(CommandLineParameters parms)
        {
            bool bTaskStarted = false;
            bool bReturn      = false;

            try
            {
                InitializeUtilities(parms);

                int rc = StartTask(parms);
                if (rc == 1)
                {
                    return(true);    // task violates scheduling rules and should not be run
                }
                if (rc == 0)
                {
                    bTaskStarted = true;  // task was marked as started and must be marked as completed or failed on exit
                }
                switch (parms.Action)
                {
                case ReconciliationAction.Import:
                    bReturn = ImportFiles(parms);
                    break;

                case ReconciliationAction.AddTrades:
                    bReturn = AddTrades(parms);
                    break;

                case ReconciliationAction.ConsolidateOptionTrades:
                    bReturn = ConsolidateTrades(parms, true, false);
                    break;

                case ReconciliationAction.ConsolidateStockTrades:
                    bReturn = ConsolidateTrades(parms, false, true);
                    break;

                case ReconciliationAction.ConstructTaxlotsFile:
                    bReturn = ConstructTaxlotsFile(parms);
                    break;

                default:
                    if (s_logger != null)
                    {
                        s_logger.Error("Invalid action: " + parms.Action.ToString());
                    }
                    bReturn = false;
                    break;
                }
            }
            catch (Exception ex)
            {
                s_lastErrorMessage = ex.Message;
                if (s_logger != null)
                {
                    s_logger.Error("Error: ", ex);
                }
                bReturn = false;
            }
            finally
            {
                // record task status
                if (bTaskStarted)
                {
                    EndTask(parms, bReturn);
                }
            }
            return(bReturn);
        }