/// <summary> /// Add a new trade rule and join condition. /// </summary> /// <param name="tradeRule">The trade rule to add.</param> /// <param name="ruleJoinType">The join condition to add.</param> public void Add(TradeRule tradeRule, RuleJoinType ruleJoinType) { if (this.RuleJoinTypes.Count > 0 && this.RuleJoinTypes.Last() == RuleJoinType.none) { throw new TradeRuleException("Can not add another trade rule. The previous join type was none."); } this.TradeRules.Add(tradeRule); this.RuleJoinTypes.Add(ruleJoinType); }
/// <summary> /// Evaluates the trade rule for the current indicatorLibraryAdapter line. /// </summary> /// <param name="indicatorLibraryAdapter">The data source to evaluate.</param> /// <returns>A boolean value for the conditionon this line.</returns> public bool Eval(TestData indicatorLibraryAdapter) { if (this.TradeRules.Count == 0) { throw new TradeRuleException("Trade rules not defined."); } DatePoint newPoint = indicatorLibraryAdapter.GetStockPoint(); TradeRule[] traderule1 = new TradeRule[TradeRules.Count]; bool evalValue = false; if (TradeRules.Count >= 2) { bool[] tradeeval = new bool[TradeRules.Count]; for (int i = 0; i < TradeRules.Count; i++) { string indicator1 = TradeRules[i].IndicatorName1; string indicator2 = TradeRules[i].IndicatorName2; TradeRule nT = TradeRuleFactory.GenerateTradeRule(indicator1, TradeRules[i].CompareType, indicator2); traderule1[i] = nT; } for (int i = 0; i < TradeRules.Count; i++) { tradeeval[i] = traderule1[i].Eval(indicatorLibraryAdapter); } for (int i = 0; i < RuleJoinTypes.Count; i++) { if (i != 0 && RuleJoinTypes[i - 1] == RuleJoinType.and) { } else { while (RuleJoinTypes[i] == RuleJoinType.and) { if (tradeeval[i] && tradeeval[i + 1]) { evalValue = true; } else { evalValue = false; break; } i++; if (i >= RuleJoinTypes.Count) { i--; break; } } } if (evalValue == true) { break; } if (RuleJoinTypes[i] == RuleJoinType.or) { if (i >= RuleJoinTypes.Count) { } else if (RuleJoinTypes[i + 1] == RuleJoinType.and) { while (RuleJoinTypes[i] == RuleJoinType.and) { if (tradeeval[i] && tradeeval[i + 1]) { evalValue = true; } else { evalValue = false; } i++; } if (evalValue == true) { break; } } else { if (tradeeval[i]) { evalValue = true; break; } } } } } else { string indicator1 = TradeRules[0].IndicatorName1; string indicator2 = TradeRules[0].IndicatorName2; traderule1[0] = TradeRuleFactory.GenerateTradeRule(indicator1, TradeRules[0].CompareType, indicator2); evalValue = traderule1[0].Eval(indicatorLibraryAdapter); } return(evalValue); }
/// <summary> /// Evaluates the trade rule for the current indicatorLibraryAdapter line. /// </summary> /// <param name="indicatorLibraryAdapter">The data source to evaluate.</param> /// <returns>A boolean value for the conditionon this line.</returns> public bool Eval(Dictionary <DateTime, Dictionary <string, double> > indicatorLibraryAdapter, DatePoint point) { if (this.TradeRules.Count == 0) { throw new TradeRuleException("Trade rules not defined."); } DatePoint newPoint = point; TradeRule[] traderule1 = new TradeRule[TradeRules.Count]; bool evalValue = false; if (TradeRules.Count >= 2) { bool[] tradeeval = new bool[TradeRules.Count]; int tradeevalcount = 0; for (int i = 0; i < TradeRules.Count; i++) { string indicator1 = TradeRules[i].IndicatorName1; string indicator2 = TradeRules[i].IndicatorName2; TradeRule nT = TradeRuleFactory.GenerateTradeRule(indicator1, TradeRules[i].CompareType, indicator2); traderule1[i] = nT; } for (int i = 0; i < TradeRules.Count; i++) { tradeeval[i] = traderule1[i].Eval(indicatorLibraryAdapter, point); } for (int i = 0; i < RuleJoinTypes.Count; i++) { #region and jointype if (RuleJoinTypes[i] == RuleJoinType.and) { int andCount = 0; int j = 0; while (RuleJoinTypes[i] == RuleJoinType.and) { for (int k = 0; k < 2; k++) { if (tradeevalcount >= tradeeval.Count()) { break; } if (tradeeval[tradeevalcount++]) { andCount++; j++; } else { j++; } } if (i >= RuleJoinTypes.Count - 1) { //i--; break; } if (RuleJoinTypes[i + 1] == RuleJoinType.and) { tradeevalcount--; } i++; } if (j == andCount) { return(true); } } #endregion #region or jointype if (RuleJoinTypes[i] == RuleJoinType.or) { /*if (i == 0) * { * if (tradeeval[tradeevalcount++]) * { * return true; * } * } * else if (RuleJoinTypes[i - 1] == RuleJoinType.or && RuleJoinTypes[i+1] != RuleJoinType.and) * { * if (tradeeval[tradeevalcount++]) * { * return true; * } * } * else */if (i >= RuleJoinTypes.Count - 1) { if (tradeeval[tradeevalcount++]) { return(true); } } else if (RuleJoinTypes[i + 1] == RuleJoinType.and) { int andCount = 0; int j = 0; while (RuleJoinTypes[i + 1] == RuleJoinType.and) { for (int k = 0; k < 2; k++) { if (tradeevalcount >= tradeeval.Count()) { break; } if (tradeeval[tradeevalcount++]) { andCount++; j++; } else { j++; } } if (RuleJoinTypes[i + 1] == RuleJoinType.and) { tradeevalcount--; } i++; if (i >= RuleJoinTypes.Count - 1) { //i--; break; } } if (andCount == j) { return(true); } } else { if (tradeevalcount >= tradeeval.Count()) { break; } if (tradeeval[tradeevalcount++]) { return(true); } } } } #endregion } else { string indicator1 = TradeRules[0].IndicatorName1; string indicator2 = TradeRules[0].IndicatorName2; traderule1[0] = TradeRuleFactory.GenerateTradeRule(indicator1, TradeRules[0].CompareType, indicator2); evalValue = traderule1[0].Eval(indicatorLibraryAdapter, point); } return(evalValue); }