Esempio n. 1
0
        private void dataGridView1_SelectionChanged(object sender, EventArgs xx)
        {
            if (dataGridView1.SelectedRows.Count <= 0)
            {
                return;
            }
            var    row      = dataGridView1.SelectedRows[0];
            string pairName = (string)row.Cells[0].Value;
            var    pair     = _pairs.FirstOrDefault(e => e.Symbol.NiceName == pairName);

            if (pair != null)
            {
                var candleStickPoints = new StockPointList();
                var candles           = pair.Symbol.Candles;
                var lastCandle        = candles.Count - 1;

                for (int i = lastCandle; i >= 0; i--)
                {
                    var    candle = candles[i];
                    double x      = new XDate(candles[lastCandle].OpenTime);

                    StockPt pt = new StockPt(new XDate(candle.OpenTime),
                                             (double)candle.High,
                                             (double)candle.Low,
                                             (double)candle.Open,
                                             (double)candle.Close,
                                             100000);
                    candleStickPoints.Add(pt);
                }

                var masterPane = zedGraphControl1.MasterPane;
                // while (masterPane.PaneList.Count < 2) masterPane.PaneList.Add(new GraphPane());
                var mainPane = masterPane.PaneList[0];
                // var mbfxPane = masterPane.PaneList[1];
                //mbfxPane.

                mainPane.Title.Text = pair.Symbol.NiceName + " " + pair.Symbol.NiceTimeFrame;
                mainPane.CurveList.Clear();
                var candleSticks = mainPane.AddJapaneseCandleStick("candles", candleStickPoints);
                candleSticks.Stick.FallingFill = new Fill(Color.Red);
                candleSticks.Stick.RisingFill  = new Fill(Color.Blue);

                mainPane.XAxis.Type = AxisType.DateAsOrdinal;

                var trendLine = new TrendLine();
                trendLine.Refresh(pair.Symbol);

                var zigZag = new ZigZag();
                zigZag.Refresh(pair.Symbol);

                var ma15Points       = new PointPairList();
                var trendPoints      = new PointPairList();
                var zigzagBuyPoints  = new PointPairList();
                var zigzagSellPoints = new PointPairList();
                for (int i = lastCandle; i >= 0; i--)
                {
                    var candle = candles[i];
                    ma15Points.Add(new XDate(candle.OpenTime),
                                   (double)MovingAverage.Get(pair.Symbol, i, 15, MaMethod.Sma, AppliedPrice.Close));

                    trendPoints.Add(new XDate(candle.OpenTime),
                                    (double)trendLine.GetValue(i)
                                    );

                    var arrow = zigZag.GetArrow(i);
                    if (arrow == ArrowType.Buy)
                    {
                        zigzagBuyPoints.Add(new XDate(candle.OpenTime), (double)candle.Low);
                    }
                    else
                    {
                        zigzagBuyPoints.Add(new XDate(candle.OpenTime), (double)0);
                    }

                    if (arrow == ArrowType.Sell)
                    {
                        zigzagSellPoints.Add(new XDate(candle.OpenTime), (double)candle.High);
                    }
                    else
                    {
                        zigzagSellPoints.Add(new XDate(candle.OpenTime), (double)0);
                    }
                }
                var ma15Curve  = mainPane.AddCurve("MA15", ma15Points, Color.DarkGray, SymbolType.None);
                var trendCurve = mainPane.AddCurve("Trend", trendPoints, Color.Green, SymbolType.None);

                var zigZagCurveBuy  = mainPane.AddCurve("", zigzagBuyPoints, Color.Green, SymbolType.Triangle);
                var zigZagCurveSell = mainPane.AddCurve("", zigzagSellPoints, Color.Red, SymbolType.TriangleDown);
                zigZagCurveBuy.Line.StepType  = StepType.ForwardSegment;
                zigZagCurveSell.Line.StepType = StepType.ForwardSegment;

                // pretty it up a little
                mainPane.Chart.Fill = new Fill(Color.LightGray, Color.LightGray, 45.0f);
                mainPane.Fill       = new Fill(Color.LightGray, Color.LightGray, 45.0f);

                // Tell ZedGraph to calculate the axis ranges
                zedGraphControl1.AxisChange();
                zedGraphControl1.Invalidate();
                if (_currentPair != pairName)
                {
                    _currentPair = pairName;
                    zedGraphControl1.RestoreScale(mainPane);
                }
            }
        }
        public Signal Process()
        {
            if (Symbol.Candles == null)
            {
                return(_signal);
            }
            if (Symbol.Candles.Count == 0)
            {
                return(_signal);
            }
            _zigZag.Refresh(Symbol);
            _mbfx.Refresh(Symbol);
            _trendLine.Refresh(Symbol);

            int     zigZagBar   = -1;
            bool    zigZagBuy   = false;
            bool    zigZagSell  = false;
            bool    mbfxOk      = false;
            bool    trendOk     = false;
            bool    sma15Ok     = false;
            decimal zigZagPrice = 0M;

            // Rule #1: a zigzar arrow appears
            for (int bar = ZigZagCandles; bar >= 1; bar--)
            {
                var arrow = _zigZag.GetArrow(bar);
                if (arrow == ArrowType.Buy)
                {
                    zigZagBuy   = true;
                    zigZagSell  = false;
                    zigZagBar   = bar;
                    zigZagPrice = Symbol.Candles[bar].Low;
                }

                if (arrow == ArrowType.Sell)
                {
                    break;
                    //zigZagBuy = false;
                    //zigZagSell = true;
                    //zigZagBar = bar;
                    //zigZagPrice = Symbol.Candles[bar].High;
                }
            }

            // BUY signals
            if (zigZagBuy && zigZagBar > 0)
            {
                // MBFX should be green at the moment
                // and should have been below < 30 some candles ago
                int barStart = zigZagBar;
                if (zigZagBar == 1)
                {
                    barStart = 2;
                }
                for (int bar = Math.Min(barStart, MBFXCandles); bar >= 1; bar--)
                {
                    var red   = _mbfx.RedValue(bar);
                    var green = _mbfx.GreenValue(bar);
                    if (red < 30M || green < 30M)
                    {
                        mbfxOk = true;
                    }
                }

                // trend line should be green at the moment
                if (_trendLine.IsGreen(1))
                {
                    trendOk = true;
                }

                // rule #4: price should be above 15 SMA
                var ma1 = MovingAverage.Get(Symbol, 1, MovingAveragePeriod, MaMethod.Sma, AppliedPrice.Close);
                if (Symbol.Candles[1].Close > ma1)
                {
                    sma15Ok = true;
                }
            }

            /*
             * // SELL signals
             * if (zigZagSell && zigZagBar > 0)
             * {
             *  // MBFX should now be red
             *  // and should been above > 70 some candles ago
             *  int barStart = zigZagBar;
             *  if (zigZagBar == 1) barStart = 2;
             *  barStart = Math.Min(barStart, MBFXCandles);
             *  for (int bar = barStart; bar >= 1; bar--)
             *  {
             *      var red = _mbfx.RedValue(bar);
             *      var green = _mbfx.GreenValue(bar);
             *      if ((red > 70M && red < 200M) || (green > 70M && green < 200M))
             *      {
             *          mbfxOk = true;
             *      }
             *  }
             *
             *  // trend line should now be red
             *  if (_trendLine.IsRed(1))
             *  {
             *      trendOk = true;
             *  }
             *
             *  // rule #4: and price below SMA15 on previous candle
             *  var ma1 = MovingAverage.Get(Symbol, 1, MovingAveragePeriod, MaMethod.Sma, AppliedPrice.Close);
             *  if (Symbol.Candles[1].Close < ma1)
             *  {
             *      sma15Ok = true;
             *  }
             * }
             */

            // set indicators
            if (zigZagBar >= 1 && (zigZagBuy || zigZagSell))
            {
                _signal.Indicators[0].IsValid = true;
                _signal.Indicators[1].IsValid = mbfxOk;
                _signal.Indicators[2].IsValid = trendOk;
                _signal.Indicators[3].IsValid = sma15Ok;
                _signal.Indicators[4].IsValid = _supportResistance.IsAtSupportResistance(zigZagPrice, Symbol.AverageCandleSize);
                _signal.Type = zigZagBuy ? SignalType.Buy : SignalType.Sell;
                if (_signal.Indicators[4].IsValid)
                {
                    if (zigZagBuy)
                    {
                        _signal.CloseAtPrice = _supportResistance.GetNextResistanceLevel(zigZagPrice, Symbol.AverageCandleSize);
                    }
                    else if (zigZagSell)
                    {
                        _signal.CloseAtPrice = _supportResistance.GetNextSupportLevel(zigZagPrice, Symbol.AverageCandleSize);
                    }
                }
                var validCount = _signal.Indicators.Count(e => e.IsValid);
                if (validCount == 5)
                {
                    string signal        = zigZagBuy ? "Buy " : "Sell ";
                    string signalInverse = "Take profit @ ";
                    string alert         = $"{signal} {Symbol.NiceName} {Symbol.NiceTimeFrame}\r\n{signalInverse} at {_signal.CloseAtPrice}";
                    Symbol.SendAlert(alert);
                }
            }
            else
            {
                _signal.Type = SignalType.None;
            }
            return(_signal);
        }