Esempio n. 1
0
 public Day(List<Candle> fiveMins)
 {
     FiveMins = fiveMins;
     Params = new Candle(fiveMins.First().Date + fiveMins.First().Time,
                         fiveMins.First().Open,
                         fiveMins.Max(c => c.High),
                         fiveMins.Min(c => c.Low),
                         fiveMins[fiveMins.Count - 1].Close,
                         24*60);
 }
Esempio n. 2
0
        public void AddCandle(Candle candle)
        {
            if (history.Any() && history.GetAtBack().DateTime >= candle.DateTime)
                throw new Exception("Last candle in history is later then added");

            history.AddToBack(candle);
            if (history.Count > maxHistoryCount)
            {
                history.RemoveFromFront();
            }
        }
Esempio n. 3
0
        private Extremum AppendToTree(Candle candle, int currentIndex)
        {
            var leftIter = searchTree.First;
            Extremum lastSecondExtremum = null;
            while (leftIter != null)
            {
                var midIter = leftIter.Value.Children.First;
                while (midIter != null)
                {
                    if (IsSkipped(midIter.Value.Candle, candle))
                    {
                        midIter = midIter.Next;
                        continue;
                    }

                    if (IsPegTop(leftIter.Value.Candle) && IsPegTop(midIter.Value.Candle) && IsPegTop(candle))
                    {
                        midIter = midIter.Next;
                        continue;
                    }

                    var extremum = TryGetExtremum(leftIter.Value.Candle, midIter.Value.Candle, candle, currentIndex);
                    if (extremum == null)
                    {
                        leftIter = searchTree.RemoveElement(leftIter);
                        goto NextIteration;
                    }

                    var secondExtremum = ExtremumsRepo.AddExtremum(extremum);
                    if (secondExtremum != null && (lastSecondExtremum == null || lastSecondExtremum.DateTime < secondExtremum.DateTime))
                    {
                        lastSecondExtremum = secondExtremum;
                    }

                    midIter = midIter.RemoveFromList();
                }

                if (!IsSkipped(leftIter.Value.Candle, candle))
                {
                    leftIter.Value.Children.AddLast(new CandleNode(candle, currentIndex));
                }
                leftIter = leftIter.Next;
            NextIteration:;
            }

            searchTree.AddLast(new CandleNode(candle, currentIndex));
            return lastSecondExtremum;
        }
Esempio n. 4
0
        public Candle AddTick(Tick tick)
        {
            try
            {
                if (candle.AppendTick(tick))
                {
                    var res = candle;
                    candle = new Candle(new List<Tick> { tick }, periodMins);
                    return res;
                }
            }
            catch (Exception ex)
            {
                Logger.Warn(ex);
            }

            return candle;
        }
        public virtual ITradeEvent Process(RobotContext context, Candle candle)
        {
            if (candle.Time >= context.EndTime)
            {
                context.Logger.Debug("End time exit. StopPrice: {0}, Candle: {1}", context.StopLossPrice, candle);
                context.CurrentState = context.Factory.GetEndState(context);
                return new DealEvent(new Deal(candle.Close, candle.DateTime, !TrendIsLong));
            }

            if (TrendIsLong && candle.Low <= context.StopLossPrice ||
                !TrendIsLong && candle.High >= context.StopLossPrice)
            {
                context.Logger.Debug("Stop has catched. StopPrice: {0}, Candle: {1}", context.StopLossPrice, candle);
                context.CurrentState = context.Factory.GetEndState(context);
                return new StopLossEvent(new Deal(context.StopLossPrice, candle.DateTime, !TrendIsLong));
            }

            return null;
        }
Esempio n. 6
0
        public ITradeEvent Process(RobotContext context, Candle candle)
        {
            int currentIndex = context.Candles.Count - 1;
            var bestSecondExtremum = AppendToTree(candle, currentIndex);

            if (bestSecondExtremum == null)
                return null;

            if (NeedToTrade(context, bestSecondExtremum))
            {
                context.Logger.Debug("Need to trade, extremum: {0}", bestSecondExtremum);
                var deal = new Deal(candle.Close, candle.DateTime, bestSecondExtremum.IsMinimum, context.Advisor.GetAdvice(candle.Close, bestSecondExtremum.IsMinimum));

                context.Logger.Debug("Deal: {0}", deal);
                context.CurrentState = context.Factory.GetTradeState(context, deal);
                return new DealEvent(deal);
            }

            return new SecondExtremumEvent(bestSecondExtremum, ExtremumsRepo.FirstMaximums, ExtremumsRepo.FirstMinimums);
        }
        public override ITradeEvent Process(RobotContext context, Candle candle)
        {
            var result = base.Process(context, candle);
            if (result != null)
                return result;

            if (!hasBreakeven &&
                (TrendIsLong && candle.High >= StartPrice + context.StopLossSize ||
                !TrendIsLong && candle.Low <= StartPrice - context.StopLossSize))
            {
                hasBreakeven = true;

                context.StopLossPrice = GetStopPrice(StartPrice, -context.BreakevenSize);

                context.Logger.Debug("Breakeven. StopPrice: {0}, Candle: {1}", context.StopLossPrice, candle);
                return new StopLossMovingEvent(context.StopLossPrice, TrendIsLong);
            }

            return null;
        }
        public override ITradeEvent Process(RobotContext context, Candle candle)
        {
            var result = base.Process(context, candle);
            if (result != null)
                return result;

            if (TrendIsLong)
            {
                if (candle.High - context.StopLossPrice >= context.TrailingStopLoss)
                {
                    var newPrice = GetStopPrice(candle.High, context.TrailingStopLoss);
                    if (newPrice > context.StopLossPrice	//if stopSize < trailingStopSize
                        && newPrice < candle.Close)
                    {
                        context.StopLossPrice = newPrice;
                        context.Logger.Debug("Trailing moving. StopPrice: {0}, Candle: {1}", context.StopLossPrice, candle);
                        result = new StopLossMovingEvent(context.StopLossPrice, TrendIsLong);
                    }
                }
            }
            else
            {
                if (context.StopLossPrice - candle.Low >= context.TrailingStopLoss)
                {
                    var newPrice = GetStopPrice(candle.Low, context.TrailingStopLoss);
                    if (newPrice < context.StopLossPrice
                        && newPrice > candle.Close)
                    {
                        context.StopLossPrice = newPrice;
                        context.Logger.Debug("Trailing moving. StopPrice: {0}, Candle: {1}", context.StopLossPrice, candle);
                        result = new StopLossMovingEvent(context.StopLossPrice, TrendIsLong);
                    }
                }
            }

            return result;
        }
Esempio n. 9
0
 public Extremum(Candle extremumCandle, int checkerIndex, bool isMinimum)
     : this(isMinimum ? extremumCandle.Low : extremumCandle.High, checkerIndex, extremumCandle.DateTime, isMinimum)
 {
 }
Esempio n. 10
0
		public bool IsOuterTo(Candle c) => High >= c.High && Low <= c.Low;
Esempio n. 11
0
        private Extremum TryGetExtremum(Candle leftCandle, Candle midCandle, Candle rightCandle, int rightIndex)
        {
            var isMinimum = IsMinimum(leftCandle, midCandle, rightCandle);
            if (!isMinimum && !IsMaximum(leftCandle, midCandle, rightCandle))
                return null;

            return new Extremum(midCandle, rightIndex, isMinimum);
        }
Esempio n. 12
0
 private bool IsSkipped(Candle previous, Candle current)
 {
     return current.IsOuterTo(previous) || current.IsInnerTo(previous);
 }
Esempio n. 13
0
 private bool IsPegTop(Candle candle)
 {
     return Math.Abs(candle.Open - candle.Close) <= pegTopSize;
 }
Esempio n. 14
0
		public bool IsInnerTo(Candle c) => High <= c.High && Low >= c.Low;
Esempio n. 15
0
 private bool IsMaximum(Candle left, Candle mid, Candle right)
 {
     return mid.High > left.High && mid.High > left.High && mid.Low >= left.Low && mid.Low >= right.Low;
 }
Esempio n. 16
0
 private static Candle ParseCandle(string row)
 {
     var fields = row.Split('\t');
     var candle = new Candle(ParseDateTime(fields[0], fields[1]),
                             (int)decimal.Parse(fields[2], new CultureInfo("en-us")),
                             (int)decimal.Parse(fields[3], new CultureInfo("en-us")),
                             (int)decimal.Parse(fields[4], new CultureInfo("en-us")),
                             (int)decimal.Parse(fields[5], new CultureInfo("en-us")),
                             5);
     return candle;
 }
Esempio n. 17
0
 public Day(Candle @params, List<Candle> fiveMins)
 {
     Params = @params;
     FiveMins = fiveMins;
 }
Esempio n. 18
0
 public ITradeEvent Process(RobotContext context, Candle candle)
 {
     return new EndEvent();
 }
Esempio n. 19
0
 public CandleNode(Candle candle, int candleIndex)
 {
     Candle = candle;
     CandleIndex = candleIndex;
     Children = new LinkedList<CandleNode>();
 }
Esempio n. 20
0
 public ITradeEvent Process(Candle candle)
 {
     Advisor.AddCandle(candle);
     candles.Add(candle);
     var result = CurrentState.Process(this, candle);
     return result;
 }
Esempio n. 21
0
 private bool IsMinimum(Candle left, Candle mid, Candle right)
 {
     return mid.Low < left.Low && mid.Low < left.Low && mid.High <= left.High && mid.High <= right.High;
 }