private static IPrimitive AddRangeChecks(MetaFormulaItem arg1, MetaFormulaItem arg2, IPrimitive crossCheck) { return(new AndPrimitive() .AddParameter(FormatRangeCheck(arg1)) .AddParameter(FormatRangeCheck(arg2)) .AddParameter(crossCheck)); }
/// <summary> /// Formats range check /// </summary> /// <param name="item">Item to format the range check to.</param> /// <exception cref="NotSupportedFormulaItemException"></exception> /// <returns>Primitive with formatted range check or null when there is no need to check it.</returns> private static IPrimitive FormatRangeCheck(MetaFormulaItem item) { switch (item.ValueType) { case FormulaItemType.Stream: { var minCount = item.Value == "source" ? "3" : "2"; var streamSize = FormatStreamName(item) + ":size()"; var streamMinSize = string.IsNullOrEmpty(item.PeriodShift) ? minCount : $"{minCount} + {item.PeriodShift}"; return($"{streamSize} >= {streamMinSize}".MakePrimitive()); } case FormulaItemType.StreamValue: { var minCount = item.Value == "source" ? "2" : "1"; var streamSize = FormatStreamName(item) + ":size()"; var streamMinSize = string.IsNullOrEmpty(item.PeriodShift) ? minCount : $"{minCount} + {item.PeriodShift}"; return($"{streamSize} >= {streamMinSize}".MakePrimitive()); } case FormulaItemType.Parameter: case FormulaItemType.Value: return(null); default: throw new NotSupportedFormulaItemException(item.ValueType, "Lua"); } }
private IPrimitive FormatCrossMethodCall(MetaFormulaItem arg1, MetaFormulaItem arg2, string methodName) { var methodCall = new FunctionCall(methodName) .AddParameter(FormatStreamOrValue(arg1)) .AddParameter(FormatStreamOrValue(arg2)); if (arg1.PeriodShiftSource != null) { methodCall.AddParameter(FormatPeriod(arg1)); } else { IPrimitive period = FormatNowPeriod(arg1); methodCall.AddParameter(period) .AddCondition(_factory.MakeIsNotNull(period)); } if (arg2.ValueType == FormulaItemType.Stream) { if (arg2.PeriodShiftSource != null) { methodCall.AddParameter(FormatPeriod(arg2)); } else { IPrimitive period = FormatNowPeriod(arg2); methodCall.AddParameter(period) .AddCondition(_factory.MakeIsNotNull(period)); } } return(_factory.MakeConst(AddRangeChecks(arg1, arg2, methodCall))); }
/// <summary> /// Formats a value /// </summary> /// <param name="item">Item to format.</param> /// <exception cref="NotSupportedFormulaItemException">When formula of the specified type is not supported</exception> /// <returns>Primitive with the formatted value</returns> private IPrimitive FormatStreamValueOrValue(MetaFormulaItem item) { switch (item.ValueType) { case FormulaItemType.Value: return(item.Value.MakePrimitive()); case FormulaItemType.Stream: case FormulaItemType.StreamValue: if (item.PeriodShiftSource != null) { return(new FunctionCall(FormatStreamName(item) + ":tick") .AddParameter(FormatPeriod(item))); } IPrimitive period = FormatNowPeriod(item); return(new FunctionCall(FormatStreamName(item) + ":tick") .AddParameter(period) .AddCondition(_factory.MakeIsNotNull(period))); case FormulaItemType.Parameter: return($"instance.parameters.{item.Value}".MakePrimitive()); default: throw new NotSupportedFormulaItemException(item.ValueType, "Lua"); } }
private IPrimitive FormatNowPeriod(MetaFormulaItem arg) { if (string.IsNullOrEmpty(arg.PeriodShift)) { return(_factory.MakeConst($"trading_logic:GetLastPeriod(period, source, {FormatStreamName(arg)})".MakePrimitive(), true)); } return(_factory.MakeConst($"trading_logic:GetPeriod(period - {arg.PeriodShift}, source, {FormatStreamName(arg)})".MakePrimitive(), true)); }
IPrimitive GetIndicatorSourceId(MetaFormulaItem source) { var substream = FormatSubstream(source); if (source.Value == null) { return($"trading_logic.MainSource{substream}".MakePrimitive()); } return($"{source.Value}{substream}".MakePrimitive()); }
private IPrimitive FormatStreamOrValue(MetaFormulaItem arg) { switch (arg.ValueType) { case FormulaItemType.Stream: return(FormatStreamName(arg).MakePrimitive()); default: return(FormatStreamValueOrValue(arg)); } }
private IPrimitive FormatPeriod(MetaFormulaItem arg) { IPrimitive nowPeriod = FormatNowPeriod(arg); var getDateCall = new FunctionCall(arg.PeriodShiftSource + ":date") .AddParameter(nowPeriod) .AddCondition(_factory.MakeIsNotNull(nowPeriod)); var findDateCall = new FunctionCall("core.findDate") .AddParameter(FormatStreamOrValue(arg)) .AddParameter(getDateCall) .AddParameter("false"); return(findDateCall); }
string FormatSubstream(MetaFormulaItem item) { var source = _model.Sources.Where(s => s.Id == item.Value).FirstOrDefault(); if (source == null) { return(string.IsNullOrEmpty(item.Substream) ? "" : "." + item.Substream); } if (source.SourceType == SourceType.Indicator) { return(string.IsNullOrEmpty(item.Substream) ? ".DATA" : item.Substream); } return(string.IsNullOrEmpty(item.Substream) ? "" : "." + item.Substream); }
private static string FormatStreamName(MetaFormulaItem arg) { var substream = arg.Substream; if (arg.StreamType == StreamType.Indicator) { if (string.IsNullOrEmpty(substream)) { substream = "DATA"; } } return(string.IsNullOrEmpty(substream) ? arg.Value : string.IsNullOrEmpty(arg.Value) ? $"source.{substream}" : $"{arg.Value}.{substream}"); }
public static string FormatSource(MetaFormulaItem source, string defaultSubstream) { if (source.StreamType == StreamType.Instrument) { if (source.Value == null && source.Substream == null) { return(defaultSubstream); } if (source.Substream == null) { return($"{source.Value}_{defaultSubstream}"); } } if (source.Value == null) { return(source.Substream.ToLower()); } var substream = string.IsNullOrEmpty(source.Substream) ? "" : "_" + source.Substream.ToLower(); return(source.Value + substream); }
public static ICondition Create(Condition condition) { switch (condition.ConditionType) { case Condition.AND: return(new MultiArgumentCondition() { ConditionType = ConditionType.And, Subconditions = CreateSubconditions(condition.Conditions) }); case Condition.OR: return(new MultiArgumentCondition() { ConditionType = ConditionType.Or, Subconditions = CreateSubconditions(condition.Conditions) }); case Condition.CROSSES: return(new TwoArgumentCondition() { ConditionType = ConditionType.Crosses, Arg1 = MetaFormulaItem.Create(condition.Arg1), Arg2 = MetaFormulaItem.Create(condition.Arg2) }); case Condition.CROSSES_OVER: return(new TwoArgumentCondition() { ConditionType = ConditionType.CrossesOver, Arg1 = MetaFormulaItem.Create(condition.Arg1), Arg2 = MetaFormulaItem.Create(condition.Arg2) }); case Condition.CROSSES_OVER_OR_TOUCH: return(new TwoArgumentCondition() { ConditionType = ConditionType.CrossesOverOrTouch, Arg1 = MetaFormulaItem.Create(condition.Arg1), Arg2 = MetaFormulaItem.Create(condition.Arg2) }); case Condition.CROSSES_UNDER: return(new TwoArgumentCondition() { ConditionType = ConditionType.CrossesUnder, Arg1 = MetaFormulaItem.Create(condition.Arg1), Arg2 = MetaFormulaItem.Create(condition.Arg2) }); case Condition.CROSSES_UNDER_OR_TOUCH: return(new TwoArgumentCondition() { ConditionType = ConditionType.CrossesUnderOrTouch, Arg1 = MetaFormulaItem.Create(condition.Arg1), Arg2 = MetaFormulaItem.Create(condition.Arg2) }); case Condition.GT: return(new TwoArgumentCondition() { ConditionType = ConditionType.Greater, Arg1 = MetaFormulaItem.Create(condition.Arg1), Arg2 = MetaFormulaItem.Create(condition.Arg2) }); case Condition.LT: return(new TwoArgumentCondition() { ConditionType = ConditionType.Lesser, Arg1 = MetaFormulaItem.Create(condition.Arg1), Arg2 = MetaFormulaItem.Create(condition.Arg2) }); case Condition.GTE: return(new TwoArgumentCondition() { ConditionType = ConditionType.GreaterOrEqual, Arg1 = MetaFormulaItem.Create(condition.Arg1), Arg2 = MetaFormulaItem.Create(condition.Arg2) }); case Condition.LTE: return(new TwoArgumentCondition() { ConditionType = ConditionType.LesserOrEqual, Arg1 = MetaFormulaItem.Create(condition.Arg1), Arg2 = MetaFormulaItem.Create(condition.Arg2) }); case Condition.EQ: return(new TwoArgumentCondition() { ConditionType = ConditionType.Equal, Arg1 = MetaFormulaItem.Create(condition.Arg1), Arg2 = MetaFormulaItem.Create(condition.Arg2) }); case Condition.NEQ: return(new TwoArgumentCondition() { ConditionType = ConditionType.NotEqual, Arg1 = MetaFormulaItem.Create(condition.Arg1), Arg2 = MetaFormulaItem.Create(condition.Arg2) }); default: throw new NotImplementedException(); } }
private IPrimitive FormatComparison(MetaFormulaItem arg1, MetaFormulaItem arg2, string v) { return(_factory.MakeConst(new ComparePrimitive(FormatStreamValueOrValue(arg1), v, FormatStreamValueOrValue(arg2)))); }
private static string FormatCrossMethodCall(MetaFormulaItem arg1, MetaFormulaItem arg2, string methodName) { return($"{methodName}({FormatStreamName(arg1)}, {FormatStreamName(arg2)})"); }
private static string FormatStreamName(MetaFormulaItem arg) { return(SourceFormatter.FormatSource(arg, "close")); }
private static string FormatComparison(MetaFormulaItem arg1, MetaFormulaItem arg2, string v) { return(FormatStreamName(arg1) + $" {v} " + FormatStreamName(arg2)); }