Skip to content

Commit

Permalink
Minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Marinovsky committed Jan 8, 2024
1 parent d08795a commit f764a62
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 73 deletions.
2 changes: 1 addition & 1 deletion Common/Python/BrokerageModelPythonWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ public IFillModel GetFillModel(Security security)
{
return csharpFillModel;
}
return (new FillModelPythonWrapper(fillModel));
return new FillModelPythonWrapper(fillModel);
}
}

Expand Down
38 changes: 28 additions & 10 deletions Tests/Common/Brokerages/BrokerageModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,9 @@ def GetFillModel(self, security):
var model = new BrokerageModelPythonWrapper(PyCustomBrokerageModel());
var fillModel = model.GetFillModel(security);
Assert.AreEqual(typeof(FillModelPythonWrapper), fillModel.GetType());
Assert.Throws<PythonException>(() => ((dynamic)fillModel).MarketFill(security, new Mock<MarketOrder>().Object));
var ex = Assert.Throws<PythonException>(() => ((dynamic)fillModel).MarketFill(security, new Mock<MarketOrder>().Object));
Assert.AreEqual("ValueError", ex.Type.Name);
Assert.AreEqual("Pepe", ex.Message);
}
}

Expand Down Expand Up @@ -305,7 +307,9 @@ def GetBenchmark(self, securities):
var model = new BrokerageModelPythonWrapper(PyCustomBrokerageModel());
var benchmarkModel = model.GetBenchmark(securityManager);
Assert.AreEqual(typeof(BenchmarkPythonWrapper), benchmarkModel.GetType());
Assert.Throws<PythonException>(() => ((dynamic)benchmarkModel).Evaluate(DateTime.Now));
var ex = Assert.Throws<PythonException>(() => ((dynamic)benchmarkModel).Evaluate(DateTime.Now));
Assert.AreEqual("ValueError", ex.Type.Name);
Assert.AreEqual("Pepe", ex.Message);
}
}

Expand Down Expand Up @@ -347,7 +351,7 @@ public void BrokerageModelPythonWrapperWorksWithCustomFeeModel()
from AlgorithmImports import *
class CustomFeeModel:
def GetOrderFee(self, parameters):
def GetOrderFee(self, security, order):
raise ValueError(""Pepe"")
class CustomBrokerageModel(DefaultBrokerageModel):
Expand All @@ -360,7 +364,9 @@ def GetFeeModel(self, securities):
Assert.AreEqual(typeof(FeeModelPythonWrapper), feeModel.GetType());
var order = new Mock<Order>();
var orderParameters = new Mock<OrderFeeParameters>(security, order.Object);
Assert.Throws<PythonException>(() => ((dynamic)feeModel).GetOrderFee(orderParameters.Object));
var ex = Assert.Throws<PythonException>(() => ((dynamic)feeModel).GetOrderFee(orderParameters.Object));
Assert.AreEqual("ValueError", ex.Type.Name);
Assert.AreEqual("Pepe", ex.Message);
}
}

Expand Down Expand Up @@ -416,9 +422,13 @@ def GetSettlementModel(self, securities):
algorithm.SetDateTime(DateTime.Now);
var portfolio = algorithm.Portfolio;
var appyFundsParameters = new ApplyFundsSettlementModelParameters(portfolio, security, DateTime.Now, new CashAmount(1000, Currencies.USD), null);
Assert.Throws<PythonException>(() => ((dynamic)settlementModel).ApplyFunds(appyFundsParameters));
var ex = Assert.Throws<PythonException>(() => ((dynamic)settlementModel).ApplyFunds(appyFundsParameters));
Assert.AreEqual("ValueError", ex.Type.Name);
Assert.AreEqual("Pepe", ex.Message);
var scanParameters = new ScanSettlementModelParameters(portfolio, security, DateTime.UtcNow);
Assert.Throws<PythonException>(() => ((dynamic)settlementModel).Scan(scanParameters));
ex = Assert.Throws<PythonException>(() => ((dynamic)settlementModel).Scan(scanParameters));
Assert.AreEqual("ValueError", ex.Type.Name);
Assert.AreEqual("Pepe2", ex.Message);
}
}

Expand Down Expand Up @@ -470,7 +480,9 @@ def GetSlippageModel(self, security):
var slippageModel = model.GetSlippageModel(security);
Assert.AreEqual(typeof(SlippageModelPythonWrapper), slippageModel.GetType());
var order = new Mock<Order>();
Assert.Throws<PythonException>(() => ((dynamic)slippageModel).GetSlippageApproximation(security, order.Object));
var ex = Assert.Throws<PythonException>(() => ((dynamic)slippageModel).GetSlippageApproximation(security, order.Object));
Assert.AreEqual("ValueError", ex.Type.Name);
Assert.AreEqual("Pepe", ex.Message);
}
}

Expand Down Expand Up @@ -517,7 +529,9 @@ def GetBuyingPowerModel(self, security):
var model = new BrokerageModelPythonWrapper(PyCustomBrokerageModel());
var buyingPowerModel = model.GetBuyingPowerModel(security);
Assert.AreEqual(typeof(BuyingPowerModelPythonWrapper), buyingPowerModel.GetType());
Assert.Throws<PythonException>(() => ((dynamic)buyingPowerModel).GetLeverage(security));
var ex = Assert.Throws<PythonException>(() => ((dynamic)buyingPowerModel).GetLeverage(security));
Assert.AreEqual("ValueError", ex.Type.Name);
Assert.AreEqual("Pepe", ex.Message);
}
}

Expand Down Expand Up @@ -563,7 +577,9 @@ def GetShortableProvider(self, security):
var model = new BrokerageModelPythonWrapper(PyCustomBrokerageModel());
var shortableProvider = model.GetShortableProvider(security);
Assert.AreEqual(typeof(ShortableProviderPythonWrapper), shortableProvider.GetType());
Assert.Throws<PythonException>(() => ((dynamic)shortableProvider).ShortableQuantity(security.Symbol, DateTime.Now));
var ex = Assert.Throws<PythonException>(() => ((dynamic)shortableProvider).ShortableQuantity(security.Symbol, DateTime.Now));
Assert.AreEqual("ValueError", ex.Type.Name);
Assert.AreEqual("Pepe", ex.Message);
}
}

Expand Down Expand Up @@ -610,7 +626,9 @@ def GetMarginInterestRateModel(self, security):
var marginInterestRateModel = model.GetMarginInterestRateModel(security);
Assert.AreEqual(typeof(MarginInterestRateModelPythonWrapper), marginInterestRateModel.GetType());
var parameters = new MarginInterestRateParameters(security, DateTime.Now);
Assert.Throws<PythonException>(() => ((dynamic)marginInterestRateModel).ApplyMarginInterestRate(parameters));
var ex = Assert.Throws<PythonException>(() => ((dynamic)marginInterestRateModel).ApplyMarginInterestRate(parameters));
Assert.AreEqual("ValueError", ex.Type.Name);
Assert.AreEqual("Pepe", ex.Message);
}
}

Expand Down
126 changes: 64 additions & 62 deletions Tests/Python/PythonWrapperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,74 +64,76 @@ public void DoesNotThrowWhenDerivedFromCSharpModel()
public void SettlementModelPythonWrapperWorks()
{
var results = AlgorithmRunner.RunLocalBacktest("CustomSettlementModelRegressionAlgorithm",
new Dictionary<string, string>()
{
{"Total Trades", "0"},
{"Average Win", "0%"},
{"Average Loss", "0%"},
{"Compounding Annual Return", "108.257%"},
{"Drawdown", "0%"},
{"Expectancy", "0"},
{"Net Profit", "1.010%"},
{"Sharpe Ratio", "10.983"},
{"Sortino Ratio", "0"},
{"Probabilistic Sharpe Ratio", "95.977%"},
{"Loss Rate", "0%"},
{"Win Rate", "0%"},
{"Profit-Loss Ratio", "0"},
{"Alpha", "1.42"},
{"Beta", "-0.273"},
{"Annual Standard Deviation", "0.08"},
{"Annual Variance", "0.006"},
{"Information Ratio", "-3.801"},
{"Tracking Error", "0.288"},
{"Treynor Ratio", "-3.226"},
{"Total Fees", "$0.00"},
{"Estimated Strategy Capacity", "$0"},
{"Lowest Capacity Asset", ""},
{"Portfolio Turnover", "0%"},
{"OrderListHash", "d41d8cd98f00b204e9800998ecf8427e"}
},
Language.Python,
AlgorithmStatus.Completed,
algorithmLocation: "../../../Algorithm.Python/CustomSettlementModelRegressionAlgorithm.py");
new Dictionary<string, string>()
{
{"Total Trades", "0"},
{"Average Win", "0%"},
{"Average Loss", "0%"},
{"Compounding Annual Return", "108.257%"},
{"Drawdown", "0%"},
{"Expectancy", "0"},
{"Net Profit", "1.010%"},
{"Sharpe Ratio", "10.983"},
{"Sortino Ratio", "0"},
{"Probabilistic Sharpe Ratio", "95.977%"},
{"Loss Rate", "0%"},
{"Win Rate", "0%"},
{"Profit-Loss Ratio", "0"},
{"Alpha", "1.42"},
{"Beta", "-0.273"},
{"Annual Standard Deviation", "0.08"},
{"Annual Variance", "0.006"},
{"Information Ratio", "-3.801"},
{"Tracking Error", "0.288"},
{"Treynor Ratio", "-3.226"},
{"Total Fees", "$0.00"},
{"Estimated Strategy Capacity", "$0"},
{"Lowest Capacity Asset", ""},
{"Portfolio Turnover", "0%"},
{"OrderListHash", "d41d8cd98f00b204e9800998ecf8427e"}
},
Language.Python,
AlgorithmStatus.Completed,
algorithmLocation: "../../../Algorithm.Python/CustomSettlementModelRegressionAlgorithm.py"
);
}

[Test]
public void BenchmarkModelPythonWrapperWorks()
{
var results = AlgorithmRunner.RunLocalBacktest("CustomBenchmarkRegressionAlgorithm",
new Dictionary<string, string>()
{
{"Total Trades", "0"},
{"Average Win", "0%"},
{"Average Loss", "0%"},
{"Compounding Annual Return", "0%"},
{"Drawdown", "0%"},
{"Expectancy", "0"},
{"Net Profit", "0%"},
{"Sharpe Ratio", "0"},
{"Sortino Ratio", "0"},
{"Probabilistic Sharpe Ratio", "0%"},
{"Loss Rate", "0%"},
{"Win Rate", "0%"},
{"Profit-Loss Ratio", "0"},
{"Alpha", "0"},
{"Beta", "0"},
{"Annual Standard Deviation", "0"},
{"Annual Variance", "0"},
{"Information Ratio", "-1.9190768915765233E+23"},
{"Tracking Error", "13.748"},
{"Treynor Ratio", "0"},
{"Total Fees", "$0.00"},
{"Estimated Strategy Capacity", "$0"},
{"Lowest Capacity Asset", ""},
{"Portfolio Turnover", "0%"},
{"OrderListHash", "d41d8cd98f00b204e9800998ecf8427e"}
},
Language.Python,
AlgorithmStatus.Completed,
algorithmLocation: "../../../Algorithm.Python/CustomBenchmarkRegressionAlgorithm.py");
new Dictionary<string, string>()
{
{"Total Trades", "0"},
{"Average Win", "0%"},
{"Average Loss", "0%"},
{"Compounding Annual Return", "0%"},
{"Drawdown", "0%"},
{"Expectancy", "0"},
{"Net Profit", "0%"},
{"Sharpe Ratio", "0"},
{"Sortino Ratio", "0"},
{"Probabilistic Sharpe Ratio", "0%"},
{"Loss Rate", "0%"},
{"Win Rate", "0%"},
{"Profit-Loss Ratio", "0"},
{"Alpha", "0"},
{"Beta", "0"},
{"Annual Standard Deviation", "0"},
{"Annual Variance", "0"},
{"Information Ratio", "-1.9190768915765233E+23"},
{"Tracking Error", "13.748"},
{"Treynor Ratio", "0"},
{"Total Fees", "$0.00"},
{"Estimated Strategy Capacity", "$0"},
{"Lowest Capacity Asset", ""},
{"Portfolio Turnover", "0%"},
{"OrderListHash", "d41d8cd98f00b204e9800998ecf8427e"}
},
Language.Python,
AlgorithmStatus.Completed,
algorithmLocation: "../../../Algorithm.Python/CustomBenchmarkRegressionAlgorithm.py"
);
}

private const string FullyImplemented =
Expand Down

0 comments on commit f764a62

Please sign in to comment.