예제 #1
0
        public StrategyDecision GetDecision(IDictionary <string, IList <ICandle> > data, string name, string currentPos, Security security, DateTime CurrentDt)
        {
            StrategyDecision dec = new StrategyDecision()
            {
                Decision = null
            };

            SimpleMovingAverage short_ma = new SimpleMovingAverage(data["60"], 9);
            SimpleMovingAverage long_ma  = new SimpleMovingAverage(data["60"], 20);

            TRENDResult trend = new TREND(long_ma, 3).GetResult();

            if (trend == TRENDResult.Up && new Crossover(short_ma, long_ma).GetResult())
            {
                dec.Decision = "open long";
            }

            if (trend == TRENDResult.Down && new Crossover(long_ma, short_ma).GetResult())
            {
                dec.Decision = "open short";
            }



            return(dec);
        }
예제 #2
0
        public StrategyDecision GetDecision(IDictionary<string, IList<ICandle>> data, string name, string currentPos, Security security, DateTime CurrentDt)
        {
            StrategyDecision dec = new StrategyDecision() { Decision = null };

            FilterResult local_offset = new LocalOffsetFilter(data).Exec();
            FilterResult trend = new TrendFilter(data).Exec();
            FilterResult candlePattern = new CandleFilter(data).Exec();
            FilterResult cross_ma = new CrossMAFilter(data).Exec();

            ICandle curr = data["5"].Last();

            dec.Code = name;
            dec.LastPrice = curr.close;
            dec.LongPrice = Math.Round(curr.close * 1.005m, 2);
            dec.ShortPrice = Math.Round(curr.close * 0.995m, 2);

            dec.Profit = Math.Round(curr.close * 0.015m, 2);//профит 1,5%
            dec.StopLoss = Math.Round(curr.close * 0.01m, 2);//стоп лосс 1,0%

            IList<FilterResult> filterResult_list = new List<FilterResult>();

            if (local_offset == FilterResult.L && (trend == FilterResult.L || trend == FilterResult.N) && candlePattern == FilterResult.L && cross_ma == FilterResult.L && currentPos == "free")
            {
                //покупаем
                dec.Decision = "open long";
            }

            if (local_offset == FilterResult.S && (trend == FilterResult.S || trend == FilterResult.N) && candlePattern == FilterResult.S && cross_ma == FilterResult.S && currentPos == "free")
            {
                //продаем
                dec.Decision = "open short";
            }

            MathFunctions func = new MathFunctions();

            dec.Profit = func.Optimize(dec.Profit, security.MinStep);
            dec.StopLoss = func.Optimize(dec.StopLoss, security.MinStep);
            dec.LongPrice = func.Optimize(dec.LongPrice, security.MinStep);
            dec.ShortPrice = func.Optimize(dec.ShortPrice, security.MinStep);

            return dec;
        }
예제 #3
0
        public void Run()
        {
            //IList<string> securities = new List<string>(new string[] { "GMKN", "LKOH", "GAZP", "MOEX", "SBER", "NVTK", "AFLT", "CHMF", "MFON", "ALRS", "MAGN", "MTLR", "MVID" });
            IList <string> securities = _securitylist.Select(s => s.Code).ToList();

            //IList<string> securities = new List<string>(new string[] { "GAZP" });

            Messenger.Default.Send <InitTradeModelMessage>(new InitTradeModelMessage()
            {
                securities = securities,
                frames     = _strategy.Need()
            }, TokenModel.Instance.AnalystToken());

            Messenger.Default.Register <GetCandlesResponseMessage>(this, TokenModel.Instance.AnalystToken(), (msg) =>
            {
                IList <AnalystData> analystData = new List <AnalystData>();

                foreach (string sec in msg.Сandles.Keys)
                {
                    Security securirty = _securitylist.Single(s => s.Code == sec);

                    StrategyDecision dec = _strategy.GetDecision(msg.Сandles[sec], sec, null, securirty, msg.DateTime);
                    analystData.Add(new AnalystData()
                    {
                        Sec    = sec,
                        Advice = string.IsNullOrEmpty(dec.Decision) ? "Neutral" : dec.Decision
                    });
                }
                ;

                Messenger.Default.Send <ShowDataMessage>(new ShowDataMessage()
                {
                    AnalystDatalist = analystData,
                    Сandles         = msg.Сandles
                });
            });
        }
예제 #4
0
        public void Run()
        {
            IList <string> securities = _securitylist.Select(s => s.Code).ToList();

            Messenger.Default.Send <InitTradeModelMessage>(new InitTradeModelMessage()
            {
                securities = securities,
                frames     = _strategy.Need()
            }, TokenModel.Instance.RobotToken());

            Messenger.Default.Register <GetCandlesResponseMessage>(this, TokenModel.Instance.RobotToken(), (msg) =>
            {
                IList <AnalystData> analystData = new List <AnalystData>();

                _decisionlist.Clear();

                foreach (string sec in msg.Сandles.Keys)
                {
                    Security securirty = _securitylist.Single(s => s.Code == sec);

                    StrategyDecision dec = _strategy.GetDecision(msg.Сandles[sec], sec, msg.Positions[sec], securirty, msg.DateTime);

                    dec.Count = securirty.DealSize;

                    _decisionlist.Add(dec);

                    analystData.Add(new AnalystData()
                    {
                        Sec       = sec,
                        LastPrice = dec.LastPrice,
                        Advice    = string.IsNullOrEmpty(dec.Decision) ? "Neutral" : dec.Decision
                    });

                    if (!string.IsNullOrEmpty(dec.Decision))
                    {
                        actions[dec.Decision].Invoke(sec, dec);
                    }
                }

                Messenger.Default.Send <ShowAnalystDataMessage>(new ShowAnalystDataMessage()
                {
                    AnalystDatalist = analystData
                });
            });

            Messenger.Default.Register <ClientMakeDealMessage>(this, (msg) =>
            {
                var dec = _decisionlist.SingleOrDefault(d => d.Code == msg.Sec);

                actions[msg.Operation].Invoke(msg.Sec, dec);
            });


            _stop = new System.Threading.CancellationTokenSource();
            System.Threading.CancellationToken token = _stop.Token;

            Task t = Task.Run(() =>
            {
                while (true)
                {
                    Task.Delay(5000).Wait();

                    //Messenger.Default.Send<GetCandlesMessage>(new GetCandlesMessage());

                    if (token.IsCancellationRequested)
                    {
                        token.ThrowIfCancellationRequested();
                    }
                }
            }, token);
        }
예제 #5
0
        public StrategyDecision GetDecision(IDictionary <string, IList <ICandle> > data, string name, string currentPos, Security security, DateTime CurrentDt)
        {
            StrategyDecision dec = new StrategyDecision()
            {
                Decision = null
            };

            SimpleMovingAverage short_ma = new SimpleMovingAverage(data["5"], 9);
            SimpleMovingAverage long_ma  = new SimpleMovingAverage(data["5"], 30);

            MACD        macd      = new MACD(data["5"], 12, 26, 9);
            TREND       macdTrend = new TREND(macd, 2);
            TRENDResult power     = macdTrend.GetResult();

            TRENDResult trend = new TREND(long_ma, 3).GetResult();

            AverageTrueRange atr = new AverageTrueRange(data["5"], 14);

            //dec.Profit = Math.Round(atr.Last() * 2m, 2);
            //dec.StopLoss = Math.Round(atr.Last(), 2);

            dec.Profit   = Math.Round(data["5"].Last().close * 0.006m, 2);
            dec.StopLoss = Math.Round(data["5"].Last().close * 0.002m, 2);


            decimal vollast = data["5"].Last().volume;
            decimal volprev = data["5"][data["5"].Count - 2].volume;

            bool vol = vollast > volprev * 2m;

            //if (power == TRENDResult.StrongUp && new Crossover(short_ma, long_ma).GetResult())
            //{
            //    //dec.Decision = "open long";

            //    //dec.Price = Math.Round(data["5"].Last().close * 1.005m, 2);

            //    signals.Add(new Signal() { Security = name, Action = "open long", DateTime = CurrentDt, Price= data["5"].Last().close });
            //}
            //else
            if (power == TRENDResult.StrongUp)
            {
                //dec.Decision = "open long";

                //dec.Price = Math.Round(data["5"].Last().close * 1.005m, 2);
                signals.Add(new Signal()
                {
                    Security = name, Action = "open long", DateTime = CurrentDt, Price = data["5"].Last().close
                });
            }

            if (currentPos == "free")
            {
                DateTime minDt = CurrentDt.AddMinutes(-20);
                DateTime maxDt = CurrentDt.AddMinutes(-15);

                var lastSignals = signals.Where(s => s.Security == name && s.Action == "open long" && s.DateTime < maxDt && s.DateTime >= minDt).ToList();

                Signal signal = lastSignals.FirstOrDefault();

                decimal summ = 0;

                if (signal != null)
                {
                    var candlesToCheck = data["5"].Where(c => c.begin >= signal.DateTime).ToList();

                    foreach (Candle c in candlesToCheck)
                    {
                        summ += c.close - c.open;
                    }
                }


                if (summ > atr.Last() * 2m && data["5"].Last().close > signal.Price)
                {
                    dec.Decision = "open long";

                    dec.LongPrice = Math.Round(data["5"].Last().close * 1.005m, 2);
                }
            }



            //if ((power == TRENDResult.Down || power == TRENDResult.StrongDown) && new Crossover(long_ma, short_ma).GetResult() && currentPos == "free")
            //{
            //    dec.Decision = "open short";

            //    dec.Price = Math.Round(data[5].Last().close * 0.995m, 2);
            //}


            if (security.MinStep == 1)
            {
                dec.Profit    = Math.Round(dec.Profit);
                dec.StopLoss  = Math.Round(dec.StopLoss);
                dec.LongPrice = Math.Round(dec.LongPrice);
            }

            //dec.Decision = "open long";

            return(dec);
        }
예제 #6
0
        public StrategyDecision GetDecision(IDictionary <string, IList <ICandle> > data, string name, string currentPos, Security security, DateTime CurrentDt)
        {
            StrategyDecision dec = new StrategyDecision()
            {
                Decision = null
            };

            ExponentialMovingAverage hours_ema = new ExponentialMovingAverage(data["60"], 9);

            //ExponentialMovingAverage minutes_ema = new ExponentialMovingAverage(data[5], 4);

            MACD macd = new MACD(data["5"], 12, 26, 9);

            TREND hoursTrend = new TREND(hours_ema, 3);
            //TREND minutesTrend = new TREND(minutes_ema, 3);
            TREND macdTrend    = new TREND(macd, 2);
            bool  valueConfirm = new ValueConfirm(data["5"], 5).GetResult();

            AverageTrueRange atr = new AverageTrueRange(data["5"], 14);

            Extremum extremum = new Extremum(atr, 3, 20);

            //hoursTrend.GetResult();
            //macdTrend.GetResult();

            //if ((hoursTrend.GetResult() == TRENDResult.Up && macdTrend.GetResult() == TRENDResult.Up || macdTrend.GetResult() == TRENDResult.StrongUp) && currentPos == "free")
            //{
            //    dec = "open long";
            //}

            //if ((hoursTrend.GetResult() == TRENDResult.Down && macdTrend.GetResult() == TRENDResult.Down || macdTrend.GetResult() == TRENDResult.StrongDown) && currentPos == "free")
            //{
            //    dec = "open short";
            //}

            TRENDResult trend = hoursTrend.GetResult();
            TRENDResult power = macdTrend.GetResult();


            decimal value = data["5"].Last().value;

            //*if (extremum.Count() > 0 && extremum.Last().ext == "L" && extremum.Last().val < atr.Last())
            if (atr.Last() > 10m && valueConfirm)
            {
                dec.Profit   = Math.Round(atr.Last() * 4m, 2);
                dec.StopLoss = Math.Round(atr.Last(), 2);



                if (trend == TRENDResult.Up && (power == TRENDResult.Up || power == TRENDResult.StrongUp) && currentPos == "free")
                {
                    dec.Decision = "open long";
                }

                if (trend == TRENDResult.Flat && power == TRENDResult.StrongUp && currentPos == "free")
                {
                    dec.Decision = "open long";
                }

                if (trend == TRENDResult.Down && (power == TRENDResult.Down || power == TRENDResult.StrongDown) && currentPos == "free")
                {
                    dec.Decision = "open short";
                }

                if (trend == TRENDResult.Flat && power == TRENDResult.StrongDown && currentPos == "free")
                {
                    dec.Decision = "open short";
                }
            }

            if (CurrentDt >= new DateTime(2017, 5, 15, 15, 20, 0))
            {
                //dec = null;
            }

            return(dec);
        }