public void TestaFormulasDiretamente() { facade = new FacadeBacktester(); FormulaManager fm = facade.formulaManager; facade.LoadAtivo("PETR4", 100, Consts.PERIODO_ACAO.DIARIO, "dados/petr4-diario.js"); Ativo ativo = facade.GetAtivo("PETR4"); Formula fTeste = new Formula(facade, "TESTE"); Formula fTeste2 = new Formula(facade, "TESTE2"); Formula f = new FormulaPercentil(facade, "PERCENTIL", fTeste); Candle candle = ativo.firstCandle; candle.SetValor("TESTE", candle.GetValor(FormulaManager.LOW)); Assert.IsTrue(f.Calc(candle) == 0, f.Calc(candle) + " <> " + 0); candle.SetValor("TESTE", candle.GetValor(FormulaManager.HIGH)); Assert.IsTrue(f.Calc(candle) == 1, f.Calc(candle) + " <> " + 1); f = new FormulaMultiply(facade, "MULT", fTeste, 10); candle.SetValor(fTeste, 10); Assert.IsTrue(f.Calc(candle) == 100, f.Calc(candle) + " <> " + 100); candle.SetValor(fTeste, 5); Assert.IsTrue(f.Calc(candle) == 50, f.Calc(candle) + " <> " + 50); f = new FormulaSUM(facade, "MULT", fTeste, fTeste2); candle.SetValor(fTeste2, 10); Assert.IsTrue(f.Calc(candle) == 15, f.Calc(candle) + " <> " + 15); //HV e LV Candle candle2 = candle.proximoCandle; Candle candle3 = candle2.proximoCandle; Candle candle4 = candle3.proximoCandle; Formula fHV = new FormulaHV(facade, "HV", fm.GetFormula(FormulaManager.HIGH), fTeste2); Formula fLV = new FormulaLV(facade, "LV", fm.GetFormula(FormulaManager.HIGH), fTeste2); candle4.SetValor(fTeste2, 3); float max = candle4.GetValor(fm.GetFormula(FormulaManager.HIGH)); float low = candle4.GetValor(fm.GetFormula(FormulaManager.HIGH)); Candle c = candle4; for (int i = 0; i < 3; i++) { float value = c.GetValor(fm.GetFormula(FormulaManager.HIGH)); if (value > max) { max = value; } if (value < low) { low = value; } c = c.candleAnterior; } Assert.IsTrue(max == fHV.Calc(candle4), max + "<>" + fHV.Calc(candle4)); Assert.IsTrue(low == fLV.Calc(candle4), max + "<>" + fLV.Calc(candle4)); //REF f = new FormulaREF(facade, "REF", fTeste, fTeste2); candle4.SetValor(fTeste, 10); candle3.SetValor(fTeste, 20); candle2.SetValor(fTeste, 30); candle4.SetValor(fTeste2, 1); Assert.IsTrue(20 == f.Calc(candle4)); candle4.SetValor(fTeste2, 2); Assert.IsTrue(30 == f.Calc(candle4)); }
private Formula CreateFormula(string name, string par) { if (formulasInst.ContainsKey(GetCode(name, par))) { return(formulasInst[GetCode(name, par)]); } string[] pars = Utils.SplitParameters(par); Formula f = null; if (Utils.IsNumber(name)) { f = new FormulaNumber(facade, name); } //Esses operadores aceitam tanto fórmula quanto número no segundo parametro if (Utils.IsNumber(pars[1])) { if (name == SUBTRACT) { f = new FormulaSUBTRACT(facade, name, GetFormula(pars[0]), float.Parse(pars[1])); } if (name == MULTIPLY) { f = new FormulaMultiply(facade, name, GetFormula(pars[0]), float.Parse(pars[1])); } if (name == DIVIDE) { f = new FormulaDivide(facade, name, GetFormula(pars[0]), float.Parse(pars[1])); } if (name == SUM) { f = new FormulaSUM(facade, name, GetFormula(pars[0]), float.Parse(pars[1])); } if (name == ABS) { f = new FormulaABS(facade, name, GetFormula(pars[0]), float.Parse(pars[1])); } if (name == DIF) { f = new FormulaDif(facade, name, GetFormula(pars[0]), float.Parse(pars[1])); } if (name == INVERT_SIGNAL) { f = new FormulaInvertSignal(facade, name, GetFormula(pars[0]), float.Parse(pars[1])); } } else { if (name == SUBTRACT) { f = new FormulaSUBTRACT(facade, name, GetFormula(pars[0]), GetFormula(pars[1])); } if (name == MULTIPLY) { f = new FormulaMultiply(facade, name, GetFormula(pars[0]), GetFormula(pars[1])); } if (name == DIVIDE) { f = new FormulaDivide(facade, name, GetFormula(pars[0]), GetFormula(pars[1])); } if (name == SUM) { f = new FormulaSUM(facade, name, GetFormula(pars[0]), GetFormula(pars[1])); } if (name == ABS) { f = new FormulaABS(facade, name, GetFormula(pars[0]), GetFormula(pars[1])); } if (name == DIF) { f = new FormulaDif(facade, name, GetFormula(pars[0]), GetFormula(pars[1])); } if (name == INVERT_SIGNAL) { f = new FormulaInvertSignal(facade, name, GetFormula(pars[0]), GetFormula(pars[1])); } } if (f == null) { f = CreateLogicalFormulas(name, pars); } if (f == null) { f = CreateBooleanFormulas(name, pars); } if (name == STOCH) { f = new FormulaStoch(facade, name, GetFormula(pars[0]), GetFormula(pars[1])); } if (name == PERCENTIL) { f = new FormulaPercentil(facade, name, GetFormula(pars[0])); } if (name == PERC_VAR) { f = new FormulaPercVariation(facade, name, GetFormula(pars[0]), GetFormula(pars[1])); } //if (name == TICK) f = new FormulaTick(facade, name); // if (name == DAYOFWEEK) f = new FormulaDayOfWeek(facade, name); if (name == REF) { f = new FormulaREF(facade, name, GetFormula(pars[0]), GetFormula(pars[1])); } if (name == IFR) { f = new FormulaRSI(facade, name, GetFormula(pars[0]), GetFormula(pars[1])); } if (name == HV) { f = new FormulaHV(facade, name, GetFormula(pars[0]), GetFormula(pars[1])); } if (name == TRIX) { f = new FormulaTRIX(facade, name, GetFormula(pars[0]), GetFormula(pars[1])); } if (name == LV) { f = new FormulaLV(facade, name, GetFormula(pars[0]), GetFormula(pars[1])); } if (name == HILO) { f = new FormulaHilo(facade, name, GetFormula(pars[0]), GetFormula(pars[1]), GetFormula(pars[2]), GetFormula(pars[3])); } if (name == AVGGAIN) { f = new FormulaAvgGain(facade, name, GetFormula(pars[0]), GetFormula(pars[1])); } if (name == AVGLOSS) { f = new FormulaAvgLoss(facade, name, GetFormula(pars[0]), GetFormula(pars[1])); } if (name == STDDEV) { f = new FormulaStdDev(facade, name, GetFormula(pars[0]), GetFormula(pars[1])); } if (name == UPPERBB) { f = new FormulaBB(facade, UPPERBB, GetFormula(pars[0]), "U", GetFormula(pars[1]), GetFormula(pars[2])); } if (name == MIDDLEBB) { f = new FormulaBB(facade, MIDDLEBB, GetFormula(pars[0]), "M", GetFormula(pars[1]), GetFormula(pars[2])); } if (name == LOWERBB) { f = new FormulaBB(facade, LOWERBB, GetFormula(pars[0]), "L", GetFormula(pars[1]), GetFormula(pars[2])); } // if (name == BB) f = new FormulaBB(facade, BB, GetFormula(pars[0]), pars[1], int.Parse(pars[2]), float.Parse(pars[3])); if (name == MOD) { f = new FormulaMod(facade, MOD, GetFormula(pars[0]), GetFormula(pars[1])); } if (name == MME) { f = new FormulaMME(facade, name, GetFormula(pars[0]), GetFormula(pars[1])); } if (name == MMS) { f = new FormulaMMS(facade, name, GetFormula(pars[0]), GetFormula(pars[1])); } if (f == null) { f = new Formula(facade, name); } string code = GetCode(name, par); formulasInst.Add(code, f); formulasCache.Add(code); return(f); }