Esempio n. 1
0
        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
            if (CurrentBar < 2)
            {
                barCount.Set(-1);
                return;
            }


            // do book-keeping on new bars...
            if (CurrentBar != lastBarSeen)
            {
                lastBarSeen = CurrentBar;
                barCount.Set(barCount[1] + 1);
                volumeCount += Volume[1];

                //Print("Bar: "+CurrentBar+" time: "+Time[0]+" tgt: "+tgtTime+ " sess: "+sessionTime);

                // determine if it's time for a new bar...
                bool barReset     = atBarResetTime();
                bool sessionStart = atSessionStart();

                if (barReset || sessionStart)
                {
                    // we need to start fresh...
                    barCount.Set(0);
                    volumeCount = 0;
                    newBar.Set(true);
                    if (sessionStart)
                    {
                        newSession.Set(true);
                    }
                }
                else
                {
                    // update stuff...
                    newBar.Set(false);
                    newSession.Set(false);
                }
            }

            //Print("CurrentBar: " + CurrentBar + " Bar Count: " + barCount[0] + " Vol count: " +volumeCount+" " +newBar[0]);
        }
Esempio n. 2
0
        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
            if (CurrentBar < 1)
            {
                up.Set(0);
                down.Set(0);
                ups.Set(0);
                downs.Set(0);
                index.Set(0);
                ADXVMA.Set(Input[0]);
            }
            else
            {
                double currentUp   = Math.Max(Input[0] - Input[1], 0);
                double currentDown = Math.Max(Input[1] - Input[0], 0);
                up.Set((1 - k) * up[1] + k * currentUp);
                down.Set((1 - k) * down[1] + k * currentDown);

                double sum          = up[0] + down[0];
                double fractionUp   = 0.0;
                double fractionDown = 0.0;
                if (sum > double.Epsilon)
                {
                    fractionUp   = up[0] / sum;
                    fractionDown = down[0] / sum;
                }
                ups.Set((1 - k) * ups[1] + k * fractionUp);
                downs.Set((1 - k) * downs[1] + k * fractionDown);

                double normDiff     = Math.Abs(ups[0] - downs[0]);
                double normSum      = ups[0] + downs[0];
                double normFraction = 0.0;
                if (normSum > double.Epsilon)
                {
                    normFraction = normDiff / normSum;
                }
                index.Set((1 - k) * index[1] + k * normFraction);

                if (FirstTickOfBar)
                {
                    epsilon = 0.1 * volatility[1];
                    hhp     = MAX(index, period)[1];
                    llp     = MIN(index, period)[1];
                }
                hhv = Math.Max(index[0], hhp);
                llv = Math.Min(index[0], llp);

                double vDiff  = hhv - llv;
                double vIndex = 0;
                if (vDiff > double.Epsilon)
                {
                    vIndex = (index[0] - llv) / vDiff;
                }

                ADXVMA.Set((1 - k * vIndex) * ADXVMA[1] + k * vIndex * Input[0]);

                if (trend[1] > -1 && ADXVMA[0] > ADXVMA[1] + epsilon)
                {
                    trend.Set(1);
                    PlotColors[0][0] = upColor;
                }
                else if (trend[1] < 1 && ADXVMA[0] < ADXVMA[1] - epsilon)
                {
                    trend.Set(-1);
                    PlotColors[0][0] = downColor;
                }
                else
                {
                    trend.Set(0);
                    PlotColors[0][0] = neutralColor;
                }

                if (showPaintBars)
                {
                    if (trend[0] == 1)
                    {
                        BarColor           = upColor;
                        CandleOutlineColor = upColor;
                    }
                    else if (trend[0] == -1)
                    {
                        BarColor           = downColor;
                        CandleOutlineColor = downColor;
                    }
                    else
                    {
                        BarColor           = neutralColor;
                        CandleOutlineColor = neutralColor;
                    }
                    if (candles && Close[0] > Open[0])
                    {
                        BarColor = Color.FromArgb(alpha, BarColor);
                    }
                }
            }
        }
Esempio n. 3
0
        protected override void OnCalculate()
        {
            if (this.MA_1 != 0 && this.MA_1 > this.RequiredBarsCount ||
                this.MA_2 != 0 && this.MA_2 > this.RequiredBarsCount ||
                this.MA_3 != 0 && this.MA_3 > this.RequiredBarsCount ||
                this.MA_4 != 0 && this.MA_4 > this.RequiredBarsCount ||
                this.MA_5 != 0 && this.MA_5 > this.RequiredBarsCount ||
                this.MA_6 != 0 && this.MA_6 > this.RequiredBarsCount)
            {
                AddChartTextFixed("AlertText", "Required bars must be at least as high as the largest moving average period.", TextPosition.Center, Color.Red, new Font("Arial", 30), Color.Red, Color.Red, 20);
            }

            int _signal_value = 0;
            int _enabled_ifs  = 0;

            if (this.MA_1 != 0)
            {
                Plot_1.Set(this.GetValue(this.MA_1_Selected, this.MA_1));
            }
            if (this.MA_2 != 0)
            {
                Plot_2.Set(this.GetValue(this.MA_2_Selected, this.MA_2));
            }
            if (this.MA_3 != 0)
            {
                Plot_3.Set(this.GetValue(this.MA_3_Selected, this.MA_3));
            }
            if (this.MA_4 != 0)
            {
                Plot_4.Set(this.GetValue(this.MA_4_Selected, this.MA_4));
            }
            if (this.MA_5 != 0)
            {
                Plot_5.Set(this.GetValue(this.MA_5_Selected, this.MA_5));
            }
            if (this.MA_6 != 0)
            {
                Plot_6.Set(this.GetValue(this.MA_6_Selected, this.MA_6));
            }

            //Signals
            if (this.If_1_over_2)
            {
                _enabled_ifs++;
                if (this.MA_1 != 0 && this.MA_2 != 0 && Plot_1.Last() > Plot_2.Last())
                {
                    _signal_value++;
                }
                else if (this.MA_1 != 0 && this.MA_2 != 0 && Plot_1.Last() < Plot_2.Last())
                {
                    _signal_value--;
                }
            }

            if (this.If_2_over_3)
            {
                _enabled_ifs++;
                if (this.MA_2 != 0 && this.MA_3 != 0 && Plot_2.Last() > Plot_3.Last())
                {
                    _signal_value++;
                }
                else if (this.MA_2 != 0 && this.MA_3 != 0 && Plot_2.Last() < Plot_3.Last())
                {
                    _signal_value--;
                }
            }

            if (this.If_3_over_4)
            {
                _enabled_ifs++;
                if (this.MA_3 != 0 && this.MA_4 != 0 && Plot_3.Last() > Plot_4.Last())
                {
                    _signal_value++;
                }
                else if (this.MA_3 != 0 && this.MA_4 != 0 && Plot_3.Last() < Plot_4.Last())
                {
                    _signal_value--;
                }
            }

            if (this.If_4_over_5)
            {
                _enabled_ifs++;
                if (this.MA_4 != 0 && this.MA_5 != 0 && Plot_4.Last() > Plot_5.Last())
                {
                    _signal_value++;
                }
                else if (this.MA_4 != 0 && this.MA_5 != 0 && Plot_4.Last() < Plot_5.Last())
                {
                    _signal_value--;
                }
            }

            if (this.If_5_over_6)
            {
                _enabled_ifs++;
                if (this.MA_5 != 0 && this.MA_6 != 0 && Plot_5.Last() > Plot_6.Last())
                {
                    _signal_value++;
                }
                else if (this.MA_5 != 0 && this.MA_6 != 0 && Plot_5.Last() < Plot_6.Last())
                {
                    _signal_value--;
                }
            }


            //signal
            if (_signal_value == _enabled_ifs)
            {
                _signals.Set(1);
            }
            else if (_signal_value == _enabled_ifs * -1)
            {
                _signals.Set(-1);
            }
            else
            {
                _signals.Set(0);
            }

            //days
            if (_signals[0] == 0)
            {
                _days.Set(0);
            }
            else
            {
                if (_signals[0] == 1 && _signals[1] == 1)
                {
                    _days.Set(_days[1] + 1);
                }
                else if (_signals[0] == -1 && _signals[1] == -1)
                {
                    _days.Set(_days[1] - 1);
                }
                else
                {
                    if (_signals[0] == 1 && _signals[1] == 0 ||
                        _signals[0] == 1 && _signals[1] == -1)
                    {
                        _days.Set(1);
                    }
                    else
                    {
                        _days.Set(-1);
                    }
                }
            }


            if (ShowSignalOnChartBackground)
            {
                //color an background
                if (_signals[0] == 1)
                {
                    this.BackColor = GlobalUtilities.AdjustOpacity(this.ColorLongSignalBackground, this.OpacityLongSignal / 100.0);
                }
                else if (_signals[0] == -1)
                {
                    this.BackColor = GlobalUtilities.AdjustOpacity(this.ColorShortSignalBackground, this.OpacityShortSignal / 100.0);
                }
            }

            //if (this.ShowSignalOnChartPercent)
            //{
            //    //percent on all signals with more _enabled_ifs
            //    if (_signals[0] == 0 && _signals[1] != 0)
            //    {
            //        this.drawpercentlines(1, this.DashStyleLine, this.PlotWidthLine);
            //    }
            //    else if (_signals[0] == 1 && _signals[1] == -1)
            //    {
            //        this.drawpercentlines(1, this.DashStyleLine, this.PlotWidthLine);
            //    }
            //    else if (_signals[0] == -1 && _signals[1] == 1)
            //    {
            //        this.drawpercentlines(1, this.DashStyleLine, this.PlotWidthLine);
            //    }

            //    //percent on last candle
            //    if (_signals[0] != 0 && IsProcessingBarIndexLast)
            //    {
            //        this.drawpercentlines(0, this.DashStyleLineLast, this.PlotWidthLineLast);
            //    }
            //}

            //percent
            //percent on all signals with more _enabled_ifs
            if (_signals[0] == 0 && _signals[1] != 0)
            {
                _percent.Set(this.getpercent(1));
                if (this.ShowSignalOnChartPercent)
                {
                    this.drawpercentlines(1, this.DashStyleLine, this.PlotWidthLine);
                }
            }
            else if (_signals[0] == 1 && _signals[1] == -1)
            {
                _percent.Set(this.getpercent(1));
                if (this.ShowSignalOnChartPercent)
                {
                    this.drawpercentlines(1, this.DashStyleLine, this.PlotWidthLine);
                }
            }
            else if (_signals[0] == -1 && _signals[1] == 1)
            {
                _percent.Set(this.getpercent(1));
                if (this.ShowSignalOnChartPercent)
                {
                    this.drawpercentlines(1, this.DashStyleLine, this.PlotWidthLine);
                }
            }

            //percent on last candle
            if (_signals[0] != 0 && IsProcessingBarIndexLast)
            {
                _percent.Set(this.getpercent(0));
                if (this.ShowSignalOnChartPercent)
                {
                    this.drawpercentlines(0, this.DashStyleLineLast, this.PlotWidthLineLast);
                }
            }


            if (this.ShowSignalOnChartArrow)
            {
                if (_signals[0] == 1 && _signals[1] != 1)
                {
                    AddChartArrowUp("ArrowLong_MA" + +Bars[0].Time.Ticks, this.IsAutoAdjustableScale, 0, Bars[0].Low, this.ColorLongSignalArrow);
                }
                else if (_signals[0] == -1 && _signals[1] != -1)
                {
                    AddChartArrowDown("ArrowShort_MA" + +Bars[0].Time.Ticks, this.IsAutoAdjustableScale, 0, Bars[0].High, this.ColorShortSignalArrow);
                }
            }


            //Set the color
            PlotColors[0][0] = this.Color_1;
            OutputDescriptors[0].PenStyle  = this.DashStyle_1;
            OutputDescriptors[0].Pen.Width = this.LineWidth_1;
            PlotColors[1][0] = this.Color_2;
            OutputDescriptors[1].PenStyle  = this.DashStyle_2;
            OutputDescriptors[1].Pen.Width = this.LineWidth_2;
            PlotColors[2][0] = this.Color_3;
            OutputDescriptors[2].PenStyle  = this.DashStyle_3;
            OutputDescriptors[2].Pen.Width = this.LineWidth_3;
            PlotColors[3][0] = this.Color_4;
            OutputDescriptors[3].PenStyle  = this.DashStyle_4;
            OutputDescriptors[3].Pen.Width = this.LineWidth_4;
            PlotColors[4][0] = this.Color_5;
            OutputDescriptors[4].PenStyle  = this.DashStyle_5;
            OutputDescriptors[4].Pen.Width = this.LineWidth_5;
            PlotColors[5][0] = this.Color_6;
            OutputDescriptors[5].PenStyle  = this.DashStyle_6;
            OutputDescriptors[5].Pen.Width = this.LineWidth_6;
        }
Esempio n. 4
0
        protected override void OnBarUpdate()
        {
            Value.Set(MAV(MAV(Input, MA1Period, (int)MA1Type), MA2Period, (int)MA2Type)[0]);

            if (CurrentBar < 2)
            {
                return;
            }

            double valueslope = (180 / Math.PI) * (Math.Atan((Value[0] - (Value[1] + Value[2]) / 2) / 1.5 / TickSize));

            if (valueslope > angle)
            {
                signal.Set(1);
                PlotColors[0][0] = BarColorUp;
            }
            else
            if (valueslope < -angle)
            {
                signal.Set(-1);
                PlotColors[0][0] = BarColorDown;
            }
            else
            {
                signal.Set(0);
                PlotColors[0][0] = BarColorNeutral;
            }

            if (paintBar)
            {
                CandleOutlineColor = ChartControl.ChartStyle.Pen.Color;
                BarColor           = signal[0] == 1 ? BarColorUp : (signal[0] == -1 ? BarColorDown : BarColorNeutral);
            }

            if (showArrows)
            {
                if (signal[0] == 1 && signal[1] != 1)
                {
                    DrawArrowUp(CurrentBar.ToString(CultureInfo.InvariantCulture), true, 0, Math.Min(Low[0], Value[0]) - TickSize * ArrowDisplacement, BarColorUp);
                }
                else
                if (signal[0] == -1 && signal[1] != -1)
                {
                    DrawArrowDown(CurrentBar.ToString(CultureInfo.InvariantCulture), true, 0, Math.Max(High[0], Value[0]) + TickSize * ArrowDisplacement, BarColorDown);
                }
                else
                {
                    RemoveDrawObject(CurrentBar.ToString(CultureInfo.InvariantCulture));
                }
            }

            if (!soundon)
            {
                return;
            }

            if (signal[0] == 1 && signal[1] != 1 && thislong != CurrentBar)
            {
                thislong = CurrentBar;
                PlaySound(longwavfilename);
            }
            else
            if (signal[0] == -1 && signal[1] != -1 && thisshort != CurrentBar)
            {
                thisshort = CurrentBar;
                PlaySound(shortwavfilename);
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
            if (CurrentBar != lastBar)
            {
                prevVol = 0;
                lastBar = CurrentBar;
                upTradeCount.Set(0);
                dnTradeCount.Set(0);
                upTradeVol.Set(0);
                dnTradeVol.Set(0);
            }

            double tradeVol         = Volume[0] - prevVol;
            bool   weHaveUpPressure = false;
            bool   weHaveDnPressure = false;

            if (!CalculateOnBarClose && !Historical && !firstTick)
            {
                switch (calcMethod)
                {
                    #region CalcMethodStuff
                case VolumeStatsMode.UpDnTick:
                    // ***********************************************
                    // the UpDnTick calculation method....
                    // ***********************************************
                    if ((Close[0] > prevClose) || ((Close[0] == prevClose) && uptick))
                    {
                        uptick           = true;
                        weHaveUpPressure = true;
                    }
                    else if ((Close[0] < prevClose) || ((Close[0] == prevClose) && !uptick))
                    {
                        uptick           = false;
                        weHaveDnPressure = true;
                    }
                    break;

                case VolumeStatsMode.PrevMedian:
                    // ***********************************************
                    // the PrevMedian calculation method....
                    // ***********************************************
                    double oldMedian = ((CurrentBar < 2)?Median[0]:Median[1]);

                    if (Close[0] > oldMedian)
                    {
                        weHaveUpPressure = true;
                    }
                    else if (Close[0] < oldMedian)
                    {
                        weHaveDnPressure = true;
                    }
                    break;

                case VolumeStatsMode.PrevClose:
                    // ***********************************************
                    // the PrevClose calculation method....
                    // ***********************************************
                    double oldClose = ((CurrentBar < 2)?Close[0]:Close[1]);

                    if (Close[0] > oldClose)
                    {
                        weHaveUpPressure = true;
                    }
                    else if (Close[0] < oldClose)
                    {
                        weHaveDnPressure = true;
                    }
                    break;

                case VolumeStatsMode.BidAsk:
                    // ***********************************************
                    // the BidAsk calculation method....
                    // ***********************************************
                    if (Close[0] >= lastAsk)
                    {
                        weHaveUpPressure = true;
                    }
                    else if (Close[0] <= lastBid)
                    {
                        weHaveDnPressure = true;
                    }
                    break;

                default:
                    break;
                    #endregion
                }

                prevVol   = Volume[0];
                prevClose = Close[0];
            }
            else
            {
                upTradeCount.Set(0);
                dnTradeCount.Set(0);
                upTradeVol.Set(0);
                dnTradeVol.Set(0);
                if (!Historical)
                {
                    firstTick = false;
                }
                prevVol   = Volume[0];
                prevClose = Close[0];
            }

            // ********************************************
            // do the bundling, and reporting....
            // ********************************************
            upTradeComponents = 0;
            upTradeAvgPrice   = -1;
            upTradeSize       = 0;
            dnTradeComponents = 0;
            dnTradeAvgPrice   = -1;
            dnTradeSize       = 0;

            #region BundlingStuff
            if (bundleTrades)
            {
                // if either of the timers have gone far enough to
                // separate a tick, then publish it...
                if (timeSinceUpVol.ElapsedMilliseconds > bundleMilliseconds)
                {
                    // report the trade if it's of the right size...
                    if ((storedUpVol >= minSize) && (storedUpVol <= maxSize))
                    {
                        upTradeVol.Set(upTradeVol[0] + storedUpVol);
                        upTradeCount.Set(upTradeCount[0] + 1);
                        upTradeComponents = storedUpTicks;
                        upTradeAvgPrice   = summedUpTradePrice / storedUpVol;
                        upTradeSize       = storedUpVol;
                    }
                    // no matter what, clear the trade out...
                    storedUpVol        = 0;
                    storedUpTicks      = 0;
                    summedUpTradePrice = 0;
                    timeSinceUpVol.Reset();
                }
                if (timeSinceDnVol.ElapsedMilliseconds > bundleMilliseconds)
                {
                    // report the trade if it's of the right size...
                    if ((storedDnVol >= minSize) && (storedDnVol <= maxSize))
                    {
                        dnTradeVol.Set(dnTradeVol[0] + storedDnVol);
                        dnTradeCount.Set(dnTradeCount[0] + 1);
                        dnTradeComponents = storedDnTicks;
                        dnTradeAvgPrice   = summedDnTradePrice / storedDnVol;
                        dnTradeSize       = storedDnVol;
                    }
                    // no matter what, clear the trade out...
                    storedDnVol        = 0;
                    storedDnTicks      = 0;
                    summedDnTradePrice = 0;
                    timeSinceDnVol.Reset();
                }

                if (weHaveUpPressure)
                {
                    storedUpVol += tradeVol;
                    ++storedUpTicks;
                    summedUpTradePrice += (Close[0] * tradeVol);
                    timeSinceUpVol.Reset();
                    timeSinceUpVol.Start();
                }
                if (weHaveDnPressure)
                {
                    storedDnVol += tradeVol;
                    ++storedDnTicks;
                    summedDnTradePrice += (Close[0] * tradeVol);
                    timeSinceDnVol.Reset();
                    timeSinceDnVol.Start();
                }
            }
            else
            {
                // not bundling trades...
                if ((tradeVol >= minSize) && (tradeVol <= maxSize))
                {
                    if (weHaveUpPressure)
                    {
                        upTradeComponents = 1;
                        upTradeAvgPrice   = Close[0];
                        upTradeSize       = tradeVol;

                        upTradeCount.Set(upTradeCount[0] + 1);
                        upTradeVol.Set(upTradeVol[0] + tradeVol);
                    }
                    if (weHaveDnPressure)
                    {
                        dnTradeComponents = 1;
                        dnTradeAvgPrice   = Close[0];
                        dnTradeSize       = tradeVol;

                        dnTradeCount.Set(dnTradeCount[0] + 1);
                        dnTradeVol.Set(dnTradeVol[0] + tradeVol);
                    }
                }
            }
            #endregion

            // plot the 1-bar delta
            OneBarDelta.Set(upTradeVol[0] - dnTradeVol[0]);
        }