Exemplo n.º 1
0
        private void MeasureSimulationStep()
        {
            DateTime start = DateTime.Now;

            ProcessSimulationStep();
            DateTime end  = DateTime.Now;
            TimeSpan span = new TimeSpan((end.Subtract(start).Ticks));

            m_StepTimeSpan.Add(span.TotalMilliseconds);

            if (OnStepProcessed != null)
            {
                OnStepProcessed(this);
            }
        }
Exemplo n.º 2
0
        private void CalculateDaily(string code)
        {
            if (string.IsNullOrEmpty(code))
            {
                return;
            }
            var scripts     = Config.Instance.INFO.ScriptSetting.Scripts;
            var macdsetting = Config.Instance.INFO.MACDSetting;
            var rsisetting  = Config.Instance.INFO.RSISetting;

            // Delete MACD, RSI values
            if ((CheckTable <DBStkMACDEntity>() && scripts.ContainsKey("DEL_MACD_BY_CD")) &&
                (CheckTable <DBStkRSIEntity>() && scripts.ContainsKey("DEL_RSI_BY_CD")))
            {
                var lprm_macd = accessor.CreateParameter("CODE", code);
                var lcmd_macd = accessor.CreateCommand(scripts["DEL_MACD_BY_CD"], new List <DbParameter>()
                {
                    lprm_macd
                });
                var lprm_rsi = accessor.CreateParameter("CODE", code);
                var lcmd_rsi = accessor.CreateCommand(scripts["DEL_RSI_BY_CD"], new List <DbParameter>()
                {
                    lprm_rsi
                });
                if (accessor.ExecuteSQLCommand(new List <DbCommand>()
                {
                    lcmd_macd, lcmd_rsi
                }) < 0)
                {
                    logger.Write(TYPE.ERROR, string.Format("delete MACD and RSI data failed.({0})", code));
                    logger.Write(TYPE.ERROR, accessor.LastError);
                    return;
                }
            }
            else
            {
                return;
            }

            // Retrieve Stocks
            var page = new EntityPage <DBTStkDailyEntity>(
                new Clause("Code = {Code}").AddParam("Code", code),
                new Sort().Add("ID", Sort.Orientation.asc), 100, accessor);
            List <DBTStkDailyEntity> lst_daily = null; DateTime ldt_date = DateTime.Today;
            MACD macd = null; RSI rsi = null; AverageValue daily30 = null;

            #region Cache For MACD
            SegCache <LinearList <KeyValuePair <DateTime, decimal> >, KeyValuePair <DateTime, decimal> > cache1 = null;
            SegCache <LinearList <KeyValuePair <DateTime, decimal> >, KeyValuePair <DateTime, decimal> > cache2 = null;
            LinearList <KeyValuePair <DateTime, decimal> > list1 = null;
            LinearList <KeyValuePair <DateTime, decimal> > list2 = null;
            #endregion
            #region Cache For AVG
            SegCache <LinearList <KeyValuePair <DateTime, decimal> >, KeyValuePair <DateTime, decimal> > cache3 = null;
            SegCache <LinearList <KeyValuePair <DateTime, decimal> >, KeyValuePair <DateTime, decimal> > cache4 = null;
            LinearList <KeyValuePair <DateTime, decimal> > list3 = null;
            LinearList <KeyValuePair <DateTime, decimal> > list4 = null;
            #endregion
            Func <KeyValuePair <DateTime, decimal>, decimal> fValue = (pair) => { return(pair.Value); };
            Func <KeyValuePair <DateTime, decimal>, KeyValuePair <DateTime, decimal>, LinearList <KeyValuePair <DateTime, decimal> >, bool> fSwitch1 = (v1, v2, segment) => {
                switch (segment.Orietion)
                {
                case 0:
                    return(false);

                case 1:
                    if (v2.Value >= v1.Value)
                    {
                        return(false);
                    }
                    break;

                case -1:
                    if (v2.Value <= v1.Value)
                    {
                        return(false);
                    }
                    break;
                }
                return(true);
            };
            Func <LinearList <KeyValuePair <DateTime, decimal> >, LinearList <KeyValuePair <DateTime, decimal> >, bool> fSwitch2 = (values, segment) =>
            {
                if (segment.Orietion == 0 || segment.Orietion == values.Orietion)
                {
                    return(false);
                }
                return(values.Count >= 10);
            };
            int pageno = 0; var count = 0;
            while ((lst_daily = page.Retrieve(++pageno)).Count > 0)
            {
                for (int i = 0; i < lst_daily.Count; i++, count++)
                {
                    try
                    {
                        var daily = lst_daily[i];
                        ldt_date = daily.Date;
                        if (pageno == 1 && i == 0)
                        {
                            macd    = new MACD(daily.Close);
                            rsi     = new RSI(rsisetting.N1, rsisetting.N2, rsisetting.N3, daily.Close);
                            daily30 = new AverageValue(30, 4);
                            daily30.Add(daily.Close);
                            cache1 = new SegCache <LinearList <KeyValuePair <DateTime, decimal> >, KeyValuePair <DateTime, decimal> >(
                                new LinearList <KeyValuePair <DateTime, decimal> >[] { new LinearList <KeyValuePair <DateTime, decimal> >(fValue), new LinearList <KeyValuePair <DateTime, decimal> >(fValue) },
                                fSwitch1, null);
                            cache2 = new SegCache <LinearList <KeyValuePair <DateTime, decimal> >, KeyValuePair <DateTime, decimal> >(
                                new LinearList <KeyValuePair <DateTime, decimal> >[] { new LinearList <KeyValuePair <DateTime, decimal> >(fValue), new LinearList <KeyValuePair <DateTime, decimal> >(fValue) },
                                null, fSwitch2);

                            cache3 = new SegCache <LinearList <KeyValuePair <DateTime, decimal> >, KeyValuePair <DateTime, decimal> >(
                                new LinearList <KeyValuePair <DateTime, decimal> >[] { new LinearList <KeyValuePair <DateTime, decimal> >(fValue), new LinearList <KeyValuePair <DateTime, decimal> >(fValue) },
                                fSwitch1, null);
                            cache4 = new SegCache <LinearList <KeyValuePair <DateTime, decimal> >, KeyValuePair <DateTime, decimal> >(
                                new LinearList <KeyValuePair <DateTime, decimal> >[] { new LinearList <KeyValuePair <DateTime, decimal> >(fValue), new LinearList <KeyValuePair <DateTime, decimal> >(fValue) },
                                null, fSwitch2);
                        }
                        else
                        {
                            macd.Add(daily.Close);
                            rsi.Add(daily.Close);
                            daily30.Add(daily.Close);
                            if (cache1.Add(new KeyValuePair <DateTime, decimal>(daily.Date, macd.DEA), out list1))
                            {
                                cache2.Add(list1, out list2);
                            }
                            if (cache3.Add(new KeyValuePair <DateTime, decimal>(daily.Date, daily30.Average), out list3))
                            {
                                cache4.Add(list3, out list4);
                            }
                        }

                        var lent_macd = new DBStkMACDEntity(daily.ID);
                        lent_macd.EMA12 = macd.EMA12;
                        lent_macd.EMA26 = macd.EMA26;
                        lent_macd.DIFF  = macd.DIFF;
                        lent_macd.DEA   = macd.DEA;
                        lent_macd.BAR   = macd.BAR;

                        var lent_rsi = new DBStkRSIEntity(daily.ID);
                        lent_rsi.RSI1       = rsi.RSI1;
                        lent_rsi.RSI2       = rsi.RSI2;
                        lent_rsi.RSI3       = rsi.RSI3;
                        lent_rsi.RSI1MaxEma = rsi.RSI1MaxEma;
                        lent_rsi.RSI1ABSEma = rsi.RSI1ABSEma;
                        lent_rsi.RSI2MaxEma = rsi.RSI2MaxEma;
                        lent_rsi.RSI2ABSEma = rsi.RSI2ABSEma;
                        lent_rsi.RSI3MaxEma = rsi.RSI3MaxEma;
                        lent_rsi.RSI3ABSEma = rsi.RSI3ABSEma;

                        accessor.SaveEntity(lent_macd, lent_rsi);
                        accessor.Commit();
                    }
                    catch { }
                }
            }
            if (count > 120)
            {
                cache2.Add(cache1.Segment, out list2);
                cache4.Add(cache3.Segment, out list4);

                var lent_sum = new DBStkSummaryResultEntity();
                lent_sum.Code          = code;
                lent_sum.Time          = DateTime.Now;
                lent_sum.PAVG30_ORIENT = cache4.Segment.Orietion;
                lent_sum.PAVG30_DAYS   = cache4.Segment.Count;
                lent_sum.PAVG30_VALUE  = cache4.Segment.Slope;
                lent_sum.PAVG30_Date1  = cache4.Segment[0].Key;
                lent_sum.PAVG30_Date2  = cache4.Segment[cache4.Segment.Count - 1].Key;

                lent_sum.DEA_ORIENT = cache2.Segment.Orietion;
                lent_sum.DEA_DAYS   = cache2.Segment.Count;
                lent_sum.DEA_VALUE  = cache2.Segment.Slope;
                lent_sum.DEA_Date1  = cache2.Segment[0].Key;
                lent_sum.DEA_Date2  = cache2.Segment[cache2.Segment.Count - 1].Key;

                lent_sum.RSI_Date   = ldt_date;
                lent_sum.RSI_VALUE1 = rsi.RSI1;
                lent_sum.RSI_VALUE2 = rsi.RSI2;
                lent_sum.RSI_VALUE3 = rsi.RSI3;

                if (CheckTable <DBStkSummaryResultEntity>())
                {
                    accessor.SetDBAccessor2(lent_sum);
                    lent_sum.GenerateID();
                    lent_sum.Save();
                }
            }
            if (page != null)
            {
                page.Dispose();
            }
            if (cache1 != null)
            {
                cache1.Dispose();
            }
            if (cache2 != null)
            {
                cache2.Dispose();
            }
            if (cache3 != null)
            {
                cache3.Dispose();
            }
            if (cache4 != null)
            {
                cache4.Dispose();
            }
            if (daily30 != null)
            {
                daily30.Dispose();
            }
        }