public BasicTradeState(RobotContext context, Deal deal)
 {
     context.Logger.Debug("Change state to {0}", GetType().Name);
     StartPrice = deal.Price;
     TrendIsLong = deal.IsBuy;
     context.StopLossPrice = GetStopPrice(StartPrice, context.StopLossSize);
 }
 public IState GetTradeState(RobotContext context, Deal deal)
 {
     switch (TradeStateType)
     {
         case TradeStateTypes.Basic:
             return new BasicTradeState(context, deal);
         case TradeStateTypes.Breakeven:
             return new BreakevenTradeState(context, deal);
         case TradeStateTypes.Trailing:
             return new TrailingTradeState(context, deal);
         default:
             throw new ArgumentException("Not expected trade state type");
     }
 }
        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 TrailingTradeState(RobotContext context, Deal deal)
     : base(context, deal)
 {
 }
 public BreakevenTradeState(RobotContext context, Deal deal)
     : base(context, deal)
 {
 }
 public void AddDeal(Deal deal)
 {
     if (!deals.Any() || deals.Peek().IsBuy == deal.IsBuy)
     {
         deals.Push(deal);
         return;
     }
     var prevDeal = deals.Pop();
     var profit = (deal.Price - prevDeal.Price)*(prevDeal.IsBuy ? 1 : -1) - Comission;
     AddTrade(new Trade(profit, prevDeal.IsBuy, deal.DateTime - prevDeal.DateTime, prevDeal.Advice));
 }