public void TestDivByZero() { // With the value changing, the indicator should return a value that is not the default 0m. var sr = new SharpeRatio("SR", 10); // push the value 100000 into the indicator 20 times (sharpeRatioPeriod + movingAveragePeriod) for (int i = 0; i < 20; i++) { IndicatorDataPoint point = new IndicatorDataPoint(new DateTime(), 0); sr.Update(point); } Assert.AreEqual(sr.Current.Value, 0m); }
/// <summary> /// Sets the additional stats in Money. /// </summary> private static void SetAdditionalMoneyStats() { string unit = " " + Configs.AccountCurrency; AdditionalStatsParamName = new[] { Language.T("Initial account"), Language.T("Account balance"), Language.T("Net profit"), Language.T("Net profit") + " %", Language.T("Gross profit"), Language.T("Gross loss"), Language.T("Profit factor"), Language.T("Annualized profit"), Language.T("Annualized profit") + " %", Language.T("Minimum account"), Language.T("Minimum account date"), Language.T("Maximum account"), Language.T("Maximum account date"), Language.T("Absolute drawdown"), Language.T("Maximum drawdown"), Language.T("Maximum drawdown") + " %", Language.T("Maximum drawdown date"), Language.T("Historical bars"), Language.T("Tested bars"), Language.T("Bars with trades"), Language.T("Bars with trades") + " %", Language.T("Number of trades"), Language.T("Winning trades"), Language.T("Losing trades"), Language.T("Win/loss ratio"), Language.T("Maximum profit"), Language.T("Average profit"), Language.T("Maximum loss"), Language.T("Average loss"), Language.T("Expected payoff"), Language.T("Average holding period returns"), Language.T("Geometric holding period returns"), Language.T("Sharpe ratio") }; int totalWinTrades = winningLongTrades + winningShortTrades; int totalLossTrades = losingLongTrades + losingShortTrades; int trades = totalWinTrades + totalLossTrades; AdditionalStatsValueTotal = new[] { Configs.InitialAccount.ToString("F2") + unit, NetMoneyBalance.ToString("F2") + unit, (NetMoneyBalance - Configs.InitialAccount).ToString("F2") + unit, (100 * ((NetMoneyBalance - Configs.InitialAccount) / Configs.InitialAccount)).ToString("F2") + "%", GrossMoneyProfit.ToString("F2") + unit, GrossMoneyLoss.ToString("F2") + unit, ProfitFactor.ToString("F2"), AnnualizedProfit.ToString("F2") + unit, AnnualizedProfitPercent.ToString("F2") + "%", MinMoneyBalance.ToString("F2") + unit, minMoneyBalanceDate.ToShortDateString(), MaxMoneyBalance.ToString("F2") + unit, maxMoneyBalanceDate.ToShortDateString(), (Configs.InitialAccount - MinMoneyBalance).ToString("F2") + unit, MaxMoneyDrawdown.ToString("F2") + unit, maxMoneyDrawdownPercent.ToString("F2") + "%", maxMoneyDrawdownDate.ToShortDateString(), Bars.ToString(CultureInfo.InvariantCulture), (Bars - FirstBar).ToString(CultureInfo.InvariantCulture), barsWithPos.ToString(CultureInfo.InvariantCulture), (100f * barsWithPos / (Bars - FirstBar)).ToString("F2") + "%", trades.ToString(CultureInfo.InvariantCulture), totalWinTrades.ToString(CultureInfo.InvariantCulture), totalLossTrades.ToString(CultureInfo.InvariantCulture), (1f * totalWinTrades / (totalWinTrades + totalLossTrades)).ToString("F2"), Math.Max(maxLongMoneyWin, maxShortMoneyWin).ToString("F2") + unit, (GrossMoneyProfit / totalWinTrades).ToString("F2") + unit, Math.Min(maxLongMoneyLoss, maxShortMoneyLoss).ToString("F2") + unit, (GrossMoneyLoss / totalLossTrades).ToString("F2") + unit, (1f * (NetMoneyBalance - Configs.InitialAccount) / trades).ToString("F2") + unit, AvrgHoldingPeriodRet.ToString("F2") + "%", GeomHoldingPeriodRet.ToString("F2") + "%", SharpeRatio.ToString("F2") }; AdditionalStatsValueLong = new[] { Configs.InitialAccount.ToString("F2") + unit, NetLongMoneyBalance.ToString("F2") + unit, (NetLongMoneyBalance - Configs.InitialAccount).ToString("F2") + unit, (100 * ((NetLongMoneyBalance - Configs.InitialAccount) / Configs.InitialAccount)).ToString("F2") + "%", grossLongMoneyProfit.ToString("F2") + unit, grossLongMoneyLoss.ToString("F2") + unit, (Math.Abs(grossLongMoneyLoss - 0) < sigma ? grossLongMoneyProfit : Math.Abs(grossLongMoneyProfit / grossLongMoneyLoss)).ToString("F2"), ((365f / Time[Bars - 1].Subtract(Time[0]).Days) * (NetLongMoneyBalance - Configs.InitialAccount)).ToString("F2") + unit, (100 * (365f / Time[Bars - 1].Subtract(Time[0]).Days) * (NetLongMoneyBalance - Configs.InitialAccount) / Configs.InitialAccount).ToString("F2") + "%", MinLongMoneyBalance.ToString("F2") + unit, minLongMoneyBalanceDate.ToShortDateString(), MaxLongMoneyBalance.ToString("F2") + unit, maxLongMoneyBalanceDate.ToShortDateString(), (Configs.InitialAccount - MinLongMoneyBalance).ToString("F2") + unit, maxLongMoneyDrawdown.ToString("F2") + unit, maxLongMoneyDrawdownPercent.ToString("F2") + "%", maxLongMoneyDrawdownDate.ToShortDateString(), Bars.ToString(CultureInfo.InvariantCulture), (Bars - FirstBar).ToString(CultureInfo.InvariantCulture), barsWithLongPos.ToString(CultureInfo.InvariantCulture), (100f * barsWithLongPos / (Bars - FirstBar)).ToString("F2") + "%", totalLongTrades.ToString(CultureInfo.InvariantCulture), winningLongTrades.ToString(CultureInfo.InvariantCulture), losingLongTrades.ToString(CultureInfo.InvariantCulture), (1f * winningLongTrades / (winningLongTrades + losingLongTrades)).ToString("F2"), maxLongMoneyWin.ToString("F2") + unit, (grossLongMoneyProfit / winningLongTrades).ToString("F2") + unit, maxLongMoneyLoss.ToString("F2") + unit, (grossLongMoneyLoss / losingLongTrades).ToString("F2") + unit, (1f * (NetLongMoneyBalance - Configs.InitialAccount) / (winningLongTrades + losingLongTrades)).ToString("F2") + unit, ahprLong.ToString("F2") + "%", ghprLong.ToString("F2") + "%", sharpeRatioLong.ToString("F2") }; AdditionalStatsValueShort = new[] { Configs.InitialAccount.ToString("F2") + unit, NetShortMoneyBalance.ToString("F2") + unit, (NetShortMoneyBalance - Configs.InitialAccount).ToString("F2") + unit, (100 * ((NetShortMoneyBalance - Configs.InitialAccount) / Configs.InitialAccount)).ToString("F2") + "%", grossShortMoneyProfit.ToString("F2") + unit, grossShortMoneyLoss.ToString("F2") + unit, (Math.Abs(grossShortMoneyLoss - 0) < sigma ? grossShortMoneyProfit : Math.Abs(grossShortMoneyProfit / grossShortMoneyLoss)).ToString("F2"), ((365f / Time[Bars - 1].Subtract(Time[0]).Days) * (NetShortMoneyBalance - Configs.InitialAccount)).ToString("F2") + unit, (100 * (365f / Time[Bars - 1].Subtract(Time[0]).Days) * (NetShortMoneyBalance - Configs.InitialAccount) / Configs.InitialAccount).ToString("F2") + "%", MinShortMoneyBalance.ToString("F2") + unit, minShortMoneyBalanceDate.ToShortDateString(), MaxShortMoneyBalance.ToString("F2") + unit, maxShortMoneyBalanceDate.ToShortDateString(), (Configs.InitialAccount - MinShortMoneyBalance).ToString("F2") + unit, maxShortMoneyDrawdown.ToString("F2") + unit, maxShortMoneyDrawdownPercent.ToString("F2") + "%", maxShortMoneyDrawdownDate.ToShortDateString(), Bars.ToString(CultureInfo.InvariantCulture), (Bars - FirstBar).ToString(CultureInfo.InvariantCulture), barsWithShortPos.ToString(CultureInfo.InvariantCulture), (100f * barsWithShortPos / (Bars - FirstBar)).ToString("F2") + "%", totalShortTrades.ToString(CultureInfo.InvariantCulture), winningShortTrades.ToString(CultureInfo.InvariantCulture), losingShortTrades.ToString(CultureInfo.InvariantCulture), (1f * winningShortTrades / (winningShortTrades + losingShortTrades)).ToString("F2"), maxShortMoneyWin.ToString("F2") + unit, (grossShortMoneyProfit / winningShortTrades).ToString("F2") + unit, maxShortMoneyLoss.ToString("F2") + unit, (grossShortMoneyLoss / losingShortTrades).ToString("F2") + unit, (1f * (NetShortMoneyBalance - Configs.InitialAccount) / (winningShortTrades + losingShortTrades)) .ToString("F2") + unit, ahprShort.ToString("F2") + "%", ghprShort.ToString("F2") + "%", sharpeRatioShort.ToString("F2") }; }
protected override void Create() { m_SharpeRatio = new SharpeRatio(this); }
/// <summary> /// Generate the Account Statistics in points. /// </summary> private static void GenerateAccountStats() { AccountStatsParam = new[] { Language.T("Intrabar scanning"), Language.T("Interpolation method"), Language.T("Ambiguous bars"), Language.T("Profit per day"), Language.T("Sharpe ratio"), Language.T("Max consecutive losses"), Language.T("Tested bars"), Language.T("Account balance"), Language.T("Minimum account"), Language.T("Maximum account"), Language.T("Maximum drawdown"), Language.T("Max equity drawdown"), Language.T("Max equity drawdown"), Language.T("Gross profit"), Language.T("Gross loss"), Language.T("Sent orders"), Language.T("Executed orders"), Language.T("Traded lots"), Language.T("Winning trades"), Language.T("Losing trades"), Language.T("Win/loss ratio"), Language.T("Time in position"), Language.T("Charged spread"), Language.T("Charged rollover"), Language.T("Charged commission"), Language.T("Charged slippage"), Language.T("Total charges"), Language.T("Balance without charges") }; string unit = " " + Language.T("points"); AccountStatsValue = new string[AccountStatsParam.Length]; int i = 0; AccountStatsValue[i++] = IsScanPerformed ? Language.T("Accomplished") : Language.T("Not accomplished"); AccountStatsValue[i++] = InterpolationMethodShortToString(); AccountStatsValue[i++] = AmbiguousBars.ToString(CultureInfo.InvariantCulture); AccountStatsValue[i++] = ProfitPerDay + unit; AccountStatsValue[i++] = SharpeRatio.ToString("F2"); AccountStatsValue[i++] = MaxConsecutiveLosses.ToString(CultureInfo.InvariantCulture); AccountStatsValue[i++] = (Bars - FirstBar).ToString(CultureInfo.InvariantCulture); AccountStatsValue[i++] = NetBalance + unit; AccountStatsValue[i++] = MinBalance + unit; AccountStatsValue[i++] = MaxBalance + unit; AccountStatsValue[i++] = MaxDrawdown + unit; AccountStatsValue[i++] = MaxEquityDrawdown + unit; AccountStatsValue[i++] = EquityPercentDrawdown.ToString("F2") + " %"; AccountStatsValue[i++] = GrossProfit + unit; AccountStatsValue[i++] = GrossLoss + unit; AccountStatsValue[i++] = SentOrders.ToString(CultureInfo.InvariantCulture); AccountStatsValue[i++] = ExecutedOrders.ToString(CultureInfo.InvariantCulture); AccountStatsValue[i++] = TradedLots.ToString("F2"); AccountStatsValue[i++] = WinningTrades.ToString(CultureInfo.InvariantCulture); AccountStatsValue[i++] = LosingTrades.ToString(CultureInfo.InvariantCulture); AccountStatsValue[i++] = ((float)WinningTrades / (WinningTrades + LosingTrades)).ToString("F2"); AccountStatsValue[i++] = TimeInPosition + " %"; AccountStatsValue[i++] = Math.Round(TotalChargedSpread) + unit; AccountStatsValue[i++] = Math.Round(TotalChargedRollOver) + unit; AccountStatsValue[i++] = Math.Round(TotalChargedCommission) + unit; AccountStatsValue[i++] = TotalChargedSlippage.ToString("F2") + unit; AccountStatsValue[i++] = Math.Round(TotalChargedSpread + TotalChargedRollOver + TotalChargedSlippage) + unit; AccountStatsValue[i++] = Math.Round(NetBalance + TotalChargedSpread + TotalChargedRollOver + TotalChargedSlippage) + unit; AccountStatsFlags = new bool[AccountStatsParam.Length]; AccountStatsFlags[0] = AmbiguousBars > 0 && !IsScanPerformed; AccountStatsFlags[1] = InterpolationMethod != InterpolationMethod.Pessimistic; AccountStatsFlags[2] = AmbiguousBars > 0; AccountStatsFlags[5] = MaxConsecutiveLosses > 6; AccountStatsFlags[7] = NetBalance < 0; AccountStatsFlags[10] = MaxDrawdown > 500; }
/// <summary> /// Generate the Account Statistics in currency. /// </summary> private static void GenerateAccountStatsInMoney() { AccountStatsParam = new[] { Language.T("Intrabar scanning"), Language.T("Interpolation method"), Language.T("Ambiguous bars"), Language.T("Profit per day"), Language.T("Sharpe ratio"), Language.T("Max consecutive losses"), Language.T("Tested bars"), Language.T("Initial account"), Language.T("Account balance"), Language.T("Minimum account"), Language.T("Maximum account"), Language.T("Maximum drawdown"), Language.T("Max equity drawdown"), Language.T("Max equity drawdown"), Language.T("Gross profit"), Language.T("Gross loss"), Language.T("Sent orders"), Language.T("Executed orders"), Language.T("Traded lots"), Language.T("Winning trades"), Language.T("Losing trades"), Language.T("Win/loss ratio"), Language.T("Time in position"), Language.T("Charged spread"), Language.T("Charged rollover"), Language.T("Charged commission"), Language.T("Charged slippage"), Language.T("Total charges"), Language.T("Balance without charges"), Language.T("Account exchange rate") }; string unit = " " + Configs.AccountCurrency; AccountStatsValue = new string[AccountStatsParam.Length]; int i = 0; AccountStatsValue[i++] = IsScanPerformed ? Language.T("Accomplished") : Language.T("Not accomplished"); AccountStatsValue[i++] = InterpolationMethodShortToString(); AccountStatsValue[i++] = AmbiguousBars.ToString(CultureInfo.InvariantCulture); AccountStatsValue[i++] = MoneyProfitPerDay.ToString("F2") + unit; AccountStatsValue[i++] = SharpeRatio.ToString("F2"); AccountStatsValue[i++] = MaxConsecutiveLosses.ToString(CultureInfo.InvariantCulture); AccountStatsValue[i++] = (Bars - FirstBar).ToString(CultureInfo.InvariantCulture); AccountStatsValue[i++] = Configs.InitialAccount.ToString("F2") + unit; AccountStatsValue[i++] = NetMoneyBalance.ToString("F2") + unit; AccountStatsValue[i++] = MinMoneyBalance.ToString("F2") + unit; AccountStatsValue[i++] = MaxMoneyBalance.ToString("F2") + unit; AccountStatsValue[i++] = MaxMoneyDrawdown.ToString("F2") + unit; AccountStatsValue[i++] = MaxMoneyEquityDrawdown.ToString("F2") + unit; AccountStatsValue[i++] = MoneyEquityPercentDrawdown.ToString("F2") + " %"; AccountStatsValue[i++] = GrossMoneyProfit.ToString("F2") + unit; AccountStatsValue[i++] = GrossMoneyLoss.ToString("F2") + unit; AccountStatsValue[i++] = SentOrders.ToString(CultureInfo.InvariantCulture); AccountStatsValue[i++] = ExecutedOrders.ToString(CultureInfo.InvariantCulture); AccountStatsValue[i++] = TradedLots.ToString("F2"); AccountStatsValue[i++] = WinningTrades.ToString(CultureInfo.InvariantCulture); AccountStatsValue[i++] = LosingTrades.ToString(CultureInfo.InvariantCulture); AccountStatsValue[i++] = WinLossRatio.ToString("F2"); AccountStatsValue[i++] = TimeInPosition + " %"; AccountStatsValue[i++] = TotalChargedMoneySpread.ToString("F2") + unit; AccountStatsValue[i++] = TotalChargedMoneyRollOver.ToString("F2") + unit; AccountStatsValue[i++] = TotalChargedMoneyCommission.ToString("F2") + unit; AccountStatsValue[i++] = TotalChargedMoneySlippage.ToString("F2") + unit; AccountStatsValue[i++] = (TotalChargedMoneySpread + TotalChargedMoneyRollOver + TotalChargedMoneyCommission + TotalChargedMoneySlippage).ToString("F2") + unit; AccountStatsValue[i++] = (NetMoneyBalance + TotalChargedMoneySpread + TotalChargedMoneyRollOver + TotalChargedMoneyCommission + TotalChargedMoneySlippage).ToString("F2") + unit; if (InstrProperties.PriceIn == Configs.AccountCurrency) { AccountStatsValue[i++] = Language.T("Not used"); } else if (InstrProperties.InstrType == InstrumetType.Forex && Symbol.StartsWith(Configs.AccountCurrency)) { AccountStatsValue[i++] = Language.T("Deal price"); } else if (Configs.AccountCurrency == "USD") { AccountStatsValue[i++] = InstrProperties.RateToUSD.ToString("F4"); } else if (Configs.AccountCurrency == "EUR") { AccountStatsValue[i++] = InstrProperties.RateToEUR.ToString("F4"); } AccountStatsFlags = new bool[AccountStatsParam.Length]; AccountStatsFlags[0] = AmbiguousBars > 0 && !IsScanPerformed; AccountStatsFlags[1] = InterpolationMethod != InterpolationMethod.Pessimistic; AccountStatsFlags[2] = AmbiguousBars > 0; AccountStatsFlags[5] = MaxConsecutiveLosses > 6; AccountStatsFlags[8] = NetMoneyBalance < Configs.InitialAccount; AccountStatsFlags[11] = MaxDrawdown > Configs.InitialAccount / 2; }