示例#1
0
        internal void updateCQGDataStatus(String dataStatus, Color backColor, Color foreColor)
        {
#if DEBUG
            try
#endif
            {
                if (this.InvokeRequired)
                {
                    this.BeginInvoke((MethodInvoker) delegate()
                    {
                        this.dataStatus.ForeColor = foreColor;
                        this.dataStatus.BackColor = backColor;
                        this.dataStatus.Text      = dataStatus;
                    });
                }
                else
                {
                    this.dataStatus.ForeColor = foreColor;
                    this.dataStatus.BackColor = backColor;
                    this.dataStatus.Text      = dataStatus;
                }
            }
#if DEBUG
            catch (Exception ex)
            {
                TSErrorCatch.errorCatchOut(Convert.ToString(this), ex);
            }
#endif
        }
示例#2
0
        // The cumulative normal distribution function
        public static double CND(double X)
        {
            try
            {
                double L, K, w;
                double a1 = 0.31938153,
                       a2 = -0.356563782,
                       a3 = 1.781477937,
                       a4 = -1.821255978,
                       a5 = 1.330274429;

                L = Math.Abs(X);
                K = 1.0 / (1.0 + 0.2316419 * L);
                w = 1.0 - 1.0 / Math.Sqrt(2.0 * Math.PI) * Math.Exp(-L * L / 2) * (a1 * K + a2 * K * K + a3
                                                                                   * Math.Pow(K, 3) + a4 * Math.Pow(K, 4) + a5 * Math.Pow(K, 5));

                if (X < 0.0)
                {
                    w = 1.0 - w;
                }
                return(w);
            }
            catch (Exception ex)
            {
                TSErrorCatch.errorCatchOut("OptionCalcs", ex);
            }

            return(0);
        }
示例#3
0
        // The Black and Scholes (1973) Stock option formula
        public static double blackScholes(char CallPutFlag, double S, double X, double T, double r, double v)
        {
            try
            {
                if (T == 0)
                {
                    T = 0.0001;
                }

                double d1, d2;

                d1 = (Math.Log(S / X) + (r + v * v / 2) * T) / (v * Math.Sqrt(T));
                d2 = d1 - v * Math.Sqrt(T);

                if (CallPutFlag == 'c' || CallPutFlag == 'C')
                {
                    return(S * CND(d1) - X * Math.Exp(-r * T) * CND(d2));
                }
                else
                {
                    return(X * Math.Exp(-r * T) * CND(-d2) - S * CND(-d1));
                }
            }
            catch (Exception ex)
            {
                TSErrorCatch.errorCatchOut("OptionCalcs", ex);
            }

            return(0);
        }
示例#4
0
        internal void initializeCQGAndCallbacks(Object obj)
        {
            try
            {
                // Create real CQGCEL object and put it into the dictionary
                // Remark: we do not use "m_CEL = new CQG.CQGCEL();" to facilitate further reflection on this COM object
                string typeName = "CQG.CQGCELClass";
                m_CEL = (CQGCEL)CQGAssm.CreateInstance(typeName);

                m_CEL_key = FakeCQG.Internal.Core.CreateUniqueKey();
                FakeCQG.Internal.ServerDictionaries.PutObjectToTheDictionary(m_CEL_key, m_CEL);
                QueryHandler.UsedObjs.Add(m_CEL_key, m_CEL);

                m_CEL_CELDataConnectionChg(eConnectionStatus.csConnectionDown);

                m_CEL.DataConnectionStatusChanged += new _ICQGCELEvents_DataConnectionStatusChangedEventHandler(m_CEL_CELDataConnectionChg);

                m_CEL.DataError += new _ICQGCELEvents_DataErrorEventHandler(m_CEL_DataError);

                m_CEL.APIConfiguration.ReadyStatusCheck = eReadyStatusCheck.rscOff;

                m_CEL.APIConfiguration.CollectionsThrowException = false;

                m_CEL.APIConfiguration.TimeZoneCode = eTimeZone.tzPacific;

                connectCQG();
            }
            catch (Exception ex)
            {
                TSErrorCatch.errorCatchOut(Convert.ToString(this), ex);
            }
        }
示例#5
0
        public static double ND(double X)
        {
            try
            {
                return(Math.Exp(-X * X / 2) / Math.Sqrt(2 * Math.PI));
            }
            catch (Exception ex)
            {
                TSErrorCatch.errorCatchOut("OptionCalcs", ex);
            }

            return(0);
        }
示例#6
0
 private void m_CEL_DataError(System.Object cqg_error, System.String error_description)
 {
     try
     {
         if (mainForm != null)
         {
             mainForm.UpdateCQGDataStatus(
                 "CQG ERROR", Color.Yellow, Color.Red);
         }
     }
     catch (Exception ex)
     {
         TSErrorCatch.errorCatchOut(Convert.ToString(this), ex);
     }
 }
示例#7
0
        // The generalized Black and Scholes formula
        //
        // S - stock price
        // X - strike price of option
        // r - risk-free interest rate
        // T - time to expiration in years
        // v - volatility of the relative price change of the underlying stock price
        // b - cost-of-carry
        //     b = r        --> Black-Scholes stock option model
        //     b = 0        --> Black futures option model
        //     b = r-q      --> Merton stock option model with continuous dividend yield q
        //     b = r - r(f) --> Garman-Kohlhagen currency option model, where r(f) is the risk-free rate of the foreign currency
        //
        // Examples:
        // a) currency option
        //    T = 0.5, 6 month to expiry
        //    S = 1.56, USD/DEM exchange rate is 1.56
        //    X = 1.6,  strike is 1.60
        //    r = 0.06, domestic interest rate in Germany is 6% per annum
        //    r(f) = 0.08, foreign risk-free interest rate in the U.S. is 8% per annum
        //    v = 0.12, volatility is 12% per annum
        //    c = 0.0291

        public static double calculateOptionVolatility(char callPutFlag, double S, double X, double T, double r, double currentOptionPrice)
        {
            double tempV = 0.5;

            try
            {
                if (T == 0)
                {
                    T = 0.0001;
                }

                int i = 0;
                //double tempV = 0.1;
                double volInc = 0.5;
                double priceTest;
                //double diffBtwnTempPriceAndTruePrice = 999999;
                int maxIter = 100;

                while (i < maxIter)
                {
                    priceTest = blackScholes(callPutFlag, S, X, T, r, tempV);

                    if (Math.Abs(currentOptionPrice - priceTest) < 0.01)
                    {
                        return(tempV);
                    }
                    else if (priceTest - currentOptionPrice > 0)
                    {
                        volInc /= 2;
                        tempV  -= volInc;
                    }
                    else
                    {
                        volInc /= 2;
                        tempV  += volInc;
                    }

                    i++;
                }
            }
            catch (Exception ex)
            {
                TSErrorCatch.errorCatchOut("OptionCalcs", ex);
            }

            return(tempV);
        }
示例#8
0
        // Vega for the generalized Black and Scholes formula
        public static double gVega(double S, double X, double T, double r, double b, double v)
        {
            try
            {
                if (T == 0)
                {
                    T = 0.0001;
                }

                double d1 = (Math.Log(S / X) + (b + Math.Pow(v, 2) / 2) * T) / (v * Math.Sqrt(T));
                return(S * Math.Exp((b - r) * T) * ND(d1) * Math.Sqrt(T));
            }
            catch (Exception ex)
            {
                TSErrorCatch.errorCatchOut("OptionCalcs", ex);
            }

            return(0);
        }
示例#9
0
        public static double calculateOptionVolatilityNRCalc(char callPutFlag, double S, double X, double T, double r, double currentOptionPrice,
                                                             double epsilon)
        {
            double vi = 0, ci, vegai, prevVi = 0;
            double b = 0; //for futures b = 0;

            try
            {
                if (T == 0)
                {
                    T = 0.0001;
                }

                vi = Math.Sqrt(Math.Abs(Math.Log(S / X) + r * T) * 2 / T);

                //vi = 0.5;

                ci    = blackScholes(callPutFlag, S, X, T, r, vi);
                vegai = gVega(S, X, T, r, b, vi);
                //epsilon = 0.0001;
                //epsilon *= 2;
                int maxIter = 100;
                int i       = 0;

                prevVi = vi;

                double priceDifference         = Math.Abs(currentOptionPrice - ci);
                double smallestPriceDifference = priceDifference;
                //bool nanFail = false;

                while (priceDifference > (epsilon / 10) && i < maxIter)
                {
                    //if (vi <= prevVi && vi > 0 && ci > 0)

                    if (priceDifference < smallestPriceDifference &&
                        vi <= prevVi &&
                        vi > 0 && ci > 0)
                    {
                        prevVi = vi;
                        smallestPriceDifference = priceDifference;
                    }

                    //if (nanFail)
                    //{
                    //    vi = Math.Abs(vi - (ci - currentOptionPrice) / vegai);
                    //}
                    //else
                    {
                        vi = Math.Abs(vi - (ci - currentOptionPrice) / vegai);
                    }

                    ci = blackScholes(callPutFlag, S, X, T, r, vi);

                    priceDifference = Math.Abs(currentOptionPrice - ci);

                    //if(double.IsNaN(ci))
                    //{
                    //    vi = prevVi;

                    //    priceDifference = smallestPriceDifference;

                    //    //nanFail = true;

                    //    //break;
                    //}
                    //else
                    //{
                    //    priceDifference = Math.Abs(currentOptionPrice - ci);

                    //    //nanFail = false;
                    //}

                    if (vi <= 0 || double.IsInfinity(vi) || double.IsNaN(vi))
                    {
                        vi = prevVi;

                        break;
                    }

                    vegai = gVega(S, X, T, r, b, vi);

                    //if (vegai <= 0.005)
                    //{
                    //    TSErrorCatch.debugWriteOut(vi + "    "
                    //        + ci);

                    //    break;
                    //}

                    //int vegaIter = 0;
                    //vegai = 0;
                    //double tempVi = vi;
                    //int signVi = 1;
                    //while (vegai == 0 && vegaIter < maxIter)
                    //{
                    //    vegai = gVega(S, X, T, r, b, tempVi);

                    //    if(vegai <= 0.005)
                    //    {

                    //        //Random random = new Random();

                    //        //tempVi = tempVi + signVi * tempVi * random.NextDouble();

                    //        //signVi *= -1;

                    //        //vi = tempVi;

                    //    }
                    //    //else
                    //    //{
                    //    //    break;
                    //    //}

                    //    vegaIter++;
                    //}

                    //if (vegaIter == maxIter && vegai == 0)
                    //if (vegai == 0)
                    //{
                    //    vi = prevVi;

                    //    break;
                    //    //vegai = gVega(S, X, T, r, b, vi);
                    //}

                    i++;
                }

                if (i == maxIter)
                {
                    vi = prevVi;
                }

                //                 TSErrorCatch.debugWriteOut(vi + "    "
                //                             + ci);

                //return vi;
            }
            catch (Exception ex)
            {
                TSErrorCatch.errorCatchOut("OptionCalcs", ex);
            }

            //if (vi <= 0 || double.IsInfinity(vi) || double.IsNaN(vi))
            //{
            //    vi = prevVi;
            //}

            vi = double.IsInfinity(vi) || vi < 0 || double.IsNaN(vi) ? 0 : vi;

            return(vi);
        }
示例#10
0
        private void m_CEL_CELDataConnectionChg(eConnectionStatus new_status)
        {
            StringBuilder connStatusString      = new StringBuilder();
            StringBuilder connStatusShortString = new StringBuilder();
            Color         connColor             = Color.Red;

            try
            {
                if (m_CEL.IsStarted)
                {
                    connStatusString.Append("CQG API:");
                    connStatusString.Append(m_CEL.Environment.CELVersion);
                    connStatusShortString.Append("CQG:");

                    if (new_status != eConnectionStatus.csConnectionUp)
                    {
                        if (new_status == eConnectionStatus.csConnectionDelayed)
                        {
                            connColor = Color.BlanchedAlmond;
                            connStatusString.Append(" - CONNECTION IS DELAYED");
                            connStatusShortString.Append("DELAYED");

                            currConnStat = eConnectionStatus.csConnectionDelayed;
                            CQGEventHandlers._ICQGCELEvents_DataConnectionStatusChangedEventHandlerImpl(eConnectionStatus.csConnectionDelayed);
                        }
                        else
                        {
                            connStatusString.Append(" - CONNECTION IS DOWN");
                            connStatusShortString.Append("DOWN");

                            currConnStat = eConnectionStatus.csConnectionDown;
                            CQGEventHandlers._ICQGCELEvents_DataConnectionStatusChangedEventHandlerImpl(eConnectionStatus.csConnectionDelayed);
                        }
                    }
                    else
                    {
                        connColor = Color.LawnGreen;
                        connStatusString.Append(" - CONNECTION IS UP");
                        connStatusShortString.Append("UP");

                        currConnStat = eConnectionStatus.csConnectionUp;
                        CQGEventHandlers._ICQGCELEvents_DataConnectionStatusChangedEventHandlerImpl(eConnectionStatus.csConnectionUp);
                    }
                }
                else
                {
                    connStatusString.Append("WAITING FOR API CONNECTION");

                    connStatusShortString.Append("WAITING");
                }

                if (mainForm != null)
                {
                    mainForm.UpdateConnectionStatus(
                        connStatusString.ToString(), connColor);
                }
            }
            catch (Exception ex)
            {
                TSErrorCatch.errorCatchOut(Convert.ToString(this), ex);
            }
        }