250624 Damir

From MillerSql.com

Hi Damir, here is a .zip file containing the Visual Studio 2017 C# stock trading project I named "Raccoon"

https://millersql.com/Pages/WingVista/WingVistaImages/fileshare/250624_Raccoon.zip

Below is the contents of the main .cs source file MouthRaccoon.cs:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using IBApi;
using System.Threading;
using System.Threading.Tasks;
using System.Collections;


//using System.Diagnostics;     //used by Stopwatch

namespace nsMouthRaccoon
{
    public class cMouthRaccoon
    {
        public static String sMouthEnvironmentProOrDev = ConfigurationManager.AppSettings["sEnvironmentProOrDev"];
        public static String sMouthBrokerTwsOrIbGateway = ConfigurationManager.AppSettings["sBrokerTwsOrIbGateway"];

        public static String sMouthWhatTypeOfLogging = ConfigurationManager.AppSettings["sMouthWhatTypeOfLogging"];
        public static String sMouthMessagePriorityToAction = ConfigurationManager.AppSettings["sMouthMessagePriorityToAction"];
        public static String sMouthBrokerIPAddress = ConfigurationManager.AppSettings["sMouthBrokerIPAddress"];

        public static String sMouthBrokerIPPortTwsDev = ConfigurationManager.AppSettings["sBrokerIPPortTwsDev"];
        public static String sMouthBrokerIPPortTwsPro = ConfigurationManager.AppSettings["sBrokerIPPortTwsPro"];
        public static String sMouthBrokerIPPortIbGatewayDev = ConfigurationManager.AppSettings["sBrokerIPPortIbGatewayDev"];
        public static String sMouthBrokerIPPortIbGatewayPro = ConfigurationManager.AppSettings["sBrokerIPPortIbGatewayPro"];

        //public static String sMouthSqlMainServerIpAddress = ConfigurationManager.AppSettings["sMouthSqlMainServerIpAddress"];
        //public static String sMouthSqlMainServerInstanceName = ConfigurationManager.AppSettings["sMouthSqlMainServerInstanceName"];
        //public static String sMouthSqlMainServerDatabaseNamePrefix = ConfigurationManager.AppSettings["sMouthSqlMainServerDatabaseNamePrefix"];
        //public static String sMouthSqlMainServerLoginPrefix = ConfigurationManager.AppSettings["sMouthSqlMainServerLoginPrefix"];
        //public static String sMouthSqlMainServerWidget = ConfigurationManager.AppSettings["sMouthSqlMainServerWidget"];
        //public static String sMouthSqlTurnstileServerIpAddress = ConfigurationManager.AppSettings["sMouthSqlTurnstileServerIpAddress"];
        //public static String sMouthSqlTurnstileServerInstanceName = ConfigurationManager.AppSettings["sMouthSqlTurnstileServerInstanceName"];
        //public static String sMouthSqlTurnstileServerDatabaseNamePrefix = ConfigurationManager.AppSettings["sMouthSqlTurnstileServerDatabaseNamePrefix"];
        //public static String sMouthSqlTurnstileServerLoginPrefix = ConfigurationManager.AppSettings["sMouthSqlTurnstileServerLoginPrefix"];
        //public static String sMouthSqlTurnstileServerWidget = ConfigurationManager.AppSettings["sMouthSqlTurnstileServerWidget"];

        static nsEarsRaccoon.cEWrapperImpl cvMouthRaccoonEWrapper = new nsEarsRaccoon.cEWrapperImpl();

        static EClientSocket cvMouthRaccoonClientSocket = cvMouthRaccoonEWrapper.fEarsEClientSocket;     //note this "f" is NOT for "float"
        static EReaderSignal cvMouthRaccoonReaderSignal = cvMouthRaccoonEWrapper.cvEarsSignal;   //note this second "cv" is NOT for "class variable".
        
        public static DateTime dtMouthUtcNow = DateTime.UtcNow;
        public static Int32 iMouthMessagePriorityToAction = Convert.ToInt32(sMouthMessagePriorityToAction);
        public static String sMouthRaccoonValueToReturn;
        public static String sMouthRaccoonReturnValueReceived;
        public static String sMouthRaccoonMessageString;
        public static String sMouthCrLf = "\n                                                 ";

        public static String sMouthLogFile = "-1";
        

        public static String mMouthEpisode(
            String sMouthEpisodeParameter1
            , String sMouthEpisodeParameter2
            , String sMouthEpisodeParameter3
            , String sMouthEpisodeParameter4
            )
        {
            String sProgrammeName = sMouthEpisodeParameter1;    //FatFinger
            String sProgrammeArgumentAlpha = sMouthEpisodeParameter2;     // "4";
            String sProgrammeSection; //not supplied as an external parameter

            String sHoldTimeBeforeGettingPricesSeconds = "-1";
            String sHoldTimeBeforeClosingOrdersSeconds = "-1";
            String sHoldTimeBeforeClosingTradesSeconds = "-1";
            Int32 iHoldTimeBeforeGettingPricesSeconds = -1;
            String sOrdersPlaceYesOrNo = "-1";
            String sPriceFieldId = "-1";
            String sOrdersAction = "-1";
            String sSequenceNumberMax = "-1";
            String sLotValueDollarsTarget = "-1";
            String sProgrammeEnabledYesOrNo = "-1";
            String sEpisodeId = "-1";
            String sEpisodeRunStatus;
            String sPositionOpenYesOrNo = "-1";
            String sWhichOrders = "-1";
            String sTradingPhase = "-1";    //Opening, Closing
            String sWaitForWhatNetPosition = "-1";
            String sAllOrNone = "-1";
            String sOrderIdToSkip = "0";
            String sCalledFromThread = "Mouth";
            String sOrderId = "-1";
            String sPriceChangeIntervalSeconds = "-1";


            //Start up Mouth SQL Server connection first:
            sMouthRaccoonReturnValueReceived = nsMouthSqlRaccoon.cMouthSqlRaccoon.mMouthSqlMainRaccoonConnect();

            //Next start up Mouth PriceReader SQL Server connection first:
            sMouthRaccoonReturnValueReceived = nsMouthSqlRaccoon.cMouthSqlRaccoon.mMouthSqlPriceReaderRaccoonConnect();

            sProgrammeSection = "";

            if (sMouthEpisodeParameter1 == "GetHistoricalPrices")
            {

                sMouthRaccoonReturnValueReceived = cMouthRaccoon.mMouthBrokerConnectionEnvironmentStartup();

                sMouthRaccoonReturnValueReceived = nsMouthRaccoon.cMouthRaccoon.mMouthGetHistoricalPrices(
                      sProgrammeName
                    , sProgrammeArgumentAlpha
                    , sProgrammeSection
                    );

                //Thread.Sleep(10000);
                
                sMouthRaccoonReturnValueReceived = cMouthRaccoon.mMouthBrokerConnectionEnvironmentShutdown();

                sMouthRaccoonMessageString = "mMouthEpisode: BrokerConnectionEnvironment Shutdown complete, with returncode:: "
                    + sMouthRaccoonReturnValueReceived
                    + sMouthCrLf + " Raccoon Application continues to run.";
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                sMouthRaccoonReturnValueReceived = nsMouthSqlRaccoon.cMouthSqlRaccoon.mMouthSqlMainRaccoonDisConnect();

                sMouthRaccoonValueToReturn = "0";

                return sMouthRaccoonValueToReturn;
            }


            sProgrammeSection = "ProgrammeSection1";
            
            sMouthRaccoonReturnValueReceived = nsMouthSqlRaccoon.cMouthSqlRaccoon.mMouthSqlEpisodeDatabaseStart(
                  sProgrammeName
                , sProgrammeArgumentAlpha
                , sProgrammeSection
                , out sHoldTimeBeforeGettingPricesSeconds
                , out sHoldTimeBeforeClosingOrdersSeconds
                , out sHoldTimeBeforeClosingTradesSeconds
                , out sOrdersPlaceYesOrNo
                , out sProgrammeEnabledYesOrNo
                , out sEpisodeId
                , out sPriceFieldId
                , out sOrdersAction  //not used
                , out sSequenceNumberMax
                , out sLotValueDollarsTarget
                , out sPriceChangeIntervalSeconds
                );


            sOrdersAction = "generate";


            sMouthRaccoonMessageString = "mMouthEpisode: mMouthSqlEpisodeDatabaseStart completed, with the following parameters output: "
                + sMouthCrLf + "sHoldTimeBeforeGettingPricesSeconds: " + sHoldTimeBeforeGettingPricesSeconds
                + sMouthCrLf + "sHoldTimeBeforeClosingOrdersSeconds: " + sHoldTimeBeforeClosingOrdersSeconds
                + sMouthCrLf + "sHoldTimeBeforeClosingTradesSeconds: " + sHoldTimeBeforeClosingTradesSeconds
                + sMouthCrLf + "sOrdersPlaceYesOrNo: " + sOrdersPlaceYesOrNo
                + sMouthCrLf + "sProgrammeEnabledYesOrNo: " + sProgrammeEnabledYesOrNo
                + sMouthCrLf + "sEpisodeId: " + sEpisodeId
                + sMouthCrLf + "sPriceFieldId: " + sPriceFieldId
                + sMouthCrLf + "sSequenceNumberMax: " + sSequenceNumberMax
                + sMouthCrLf + "sPriceChangeIntervalSeconds: " + sPriceChangeIntervalSeconds
                //+ sMouthCrLf + "test: " + DateTime.UtcNow.ToLongTimeString() + " secs " +  (Convert.ToInt32(sPriceChangeIntervalSeconds)).ToString()
                //+ sMouthCrLf + "test2: " + DateTime.UtcNow.AddSeconds(Convert.ToInt32(sPriceChangeIntervalSeconds)).ToLongTimeString()
                ;
            nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");
            

            if (sEpisodeId == "running")
            {
                sMouthRaccoonMessageString = "mMouthEpisode: sEpisodeId is running, so a previous episode is still running." +
                    " Exiting this episode, without logging a new EpisodeId";
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                sMouthRaccoonReturnValueReceived = nsMouthSqlRaccoon.cMouthSqlRaccoon.mMouthSqlMainRaccoonDisConnect();

                sMouthRaccoonValueToReturn = "0";
                return sMouthRaccoonValueToReturn;
            }


            if (sEpisodeId == "weekend")
            {
                sMouthRaccoonMessageString = "mMouthEpisode: Current day is a weekend day, so the stock market is closed." +
                    " Exiting this episode, without logging a new EpisodeId";
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                sMouthRaccoonReturnValueReceived = nsMouthSqlRaccoon.cMouthSqlRaccoon.mMouthSqlMainRaccoonDisConnect();

                sMouthRaccoonValueToReturn = "0";
                return sMouthRaccoonValueToReturn;
            }


            if (sProgrammeEnabledYesOrNo == "no")
            {
                sMouthRaccoonMessageString = "mMouthEpisode: sProgrammeEnabledYesOrNo is set to no. Exiting episode.";
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                sMouthRaccoonReturnValueReceived = nsMouthSqlRaccoon.cMouthSqlRaccoon.mMouthSqlMainRaccoonDisConnect();

                sMouthRaccoonValueToReturn = "0";
                return sMouthRaccoonValueToReturn;
            }
       

            sProgrammeSection = "ProgrammeSection2";

            sMouthRaccoonReturnValueReceived = cMouthRaccoon.mMouthBrokerConnectionEnvironmentStartup();

            sMouthRaccoonMessageString = "mMouthEpisode: BrokerConnectionEnvironment Startup complete, with returncode:: "
                + sMouthRaccoonReturnValueReceived;
            nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");
            

            sProgrammeSection = "ProgrammeSection3";

            sTradingPhase = "Opening";  //Opening new positions at this part of the episode.

            //Waiting for period sHoldTimeBeforeGettingPricesSeconds before starting to request prices

            sMouthRaccoonMessageString = "mMouthEpisode: !!!! Sleeping for sHoldTimeBeforeGettingPricesSeconds before getting prices "
                + sMouthCrLf + "sHoldTimeBeforeGettingPricesSeconds: " + sHoldTimeBeforeGettingPricesSeconds
                ;
            nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

            try
            {
                iHoldTimeBeforeGettingPricesSeconds = Convert.ToInt32(sHoldTimeBeforeGettingPricesSeconds);
                Thread.Sleep(1000 * iHoldTimeBeforeGettingPricesSeconds);

                sMouthRaccoonMessageString = "mMouthEpisode: !!!! Sleep complete "
                    ;
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");
            }
            catch
            {
                sMouthRaccoonMessageString = "mMouthEpisode: !!!! CATCH statment in sleeping before getting prices "
                    + sMouthCrLf + "sHoldTimeBeforeGettingPricesSeconds: " + sHoldTimeBeforeGettingPricesSeconds
                    ;
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 1, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");
            }


            sMouthRaccoonMessageString = "mMouthEpisode: Running mMouthInstrumentPricesByProgrammeGetAndSave, with the following parameters: "
                + sMouthCrLf + "sProgrammeName: " + sProgrammeName
                + sMouthCrLf + "sProgrammeArgumentAlpha: " + sProgrammeArgumentAlpha
                + sMouthCrLf + "sProgrammeSection: " + sProgrammeSection
                ;
            nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");
            

            sMouthRaccoonReturnValueReceived = cMouthRaccoon.mMouthInstrumentPricesByProgrammeGetAndSave(
                  sProgrammeName
                , sProgrammeArgumentAlpha
                , sProgrammeSection
                , sSequenceNumberMax
                );


            sProgrammeSection = "ProgrammeSection4";
            sAllOrNone = "False";   //normally True


            if (sOrdersPlaceYesOrNo == "yes")
            {
                sMouthRaccoonMessageString = "mMouthEpisode: Running mMouthOrdersByProgrammePlace..";
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                sMouthRaccoonReturnValueReceived = cMouthRaccoon.mMouthOrdersByProgrammePlace(
                      sProgrammeName
                    , sProgrammeArgumentAlpha
                    , sProgrammeSection
                    , sEpisodeId
                    , sSequenceNumberMax
                    , sPriceFieldId
                    , sOrdersAction  //not used
                    , sLotValueDollarsTarget
                    , sTradingPhase
                    , sAllOrNone
                    );
            }
            else
            {
                sMouthRaccoonMessageString = "mMouthEpisode: Not placing orders, as sOrdersPlaceYesOrNo is not set to yes";
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");
            }

            sProgrammeSection = "ProgrammeSection5NetsCast";

            sMouthRaccoonMessageString = "mMouthEpisode: All orders placed. Now waiting for the order wait period (typically 10 minutes)" +
                " to see whether of these orders trade";
            nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");
                        
            sWaitForWhatNetPosition = "yes";

            sOrderId = "All";
            
            sMouthRaccoonReturnValueReceived = cMouthRaccoon.mMouthWaitForNetTradingPosition(
                  sProgrammeName
                , sProgrammeArgumentAlpha
                , sProgrammeSection
                , sEpisodeId
                , sHoldTimeBeforeClosingOrdersSeconds
                , sWaitForWhatNetPosition
                , out sPositionOpenYesOrNo
                , sOrderId
                , "0"   //sPriceChangeIntervalSeconds - we don't want any order price changes on the opening orders.
                , sTradingPhase
                );

            Int32 iHoldTimeBeforeClosingTradesSeconds = -1;

            iHoldTimeBeforeClosingTradesSeconds = Convert.ToInt32(sHoldTimeBeforeClosingTradesSeconds);

            sProgrammeSection = "ProgrammeSection6";
            
            sTradingPhase = "Closing";  //Now closing positions at this part of the episode.

            if (sPositionOpenYesOrNo == "no")   //if none of the open orders have traded
            {
                sMouthRaccoonMessageString = "mMouthEpisode: mMouthWaitForNetTradingPosition: "
                    + sMouthCrLf + "Completed mMouthWaitForNetTradingPosition loop,"
                    + sMouthCrLf + " for time length: " + sHoldTimeBeforeClosingOrdersSeconds
                    + sMouthCrLf + " without any trades. "
                    + sMouthCrLf + "Now moving on to close the orders and complete the episode";
                
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                sWhichOrders = "UnFilledPartialFilled";
                
                // in this case "NotTraded" will return all orders.
                // Using "NotTraded" here instead of "All" means that each trade will be marked in tOrders as "Cancelled"
                // , whereas if "All" was used, the single broker command to cancel all orders does not result in ears commands 
                // cancelling each one.

                sMouthRaccoonReturnValueReceived = cMouthRaccoon.mMouthOrdersCancel(
                    sWhichOrders
                    ,sOrderIdToSkip
                    ,sCalledFromThread
                    );

                sMouthRaccoonMessageString = "mMouthEpisode: Orders closed. Now waiting for 10 seconds for the Broker notifies "
                    + sMouthCrLf + "these closures through the ears thread, to mark them as closed in the SQL database."
                    + sMouthCrLf + "(if this is not done, they continue to show as -Submitted- in tOrdersArchive.";

                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                Thread.Sleep(5000);

                sEpisodeRunStatus = "completed";

                sMouthRaccoonReturnValueReceived = nsMouthSqlRaccoon.cMouthSqlRaccoon.mMouthSqlEpisodeDatabaseEnd(
                      sEpisodeId
                    , sEpisodeRunStatus
                    );


                sMouthRaccoonReturnValueReceived = cMouthRaccoon.mMouthBrokerConnectionEnvironmentShutdown();

                sMouthRaccoonMessageString = "mMouthEpisode: BrokerConnectionEnvironment Shutdown complete, with returncode:: "
                    + sMouthRaccoonReturnValueReceived
                    + sMouthCrLf + " Raccoon Application continues to run.";
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                sMouthRaccoonReturnValueReceived = nsMouthSqlRaccoon.cMouthSqlRaccoon.mMouthSqlMainRaccoonDisConnect();

                sMouthRaccoonValueToReturn = "0";

                return sMouthRaccoonValueToReturn;
            }

            // If the program has got this far, then it must be because there has been a trade (i.e. that after the wait, there is an open position).

            //TimeSpan tsOrdersLiveForSpan;
            //TimeSpan tsSecondsSinceLastPriceChange;

            sMouthRaccoonMessageString = "mMouthEpisode: mMouthWaitForNetTradingPosition: TRADE!"
                + sMouthCrLf + "One(or more) of the orders has traded."
                + sMouthCrLf + "(So now abandoning the wait)."
                + sMouthCrLf
                + sMouthCrLf + "Now starting the wait for the trade wait period, which is:"
                + sMouthCrLf + "sHoldTimeBeforeClosingTradesSeconds: " + sHoldTimeBeforeClosingTradesSeconds + " seconds."
                + sMouthCrLf + " to allow the market to return to normality."
                + sMouthCrLf + "(before then closing all open trade positions, and exiting the episode)"
                + sMouthCrLf + ""
                + sMouthCrLf + "This will happen at UTC time: " + DateTime.UtcNow.AddSeconds(Convert.ToInt32(sHoldTimeBeforeClosingTradesSeconds)).ToLongTimeString()
                ;

            nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

            Thread.Sleep(iHoldTimeBeforeClosingTradesSeconds * 1000);


            sProgrammeSection = "ProgrammeSection7";

            sMouthRaccoonMessageString = "mMouthEpisode: Now close the open Trade positions. ";

            nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

            sWhichOrders = "PartialFilledFullyFilled";
            sAllOrNone = "False";

            sMouthRaccoonReturnValueReceived = cMouthRaccoon.mMouthPositionsAllClose(
                sProgrammeName
                , sProgrammeArgumentAlpha
                , sProgrammeSection
                , sWhichOrders
                , sTradingPhase
                , sAllOrNone
                , sPriceChangeIntervalSeconds
                );


            sEpisodeRunStatus = "Wait 5 seconds at the end";

            Thread.Sleep(5000);
            

            sProgrammeSection = "ProgrammeSection9";

            sMouthRaccoonMessageString = "mMouthEpisode: : mMouthWaitForNetTradingPosition complete. The closing order(s) have now traded" +
                "Exiting Episode with success.";

            nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

            
            sEpisodeRunStatus = "completed";

            sMouthRaccoonReturnValueReceived = nsMouthSqlRaccoon.cMouthSqlRaccoon.mMouthSqlEpisodeDatabaseEnd(
                  sEpisodeId
                , sEpisodeRunStatus
                );
            

            sMouthRaccoonReturnValueReceived = cMouthRaccoon.mMouthBrokerConnectionEnvironmentShutdown();

            sMouthRaccoonMessageString = "mMouthEpisode: BrokerConnectionEnvironment Shutdown complete, with returncode:: "
                + sMouthRaccoonReturnValueReceived 
                + sMouthCrLf + " Raccoon Application continues to run.";
            nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

            sMouthRaccoonReturnValueReceived = nsMouthSqlRaccoon.cMouthSqlRaccoon.mMouthSqlMainRaccoonDisConnect();

            sMouthRaccoonValueToReturn = "0";

            return sMouthRaccoonValueToReturn;
        }
        


        //Get Prices group of three loop:
        public static String mMouthInstrumentPricesByProgrammeGetAndSave(
              String sProgrammeName
            , String sProgrammeArgumentAlpha
            , String sProgrammeSection
            , String sSequenceNumberMax
            )
        {
            String sSequenceNumberNow = "1";
            Int32 iSequenceNumberNow = 1;
            Int32 iSequenceNumberMax = -1;
            
            String sRejectPriceFractionGreaterThan = "0";
            String sPriceValue = "-1";
            String sPriceBidValue = "-1";
            String sPriceAskValue = "-1";
            String sPriceFieldId = "1";

            sMouthRaccoonMessageString = "mMouthInstrumentPricesByProgrammeGetAndSave: "
                + "Calling mMouthSqlInstrumentDetailsGet() with sProgrammeName = " + sProgrammeName + " ..";
            nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");
            
            String sSymbolName = "-1";
            String sSecTypeName = "-1";
            String sExchangeName = "-1";
            String sCurrencyBaseName = "-1";
            String sInstrumentId = "-1";
            String sOrderPriceOpenOffsetFraction = "-1";
            String sOrderPriceCloseOffsetFraction = "-1";
            String sTradableYesOrNo = "-1";
            String sInstrumentPrice = "-1";

            try
            {
                Int32.TryParse(sSequenceNumberMax, out iSequenceNumberMax);
            }
            catch
            {
                sMouthRaccoonMessageString = "mMouthInstrumentPricesByProgrammeGetAndSave: Error converting sSequenceNumberMax string to int. Aborting..";
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 2, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;

                return sMouthRaccoonValueToReturn;
            }


            try
            {
                sMouthRaccoonReturnValueReceived = nsMouthSqlRaccoon.cMouthSqlRaccoon.mMouthSqlInstrumentDetailsGet(
                      out sInstrumentId
                    , out sSymbolName
                    , out sSecTypeName
                    , out sExchangeName
                    , out sCurrencyBaseName
                    , out sOrderPriceOpenOffsetFraction     //not used
                    , out sOrderPriceCloseOffsetFraction     //not used
                    , out sTradableYesOrNo
                    , out sInstrumentPrice              //also not used here
                    , sProgrammeName
                    , sProgrammeArgumentAlpha
                    , sProgrammeSection
                    , sSequenceNumberNow
                    , "-1"  //OrderId, ignored in this case
                    );
            }
            catch
            {
                sMouthRaccoonMessageString = "mMouthInstrumentPricesByProgrammeGetAndSave: !!!! "
                    + sMouthCrLf + "Catch entered when calling first mMouthSqlInstrumentDetailsGet !!!!";
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 1, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;

                return sMouthRaccoonValueToReturn;
            }
            

            sMouthRaccoonMessageString = "mMouthInstrumentPricesByProgrammeGetAndSave:"
                + sMouthCrLf + "sSequenceNumberNow is: " + sSequenceNumberNow
                + sMouthCrLf + "sSequenceNumberMax is: " + sSequenceNumberMax
                + sMouthCrLf + "sInstrumentId is: " + sInstrumentId
                + sMouthCrLf + "sSymbolName is: " + sSymbolName
                + sMouthCrLf + "sSecTypeName is: " + sSecTypeName
                + sMouthCrLf + "sExchangeName is: " + sExchangeName
                + sMouthCrLf + "sCurrencyBaseName is: " + sCurrencyBaseName
                + sMouthCrLf + "sTradableYesOrNo is: " + sTradableYesOrNo
                ;
            
            nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

            sPriceValue = "-1.0";

            try
            {
                sMouthRaccoonReturnValueReceived = cMouthRaccoon.mMouthInstrumentPriceGetAndSave(
                      out sPriceValue
                    , out sPriceBidValue
                    , out sPriceAskValue
                    , sInstrumentId
                    , sSymbolName
                    , sSecTypeName
                    , sExchangeName
                    , sCurrencyBaseName
                    , sPriceFieldId
                    , sRejectPriceFractionGreaterThan
                    );
            }
            catch
            {
                sMouthRaccoonMessageString = "mMouthInstrumentPricesByProgrammeGetAndSave: !!!! "
                    + sMouthCrLf + "Catch entered when calling first mMouthInstrumentPriceGetAndSave !!!!";
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 1, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;

                return sMouthRaccoonValueToReturn;
            }
            

            iSequenceNumberNow = iSequenceNumberNow + 1;
            
            while (iSequenceNumberNow <= iSequenceNumberMax)
            {
                sSequenceNumberNow = iSequenceNumberNow.ToString();
                sTradableYesOrNo = "-1";
                sInstrumentPrice = "-1";

                try
                {
                    sMouthRaccoonReturnValueReceived = nsMouthSqlRaccoon.cMouthSqlRaccoon.mMouthSqlInstrumentDetailsGet(
                          out sInstrumentId
                        , out sSymbolName
                        , out sSecTypeName
                        , out sExchangeName
                        , out sCurrencyBaseName
                        , out sOrderPriceOpenOffsetFraction     //not used
                        , out sOrderPriceCloseOffsetFraction     //not used
                        , out sTradableYesOrNo
                        , out sInstrumentPrice              //also not used here
                        , sProgrammeName
                        , sProgrammeArgumentAlpha
                        , sProgrammeSection
                        , sSequenceNumberNow
                        , "-1"  //OrderId, ignored in this case
                        );
                }
                catch
                {
                    sMouthRaccoonMessageString = "mMouthInstrumentPricesByProgrammeGetAndSave: !!!! "
                        + sMouthCrLf + "Catch entered when calling subsequents mMouthSqlInstrumentDetailsGet !!!!";
                    nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                        , sMouthWhatTypeOfLogging, 1, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                    sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;

                    return sMouthRaccoonValueToReturn;
                }
                

                sMouthRaccoonMessageString = "mMouthInstrumentPricesByProgrammeGetAndSave:"
                    + sMouthCrLf + "iSequenceNumberNow is: " + sSequenceNumberNow
                    + sMouthCrLf + "sSequenceNumberMax is: " + sSequenceNumberMax
                    + sMouthCrLf + "sInstrumentId is: " + sInstrumentId
                    + sMouthCrLf + "sSymbolName is: " + sSymbolName
                    + sMouthCrLf + "sSecTypeName is: " + sSecTypeName
                    + sMouthCrLf + "sExchangeName is: " + sExchangeName
                    + sMouthCrLf + "sCurrencyBaseName is: " + sCurrencyBaseName
                    + sMouthCrLf + "sTradableYesOrNo is: " + sTradableYesOrNo
                    ;
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");
                
                sPriceValue = "-1.0";

                try
                {
                    sMouthRaccoonReturnValueReceived = cMouthRaccoon.mMouthInstrumentPriceGetAndSave(
                          out sPriceValue
                        , out sPriceBidValue
                        , out sPriceAskValue
                        , sInstrumentId
                        , sSymbolName
                        , sSecTypeName
                        , sExchangeName
                        , sCurrencyBaseName
                        , sPriceFieldId
                        , sRejectPriceFractionGreaterThan
                        );
                }
                catch
                {
                    sMouthRaccoonMessageString = "mMouthInstrumentPricesByProgrammeGetAndSave: !!!! "
                        + sMouthCrLf + "Catch entered when calling subsequent mMouthInstrumentPriceGetAndSave !!!!";
                    nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                        , sMouthWhatTypeOfLogging, 1, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                    sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;

                    return sMouthRaccoonValueToReturn;
                }
                

                iSequenceNumberNow = iSequenceNumberNow + 1;
            }

            sMouthRaccoonValueToReturn = "0";

            return sMouthRaccoonValueToReturn;
        }



        public static String mMouthOrdersByProgrammePlace(
              String sProgrammeName
            , String sProgrammeArgumentAlpha
            , String sProgrammeSection
            , String sEpisodeId
            , String sSequenceNumberMax
            , String sPriceFieldId
            , String sOrdersAction  //not used
            , String sLotValueDollarsTarget
            , String sTradingPhase
            , String sAllOrNone
            )
        {
            String sSequenceNumberNow = "1";
            Int32 iSequenceNumberNow = 1;
            Int32 iSequenceNumberMax;
            String sOrderType = "LMT";
            String sPositionSize = "-1";    //sPositionSize not used in mMouthOrdersByProgrammePlace
            String sOrderPriceOffsetFraction = "-1";
            String sOrderId = "-1";
            String sOrderIdCreated = "-1";

            sMouthRaccoonMessageString = "mMouthOrdersByProgrammePlace: "
                + "Calling mMouthSqlInstrumentDetailsGet() with: "
                + sMouthCrLf + " sProgrammeName = " + sProgrammeName
                + sMouthCrLf + " sProgrammeSection = " + sProgrammeSection
                + sMouthCrLf + " sEpisodeId = " + sEpisodeId
                + sMouthCrLf + " sSequenceNumberMax = " + sSequenceNumberMax
                ;
            nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

            String sSymbolName = "-1";
            String sSecTypeName = "-1";
            String sExchangeName = "-1";
            String sCurrencyBaseName = "-1";
            String sInstrumentId = "-1";
            String sOrderPriceOpenOffsetFraction = "-1";
            String sOrderPriceCloseOffsetFraction = "-1";
            String sTradableYesOrNo = "-1";
            String sInstrumentPrice = "-1";
            String sOcaGroup = "-1";

            sOcaGroup = "OCAA" + sEpisodeId;

            try
            {
                sMouthRaccoonReturnValueReceived = nsMouthSqlRaccoon.cMouthSqlRaccoon.mMouthSqlInstrumentDetailsGet(
                    out sInstrumentId
                    , out sSymbolName
                    , out sSecTypeName
                    , out sExchangeName
                    , out sCurrencyBaseName
                    , out sOrderPriceOpenOffsetFraction
                    , out sOrderPriceCloseOffsetFraction
                    , out sTradableYesOrNo
                    , out sInstrumentPrice
                    , sProgrammeName
                    , sProgrammeArgumentAlpha
                    , sProgrammeSection
                    , sSequenceNumberNow
                    , "-1"  //OrderId, ignored in this case
                    );
            }
            catch
            {
                sMouthRaccoonMessageString = "mMouthOrdersByProgrammePlace: !!!! "
                    + sMouthCrLf + "Catch entered when calling first mMouthSqlInstrumentDetailsGet !!!!";
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 1, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;

                return sMouthRaccoonValueToReturn;
            }


            try
            {
                Int32.TryParse(sSequenceNumberMax, out iSequenceNumberMax);
            }
            catch
            {
                sMouthRaccoonMessageString = "mMouthOrdersByProgrammePlace: Error converting sSequenceNumberMax string to int. Aborting..";
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 2, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;

                return sMouthRaccoonValueToReturn;
            }


            sOrderPriceOffsetFraction = "-1";

            if (sTradingPhase == "Opening")
            {
                sOrderPriceOffsetFraction = sOrderPriceOpenOffsetFraction;
            }

            if (sTradingPhase == "Closing")
            {
                sOrderPriceOffsetFraction = sOrderPriceCloseOffsetFraction;
            }
            

            sMouthRaccoonMessageString = "mMouthOrdersByProgrammePlace:"
                + sMouthCrLf + "sSequenceNumberNow is: " + sSequenceNumberNow
                + sMouthCrLf + "sSequenceNumberMax is: " + sSequenceNumberMax
                + sMouthCrLf + "sEpisodeId is: " + sEpisodeId
                + sMouthCrLf + "sOcaGroup is: " + sOcaGroup
                + sMouthCrLf + "sAllOrNone is: " + sAllOrNone
                + sMouthCrLf + "sInstrumentId is: " + sInstrumentId
                + sMouthCrLf + "sSymbolName is: " + sSymbolName
                + sMouthCrLf + "sSecTypeName is: " + sSecTypeName
                + sMouthCrLf + "sExchangeName is: " + sExchangeName
                + sMouthCrLf + "sCurrencyBaseName is: " + sCurrencyBaseName
                + sMouthCrLf + "sOrderPriceOpenOffsetFraction is: " + sOrderPriceOpenOffsetFraction
                + sMouthCrLf + "sOrderPriceCloseOffsetFraction is: " + sOrderPriceCloseOffsetFraction
                + sMouthCrLf + "sOrderPriceOffsetFraction is: " + sOrderPriceOffsetFraction
                + sMouthCrLf + "sPriceFieldId is: " + sPriceFieldId
                + sMouthCrLf + "sTradableYesOrNo is: " + sTradableYesOrNo
                + sMouthCrLf + "sInstrumentPrice is: " + sInstrumentPrice
                ;
            nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

            sOrderId = "0";

            try
            {
                sMouthRaccoonReturnValueReceived = cMouthRaccoon.mMouthOrderPlace(
                          sProgrammeName
                        , sProgrammeArgumentAlpha
                        , sProgrammeSection
                        , sEpisodeId
                        , sOcaGroup
                        , sAllOrNone
                        , sSequenceNumberNow
                        , sSequenceNumberMax
                        , sInstrumentId
                        , sSymbolName
                        , sSecTypeName
                        , sExchangeName
                        , sCurrencyBaseName
                        , sOrderPriceOffsetFraction
                        , sTradableYesOrNo
                        , sOrderType
                        , sOrdersAction  //not used
                        , sPriceFieldId
                        , sInstrumentPrice
                        , sLotValueDollarsTarget
                        , sPositionSize
                        , sTradingPhase
                        , sOrderId
                        , out sOrderIdCreated
                    );
            }
            catch
            {
                sMouthRaccoonMessageString = "mMouthPriceByProgrammeAndSequenceNumGetAndSave: !!!! "
                    + sMouthCrLf + "Catch entered when calling first mMouthInstrumentPriceGetAndSave !!!!";
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 1, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;

                return sMouthRaccoonValueToReturn;
            }

            iSequenceNumberNow = iSequenceNumberNow + 1;
            sOrderPriceOpenOffsetFraction = "-1";
            sOrderPriceCloseOffsetFraction = "-1";


            while (iSequenceNumberNow <= iSequenceNumberMax)
            {
                sSequenceNumberNow = iSequenceNumberNow.ToString();
                sTradableYesOrNo = "-1";
                sInstrumentPrice = "-1.0";

                try
                {
                    sMouthRaccoonReturnValueReceived = nsMouthSqlRaccoon.cMouthSqlRaccoon.mMouthSqlInstrumentDetailsGet(
                        out sInstrumentId
                        , out sSymbolName
                        , out sSecTypeName
                        , out sExchangeName
                        , out sCurrencyBaseName
                        , out sOrderPriceOpenOffsetFraction
                        , out sOrderPriceCloseOffsetFraction
                        , out sTradableYesOrNo
                        , out sInstrumentPrice
                        , sProgrammeName
                        , sProgrammeArgumentAlpha
                        , sProgrammeSection
                        , sSequenceNumberNow
                        , "-1"  //OrderId, ignored in this case
                        );
                }
                catch
                {
                    sMouthRaccoonMessageString = "mMouthOrdersByProgrammePlace: !!!! "
                        + sMouthCrLf + "Catch entered when calling subsequents mMouthSqlInstrumentDetailsGet !!!!";
                    nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                        , sMouthWhatTypeOfLogging, 1, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                    sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;

                    return sMouthRaccoonValueToReturn;
                }


                sOrderPriceOffsetFraction = "-1";

                if (sTradingPhase == "Opening")
                {
                    sOrderPriceOffsetFraction = sOrderPriceOpenOffsetFraction;
                }

                if (sTradingPhase == "Closing")
                {
                    sOrderPriceOffsetFraction = sOrderPriceCloseOffsetFraction;
                }

                sMouthRaccoonMessageString = "mMouthOrdersByProgrammePlace:"
                    + sMouthCrLf + "sSequenceNumberNow is: " + sSequenceNumberNow
                    + sMouthCrLf + "sSequenceNumberMax is: " + sSequenceNumberMax
                    + sMouthCrLf + "sEpisodeId is: " + sEpisodeId
                    + sMouthCrLf + "sOcaGroup is: " + sOcaGroup
                    + sMouthCrLf + "sInstrumentId is: " + sInstrumentId
                    + sMouthCrLf + "sSymbolName is: " + sSymbolName
                    + sMouthCrLf + "sSecTypeName is: " + sSecTypeName
                    + sMouthCrLf + "sExchangeName is: " + sExchangeName
                    + sMouthCrLf + "sCurrencyBaseName is: " + sCurrencyBaseName
                    + sMouthCrLf + "sOrderPriceOpenOffsetFraction is: " + sOrderPriceOpenOffsetFraction
                    + sMouthCrLf + "sOrderPriceCloseOffsetFraction is: " + sOrderPriceCloseOffsetFraction
                    + sMouthCrLf + "sOrderPriceOffsetFraction is: " + sOrderPriceOffsetFraction
                    + sMouthCrLf + "sPriceFieldId is: " + sPriceFieldId
                    + sMouthCrLf + "sTradableYesOrNo is: " + sTradableYesOrNo
                    + sMouthCrLf + "sInstrumentPrice is: " + sInstrumentPrice
                    ;
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                sOrderId = "0";

                try
                {
                    sMouthRaccoonReturnValueReceived = cMouthRaccoon.mMouthOrderPlace(
                          sProgrammeName
                        , sProgrammeArgumentAlpha
                        , sProgrammeSection
                        , sEpisodeId
                        , sOcaGroup
                        , sAllOrNone
                        , sSequenceNumberNow
                        , sSequenceNumberMax
                        , sInstrumentId
                        , sSymbolName
                        , sSecTypeName
                        , sExchangeName
                        , sCurrencyBaseName
                        , sOrderPriceOffsetFraction
                        , sTradableYesOrNo
                        , sOrderType
                        , sOrdersAction  //not used
                        , sPriceFieldId
                        , sInstrumentPrice
                        , sLotValueDollarsTarget
                        , sPositionSize
                        , sTradingPhase
                        , sOrderId
                        , out sOrderIdCreated
                        );
                }
                catch
                {
                    sMouthRaccoonMessageString = "mMouthPriceByProgrammeAndSequenceNumGetAndSave: !!!! "
                        + sMouthCrLf + "Catch entered when calling first mMouthInstrumentPriceGetAndSave !!!!";
                    nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                        , sMouthWhatTypeOfLogging, 1, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                    sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;

                    return sMouthRaccoonValueToReturn;
                }

                iSequenceNumberNow = iSequenceNumberNow + 1;
            }

            sMouthRaccoonValueToReturn = "0";

            return sMouthRaccoonValueToReturn;
        }



        public static String mMouthInstrumentPriceGetAndSave(
              out String sPriceValue
            , out String sPriceBidValue
            , out String sPriceAskValue
            , String sInstrumentId
            , String sSymbol
            , String sSecType
            , String sExchange
            , String sCurrency
            , String sPriceFieldId
            , String sRejectPriceFractionGreaterThan
            )
        {
            String sPrimaryExch = "-1";
            sMouthRaccoonValueToReturn = "-1";
            sPriceValue = "-1";
            sPriceBidValue = "-1";
            sPriceAskValue = "-1";

            sMouthRaccoonMessageString = "mMouthInstrumentPriceGetAndSave: Starting..";
            nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

            sMouthRaccoonMessageString = "mMouthInstrumentPriceGetAndSave: Input parameters: "
                + sMouthCrLf + "sInstrumentId: " + sInstrumentId
                + sMouthCrLf + "sSymbol: " + sSymbol
                + sMouthCrLf + "sSecType: " + sSecType
                + sMouthCrLf + "sExchange: " + sExchange
                + sMouthCrLf + "sCurrency: " + sCurrency
                + sMouthCrLf + "sPriceFieldId: " + sPriceFieldId;
            nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

            
            try
            {
                sMouthRaccoonReturnValueReceived = cMouthRaccoon.mMouthBrokerMarketPricesRequest(
                    sInstrumentId
                    , sSymbol
                    , sSecType
                    , sExchange
                    , sCurrency
                    , sPrimaryExch
                );

                if (sMouthRaccoonReturnValueReceived != "0")
                {
                    sMouthRaccoonMessageString = "mMouthInstrumentPriceGetAndSave: mMouthBrokerMarketPricesRequest returned non zero code: "
                        + sMouthRaccoonReturnValueReceived;
                    nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                        , sMouthWhatTypeOfLogging, 1, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                    sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;
                    return sMouthRaccoonValueToReturn;
                }
            }
            catch
            {
                sMouthRaccoonMessageString = "mMouthInstrumentPriceGetAndSave: !!!! Catch statement entered on" +
                    " calling mMouthBrokerMarketPricesRequest !!!!";
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 1, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;
                return sMouthRaccoonValueToReturn;
            }
            

            try
            {
                sMouthRaccoonReturnValueReceived = nsMouthSqlRaccoon.cMouthSqlRaccoon.mMouthSqlMainPriceArrivalsGet(
                      out sPriceValue
                    , out sPriceBidValue
                    , out sPriceAskValue
                    , sPriceFieldId
                    , sInstrumentId
                    );

                if (sMouthRaccoonReturnValueReceived != "0")
                {
                    sMouthRaccoonMessageString = "mMouthInstrumentPriceGetAndSave: mMouthSqlMainPriceArrivalsGet returned non zero code: "
                        + sMouthRaccoonReturnValueReceived;
                    nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                        , sMouthWhatTypeOfLogging, 1, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                    sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;
                    return sMouthRaccoonValueToReturn;
                }
            }
            catch
            {
                sMouthRaccoonMessageString = "mMouthInstrumentPriceGetAndSave: !!!! Catch statement entered on" +
                    " calling mMouthSqlMainPriceArrivalsGet !!!!";
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 1, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;
                return sMouthRaccoonValueToReturn;
            }


            sMouthRaccoonMessageString = "mMouthInstrumentPriceGetAndSave: Price received is: " + sPriceValue;
            nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");
            

            try
            {
                sMouthRaccoonReturnValueReceived = nsMouthSqlRaccoon.cMouthSqlRaccoon.mMouthSqlInstrumentPriceUpdate(
                      sInstrumentId
                    , sPriceFieldId
                    , sPriceValue
                    , sPriceBidValue
                    , sPriceAskValue
                    , sRejectPriceFractionGreaterThan
                    );

                if (sMouthRaccoonReturnValueReceived != "0")
                {
                    sMouthRaccoonMessageString = "mMouthInstrumentPriceGetAndSave: mMouthSqlInstrumentPriceUpdate returned non zero code: "
                        + sMouthRaccoonReturnValueReceived;
                    nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                        , sMouthWhatTypeOfLogging, 3, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                    sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;
                    return sMouthRaccoonValueToReturn;
                }
            }
            catch
            {
                sMouthRaccoonMessageString = "mMouthInstrumentPriceGetAndSave: !!!! Catch statement entered on" +
                    " calling mMouthSqlInstrumentPriceUpdate !!!!";
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 1, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;
                return sMouthRaccoonValueToReturn;
            }

            sMouthRaccoonValueToReturn = "0";

            return sMouthRaccoonValueToReturn;
        }
        


        public static String mMouthOrderPlace(
              String sProgrammeName
            , String sProgrammeArgumentAlpha
            , String sProgrammeSection
            , String sEpisodeId
            , String sOcaGroup
            , String sAllOrNone
            , String sSequenceNumberNow
            , String sSequenceNumberMax
            , String sInstrumentId
            , String sSymbolName
            , String sSecTypeName
            , String sExchangeName
            , String sCurrencyBaseName
            , String sOrderPriceOffsetFraction
            , String sTradableYesOrNo
            , String sOrderType
            , String sOrdersAction
            , String sPriceFieldId
            , String sInstrumentPrice
            , String sLotValueDollarsTarget
            , String sPositionSize
            , String sTradingPhase
            , String sOrderId
            , out String sOrderIdCreated
            )
        {
            sMouthRaccoonValueToReturn = "-1";

            sMouthRaccoonMessageString = "Starting mMouthOrderPlace, with the following parameters: "
                + sMouthCrLf + " sProgrammeName = " + sProgrammeName
                + sMouthCrLf + " sProgrammeArgumentAlpha = " + sProgrammeArgumentAlpha
                + sMouthCrLf + " sProgrammeSection = " + sProgrammeSection
                + sMouthCrLf + " sEpisodeId = " + sEpisodeId
                + sMouthCrLf + " sOcaGroup = " + sOcaGroup
                + sMouthCrLf + " sAllOrNone = " + sAllOrNone
                + sMouthCrLf + " sSequenceNumberNow = " + sSequenceNumberNow
                + sMouthCrLf + " sSequenceNumberMax = " + sSequenceNumberMax
                + sMouthCrLf + " sInstrumentId = " + sInstrumentId
                + sMouthCrLf + " sSymbolName = " + sSymbolName
                + sMouthCrLf + " sSecTypeName = " + sSecTypeName
                + sMouthCrLf + " sExchangeName = " + sExchangeName
                + sMouthCrLf + " sCurrencyBaseName = " + sCurrencyBaseName
                + sMouthCrLf + " sOrderPriceOffsetFraction = " + sOrderPriceOffsetFraction
                + sMouthCrLf + " sTradableYesOrNo = " + sTradableYesOrNo
                + sMouthCrLf + " sOrderType = " + sOrderType
                + sMouthCrLf + " sOrdersAction = " + sOrdersAction
                + sMouthCrLf + " sPriceFieldId = " + sPriceFieldId
                + sMouthCrLf + " sInstrumentPrice = " + sInstrumentPrice
                + sMouthCrLf + " sLotValueDollarsTarget = " + sLotValueDollarsTarget
                + sMouthCrLf + " sPositionSize = " + sPositionSize
                + sMouthCrLf + " sOrderId (0 for new order) = " + sOrderId
                ;
            nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");
            

            String sNextValidOrderIdentifier = "-1";
            String sOrderQuantity = "-1";
            String sOrderLimitPrice = "-1";
            String sPermissionToPlaceOrder = "no";
            String sOrdersActionInput = "-1";
            String sOrdersActionOutput = "-1";
            String sUseOneCancelsAll = "-1";

            if (sTradingPhase == "Opening")
            {
                sUseOneCancelsAll = "yes";
            }
            else
            {
                sUseOneCancelsAll = "no";
            }

            sOrderIdCreated = "-1";
            sOrdersActionInput = sOrdersAction;

            if (sTradableYesOrNo == "yes")
            {
                //do nothing
            }
            else
            {
                sMouthRaccoonMessageString = "mMouthOrderPlace: Stock: " + sInstrumentId + " symbol: " + sSymbolName
                    + " is a non-trading item. Not placing order";
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;

                return sMouthRaccoonValueToReturn;
            }


            try
            {
                sMouthRaccoonReturnValueReceived = nsMouthSqlRaccoon.cMouthSqlRaccoon.mMouthSqlOrderPlaceDatabase(
                sProgrammeName
                , sProgrammeArgumentAlpha
                , sProgrammeSection
                , sEpisodeId
                , sSequenceNumberNow
                , sSequenceNumberMax
                , sInstrumentId
                , sSymbolName
                , sSecTypeName
                , sExchangeName
                , sCurrencyBaseName
                , sOrderPriceOffsetFraction
                , sOrderType
                , sOrdersActionInput
                , out sOrdersActionOutput
                , sPriceFieldId
                , sInstrumentPrice
                , sLotValueDollarsTarget
                , out sNextValidOrderIdentifier
                , out sOrderQuantity
                , out sOrderLimitPrice
                , out sPermissionToPlaceOrder
                , sPositionSize
                , sTradingPhase
                , sOrderId
                );

                if (sMouthRaccoonReturnValueReceived != "0")
                {
                    sMouthRaccoonMessageString = "mMouthOrderPlace: mMouthSqlOrderPlaceDatabase: "
                        + " returned a non zero return code. Not placing order";
                    nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                        , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                    sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;

                    return sMouthRaccoonValueToReturn;
                }
            }
            catch
            {
                sMouthRaccoonMessageString = "mMouthOrderPlace: Catch when running mMouthSqlOrderPlaceDatabase. Not placing order ";
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;

                return sMouthRaccoonValueToReturn;
            }
            
            
            if (sPermissionToPlaceOrder == "yes")
            {
                //do nothing
            }
            else
            {
                sMouthRaccoonMessageString = "mMouthOrderPlace: Order NOT placed, as sPermissionToPlaceOrder not set to yes";

                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;

                return sMouthRaccoonValueToReturn;
            }


            try
            {
                sMouthRaccoonReturnValueReceived = cMouthRaccoon.mMouthBrokerOrderPlace(
                      sNextValidOrderIdentifier
                    , sSymbolName
                    , sSecTypeName
                    , sExchangeName
                    , sCurrencyBaseName
                    , sOrderType
                    , sOrdersActionOutput
                    , sOrderQuantity
                    , sOrderLimitPrice
                    , sUseOneCancelsAll
                    , sOcaGroup
                    , sAllOrNone
                    );
            }
            catch
            {
                sMouthRaccoonMessageString = "mMouthOrderPlace: Catch when running mMouthBrokerOrderPlace.";
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;

                return sMouthRaccoonValueToReturn;
            }


            sMouthRaccoonMessageString = "mMouthOrderPlace: Order placed, with parameters: "
                + sMouthCrLf + "sInstrumentId: " + sInstrumentId
                + sMouthCrLf + "sSymbolName: " + sSymbolName
                + sMouthCrLf + "sSecTypeName: " + sSecTypeName
                + sMouthCrLf + "sExchangeName: " + sExchangeName
                + sMouthCrLf + "sCurrencyName: " + sCurrencyBaseName
                + sMouthCrLf + "sNextValidOrderIdentifier: " + sNextValidOrderIdentifier
                + sMouthCrLf + "sOrderType: " + sOrderType
                + sMouthCrLf + "sOrdersActionOutput: " + sOrdersActionOutput
                + sMouthCrLf + "sOrderQuantity: " + sOrderQuantity
                + sMouthCrLf + "sOrderLimitPrice: " + sOrderLimitPrice
                + sMouthCrLf + "sOcaGroup: " + sOcaGroup
                + sMouthCrLf + "sPermissionToPlaceOrder: " + sPermissionToPlaceOrder
                + sMouthCrLf + "sPositionSize: " + sPositionSize;


            nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

            sOrderIdCreated = sNextValidOrderIdentifier;


            sMouthRaccoonValueToReturn = "0";

            return sMouthRaccoonValueToReturn;
        }

        

        public static String mMouthWaitForNetTradingPosition(
              String sProgrammeName
            , String sProgrammeArgumentAlpha
            , String sProgrammeSection
            , String sEpisodeId
            , String sHoldTimeBeforeClosingOrdersSeconds
            , String sWaitForWhatNetPosition    //yes or no
            , out String sPositionOpenYesOrNo
            , String sOrderId
            , String sPriceChangeIntervalSeconds
            , String sTradingPhase
            )
        {
            String sMouthRaccoonValueToReturn = "-1";
            String sNumberOfTradesSoFar = "-1";
            //String sPositionOpenYesOrNo = "no";

            DateTime dWaitForNetTradingPositionStartUtcDateTime;
            DateTime dCurrentUtcDateTimeInLoop;
            Int32 iNumberOfSecondsInLoop;
            Int32 iSecondsSinceLastPriceChange = -1;
            Int32 iHoldTimeBeforeClosingOrdersSeconds;
            Int32 iPriceChangesNumber = 0;
            Int32 iPriceChangeIntervalSeconds = -1;
            TimeSpan tsOrdersLiveForSpan;
            TimeSpan tsSecondsSinceLastPriceChange;
            String sPositionOpenYesOrNoNew = "-1";
            DateTime dDateTimeLastPriceChange;
            String sSymbolName = "-1";
            String sSecTypeName = "-1";
            String sExchangeName = "-1";
            String sCurrencyBaseName = "-1";
            String sOrderPriceOpenOffsetFraction = "-1";
            String sOrderPriceCloseOffsetFraction = "-1";
            String sTradableYesOrNo = "-1";
            String sInstrumentPrice = "-1";
            String sPriceBidValue = "-1";
            String sPriceAskValue = "-1";
            String sRejectPriceFractionGreaterThan = "0";

            iPriceChangeIntervalSeconds = Convert.ToInt32(sPriceChangeIntervalSeconds);

            //Next 10 rows to toggle the value of sPositionOpenYesOrNo just to get it initially into the WHILE loop.

            sPositionOpenYesOrNo = sWaitForWhatNetPosition; //"yes" - initial setup;

                if (sPositionOpenYesOrNo == "yes")
                {
                    sPositionOpenYesOrNoNew = "no";
                }

                if (sPositionOpenYesOrNo == "no")
                {
                    sPositionOpenYesOrNoNew = "yes";
                }

                sPositionOpenYesOrNo = sPositionOpenYesOrNoNew;

            try
            {
                iHoldTimeBeforeClosingOrdersSeconds = Convert.ToInt32(sHoldTimeBeforeClosingOrdersSeconds);
            } 
            catch
            {
                sMouthRaccoonMessageString = "mMouthWaitForNetTradingPosition: Catch when converting sHoldTimeBeforeClosingOrdersSeconds to INT";
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;

                return sMouthRaccoonValueToReturn;
            }


            dWaitForNetTradingPositionStartUtcDateTime = DateTime.UtcNow;
            dCurrentUtcDateTimeInLoop = DateTime.UtcNow;

            tsOrdersLiveForSpan = (dCurrentUtcDateTimeInLoop - dWaitForNetTradingPositionStartUtcDateTime);
            iNumberOfSecondsInLoop = tsOrdersLiveForSpan.Seconds + (tsOrdersLiveForSpan.Minutes * 60);


            dDateTimeLastPriceChange = DateTime.UtcNow;

            while
                (
                    (iNumberOfSecondsInLoop < iHoldTimeBeforeClosingOrdersSeconds)
                    &&
                    (sPositionOpenYesOrNo != sWaitForWhatNetPosition)    //"yes")  //
                )
            {
                String sInstrumentId = "-1";
                String sLongOrShort = "-1";
                String sPriceFieldId = "-1";
                String sPositionSize = "-1";
                String sPositionsOpenNumber = "-1";

                tsSecondsSinceLastPriceChange = (DateTime.UtcNow - dDateTimeLastPriceChange);
                iSecondsSinceLastPriceChange = tsSecondsSinceLastPriceChange.Seconds + (60 * tsSecondsSinceLastPriceChange.Minutes);

                sMouthRaccoonMessageString = "mMouthWaitForNetTradingPosition: PriceChange variables are:"
                    + sMouthCrLf + " iSecondsSinceLastPriceChange is: " + iSecondsSinceLastPriceChange.ToString()
                    + sMouthCrLf + " iPriceChangesNumber is: " + iPriceChangesNumber.ToString()
                    + sMouthCrLf + " dDateTimeLastPriceChange.Hour is: " + dDateTimeLastPriceChange.Hour.ToString()
                    + sMouthCrLf + " dDateTimeLastPriceChange.Minute is: " + dDateTimeLastPriceChange.Minute.ToString()
                    + sMouthCrLf + " iPriceChangeIntervalSeconds is: " + iPriceChangeIntervalSeconds.ToString()
                    ;
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 3, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");


                if ((iSecondsSinceLastPriceChange > iPriceChangeIntervalSeconds) & (iPriceChangeIntervalSeconds != 0))
                {

                    sMouthRaccoonMessageString = "mMouthWaitForNetTradingPosition:  Inside PriceChangeIf statment."
                        + sMouthCrLf + " Re-setting order's price "
                        ;

                    nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                        , sMouthWhatTypeOfLogging, 3, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                    dDateTimeLastPriceChange = DateTime.UtcNow;
                    iPriceChangesNumber = iPriceChangesNumber + 1;
                    
                    sSymbolName = "-1";
                    sSecTypeName = "-1";
                    sExchangeName = "-1";
                    sCurrencyBaseName = "-1";
                    sOrderPriceOpenOffsetFraction = "-1";
                    sOrderPriceCloseOffsetFraction = "-1";
                    sTradableYesOrNo = "-1";
                    sInstrumentPrice = "-1";
                                        
                    try
                    {
                        sMouthRaccoonReturnValueReceived = nsMouthSqlRaccoon.cMouthSqlRaccoon.mMouthSqlInstrumentDetailsGet(
                              out sInstrumentId
                            , out sSymbolName
                            , out sSecTypeName
                            , out sExchangeName
                            , out sCurrencyBaseName
                            , out sOrderPriceOpenOffsetFraction
                            , out sOrderPriceCloseOffsetFraction
                            , out sTradableYesOrNo
                            , out sInstrumentPrice  //not used here
                            , sProgrammeName
                            , sProgrammeArgumentAlpha
                            , sProgrammeSection
                            , "-1"  //sSequenceNumberNow - not used here
                            , sOrderId
                            );
                    }
                    catch
                    {
                        sMouthRaccoonMessageString = "mMouthWaitForNetTradingPosition: !!!! "
                            + sMouthCrLf + "Catch entered when calling mMouthSqlInstrumentDetailsGet !!!!";
                        nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                            , sMouthWhatTypeOfLogging, 1, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                        sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;

                        return sMouthRaccoonValueToReturn;
                    }

                    sMouthRaccoonMessageString = "mMouthWaitForNetTradingPosition: PriceIf: mMouthSqlInstrumentDetailsGet:"
                        + sMouthCrLf + "sInstrumentId is: " + sInstrumentId
                        + sMouthCrLf + "sSymbolName is: " + sSymbolName
                        + sMouthCrLf + "sSecTypeName is: " + sSecTypeName
                        + sMouthCrLf + "sExchangeName is: " + sExchangeName
                        + sMouthCrLf + "sCurrencyBaseName is: " + sCurrencyBaseName
                        + sMouthCrLf + "sTradableYesOrNo is: " + sTradableYesOrNo
                        + sMouthCrLf + "sProgrammeName is: " + sProgrammeName
                        + sMouthCrLf + "sProgrammeArgumentAlpha is: " + sProgrammeArgumentAlpha
                        + sMouthCrLf + "sProgrammeSection is: " + sProgrammeSection
                        + sMouthCrLf + "sOrderId is: " + sOrderId
                        ;

                    nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                        , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");


                    try
                    {
                        sMouthRaccoonReturnValueReceived = cMouthRaccoon.mMouthInstrumentPriceGetAndSave(
                              out sInstrumentPrice
                            , out sPriceBidValue   //not used here
                            , out sPriceAskValue   //not used here
                            , sInstrumentId
                            , sSymbolName
                            , sSecTypeName
                            , sExchangeName
                            , sCurrencyBaseName
                            , sPriceFieldId
                            , sRejectPriceFractionGreaterThan
                            );
                    }
                    catch
                    {
                        sMouthRaccoonMessageString = "mMouthWaitForNetTradingPosition: mMouthInstrumentPriceGetAndSave !!!! "
                            + sMouthCrLf + "Catch entered when calling first mMouthInstrumentPriceGetAndSave !!!!";
                        nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                            , sMouthWhatTypeOfLogging, 1, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                        sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;

                        return sMouthRaccoonValueToReturn;
                    }


                    try
                    {
                        sMouthRaccoonReturnValueReceived = nsMouthSqlRaccoon.cMouthSqlRaccoon.mMouthSqlPositionOpenDetailsGet(
                              sOrderId
                            , out sInstrumentId
                            , out sEpisodeId
                            , out sLongOrShort
                            , out sPriceFieldId
                            , out sPositionSize
                            , out sPositionsOpenNumber
                            );
                    }
                    catch
                    {
                        sMouthRaccoonMessageString = "mMouthPositionClose: !!!! "
                            + sMouthCrLf + "Catch entered when calling first mMouthSqlPositionOpenDetailsGet !!!!";
                        nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                            , sMouthWhatTypeOfLogging, 1, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                        sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;

                        return sMouthRaccoonValueToReturn;
                    }


                    sMouthRaccoonMessageString = "mMouthPositionClose: mMouthSqlPositionOpenDetailsGet:"
                        + sMouthCrLf + "sInstrumentId is: " + sInstrumentId
                        + sMouthCrLf + "sEpisodeId is: " + sEpisodeId
                        + sMouthCrLf + "sLongOrShort is: " + sLongOrShort
                        + sMouthCrLf + "sPriceFieldId is: " + sPriceFieldId
                        + sMouthCrLf + "sPositionSize is: " + sPositionSize
                        + sMouthCrLf + "sOrderId is: " + sOrderId
                        + sMouthCrLf + "sPositionsOpenNumber is: " + sPositionsOpenNumber
                        ;

                    nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                        , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                    String sOrdersAction = "-1";

                    if (sLongOrShort == "Long")
                    {
                        sOrdersAction = "SELL";
                    }
                    else
                    {
                        sOrdersAction = "BUY";
                    }
                    

                    if (((dDateTimeLastPriceChange.Hour) * 60 + dDateTimeLastPriceChange.Minute) > 1190)
                    {
                        //set closing fraction to in-the-money to close it quickly before the end of the day's trading
                        //1190 corresponds to 19:50, which in UTC is 10 minutes before close of trading

                        sOrderPriceCloseOffsetFraction = "0.999";     //values below 1 are in-the-money for immediate fills
                    }

                    String sOrderIdCreated = "-1";

                    try
                    {
                        sMouthRaccoonReturnValueReceived = cMouthRaccoon.mMouthOrderPlace(
                              sProgrammeName
                            , sProgrammeArgumentAlpha
                            , sProgrammeSection
                            , sEpisodeId
                            , "-1"  //sOcaGroup
                            , "-1"  //sAllOrNone    //Not Used
                            , "-1"  //sSequenceNumberNow
                            , "-1"  //sSequenceNumberMax
                            , sInstrumentId
                            , sSymbolName
                            , sSecTypeName
                            , sExchangeName
                            , sCurrencyBaseName
                            , sOrderPriceCloseOffsetFraction    //sOrderPriceOffsetFraction
                            , sTradableYesOrNo
                            , "LMT"    //sOrderType
                            , sOrdersAction
                            , sPriceFieldId
                            , sInstrumentPrice
                            , "-1"  //sLotValueDollarsTarget     //Not Used
                            , "-1"  //sPositionSize //Not Used, because the position size is taken from the existing order
                            , sTradingPhase //Closing
                            , sOrderId
                            , out sOrderIdCreated   //Not Used
                            );

                            sMouthRaccoonMessageString = "mMouthWaitForNetTradingPosition: mMouthOrderPlace: Order price updated "
                                + sMouthCrLf + " for order: " + sOrderId
                                ;
                            nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                                , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                            sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;

                    }
                    catch
                    {
                        sMouthRaccoonMessageString = "mMouthWaitForNetTradingPosition: mMouthOrderPlace: !!!! Catch entered !!!! ";
                        nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                            , sMouthWhatTypeOfLogging, 1, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                        sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;

                        return sMouthRaccoonValueToReturn;
                    }
                    
                }


                try
                {

                    sMouthRaccoonReturnValueReceived = nsMouthSqlRaccoon.cMouthSqlRaccoon.mMouthSqlPositionOpenDetailsGet(
                          sOrderId
                        , out sInstrumentId
                        , out sEpisodeId
                        , out sLongOrShort
                        , out sPriceFieldId
                        , out sPositionSize
                        , out sPositionsOpenNumber
                        );

                    sMouthRaccoonMessageString = "mMouthWaitForNetTradingPosition: mMouthSqlPositionOpenDetailsGet "
                    + sMouthCrLf + " sProgrammeSection is: " + sProgrammeSection
                    + sMouthCrLf + " sPositionsOpenNumber returned is: " + sPositionsOpenNumber
                    ;
                    nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                        , sMouthWhatTypeOfLogging, 3, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");
                }
                catch
                {
                    sMouthRaccoonMessageString = "mMouthWaitForNetTradingPosition: Catch when running mMouthSqlPositionOpenDetailsGet";
                    nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                        , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                    sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;

                    return sMouthRaccoonValueToReturn;
                }


                sPositionOpenYesOrNo = "-1";

                if (sPositionsOpenNumber != "0")
                {
                    sPositionOpenYesOrNo = "yes";
                }

                if (sPositionsOpenNumber == "0")
                {
                    sPositionOpenYesOrNo = "no";
                }

                
                sMouthRaccoonMessageString = "mMouthWaitForNetTradingPosition:"
                    + sMouthCrLf + "sOrderId input is: " + sOrderId
                    + sMouthCrLf + "iNumberOfSecondsInLoop is: " + iNumberOfSecondsInLoop.ToString()
                    + sMouthCrLf + "tsOrdersLiveForSpan.Seconds is: " + tsOrdersLiveForSpan.Seconds.ToString()
                    + sMouthCrLf + "tsOrdersLiveForSpan.Minutes is: " + tsOrdersLiveForSpan.Minutes.ToString()
                    + sMouthCrLf + "iSecondsSinceLastPriceChange is: " + iSecondsSinceLastPriceChange.ToString()
                    
                    + sMouthCrLf + "dCurrentUtcDateTimeInLoop is: " + dCurrentUtcDateTimeInLoop.ToLongTimeString()
                    + sMouthCrLf + "dWaitForNetTradingPositionStartUtcDateTime is: " + dWaitForNetTradingPositionStartUtcDateTime.ToLongTimeString()

                    + sMouthCrLf + "iHoldTimeBeforeClosingOrdersSeconds is: " + iHoldTimeBeforeClosingOrdersSeconds.ToString()
                    + sMouthCrLf + "sNumberOfTradesSoFar is: " + sNumberOfTradesSoFar
                    + sMouthCrLf + "sWaitForWhatNetPosition is: " + sWaitForWhatNetPosition
                    + sMouthCrLf + "sPositionOpenYesOrNo is: " + sPositionOpenYesOrNo;

                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                Thread.Sleep(4000); //4 second wait

                dCurrentUtcDateTimeInLoop = DateTime.UtcNow;

                tsOrdersLiveForSpan = (dCurrentUtcDateTimeInLoop - dWaitForNetTradingPositionStartUtcDateTime);
                iNumberOfSecondsInLoop = tsOrdersLiveForSpan.Seconds + (tsOrdersLiveForSpan.Minutes * 60);

                
                tsSecondsSinceLastPriceChange = (DateTime.UtcNow - dDateTimeLastPriceChange);
                iSecondsSinceLastPriceChange = tsSecondsSinceLastPriceChange.Seconds;
            }


            sMouthRaccoonValueToReturn = "0";

            return sMouthRaccoonValueToReturn;
        }



        public static String mMouthOrdersCancel(
              String sWhichOrders    //"OpeningNotTraded" or "NotTraded"
            , String sOrderIdToSkip
            , String sCalledFromThread
            )
        {
            String sOrdersCsv = "-1";

            sMouthRaccoonMessageString = "mMouthOrdersCancel: Starting, with parameters:"
                + sMouthCrLf + " sWhichOrders: " + sWhichOrders
                + sMouthCrLf + " sOrderIdToSkip: " + sOrderIdToSkip
                + sMouthCrLf + " sCalledFromThread: " + sCalledFromThread;
            nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");
            

            try
            {
                sMouthRaccoonReturnValueReceived = nsMouthSqlRaccoon.cMouthSqlRaccoon.mMouthSqlOrdersGet(
                    out sOrdersCsv  //is 0 if no trades yet
                    , sWhichOrders
                    );
            }
            catch
            {
                sMouthRaccoonMessageString = "mMouthOrdersCancel: Catch when running mMouthSqlOrdersGet";
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;

                return sMouthRaccoonValueToReturn;
            }
            

            if (sOrdersCsv == "")
            {
                sMouthRaccoonMessageString = "mMouthOrdersCancel: No orders present in sOrdersCsv return string, so exiting sOrdersCsv"
                    ;
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 3, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");


                sMouthRaccoonValueToReturn = "0";

                return sMouthRaccoonValueToReturn;
            }
            

            String[] sOrdersToClose = sOrdersCsv.Split(',');

            foreach (String sOrderToClose in sOrdersToClose)
            {
                if (sOrderToClose != sOrderIdToSkip)
                {
                    sMouthRaccoonMessageString = "mMouthOrdersCancel: "
                        + sMouthCrLf + " alsOrderArrayList sOrdersId value = " + sOrderToClose
                        + sMouthCrLf + " sCalledFromThread: " + sCalledFromThread;
                    ;
                    nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                        , sMouthWhatTypeOfLogging, 3, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                    try
                    {
                        sMouthRaccoonReturnValueReceived = cMouthRaccoon.mMouthBrokerOrderCancel(
                            sOrderToClose
                            );
                    }
                    catch
                    {
                        sMouthRaccoonMessageString = "mMouthWaitForNetTradingPosition: Catch when running mMouthSqlOrdersGet";
                        nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                            , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                        sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;

                        return sMouthRaccoonValueToReturn;
                    }
                }
            }
            
            sMouthRaccoonValueToReturn = "0";

            return sMouthRaccoonValueToReturn;
        }



        public static String mMouthPositionsAllClose(
              String sProgrammeName
            , String sProgrammeArgumentAlpha
            , String sProgrammeSection
            , String sWhichOrders    //"PartialFilledFullyFilled" or "UnFilledPartialFilled". "PartialFilledFullyFilled" in use at the moment.
            , String sTradingPhase
            , String sAllOrNone
            , String sPriceChangeIntervalSeconds
            )
        {
            String sOrdersCsv = "-1";

            try
            {
                sMouthRaccoonReturnValueReceived = nsMouthSqlRaccoon.cMouthSqlRaccoon.mMouthSqlOrdersGet(
                    out sOrdersCsv  //is 0 if no trades yet
                    , sWhichOrders  // "Traded". So only outputs those OrderId values that have corresponding completed trades, and thus open positions.
                    );
            }
            catch
            {
                sMouthRaccoonMessageString = "mMouthPositionsAllClose: Catch when running mMouthSqlOrdersGet";
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;

                return sMouthRaccoonValueToReturn;
            }

            String[] sOrdersToClose = sOrdersCsv.Split(',');

            foreach (String sOrderToClose in sOrdersToClose)
            {
                sMouthRaccoonMessageString = "mMouthPositionsAllClose: Closing position for order."
                    + sMouthCrLf + " alsOrderArrayList sOrdersId value = " + sOrderToClose
                    + sMouthCrLf + " sOrdersCsv (parent) is: " + sOrdersCsv
                    + sMouthCrLf + " sWhichOrders is: " + sWhichOrders
                    ;
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 3, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");
                
                try
                {
                    sMouthRaccoonReturnValueReceived = cMouthRaccoon.mMouthPositionClose(
                          sProgrammeName
                        , sProgrammeArgumentAlpha
                        , sProgrammeSection
                        , sOrderToClose
                        , sTradingPhase
                        , sAllOrNone
                        , sPriceChangeIntervalSeconds
                        );
                }
                catch
                {
                    sMouthRaccoonMessageString = "mMouthPositionsAllClose: Catch when running mMouthSqlOrdersGet";
                    nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                        , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                    sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;

                    return sMouthRaccoonValueToReturn;
                }

            }
            
            sMouthRaccoonValueToReturn = "0";

            return sMouthRaccoonValueToReturn;
        }

        

        public static String mMouthPositionClose(
              String sProgrammeName
            , String sProgrammeArgumentAlpha
            , String sProgrammeSection
            , String sOrderToClose
            , String sTradingPhase
            , String sAllOrNone
            , String sPriceChangeIntervalSeconds
            )
        {
            String sSymbolName = "-1";
            String sSecTypeName = "-1";
            String sExchangeName = "-1";
            String sCurrencyBaseName = "-1";
            String sInstrumentId = "-1";
            String sOrderPriceOpenOffsetFraction = "-1";
            String sOrderPriceCloseOffsetFraction = "-1";
            String sOrderPriceOffsetFraction = "-1";
            String sTradableYesOrNo = "-1";
            String sInstrumentPrice = "-1";
            String sSequenceNumberNow = "-1";
            String sOrderId;
            String sOrderIdCreated = "-1";
            String sClosingOrderId = "-1";

            String sEpisodeId = "-1";
            String sLongOrShort = "-1";
            String sPriceFieldId = "-1";
            String sPositionSize = "-1";
            String sPositionsOpenNumber = "-1";
            String sPriceBidValue = "-1";
            String sPriceAskValue = "-1";
            String sRejectPriceFractionGreaterThan = "0";
            String sHoldTimeBeforeClosingOrdersSeconds = "-1";

            sOrderId = sOrderToClose;
            
            
            try
            {
                sMouthRaccoonReturnValueReceived = nsMouthSqlRaccoon.cMouthSqlRaccoon.mMouthSqlInstrumentDetailsGet(
                    out sInstrumentId
                    , out sSymbolName
                    , out sSecTypeName
                    , out sExchangeName
                    , out sCurrencyBaseName
                    , out sOrderPriceOpenOffsetFraction
                    , out sOrderPriceCloseOffsetFraction
                    , out sTradableYesOrNo
                    , out sInstrumentPrice  //not used here
                    , sProgrammeName
                    , sProgrammeArgumentAlpha
                    , sProgrammeSection
                    , sSequenceNumberNow
                    , sOrderId
                    );
            }
            catch
            {
                sMouthRaccoonMessageString = "mMouthOrdersByProgrammePlace: !!!! "
                    + sMouthCrLf + "Catch entered when calling first mMouthSqlInstrumentDetailsGet !!!!";
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 1, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;

                return sMouthRaccoonValueToReturn;
            }

            sMouthRaccoonMessageString = "mMouthPositionClose: mMouthSqlInstrumentDetailsGet:"
                + sMouthCrLf + "sInstrumentId is: " + sInstrumentId
                + sMouthCrLf + "sSymbolName is: " + sSymbolName
                + sMouthCrLf + "sSecTypeName is: " + sSecTypeName
                + sMouthCrLf + "sExchangeName is: " + sExchangeName
                + sMouthCrLf + "sCurrencyBaseName is: " + sCurrencyBaseName
                + sMouthCrLf + "sTradableYesOrNo is: " + sTradableYesOrNo
                + sMouthCrLf + "sProgrammeName is: " + sProgrammeName
                + sMouthCrLf + "sProgrammeArgumentAlpha is: " + sProgrammeArgumentAlpha
                + sMouthCrLf + "sProgrammeSection is: " + sProgrammeSection
                + sMouthCrLf + "sSequenceNumberNow is: " + sSequenceNumberNow
                + sMouthCrLf + "sOrderId is: " + sOrderId
                ;

            nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");
            

            try
            {
                sMouthRaccoonReturnValueReceived = cMouthRaccoon.mMouthInstrumentPriceGetAndSave(
                      out sInstrumentPrice
                    , out sPriceBidValue   //not used here
                    , out sPriceAskValue   //not used here
                    , sInstrumentId
                    , sSymbolName
                    , sSecTypeName
                    , sExchangeName
                    , sCurrencyBaseName
                    , sPriceFieldId
                    , sRejectPriceFractionGreaterThan
                    );
            }
            catch
            {
                sMouthRaccoonMessageString = "mMouthPositionClose: mMouthInstrumentPriceGetAndSave !!!! "
                    + sMouthCrLf + "Catch entered when calling first mMouthInstrumentPriceGetAndSave !!!!";
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 1, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;

                return sMouthRaccoonValueToReturn;
            }


            try
            {
                sMouthRaccoonReturnValueReceived = nsMouthSqlRaccoon.cMouthSqlRaccoon.mMouthSqlPositionOpenDetailsGet(
                      sOrderId
                    , out sInstrumentId
                    , out sEpisodeId
                    , out sLongOrShort
                    , out sPriceFieldId
                    , out sPositionSize
                    , out sPositionsOpenNumber
                    );
            }
            catch
            {
                sMouthRaccoonMessageString = "mMouthPositionClose: !!!! "
                    + sMouthCrLf + "Catch entered when calling first mMouthSqlPositionOpenDetailsGet !!!!";
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 1, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;

                return sMouthRaccoonValueToReturn;
            }


            sMouthRaccoonMessageString = "mMouthPositionClose: mMouthSqlPositionOpenDetailsGet:"
                + sMouthCrLf + "sInstrumentId is: " + sInstrumentId
                + sMouthCrLf + "sEpisodeId is: " + sEpisodeId
                + sMouthCrLf + "sLongOrShort is: " + sLongOrShort
                + sMouthCrLf + "sPriceFieldId is: " + sPriceFieldId
                + sMouthCrLf + "sPositionSize is: " + sPositionSize
                + sMouthCrLf + "sOrderId is: " + sOrderId
                + sMouthCrLf + "sPositionsOpenNumber is: " + sPositionsOpenNumber
                ;

            nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

            String sLotValueDollarsTarget = "-1";

            String sOrdersAction = "-1";

            if (sLongOrShort == "Long")
            {
                sOrdersAction = "SELL";
            }
            else
            {
                sOrdersAction = "BUY";
            }

            
            if (sTradingPhase == "Opening")
            {
                sOrderPriceOffsetFraction = sOrderPriceOpenOffsetFraction;
            }

            if (sTradingPhase == "Closing")
            {
                sOrderPriceOffsetFraction = sOrderPriceCloseOffsetFraction;
            }

            sClosingOrderId = "0";

            try
            {
                sMouthRaccoonReturnValueReceived = cMouthRaccoon.mMouthOrderPlace(
                      sProgrammeName
                    , sProgrammeArgumentAlpha
                    , sProgrammeSection
                    , sEpisodeId
                    , "-1"  //sOcaGroup
                    , sAllOrNone
                    , "-1"  //sSequenceNumberNow
                    , "-1"  //sSequenceNumberMax
                    , sInstrumentId
                    , sSymbolName
                    , sSecTypeName
                    , sExchangeName
                    , sCurrencyBaseName
                    , sOrderPriceOffsetFraction
                    , sTradableYesOrNo
                    , "LMT"    //sOrderType
                    , sOrdersAction
                    , sPriceFieldId
                    , sInstrumentPrice
                    , sLotValueDollarsTarget
                    , sPositionSize
                    , sTradingPhase
                    , sClosingOrderId
                    , out sOrderIdCreated
                    );
            }
            catch
            {
                sMouthRaccoonMessageString = "mMouthPositionClose: mMouthOrderPlace: !!!! Catch entered !!!! ";
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 1, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;

                return sMouthRaccoonValueToReturn;
            }


            String sWaitForWhatNetPosition = "no";
            String sPositionOpenYesOrNo = "-1";
            sHoldTimeBeforeClosingOrdersSeconds = "28800";   // "1800";

            sMouthRaccoonReturnValueReceived = cMouthRaccoon.mMouthWaitForNetTradingPosition(
                  sProgrammeName
                , sProgrammeArgumentAlpha
                , sProgrammeSection
                , sEpisodeId
                , sHoldTimeBeforeClosingOrdersSeconds
                , sWaitForWhatNetPosition
                , out sPositionOpenYesOrNo
                , sOrderIdCreated   //sClosingOrderId
                , sPriceChangeIntervalSeconds
                , sTradingPhase
                );


            sMouthRaccoonValueToReturn = "0";

            return sMouthRaccoonValueToReturn;
        }



            public static String mMouthGetHistoricalPrices(
              String sProgrammeName
            , String sProgrammeArgumentAlpha
            , String sProgrammeSection
            )
        {
            String sSharesInstrumentId = "-1";
            String sSymbol = "-1";
            String sSecType = "-1";
            String sExchange = "-1";
            String sPrimaryExch = "-1";
            String sCurrency = "-1";
            String sEndDate = "-1";
            DateTime dStartDate = Convert.ToDateTime("14/09/2017");
            DateTime dEndDate = Convert.ToDateTime("15/09/2016");

            DateTime dHistoricalDate = dStartDate;

            sSecType = "STK";
            sExchange = "SMART";
            sCurrency = "USD";
            sSharesInstrumentId = "54";
            sSymbol = "KR";


            while (dHistoricalDate >= dEndDate)
            {
                if (dHistoricalDate.DayOfWeek.ToString() != "Saturday" && dHistoricalDate.DayOfWeek.ToString() != "Sunday")
                {
                    sEndDate = dHistoricalDate.ToString("yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture);

                    sMouthRaccoonMessageString = "mMouthGetHistoricalPrices: "
                        + sMouthCrLf + "Historical date is: " + dHistoricalDate.ToLongDateString()
                        + sMouthCrLf + "sEndDate date is: " + sEndDate
                        + sMouthCrLf + "Day of week is: " + dHistoricalDate.DayOfWeek
                        ;

                    nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                        , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "N");

                    try
                    {
                        //sEndDate = "20170823";
                        //sSharesInstrumentId = "52";
                        //sSymbol = "MPC";
                        sMouthRaccoonReturnValueReceived = cMouthRaccoon.mMouthGetHistoricalPrice(
                             sSharesInstrumentId
                            , sSymbol
                            , sSecType
                            , sExchange
                            , sCurrency
                            , sPrimaryExch
                            , sEndDate
                            );

                        Thread.Sleep(5000);

                    }
                    catch
                    {
                        sMouthRaccoonMessageString = "mMouthGetHistoricalPrices: CATCH hit when running mMouthGetHistoricalPrice. Aborting method..";

                        nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                            , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "N");

                        sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;

                        return sMouthRaccoonValueToReturn;
                    }

                }

                dHistoricalDate = dHistoricalDate.AddDays(-1);
            }

            
            return sMouthRaccoonValueToReturn;
        }



            public static String mMouthGetHistoricalPrice(
                String sSharesInstrumentId
                ,String sSymbol
                ,String sSecType
                ,String sExchange
                ,String sCurrency
                ,String sPrimaryExch
                ,String sEndDate
            //, String sProgrammeArgumentAlpha
            //, String sProgrammeSection
            )
        {
            Int32 iSharesInstrumentId = -1;
            String sEndDateTimeUTC = "-1";

            //sSymbol = "VIX";
            //sSecType = "IND";
            //sCurrency = "USD";
            //sExchange = "SMART";    // "CFE";
            //sPrimaryExch = "CBOE";

            //sSymbol = "EMR";
            //sSecType = "STK";

            IBApi.Contract cvRaccoonMouthContract;

            try
            {
                sMouthRaccoonReturnValueReceived = cMouthRaccoon.mMouthBrokerContractGet(
                    out cvRaccoonMouthContract
                    , sSymbol
                    , sSecType
                    , sExchange
                    , sCurrency
                    , sPrimaryExch
                    );
            }
            catch
            {
                sMouthRaccoonMessageString = "mMouthGetHistoricalPrice: CATCH hit when running mMouthBrokerContractGet. Aborting method..";

                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "N");

                sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;

                return sMouthRaccoonValueToReturn;
            }


            try
            {
                Int32.TryParse(sSharesInstrumentId, out iSharesInstrumentId);
            }
            catch
            {
                sMouthRaccoonMessageString = "mMouthGetHistoricalPrice: Error converting InstrumentId string to int. Aborting method..";
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 2, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;

                return sMouthRaccoonValueToReturn;
            }


            try
            {
                List<TagValue> cvRaccoonMouthChartOptions = new List<TagValue>();

                ////sEndDateTimeUTC = "20170823 18:00:00 GMT";
                //sEndDateTimeUTC = sEndDate + " " + "14:00:00" + " GMT";

                //sMouthRaccoonMessageString = "mMouthGetHistoricalPrice: sEndDateTimeUTC is: " + sEndDateTimeUTC;
                //nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                //    , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                //sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;

                //cvMouthRaccoonEWrapper.fEarsEClientSocket.reqHistoricalData(
                //      iSharesInstrumentId
                //    , cvRaccoonMouthContract        //Contract
                //    , sEndDateTimeUTC       //string endDateTime
                //                            //, "1 Y"                         //string durationString
                //                            //, "1 hour"                      //string barSizeSetting
                //    , "1800 S"                         //string durationString
                //    , "1 secs"                      //string barSizeSetting
                //    , "TRADES"                    //string whatToShow
                //    , 1                             //int useRTH
                //    , 1                             //int formatDate
                //    , cvRaccoonMouthChartOptions    // < TagValue > chartOptions
                //    );

                //Thread.Sleep(10000);


                //sEndDateTimeUTC = sEndDate + " " + "09:30:00"; //+ " GMT";
                //sEndDateTimeUTC = "20170823 18:00:00 GMT";
                //sEndDateTimeUTC = "20170227 14:30:00 GMT";

                //Thread.Sleep(10000);

                //cvMouthRaccoonEWrapper.fEarsEClientSocket.cancelHistoricalData(iSharesInstrumentId);

                //Thread.Sleep(10000);
                
                sEndDateTimeUTC = sEndDate + " " + "14:00:00"; // + " GMT";

                sMouthRaccoonMessageString = "mMouthGetHistoricalPrice: sEndDateTimeUTC is: " + sEndDateTimeUTC;
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;


                cvMouthRaccoonEWrapper.fEarsEClientSocket.reqHistoricalData(
                      iSharesInstrumentId
                    , cvRaccoonMouthContract        //Contract
                    , sEndDateTimeUTC       //string endDateTime
                                            //, "1 Y"                         //string durationString
                                            //, "1 hour"                      //string barSizeSetting
                    , "1800 S"                         //string durationString
                                                       //, "1 D"                         //string durationString
                    , "5 secs"                      //string barSizeSetting
                    , "TRADES"                    //string whatToShow
                    , 1                             //int useRTH
                    , 1                             //int formatDate
                    , cvRaccoonMouthChartOptions    // < TagValue > chartOptions
                    );

                Thread.Sleep(10000);



                sEndDateTimeUTC = sEndDate + " " + "14:30:00"; // + " GMT";

                cvMouthRaccoonEWrapper.fEarsEClientSocket.reqHistoricalData(
                      iSharesInstrumentId
                    , cvRaccoonMouthContract        //Contract
                    , sEndDateTimeUTC       //string endDateTime
                                            //, "1 Y"                         //string durationString
                                            //, "1 hour"                      //string barSizeSetting
                    , "1800 S"                         //string durationString
                                                       //, "1 D"                         //string durationString
                    , "5 secs"                      //string barSizeSetting
                    , "TRADES"                    //string whatToShow
                    , 1                             //int useRTH
                    , 1                             //int formatDate
                    , cvRaccoonMouthChartOptions    // < TagValue > chartOptions
                    );

                Thread.Sleep(10000);


                sEndDateTimeUTC = sEndDate + " " + "15:00:00"; // + " GMT";

                cvMouthRaccoonEWrapper.fEarsEClientSocket.reqHistoricalData(
                      iSharesInstrumentId
                    , cvRaccoonMouthContract        //Contract
                    , sEndDateTimeUTC       //string endDateTime
                                            //, "1 Y"                         //string durationString
                                            //, "1 hour"                      //string barSizeSetting
                    , "1800 S"                         //string durationString
                    //, "1 D"                         //string durationString
                    , "5 secs"                      //string barSizeSetting
                    , "TRADES"                    //string whatToShow
                    , 1                             //int useRTH
                    , 1                             //int formatDate
                    , cvRaccoonMouthChartOptions    // < TagValue > chartOptions
                    );

                Thread.Sleep(10000);


                sEndDateTimeUTC = sEndDate + " " + "15:30:00"; // + " GMT";

                cvMouthRaccoonEWrapper.fEarsEClientSocket.reqHistoricalData(
                      iSharesInstrumentId
                    , cvRaccoonMouthContract        //Contract
                    , sEndDateTimeUTC       //string endDateTime
                                            //, "1 Y"                         //string durationString
                                            //, "1 hour"                      //string barSizeSetting
                    , "1800 S"                         //string durationString
                    , "5 secs"                      //string barSizeSetting
                    , "TRADES"                    //string whatToShow
                    , 1                             //int useRTH
                    , 1                             //int formatDate
                    , cvRaccoonMouthChartOptions    // < TagValue > chartOptions
                    );

                Thread.Sleep(10000);


                sEndDateTimeUTC = sEndDate + " " + "16:00:00"; // + " GMT";

                cvMouthRaccoonEWrapper.fEarsEClientSocket.reqHistoricalData(
                      iSharesInstrumentId
                    , cvRaccoonMouthContract        //Contract
                    , sEndDateTimeUTC       //string endDateTime
                                            //, "1 Y"                         //string durationString
                                            //, "1 hour"                      //string barSizeSetting
                    , "1800 S"                         //string durationString
                    , "5 secs"                      //string barSizeSetting
                    , "TRADES"                    //string whatToShow
                    , 1                             //int useRTH
                    , 1                             //int formatDate
                    , cvRaccoonMouthChartOptions    // < TagValue > chartOptions
                    );

                Thread.Sleep(10000);


                sEndDateTimeUTC = sEndDate + " " + "16:30:00"; // + " GMT";

                cvMouthRaccoonEWrapper.fEarsEClientSocket.reqHistoricalData(
                      iSharesInstrumentId
                    , cvRaccoonMouthContract        //Contract
                    , sEndDateTimeUTC       //string endDateTime
                                            //, "1 Y"                         //string durationString
                                            //, "1 hour"                      //string barSizeSetting
                    , "1800 S"                         //string durationString
                    , "5 secs"                      //string barSizeSetting
                    , "TRADES"                    //string whatToShow
                    , 1                             //int useRTH
                    , 1                             //int formatDate
                    , cvRaccoonMouthChartOptions    // < TagValue > chartOptions
                    );

                Thread.Sleep(10000);


                sEndDateTimeUTC = sEndDate + " " + "17:00:00"; // + " GMT";

                cvMouthRaccoonEWrapper.fEarsEClientSocket.reqHistoricalData(
                      iSharesInstrumentId
                    , cvRaccoonMouthContract        //Contract
                    , sEndDateTimeUTC       //string endDateTime
                                            //, "1 Y"                         //string durationString
                                            //, "1 hour"                      //string barSizeSetting
                    , "1800 S"                         //string durationString
                    , "5 secs"                      //string barSizeSetting
                    , "TRADES"                    //string whatToShow
                    , 1                             //int useRTH
                    , 1                             //int formatDate
                    , cvRaccoonMouthChartOptions    // < TagValue > chartOptions
                    );

                Thread.Sleep(10000);


                sEndDateTimeUTC = sEndDate + " " + "17:30:00"; // + " GMT";

                cvMouthRaccoonEWrapper.fEarsEClientSocket.reqHistoricalData(
                      iSharesInstrumentId
                    , cvRaccoonMouthContract        //Contract
                    , sEndDateTimeUTC       //string endDateTime
                                            //, "1 Y"                         //string durationString
                                            //, "1 hour"                      //string barSizeSetting
                    , "1800 S"                         //string durationString
                    , "5 secs"                      //string barSizeSetting
                    , "TRADES"                    //string whatToShow
                    , 1                             //int useRTH
                    , 1                             //int formatDate
                    , cvRaccoonMouthChartOptions    // < TagValue > chartOptions
                    );

                Thread.Sleep(10000);


                sEndDateTimeUTC = sEndDate + " " + "18:00:00"; // + " GMT";

                cvMouthRaccoonEWrapper.fEarsEClientSocket.reqHistoricalData(
                      iSharesInstrumentId
                    , cvRaccoonMouthContract        //Contract
                    , sEndDateTimeUTC       //string endDateTime
                                            //, "1 Y"                         //string durationString
                                            //, "1 hour"                      //string barSizeSetting
                    , "1800 S"                         //string durationString
                    , "5 secs"                      //string barSizeSetting
                    , "TRADES"                    //string whatToShow
                    , 1                             //int useRTH
                    , 1                             //int formatDate
                    , cvRaccoonMouthChartOptions    // < TagValue > chartOptions
                    );

                Thread.Sleep(10000);


                sEndDateTimeUTC = sEndDate + " " + "18:30:00"; // + " GMT";

                cvMouthRaccoonEWrapper.fEarsEClientSocket.reqHistoricalData(
                      iSharesInstrumentId
                    , cvRaccoonMouthContract        //Contract
                    , sEndDateTimeUTC       //string endDateTime
                                            //, "1 Y"                         //string durationString
                                            //, "1 hour"                      //string barSizeSetting
                    , "1800 S"                         //string durationString
                    , "5 secs"                      //string barSizeSetting
                    , "TRADES"                    //string whatToShow
                    , 1                             //int useRTH
                    , 1                             //int formatDate
                    , cvRaccoonMouthChartOptions    // < TagValue > chartOptions
                    );

                Thread.Sleep(10000);


                sEndDateTimeUTC = sEndDate + " " + "19:00:00"; // + " GMT";

                cvMouthRaccoonEWrapper.fEarsEClientSocket.reqHistoricalData(
                      iSharesInstrumentId
                    , cvRaccoonMouthContract        //Contract
                    , sEndDateTimeUTC       //string endDateTime
                                            //, "1 Y"                         //string durationString
                                            //, "1 hour"                      //string barSizeSetting
                    , "1800 S"                         //string durationString
                    , "5 secs"                      //string barSizeSetting
                    , "TRADES"                    //string whatToShow
                    , 1                             //int useRTH
                    , 1                             //int formatDate
                    , cvRaccoonMouthChartOptions    // < TagValue > chartOptions
                    );

                Thread.Sleep(10000);


                sEndDateTimeUTC = sEndDate + " " + "19:30:00"; // + " GMT";

                cvMouthRaccoonEWrapper.fEarsEClientSocket.reqHistoricalData(
                      iSharesInstrumentId
                    , cvRaccoonMouthContract        //Contract
                    , sEndDateTimeUTC       //string endDateTime
                                            //, "1 Y"                         //string durationString
                                            //, "1 hour"                      //string barSizeSetting
                    , "1800 S"                         //string durationString
                    , "5 secs"                      //string barSizeSetting
                    , "TRADES"                    //string whatToShow
                    , 1                             //int useRTH
                    , 1                             //int formatDate
                    , cvRaccoonMouthChartOptions    // < TagValue > chartOptions
                    );

                Thread.Sleep(10000);


                sEndDateTimeUTC = sEndDate + " " + "20:00:00"; // + " GMT";

                cvMouthRaccoonEWrapper.fEarsEClientSocket.reqHistoricalData(
                      iSharesInstrumentId
                    , cvRaccoonMouthContract        //Contract
                    , sEndDateTimeUTC       //string endDateTime
                                            //, "1 Y"                         //string durationString
                                            //, "1 hour"                      //string barSizeSetting
                    , "1800 S"                         //string durationString
                    , "5 secs"                      //string barSizeSetting
                    , "TRADES"                    //string whatToShow
                    , 1                             //int useRTH
                    , 1                             //int formatDate
                    , cvRaccoonMouthChartOptions    // < TagValue > chartOptions
                    );

                Thread.Sleep(10000);


                sEndDateTimeUTC = sEndDate + " " + "20:30:00"; // + " GMT";

                cvMouthRaccoonEWrapper.fEarsEClientSocket.reqHistoricalData(
                      iSharesInstrumentId
                    , cvRaccoonMouthContract        //Contract
                    , sEndDateTimeUTC       //string endDateTime
                                            //, "1 Y"                         //string durationString
                                            //, "1 hour"                      //string barSizeSetting
                    , "1800 S"                         //string durationString
                    , "5 secs"                      //string barSizeSetting
                    , "TRADES"                    //string whatToShow
                    , 1                             //int useRTH
                    , 1                             //int formatDate
                    , cvRaccoonMouthChartOptions    // < TagValue > chartOptions
                    );

                Thread.Sleep(10000);


                sEndDateTimeUTC = sEndDate + " " + "21:00:00"; // + " GMT";

                cvMouthRaccoonEWrapper.fEarsEClientSocket.reqHistoricalData(
                      iSharesInstrumentId
                    , cvRaccoonMouthContract        //Contract
                    , sEndDateTimeUTC       //string endDateTime
                                            //, "1 Y"                         //string durationString
                                            //, "1 hour"                      //string barSizeSetting
                    , "1800 S"                         //string durationString
                    , "5 secs"                      //string barSizeSetting
                    , "TRADES"                    //string whatToShow
                    , 1                             //int useRTH
                    , 1                             //int formatDate
                    , cvRaccoonMouthChartOptions    // < TagValue > chartOptions
                    );

                Thread.Sleep(10000);



                sEndDateTimeUTC = sEndDate + " " + "21:30:00"; // + " GMT";

                cvMouthRaccoonEWrapper.fEarsEClientSocket.reqHistoricalData(
                      iSharesInstrumentId
                    , cvRaccoonMouthContract        //Contract
                    , sEndDateTimeUTC       //string endDateTime
                                            //, "1 Y"                         //string durationString
                                            //, "1 hour"                      //string barSizeSetting
                    , "1800 S"                         //string durationString
                                                       //, "1 D"                         //string durationString
                    , "5 secs"                      //string barSizeSetting
                    , "TRADES"                    //string whatToShow
                    , 1                             //int useRTH
                    , 1                             //int formatDate
                    , cvRaccoonMouthChartOptions    // < TagValue > chartOptions
                    );

                Thread.Sleep(10000);

                
                sEndDateTimeUTC = sEndDate + " " + "22:00:00"; // + " GMT";

                cvMouthRaccoonEWrapper.fEarsEClientSocket.reqHistoricalData(
                      iSharesInstrumentId
                    , cvRaccoonMouthContract        //Contract
                    , sEndDateTimeUTC       //string endDateTime
                                            //, "1 Y"                         //string durationString
                                            //, "1 hour"                      //string barSizeSetting
                    , "1800 S"                         //string durationString
                                                       //, "1 D"                         //string durationString
                    , "5 secs"                      //string barSizeSetting
                    , "TRADES"                    //string whatToShow
                    , 1                             //int useRTH
                    , 1                             //int formatDate
                    , cvRaccoonMouthChartOptions    // < TagValue > chartOptions
                    );

                Thread.Sleep(20000);

                //cvMouthRaccoonEWrapper.fEarsEClientSocket.cancelHistoricalData(
                //    iSharesInstrumentId
                //    );
            }
            catch
            {
                sMouthRaccoonMessageString = "mMouthGetHistoricalPrice: reqHistoricalData. CATCH !!!!. Aborting method..";

                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "N");

                sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;

                return sMouthRaccoonValueToReturn;
            }

            Thread.Sleep(10000);

            sMouthRaccoonMessageString = "mMouthGetHistoricalPrice: Completed method ";
            nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

            sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;
            
            return sMouthRaccoonValueToReturn;
        }




        // Broker code:
        public static String mMouthBrokerMarketPricesRequest(
            String sSharesInstrumentId
            ,String sSymbol
            ,String sSecType
            ,String sExchange
            ,String sCurrency
            ,String sPrimaryExch    //Always "-1" to be ignored
            )
        {
            sMouthRaccoonValueToReturn = "-1";
            IBApi.Contract cvRaccoonMouthContract;

            try
            {
                sMouthRaccoonReturnValueReceived = cMouthRaccoon.mMouthBrokerContractGet(
                    out cvRaccoonMouthContract
                    , sSymbol
                    , sSecType
                    , sExchange
                    , sCurrency
                    , sPrimaryExch
                    );
            }
            catch
            {
                sMouthRaccoonMessageString = "mMouthBrokerMarketPricesRequest: CATCH hit when running mMouthBrokerContractGet. Aborting method..";

                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "N");

                sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;

                return sMouthRaccoonValueToReturn;
            }
            

            Int32 iSharesInstrumentId = -1;
                  
            sMouthRaccoonMessageString = "mMouthBrokerMarketPricesRequest: Starting..";
            nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "N");

            try
            {
                Int32.TryParse(sSharesInstrumentId, out iSharesInstrumentId);
            }
            catch
            {
                sMouthRaccoonMessageString = "mMouthBrokerMarketPricesRequest: Error converting InstrumentId string to int. Aborting method..";
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 2, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;

                return sMouthRaccoonValueToReturn;
            }
            

            try
            {
                if (cvMouthRaccoonClientSocket.IsConnected())
                {
                    //do nothing
                }
                else
                {
                    sMouthRaccoonMessageString = "mMouthBrokerMarketPricesRequest: RaccoonMouth not connected to Broker. Aborting..";
                    nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                        , sMouthWhatTypeOfLogging, 2, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                    sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;

                    return sMouthRaccoonValueToReturn;
                }
            }
            catch
            {
                sMouthRaccoonMessageString = "mMouthBrokerMarketPricesRequest: !!!! Catch entered when running cvMouthRaccoonClientSocket.IsConnected !!!!";
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 1, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;

                return sMouthRaccoonValueToReturn;
            }


            try
            {
                List<TagValue> cvRaccoonMouthMarketDataOptions = new List<TagValue>();

                cvMouthRaccoonEWrapper.fEarsEClientSocket.reqMktData(iSharesInstrumentId, cvRaccoonMouthContract, ""
                    , true, cvRaccoonMouthMarketDataOptions);

                sMouthRaccoonValueToReturn = "0";
            }
            catch
            {
                sMouthRaccoonMessageString = "mMouthBrokerMarketPricesRequest: !!!! " +
                    "Catch entered when running cvMouthRaccoonEWrapper.fEarsEClientSocket.reqMktData !!!!";
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 1, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;

                return sMouthRaccoonValueToReturn;
            }

            return sMouthRaccoonValueToReturn;
        }

        

        public static String mMouthBrokerOrderPlace
            (
            String sNextValidIdentifier
            , String sContractSymbol
            , String sContractSecType
            , String sContractExchange
            , String sContractCurrency
            , String sOrderType
            , String sOrderAction
            , String sOrderQuantity
            , String sOrderLimitPrice
            , String sUseOneCancelsAll
            , String sOcaGroup
            , String sAllOrNone
            )
        {
            sMouthRaccoonValueToReturn = "-1";
            sMouthRaccoonMessageString = "-1";

            try
            {
                if (cvMouthRaccoonClientSocket.IsConnected())
                {
                    //do nothing
                }
                else
                {
                    sMouthRaccoonMessageString = "mMouthBrokerOrderPlace: !!!! Catch statement entered !!!! " +
                        "Raccoon reports not connected to broker. Command not run";
                  nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                        , sMouthWhatTypeOfLogging, 1, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                    sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;
                    return sMouthRaccoonValueToReturn;
                }
            }
            catch
            {
                sMouthRaccoonMessageString = "mMouthBrokerOrderPlace: !!!! Catch statement entered on checking IsConnected !!!! ";
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 1, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;
                return sMouthRaccoonValueToReturn;
            }


            nsLog.cLogMessage.mLogMessage("mMouthBrokerOrderPlace: Placing Limit Order.."
                , sMouthWhatTypeOfLogging, 3, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

            try
            {
                Int32 iNextValidIdentifier = Convert.ToInt32(sNextValidIdentifier);
                Int32 iOrderQuantity = Convert.ToInt32(sOrderQuantity);
                Double dOrderLimitPrice = Convert.ToDouble(sOrderLimitPrice);

                Contract oContract = new Contract();
                oContract.Symbol = sContractSymbol;         // "EUR";
                oContract.SecType = sContractSecType;       // "CASH";
                oContract.Currency = sContractCurrency;     // "GBP";
                oContract.Exchange = sContractExchange;     // "IDEALPRO";

                Order oOrder = new Order();
                oOrder.Action = sOrderAction;       //"BUY", "SELL"
                oOrder.OrderType = sOrderType;     //"LMT", "MKT";
                oOrder.TotalQuantity = iOrderQuantity;
                oOrder.Hidden = true;
                //oOrder.Tif = "GTC";

                if (sOrderType == "LMT")
                {
                    oOrder.LmtPrice = dOrderLimitPrice; //no need to provide a price for a market order
                }

                if (sAllOrNone == "True")
                {
                    oOrder.AllOrNone = true;      //A trade to fill all of the order not just some of the order, or nothing.
                }
                else
                {
                    oOrder.AllOrNone = false;
                }

                if (sUseOneCancelsAll == "yes")
                {
                    oOrder.OcaGroup = sOcaGroup;  //One (trade) cancels all (orders).
                    oOrder.OcaType = 1;           //Downside of this is that I can't cancel individual orders mid episode 
                    //without cancelling all of the orders.
                    //oOrder.AllOrNone = true;      //A trade to fill all of the order not just some of the order, or nothing.
                }

                cvMouthRaccoonClientSocket.placeOrder(iNextValidIdentifier, oContract, oOrder);       
            }
            catch
            {
                sMouthRaccoonMessageString = "mMouthBrokerOrderPlace: !!!! Catch statement entered on placeOrder !!!!";
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 1, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;
                return sMouthRaccoonValueToReturn;
            }

            nsLog.cLogMessage.mLogMessage("mMouthBrokerOrderPlace: Order Placed"
                , sMouthWhatTypeOfLogging, 3, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "N");

            sMouthRaccoonValueToReturn = "0";
            return sMouthRaccoonValueToReturn;
        }

        

        public static String mMouthBrokerOrderCancel
            (
            String sOrderToClose
            )
        {
            sMouthRaccoonValueToReturn = "-1";
            sMouthRaccoonMessageString = "-1";
            Int32 iOrderToClose = -1;

            try
            {
                if (cvMouthRaccoonClientSocket.IsConnected())
                {
                    //do nothing
                }
                else
                {
                    sMouthRaccoonMessageString = "mMouthBrokerOrderCancel: !!!! Catch statement entered !!!! " +
                        "Raccoon reports not connected to broker. Command not run";
                    nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                          , sMouthWhatTypeOfLogging, 1, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                    sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;
                    return sMouthRaccoonValueToReturn;
                }
            }
            catch
            {
                sMouthRaccoonMessageString = "mMouthBrokerOrderCancel: !!!! Catch statement entered on checking IsConnected !!!! ";
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 1, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;
                return sMouthRaccoonValueToReturn;
            }

            nsLog.cLogMessage.mLogMessage("mMouthBrokerOrderCancel: Cancelling Order Id: " + sOrderToClose
                , sMouthWhatTypeOfLogging, 3, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

            try
            {
                iOrderToClose = Convert.ToInt32(sOrderToClose);

                cvMouthRaccoonClientSocket.cancelOrder(iOrderToClose);
            }
            catch
            {
                sMouthRaccoonMessageString = "mMouthBrokerOrderCancel: !!!! Catch statement entered on cancelOrder !!!!";
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 1, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;
                return sMouthRaccoonValueToReturn;
            }

            nsLog.cLogMessage.mLogMessage("mMouthBrokerOrderCancel: Order Cancelled"
                , sMouthWhatTypeOfLogging, 3, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "N");

            sMouthRaccoonValueToReturn = "0";
            return sMouthRaccoonValueToReturn;
        }

        

        public static String mMouthBrokerContractGet(
            out IBApi.Contract cvRaccoonMouthContract
            , String sSymbol
            , String sSecType
            , String sExchange
            , String sCurrency
            , String sPrimaryExch
            )
        {
            sMouthRaccoonValueToReturn = "mMouthBrokerContractGet: Failed";

            try
            {
                sMouthRaccoonMessageString = "mMouthBrokerContractGet: Parameters: "
                    + sMouthCrLf + "sSymbol: " + sSymbol
                    + sMouthCrLf + "sSecType: " + sSecType
                    + sMouthCrLf + "sExchange: " + sExchange
                    + sMouthCrLf + "sCurrency: " + sCurrency
                    + sMouthCrLf + "sPrimaryExch: " + sPrimaryExch;

                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "N");
            }
            catch
            {
                sMouthRaccoonMessageString = "mMouthBrokerContractGet: !!!! Catch entered when logging input parameters !!!!";
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 1, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;

                cvRaccoonMouthContract = null;
                return sMouthRaccoonValueToReturn;
            }


            IBApi.Contract cvRaccoonMouthContractLocal = new IBApi.Contract();
            //Note this cannot go into a try-catch block


            try
            {
                //IBApi.Contract cvRaccoonMouthContract = new Contract();
                // Fill in the Contract properties
                cvRaccoonMouthContractLocal.Symbol = sSymbol;     //"EUR";
                cvRaccoonMouthContractLocal.SecType = sSecType;     //"CASH";
                cvRaccoonMouthContractLocal.Exchange = sExchange;     //"IDEALPRO";
                cvRaccoonMouthContractLocal.Currency = sCurrency;     //"USD";

                //if (sPrimaryExch != "-1")
                //{
                //    cvRaccoonMouthContractLocal.PrimaryExch = "CBOE";
                //}
                
                sMouthRaccoonValueToReturn = "0";
            }
            catch
            {
                sMouthRaccoonMessageString = "mMouthBrokerContractGet: !!!! Catch statement entered !!!! ";
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                   , sMouthWhatTypeOfLogging, 1, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;
            }

            cvRaccoonMouthContract = cvRaccoonMouthContractLocal;

            return sMouthRaccoonValueToReturn;
        }

        


        //BrokerConnectionEnvironment start/stop code below:
        public static String mMouthBrokerConnectionEnvironmentStartup()
        {
            String sMouthBrokerIPPort = "-1";
                        

            sMouthRaccoonMessageString = "mMouthBrokerConnectionEnvironmentStartup: Calling mMouthSqlTurnstileRaccoonConnect()..";
            nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

            try
            {
                sMouthRaccoonReturnValueReceived = nsMouthSqlRaccoon.cMouthSqlRaccoon.mMouthSqlTurnstileRaccoonConnect();

                sMouthRaccoonMessageString = "mMouthBrokerConnectionEnvironmentStartup: Calling mMouthBrokerRaccoonConnect()..";
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                if (sMouthEnvironmentProOrDev == "Dev" && sMouthBrokerTwsOrIbGateway == "Tws")
                { sMouthBrokerIPPort = sMouthBrokerIPPortTwsDev; }

                if (sMouthEnvironmentProOrDev == "Pro" && sMouthBrokerTwsOrIbGateway == "Tws")
                { sMouthBrokerIPPort = sMouthBrokerIPPortTwsPro; }

                if (sMouthEnvironmentProOrDev == "Dev" && sMouthBrokerTwsOrIbGateway == "IbGateway")
                { sMouthBrokerIPPort = sMouthBrokerIPPortIbGatewayDev; }

                if (sMouthEnvironmentProOrDev == "Pro" && sMouthBrokerTwsOrIbGateway == "IbGateway")
                { sMouthBrokerIPPort = sMouthBrokerIPPortIbGatewayPro; }

                sMouthRaccoonMessageString = "mMouthBrokerConnectionEnvironmentStartup: Connecting to Broker, on port: " + sMouthBrokerIPPort;
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                sMouthRaccoonReturnValueReceived = cMouthRaccoon.mMouthBrokerRaccoonConnect(sMouthBrokerIPAddress, sMouthBrokerIPPort);

                sMouthRaccoonMessageString = "mMouthBrokerConnectionEnvironmentStartup: Calling mMouthSqlTurnstilePriceArrivalsTurnstile() to enable..";
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                sMouthRaccoonReturnValueReceived = nsMouthSqlRaccoon.cMouthSqlRaccoon.mMouthSqlTurnstilePriceArrivalsTurnstile(
                    out sMouthRaccoonReturnValueReceived
                    , "TurnstileEnable");

                sMouthRaccoonMessageString = "mMouthBrokerConnectionEnvironmentStartup: mMouthSqlTurnstilePriceArrivalsTurnstile()"
                    + sMouthCrLf + "output is:" + sMouthRaccoonReturnValueReceived;
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");
            }
            catch
            {
                sMouthRaccoonMessageString = "mMouthBrokerConnectionEnvironmentStartup: !!!! Catch statement entered " +
                    "on mMouthSqlTurnstileRaccoonConnect or mMouthBrokerRaccoonConnect or mMouthSqlTurnstilePriceArrivalsTurnstile";
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                   , sMouthWhatTypeOfLogging, 1, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;
            }
            
            return sMouthRaccoonValueToReturn;
        }

        

        public static String mMouthBrokerConnectionEnvironmentShutdown()
        {

            try
            {
                sMouthRaccoonMessageString = "mMouthBrokerConnectionEnvironmentShutdown: Calling mMouthSqlTurnstilePriceArrivalsTurnstile() to Disable..";
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                sMouthRaccoonReturnValueReceived = nsMouthSqlRaccoon.cMouthSqlRaccoon.mMouthSqlTurnstilePriceArrivalsTurnstile(
                    out sMouthRaccoonReturnValueReceived
                    , "TurnstileDisable");


                sMouthRaccoonMessageString = "mMouthBrokerConnectionEnvironmentShutdown: Calling mMouthBrokerRaccoonDisConnect()..";
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                sMouthRaccoonReturnValueReceived = cMouthRaccoon.mMouthBrokerRaccoonDisConnect();


                sMouthRaccoonMessageString = "mMouthBrokerConnectionEnvironmentShutdown: Calling mMouthSqlTurnstileRaccoonDisConnect()..";
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                sMouthRaccoonReturnValueReceived = nsMouthSqlRaccoon.cMouthSqlRaccoon.mMouthSqlTurnstileRaccoonDisConnect();
            }
            catch
            {
                sMouthRaccoonMessageString = "mMouthBrokerConnectionEnvironmentShutdown: !!!! Catch statement entered " +
                    "on mMouthSqlTurnstileRaccoonConnect or mMouthBrokerRaccoonConnect or mMouthSqlTurnstilePriceArrivalsTurnstile";
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                   , sMouthWhatTypeOfLogging, 1, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;
            }

            
            return sMouthRaccoonReturnValueReceived;
        }

        

        public static String mMouthBrokerRaccoonConnect(
            String sMouthBrokerIPAddress
            , String sMouthBrokerIPPort
            )
        {
            sMouthRaccoonValueToReturn = "MouthRaccoonReportsConnectionToBrokerApplicationFailed";    //assume connection attempt fails at first

            nsLog.cLogMessage.mLogMessage("mMouthBrokerRaccoonConnect: Connecting to broker.."
                , sMouthWhatTypeOfLogging, 3, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

            try
            {
                Int32 iMouthBrokerIPPort = Convert.ToInt32(sMouthBrokerIPPort);
                cvMouthRaccoonClientSocket.eConnect(sMouthBrokerIPAddress, iMouthBrokerIPPort, 0);
            }
            catch
            {
                nsLog.cLogMessage.mLogMessage("mMouthBrokerRaccoonConnect: !!!! Catch statement entered when calling method eConnect !!!! "
                , sMouthWhatTypeOfLogging, 1, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");
            }

            if (cvMouthRaccoonClientSocket.IsConnected())
            {
                try
                {
                    var cvMouthRaccoonReader = new EReader(cvMouthRaccoonClientSocket, cvMouthRaccoonReaderSignal);
                    cvMouthRaccoonReader.Start();
                    
                    Thread tMouthRaccoon = new Thread
                    (
                        () =>
                        {
                            nsLog.cLogMessage.mLogMessage("mMouthBrokerRaccoonConnect: tMouthRaccoonThread thread Starting.."
                                , sMouthWhatTypeOfLogging, 4, iMouthMessagePriorityToAction, "ears", sMouthLogFile, "N");

                            while (cvMouthRaccoonClientSocket.IsConnected())
                            {
                                cvMouthRaccoonReaderSignal.waitForSignal();
                                cvMouthRaccoonReader.processMsgs();
                            }

                            nsLog.cLogMessage.mLogMessage("mMouthBrokerRaccoonConnect: tMouthRaccoonThread thread Closing "
                                + sMouthCrLf + "(this is normal behaviour if it occurs during mMouthBrokerRaccoonDisConnect).."
                                , sMouthWhatTypeOfLogging, 3, iMouthMessagePriorityToAction, "ears", sMouthLogFile, "N");
                        }
                    );

                    tMouthRaccoon.Start();

                    nsLog.cLogMessage.mLogMessage("mMouthBrokerRaccoonConnect: Started RaccoonEars broker listening thread"
                        , sMouthWhatTypeOfLogging, 3, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "N");
                    nsLog.cLogMessage.mLogMessage("mMouthBrokerRaccoonConnect: Completed connection to broker"
                        , sMouthWhatTypeOfLogging, 3, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "N");

                    sMouthRaccoonValueToReturn = "0";
                }
                catch
                {
                    nsLog.cLogMessage.mLogMessage("mMouthBrokerRaccoonConnect: !!!! Catch statement entered on cvMouthRaccoonReader " +
                        "start or thread start !!!! ", sMouthWhatTypeOfLogging, 1, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");
                }

            }
            else
            {
                nsLog.cLogMessage.mLogMessage("mMouthBrokerRaccoonConnect: Connect attempt failed. Perhaps the Trader Workstation app" +
                    ", or IB Gateway app is not running"
                , sMouthWhatTypeOfLogging, 1, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");
            }

            return sMouthRaccoonValueToReturn;
        }



        public static String mMouthBrokerRaccoonDisConnect()
        {
            sMouthRaccoonValueToReturn = "MouthRaccoonReportsDisConnectionFromBrokerApplicationFailed";

            try
            {
                if (cvMouthRaccoonClientSocket.IsConnected())
                {
                    //do nothing
                }
                else
                {
                    sMouthRaccoonMessageString = "mMouthBrokerRaccoonDisConnect: MouthRaccoon reports not connected to broker,"
                        + sMouthCrLf + "so no need to attempt a disconnect";
                    nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                        , sMouthWhatTypeOfLogging, 3, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                    sMouthRaccoonValueToReturn = sMouthRaccoonMessageString;

                    return sMouthRaccoonValueToReturn;
                }
            }
            catch
            {
                sMouthRaccoonMessageString = "mMouthBrokerRaccoonDisConnect: !!!! Catch statement entered on checking IsConnected !!!! ";
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 1, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");
            }

            sMouthRaccoonMessageString = "mMouthBrokerRaccoonDisConnect: Disconnecting MouthRaccoon from broker..";
            nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                , sMouthWhatTypeOfLogging, 3, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

            try
            {
                cvMouthRaccoonClientSocket.eDisconnect();
                cvMouthRaccoonClientSocket.Close();

                if (cvMouthRaccoonClientSocket.IsConnected())
                {
                    sMouthRaccoonMessageString = "mMouthBrokerRaccoonDisConnect: Disconnection failed";
                    nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                        , sMouthWhatTypeOfLogging, 1, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "N");
                }
                else
                {
                    //Disconnection successful
                    sMouthRaccoonMessageString = "mMouthBrokerRaccoonDisConnect: Disconnection complete";
                    nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                        , sMouthWhatTypeOfLogging, 3, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "N");

                    sMouthRaccoonValueToReturn = "0";
                }

            }
            catch
            {
                sMouthRaccoonMessageString = "mMouthBrokerRaccoonDisConnect: !!!! Catch statement entered !!!! ";
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 1, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");
            }

            return sMouthRaccoonValueToReturn;
        }


        
        public static String mMouthSqlMainRaccoonConnect()
        {
            sMouthRaccoonValueToReturn = "mMouthSqlMainRaccoonConnectFailed";    //assume connection attempt fails at first

            sMouthRaccoonMessageString = "mMouthSqlMainRaccoonConnect: Connecting MouthRaccoon to SQL Server..";
            nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                , sMouthWhatTypeOfLogging, 3, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

            try
            {
                if (nsMouthSqlRaccoon.cMouthSqlRaccoon.cvMouthSqlMainConnection.State.ToString() == "Open")
                {
                    sMouthRaccoonValueToReturn = "mMouthSqlMainRaccoonDisConnect: MouthRaccoon connection to SQL Server"
                        + sMouthCrLf + "is already in state: Open. So Connection has not been attempted.";

                    return sMouthRaccoonValueToReturn;
                }
                else
                {
                    //do nothing, and continue on to connect command
                }
            }
            catch
            {
                sMouthRaccoonMessageString = "mMouthSqlMainRaccoonConnect: !!!! Catch statement entered on checking SQL connection state";
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString, sMouthWhatTypeOfLogging, 1
                    , iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");
            }


            try
            {
                nsMouthSqlRaccoon.cMouthSqlRaccoon.cvMouthSqlMainConnection.Open();

                if (nsMouthSqlRaccoon.cMouthSqlRaccoon.cvMouthSqlMainConnection.State.ToString() == "Open")
                {
                    sMouthRaccoonValueToReturn = "0";
                }
                else
                {
                    sMouthRaccoonValueToReturn = "mMouthSqlMainRaccoonConnect: Connection to SQL Server failed";
                }
            }
            catch
            {
                sMouthRaccoonMessageString = "mMouthEpisode: !!!! Catch statement entered on connecting to SQL Server ";
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 1, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");
            }
            
            return sMouthRaccoonValueToReturn;
        }



        public static String mMouthSqlMainRaccoonDisConnect()
        {
            sMouthRaccoonValueToReturn = "MouthRaccoonReportsmMouthSqlMainRaccoonDisConnectFailed";
            
            try
            {
                if (nsMouthSqlRaccoon.cMouthSqlRaccoon.cvMouthSqlMainConnection.State.ToString() == "Closed")
                {
                    sMouthRaccoonValueToReturn = "mMouthSqlMainRaccoonDisConnect: MouthRaccoon connection to SQL Server"
                        + sMouthCrLf + "is already in state: Closed. So disconnection has not been attempted.";

                    nsLog.cLogMessage.mLogMessage(sMouthRaccoonValueToReturn
                        , sMouthWhatTypeOfLogging, 2, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                    return sMouthRaccoonValueToReturn;
                }
                else
                {
                    //do nothing, and continue on to disconnect command
                }
            }
            catch
            {
                nsLog.cLogMessage.mLogMessage("mMouthSqlMainRaccoonDisConnect: !!!! Catch statement entered on checking SQL " +
                    "connection state", sMouthWhatTypeOfLogging, 1, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");
            }


            try
            {
                nsMouthSqlRaccoon.cMouthSqlRaccoon.cvMouthSqlMainConnection.Close();

                if (nsMouthSqlRaccoon.cMouthSqlRaccoon.cvMouthSqlMainConnection.State.ToString() == "Open")
                {
                    sMouthRaccoonValueToReturn = "mMouthSqlMainRaccoonDisConnect: DisConnection from SQL Server failed";
                }
                else
                {
                    sMouthRaccoonValueToReturn = "0";
                }
            }
            catch
            {
                nsLog.cLogMessage.mLogMessage("mMouthSqlMainRaccoonDisConnect: !!!! Catch statement entered on disconnecting from SQL Server "
                    , sMouthWhatTypeOfLogging, 1, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");
            }

            return sMouthRaccoonValueToReturn;
        }

        
        
        public static String mMouthSqlTurnstileRaccoonConnect()
        {
            sMouthRaccoonValueToReturn = "mMouthSqlTurnstileRaccoonConnectFailed";    //assume connection attempt fails at first

            sMouthRaccoonMessageString = "mMouthSqlTurnstileRaccoonConnect: Connecting MouthRaccoon to SQL Server..";
            nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                , sMouthWhatTypeOfLogging, 3, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

            try
            {
                if (nsMouthSqlRaccoon.cMouthSqlRaccoon.cvMouthSqlTurnstileConnection.State.ToString() == "Open")
                {
                    sMouthRaccoonValueToReturn = "mMouthSqlTurnstileRaccoonConnect: MouthRaccoon connection to SQL Server"
                        + sMouthCrLf + "is already in state: Open. So Connection has not been attempted.";

                    return sMouthRaccoonValueToReturn;
                }
                else
                {
                    //do nothing, and continue on to connect command
                }
            }
            catch
            {
                sMouthRaccoonMessageString = "mMouthSqlTurnstileRaccoonConnect: !!!! Catch statement entered on checking SQL connection state";
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString, sMouthWhatTypeOfLogging, 1
                    , iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");
            }


            try
            {
                nsMouthSqlRaccoon.cMouthSqlRaccoon.cvMouthSqlTurnstileConnection.Open();

                if (nsMouthSqlRaccoon.cMouthSqlRaccoon.cvMouthSqlTurnstileConnection.State.ToString() == "Open")
                {
                    sMouthRaccoonValueToReturn = "0";
                }
                else
                {
                    sMouthRaccoonValueToReturn = "mMouthSqlTurnstileRaccoonConnect: Connection to SQL Server failed";
                }
            }
            catch
            {
                sMouthRaccoonMessageString = "mMouthEpisode: !!!! Catch statement entered on connecting to SQL Server ";
                nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                    , sMouthWhatTypeOfLogging, 1, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");
            }

            return sMouthRaccoonValueToReturn;
        }



        public static String mMouthSqlTurnstileRaccoonDisConnect()
        {
            sMouthRaccoonValueToReturn = "MouthRaccoonReportsmMouthSqlTurnstileRaccoonDisConnectFailed";
            
            try
            {
                if (nsMouthSqlRaccoon.cMouthSqlRaccoon.cvMouthSqlTurnstileConnection.State.ToString() == "Closed")
                {
                    sMouthRaccoonValueToReturn = "mMouthSqlTurnstileRaccoonDisConnect: MouthRaccoon connection to SQL Server"
                        + sMouthCrLf + "is already in state: Closed. So disconnection has not been attempted.";

                    nsLog.cLogMessage.mLogMessage(sMouthRaccoonValueToReturn
                        , sMouthWhatTypeOfLogging, 2, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

                    return sMouthRaccoonValueToReturn;
                }
                else
                {
                    //do nothing, and continue on to disconnect command
                }
            }
            catch
            {
                nsLog.cLogMessage.mLogMessage("mMouthSqlTurnstileRaccoonDisConnect: !!!! Catch statement entered on checking SQL " +
                    "connection state", sMouthWhatTypeOfLogging, 1, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");
            }


            try
            {
                nsMouthSqlRaccoon.cMouthSqlRaccoon.cvMouthSqlTurnstileConnection.Close();

                if (nsMouthSqlRaccoon.cMouthSqlRaccoon.cvMouthSqlTurnstileConnection.State.ToString() == "Open")
                {
                    sMouthRaccoonValueToReturn = "mMouthSqlTurnstileRaccoonDisConnect: DisConnection from SQL Server failed";
                }
                else
                {
                    sMouthRaccoonValueToReturn = "0";
                }
            }
            catch
            {
                nsLog.cLogMessage.mLogMessage("mMouthSqlTurnstileRaccoonDisConnect: !!!! Catch statement entered on disconnecting from SQL Server "
                    , sMouthWhatTypeOfLogging, 1, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");
            }
            
            return sMouthRaccoonValueToReturn;
        }



        //Test and obsolete code..
        public static String mMouthTest(
            String sMouthTestParameter1
            , String sMouthTestParameter2
            , String sMouthTestParameter3
            , String sMouthTestParameter4
            )
        {
            sMouthRaccoonMessageString = "mMouthTest: MouthRaccoonReportsMouthTestSuccessful..";
            nsLog.cLogMessage.mLogMessage(sMouthRaccoonMessageString
                , sMouthWhatTypeOfLogging, 3, iMouthMessagePriorityToAction, "mouth", sMouthLogFile, "BA");

            sMouthRaccoonValueToReturn = "0: " +
                "Parameter 1 is: " + sMouthTestParameter1 + ", " +
                "Parameter 2 is: " + sMouthTestParameter2 + ", " +
                "Parameter 3 is: " + sMouthTestParameter3 + ", " +
                "Parameter 4 is: " + sMouthTestParameter4 + ". " +
                "Are input values returned from MouthRaccoon. MouthRaccoonReportsmMouthTestCompletedSuccessfully. ";

            return sMouthRaccoonValueToReturn;
        }

        
    }
}