Beispiel #1
0
 public TradeIdeaLog(string symbol, TradeIdeasGeneratorArgument argument, DateTime date)
 {
     Symbol  = symbol;
     Date    = date;
     Results = new List <TradeIdeaRule>();
     AdditionalInformation = new List <string>();
     Parameters            = new List <Tuple <string, object> >
     {
         new Tuple <string, object>("LastPrice", argument.LastPrice),
         new Tuple <string, object>("SmaVol(20)", argument.SmaVol20),
         new Tuple <string, object>("Sma(50)", argument.Sma50),
         new Tuple <string, object>("Rsi(14)", argument.Rsi14),
         new Tuple <string, object>("YesterdayRsi(14)", argument.YesterdayRsi14),
         new Tuple <string, object>("Cci(14)", argument.Cci14),
         new Tuple <string, object>("YesterdayCci(14)", argument.YesterdayCci14),
         new Tuple <string, object>("Stoch(14,14,3)", argument.Stoch14),
         new Tuple <string, object>("YesterdayStoch(14,14,3)", argument.YesterdayStoch14),
         new Tuple <string, object>("WillR(14)", argument.WillR14),
         new Tuple <string, object>("YesterdayWillR(14)", argument.YesterdayWillR14),
         new Tuple <string, object>("Mfi(14)", argument.Mfi14),
         new Tuple <string, object>("YesterdayMfi(14)", argument.YesterdayMfi14),
         new Tuple <string, object>("Adx(20)", argument.Adx20),
         new Tuple <string, object>("Atr(20)", argument.Atr20),
         new Tuple <string, object>("RangeStdDev", argument.RangeStdDev),
         new Tuple <string, object>("NearestSupport", argument.NearestSupport),
         new Tuple <string, object>("LtRsi(50)", argument.LtRsi50),
         new Tuple <string, object>("StRsi(5)", argument.StRsi5),
         new Tuple <string, object>("YesterdayStRsi(5)", argument.YesterdayStRsi5),
         new Tuple <string, object>("LtCci(50)", argument.LtCci50),
         new Tuple <string, object>("StCci(5)", argument.StCci5),
         new Tuple <string, object>("YesterdayStCci(5)", argument.YesterdayStCci5),
         new Tuple <string, object>("YesterdayHigh", argument.YesterdayHigh),
         new Tuple <string, object>("YesterdayLow", argument.YesterdayLow),
         new Tuple <string, object>("LongTerm Sentiment", argument.LongTermSentiment),
         new Tuple <string, object>("ShortTerm Sentiment", argument.ShortTermSentiment),
         new Tuple <string, object>("HasOption", argument.HasOption),
     };
 }
        private Tuple <TradeIdea, TradeIdeaLog> GenerateTradeIdeaForLatestDate(List <Signal> signals, HistoricalData historicalData)
        {
            Tuple <TradeIdea, TradeIdeaLog> result;

            DateTime date = historicalData.LatestDate;

            //TODO: should we remove this at all? now delayed
            //SupportAndResistance sr = _algoService.GetSupportAndResistance(historicalData, expandedQuote.Last);
            TradeIdeasGeneratorArgument args = TradeIdeasGeneratorArgument.Create(signals, historicalData /*, sr*/);

            double marketCap = 0;
            //if (quote != null)
            //{
            //    marketCap = quote.MarketCapitalization ?? 0;
            //}

            TradeIdeaLog tradeIdeaLog = new TradeIdeaLog(historicalData.SecurityCode, args, date);
            List <TradeIdeaRuleWrapper> generatedTradeIdeas = GenerateTradeIdeas(args, tradeIdeaLog);

            Signal syrahSentiment = signals
                                    .ForIndicator(TradeIdeasGeneratorArgument.LongTermSentimentForDependencies)
                                    .FirstOrDefault(s => s.Date == date);
            int?syrahSentimentLongTerm = syrahSentiment == null
                ? null
                : (int?)syrahSentiment.Value;

            syrahSentiment = signals
                             .ForIndicator(TradeIdeasGeneratorArgument.ShortTermSentimentForDependencies)
                             .FirstOrDefault(s => s.Date == date);
            int?syrahSentimentShortTerm = syrahSentiment == null
                ? null
                : (int?)syrahSentiment.Value;

            double lastPrice = historicalData.Close.Last();

            string companyName = historicalData.SecurityCode;

            if (generatedTradeIdeas != null && generatedTradeIdeas.Any())
            {
                TradeIdea tradeIdea = new TradeIdea
                {
                    DailyPlay               = false,
                    DateOfScan              = date,
                    MarketCap               = marketCap,
                    Price                   = lastPrice,
                    StockCode               = historicalData.SecurityCode,
                    CompanyName             = companyName,
                    SyrahSentimentValue     = syrahSentimentLongTerm,
                    SyrahSentimentShortTerm = syrahSentimentShortTerm,
                    RuleMatch               = generatedTradeIdeas.Select(item => item.Rule).ToList(),
                    RulesWithLogs           = generatedTradeIdeas
                };

                result = Tuple.Create(tradeIdea, tradeIdeaLog);
            }
            else
            {
                result = Tuple.Create((TradeIdea)null, tradeIdeaLog);
            }

            return(result);
        }
        /// <summary>
        /// Generate trade ideas rules.
        /// </summary>
        public List <TradeIdeaRuleWrapper> GenerateTradeIdeas(TradeIdeasGeneratorArgument arg, TradeIdeaLog tradeIdeaLog)
        {
            const double period         = 14;
            const string cciStudy       = "CCI";
            const string rsiStudy       = "RSI";
            const string mfiStudy       = "MFI";
            const string williamsRStudy = "Williams %R";

            List <TradeIdeaRuleWrapper> returnList = new List <TradeIdeaRuleWrapper>();

            //if (arg.HasOption)
            //{
            //if (NearResistance(arg.LastPrice, arg.NearestResistance, arg.RangeStdDev))
            if (arg.LongTermSentiment != null &&
                arg.LongTermSentiment.Value == Sentiment.Bearish &&
                arg.ShortTermSentiment != null &&
                arg.ShortTermSentiment.Value != Sentiment.Bullish)
            {
                if (ObBearScan(arg.YesterdayRsi14, arg.Rsi14, _rsiObThresh))
                {
                    returnList.Add(new TradeIdeaRuleWrapper
                    {
                        Rule            = TradeIdeaRule.RsiOverboughtBearishCrossover,
                        Study           = rsiStudy,
                        Period          = period,
                        OverBoughtLevel = _rsiObThresh,
                        OverSoldLevel   = _rsiOsThresh
                    });
                }
                if (ObBearScan(arg.YesterdayCci14, arg.Cci14, _cciObThresh))
                {
                    returnList.Add(new TradeIdeaRuleWrapper
                    {
                        Rule            = TradeIdeaRule.CciOverboughtBearishCrossover,
                        Study           = cciStudy,
                        Period          = period,
                        OverBoughtLevel = _cciObThresh,
                        OverSoldLevel   = _cciOsThresh
                    });
                }
                if (ObBearScan(arg.YesterdayWillR14, arg.WillR14, _willrObThresh))
                {
                    returnList.Add(new TradeIdeaRuleWrapper
                    {
                        Rule            = TradeIdeaRule.WilliamsOverboughtBearishCrossover,
                        Study           = williamsRStudy,
                        Period          = period,
                        OverBoughtLevel = _willrObThresh,
                        OverSoldLevel   = _willrOsThresh
                    });
                }
                if (ObBearScan(arg.YesterdayMfi14, arg.Mfi14, _mfiObThresh))
                {
                    returnList.Add(new TradeIdeaRuleWrapper
                    {
                        Rule            = TradeIdeaRule.MfiOverboughtBearishCrossover,
                        Study           = mfiStudy,
                        Period          = period,
                        OverBoughtLevel = _mfiObThresh,
                        OverSoldLevel   = _mfiOsThresh
                    });
                }
            }

            //if (NearSupport(arg.LastPrice, arg.NearestSupport, arg.RangeStdDev))
            if (arg.LongTermSentiment != null &&
                arg.LongTermSentiment.Value == Sentiment.Bullish &&
                arg.ShortTermSentiment != null &&
                arg.ShortTermSentiment.Value != Sentiment.Bearish)
            {
                if (OsBullScan(arg.YesterdayRsi14, arg.Rsi14, _rsiOsThresh))
                {
                    returnList.Add(new TradeIdeaRuleWrapper
                    {
                        Rule            = TradeIdeaRule.RsiOversoldBullishCrossover,
                        Study           = rsiStudy,
                        Period          = period,
                        OverBoughtLevel = _rsiObThresh,
                        OverSoldLevel   = _rsiOsThresh
                    });
                }
                if (OsBullScan(arg.YesterdayCci14, arg.Cci14, _cciOsThresh))
                {
                    returnList.Add(new TradeIdeaRuleWrapper
                    {
                        Rule            = TradeIdeaRule.CciOversoldBullishCrossover,
                        Study           = cciStudy,
                        Period          = period,
                        OverBoughtLevel = _cciObThresh,
                        OverSoldLevel   = _cciOsThresh
                    });
                }
                if (OsBullScan(arg.YesterdayWillR14, arg.WillR14, _willrOsThresh))
                {
                    returnList.Add(new TradeIdeaRuleWrapper
                    {
                        Rule            = TradeIdeaRule.WilliamsROversoldBullishCrossover,
                        Study           = williamsRStudy,
                        Period          = period,
                        OverBoughtLevel = _willrObThresh,
                        OverSoldLevel   = _willrOsThresh
                    });
                }
                if (OsBullScan(arg.YesterdayMfi14, arg.Mfi14, _mfiOsThresh))
                {
                    returnList.Add(new TradeIdeaRuleWrapper
                    {
                        Rule            = TradeIdeaRule.MfiOversoldBullishCrossover,
                        Study           = mfiStudy,
                        Period          = period,
                        OverBoughtLevel = _mfiObThresh,
                        OverSoldLevel   = _mfiOsThresh
                    });
                }
            }

            if (LtStDisagreementFilter(arg.Atr20, arg.YesterdayHigh, arg.YesterdayLow))
            {
                if (arg.LongTermSentiment != null &&
                    arg.LongTermSentiment.Value == Sentiment.Bearish &&
                    arg.ShortTermSentiment != null &&
                    arg.ShortTermSentiment.Value == Sentiment.Bearish)
                {
                    if (StLtBearDisagreementScan(arg.LtRsi50, arg.StRsi5, arg.YesterdayStRsi5, _ltRsiBearThresh, _stRsiBullThresh))
                    {
                        returnList.Add(new TradeIdeaRuleWrapper
                        {
                            Rule            = TradeIdeaRule.RsiRallyInBearishTrend,
                            Study           = rsiStudy,
                            Period          = 5,
                            OverBoughtLevel = _stRsiBullThresh,
                            OverSoldLevel   = _ltRsiBearThresh
                        });
                    }
                    if (StLtBearDisagreementScan(arg.LtCci50, arg.StCci5, arg.YesterdayStCci5, _ltCciBearThresh, _stCciBullThresh))
                    {
                        returnList.Add(new TradeIdeaRuleWrapper
                        {
                            Rule            = TradeIdeaRule.CciRallyInBearishTrend,
                            Study           = cciStudy,
                            Period          = 5,
                            OverBoughtLevel = _stCciBullThresh,
                            OverSoldLevel   = _ltCciBearThresh
                        });
                    }
                }

                if (arg.LongTermSentiment != null &&
                    arg.LongTermSentiment.Value == Sentiment.Bullish &&
                    arg.ShortTermSentiment != null &&
                    arg.ShortTermSentiment.Value == Sentiment.Bullish)
                {
                    if (StLtBullDisagreementScan(arg.LtRsi50, arg.StRsi5, arg.YesterdayStRsi5, _ltRsiBullThresh, _stRsiBearThresh))
                    {
                        returnList.Add(new TradeIdeaRuleWrapper
                        {
                            Rule            = TradeIdeaRule.RsiDipInBullishTrend,
                            Study           = rsiStudy,
                            Period          = 5,
                            OverBoughtLevel = _ltRsiBullThresh,
                            OverSoldLevel   = _stRsiBearThresh
                        });
                    }

                    if (StLtBullDisagreementScan(arg.LtCci50, arg.StCci5, arg.YesterdayStCci5, _ltCciBullThresh, _stCciBearThresh))
                    {
                        returnList.Add(new TradeIdeaRuleWrapper
                        {
                            Rule            = TradeIdeaRule.CciDipInBullishTrend,
                            Study           = cciStudy,
                            Period          = 5,
                            OverBoughtLevel = _ltCciBullThresh,
                            OverSoldLevel   = _stCciBearThresh
                        });
                    }
                }
            }
            //}

            foreach (TradeIdeaRule tradeIdeaRule in returnList.Select(item => item.Rule))
            {
                tradeIdeaLog.Results.Add(tradeIdeaRule);
            }

            return(returnList);
        }
        public DependencyScope GetDependencies(int numberOfBussinessDays = 1)
        {
            DependencyScope deps = TradeIdeasGeneratorArgument.GetDependencies(numberOfBussinessDays);

            return(deps);
        }
        internal static TradeIdeasGeneratorArgument Create(List <Signal> signals, HistoricalData historicalData)
        {
            TradeIdeasGeneratorArgument result = new TradeIdeasGeneratorArgument();

            SmaVol smaVol20 = new SmaVol(20);
            Sma    sma50    = new Sma(50);
            Rsi    stRsi5   = new Rsi(5);
            Rsi    rsi14    = new Rsi(14);
            Rsi    ltrsi50  = new Rsi(50);
            Cci    stCci5   = new Cci(5);
            Cci    cci14    = new Cci(14);
            Cci    ltCci50  = new Cci(50);
            Stoch  stoch14  = new Stoch(14, 14, 3);
            WillR  willr14  = new WillR(14);
            Mfi    mfi14    = new Mfi(14);
            Adx    adx20    = new Adx(20);
            Atr    atr20    = new Atr(20);

            //Assuming that signals are sorted by dates descending and all signals are present. otherwize an exception will be thrown during fetching signals (First())

            #region Indicators

            result.Rsi14           = GetValue(signals.LatestForIndicator(rsi14));
            result.YesterdayRsi14  = GetValue(signals.PreviousForIndicator(rsi14, 1));
            result.StRsi5          = GetValue(signals.LatestForIndicator(stRsi5));
            result.YesterdayStRsi5 = GetValue(signals.PreviousForIndicator(stRsi5, 1));
            result.LtRsi50         = GetValue(signals.LatestForIndicator(ltrsi50));

            result.Cci14           = GetValue(signals.LatestForIndicator(cci14));
            result.YesterdayCci14  = GetValue(signals.PreviousForIndicator(cci14, 1));
            result.StCci5          = GetValue(signals.LatestForIndicator(stCci5));
            result.YesterdayStCci5 = GetValue(signals.PreviousForIndicator(stCci5, 1));
            result.LtCci50         = GetValue(signals.LatestForIndicator(ltCci50));

            result.Stoch14          = GetValue(signals.LatestForIndicator(stoch14));
            result.YesterdayStoch14 = GetValue(signals.PreviousForIndicator(stoch14, 1));

            result.WillR14          = GetValue(signals.LatestForIndicator(willr14));
            result.YesterdayWillR14 = GetValue(signals.PreviousForIndicator(willr14, 1));

            result.Mfi14          = GetValue(signals.LatestForIndicator(mfi14));
            result.YesterdayMfi14 = GetValue(signals.PreviousForIndicator(mfi14, 1));

            result.SmaVol20 = GetValue(signals.LatestForIndicator(smaVol20));
            result.Sma50    = GetValue(signals.LatestForIndicator(sma50));

            result.Adx20 = GetValue(signals.LatestForIndicator(adx20));

            result.Atr20 = GetValue(signals.LatestForIndicator(atr20));

            //Long Term Sentiment(6 months)
            Signal syrahSentiment = signals.LatestForIndicator(LongTermSentimentForDependencies);
            int?   sentimentValue = syrahSentiment == null
                ? null
                : (int?)syrahSentiment.Value;
            result.LongTermSentiment = SyrahSentiment.MakeInterpretationInTermsOfSentiment(sentimentValue);

            //Short Term Sentiment(1 month)
            syrahSentiment = signals.LatestForIndicator(ShortTermSentimentForDependencies);
            sentimentValue = syrahSentiment == null
                ? null
                : (int?)syrahSentiment.Value;
            result.ShortTermSentiment = SyrahSentiment.MakeInterpretationInTermsOfSentiment(sentimentValue);

            #endregion

            //if (expandedQuote == null)
            //{
            //    result.LastPrice = historicalData.Close[historicalData.Count - 1];
            //}
            //else
            //{
            //    result.LastPrice = expandedQuote.Last;
            //    result.HasOption = expandedQuote.HasOption;
            //}

            result.RangeStdDev = historicalData.GetPriceRangeStdDevFor6Months();

            //result.NearestSupport = supportAndResistance.GetClosestSupport(expandedQuote.Last);
            //result.NearestResistance = supportAndResistance.GetClosestResistance(expandedQuote.Last);

            //TODO: check
            int yesterdayIndex = historicalData.High.Length - 2;
            result.YesterdayHigh = historicalData.High[yesterdayIndex];
            result.YesterdayLow  = historicalData.Low[yesterdayIndex];

            return(result);
        }