// If your activity returns a value, derive from CodeActivity<TResult>
        // and return the value from the Execute method.
        protected override void Execute(CodeActivityContext context)
        {
            string strSymbol = this.MySymbol.Get(context);
            string strConn = this.MyConnString.Get(context);

            using (StockDBDataContext dbContext = new StockDBDataContext(strConn))
            {
                StockPick lastPick = (from a in dbContext.StockPicks
                                      where a.Symbol == strSymbol
                                      && a.PickType == 2
                                      orderby a.PickDate descending
                                      select a).FirstOrDefault();

                DateTime lastDtPick = DateTime.Now.AddYears(-20);

                if (lastPick != null)
                {
                    lastDtPick = lastPick.PickDate;
                }

                List<StockPeak> lstPeaks = (from a in dbContext.StockPeaks
                                            where a.Symbol == strSymbol
                                            && a.TimeFrame == 1
                                            && a.PeakDate >= lastDtPick.AddMonths(-6)
                                            && a.PeakType >0
                                            orderby a.PeakDate
                                            select a).ToList();

                List<StockQuote> lstQuote = (from a in dbContext.StockQuotes
                                             where a.Symbol == strSymbol
                                             && a.TimeFrame == 1
                                             && a.QuoteDate >= lastDtPick
                                             orderby a.QuoteDate
                                             select a).ToList();
                string orgPickKey = "";
                foreach (StockQuote sq in lstQuote)
                {
                    List<StockPeak> lstRcntPk = (from p in lstPeaks
                                                 where p.PeakDate <= sq.QuoteDate
                                                 && p.PeakDate >= sq.QuoteDate.AddMonths(-6)
                                                 && p.PeakType >0
                                                 orderby p.PeakDate descending
                                                 select p).ToList();

                    StockQuote prevQuote = (from q in lstQuote
                                            where q.QuoteDate < sq.QuoteDate
                                            orderby q.QuoteDate descending
                                            select q).FirstOrDefault();

                    if (prevQuote != null)
                    {
                        decimal maxPeak = 0;
                        foreach (StockPeak sPeak in lstRcntPk)
                        {
                            StockQuote pQuote = (from q in lstQuote
                                            where q.TimeFrame == sPeak.TimeFrame
                                            && q.QuoteDate == sPeak.PeakDate
                                            select q).FirstOrDefault();

                            if (pQuote != null)
                            {
                                if (pQuote.HighValue > maxPeak)
                                    maxPeak = pQuote.HighValue;
                            }
                        }

                        if (prevQuote.CloseValue < maxPeak && sq.CloseValue > maxPeak)
                        {
                                string pickKey = string.Format("{0}_{1:yyyy-MM-dd}_2", sq.Symbol,sq.QuoteDate );

                                if (pickKey != orgPickKey)
                                {
                                    int existFlag = (from p in dbContext.StockPicks
                                                     where p.PickKey == pickKey
                                                     select p.PickKey).Count();

                                    if (existFlag <= 0)
                                    {
                                        StockPick sp = new StockPick();
                                        sp.PickDate = sq.QuoteDate;
                                        sp.PickType = 2;
                                        sp.Symbol = sq.Symbol;
                                        sp.PickKey = pickKey;
                                        dbContext.StockPicks.InsertOnSubmit(sp);

                                        dbContext.SubmitChanges();
                                    }

                                    //Console.WriteLine(string.Format("Pick {0:yyyy-MM-dd}", sq.QuoteDate));
                                    orgPickKey = pickKey;
                                }
                        }
                    }

                }
            }
        }
        // If your activity returns a value, derive from CodeActivity<TResult>
        // and return the value from the Execute method.
        protected override void Execute(CodeActivityContext context)
        {
            string strSymbol = this.MySymbol.Get(context);
            string strConn = this.MyConnString.Get(context);

            using (StockDBDataContext dbContext = new StockDBDataContext(strConn))
            {
                StockPick lastPick = (from a in dbContext.StockPicks
                                      where a.Symbol == strSymbol
                                      && a.PickType == 3
                                      orderby a.PickDate descending
                                      select a).FirstOrDefault();

                DateTime lastDtPick = DateTime.Now.AddYears(-20);

                if (lastPick != null)
                {
                    lastDtPick = lastPick.PickDate;
                }

                List<StockQuote> lstQuote = (from a in dbContext.StockQuotes
                                             where a.Symbol == strSymbol
                                             && a.TimeFrame == 2
                                             && a.QuoteDate >= lastDtPick
                                             orderby a.QuoteDate
                                             select a).ToList();

                int fallWeeks = 0;
                StockQuote prevQuote = null;
                int pastWeeks = 0;
                bool startChecking = false;

                foreach (StockQuote quote in lstQuote)
                {
                    if (quote.CloseValue < quote.OpenValue)
                        fallWeeks++;
                    else
                        fallWeeks = 0;

                    if (fallWeeks >= 4)
                    {
                        pastWeeks = 0;
                        startChecking = true;
                    }

                    if (pastWeeks <= 8 && startChecking)
                    {
                        pastWeeks++;
                    }
                    else
                    {
                        startChecking = false;
                    }

                    if (startChecking)
                    {
                        if (quote.CloseValue > quote.OpenValue)
                        {
                            string pickKey = string.Format("{0}_{1:yyyy-MM-dd}", quote.Symbol,
                                quote.QuoteDate);

                            if (quote.CloseValue > prevQuote.HighValue)
                            {
                                StockPick sp = new StockPick();
                                sp.PickDate = quote.QuoteDate;
                                sp.PickType = 3;
                                sp.Symbol = quote.Symbol;
                                sp.PickKey = pickKey;
                                dbContext.StockPicks.InsertOnSubmit(sp);

                                dbContext.SubmitChanges();

                                startChecking = false;
                            }
                        }
                        else
                            prevQuote = quote;
                    }
                }
            }
        }
        // If your activity returns a value, derive from CodeActivity<TResult>
        // and return the value from the Execute method.
        protected override void Execute(CodeActivityContext context)
        {
            string strSymbol = this.MySymbol.Get(context);
            string strConn = this.MyConnString.Get(context);

            using (StockDBDataContext dbContext = new StockDBDataContext(strConn))
            {
                StockPick lastPick = (from a in dbContext.StockPicks
                                      where a.Symbol == strSymbol
                                      && a.PickType == 1
                                      orderby a.PickDate descending
                                      select a).FirstOrDefault();

                DateTime lastDtPick = DateTime.Now.AddYears(-20);

                if (lastPick != null)
                {
                    lastDtPick = lastPick.PickDate;
                }

                List<StockPeak> lstPeaks = (from a in dbContext.StockPeaks
                                            where a.Symbol == strSymbol
                                            && a.TimeFrame == 1
                                            && a.PeakDate >= lastDtPick.AddMonths(-6)
                                            orderby a.PeakDate
                                            select a).ToList();

                List<StockQuote> lstQuote = (from a in dbContext.StockQuotes
                                             where a.Symbol == strSymbol
                                             && a.TimeFrame == 1
                                             && a.QuoteDate >= lastDtPick
                                             orderby a.QuoteDate
                                             select a).ToList();

                string orgPickKey = "";
                foreach (StockQuote sq in lstQuote)
                {
                    List<StockPeak> lstRcntPk = (from p in lstPeaks
                                                  where p.PeakDate <= sq.QuoteDate
                                                  && p.PeakDate >= sq.QuoteDate.AddDays(-45)
                                                  orderby p.PeakDate descending
                                                  select p).ToList();

                    if (lstRcntPk.Count >= 3)
                    {
                        if (lstRcntPk[0].PeakType < 0)
                        {
                            decimal p1 = (from q in lstQuote
                                          where q.QuoteDate == lstRcntPk[0].PeakDate
                                          select q.LowValue).FirstOrDefault();

                            decimal p2 = 0;
                            decimal p3 = 0;

                            for (int i = 1; i < lstRcntPk.Count; i++)
                            {
                                if (lstRcntPk[i].PeakType < 0 && p3==0)
                                {
                                    p3 = (from q in lstQuote
                                          where q.QuoteDate == lstRcntPk[i].PeakDate
                                          select q.LowValue).FirstOrDefault();
                                }

                                if (lstRcntPk[i].PeakType >0 && p2 == 0)
                                {
                                    p2 = (from q in lstQuote
                                          where q.QuoteDate == lstRcntPk[i].PeakDate
                                          select q.HighValue).FirstOrDefault();
                                }

                                if (p2 != 0 && p3 != 0)
                                    break;
                            }

                            if (p2!=0 && p3 !=0)
                            {
                                if (p1 <= p3 && sq.CloseValue > p2)
                                {
                                    string pickKey = string.Format("{0}_{1:yyyy-MM-dd}_{2:yyyy-MM-dd}", sq.Symbol,
                                        lstRcntPk[0].PeakDate, lstRcntPk[1].PeakDate);

                                    if (pickKey != orgPickKey)
                                    {
                                        int existFlag = (from p in dbContext.StockPicks
                                                         where p.PickKey == pickKey
                                                         select p.PickKey).Count();

                                        if (existFlag <= 0)
                                        {
                                            StockPick sp = new StockPick();
                                            sp.PickDate = sq.QuoteDate;
                                            sp.PickType = 1;
                                            sp.Symbol = sq.Symbol;
                                            sp.PickKey = pickKey;
                                            dbContext.StockPicks.InsertOnSubmit(sp);

                                            dbContext.SubmitChanges();
                                        }

                                        //Console.WriteLine(string.Format("Pick {0:yyyy-MM-dd}", sq.QuoteDate));
                                        orgPickKey = pickKey;
                                    }
                                }

                            }
                        }
                    }
                }

            }
        }
		private void detach_StockPicks(StockPick entity)
		{
			this.SendPropertyChanging();
			entity.StockSymbol = null;
		}
 partial void DeleteStockPick(StockPick instance);
 partial void UpdateStockPick(StockPick instance);
 partial void InsertStockPick(StockPick instance);