/// <summary>
 /// Constructor with all data
 /// </summary>
 /// <param name="d"></param>
 public GenericStrategy(AnalysisData d)
 {
     this.data = d;
     this.adviceInfo = new TradePoints();
     this.last_position = 0;
     this.trailing_stop = -1;
 }
Beispiel #2
0
        override public TradePoints Execute(application.Data data, int[] parameters)
        {
            //application.Data vnidxData1 =  data.New("^VNINDEX");
            
            adviceInfo = new TradePoints();
            DataSeries minusDmi_14 = new Indicators.MinusDI(data.Bars, parameters[0], "");
            DataSeries plusDmi_14 = new Indicators.PlusDI(data.Bars, parameters[1], "");

            AppTypes.MarketTrend lastTrend = AppTypes.MarketTrend.Unspecified;
            AppTypes.MarketTrend currentTrend = AppTypes.MarketTrend.Unspecified;

            for (int idx = 0; idx < minusDmi_14.Count; idx++)
            {

                currentTrend = ((plusDmi_14[idx] > minusDmi_14[idx]) ? AppTypes.MarketTrend.Upward : AppTypes.MarketTrend.Downward);
                if (lastTrend == AppTypes.MarketTrend.Downward && currentTrend == AppTypes.MarketTrend.Upward)
                    BuyAtClose(idx);
                if (lastTrend == AppTypes.MarketTrend.Upward && currentTrend == AppTypes.MarketTrend.Downward)
                    SellAtClose(idx);
                lastTrend = currentTrend;
            }
            return adviceInfo;

        }
        //public static int CreateTradeAlert()
        //{
        //    CreateTradeAlert(null, null, null);
        //}
        public static int CreateTradeAlert(onProcessStart onStartFunc, onProcessItem onProcessItemFunc, onProcessEnd onEndFunc)
        {
            DateTime frDate = common.Consts.constNullDate;
            DateTime toDate = DateTime.Now;

            //Run all strategy analysis for all stocks.
            databases.tmpDS.stockCodeDataTable stockCodeTbl = new databases.tmpDS.stockCodeDataTable();
            databases.DbAccess.LoadData(stockCodeTbl, AppTypes.CommonStatus.Enable);

            application.AnalysisData data = new application.AnalysisData();
            data.DataTimeRange = AppTypes.TimeRanges.None;
            data.DataMaxCount  = Settings.sysGlobal.AlertDataCount;

            TradeAlertItem[] tradeAlertList = new TradeAlertItem[0];
            StringCollection strategyList   = new StringCollection();

            for (int idx = 0; idx < StrategyData.MetaList.Values.Length; idx++)
            {
                application.Strategy.StrategyMeta meta = (application.Strategy.StrategyMeta)StrategyData.MetaList.Values[idx];
                if (meta.Type != AppTypes.StrategyTypes.Strategy)
                {
                    continue;
                }
                strategyList.Add(((application.Strategy.StrategyMeta)StrategyData.MetaList.Values[idx]).Code);
            }

            if (onStartFunc != null)
            {
                onStartFunc(stockCodeTbl.Count);
            }

            DateTime alertDate;
            DateTime alertFrDate = toDate.Date;
            DateTime alertToDate = toDate;

            for (int stockCodeIdx = 0; stockCodeIdx < stockCodeTbl.Count; stockCodeIdx++)
            {
                if (onProcessItemFunc != null)
                {
                    if (!onProcessItemFunc(stockCodeTbl[stockCodeIdx].code))
                    {
                        break;
                    }
                }

                //foreach (AppTypes.TimeScale timeScale in AppTypes.myTimeScales) //???
                AppTypes.TimeScale timeScale = AppTypes.TimeScaleFromType(AppTypes.TimeScaleTypes.Day);
                {
                    data.DataStockCode = stockCodeTbl[stockCodeIdx].code;
                    data.DataTimeScale = timeScale;
                    data.LoadData();
                    for (int strategyIdx = 0; strategyIdx < strategyList.Count; strategyIdx++)
                    {
                        StrategyData.ClearCache();
                        TradePoints advices = application.Strategy.StrategyLibs.AnalysisStrategy(data, strategyList[strategyIdx].Trim());

                        if ((advices == null) || (advices.Count == 0))
                        {
                            continue;
                        }

                        //Only check the last advices for alert
                        TradePointInfo tradeInfo = (TradePointInfo)advices[advices.Count - 1];
                        alertDate = DateTime.FromOADate(data.DateTime[tradeInfo.DataIdx]);

                        //Ignore alerts that out of date range.
                        if (alertDate < alertFrDate || alertDate > alertToDate)
                        {
                            continue;
                        }
                        Array.Resize(ref tradeAlertList, tradeAlertList.Length + 1);

                        tradeAlertList[tradeAlertList.Length - 1] = new TradeAlertItem(stockCodeTbl[stockCodeIdx].code.Trim(), strategyList[strategyIdx].Trim(),
                                                                                       timeScale, alertDate,
                                                                                       data.Close[tradeInfo.DataIdx],
                                                                                       data.Volume[tradeInfo.DataIdx], tradeInfo);
                    }
                }
            }
            //Create alerts in the day
            int noAlertCreated = CreateTradeAlert(tradeAlertList);

            //Save last lun date
            //SaveLastRunTime(toDate);
            if (onEndFunc != null)
            {
                onEndFunc();
            }
            stockCodeTbl.Dispose();
            return(noAlertCreated);
        }
        /// <summary>
        /// Draw tradepoints/recommendations for buy/sell decision
        /// </summary>

        protected void PlotStrategyTradepoints(application.TradePoints tradePoints, baseClass.controls.graphPanel toPanel)
        {
            ClearStrategyTradepoints(toPanel);
            Charts.DrawCurve[] curveList = myCurveList.CurveInPane(toPanel.Name);
            if (curveList.Length == 0)
            {
                return;
            }
            CurveItem      curveItem = curveList[0].Curve;
            TradePointInfo tradePointInfo;

            for (int idx = 0; idx < tradePoints.Count; idx++)
            {
                tradePointInfo = (TradePointInfo)tradePoints[idx];
                if (!tradePointInfo.isValid)
                {
                    continue;
                }

                TextObj obj = new TextObj();
                obj.FontSpec.Size             = Settings.sysTradePointMarkerFontSize;
                obj.FontSpec.IsBold           = true;
                obj.FontSpec.Border.IsVisible = true;
                obj.FontSpec.Fill.IsVisible   = true;
                obj.FontSpec.Fill.Color       = Settings.sysTradePointMarkerColorBG;
                switch (toPanel.myGraphObj.myViewportState.myAxisType)
                {
                case Charts.AxisType.DateAsOrdinal:
                    obj.Location.X = tradePointInfo.DataIdx + 1;
                    break;

                default:
                    obj.Location.X = curveItem.Points[tradePointInfo.DataIdx].X;
                    break;
                }
                obj.Location.Y = curveItem.Points[tradePointInfo.DataIdx].Y;
                obj.Location.CoordinateFrame = CoordType.AxisXYScale;

                obj.Location.AlignH = AlignH.Center;
                switch (tradePointInfo.TradeAction)
                {
                case AppTypes.TradeActions.Buy:
                case AppTypes.TradeActions.Accumulate:
                    obj.Text = Settings.sysTradePointMarkeBUY;
                    obj.FontSpec.FontColor = Settings.sysTradePointMarkerColorBUY;
                    break;

                case AppTypes.TradeActions.Sell:
                case AppTypes.TradeActions.ClearAll:
                    obj.Text = Settings.sysTradePointMarkerSELL;
                    obj.FontSpec.FontColor = Settings.sysTradePointMarkerColorSELL;
                    break;

                default:
                    obj.Text = Settings.sysTradePointMarkerOTHER;
                    obj.FontSpec.FontColor = Settings.sysTradePointMarkerColorOTHER;
                    break;
                }
                toPanel.myGraphObj.myGraphPane.GraphObjList.Add(obj);
            }
            toPanel.myGraphObj.UpdateChart();
        }
 /// <summary>
 /// Constructor with all data and parameter
 /// </summary>
 /// <param name="d"></param>
 /// <param name="p"></param>
 public GenericStrategy(AnalysisData d, Parameters p)
 {
     this.data = d;
     this.parameters = p;
     if (adviceInfo == null)
         this.adviceInfo = new TradePoints();
     this.last_position = 0;
     this.trailing_stop = -1;
 }
 /// <summary>
 /// constructor
 /// </summary>
 public GenericStrategy()
 {
     if (adviceInfo==null)
         adviceInfo = new TradePoints(); 
 }
 //virtual public TradePoints Execute(AnalysisData data, double[] paras)
 virtual public TradePoints Execute(AnalysisData data, double[] paras)
 {
     this.data = data;
     parameters = new Parameters(paras);
     //??Bug fixed by Dung 11 Nov 2011
     if (adviceInfo == null)            
         adviceInfo = new TradePoints();
     StrategyExecute();
     return adviceInfo;
 }