/// <summary>
        /// Initializes the strategy field
        /// </summary>
        public Strategy_Layout(Strategy strategy)
        {
            this.strategy      = strategy;
            slots              = strategy.Slots;
            slotMinMidMax      = SlotSizeMinMidMax.mid;
            toolTip            = new ToolTip();
            flowLayoutStrategy = new FlowLayoutPanel();
            VScrollBarStrategy = new VScrollBar();
            apnlSlot           = new Panel[slots];
            pnlProperties      = new Panel();

            for (int slot = 0; slot < slots; slot++)
                apnlSlot[slot] = new Panel();

            if (showRemoveSlotButtons)
            {
                abtnRemoveSlot = new Button[slots];
                for (int iSlot = 0; iSlot < slots; iSlot++)
                    abtnRemoveSlot[iSlot] = new Button();
            }

            // FlowLayoutStrategy
            flowLayoutStrategy.Parent     = this;
            flowLayoutStrategy.AutoScroll = false;

            //VScrollBarStrategy
            VScrollBarStrategy.Parent  = this;
            VScrollBarStrategy.TabStop = true;
            VScrollBarStrategy.Scroll += new ScrollEventHandler(VScrollBarStrategy_Scroll);

            if (showAddSlotButtons)
            {
                // btnAddOpenFilter
                btnAddOpenFilter = new Button();
                btnAddOpenFilter.Tag       = strategy.OpenSlot;
                btnAddOpenFilter.Text      = Language.T("Add an Opening Logic Condition");
                btnAddOpenFilter.Margin    = new Padding(30, 0, 0, space);
                btnAddOpenFilter.UseVisualStyleBackColor = true;
                toolTip.SetToolTip(btnAddOpenFilter, Language.T("Add a new entry logic slot to the strategy."));

                // btnAddCloseFilter
                btnAddCloseFilter = new Button();
                btnAddCloseFilter.Tag       = strategy.CloseSlot;
                btnAddCloseFilter.Text      = Language.T("Add a Closing Logic Condition");
                btnAddCloseFilter.Margin    = new Padding(30, 0, 0, space);
                btnAddCloseFilter.UseVisualStyleBackColor = true;
                toolTip.SetToolTip(btnAddCloseFilter, Language.T("Add a new exit logic slot to the strategy."));

                // btnClosingFilterHelp
                btnClosingFilterHelp = new Button();
                btnClosingFilterHelp.Image   = Properties.Resources.info;
                btnClosingFilterHelp.Margin  = new Padding(2, 2, 0, space);
                btnClosingFilterHelp.TabStop = false;
                btnClosingFilterHelp.Click  += new EventHandler(BtnClosingFilterHelp_Click);
                btnClosingFilterHelp.UseVisualStyleBackColor = true;
            }

            return;
        }
        /// <summary>
        /// Initializes the strategy field
        /// </summary>
        public StrategyLayout(Strategy strategy)
        {
            SlotPropertiesTipText = Language.T("Averaging, Trading size, Protection.");
            SlotToolTipText = Language.T("Long position logic.");
            ShowRemoveSlotButtons = true;
            ShowAddSlotButtons = true;
            _strategy = strategy;
            _slots = strategy.Slots;
            SlotMinMidMax = SlotSizeMinMidMax.mid;
            FlowLayoutStrategy = new FlowLayoutPanel();
            VScrollBarStrategy = new VScrollBar();
            SlotPanelsList = new ContextPanel[_slots];
            PanelProperties = new Panel();

            for (int slot = 0; slot < _slots; slot++)
                SlotPanelsList[slot] = new ContextPanel();

            // FlowLayoutStrategy
            FlowLayoutStrategy.Parent = this;
            FlowLayoutStrategy.AutoScroll = false;

            //VScrollBarStrategy
            VScrollBarStrategy.Parent = this;
            VScrollBarStrategy.TabStop = true;
            VScrollBarStrategy.Scroll += VScrollBarStrategyScroll;

            if (ShowAddSlotButtons)
            {
                var toolTip = new ToolTip();
                ButtonAddOpenFilter = new Button
                                          {
                                              Tag = strategy.OpenSlot,
                                              Text = Language.T("Add an Opening Logic Condition"),
                                              Margin = new Padding(30, 0, 0, Space),
                                              UseVisualStyleBackColor = true
                                          };
                toolTip.SetToolTip(ButtonAddOpenFilter, Language.T("Add a new entry logic slot to the strategy."));

                ButtonAddCloseFilter = new Button
                                           {
                                               Tag = strategy.CloseSlot,
                                               Text = Language.T("Add a Closing Logic Condition"),
                                               Margin = new Padding(30, 0, 0, Space),
                                               UseVisualStyleBackColor = true
                                           };
                toolTip.SetToolTip(ButtonAddCloseFilter, Language.T("Add a new exit logic slot to the strategy."));

                BtnClosingFilterHelp = new Button
                                            {
                                                Image = Resources.info,
                                                Margin = new Padding(2, 2, 0, Space),
                                                TabStop = false
                                            };
                BtnClosingFilterHelp.Click += BtnClosingFilterHelpClick;
                BtnClosingFilterHelp.UseVisualStyleBackColor = true;
            }
        }
 /// <summary>
 /// Repaints the strategy slots
 /// </summary>
 /// <param name="strategy">The strategy</param>
 public void RepaintStrategyControls(Strategy strategy)
 {
     this.strategy = strategy;
     slots = strategy.Slots;
     foreach (Panel pnl in apnlSlot)
         pnl.Invalidate();
     pnlProperties.Invalidate();
 }
 /// <summary>
 /// Rebuilds all the controls in panel Strategy
 /// </summary>
 public void RebuildStrategyControls(Strategy strategy)
 {
     this.strategy = strategy;
     slots = strategy.Slots;
     flowLayoutStrategy.SuspendLayout();
     flowLayoutStrategy.Controls.Clear();
     InitializeStrategySlots();
     ArrangeStrategyControls();
     if (showAddSlotButtons) SetAddSlotButtons();
     flowLayoutStrategy.ResumeLayout();
 }
Exemplo n.º 5
0
        /// <summary>
        /// Returns a copy of the current strategy.
        /// </summary>
        public Strategy Clone()
        {
            // Creates a new strategy.
            var tempStrategy = new Strategy(OpenFilters, CloseFilters)
                                   {
                                       FirstBar = FirstBar,
                                       OpenFilters = OpenFilters,
                                       CloseFilters = CloseFilters,
                                       StrategyName = StrategyName,
                                       Description = Description,
                                       Symbol = Symbol,
                                       DataPeriod = DataPeriod,
                                       SameSignalAction = SameSignalAction,
                                       OppSignalAction = OppSignalAction,
                                       UseAccountPercentEntry = UseAccountPercentEntry,
                                       MaxOpenLots = MaxOpenLots,
                                       EntryLots = EntryLots,
                                       AddingLots = AddingLots,
                                       ReducingLots = ReducingLots,
                                       UseMartingale = UseMartingale,
                                       MartingaleMultiplier = MartingaleMultiplier,
                                       UsePermanentSL = UsePermanentSL,
                                       PermanentSLType = PermanentSLType,
                                       PermanentSL = PermanentSL,
                                       UsePermanentTP = UsePermanentTP,
                                       PermanentTPType = PermanentTPType,
                                       PermanentTP = PermanentTP,
                                       UseBreakEven = UseBreakEven,
                                       BreakEven = BreakEven
                                   };

            // Reading the slots
            for (int slot = 0; slot < Slots; slot++)
                tempStrategy.Slot[slot] = Slot[slot].Clone();

            return tempStrategy;
        }
        /// <summary>
        /// Pareses a strategy from a xml document.
        /// </summary>
        public Strategy ParseXmlStrategy(XmlDocument xmlDocStrategy)
        {
            // Read the number of slots
            int openFilters = int.Parse(xmlDocStrategy.GetElementsByTagName("openFilters")[0].InnerText);
            int closeFilters = int.Parse(xmlDocStrategy.GetElementsByTagName("closeFilters")[0].InnerText);

            // Create the strategy.
            var tempStrategy = new Strategy(openFilters, closeFilters);

            // Same and Opposite direction Actions
            tempStrategy.SameSignalAction =
                (SameDirSignalAction)
                Enum.Parse(typeof (SameDirSignalAction),
                           xmlDocStrategy.GetElementsByTagName("sameDirSignalAction")[0].InnerText);
            tempStrategy.OppSignalAction =
                (OppositeDirSignalAction)
                Enum.Parse(typeof (OppositeDirSignalAction),
                           xmlDocStrategy.GetElementsByTagName("oppDirSignalAction")[0].InnerText);

            // Market
            tempStrategy.Symbol = xmlDocStrategy.GetElementsByTagName("instrumentSymbol")[0].InnerText;
            tempStrategy.DataPeriod =
                (DataPeriods)
                Enum.Parse(typeof (DataPeriods), xmlDocStrategy.GetElementsByTagName("instrumentPeriod")[0].InnerText);

            // Permanent Stop Loss
            tempStrategy.PermanentSL =
                Math.Abs(int.Parse(xmlDocStrategy.GetElementsByTagName("permanentStopLoss")[0].InnerText));
                // Math.Abs() removes the negative sign from previous versions.
            XmlAttributeCollection xmlAttributeCollection =
                xmlDocStrategy.GetElementsByTagName("permanentStopLoss")[0].Attributes;
            if (xmlAttributeCollection != null)
            {
                tempStrategy.UsePermanentSL = bool.Parse(xmlAttributeCollection["usePermanentSL"].InnerText);
                try
                {
                    tempStrategy.PermanentSLType =
                        (PermanentProtectionType)
                        Enum.Parse(typeof (PermanentProtectionType), xmlAttributeCollection["permanentSLType"].InnerText);
                }
                catch
                {
                    tempStrategy.PermanentSLType = PermanentProtectionType.Relative;
                }
            }

            // Permanent Take Profit
            tempStrategy.PermanentTP = int.Parse(xmlDocStrategy.GetElementsByTagName("permanentTakeProfit")[0].InnerText);
            XmlAttributeCollection attributeCollection =
                xmlDocStrategy.GetElementsByTagName("permanentTakeProfit")[0].Attributes;
            if (attributeCollection != null)
            {
                tempStrategy.UsePermanentTP = bool.Parse(attributeCollection["usePermanentTP"].InnerText);
                try
                {
                    tempStrategy.PermanentTPType =
                        (PermanentProtectionType)
                        Enum.Parse(typeof (PermanentProtectionType), attributeCollection["permanentTPType"].InnerText);
                }
                catch
                {
                    tempStrategy.PermanentTPType = PermanentProtectionType.Relative;
                }
            }

            // Break Even
            try
            {
                tempStrategy.BreakEven = int.Parse(xmlDocStrategy.GetElementsByTagName("breakEven")[0].InnerText);
                XmlAttributeCollection attributes = xmlDocStrategy.GetElementsByTagName("breakEven")[0].Attributes;
                if (attributes != null)
                    tempStrategy.UseBreakEven = bool.Parse(attributes["useBreakEven"].InnerText);
            }
            catch (Exception exception)
            {
                Console.Write(exception.Message);
            }

            // Money Management
            try
            {
                tempStrategy.UseAccountPercentEntry =
                    bool.Parse(xmlDocStrategy.GetElementsByTagName("useAccountPercentEntry")[0].InnerText);
            }
            catch
            {
                tempStrategy.UseAccountPercentEntry =
                    bool.Parse(xmlDocStrategy.GetElementsByTagName("useAcountPercentEntry")[0].InnerText);
            }
            tempStrategy.MaxOpenLots = StringToDouble(xmlDocStrategy.GetElementsByTagName("maxOpenLots")[0].InnerText);
            tempStrategy.EntryLots = StringToDouble(xmlDocStrategy.GetElementsByTagName("entryLots")[0].InnerText);
            tempStrategy.AddingLots = StringToDouble(xmlDocStrategy.GetElementsByTagName("addingLots")[0].InnerText);
            tempStrategy.ReducingLots = StringToDouble(xmlDocStrategy.GetElementsByTagName("reducingLots")[0].InnerText);
            try
            {
                tempStrategy.UseMartingale =
                    bool.Parse(xmlDocStrategy.GetElementsByTagName("useMartingale")[0].InnerText);
                tempStrategy.MartingaleMultiplier =
                    StringToDouble(xmlDocStrategy.GetElementsByTagName("martingaleMultiplier")[0].InnerText);
            }
            catch
            {
                tempStrategy.UseMartingale = false;
                tempStrategy.MartingaleMultiplier = 2.0;
            }

            // Description
            tempStrategy.Description = xmlDocStrategy.GetElementsByTagName("description")[0].InnerText;

            // Strategy name.
            tempStrategy.StrategyName = xmlDocStrategy.GetElementsByTagName("strategyName")[0].InnerText;

            // Reading the slots
            XmlNodeList xmlSlotList = xmlDocStrategy.GetElementsByTagName("slot");
            for (int slot = 0; slot < xmlSlotList.Count; slot++)
            {
                XmlNodeList xmlSlotTagList = xmlSlotList[slot].ChildNodes;

                XmlAttributeCollection collection = xmlSlotList[slot].Attributes;
                if (collection != null)
                {
                    var slotType = (SlotTypes) Enum.Parse(typeof (SlotTypes), collection["slotType"].InnerText);

                    // Logical group
                    if (slotType == SlotTypes.OpenFilter || slotType == SlotTypes.CloseFilter)
                    {
                        XmlAttributeCollection attributes = collection;
                        XmlNode nodeGroup = attributes.GetNamedItem("logicalGroup");
                        string defGroup = GetDefaultGroup(slotType, slot, tempStrategy.CloseSlot);
                        if (nodeGroup != null)
                        {
                            string group = nodeGroup.InnerText;
                            tempStrategy.Slot[slot].LogicalGroup = @group;
                            if (@group != defGroup && @group.ToLower() != "all" && !Configs.UseLogicalGroups)
                            {
                                MessageBox.Show(
                                    Language.T("The strategy requires logical groups.") + Environment.NewLine +
                                    Language.T("\"Use Logical Groups\" option was temporarily switched on."),
                                    Language.T("Logical Groups"),
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Information);
                                Configs.UseLogicalGroups = true;
                            }
                        }
                        else
                            tempStrategy.Slot[slot].LogicalGroup = defGroup;
                    }

                    // Indicator name.
                    string indicatorName = xmlSlotTagList[0].InnerText;
                    Indicator indicator = IndicatorStore.ConstructIndicator(indicatorName, slotType);

                    for (int tag = 1; tag < xmlSlotTagList.Count; tag++)
                    {
                        // List parameters
                        if (xmlSlotTagList[tag].Name == "listParam")
                        {
                            XmlAttributeCollection attributes = xmlSlotTagList[tag].Attributes;
                            if (attributes != null)
                            {
                                int listParam = int.Parse(attributes["paramNumber"].InnerText);
                                XmlNode xmlListParamNode = xmlSlotTagList[tag].FirstChild;

                                indicator.IndParam.ListParam[listParam].Caption = xmlListParamNode.InnerText;

                                xmlListParamNode = xmlListParamNode.NextSibling;
                                if (xmlListParamNode != null)
                                {
                                    int index = int.Parse(xmlListParamNode.InnerText);
                                    indicator.IndParam.ListParam[listParam].Index = index;
                                    indicator.IndParam.ListParam[listParam].Text =
                                        indicator.IndParam.ListParam[listParam].ItemList[index];
                                }
                            }
                        }

                        // Numeric parameters
                        if (xmlSlotTagList[tag].Name == "numParam")
                        {
                            XmlNode xmlNumParamNode = xmlSlotTagList[tag].FirstChild;
                            XmlAttributeCollection attributes = xmlSlotTagList[tag].Attributes;
                            if (attributes != null)
                            {
                                int numParam = int.Parse(attributes["paramNumber"].InnerText);
                                indicator.IndParam.NumParam[numParam].Caption = xmlNumParamNode.InnerText;

                                xmlNumParamNode = xmlNumParamNode.NextSibling;
                                if (xmlNumParamNode != null)
                                {
                                    string numParamValue = xmlNumParamNode.InnerText;
                                    numParamValue = numParamValue.Replace(',', Data.PointChar);
                                    numParamValue = numParamValue.Replace('.', Data.PointChar);
                                    float value = float.Parse(numParamValue);

                                    // Removing of the Stop Loss negative sign used in previous versions.
                                    string parCaption = indicator.IndParam.NumParam[numParam].Caption;
                                    if (parCaption == "Trailing Stop" ||
                                        parCaption == "Initial Stop Loss" ||
                                        parCaption == "Stop Loss")
                                        value = Math.Abs(value);
                                    indicator.IndParam.NumParam[numParam].Value = value;
                                }
                            }
                        }

                        // Check parameters
                        if (xmlSlotTagList[tag].Name == "checkParam")
                        {
                            XmlNode xmlCheckParamNode = xmlSlotTagList[tag].FirstChild;
                            XmlAttributeCollection attributes = xmlSlotTagList[tag].Attributes;
                            if (attributes != null)
                            {
                                int checkParam = int.Parse(attributes["paramNumber"].InnerText);
                                indicator.IndParam.CheckParam[checkParam].Caption = xmlCheckParamNode.InnerText;

                                xmlCheckParamNode = xmlCheckParamNode.NextSibling;
                                if (xmlCheckParamNode != null)
                                    indicator.IndParam.CheckParam[checkParam].Checked =
                                        bool.Parse(xmlCheckParamNode.InnerText);
                            }
                        }
                    }

                    // Calculate the indicator.
                    indicator.Calculate(slotType);
                    tempStrategy.Slot[slot].IndicatorName = indicator.IndicatorName;
                    tempStrategy.Slot[slot].IndParam = indicator.IndParam;
                    tempStrategy.Slot[slot].Component = indicator.Component;
                    tempStrategy.Slot[slot].SeparatedChart = indicator.SeparatedChart;
                    tempStrategy.Slot[slot].SpecValue = indicator.SpecialValues;
                    tempStrategy.Slot[slot].MinValue = indicator.SeparatedChartMinValue;
                    tempStrategy.Slot[slot].MaxValue = indicator.SeparatedChartMaxValue;
                }
                tempStrategy.Slot[slot].IsDefined = true;
            }

            return tempStrategy;
        }
        /// <summary>
        /// Represents the Strategy as a XmlDocument.
        /// </summary>
        public static XmlDocument CreateStrategyXmlDoc(Strategy strategy)
        {
            // Create the XmlDocument.
            var xmlDocStrategy = new XmlDocument();
            xmlDocStrategy.LoadXml("<strategy></strategy>");

            //Create the XML declaration.
            XmlDeclaration xmldecl = xmlDocStrategy.CreateXmlDeclaration("1.0", null, null);

            //Add new node to the document.
            XmlElement root = xmlDocStrategy.DocumentElement;
            xmlDocStrategy.InsertBefore(xmldecl, root);

            // Add the program name.
            XmlElement newElem = xmlDocStrategy.CreateElement("programName");
            newElem.InnerText = Data.ProgramName;
            if (xmlDocStrategy.DocumentElement != null) xmlDocStrategy.DocumentElement.AppendChild(newElem);

            // Add the program version.
            newElem = xmlDocStrategy.CreateElement("programVersion");
            newElem.InnerText = Data.ProgramVersion;
            if (xmlDocStrategy.DocumentElement != null) xmlDocStrategy.DocumentElement.AppendChild(newElem);

            // Add the strategy name.
            newElem = xmlDocStrategy.CreateElement("strategyName");
            newElem.InnerText = strategy.StrategyName;
            if (xmlDocStrategy.DocumentElement != null) xmlDocStrategy.DocumentElement.AppendChild(newElem);

            // Add the Symbol.
            newElem = xmlDocStrategy.CreateElement("instrumentSymbol");
            newElem.InnerText = strategy.Symbol;
            if (xmlDocStrategy.DocumentElement != null) xmlDocStrategy.DocumentElement.AppendChild(newElem);

            // Add the Period.
            newElem = xmlDocStrategy.CreateElement("instrumentPeriod");
            newElem.InnerText = strategy.DataPeriod.ToString();
            if (xmlDocStrategy.DocumentElement != null) xmlDocStrategy.DocumentElement.AppendChild(newElem);

            // Add the same direction signal action.
            newElem = xmlDocStrategy.CreateElement("sameDirSignalAction");
            newElem.InnerText = strategy.SameSignalAction.ToString();
            if (xmlDocStrategy.DocumentElement != null) xmlDocStrategy.DocumentElement.AppendChild(newElem);

            // Add the opposite direction signal action.
            newElem = xmlDocStrategy.CreateElement("oppDirSignalAction");
            newElem.InnerText = strategy.OppSignalAction.ToString();
            if (xmlDocStrategy.DocumentElement != null) xmlDocStrategy.DocumentElement.AppendChild(newElem);

            // Add the Permanent Stop Loss
            newElem = xmlDocStrategy.CreateElement("permanentStopLoss");
            newElem.InnerText = strategy.PermanentSL.ToString(CultureInfo.InvariantCulture);
            newElem.SetAttribute("usePermanentSL", strategy.UsePermanentSL.ToString(CultureInfo.InvariantCulture));
            newElem.SetAttribute("permanentSLType", strategy.PermanentSLType.ToString());
            if (xmlDocStrategy.DocumentElement != null) xmlDocStrategy.DocumentElement.AppendChild(newElem);

            // Add the Permanent Take Profit
            newElem = xmlDocStrategy.CreateElement("permanentTakeProfit");
            newElem.InnerText = strategy.PermanentTP.ToString(CultureInfo.InvariantCulture);
            newElem.SetAttribute("usePermanentTP", strategy.UsePermanentTP.ToString(CultureInfo.InvariantCulture));
            newElem.SetAttribute("permanentTPType", strategy.PermanentTPType.ToString());
            if (xmlDocStrategy.DocumentElement != null) xmlDocStrategy.DocumentElement.AppendChild(newElem);

            // Add the Break Even
            newElem = xmlDocStrategy.CreateElement("breakEven");
            newElem.InnerText = strategy.BreakEven.ToString(CultureInfo.InvariantCulture);
            newElem.SetAttribute("useBreakEven", strategy.UseBreakEven.ToString(CultureInfo.InvariantCulture));
            if (xmlDocStrategy.DocumentElement != null) xmlDocStrategy.DocumentElement.AppendChild(newElem);

            // Add the max open lots
            newElem = xmlDocStrategy.CreateElement("maxOpenLots");
            newElem.InnerText = strategy.MaxOpenLots.ToString(CultureInfo.InvariantCulture);
            if (xmlDocStrategy.DocumentElement != null) xmlDocStrategy.DocumentElement.AppendChild(newElem);

            // Add Use Account Percent Entry
            newElem = xmlDocStrategy.CreateElement("useAccountPercentEntry");
            newElem.InnerText = strategy.UseAccountPercentEntry.ToString(CultureInfo.InvariantCulture);
            if (xmlDocStrategy.DocumentElement != null) xmlDocStrategy.DocumentElement.AppendChild(newElem);

            // Add the Entry lots
            newElem = xmlDocStrategy.CreateElement("entryLots");
            newElem.InnerText = strategy.EntryLots.ToString(CultureInfo.InvariantCulture);
            if (xmlDocStrategy.DocumentElement != null) xmlDocStrategy.DocumentElement.AppendChild(newElem);

            // Add the Adding lots
            newElem = xmlDocStrategy.CreateElement("addingLots");
            newElem.InnerText = strategy.AddingLots.ToString(CultureInfo.InvariantCulture);
            if (xmlDocStrategy.DocumentElement != null) xmlDocStrategy.DocumentElement.AppendChild(newElem);

            // Add the Reducing lots
            newElem = xmlDocStrategy.CreateElement("reducingLots");
            newElem.InnerText = strategy.ReducingLots.ToString(CultureInfo.InvariantCulture);
            if (xmlDocStrategy.DocumentElement != null) xmlDocStrategy.DocumentElement.AppendChild(newElem);

            // Add Use Martingale MM
            newElem = xmlDocStrategy.CreateElement("useMartingale");
            newElem.InnerText = strategy.UseMartingale.ToString(CultureInfo.InvariantCulture);
            if (xmlDocStrategy.DocumentElement != null) xmlDocStrategy.DocumentElement.AppendChild(newElem);

            // Add the Martingale Multiplier
            newElem = xmlDocStrategy.CreateElement("martingaleMultiplier");
            newElem.InnerText = strategy.MartingaleMultiplier.ToString(CultureInfo.InvariantCulture);
            if (xmlDocStrategy.DocumentElement != null) xmlDocStrategy.DocumentElement.AppendChild(newElem);

            // Description
            newElem = xmlDocStrategy.CreateElement("description");
            newElem.InnerText = strategy.Description;
            if (xmlDocStrategy.DocumentElement != null) xmlDocStrategy.DocumentElement.AppendChild(newElem);

            // Add the number of open filters.
            newElem = xmlDocStrategy.CreateElement("openFilters");
            newElem.InnerText = strategy.OpenFilters.ToString(CultureInfo.InvariantCulture);
            if (xmlDocStrategy.DocumentElement != null) xmlDocStrategy.DocumentElement.AppendChild(newElem);

            // Add the number of close filters.
            newElem = xmlDocStrategy.CreateElement("closeFilters");
            newElem.InnerText = strategy.CloseFilters.ToString(CultureInfo.InvariantCulture);
            if (xmlDocStrategy.DocumentElement != null) xmlDocStrategy.DocumentElement.AppendChild(newElem);

            // Add the slots.
            for (int slot = 0; slot < strategy.Slots; slot++)
            {
                SlotTypes slType = strategy.Slot[slot].SlotType;

                // Add a slot element.
                XmlElement newSlot = xmlDocStrategy.CreateElement("slot");
                newSlot.SetAttribute("slotNumber", slot.ToString(CultureInfo.InvariantCulture));
                newSlot.SetAttribute("slotType", slType.ToString());

                if (slType == SlotTypes.OpenFilter || slType == SlotTypes.CloseFilter)
                    newSlot.SetAttribute("logicalGroup", strategy.Slot[slot].LogicalGroup);

                // Add an element.
                newElem = xmlDocStrategy.CreateElement("indicatorName");
                newElem.InnerText = strategy.Slot[slot].IndicatorName;
                newSlot.AppendChild(newElem);

                // Add the list parameters.
                for (int param = 0; param < strategy.Slot[slot].IndParam.ListParam.Length; param++)
                {
                    if (strategy.Slot[slot].IndParam.ListParam[param].Enabled)
                    {
                        // Add an element.
                        XmlElement newListElem = xmlDocStrategy.CreateElement("listParam");
                        newListElem.SetAttribute("paramNumber", param.ToString(CultureInfo.InvariantCulture));

                        // Add an element.
                        newElem = xmlDocStrategy.CreateElement("caption");
                        newElem.InnerText = strategy.Slot[slot].IndParam.ListParam[param].Caption;
                        newListElem.AppendChild(newElem);

                        // Add an element.
                        newElem = xmlDocStrategy.CreateElement("index");
                        newElem.InnerText =
                            strategy.Slot[slot].IndParam.ListParam[param].Index.ToString(CultureInfo.InvariantCulture);
                        newListElem.AppendChild(newElem);

                        // Add an element.
                        newElem = xmlDocStrategy.CreateElement("value");
                        newElem.InnerText = strategy.Slot[slot].IndParam.ListParam[param].Text;
                        newListElem.AppendChild(newElem);

                        newSlot.AppendChild(newListElem);
                    }
                }

                // Add the num parameters.
                for (int param = 0; param < strategy.Slot[slot].IndParam.NumParam.Length; param++)
                {
                    if (strategy.Slot[slot].IndParam.NumParam[param].Enabled)
                    {
                        // Add an element.
                        XmlElement newNumElem = xmlDocStrategy.CreateElement("numParam");
                        newNumElem.SetAttribute("paramNumber", param.ToString(CultureInfo.InvariantCulture));

                        // Add an element.
                        newElem = xmlDocStrategy.CreateElement("caption");
                        newElem.InnerText = strategy.Slot[slot].IndParam.NumParam[param].Caption;
                        newNumElem.AppendChild(newElem);

                        // Add an element.
                        newElem = xmlDocStrategy.CreateElement("value");
                        newElem.InnerText = strategy.Slot[slot].IndParam.NumParam[param].ValueToString;
                        newNumElem.AppendChild(newElem);

                        newSlot.AppendChild(newNumElem);
                    }
                }

                // Add the check parameters.
                for (int param = 0; param < strategy.Slot[slot].IndParam.CheckParam.Length; param++)
                {
                    if (strategy.Slot[slot].IndParam.CheckParam[param].Enabled)
                    {
                        // Add an element.
                        XmlElement newCheckElem = xmlDocStrategy.CreateElement("checkParam");
                        newCheckElem.SetAttribute("paramNumber", param.ToString(CultureInfo.InvariantCulture));

                        // Add an element.
                        newElem = xmlDocStrategy.CreateElement("caption");
                        newElem.InnerText = strategy.Slot[slot].IndParam.CheckParam[param].Caption;
                        newCheckElem.AppendChild(newElem);

                        // Add an element.
                        newElem = xmlDocStrategy.CreateElement("value");
                        newElem.InnerText =
                            strategy.Slot[slot].IndParam.CheckParam[param].Checked.ToString(CultureInfo.InvariantCulture);
                        newCheckElem.AppendChild(newElem);

                        newSlot.AppendChild(newCheckElem);
                    }
                }

                if (xmlDocStrategy.DocumentElement != null) xmlDocStrategy.DocumentElement.AppendChild(newSlot);
            }

            return xmlDocStrategy;
        }
 /// <summary>
 /// Repaints the strategy slots
 /// </summary>
 /// <param name="strategy">The strategy</param>
 public void RepaintStrategyControls(Strategy strategy)
 {
     _strategy = strategy;
     _slots = strategy.Slots;
     FlowLayoutStrategy.SuspendLayout();
     foreach (ContextPanel pnl in SlotPanelsList)
         pnl.Invalidate();
     PanelProperties.Invalidate();
     FlowLayoutStrategy.ResumeLayout();
 }
Exemplo n.º 9
0
 /// <summary>
 ///     Rebuilds all the controls in panel Strategy
 /// </summary>
 public void RebuildStrategyControls(Strategy newStrategy)
 {
     strategy = newStrategy;
     slots = newStrategy.Slots;
     pnlStrategy.SuspendLayout();
     pnlStrategy.Controls.Clear();
     InitializeStrategySlots();
     ArrangeStrategyControls();
     if (ShowAddSlotButtons) SetAddSlotButtons();
     pnlStrategy.ResumeLayout();
 }
Exemplo n.º 10
0
        /// <summary>
        /// Returns a copy of the current strategy.
        /// </summary>
        public Strategy Clone()
        {
            // Creates a new strategy.
            Strategy tempStrategy = new Strategy(openFilters, closeFilters);

            // Number of slots
            tempStrategy.openFilters  = openFilters;
            tempStrategy.closeFilters = closeFilters;

            // Strategy name
            tempStrategy.strategyName = strategyName;

            // Description
            tempStrategy.description = description;

            // Market
            tempStrategy.symbol = symbol;
            tempStrategy.period = period;

            // Same and Opposite direction Actions
            tempStrategy.sameDirSignal = sameDirSignal;
            tempStrategy.oppDirSignal  = oppDirSignal;

            // Money Management
            tempStrategy.useAccountPercentEntry = useAccountPercentEntry;
            tempStrategy.maxOpenLots  = maxOpenLots;
            tempStrategy.entryLots    = entryLots;
            tempStrategy.addingLots   = addingLots;
            tempStrategy.reducingLots = reducingLots;

            // Permanet Stop Loss
            tempStrategy.usePermanentSL  = usePermanentSL;
            tempStrategy.permanentSLType = permanentSLType;
            tempStrategy.permanentSL     = permanentSL;

            // Permanet Take Profit
            tempStrategy.usePermanentTP  = usePermanentTP;
            tempStrategy.permanentTPType = permanentTPType;
            tempStrategy.permanentTP     = permanentTP;

            // Break Even
            tempStrategy.useBreakEven = useBreakEven;
            tempStrategy.breakEven    = breakEven;

            // Reading the slots
            for (int slot = 0; slot < Slots; slot++)
                tempStrategy.Slot[slot] = indicatorSlot[slot].Clone();

            return tempStrategy;
        }
Exemplo n.º 11
0
        /// <summary>
        /// Represents the Strategy as a XmlDocument.
        /// </summary>
        public static XmlDocument CreateStrategyXmlDoc(Strategy strategy)
        {
            // Create the XmlDocument.
            var xmlDocStrategy = new XmlDocument();
            xmlDocStrategy.LoadXml("<strategy></strategy>");

            //Create the XML declaration.
            XmlDeclaration xmldecl = xmlDocStrategy.CreateXmlDeclaration("1.0", null, null);

            //Add new node to the document.
            XmlElement root = xmlDocStrategy.DocumentElement;
            xmlDocStrategy.InsertBefore(xmldecl, root);

            AppendStringElement(xmlDocStrategy, "programName", Data.ProgramName);
            AppendStringElement(xmlDocStrategy, "programVersion", Data.ProgramVersion);
            AppendStringElement(xmlDocStrategy, "strategyName", strategy.StrategyName);
            AppendStringElement(xmlDocStrategy, "instrumentSymbol", strategy.Symbol);
            AppendStringElement(xmlDocStrategy, "instrumentPeriod", (int)strategy.DataPeriod);
            AppendStringElement(xmlDocStrategy, "sameDirSignalAction", strategy.SameSignalAction.ToString());
            AppendStringElement(xmlDocStrategy, "oppDirSignalAction", strategy.OppSignalAction.ToString());

            // Add the Permanent Stop Loss
            XmlElement newElem = xmlDocStrategy.CreateElement("permanentStopLoss");
            newElem.InnerText = strategy.PermanentSL.ToString(CultureInfo.InvariantCulture);
            newElem.SetAttribute("usePermanentSL", strategy.UsePermanentSL.ToString(CultureInfo.InvariantCulture));
            newElem.SetAttribute("permanentSLType", strategy.PermanentSLType.ToString());
            if (xmlDocStrategy.DocumentElement != null) xmlDocStrategy.DocumentElement.AppendChild(newElem);

            // Add the Permanent Take Profit
            newElem = xmlDocStrategy.CreateElement("permanentTakeProfit");
            newElem.InnerText = strategy.PermanentTP.ToString(CultureInfo.InvariantCulture);
            newElem.SetAttribute("usePermanentTP", strategy.UsePermanentTP.ToString(CultureInfo.InvariantCulture));
            newElem.SetAttribute("permanentTPType", strategy.PermanentTPType.ToString());
            if (xmlDocStrategy.DocumentElement != null) xmlDocStrategy.DocumentElement.AppendChild(newElem);

            // Add the Break Even
            newElem = xmlDocStrategy.CreateElement("breakEven");
            newElem.InnerText = strategy.BreakEven.ToString(CultureInfo.InvariantCulture);
            newElem.SetAttribute("useBreakEven", strategy.UseBreakEven.ToString(CultureInfo.InvariantCulture));
            if (xmlDocStrategy.DocumentElement != null) xmlDocStrategy.DocumentElement.AppendChild(newElem);

            AppendStringElement(xmlDocStrategy, "maxOpenLots", strategy.MaxOpenLots);
            AppendStringElement(xmlDocStrategy, "useAccountPercentEntry", strategy.UseAccountPercentEntry);
            AppendStringElement(xmlDocStrategy, "entryLots", strategy.EntryLots);
            AppendStringElement(xmlDocStrategy, "addingLots", strategy.AddingLots);
            AppendStringElement(xmlDocStrategy, "reducingLots", strategy.ReducingLots);
            AppendStringElement(xmlDocStrategy, "useMartingale", strategy.UseMartingale);
            AppendStringElement(xmlDocStrategy, "martingaleMultiplier", strategy.MartingaleMultiplier);
            AppendStringElement(xmlDocStrategy, "description", strategy.Description);

            // Add the slots.
            AppendStringElement(xmlDocStrategy, "openFilters", strategy.OpenFilters);
            AppendStringElement(xmlDocStrategy, "closeFilters", strategy.CloseFilters);
            for (int slot = 0; slot < strategy.Slots; slot++)
            {
                IndicatorSlot stratSlot = strategy.Slot[slot];
                SlotTypes slType = stratSlot.SlotType;

                // Add a slot element.
                XmlElement newSlot = xmlDocStrategy.CreateElement("slot");
                newSlot.SetAttribute("slotNumber", slot.ToString(CultureInfo.InvariantCulture));
                newSlot.SetAttribute("slotType", slType.ToString());

                if (slType == SlotTypes.OpenFilter || slType == SlotTypes.CloseFilter)
                    newSlot.SetAttribute("logicalGroup", stratSlot.LogicalGroup);

                // Add an element.
                newElem = xmlDocStrategy.CreateElement("indicatorName");
                newElem.InnerText = stratSlot.IndicatorName;
                newSlot.AppendChild(newElem);

                // Add the list parameters.
                for (int param = 0; param < stratSlot.IndParam.ListParam.Length; param++)
                {
                    if (!stratSlot.IndParam.ListParam[param].Enabled) continue;

                    // Add an element.
                    XmlElement newListElem = xmlDocStrategy.CreateElement("listParam");
                    newListElem.SetAttribute("paramNumber", param.ToString(CultureInfo.InvariantCulture));

                    // Add an element.
                    newElem = xmlDocStrategy.CreateElement("caption");
                    newElem.InnerText = stratSlot.IndParam.ListParam[param].Caption;
                    newListElem.AppendChild(newElem);

                    // Add an element.
                    newElem = xmlDocStrategy.CreateElement("index");
                    newElem.InnerText = stratSlot.IndParam.ListParam[param].Index.ToString(CultureInfo.InvariantCulture);
                    newListElem.AppendChild(newElem);

                    // Add an element.
                    newElem = xmlDocStrategy.CreateElement("value");
                    newElem.InnerText = stratSlot.IndParam.ListParam[param].Text;
                    newListElem.AppendChild(newElem);

                    newSlot.AppendChild(newListElem);
                }

                // Add the num parameters.
                for (int param = 0; param < stratSlot.IndParam.NumParam.Length; param++)
                {
                    if (!stratSlot.IndParam.NumParam[param].Enabled) continue;

                    // Add an element.
                    XmlElement newNumElem = xmlDocStrategy.CreateElement("numParam");
                    newNumElem.SetAttribute("paramNumber", param.ToString(CultureInfo.InvariantCulture));

                    // Add an element.
                    newElem = xmlDocStrategy.CreateElement("caption");
                    newElem.InnerText = stratSlot.IndParam.NumParam[param].Caption;
                    newNumElem.AppendChild(newElem);

                    // Add an element.
                    newElem = xmlDocStrategy.CreateElement("value");
                    newElem.InnerText = stratSlot.IndParam.NumParam[param].ValueToString;
                    newNumElem.AppendChild(newElem);

                    newSlot.AppendChild(newNumElem);
                }

                // Add the check parameters.
                for (int param = 0; param < stratSlot.IndParam.CheckParam.Length; param++)
                {
                    if (!stratSlot.IndParam.CheckParam[param].Enabled) continue;

                    // Add an element.
                    XmlElement newCheckElem = xmlDocStrategy.CreateElement("checkParam");
                    newCheckElem.SetAttribute("paramNumber", param.ToString(CultureInfo.InvariantCulture));

                    // Add an element.
                    newElem = xmlDocStrategy.CreateElement("caption");
                    newElem.InnerText = stratSlot.IndParam.CheckParam[param].Caption;
                    newCheckElem.AppendChild(newElem);

                    // Add an element.
                    newElem = xmlDocStrategy.CreateElement("value");
                    newElem.InnerText = stratSlot.IndParam.CheckParam[param].Checked.ToString(CultureInfo.InvariantCulture);
                    newCheckElem.AppendChild(newElem);

                    newSlot.AppendChild(newCheckElem);
                }

                if (xmlDocStrategy.DocumentElement != null)
                    xmlDocStrategy.DocumentElement.AppendChild(newSlot);
            }

            // Add statistics meta data.
            string unit = " " + Configs.AccountCurrency;
            AppendStringElement(xmlDocStrategy, "AccountBalance", Backtester.NetMoneyBalance.ToString("F2") + unit);
            AppendStringElement(xmlDocStrategy, "ProfitPerDay", Backtester.MoneyProfitPerDay.ToString("F2") + unit);
            AppendStringElement(xmlDocStrategy, "WinLossRatio", Backtester.WinLossRatio.ToString("F2"));
            AppendStringElement(xmlDocStrategy, "AccountStatsParam", String.Join(";", Backtester.AccountStatsParam));
            AppendStringElement(xmlDocStrategy, "AccountStatsValue", String.Join(";", Backtester.AccountStatsValue));
            AppendStringElement(xmlDocStrategy, "MarketStatsParam", String.Join(";", Data.MarketStatsParam));
            AppendStringElement(xmlDocStrategy, "MarketStatsValue", String.Join(";", Data.MarketStatsValue));

            // Add chart data
            int length = Data.Bars - StatsBuffer.FirstBar;
            var balanceLine = new double[length];
            var equityLine = new double[length];
            for (int bar = 0; bar < length; bar++)
            {
                balanceLine[bar] = Backtester.MoneyBalance(bar);
                equityLine[bar] = Backtester.MoneyEquity(bar);
            }
            int size = Math.Min(600, length);
            var balanceList = MathUtils.ArrayToStringArray(MathUtils.ArrayResize(balanceLine, size));
            var equityList = MathUtils.ArrayToStringArray(MathUtils.ArrayResize(equityLine, size));
            AppendStringElement(xmlDocStrategy, "BalanceLine", String.Join(";", balanceList));
            AppendStringElement(xmlDocStrategy, "EquityLine", String.Join(";", equityList));

            return xmlDocStrategy;
        }