Exemplo n.º 1
0
 //+------------------------------------------------------------------+
 private bool DoesSRLevelExists(decimal price, decimal range, out SRLine srLine)
 {
     srLine = null;
     if (_lines.Count <= 0)
     {
         return(false);
     }
     foreach (var line in _lines)
     {
         var diff = Math.Abs(price - line.Price);
         if (diff <= range)
         {
             srLine = line;
             return(true);
         }
     }
     return(false);
 }
Exemplo n.º 2
0
        //+------------------------------------------------------------------+
        private void Refresh()
        {
            _lines = new List <SRLine>();

            var barsAvailable = _symbol.Candles.Count;
            var bars          = Math.Min(_barsHistory, barsAvailable);
            var lowestBar     = _symbol.LowestBar;
            var highestBar    = _symbol.HighestBar;
            var highestPrice  = _symbol.Candles[highestBar].High;
            var lowestPrice   = _symbol.Candles[lowestBar].Low;

            decimal priceRange = highestPrice - lowestPrice;

            decimal div = 30.0M;

            switch (_detailLevel)
            {
            case Details.Minium:
                div = 10.0M;
                break;

            case Details.MediumLow:
                div = 20.0M;
                break;

            case Details.Medium:
                div = 30.0M;
                break;

            case Details.MediumHigh:
                div = 40.0M;
                break;

            case Details.Maximum:
                div = 50.0M;
                break;
            }
            _maxDistance = priceRange / div;

            var zigZag = new ZigZag(12, 5, 3);

            zigZag.Refresh(_symbol);

            bool skipFirstArrow = true;

            for (int bar = 1; bar < bars; bar++)
            {
                var candle = _symbol.Candles[bar];
                var arrow  = zigZag.GetArrow(bar);
                if (arrow == ArrowType.None)
                {
                    continue;
                }
                if (skipFirstArrow)
                {
                    skipFirstArrow = false;
                    continue;
                }

                if (arrow == ArrowType.Buy)
                {
                    var price     = candle.Low;
                    var time      = candle.OpenTime;
                    var startTime = time;
                    int startBar  = bar;
                    if (!DoesLevelExists(bar, price, startTime))
                    {
                        int touches = GetTouches(zigZag, bar, bars, ref price, ref startTime, ref startBar);
                        if (touches >= 0)
                        {
                            var line = new SRLine();
                            line.Price     = price;
                            line.Touches   = touches;
                            line.EndBar    = bar;
                            line.EndDate   = time;
                            line.StartDate = startTime;
                            line.StartBar  = startBar;
                            _lines.Add(line);
                        }
                    }
                }
                else if (arrow == ArrowType.Sell)
                {
                    var price     = candle.High;
                    var time      = candle.OpenTime;
                    var startTime = time;
                    int startBar  = bar;
                    if (!DoesLevelExists(bar, price, startTime))
                    {
                        int touches = GetTouches(zigZag, bar, bars, ref price, ref startTime, ref startBar);
                        if (touches >= 0)
                        {
                            var line = new SRLine();
                            line.Price     = price;
                            line.Touches   = touches;
                            line.EndBar    = bar;
                            line.EndDate   = time;
                            line.StartDate = startTime;
                            line.StartBar  = startBar;
                            _lines.Add(line);
                        }
                    }
                }
            }

            // add s/r line for highest price
            var mostRecentTime = _symbol.Candles[highestBar].OpenTime;

            if (!DoesLevelExists(highestBar, highestPrice, mostRecentTime))
            {
                var line = new SRLine();
                line.Price     = highestPrice;
                line.Touches   = 1;
                line.StartBar  = highestBar;
                line.StartDate = _symbol.Candles[highestBar].OpenTime;
                line.EndDate   = _symbol.Candles[0].CloseTime;
                line.EndBar    = 0;
                _lines.Add(line);
            }

            // add s/r line for lowest price
            mostRecentTime = _symbol.Candles[lowestBar].OpenTime;
            if (!DoesLevelExists(lowestBar, lowestPrice, mostRecentTime))
            {
                var line = new SRLine();
                line.Price     = lowestPrice;
                line.Touches   = 1;
                line.StartBar  = lowestBar;
                line.StartDate = _symbol.Candles[lowestBar].OpenTime;
                line.EndDate   = _symbol.Candles[0].CloseTime;
                line.EndBar    = 0;
            }
        }