Пример #1
0
        public void ApplyStrategySettings()
        {
            _accountVm.Host.PortfApplyStrategySettings(this.Id,
                                                       StrategySetting.Name, StrategySetting.Serialize());

            _accountVm.PublishChanged();
        }
Пример #2
0
        public XElement Persist()
        {
            XElement elem = new XElement("portfolio");

            elem.Add(new XAttribute("id", _id));
            elem.Add(new XAttribute("quantity", _qty));
            elem.Add(new XAttribute("currentPosition", _position));
            elem.Add(new XAttribute("avgCost", _avgCost.ToString("F2")));
            elem.Add(new XAttribute("maxPosition", _maxPosition));
            elem.Add(new XAttribute("autoOpen", _autoOpen.ToString()));
            elem.Add(new XAttribute("autoStopGain", _autoStopGain.ToString()));
            elem.Add(new XAttribute("autoStopLoss", _autoStopLoss.ToString()));
            elem.Add(new XAttribute("autoTracking", _autoTracking.ToString()));
            elem.Add(new XAttribute("enablePrefer", _enablePrefer.ToString()));


            XElement elemLegs = new XElement("legs");

            foreach (var l in _legs)
            {
                elemLegs.Add(l.Persist());
            }
            elem.Add(elemLegs);

            XElement settingElem = new XElement("setting", new XAttribute("name", StrategySetting.Name));

            settingElem.Add(new XCData(StrategySetting.Persist()));

            elem.Add(settingElem);

            return(elem);
        }
Пример #3
0
        public entity.PortfolioItem GetEntity()
        {
            entity.PortfolioItem portfolioItem = new entity.PortfolioItem();
            portfolioItem.ID              = Id;
            portfolioItem.AutoOpen        = AutoOpen;
            portfolioItem.AutoStopGain    = AutoStopGain;
            portfolioItem.AutoStopLoss    = AutoStopLoss;
            portfolioItem.AutoTrack       = AutoTracking;
            portfolioItem.EnablePrefer    = EnablePrefer;
            portfolioItem.Quantity        = Quantity;
            portfolioItem.MaxPosition     = MaxPosition;
            portfolioItem.OpenTimes       = OpenTimes;
            portfolioItem.CloseTimes      = 0;
            portfolioItem.CurrentPosition = Position;
            portfolioItem.AvgCost         = AvgCost;

            foreach (var legVm in _legs)
            {
                entity.LegItem leg = new entity.LegItem();
                leg.Symbol      = legVm.Symbol;
                leg.Side        = legVm.Side;
                leg.Ratio       = legVm.Ratio;
                leg.IsPreferred = legVm.IsPreferred;
                portfolioItem.Legs.Add(leg);
            }

            portfolioItem.StrategyName = StrategySetting.Name;
            portfolioItem.StrategyData = StrategySetting.Serialize();

            return(portfolioItem);
        }
Пример #4
0
        public void ApplyStrategySettings(StrategySetting settings)
        {
            this.StrategySetting.CopyFrom(settings);
            _accountVm.Host.PortfApplyStrategySettings(this.Id, StrategySetting.GetEntity());

            _accountVm.PublishChanged();
        }
Пример #5
0
        private int SetOptimize(IAsset asset, int repeat)
        {
            try
            {
                set = new StrategySetting
                {
                    ShortTick = SetValue(ran.Next(asset.ShortTickPeriod / 3, asset.ShortTickPeriod), ran.Next(15, 50), ran.Next(asset.ShortTickPeriod, asset.ShortTickPeriod * 5)),
                    LongTick  = SetValue(ran.Next(asset.LongTickPeriod / 3, asset.LongTickPeriod), ran.Next(30, 150), ran.Next(asset.LongTickPeriod, asset.LongTickPeriod * 5)),
                    ShortDay  = SetValue(2, ran.Next(1, 3), ran.Next(asset.ShortDayPeriod, asset.ShortDayPeriod * 3)),
                    LongDay   = SetValue(ran.Next(asset.LongDayPeriod / 3, asset.LongDayPeriod), ran.Next(1, 10), ran.Next(asset.LongDayPeriod, asset.LongDayPeriod * 3)),
                    Reaction  = SetValue(ran.Next(asset.Reaction / 3, asset.Reaction), ran.Next(1, 5), ran.Next(asset.Reaction, asset.Reaction * 2)),
                    Hedge     = SetValue(0, 1, ran.Next(0, 5)),
                    Capital   = asset.Assets
                };
                return(set.EstimatedTime());
            }
            catch (Exception ex)
            {
                new LogMessage().Record("Exception", ex.ToString());

                if (TimerBox.Show("Run the Program Again and Set it Manually.", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2, 3157).Equals(DialogResult.OK))
                {
                    Application.Restart();
                }
            }
            finally
            {
                if (repeat % 5000 == 0 && TimerBox.Show("Run the Program Again and Set it Manually.", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2, 3157).Equals(DialogResult.OK))
                {
                    Application.Restart();
                }
            }
            return(0);
        }
Пример #6
0
 private void OnShortOpen()
 {
     if (StrategySetting is ManualStrategySetting)
     {
         ((ManualStrategySetting)StrategySetting).Direction = PTEntity.PosiDirectionType.SHORT;
         _accountVm.Host.PortfApplyStrategySettings(this.Id, StrategySetting.GetEntity());
         OnOpenPosition();
         NotifyManualOpenDirectionChange();
     }
 }
Пример #7
0
        private int SetOptimize(IAsset asset, AutomaticallySetting auto, int repeat)
        {
            try
            {
                set = new StrategySetting
                {
                    ShortTick = auto.SetVariableAutomatic(IAsset.Variable.ShortTick, asset.ShortTickPeriod, ran.Next(1, 21)),
                    LongTick  = auto.SetVariableAutomatic(IAsset.Variable.LongTick, asset.LongTickPeriod, ran.Next(1, 16)),
                    ShortDay  = auto.SetVariableAutomatic(IAsset.Variable.ShortDay, asset.ShortDayPeriod, ran.Next(1, 11)),
                    LongDay   = auto.SetVariableAutomatic(IAsset.Variable.LongDay, asset.LongDayPeriod, ran.Next(1, 11)),
                    Reaction  = auto.SetVariableAutomatic(IAsset.Variable.Reaction, asset.Reaction, ran.Next(1, 51)),
                    Hedge     = auto.SetVariableAutomatic(IAsset.Variable.Hedge, asset.Hedge, ran.Next(0, 6)),
                    Base      = auto.SetVariableAutomatic(IAsset.Variable.Base, asset.Base, ran.Next(0, 3)),
                    Sigma     = auto.SetVariableAutomatic(IAsset.Variable.Sigma, asset.Sigma, ran.Next(0, 2)),
                    Percent   = auto.SetVariableAutomatic(IAsset.Variable.Percent, asset.Percent, ran.Next(0, 2)),
                    Max       = auto.SetVariableAutomatic(IAsset.Variable.Max, asset.Max, ran.Next(0, 2)),
                    Quantity  = auto.SetVariableAutomatic(IAsset.Variable.Quantity, asset.Quantity, ran.Next(0, 2)),
                    Time      = auto.SetVariableAutomatic(IAsset.Variable.Time, asset.Time, ran.Next(0, 6)),
                    Capital   = asset.Assets
                };
                Estimate = set.EstimatedTime(new List <string>(64), CalculateTheRemainingTime() * count, true);

                if (repeat % 2500 == 0 && TimerBox.Show("Run the Program Again and Set it Manually.", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2, 5157).Equals(DialogResult.OK))
                {
                    Application.ExitThread();
                    Application.Exit();
                }
                else if (repeat % 500 == 0 && TimerBox.Show("Do You Want to Use the Existing Settings?", "Question", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, 7519).Equals(DialogResult.OK))
                {
                    checkBox.Checked = true;
                    button.ForeColor = Color.Gold;
                    button.PerformClick();

                    return(int.MinValue);
                }
            }
            catch (Exception ex)
            {
                new LogMessage().Record("Exception", ex.ToString());

                if (TimerBox.Show("Run the Program Again and Set it Manually.", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2, 5157).Equals(DialogResult.OK))
                {
                    Application.ExitThread();
                    Application.Exit();
                }
            }
            return(Estimate.Count);
        }
Пример #8
0
        private void CheckBoxCheckedChanged(object sender, EventArgs e)
        {
            if (!button.ForeColor.Equals(Color.Maroon))
            {
                if (button.ForeColor.Equals(Color.Ivory) && CheckCurrent)
                {
                    set = new StrategySetting
                    {
                        ShortTick = SetValue((int)numericPST.Value, (int)numericIST.Value, (int)numericDST.Value),
                        ShortDay  = SetValue((int)numericPSD.Value, (int)numericISD.Value, (int)numericDSD.Value),
                        LongTick  = SetValue((int)numericPLT.Value, (int)numericILT.Value, (int)numericDLT.Value),
                        LongDay   = SetValue((int)numericPLD.Value, (int)numericILD.Value, (int)numericDLD.Value),
                        Reaction  = SetValue((int)numericPR.Value, (int)numericIR.Value, (int)numericDR.Value),
                        Hedge     = SetValue((int)numericPH.Value, (int)numericIH.Value, (int)numericDH.Value),
                        Capital   = (long)numericCapital.Value
                    };
                    button.Text        = string.Concat("Estimated Back Testing Time is ", pro.Rate(set.EstimatedTime(), count).ToString("N0"), " Minutes.");
                    checkBox.Text      = "Reset";
                    checkBox.ForeColor = Color.Yellow;
                    button.ForeColor   = Color.Gold;

                    return;
                }
                button.Text        = string.Concat("Click to Recommend ", DateTime.Now.DayOfWeek.Equals(DayOfWeek.Friday) ? 2880 + 870 : 870, " Minutes and Save Existing Data.");
                button.ForeColor   = Color.Ivory;
                checkBox.ForeColor = Color.Ivory;
                checkBox.Text      = "Process";
            }
            else if (CheckCurrent == false)
            {
                checkBox.Text = string.Concat(pro.ProgressBarValue.ToString("N0"), " / ", Max.ToString("N0"));
                checkBox.Font = new Font(checkBox.Font.Name, 10.25F, FontStyle.Regular);

                if (TimerBox.Show(string.Concat("Currently\n", GC.GetTotalMemory(false).ToString("N0"), "bytes of Memory\nare in Use.\n\nDo You want to Clean Up the Accumulated Memory?"), "Notice", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, 1325).Equals(DialogResult.OK))
                {
                    GC.Collect();
                }
            }
            else if (CheckCurrent)
            {
                checkBox.Text = string.Concat("Parallel ", Process.GetCurrentProcess().Threads.Count - Count > 0 ? (Process.GetCurrentProcess().Threads.Count - Count).ToString("N0") : "End");
                checkBox.Font = new Font(checkBox.Font.Name, 15.75F, FontStyle.Regular);
            }
        }
Пример #9
0
        public PTEntity.PortfolioItem GetEntity()
        {
            PTEntity.PortfolioItem portfolioItem = new PTEntity.PortfolioItem();
            portfolioItem.ID              = Id;
            portfolioItem.Quantity        = Quantity;
            portfolioItem.MaxOpenPerStart = MaxOpenPerStart;
            portfolioItem.MaxCancel       = MaxCancel;
            portfolioItem.TotalOpenLimit  = TotalOpenLimit;
            portfolioItem.HedgeFlag       = HedgeFlag;

            foreach (var legVm in _legs)
            {
                PTEntity.LegItem leg = new PTEntity.LegItem();
                leg.Symbol         = legVm.Symbol;
                leg.Side           = (PTEntity.PosiDirectionType)legVm.Side;
                leg.Ratio          = legVm.Ratio;
                leg.IsPreferred    = legVm.IsPreferred;
                leg.MinPriceChange = legVm.MinPriceChange;
                portfolioItem.Legs.Add(leg);
            }

            if (!string.IsNullOrEmpty(_endTimePointsExpr))
            {
                string[] tps = _endTimePointsExpr.Split(',');
                foreach (string s in tps)
                {
                    if (!string.IsNullOrWhiteSpace(s))
                    {
                        portfolioItem.EndTimePoints.Add(s.Trim());
                    }
                }
            }

            portfolioItem.Strategy = StrategySetting.GetEntity();

            return(portfolioItem);
        }
        public void To(PortfolioVM portf)
        {
            portf.Quantity    = this.Quantity;
            portf.MaxPosition = this.MaxQuantity;

            if (Leg1)
            {
                LegVM leg;
                if (portf.LegCount < 1)
                {
                    leg = new LegVM();
                    portf.AddLeg(leg);
                }
                else
                {
                    leg = portf.GetLeg(0);
                }
                leg.Symbol = Symbol1;
                leg.Side   = Side1;
                leg.Ratio  = Ratio1;
                leg.SetIsPreferred(PreferLeg1);
            }

            if (Leg2 || StrategyName == StrategySetting.ChangePositionStrategyName)
            {
                LegVM leg;
                if (portf.LegCount < 2)
                {
                    leg = new LegVM();
                    portf.AddLeg(leg);
                }
                else
                {
                    leg = portf.GetLeg(1);
                }
                leg.Symbol      = Symbol2;
                leg.Side        = Side2;
                leg.Ratio       = Ratio2;
                leg.IsPreferred = PreferLeg2;
            }

            portf.StrategySetting = StrategySetting.Create(StrategyName);
            if (StrategyName == StrategySetting.ChangePositionStrategyName)
            {
                portf.AutoOpen     = true;
                portf.AutoStopGain = false;
                portf.AutoStopLoss = false;
                portf.EnablePrefer = true;
                portf.AutoTracking = true;
                ChangePositionSetting setting = (ChangePositionSetting)portf.StrategySetting;
                setting.CloseLeg     = Symbol1;
                setting.CloseLegSide = Side1;
            }
            else if (StrategyName == StrategySetting.ScalperStrategyName)
            {
                if (!string.IsNullOrEmpty(Symbol1))
                {
                    ScalperSetting setting = (ScalperSetting)portf.StrategySetting;
                    setting.PriceTick = this.PriceTick;
                    setting.Threshold = this.Threshold;
                }
            }
        }
Пример #11
0
        public static PortfolioVM Load(AccountVM acct, XElement xmlElement)
        {
            PortfolioVM portf = new PortfolioVM(acct);

            portf.IsLoading = true;
            XAttribute attr = xmlElement.Attribute("id");

            if (attr != null)
            {
                portf.Id = attr.Value;
            }

            attr = xmlElement.Attribute("quantity");
            if (attr != null)
            {
                portf.Quantity = int.Parse(attr.Value);
            }

            attr = xmlElement.Attribute("currentPosition");
            if (attr != null)
            {
                int currPos = int.Parse(attr.Value);
                portf.Position  = currPos;
                portf.OpenTimes = currPos;
            }

            attr = xmlElement.Attribute("avgCost");
            if (attr != null)
            {
                double avgCost = double.Parse(attr.Value);
                portf.AvgCost = avgCost;
            }

            attr = xmlElement.Attribute("maxPosition");
            if (attr != null)
            {
                portf.MaxPosition = int.Parse(attr.Value);
            }

            attr = xmlElement.Attribute("autoOpen");
            if (attr != null)
            {
                portf.AutoOpen = attr.Value == bool.TrueString;
            }

            attr = xmlElement.Attribute("autoStopGain");
            if (attr != null)
            {
                portf.AutoStopGain = attr.Value == bool.TrueString;
            }

            attr = xmlElement.Attribute("autoStopLoss");
            if (attr != null)
            {
                portf.AutoStopLoss = attr.Value == bool.TrueString;
            }

            attr = xmlElement.Attribute("autoTracking");
            if (attr != null)
            {
                portf.AutoTracking = attr.Value == bool.TrueString;
            }

            attr = xmlElement.Attribute("enablePrefer");
            if (attr != null)
            {
                portf.EnablePrefer = attr.Value == bool.TrueString;
            }

            foreach (var legElem in xmlElement.Element("legs").Elements("leg"))
            {
                LegVM legVm = LegVM.Load(legElem);
                portf.AddLeg(legVm);
            }

            XElement xmlSetting      = xmlElement.Element("setting");
            string   strategyName    = xmlSetting.Attribute("name").Value;
            string   strategyXmlText = xmlSetting.Value;

            portf.StrategySetting = StrategySetting.Load(strategyName, strategyXmlText);
            portf.IsLoading       = false;
            return(portf);
        }
        public void To(PortfolioVM portf)
        {
            portf.Quantity          = this.Quantity;
            portf.TotalOpenLimit    = this.TotalOpenLimit;
            portf.MaxCancel         = this.MaxCancelVol;
            portf.MaxOpenPerStart   = this.MaxOpenPerStart;
            portf.EndTimePointsExpr = this.EndTimePointsExpr;

            if (Leg1)
            {
                LegVM leg;
                if (portf.LegCount < 1)
                {
                    leg = new LegVM();
                    portf.AddLeg(leg);
                }
                else
                {
                    leg = portf.GetLeg(0);
                }
                leg.Symbol = Symbol1;
                leg.Side   = Side1;
                leg.Ratio  = Ratio1;
                leg.SetIsPreferred(PreferLeg1);
            }

            if (Leg2 || StrategyName == StrategySetting.ChangePositionStrategyName)
            {
                LegVM leg;
                if (portf.LegCount < 2)
                {
                    leg = new LegVM();
                    portf.AddLeg(leg);
                }
                else
                {
                    leg = portf.GetLeg(1);
                }
                leg.Symbol      = Symbol2;
                leg.Side        = Side2;
                leg.Ratio       = Ratio2;
                leg.IsPreferred = PreferLeg2;
            }

            portf.StrategySetting = StrategySetting.Create(StrategyName);
            if (StrategyName == StrategySetting.ArbitrageStrategyName)
            {
                ArbitrageStrategySetting setting = (ArbitrageStrategySetting)portf.StrategySetting;
                setting.BollPeriod       = AR_BollPeriod;
                setting.StdDevMultiplier = AR_StdDevMultiplier;
                setting.TimeFrame        = 60;
                setting.FirstLegSymbol   = Symbol1;
                setting.SecondLegSymbol  = Symbol2;

                foreach (var l in portf.Legs)
                {
                    l.MinPriceChange = PriceTick;
                }
            }
            else if (StrategyName == StrategySetting.ChangePositionStrategyName)
            {
                portf.AutoOpen     = true;
                portf.AutoStopGain = false;
                portf.AutoStopLoss = false;
                portf.EnablePrefer = true;
                portf.AutoTracking = true;
                ChangePositionSetting setting = (ChangePositionSetting)portf.StrategySetting;
                setting.CloseLeg     = Symbol1;
                setting.CloseLegSide = Side1;
            }
            else if (StrategyName == StrategySetting.ScalperStrategyName)
            {
                if (!string.IsNullOrEmpty(Symbol1))
                {
                    ScalperSetting setting = (ScalperSetting)portf.StrategySetting;
                    setting.PriceTick = this.PriceTick;
                    setting.Threshold = this.Threshold;
                }
            }
            else if (StrategyName == StrategySetting.MACDHistSlopeStrategyName)
            {
                if (!string.IsNullOrEmpty(Symbol1))
                {
                    MACDHistSlopeStrategySetting setting = (MACDHistSlopeStrategySetting)portf.StrategySetting;
                    setting.Symbol             = Symbol1;
                    setting.MACD_Short         = 12;
                    setting.MACD_Long          = 26;
                    setting.MACD_M             = 9;
                    setting.FastPeriod         = this.FastPeriod;
                    setting.SlowPeriod         = this.SlowPeriod;
                    setting.FastStdDiff        = this.FastStdDiff;
                    setting.SlowStdDiff        = this.SlowStdDiff;
                    setting.FastAngleThreshold = 45;
                    setting.SlowAngleThreshold = 30;
                    setting.TrailingStopValue  = this.TrailingStop;
                }
            }
            else if (StrategyName == StrategySetting.WMATrendStrategyName)
            {
                if (!string.IsNullOrEmpty(Symbol1))
                {
                    WMATrendStrategySettings setting = (WMATrendStrategySettings)portf.StrategySetting;
                    setting.Symbol    = Symbol1;
                    setting.Period    = WT_Period;
                    setting.WMA_Param = WMA_Param;
                    setting.MA_N      = MA_N;
                    //setting.TrailingStopValue = this.TrailingStop;
                }
            }
            else if (StrategyName == StrategySetting.LinerRegressionStrategyName)
            {
                if (!string.IsNullOrEmpty(Symbol1))
                {
                    LinerRegStrategySettings setting = (LinerRegStrategySettings)portf.StrategySetting;
                    setting.Symbol         = Symbol1;
                    setting.Period         = LR_Period;
                    setting.Number         = LR_Number;
                    setting.OpenThreshold  = LR_OpenThreshold;
                    setting.CloseThreshold = LR_CloseThreshold;
                }
            }
            else if (StrategyName == StrategySetting.ASCTrendStrategyName)
            {
                if (!string.IsNullOrEmpty(Symbol1))
                {
                    ASCTrendStrategySettings setting = (ASCTrendStrategySettings)portf.StrategySetting;
                    setting.Symbol         = Symbol1;
                    setting.Period         = AS_Period;
                    setting.Risk           = AS_Risk;
                    setting.AvergatePeriod = AS_AveragePeriod;
                    setting.BreakoutLength = AS_BreakoutLength;
                }
            }
            else if (StrategyName == StrategySetting.RangeTrendStrategyName)
            {
                if (!string.IsNullOrEmpty(Symbol1))
                {
                    RangeTrendStrategySettings setting = (RangeTrendStrategySettings)portf.StrategySetting;
                    setting.Symbol         = Symbol1;
                    setting.Period         = RT_TimeFrame;
                    setting.OpenPeriod     = RT_OpenPeriod;
                    setting.ClosePeriod    = RT_ClosePeriod;
                    setting.StopLossFactor = RT_StopLossFactor;
                    setting.TrendFactor    = RT_TrendFactor;
                }
            }
            else if (StrategyName == StrategySetting.ManualStrategyName)
            {
                if (!string.IsNullOrEmpty(Symbol1))
                {
                    ManualStrategySetting setting = (ManualStrategySetting)portf.StrategySetting;
                    setting.RetryTimes = RetryTimes;
                }
            }
        }