/// <summary> /// Calculates the strategy indicators and returns the first meaningful bar number. /// </summary> /// <returns>First bar</returns> public int Calculate() { int firstBar = 0; foreach (IndicatorSlot indSlot in Slot) { string sndicatorName = indSlot.IndicatorName; SlotTypes slotType = indSlot.SlotType; Indicator indicator = Indicator_Store.ConstructIndicator(sndicatorName, slotType); indicator.IndParam = indSlot.IndParam; indicator.Calculate(slotType); indSlot.IndicatorName = indicator.IndicatorName; indSlot.IndParam = indicator.IndParam; indSlot.Component = indicator.Component; indSlot.SeparatedChart = indicator.SeparatedChart; indSlot.SpecValue = indicator.SpecialValues; indSlot.MinValue = indicator.SeparatedChartMinValue; indSlot.MaxValue = indicator.SeparatedChartMaxValue; indSlot.IsDefined = true; foreach (IndicatorComp indComp in indSlot.Component) { if (indComp.FirstBar > firstBar) { firstBar = indComp.FirstBar; } } } return(firstBar); }
/// <summary> /// Show indicators in the selected bars. /// </summary> void ShowIndicators(string input) { string pattern = @"^ind (?<numb>\d+)$"; Regex expression = new Regex(pattern, RegexOptions.Compiled); Match match = expression.Match(input); if (match.Success) { int iBar = int.Parse(match.Groups["numb"].Value); if (iBar < 1 || iBar > Data.Bars) { return; } iBar--; System.Text.StringBuilder sb = new System.Text.StringBuilder(); for (int iSlot = 0; iSlot < Data.Strategy.Slots; iSlot++) { Indicator indicator = Indicator_Store.ConstructIndicator(Data.Strategy.Slot[iSlot].IndicatorName, Data.Strategy.Slot[iSlot].SlotType); sb.Append(Environment.NewLine + indicator.ToString() + Environment.NewLine + "Logic: " + indicator.IndParam.ListParam[0].Text + Environment.NewLine + "-----------------" + Environment.NewLine); foreach (IndicatorComp indComp in Data.Strategy.Slot[iSlot].Component) { sb.Append(indComp.CompName + " "); sb.Append(indComp.Value[iBar].ToString() + Environment.NewLine); } } tbxOutput.Text += Environment.NewLine + "Indicators for bar " + (iBar + 1).ToString() + Environment.NewLine + "-----------------" + Environment.NewLine + sb.ToString(); } }
/// <summary> /// Loads the defaults parameters for the selected indicator. /// </summary> void BtnDefault_Click(object sender, EventArgs e) { Indicator indicator = Indicator_Store.ConstructIndicator(indicatorName, slotType); UpdateFromIndicatorParam(indicator.IndParam); SetDefaultGroup(); CalculateIndicator(); return; }
/// <summary> /// Loads an Indicator /// </summary> /// <param name="treeNode">The node</param> void TrvIndicatorsLoadIndicator(TreeNode treeNode) { Indicator indicator = Indicator_Store.ConstructIndicator(trvIndicators.SelectedNode.Text, slotType); UpdateFromIndicatorParam(indicator.IndParam); SetDefaultGroup(); CalculateIndicator(); return; }
/// <summary> /// Sets "Use previous bar value" parameter. /// </summary> public bool AdjustUsePreviousBarValue() { bool isSomethingChanged = false; if (Data.AutoUsePrvBarValue == false) { return(isSomethingChanged); } for (int slot = 0; slot < Slots; slot++) { isSomethingChanged = SetUsePrevBarValueCheckBox(slot) ? true : isSomethingChanged; } // Recalculates the indicators. if (isSomethingChanged) { for (int slot = 0; slot < Slots; slot++) { string indicatorName = Data.Strategy.Slot[slot].IndicatorName; SlotTypes slotType = Data.Strategy.Slot[slot].SlotType; Indicator indicator = Indicator_Store.ConstructIndicator(indicatorName, slotType); indicator.IndParam = Data.Strategy.Slot[slot].IndParam; indicator.Calculate(slotType); Slot[slot].IndicatorName = indicator.IndicatorName; Slot[slot].IndParam = indicator.IndParam; Slot[slot].Component = indicator.Component; Slot[slot].SeparatedChart = indicator.SeparatedChart; Slot[slot].SpecValue = indicator.SpecialValues; Slot[slot].MinValue = indicator.SeparatedChartMinValue; Slot[slot].MaxValue = indicator.SeparatedChartMaxValue; Slot[slot].IsDefined = true; } } return(isSomethingChanged); }
/// <summary> /// Generates a HTML report about the closing logic. /// </summary> StringBuilder ClosingLogicHTMLReport() { StringBuilder sb = new StringBuilder(); int iClosingSlotNmb = Data.Strategy.CloseSlot; string sIndicatorName = Data.Strategy.Slot[iClosingSlotNmb].IndicatorName; Indicator indClose = Indicator_Store.ConstructIndicator(sIndicatorName, SlotTypes.Close); indClose.IndParam = Data.Strategy.Slot[iClosingSlotNmb].IndParam; indClose.SetDescription(SlotTypes.Close); bool bGroups = false; List <string> closegroups = new List <string>(); if (Data.Strategy.CloseFilters > 0) { foreach (IndicatorSlot slot in Data.Strategy.Slot) { if (slot.SlotType == SlotTypes.CloseFilter) { if (slot.LogicalGroup == "all" && Data.Strategy.CloseFilters > 1) { bGroups = true; } if (closegroups.Contains(slot.LogicalGroup)) { bGroups = true; } else if (slot.LogicalGroup != "all") { closegroups.Add(slot.LogicalGroup); } } } } if (closegroups.Count == 0 && Data.Strategy.CloseFilters > 0) { closegroups.Add("all"); // If all the slots are in "all" group, adds "all" to the list. } // Long position string sColoseLong = "<p>" + Language.T("Close an existing long position") + " " + indClose.ExitPointLongDescription; if (Data.Strategy.CloseFilters == 0) { sColoseLong += ".</p>"; } else if (Data.Strategy.CloseFilters == 1) { sColoseLong += " " + Language.T("when the following logic condition is satisfied") + ":</p>"; } else if (bGroups) { sColoseLong += " " + Language.T("when") + ":</p>"; } else { sColoseLong += " " + Language.T("when at least one of the following logic conditions is satisfied") + ":</p>"; } sb.AppendLine(sColoseLong); // Close Filters if (Data.Strategy.CloseFilters > 0) { int groupnumb = 1; sb.AppendLine("<ul>"); foreach (string group in closegroups) { if (bGroups) { sb.AppendLine("<li>" + (groupnumb == 1 ? "" : Language.T("or") + " ") + Language.T("logical group [#] is satisfied").Replace("#", group) + ":"); sb.AppendLine("<ul>"); groupnumb++; } int indInGroup = 0; for (int iSlot = iClosingSlotNmb + 1; iSlot < Data.Strategy.Slots; iSlot++) { if (Data.Strategy.Slot[iSlot].LogicalGroup == group || Data.Strategy.Slot[iSlot].LogicalGroup == "all") { indInGroup++; } } int indnumb = 1; for (int iSlot = iClosingSlotNmb + 1; iSlot < Data.Strategy.Slots; iSlot++) { if (Data.Strategy.Slot[iSlot].LogicalGroup != group && Data.Strategy.Slot[iSlot].LogicalGroup != "all") { continue; } Indicator indCloseFilter = Indicator_Store.ConstructIndicator(Data.Strategy.Slot[iSlot].IndicatorName, SlotTypes.CloseFilter); indCloseFilter.IndParam = Data.Strategy.Slot[iSlot].IndParam; indCloseFilter.SetDescription(SlotTypes.CloseFilter); if (bGroups) { if (indnumb < indInGroup) { sb.AppendLine("<li>" + indCloseFilter.ExitFilterLongDescription + "; " + Language.T("and") + "</li>"); } else { sb.AppendLine("<li>" + indCloseFilter.ExitFilterLongDescription + ".</li>"); } } else { if (iSlot < Data.Strategy.Slots - 1) { sb.AppendLine("<li>" + indCloseFilter.ExitFilterLongDescription + "; " + Language.T("or") + "</li>"); } else { sb.AppendLine("<li>" + indCloseFilter.ExitFilterLongDescription + ".</li>"); } } indnumb++; } if (bGroups) { sb.AppendLine("</ul>"); sb.AppendLine("</li>"); } } sb.AppendLine("</ul>"); } // Short position string sColoseShort = "<p>" + Language.T("Close an existing short position") + " " + indClose.ExitPointShortDescription; if (Data.Strategy.CloseFilters == 0) { sColoseShort += ".</p>"; } else if (Data.Strategy.CloseFilters == 1) { sColoseShort += " " + Language.T("when the following logic condition is satisfied") + ":</p>"; } else if (bGroups) { sColoseShort += " " + Language.T("when") + ":</p>"; } else { sColoseShort += " " + Language.T("when at least one of the following logic conditions is satisfied") + ":</p>"; } sb.AppendLine(sColoseShort); // Close Filters if (Data.Strategy.CloseFilters > 0) { int groupnumb = 1; sb.AppendLine("<ul>"); foreach (string group in closegroups) { if (bGroups) { sb.AppendLine("<li>" + (groupnumb == 1 ? "" : Language.T("or") + " ") + Language.T("logical group [#] is satisfied").Replace("#", group) + ":"); sb.AppendLine("<ul>"); groupnumb++; } int indInGroup = 0; for (int iSlot = iClosingSlotNmb + 1; iSlot < Data.Strategy.Slots; iSlot++) { if (Data.Strategy.Slot[iSlot].LogicalGroup == group || Data.Strategy.Slot[iSlot].LogicalGroup == "all") { indInGroup++; } } int indnumb = 1; for (int iSlot = iClosingSlotNmb + 1; iSlot < Data.Strategy.Slots; iSlot++) { if (Data.Strategy.Slot[iSlot].LogicalGroup != group && Data.Strategy.Slot[iSlot].LogicalGroup != "all") { continue; } Indicator indCloseFilter = Indicator_Store.ConstructIndicator(Data.Strategy.Slot[iSlot].IndicatorName, SlotTypes.CloseFilter); indCloseFilter.IndParam = Data.Strategy.Slot[iSlot].IndParam; indCloseFilter.SetDescription(SlotTypes.CloseFilter); if (bGroups) { if (indnumb < indInGroup) { sb.AppendLine("<li>" + indCloseFilter.ExitFilterShortDescription + "; " + Language.T("and") + "</li>"); } else { sb.AppendLine("<li>" + indCloseFilter.ExitFilterShortDescription + ".</li>"); } } else { if (iSlot < Data.Strategy.Slots - 1) { sb.AppendLine("<li>" + indCloseFilter.ExitFilterShortDescription + "; " + Language.T("or") + "</li>"); } else { sb.AppendLine("<li>" + indCloseFilter.ExitFilterShortDescription + ".</li>"); } } indnumb++; } if (bGroups) { sb.AppendLine("</ul>"); sb.AppendLine("</li>"); } } sb.AppendLine("</ul>"); } return(sb); }
/// <summary> /// Generates a HTML report about the opening logic. /// </summary> StringBuilder OpeningLogicHTMLReport() { StringBuilder sb = new StringBuilder(); string sIndicatorName = Data.Strategy.Slot[0].IndicatorName; Indicator indOpen = Indicator_Store.ConstructIndicator(sIndicatorName, SlotTypes.Open); indOpen.IndParam = Data.Strategy.Slot[0].IndParam; indOpen.SetDescription(SlotTypes.Open); // Logical groups of the opening conditions. List <string> opengroups = new List <string>(); for (int iSlot = 1; iSlot <= Data.Strategy.OpenFilters; iSlot++) { string group = Data.Strategy.Slot[iSlot].LogicalGroup; if (!opengroups.Contains(group) && group != "All") { opengroups.Add(group); // Adds all groups except "All" } } if (opengroups.Count == 0 && Data.Strategy.OpenFilters > 0) { opengroups.Add("All"); // If all the slots are in "All" group, adds "All" to the list. } // Long position string sOpenLong = "<p>"; if (Data.Strategy.sameDirSignlAct == SameDirSignalAction.Add) { sOpenLong = Language.T("Open a new long position or add to an existing position"); } else if (Data.Strategy.sameDirSignlAct == SameDirSignalAction.Winner) { sOpenLong = Language.T("Open a new long position or add to a winning position"); } else if (Data.Strategy.sameDirSignlAct == SameDirSignalAction.Nothing) { sOpenLong = Language.T("Open a new long position"); } if (OppSignalAction == OppositeDirSignalAction.Close) { sOpenLong += " " + Language.T("or close a short position"); } else if (OppSignalAction == OppositeDirSignalAction.Reduce) { sOpenLong += " " + Language.T("or reduce a short position"); } else if (OppSignalAction == OppositeDirSignalAction.Reverse) { sOpenLong += " " + Language.T("or reverse a short position"); } else if (OppSignalAction == OppositeDirSignalAction.Nothing) { sOpenLong += ""; } sOpenLong += " " + indOpen.EntryPointLongDescription; if (Data.Strategy.OpenFilters == 0) { sOpenLong += ".</p>"; } else if (Data.Strategy.OpenFilters == 1) { sOpenLong += " " + Language.T("when the following logic condition is satisfied") + ":</p>"; } else if (opengroups.Count > 1) { sOpenLong += " " + Language.T("when") + ":</p>"; } else { sOpenLong += " " + Language.T("when all the following logic conditions are satisfied") + ":</p>"; } sb.AppendLine(sOpenLong); // Open Filters if (Data.Strategy.OpenFilters > 0) { int groupnumb = 1; if (opengroups.Count > 1) { sb.AppendLine("<ul>"); } foreach (string group in opengroups) { if (opengroups.Count > 1) { sb.AppendLine("<li>" + (groupnumb == 1 ? "" : Language.T("or") + " ") + Language.T("logical group [#] is satisfied").Replace("#", group) + ":"); groupnumb++; } sb.AppendLine("<ul>"); int indInGroup = 0; for (int iSlot = 1; iSlot <= Data.Strategy.OpenFilters; iSlot++) { if (Data.Strategy.Slot[iSlot].LogicalGroup == group || Data.Strategy.Slot[iSlot].LogicalGroup == "All") { indInGroup++; } } int indnumb = 1; for (int iSlot = 1; iSlot <= Data.Strategy.OpenFilters; iSlot++) { if (Data.Strategy.Slot[iSlot].LogicalGroup != group && Data.Strategy.Slot[iSlot].LogicalGroup != "All") { continue; } Indicator indOpenFilter = Indicator_Store.ConstructIndicator(Data.Strategy.Slot[iSlot].IndicatorName, SlotTypes.OpenFilter); indOpenFilter.IndParam = Data.Strategy.Slot[iSlot].IndParam; indOpenFilter.SetDescription(SlotTypes.OpenFilter); if (indnumb < indInGroup) { sb.AppendLine("<li>" + indOpenFilter.EntryFilterLongDescription + "; " + Language.T("and") + "</li>"); } else { sb.AppendLine("<li>" + indOpenFilter.EntryFilterLongDescription + ".</li>"); } indnumb++; } sb.AppendLine("</ul>"); if (opengroups.Count > 1) { sb.AppendLine("</li>"); } } if (opengroups.Count > 1) { sb.AppendLine("</ul>"); } } // Short position string sOpenShort = "<p>"; if (Data.Strategy.sameDirSignlAct == SameDirSignalAction.Add) { sOpenShort = Language.T("Open a new short position or add to an existing position"); } else if (Data.Strategy.sameDirSignlAct == SameDirSignalAction.Winner) { sOpenShort = Language.T("Open a new short position or add to a winning position"); } else if (Data.Strategy.sameDirSignlAct == SameDirSignalAction.Nothing) { sOpenShort = Language.T("Open a new short position"); } if (OppSignalAction == OppositeDirSignalAction.Close) { sOpenShort += " " + Language.T("or close a long position"); } else if (OppSignalAction == OppositeDirSignalAction.Reduce) { sOpenShort += " " + Language.T("or reduce a long position"); } else if (OppSignalAction == OppositeDirSignalAction.Reverse) { sOpenShort += " " + Language.T("or reverse a long position"); } else if (OppSignalAction == OppositeDirSignalAction.Nothing) { sOpenShort += ""; } sOpenShort += " " + indOpen.EntryPointShortDescription; if (Data.Strategy.OpenFilters == 0) { sOpenShort += ".</p>"; } else if (Data.Strategy.OpenFilters == 1) { sOpenShort += " " + Language.T("when the following logic condition is satisfied") + ":</p>"; } else if (opengroups.Count > 1) { sOpenShort += " " + Language.T("when") + ":</p>"; } else { sOpenShort += " " + Language.T("when all the following logic conditions are satisfied") + ":</p>"; } sb.AppendLine(sOpenShort); // Open Filters if (Data.Strategy.OpenFilters > 0) { int groupnumb = 1; if (opengroups.Count > 1) { sb.AppendLine("<ul>"); } foreach (string group in opengroups) { if (opengroups.Count > 1) { sb.AppendLine("<li>" + (groupnumb == 1 ? "" : Language.T("or") + " ") + Language.T("logical group [#] is satisfied").Replace("#", group) + ":"); groupnumb++; } sb.AppendLine("<ul>"); int indInGroup = 0; for (int iSlot = 1; iSlot <= Data.Strategy.OpenFilters; iSlot++) { if (Data.Strategy.Slot[iSlot].LogicalGroup == group || Data.Strategy.Slot[iSlot].LogicalGroup == "All") { indInGroup++; } } int indnumb = 1; for (int iSlot = 1; iSlot <= Data.Strategy.OpenFilters; iSlot++) { if (Data.Strategy.Slot[iSlot].LogicalGroup != group && Data.Strategy.Slot[iSlot].LogicalGroup != "All") { continue; } Indicator indOpenFilter = Indicator_Store.ConstructIndicator(Data.Strategy.Slot[iSlot].IndicatorName, SlotTypes.OpenFilter); indOpenFilter.IndParam = Data.Strategy.Slot[iSlot].IndParam; indOpenFilter.SetDescription(SlotTypes.OpenFilter); if (indnumb < indInGroup) { sb.AppendLine("<li>" + indOpenFilter.EntryFilterShortDescription + "; " + Language.T("and") + "</li>"); } else { sb.AppendLine("<li>" + indOpenFilter.EntryFilterShortDescription + ".</li>"); } indnumb++; } sb.AppendLine("</ul>"); if (opengroups.Count > 1) { sb.AppendLine("</li>"); } } if (opengroups.Count > 1) { sb.AppendLine("</ul>"); } } return(sb); }
/// <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. Strategy 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. tempStrategy.UsePermanentSL = bool.Parse(xmlDocStrategy.GetElementsByTagName("permanentStopLoss")[0].Attributes["usePermanentSL"].InnerText); try { tempStrategy.PermanentSLType = (PermanentProtectionType)Enum.Parse(typeof(PermanentProtectionType), xmlDocStrategy.GetElementsByTagName("permanentStopLoss")[0].Attributes["permanentSLType"].InnerText); } catch { tempStrategy.PermanentSLType = PermanentProtectionType.Relative; } // Permanent Take Profit tempStrategy.PermanentTP = int.Parse(xmlDocStrategy.GetElementsByTagName("permanentTakeProfit")[0].InnerText); tempStrategy.UsePermanentTP = bool.Parse(xmlDocStrategy.GetElementsByTagName("permanentTakeProfit")[0].Attributes["usePermanentTP"].InnerText); try { tempStrategy.PermanentTPType = (PermanentProtectionType)Enum.Parse(typeof(PermanentProtectionType), xmlDocStrategy.GetElementsByTagName("permanentTakeProfit")[0].Attributes["permanentTPType"].InnerText); } catch { tempStrategy.PermanentTPType = PermanentProtectionType.Relative; } // Break Even try { tempStrategy.BreakEven = int.Parse(xmlDocStrategy.GetElementsByTagName("breakEven")[0].InnerText); tempStrategy.UseBreakEven = bool.Parse(xmlDocStrategy.GetElementsByTagName("breakEven")[0].Attributes["useBreakEven"].InnerText); } catch { } // 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); // 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 iSlot = 0; iSlot < xmlSlotList.Count; iSlot++) { XmlNodeList xmlSlotTagList = xmlSlotList[iSlot].ChildNodes; SlotTypes slType = (SlotTypes)Enum.Parse(typeof(SlotTypes), xmlSlotList[iSlot].Attributes["slotType"].InnerText); // Logical group if (slType == SlotTypes.OpenFilter || slType == SlotTypes.CloseFilter) { XmlAttributeCollection attributes = xmlSlotList[iSlot].Attributes; XmlNode nodeGroup = attributes.GetNamedItem("logicalGroup"); string defGroup = GetDefaultGroup(slType, iSlot, tempStrategy.CloseSlot); if (nodeGroup != null) { tempStrategy.Slot[iSlot].LogicalGroup = nodeGroup.InnerText; if (nodeGroup.InnerText != defGroup && !Configs.UseLogicalGroups) { System.Windows.Forms.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"), System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information); Configs.UseLogicalGroups = true; } } else { tempStrategy.Slot[iSlot].LogicalGroup = defGroup; } } // Indicator name. string sIndicatorName = xmlSlotTagList[0].InnerText; Indicator indicator = Indicator_Store.ConstructIndicator(sIndicatorName, slType); for (int iTag = 1; iTag < xmlSlotTagList.Count; iTag++) { // List parameters if (xmlSlotTagList[iTag].Name == "listParam") { int iListParam = int.Parse(xmlSlotTagList[iTag].Attributes["paramNumber"].InnerText); XmlNode xmlListParamNode = xmlSlotTagList[iTag].FirstChild; indicator.IndParam.ListParam[iListParam].Caption = xmlListParamNode.InnerText; xmlListParamNode = xmlListParamNode.NextSibling; int index = int.Parse(xmlListParamNode.InnerText); indicator.IndParam.ListParam[iListParam].Index = index; indicator.IndParam.ListParam[iListParam].Text = indicator.IndParam.ListParam[iListParam].ItemList[index]; } // Numeric parameters if (xmlSlotTagList[iTag].Name == "numParam") { XmlNode xmlNumParamNode = xmlSlotTagList[iTag].FirstChild; int iNumParam = int.Parse(xmlSlotTagList[iTag].Attributes["paramNumber"].InnerText); indicator.IndParam.NumParam[iNumParam].Caption = xmlNumParamNode.InnerText; xmlNumParamNode = xmlNumParamNode.NextSibling; string sNumParamValue = xmlNumParamNode.InnerText; sNumParamValue = sNumParamValue.Replace(',', Data.PointChar); sNumParamValue = sNumParamValue.Replace('.', Data.PointChar); float fValue = float.Parse(sNumParamValue); // Removing of the Stop Loss negative sign used in previous versions. string sParCaption = indicator.IndParam.NumParam[iNumParam].Caption; if (sParCaption == "Trailing Stop" || sParCaption == "Initial Stop Loss" || sParCaption == "Stop Loss") { fValue = Math.Abs(fValue); } indicator.IndParam.NumParam[iNumParam].Value = fValue; } // Check parameters if (xmlSlotTagList[iTag].Name == "checkParam") { XmlNode xmlCheckParamNode = xmlSlotTagList[iTag].FirstChild; int iCheckParam = int.Parse(xmlSlotTagList[iTag].Attributes["paramNumber"].InnerText); indicator.IndParam.CheckParam[iCheckParam].Caption = xmlCheckParamNode.InnerText; xmlCheckParamNode = xmlCheckParamNode.NextSibling; indicator.IndParam.CheckParam[iCheckParam].Checked = bool.Parse(xmlCheckParamNode.InnerText); } } // Calculate the indicator. indicator.Calculate(slType); tempStrategy.Slot[iSlot].IndicatorName = indicator.IndicatorName; tempStrategy.Slot[iSlot].IndParam = indicator.IndParam; tempStrategy.Slot[iSlot].Component = indicator.Component; tempStrategy.Slot[iSlot].SeparatedChart = indicator.SeparatedChart; tempStrategy.Slot[iSlot].SpecValue = indicator.SpecialValues; tempStrategy.Slot[iSlot].MinValue = indicator.SeparatedChartMinValue; tempStrategy.Slot[iSlot].MaxValue = indicator.SeparatedChartMaxValue; tempStrategy.Slot[iSlot].IsDefined = true; } return(tempStrategy); }
/// <summary> /// Performs thorough indicator test. /// </summary> /// <returns>True, if the test succeed.</returns> public static bool CustomIndicatorThoroughTest(string indicatorName, out string errorList) { bool isOk = true; StringBuilder sb = new StringBuilder(); sb.AppendLine("ERROR: Indicator test failed for the '" + indicatorName + "' indicator."); foreach (SlotTypes slotType in Enum.GetValues(typeof(SlotTypes))) { if (slotType == SlotTypes.NotDefined) { continue; } Indicator indicator = Indicator_Store.ConstructIndicator(indicatorName, slotType); if (!indicator.TestPossibleSlot(slotType)) { continue; } foreach (NumericParam numParam in indicator.IndParam.NumParam) { if (numParam.Enabled) { numParam.Value = numParam.Min; } } try { indicator.Calculate(slotType); } catch (System.Exception exc) { sb.AppendLine("\tError when calculating with NumParams set to their minimal values. " + exc.Message); isOk = false; break; } foreach (NumericParam numParam in indicator.IndParam.NumParam) { if (numParam.Enabled) { numParam.Value = numParam.Max; } } try { indicator.Calculate(slotType); } catch (System.Exception exc) { sb.AppendLine("\tError when calculating with NumParams set to their maximal values. " + exc.Message); isOk = false; break; } try { foreach (IndicatorComp component in indicator.Component) { switch (slotType) { case SlotTypes.Open: if (component.DataType == IndComponentType.AllowOpenLong || component.DataType == IndComponentType.AllowOpenShort || component.DataType == IndComponentType.CloseLongPrice || component.DataType == IndComponentType.ClosePrice || component.DataType == IndComponentType.CloseShortPrice || component.DataType == IndComponentType.ForceClose || component.DataType == IndComponentType.ForceCloseLong || component.DataType == IndComponentType.ForceCloseShort || component.DataType == IndComponentType.NotDefined) { sb.AppendLine("\tProbably wrong component type when SlotType is 'SlotTypes.Open' - '" + component.CompName + "' of type '" + component.DataType + "'."); isOk = false; } break; case SlotTypes.OpenFilter: if (component.DataType == IndComponentType.OpenClosePrice || component.DataType == IndComponentType.OpenLongPrice || component.DataType == IndComponentType.OpenPrice || component.DataType == IndComponentType.OpenShortPrice || component.DataType == IndComponentType.CloseLongPrice || component.DataType == IndComponentType.ClosePrice || component.DataType == IndComponentType.CloseShortPrice || component.DataType == IndComponentType.ForceClose || component.DataType == IndComponentType.ForceCloseLong || component.DataType == IndComponentType.ForceCloseShort || component.DataType == IndComponentType.NotDefined) { sb.AppendLine("\tProbably wrong component type when SlotType is 'SlotTypes.OpenFilter' - '" + component.CompName + "' of type '" + component.DataType + "'."); isOk = false; } break; case SlotTypes.Close: if (component.DataType == IndComponentType.AllowOpenLong || component.DataType == IndComponentType.AllowOpenShort || component.DataType == IndComponentType.OpenLongPrice || component.DataType == IndComponentType.OpenPrice || component.DataType == IndComponentType.OpenShortPrice || component.DataType == IndComponentType.ForceClose || component.DataType == IndComponentType.ForceCloseLong || component.DataType == IndComponentType.ForceCloseShort || component.DataType == IndComponentType.NotDefined) { sb.AppendLine("\tProbably wrong component type when SlotType is 'SlotTypes.Close' - '" + component.CompName + "' of type '" + component.DataType + "'."); isOk = false; } break; case SlotTypes.CloseFilter: if (component.DataType == IndComponentType.AllowOpenLong || component.DataType == IndComponentType.AllowOpenShort || component.DataType == IndComponentType.OpenLongPrice || component.DataType == IndComponentType.OpenPrice || component.DataType == IndComponentType.OpenShortPrice || component.DataType == IndComponentType.CloseLongPrice || component.DataType == IndComponentType.ClosePrice || component.DataType == IndComponentType.CloseShortPrice || component.DataType == IndComponentType.NotDefined) { sb.AppendLine("\tProbably wrong component type when SlotType is 'SlotTypes.CloseFilter' - '" + component.CompName + "' of type '" + component.DataType + "'."); isOk = false; } break; default: break; } if (component.DataType == IndComponentType.AllowOpenLong || component.DataType == IndComponentType.AllowOpenShort || component.DataType == IndComponentType.ForceClose || component.DataType == IndComponentType.ForceCloseLong || component.DataType == IndComponentType.ForceCloseShort) { foreach (double value in component.Value) { if (value != 0 && value != 1) { sb.AppendLine("\tWrong component values. The values of '" + component.CompName + "' must be 0 or 1."); isOk = false; break; } } } } } catch (System.Exception exc) { sb.AppendLine("\tError when checking the indicator's components. " + exc.Message); isOk = false; break; } } if (isOk) { errorList = string.Empty; } else { errorList = sb.ToString(); } return(isOk); }
/// <summary> /// Calculates the selected indicator chart. /// </summary> void CalculateIndicator() { if (!Data.IsData || !isPaintAllowed) { return; } SetOppositeSignalBehaviour(); SetClosingLogicConditions(); Indicator indicator = Indicator_Store.ConstructIndicator(indicatorName, slotType); // List params for (int i = 0; i < 5; i++) { indicator.IndParam.ListParam[i].Index = ListParam[i].SelectedIndex; indicator.IndParam.ListParam[i].Text = ListParam[i].Text; indicator.IndParam.ListParam[i].Enabled = ListParam[i].Enabled; } // Numeric params for (int i = 0; i < 6; i++) { indicator.IndParam.NumParam[i].Value = (double)NumParam[i].Value; indicator.IndParam.NumParam[i].Enabled = NumParam[i].Enabled; } // Check params for (int i = 0; i < 2; i++) { indicator.IndParam.CheckParam[i].Checked = CheckParam[i].Checked; indicator.IndParam.CheckParam[i].Enabled = CheckParam[i].Enabled; if (CheckParam[i].Text == "Use previous bar value") { indicator.IndParam.CheckParam[i].Enabled = true; } else { indicator.IndParam.CheckParam[i].Enabled = CheckParam[i].Enabled; } } indicator.Calculate(slotType); //Sets the Data.Strategy. Data.Strategy.Slot[slot].IndicatorName = indicator.IndicatorName; Data.Strategy.Slot[slot].IndParam = indicator.IndParam; Data.Strategy.Slot[slot].Component = indicator.Component; Data.Strategy.Slot[slot].SeparatedChart = indicator.SeparatedChart; Data.Strategy.Slot[slot].SpecValue = indicator.SpecialValues; Data.Strategy.Slot[slot].MinValue = indicator.SeparatedChartMinValue; Data.Strategy.Slot[slot].MaxValue = indicator.SeparatedChartMaxValue; Data.Strategy.Slot[slot].IsDefined = true; // Searches the indicators' components to determine the Data.FirstBar. Data.FirstBar = Data.Strategy.SetFirstBar(); // Checks "Use previous bar value". if (Data.Strategy.AdjustUsePreviousBarValue()) { for (int i = 0; i < 2; i++) { if (indicator.IndParam.CheckParam[i].Caption == "Use previous bar value") { aChbCheck[i].Checked = Data.Strategy.Slot[slot].IndParam.CheckParam[i].Checked; } } } SetIndicatorNotification(indicator); return; }
/// <summary> /// Sets the trvIndicators nodes /// </summary> void SetTreeViewIndicators() { TreeNode trnAll = new TreeNode(); trnAll.Name = "trnAll"; trnAll.Text = Language.T("All"); trnAll.Tag = false; TreeNode trnIndicators = new TreeNode(); trnIndicators.Name = "trnIndicators"; trnIndicators.Text = Language.T("Indicators"); trnIndicators.Tag = false; TreeNode trnAdditional = new TreeNode(); trnAdditional.Name = "trnAdditional"; trnAdditional.Text = Language.T("Additional"); trnAdditional.Tag = false; TreeNode trnOscillatorOfIndicators = new TreeNode(); trnOscillatorOfIndicators.Name = "trnOscillatorOfIndicators"; trnOscillatorOfIndicators.Text = Language.T("Oscillator of Indicators"); trnOscillatorOfIndicators.Tag = false; TreeNode trnIndicatorsMAOscillator = new TreeNode(); trnIndicatorsMAOscillator.Name = "trnIndicatorMA"; trnIndicatorsMAOscillator.Text = Language.T("Indicator's MA Oscillator"); trnIndicatorsMAOscillator.Tag = false; TreeNode trnDateTime = new TreeNode(); trnDateTime.Name = "trnDateTime"; trnDateTime.Text = Language.T("Date/Time Functions"); trnDateTime.Tag = false; TreeNode trnCustomIndicators = new TreeNode(); trnCustomIndicators.Name = "trnCustomIndicators"; trnCustomIndicators.Text = Language.T("Custom Indicators"); trnCustomIndicators.Tag = false; trvIndicators.Nodes.AddRange(new TreeNode[] { trnAll, trnIndicators, trnAdditional, trnOscillatorOfIndicators, trnIndicatorsMAOscillator, trnDateTime, trnCustomIndicators }); foreach (string indicatorName in Indicator_Store.GetIndicatorNames(slotType)) { // Checks the indicator name in the list of forbidden indicators. bool toContinue = false; foreach (string forbiden in Data.IndicatorsForBacktestOnly) { if (indicatorName == forbiden) { toContinue = true; break; } } if (toContinue) { continue; } TreeNode trn = new TreeNode(); trn.Tag = true; trn.Name = indicatorName; trn.Text = indicatorName; trnAll.Nodes.Add(trn); Indicator indicator = Indicator_Store.ConstructIndicator(indicatorName, slotType); TypeOfIndicator type = indicator.IndParam.IndicatorType; if (indicator.CustomIndicator) { TreeNode trnCustom = new TreeNode(); trnCustom.Tag = true; trnCustom.Name = indicatorName; trnCustom.Text = indicatorName; trnCustomIndicators.Nodes.Add(trnCustom); } TreeNode trnGroups = new TreeNode(); trnGroups.Tag = true; trnGroups.Name = indicatorName; trnGroups.Text = indicatorName; if (type == TypeOfIndicator.Indicator) { trnIndicators.Nodes.Add(trnGroups); } else if (type == TypeOfIndicator.Additional) { trnAdditional.Nodes.Add(trnGroups); } else if (type == TypeOfIndicator.OscillatorOfIndicators) { trnOscillatorOfIndicators.Nodes.Add(trnGroups); } else if (type == TypeOfIndicator.IndicatorsMA) { trnIndicatorsMAOscillator.Nodes.Add(trnGroups); } else if (type == TypeOfIndicator.DateTime) { trnDateTime.Nodes.Add(trnGroups); } } return; }