Пример #1
0
        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
            double squared      = 0;
            double mean         = 0;
            double sumDeltaVol  = 0;
            double stddevtemp   = 0;
            double normdisttemp = 0;
            double vpintemp     = 0;

            if (useRealtimeVol)
            {
                if (Historical)
                {
                    return;
                }
            }


            if (CurrentBar < 150)
            {
                return;
            }

            deltaPrice[0] = Close[0] - Close[1];

            if (useRealtimeVol)
            {
                accumVol = accumVol + lastTradeVol;
            }
            else
            {
                accumVol = accumVol + Volume[1];

                timeBarCounter = timeBarCounter + 1;
                Print("timeBarCounter  = " + timeBarCounter);
                Print("numTimeBarLastBucket  = " + numTimeBarLastBucket);
                Print("deltaPrice[1]  = " + deltaPrice[0]);
                Print("deltaPrice[1]  = " + deltaPrice[1]);
                Print("deltaPrice[1]  = " + deltaPrice[2]);
                Print("Volume[1]  = " + Volume[1]);
                Print("StdDev  = " + StdDev(deltaPrice, 100)[0]);
                Print("SMA  = " + SMA(deltaPrice, 100)[0]);

                stddevtemp   = StdDev(deltaPrice, 100)[1];
                normdisttemp = NORMDIST((deltaPrice[1] / stddevtemp), /*SMA(deltaPrice,100)[1]*/ 0, StdDev(deltaPrice, 100)[1], true);
                if (double.IsNaN(normdisttemp))
                {
                    normdisttemp = 0.5;
                }
                //if( stddevtemp == 0 ) stddevtemp = 0.01;
                Print("normdist = " + normdisttemp);

                VolBuy  = VolBuy + (Volume[1] * normdisttemp);
                VolSell = VolSell + (Volume[1] - (Volume[1] * normdisttemp));
                Print("VolBuy = " + VolBuy);
                Print("VolSell = " + VolSell);
            }
            Print("accumVol = " + accumVol);


            // New bar is formed.  Calculate deltaP/sigma.
            //    calculate the stddev based on num of time bars included in a vol bucket -- in slow days, can be more time bars, and need prev vol bucket # of time bars
            if (useRealtimeVol)
            {
                if (currentBarSaved != CurrentBar)
                {
                    Print("deltaPrice 0 = " + deltaPrice[0]);
                    Print("deltaPrice 1 = " + deltaPrice[1]);
                    timeBarCounter = timeBarCounter + 1;                                // count time bar inside vol bucket

                    //VolBuy = Volume[1] * NORMDIST( (deltaPrice[1]/StdDev(deltaPrice,timeBarCounter)[0]), SMA(deltaPrice,timeBarCounter)[0], StdDev(deltaPrice, timeBarCounter)[0], true);


                    currentBarSaved = CurrentBar;
                }
            }

            // Find the mean of delta price of the gathered data of 1/50 * daily vol.
            // Keep track of volume. Mark the start with the 1-min price bar open


            // V bucket is accomplished, VPIN then calculated (every completed bucket)
            if (accumVol > volBucketSize)
            {
                n                    = n + 1; // count bucket sampling
                bucketFull           = true;
                numTimeBarLastBucket = timeBarCounter;
                if (numTimeBarLastBucket < 2)
                {
                    numTimeBarLastBucket = 5;
                }
                timeBarCounter = 0;
                accumVol       = 0;           //reset accummulated vol counter

                // VPIN formula sigma over 50 buckets... Delta |Vs - Vb|  /  nV
                // so accummulate delta |Vs -Vb| every completed bucket
                //cummDeltaVsVB = cummDeltaVsVB + Math.Abs(VolSell - VolBuy);

                if (n <= sampleSize)
                {                       // filll up queue until 50
                    Print(" filling up queue , n = " + n);
                    Print("delta abs Vs-Vb = " + (double)Math.Abs(VolSell - VolBuy));
                    deltaVol.Enqueue((double)Math.Abs(VolSell - VolBuy));

                    foreach (double value in deltaVol)
                    {
                        Print(" Value in each deltaVol = " + value);
                    }
                }
                else
                {
                    Print(" Queue is full count = " + deltaVol.Count);
                    initialBucketFull = true;
                }

                if (initialBucketFull)
                {
                    deltaVol.Dequeue(  );
                    deltaVol.Enqueue(Math.Abs(VolSell - VolBuy));

                    //deltaVol.CopyTo( deltaVolArray, 0 );

                    // Loop through queue.
                    foreach (double value in deltaVol)
                    {
                        sumDeltaVol = value + sumDeltaVol;
                        Print(" Value in each deltaVol = " + value);
                    }
                    Print("sumDeltaVol " + sumDeltaVol);


                    // Calculate now, VPIN = sigma cummulative deltaVsVb / nV
                    VPINvalue = sumDeltaVol / (sampleSize * volBucketSize);
                    Print("VPINvalue " + VPINvalue);

                    vpinDataseries.Set(VPINvalue);
                }

                VolBuy  = 0;
                VolSell = 0;
            }
            else
            {
                vpinDataseries.Set(VPINvalue);
            }



            Print("vpinDataseries [0] " + vpinDataseries[0]);
            Print("vpinDataseries [1] " + vpinDataseries[1]);
            Print("vpinDataseries [2] " + vpinDataseries[2]);
            vpintemp = NORMDIST((vpinDataseries[1] / StdDev(vpinDataseries, 50)[1]), /*SMA(vpinDataseries,50)[1]*/ 0, StdDev(vpinDataseries, 50)[1], true);


            // PLOT
            // CDFofVPIN.Set(vpintemp)  ;
            //if( double.IsNaN(VPINvalue) ) VPINvalue = 0.25;
            VPIN.Set(VPINvalue);
        }
        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
            double squared      = 0;
            double mean         = 0;
            double sumDeltaVol  = 0;
            double stddevtemp   = 0;
            double normdisttemp = 0;
            double sumOfOI      = 0;

            double sumOfDerivationAverage = 0;
            double remainingVolume        = 0;
            double remainingAggBuyVol     = 0;
            double remainingAggSellVol    = 0;
            double sumOfDerivation        = 0.0;
            int    counter = 0;

            double cdfBuyDeltaPrice, cdfSellDeltaPrice, volBuyDeltaPrice, volSellDeltaPrice;


            //Print("Currentbar   = " + CurrentBar);

            if (useRealtimeVol)
            {
                if (Historical)
                {
                    return;
                }
            }

            // CALCULATE EACH BAR
            // then, if over V > 30000 , then basket full, CALCULATE VPIN
            if (CurrentBar < 5)
            {
                return;
            }
            deltaPrice[0] = Close[0] - Close[1];

            if (CurrentBar < 510)
            {
                return;
            }

            stddevtemp         = StdDev(deltaPrice, 500)[0];
            deltaPdivSigmaP[0] = deltaPrice[0] / stddevtemp;

            //normdisttemp = NORMDIST( deltaPdivSigmaP[0], SMA(deltaPdivSigmaP, 500)[0], StdDev(deltaPdivSigmaP, 500)[0], true);
            normdisttemp = NORMDIST(deltaPdivSigmaP[0], 0, StdDev(deltaPdivSigmaP, 500)[0], true);


            //VolBuy = VolBuy + (Volume[0] * normdisttemp  );
            //VolSell = VolSell + ( Volume[0] * (1 - normdisttemp ) );

            // keep track of the volume bucket
            // if over V, then calculate vpin

            VolCounter = VolCounter + Volume[0];

            // LOGS ----------------------------------
            cdfSellDeltaPrice = 1 - normdisttemp;
            volBuyDeltaPrice  = (Volume[0] * normdisttemp);
            volSellDeltaPrice = Volume[0] * (1 - normdisttemp);
            //Print(" " + Time[0] + "\t" + deltaPrice[0].ToString("0.00") + "\t" + Volume[0] + "\t" + VolCounter + "\t" + basketCount + "\t" +
            //  normdisttemp.ToString("0.00") + "\t" + cdfSellDeltaPrice.ToString("0.00") + "\t" +
            //	volBuyDeltaPrice.ToString("0") + "\t" + ( volSellDeltaPrice.ToString("0")));
            // ------------------------------------------

            accumulatedBuyVol  = accumulatedBuyVol + volBuyDeltaPrice;
            accumulatedSellVol = accumulatedSellVol + volSellDeltaPrice;

            // V bucket is accomplished, VPIN then calculated (every completed bucket)
            if (VolCounter > volBucketSize)
            {
                basketTempCount = basketTempCount + 1;

                remainingVolume = VolCounter - volBucketSize;
                //	Print("remainingVolume " +remainingVolume);
                remainingAggBuyVol  = remainingVolume * normdisttemp;
                remainingAggSellVol = remainingVolume * (1 - normdisttemp);

                //save previous orderImbalance value first
                prevBasketOI = orderImbalance;
                //Print("prevBasketOI " +prevBasketOI);
                // VPIN = sum(order imbalance of 50 buckets) / 50 * VolBucketSize
                orderImbalance = Math.Abs(accumulatedBuyVol - accumulatedSellVol);
                //orderImbalance = accumulatedBuyVol - accumulatedSellVol;

                qAccOI.Enqueue(orderImbalance);
                if (qAccOI.Count >= 50)
                {
                    foreach (double value in qAccOI)
                    {
                        sumOfOI += value;
                    }
                    qAccOI.Dequeue();
                }

                //Print("orderImbalance = "+orderImbalance.ToString("0")
                //+ " sumOfOI = " + sumOfOI.ToString("0")
                //+" accumulatedBuyVol "+accumulatedBuyVol.ToString("0")
                //+ " accumulatedSellVol " +accumulatedSellVol.ToString("0") );

                basketCount = basketCount + 1;

                // Calculate VPIN
                VPINvalue = sumOfOI / (nWindowsize * volBucketSize);


                // Calculate VPIN CDF
                queueAvgVpin.Enqueue((double)VPINvalue);
                if (queueAvgVpin.Count >= 50)
                {
                    foreach (double value in queueAvgVpin)
                    {
                        //Print("queue["+counter+"] = " + value);
                        counter         += 1;
                        avgVpin          = avgVpin + value;
                        sumOfDerivation += (value) * (value);
                    }
                    sumOfDerivationAverage = sumOfDerivation / queueAvgVpin.Count;

                    avgVpin    = avgVpin / queueAvgVpin.Count;
                    stddevVpin = Math.Sqrt(sumOfDerivationAverage - (avgVpin * avgVpin));
                    //		Print("avgVpin = " + avgVpin);
                    //		Print("stddevVpin = " + stddevVpin);
                    queueAvgVpin.Dequeue();
                }
                //Print(" " + Time[0] + "\t" + Close[0]  +"\t" + VPINvalue.ToString("0.0000"));
                //	Print( "" + VPINvalue.ToString("0.0000") + "\t" + sumOfOI.ToString("0") + "\t" + (basketCount-1) + "\t" + "\t"
                //		+ accumulatedBuyVol.ToString("0")+ "\t"+ accumulatedSellVol.ToString("0") + "\t" + orderImbalance.ToString("0") );

                if (basketCount > (nWindowsize - 1))
                {
                    basketCount = 0;
                }

                accumulatedBuyVol  = remainingAggBuyVol;
                accumulatedSellVol = remainingAggSellVol;
                VolCounter         = remainingVolume;

                vpintemp = NORMDIST(VPINvalue, avgVpin, stddevVpin, true);
            }             // if( VolCounter > volBucketSize )

            vpinDataseries[0] = VPINvalue;


            avgVpin = 0;

            //Print("vpinDataseries[0] = " + vpinDataseries[0] + " stddevVpin = "+stddevVpin+" vpintemp = " +vpintemp );
            VPIN.Set(vpinDataseries[0]);
            CDFofVPIN.Set(vpintemp);
        }
        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
            double squared                = 0;
            double mean                   = 0;
            double sumDeltaVol            = 0;
            double stddevtemp             = 0;
            double normdisttemp           = 0;
            double sumOfDerivation        = 0;
            double stddevVpin             = 0;
            double sumOfDerivationAverage = 0;

            //Print("Currentbar   = " + CurrentBar);

            if (useRealtimeVol)
            {
                if (Historical)
                {
                    return;
                }
            }


            if (CurrentBar < 205)
            {
                return;
            }


            // CALCULATE EACH BAR
            // then, if over V > 30000 , then basket full, CALCULATE VPIN

            deltaPrice[0] = Close[0] - Close[1];

            stddevtemp         = StdDev(deltaPrice, cdfsamplesize)[0];
            deltaPdivSigmaP[0] = deltaPrice[0] / stddevtemp;

            normdisttemp = NORMDIST(deltaPdivSigmaP[0], SMA(deltaPdivSigmaP, cdfsamplesize)[0], StdDev(deltaPdivSigmaP, cdfsamplesize)[0], true);
            //normdisttemp = NORMDIST( deltaPdivSigmaP[0], 0, 1, true);
            //Print("normdist = " + normdisttemp );

            VolBuy  = VolBuy + (Volume[0] * normdisttemp);
            VolSell = VolSell + (Volume[0] * (1 - normdisttemp));
            //Print("VolBuy = " + VolBuy);
            //Print("VolSell = " + VolSell);

            // keep track of the volume bucket
            // if over V, then calculate vpin

            VolCounter = VolCounter + Volume[0];

            vpinDataseries[0] = VPINvalue;

            // V bucket is accomplished, VPIN then calculated (every completed bucket)
            if (VolCounter > volBucketSize)
            {
                //Print("===== new bucket, vol = " + VolCounter);

                // VPIN formula sigma over 50 buckets... Delta |Vs - Vb|  /  nV
                // so accummulate delta |Vs -Vb| every completed bucket
                //cummDeltaVsVB = cummDeltaVsVB + Math.Abs(VolSell - VolBuy);

                deltaVol.Enqueue((double)Math.Abs(VolSell - VolBuy));

                if (deltaVol.Count >= sampleSize)
                {
                    // calc vpin
                    foreach (double value in deltaVol)
                    {
                        cummDeltaVsVB = cummDeltaVsVB + value;

                        //Print( " Value in each deltaVol = " + value);
                    }

                    // Calculate now, VPIN = sigma cummulative deltaVsVb / nV
                    VPINvalue = cummDeltaVsVB / (sampleSize * VolCounter);
                    //Print("VPINvalue " + VPINvalue);

                    queueAvgVpin.Enqueue((double)VPINvalue);
                    if (queueAvgVpin.Count >= sampleSize)
                    {
                        foreach (double value in queueAvgVpin)
                        {
                            avgVpin          = avgVpin + value;
                            sumOfDerivation += (value) * (value);
                        }
                        sumOfDerivationAverage = sumOfDerivation / queueAvgVpin.Count;

                        avgVpin    = avgVpin / queueAvgVpin.Count;
                        stddevVpin = Math.Sqrt(sumOfDerivationAverage - (avgVpin * avgVpin));

                        queueAvgVpin.Dequeue();
                    }

                    // clear vars
                    cummDeltaVsVB = 0;
                    deltaVol.Dequeue(  );
                }

                vpinDataseries[0] = VPINvalue;
                VolCounter        = 0;
                VolBuy            = 0;
                VolSell           = 0;
                //Print("avgVpin " + avgVpin);
                //Print("stddevVpin " + stddevVpin);
                vpintemp = NORMDIST((vpinDataseries[0]), avgVpin, stddevVpin, true);

                avgVpin = 0;

                //if( (Time[0].Month == 8) && Time[0].Year == 2011 ){
                //	Print(Time[0].Month + "/" + Time[0].Day + " " + Time[0].TimeOfDay + "	" + Close[0] + "	" + vpinDataseries[0] + "	" + vpintemp);
                //}
            }



            //vpintemp=NORMDIST( (vpinDataseries[0]), 0, 1, true);
//Print("vpintemp " + vpintemp);
            //Print("vpinDataseries[0] " + vpinDataseries[0]);
            //Print("SMA(vpinDataseries,20)[0]  "+SMA(vpinDataseries,20)[0]);
            // PLOT

            VPIN.Set(vpinDataseries[0]);
            CDFofVPIN.Set(vpintemp);
        }