Exemplo n.º 1
0
        public bool CanExecute(double price, int barIndex)
        {
            if (_chartTrendLine.Thickness <= 1)
            {
                return(false);
            }

            var lineValue = _chartTrendLine.CalculateY(barIndex);

            switch (SignalType)
            {
            case SignalType.Breakout:
                return(CanExecuteForBreakout(price, lineValue));

            case SignalType.Retracement:
                return(CanExecuteForRetrace(price, lineValue));

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Exemplo n.º 2
0
        private void DrawLevel(ChartTrendLine medianLine, double medianLineSecondBarIndex, double barsDelta, double lengthInMinutes, double priceDelta, double handleLineSlope, double percent, Color lineColor, long id)
        {
            var barsPercent = barsDelta * percent;

            var firstBarIndex = handleLineSlope > 0 ? medianLineSecondBarIndex + barsPercent : medianLineSecondBarIndex - barsPercent;
            var firstTime     = Chart.Bars.GetOpenTime(firstBarIndex, Chart.Symbol);
            var firstPrice    = medianLine.Y2 + priceDelta * percent;

            var secondTime = medianLine.Time1 > medianLine.Time2 ? firstTime.AddMinutes(-lengthInMinutes) : firstTime.AddMinutes(lengthInMinutes);

            var priceDistanceWithMediumLine = Math.Abs(medianLine.CalculateY(firstTime) - medianLine.CalculateY(secondTime));

            var secondPrice = medianLine.Y2 > medianLine.Y1 ? firstPrice + priceDistanceWithMediumLine : firstPrice - priceDistanceWithMediumLine;

            var name = GetObjectName(string.Format("Level_{0}", percent.ToString(CultureInfo.InvariantCulture)), id: id);

            var line = Chart.DrawTrendLine(name, firstTime, firstPrice, secondTime, secondPrice, lineColor);

            line.ExtendToInfinity = true;
            line.IsInteractive    = true;
            line.IsLocked         = true;
        }
        private void _manageTrendLine(OnState mystate, ChartTrendLine myline)
        {
            // --> Se non è inizializzata non devo fare nulla
            if (myline.Comment == null)
            {
                return;
            }

            // --> Potrebbe essere in un protoccollo non in linea con le aspettative
            string[] directive = myline.Comment.Split(Flag.Separator);

            // --> Aggiorno il feedback visivo
            if (!_checkFeedback(myline, directive))
            {
                return;
            }

            // --> Se la trendline non è infinita allora devo controllare il tempo, inizio con le scadute
            if (!myline.ExtendToInfinity && myline.Time1 < Bars.LastBar.OpenTime && myline.Time2 < Bars.LastBar.OpenTime)
            {
                myline.ToDelivered();
                return;
            }
            else if (myline.Time1 > Bars.LastBar.OpenTime && myline.Time2 > Bars.LastBar.OpenTime)
            {
                // --> Sono nel futuro, non opero
                return;
            }

            // --> Prelevo il prezzo della trendline
            double lineprice = Math.Round(myline.CalculateY(Chart.BarsTotal - 1), Symbol.Digits);

            switch (mystate)
            {
            // --> Solo controlli per le bar,
            case OnState.Bar:

                // --> Prelevo lo stato attuale del prezzo
                CurrentStateLine myPricePosition = _checkCurrentState(lineprice);

                if (myPricePosition == CurrentStateLine.OverBar)
                {
                    if (directive[0] == Flag.OverBar)
                    {
                        _alert(myline, directive[4]);
                        directive[0] = Flag.DISABLED;
                    }

                    if (directive[1] == Flag.OverBar)
                    {
                        _close(myline);
                        directive[1] = Flag.DISABLED;
                    }

                    if (directive[2] == Flag.OpenBuyStopBar)
                    {
                        _open(myline, TradeType.Buy, directive[3]);
                        directive[2] = Flag.DISABLED;
                    }
                }
                else if (myPricePosition == CurrentStateLine.UnderBar)
                {
                    if (directive[0] == Flag.UnderBar)
                    {
                        _alert(myline, directive[4]);
                        directive[0] = Flag.DISABLED;
                    }

                    if (directive[1] == Flag.UnderBar)
                    {
                        _close(myline);
                        directive[1] = Flag.DISABLED;
                    }

                    if (directive[2] == Flag.OpenSellStopBar)
                    {
                        _open(myline, TradeType.Sell, directive[3]);
                        directive[2] = Flag.DISABLED;
                    }
                }
                else if (myPricePosition == CurrentStateLine.OnGAP)
                {
                    double gapBar = Math.Round(Math.Abs(Bars.ClosePrices.Last(1) - Bars.OpenPrices.Last(0)) / Symbol.PipSize, 2);

                    // --> Procedo se il GAP è nella norma
                    if (gapBar <= MaxGAPBar)
                    {
                        _alert(myline, directive[4]);
                        _close(myline);

                        switch (directive[2])
                        {
                        case Flag.OpenBuyStopBar:

                            _open(myline, TradeType.Buy, directive[3]);
                            break;

                        case Flag.OpenSellStopBar:

                            _open(myline, TradeType.Sell, directive[3]);
                            break;
                        }

                        _log("GAP (" + gapBar + ") then Triggered (check cBot setup)");
                    }
                    else
                    {
                        _log("GAP (" + gapBar + ") then Disabled (check cBot setup)");
                    }

                    // --> Disabilito a prescindere
                    directive[0] = Flag.DISABLED;
                    directive[1] = Flag.DISABLED;
                    directive[2] = Flag.DISABLED;
                }

                break;

            default:


                // --> Prelevo lo stato attuale del prezzo
                CurrentStateLine myPricePositionForAsk = _checkCurrentState(lineprice, Ask);
                CurrentStateLine myPricePositionForBid = _checkCurrentState(lineprice, Bid);

                if (myPricePositionForAsk == CurrentStateLine.Over)
                {
                    if (directive[0] == Flag.Over)
                    {
                        _alert(myline, directive[4]);
                        directive[0] = Flag.DISABLED;
                    }

                    /*
                     * if (directive[1] == Flag.Over)
                     * {
                     *
                     *  _close(myline);
                     *  directive[1] = Flag.DISABLED;
                     *
                     * }
                     */
                    if (directive[2] == Flag.OpenBuyStop)
                    {
                        _open(myline, TradeType.Buy, directive[3]);
                        directive[2] = Flag.DISABLED;
                    }
                }
                else if (myPricePositionForAsk == CurrentStateLine.Under)
                {
                    if (directive[0] == Flag.Under)
                    {
                        _alert(myline, directive[4]);
                        directive[0] = Flag.DISABLED;
                    }

                    if (directive[1] == Flag.Under)
                    {
                        _close(myline);
                        directive[1] = Flag.DISABLED;
                    }

                    if (directive[2] == Flag.OpenBuyLimit)
                    {
                        _open(myline, TradeType.Buy, directive[3]);
                        directive[2] = Flag.DISABLED;
                    }
                }

                if (myPricePositionForBid == CurrentStateLine.Over)
                {
                    if (directive[0] == Flag.Over)
                    {
                        _alert(myline, directive[4]);
                        directive[0] = Flag.DISABLED;
                    }

                    if (directive[1] == Flag.Over)
                    {
                        _close(myline);
                        directive[1] = Flag.DISABLED;
                    }

                    if (directive[2] == Flag.OpenSellLimit)
                    {
                        _open(myline, TradeType.Sell, directive[3]);
                        directive[2] = Flag.DISABLED;
                    }
                }
                else if (myPricePositionForBid == CurrentStateLine.Under)
                {
                    if (directive[0] == Flag.Under)
                    {
                        _alert(myline, directive[4]);
                        directive[0] = Flag.DISABLED;
                    }

                    /*
                     * if (directive[1] == Flag.Under)
                     * {
                     *
                     *  _close(myline);
                     *  directive[1] = Flag.DISABLED;
                     *
                     * }
                     */
                    if (directive[2] == Flag.OpenSellStop)
                    {
                        _open(myline, TradeType.Sell, directive[3]);
                        directive[2] = Flag.DISABLED;
                    }
                }

                break;
            }

            // --> Ricostruisco le direttive
            myline.Comment = string.Join("/", directive);
        }