示例#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);
 }
示例#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();
            }
        }
示例#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;
        }
        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;
        }
示例#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;
        }
示例#9
0
 public Extremum(Candle extremumCandle, int checkerIndex, bool isMinimum)
     : this(isMinimum ? extremumCandle.Low : extremumCandle.High, checkerIndex, extremumCandle.DateTime, isMinimum)
 {
 }
示例#10
0
		public bool IsOuterTo(Candle c) => High >= c.High && Low <= c.Low;
示例#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);
        }
示例#12
0
 private bool IsSkipped(Candle previous, Candle current)
 {
     return current.IsOuterTo(previous) || current.IsInnerTo(previous);
 }
示例#13
0
 private bool IsPegTop(Candle candle)
 {
     return Math.Abs(candle.Open - candle.Close) <= pegTopSize;
 }
示例#14
0
		public bool IsInnerTo(Candle c) => High <= c.High && Low >= c.Low;
示例#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;
 }
示例#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;
 }
示例#17
0
 public Day(Candle @params, List<Candle> fiveMins)
 {
     Params = @params;
     FiveMins = fiveMins;
 }
示例#18
0
 public ITradeEvent Process(RobotContext context, Candle candle)
 {
     return new EndEvent();
 }
示例#19
0
 public CandleNode(Candle candle, int candleIndex)
 {
     Candle = candle;
     CandleIndex = candleIndex;
     Children = new LinkedList<CandleNode>();
 }
示例#20
0
 public ITradeEvent Process(Candle candle)
 {
     Advisor.AddCandle(candle);
     candles.Add(candle);
     var result = CurrentState.Process(this, candle);
     return result;
 }
示例#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;
 }