Skip to content

Commit

Permalink
Improve message for python algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
Marinovsky committed Dec 24, 2024
1 parent 12930c4 commit 83d1b09
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 21 deletions.
21 changes: 18 additions & 3 deletions Common/Messages/Messages.Orders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ namespace QuantConnect
/// </summary>
public static partial class Messages
{
private readonly static bool _languageIsCSharp = Configuration.Config.GetValue<Language>("algorithm-language") == Language.CSharp;

/// <summary>
/// Provides user-facing messages for the <see cref="Orders.CancelOrderRequest"/> class and its consumers or related classes
/// </summary>
Expand Down Expand Up @@ -354,7 +356,14 @@ public static string ZeroQuantity(Orders.OrderRequest request)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string MissingSecurity(Orders.SubmitOrderRequest request)
{
return Invariant($"You haven't requested {request.Symbol} data. Add this with AddSecurity() in the Initialize() Method.");
if (_languageIsCSharp)
{
return Invariant($"You haven't requested {request.Symbol} data. Add this with AddSecurity() in the Initialize() Method.");
}
else
{
return Invariant($"You haven't requested {request.Symbol} data. Add this with add_security() in the initialize() Method.");
}
}

/// <summary>
Expand All @@ -365,8 +374,14 @@ public static string MissingSecurity(Orders.SubmitOrderRequest request)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string WarmingUp(Orders.OrderRequest request)
{
return Invariant($@"This operation is not allowed in Initialize or during warm up: OrderRequest.{
request.OrderRequestType}. Please move this code to the OnWarmupFinished() method.");
if (_languageIsCSharp)
{
return Invariant($@"This operation is not allowed in Initialize or during warm up: OrderRequest.{request.OrderRequestType}. Please move this code to the OnWarmupFinished() method.");
}
else
{
return Invariant($@"This operation is not allowed in initialize or during warm up: OrderRequest.{request.OrderRequestType}. Please move this code to the on_warmup_finished() method.");
}
}
}

Expand Down
42 changes: 24 additions & 18 deletions Common/Messages/Messages.Securities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,9 @@ public static string TargetOrderMarginNotAboveMinimum(decimal absDifferenceOfMar
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string TargetOrderMarginNotAboveMinimum()
{
var settingsMinimumOrderMarginPortfolioPercentage = _languageIsCSharp ? "Settings.MinimumOrderMarginPortfolioPercentage" : "settings.minimum_order_margin_portfolio_percentage";
return "Warning: Portfolio rebalance result ignored as it resulted in a single share trade recommendation which can generate high fees." +
" To disable minimum order size checks please set Settings.MinimumOrderMarginPortfolioPercentage = 0.";
$" To disable minimum order size checks please set {settingsMinimumOrderMarginPortfolioPercentage} = 0.";
}

/// <summary>
Expand All @@ -132,7 +133,8 @@ public static string OrderQuantityLessThanLotSize(Securities.Security security,
public static string FailedToConvergeOnTheTargetMargin(GetMaximumOrderQuantityForTargetBuyingPowerParameters parameters,
decimal signedTargetFinalMarginValue, decimal orderFees)
{
return Invariant($@"GetMaximumOrderQuantityForTargetBuyingPower failed to converge on the target margin: {
var getMaximumOrderQuantityForTargetBuyingPower = _languageIsCSharp ? "GetMaximumOrderQuantityForTargetBuyingPower" : "get_maximum_order_quantity_for_target_buying_power";
return Invariant($@"{getMaximumOrderQuantityForTargetBuyingPower} failed to converge on the target margin: {
signedTargetFinalMarginValue}; the following information can be used to reproduce the issue. Total Portfolio Cash: {
parameters.Portfolio.Cash}; Security : {parameters.Security.Symbol.ID}; Price : {parameters.Security.Close}; Leverage: {
parameters.Security.Leverage}; Order Fee: {orderFees}; Lot Size: {
Expand Down Expand Up @@ -440,7 +442,8 @@ public static string OrderQuantityLessThanLotSizeOrderDetails(decimal targetOrde
public static string FailedToConvergeOnTargetOrderValue(decimal targetOrderValue, decimal currentOrderValue, decimal orderQuantity,
decimal orderFees, Securities.Security security)
{
return Invariant($@"GetMaximumOrderQuantityForTargetBuyingPower failed to converge to target order value {
var getMaximumOrderQuantityForTargetBuyingPower = _languageIsCSharp ? "GetMaximumOrderQuantityForTargetBuyingPower" : "get_maximum_order_quantity_for_target_buying_power";
return Invariant($@"{getMaximumOrderQuantityForTargetBuyingPower} failed to converge to target order value {
targetOrderValue}. Current order value is {currentOrderValue}. Order quantity {orderQuantity}. Lot size is {
security.SymbolProperties.LotSize}. Order fees {orderFees}. Security symbol {security.Symbol}");
}
Expand Down Expand Up @@ -708,12 +711,12 @@ public static class Security
/// <summary>
/// String message saying: Security requires a valid SymbolProperties instance
/// </summary>
public static string ValidSymbolPropertiesInstanceRequired = "Security requires a valid SymbolProperties instance.";
public static string ValidSymbolPropertiesInstanceRequired = _languageIsCSharp ? "Security requires a valid SymbolProperties instance." : "Security requires a valid symbol_properties instance.";

/// <summary>
/// String message saying: symbolProperties.QuoteCurrency must match the quoteCurrency.Symbol
/// </summary>
public static string UnmatchingQuoteCurrencies = "symbolProperties.QuoteCurrency must match the quoteCurrency.Symbol";
public static string UnmatchingQuoteCurrencies = _languageIsCSharp ? "symbolProperties.QuoteCurrency must match the quoteCurrency.Symbol" : "symbol_properties.quote_currency must match the quote_currency.symbol";

/// <summary>
/// String message saying: Security.SetLocalTimeKeeper(LocalTimeKeeper) must be called in order to use the LocalTime property
Expand Down Expand Up @@ -823,9 +826,8 @@ public static class SecurityManager
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string SymbolNotFoundInSecurities(QuantConnect.Symbol symbol)
{
return Invariant($@"This asset symbol ({
symbol}) was not found in your security list. Please add this security or check it exists before using it with 'Securities.ContainsKey(""{
QuantConnect.SymbolCache.GetTicker(symbol)}"")'");
var securitiesContainsKey = _languageIsCSharp ? "Securities.ContainsKey" : "securities.contains_key";
return Invariant($@"This asset symbol ({symbol}) was not found in your security list. Please add this security or check it exists before using it with '{securitiesContainsKey}(""{QuantConnect.SymbolCache.GetTicker(symbol)}"")'");
}

/// <summary>
Expand Down Expand Up @@ -864,15 +866,17 @@ public static class SecurityPortfolioManager
/// Returns a string message saying the AccountCurrency cannot be changed after adding a Security and that the method
/// SetAccountCurrency() should be moved before AddSecurity()
/// </summary>
public static string CannotChangeAccountCurrencyAfterAddingSecurity =
"Cannot change AccountCurrency after adding a Security. Please move SetAccountCurrency() before AddSecurity().";
public static string CannotChangeAccountCurrencyAfterAddingSecurity = _languageIsCSharp ?
"Cannot change AccountCurrency after adding a Security. Please move SetAccountCurrency() before AddSecurity()." :
"Cannot change AccountCurrency after adding a Security. Please move set_account_currency() before add_security().";

/// <summary>
/// Returns a string message saying the AccountCurrency cannot be changed after setting cash and that the method
/// SetAccountCurrency() should be moved before SetCash()
/// </summary>
public static string CannotChangeAccountCurrencyAfterSettingCash =
"Cannot change AccountCurrency after setting cash. Please move SetAccountCurrency() before SetCash().";
public static string CannotChangeAccountCurrencyAfterSettingCash = _languageIsCSharp ?
"Cannot change AccountCurrency after setting cash. Please move SetAccountCurrency() before SetCash()." :
"Cannot change AccountCurrency after setting cash. Please move set_account_currency() before set_cash().";

/// <summary>
/// Returns a string message saying the AccountCurrency has already been set and that the new value for this property
Expand Down Expand Up @@ -936,16 +940,18 @@ public static class SecurityTransactionManager
/// <summary>
/// Returns a string message saying CancelOpenOrders operation is not allowed in Initialize or during warm up
/// </summary>
public static string CancelOpenOrdersNotAllowedOnInitializeOrWarmUp =
"This operation is not allowed in Initialize or during warm up: CancelOpenOrders. Please move this code to the OnWarmupFinished() method.";
public static string CancelOpenOrdersNotAllowedOnInitializeOrWarmUp = _languageIsCSharp ?
"This operation is not allowed in Initialize or during warm up: CancelOpenOrders. Please move this code to the OnWarmupFinished() method." :
"This operation is not allowed in initialize or during warm up: cancel_open_orders. Please move this code to the on_warmup_finished() method.";

/// <summary>
/// Returns a string message saying the order was canceled by the CancelOpenOrders() at the given time
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string OrderCanceledByCancelOpenOrders(DateTime time)
{
return Invariant($"Canceled by CancelOpenOrders() at {time:o}");
var cancelOpenOrders = _languageIsCSharp ? "CancelOpenOrders" : "cancel_open_orders";
return Invariant($"Canceled by {cancelOpenOrders}() at {time:o}");
}

/// <summary>
Expand Down Expand Up @@ -975,17 +981,17 @@ public static class SymbolProperties
/// <summary>
/// String message saying the SymbolProperties LotSize can not be less than or equal to 0
/// </summary>
public static string InvalidLotSize = "SymbolProperties LotSize can not be less than or equal to 0";
public static string InvalidLotSize = _languageIsCSharp ? "SymbolProperties LotSize can not be less than or equal to 0" : "symbol_properties LOT_SIZE can not be less than or equal to 0";

/// <summary>
/// String message saying the SymbolProperties PriceMagnifier can not be less than or equal to 0
/// </summary>
public static string InvalidPriceMagnifier = "SymbolProprties PriceMagnifier can not be less than or equal to 0";
public static string InvalidPriceMagnifier = _languageIsCSharp ? "SymbolProprties PriceMagnifier can not be less than or equal to 0" : "symbol_properties PRICE_MAGNIFIER can not be less than or equal to 0";

/// <summary>
/// String message saying the SymbolProperties StrikeMultiplier can not be less than or equal to 0
/// </summary>
public static string InvalidStrikeMultiplier = "SymbolProperties StrikeMultiplier can not be less than or equal to 0";
public static string InvalidStrikeMultiplier = _languageIsCSharp ? "SymbolProperties StrikeMultiplier can not be less than or equal to 0" : "symbol_properties STRIKE_MULTIPLIER can not be less than or equal to 0";

/// <summary>
/// Parses a given SymbolProperties object into a string message
Expand Down

0 comments on commit 83d1b09

Please sign in to comment.