Skip to content

Commit

Permalink
Fix currency formatting (#7723)
Browse files Browse the repository at this point in the history
* Replace currency string formatting with Lean currency symbols.

Also clean up orders tags from having redundant information.

* Setting default order tag only after price currency is properly set

* Minor fixes and new unit tests

* Minor fixes
  • Loading branch information
jhonabreul authored Jan 25, 2024
1 parent 0cd979f commit 07ded61
Show file tree
Hide file tree
Showing 32 changed files with 228 additions and 141 deletions.
2 changes: 1 addition & 1 deletion Algorithm.CSharp/AddBetaIndicatorRegressionAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public override void OnOrderEvent(OrderEvent orderEvent)
{"Estimated Strategy Capacity", "$35000000.00"},
{"Lowest Capacity Asset", "IBM R735QTJ8XC9X"},
{"Portfolio Turnover", "1.51%"},
{"OrderListHash", "e930f95771bc50dd2db1c353e054c4e7"}
{"OrderListHash", "381bb9310f9dceb8a79a56849789bdab"}
};
}
}
2 changes: 1 addition & 1 deletion Algorithm.CSharp/BasicTemplateCryptoAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ public override void OnEndOfAlgorithm()
{"Estimated Strategy Capacity", "$0"},
{"Lowest Capacity Asset", "BTCEUR 2XR"},
{"Portfolio Turnover", "118.08%"},
{"OrderListHash", "551b20736f4558a5af5c02b84451fb77"}
{"OrderListHash", "77458586d24f1cd00623d63da8279be2"}
};
}
}
2 changes: 1 addition & 1 deletion Algorithm.CSharp/BybitCryptoRegressionAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public override void OnEndOfAlgorithm()
{"Estimated Strategy Capacity", "₮560000.00"},
{"Lowest Capacity Asset", "BTCUSDT 2UZ"},
{"Portfolio Turnover", "44.04%"},
{"OrderListHash", "fb30d137fffe2bc4195d261f0f195b69"}
{"OrderListHash", "b3a9eb7392ba1eb7eb0cc387f7382b6c"}
};
}
}
2 changes: 1 addition & 1 deletion Algorithm.CSharp/CancelOpenOrdersRegressionAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public override void OnEndOfAlgorithm()
{"Estimated Strategy Capacity", "$370000.00"},
{"Lowest Capacity Asset", "ETHUSD 2XR"},
{"Portfolio Turnover", "104.59%"},
{"OrderListHash", "bd538acb61fa1fd91732212664c9bbed"}
{"OrderListHash", "ec714d818fa30597e992d4c6e939e68c"}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public override void OnEndOfAlgorithm()
{"Estimated Strategy Capacity", "$2600000000.00"},
{"Lowest Capacity Asset", "ES VMKLFZIH2MTD"},
{"Portfolio Turnover", "16.49%"},
{"OrderListHash", "a8b0528aa2c4c2b8f013a06104ddb1d0"}
{"OrderListHash", "a83211606af7216647acfd9b5f6907a5"}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public bool InMarketHours()
{"Estimated Strategy Capacity", "$14000000.00"},
{"Lowest Capacity Asset", "SPY R735QTJ8XC9X"},
{"Portfolio Turnover", "1.44%"},
{"OrderListHash", "730d1d797bbe9449a6dc123990f5bd4b"}
{"OrderListHash", "1ebbd6a077af944e04ee437a5219eef4"}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public override void OnEndOfAlgorithm()
{"Estimated Strategy Capacity", "$3400000.00"},
{"Lowest Capacity Asset", "ES VMKLFZIH2MTD"},
{"Portfolio Turnover", "138.95%"},
{"OrderListHash", "c85997dd5b7d9acda46ac9d11dd1a039"}
{"OrderListHash", "b26a8ad66a8dc5af768a22fbf9a80cec"}
};
}
}
2 changes: 1 addition & 1 deletion Algorithm.CSharp/LimitFillRegressionAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public override void OnOrderEvent(OrderEvent orderEvent)
{"Estimated Strategy Capacity", "$180000000.00"},
{"Lowest Capacity Asset", "SPY R735QTJ8XC9X"},
{"Portfolio Turnover", "9.86%"},
{"OrderListHash", "14eaea4041585a9110df4b5861793f9e"}
{"OrderListHash", "a5363ad9b8119f0e256a48efdc7acbb5"}
};
}
}
12 changes: 6 additions & 6 deletions Algorithm.CSharp/LimitIfTouchedRegressionAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public class LimitIfTouchedRegressionAlgorithm : QCAlgorithm, IRegressionAlgorit
// We assert the following occur in FIFO order in OnOrderEvent
private readonly Queue<string> _expectedEvents = new Queue<string>(new[]
{
"Time: 10/10/2013 13:31:00 OrderID: 72 EventID: 399 Symbol: SPY Status: Filled Quantity: -1 FillQuantity: -1 FillPrice: 144.6434 USD LimitPrice: 144.3551 TriggerPrice: 143.61 OrderFee: 1 USD",
"Time: 10/10/2013 15:57:00 OrderID: 73 EventID: 156 Symbol: SPY Status: Filled Quantity: -1 FillQuantity: -1 FillPrice: 145.6636 USD LimitPrice: 145.6434 TriggerPrice: 144.89 OrderFee: 1 USD",
"Time: 10/11/2013 15:37:00 OrderID: 74 EventID: 380 Symbol: SPY Status: Filled Quantity: -1 FillQuantity: -1 FillPrice: 146.7185 USD LimitPrice: 146.6723 TriggerPrice: 145.92 OrderFee: 1 USD" });
"Time: 10/10/2013 13:31:00 OrderID: 72 EventID: 399 Symbol: SPY Status: Filled Quantity: -1 FillQuantity: -1 FillPrice: $144.6434 LimitPrice: $144.3551 TriggerPrice: $143.61 OrderFee: 1 USD",
"Time: 10/10/2013 15:57:00 OrderID: 73 EventID: 156 Symbol: SPY Status: Filled Quantity: -1 FillQuantity: -1 FillPrice: $145.6636 LimitPrice: $145.6434 TriggerPrice: $144.89 OrderFee: 1 USD",
"Time: 10/11/2013 15:37:00 OrderID: 74 EventID: 380 Symbol: SPY Status: Filled Quantity: -1 FillQuantity: -1 FillPrice: $146.7185 LimitPrice: $146.6723 TriggerPrice: $145.92 OrderFee: 1 USD" });

/// <summary>
/// Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.
Expand Down Expand Up @@ -74,10 +74,10 @@ public override void OnData(Slice data)
$"LIT - Quantity: {_negative * 10}");
_request = Transactions.AddOrder(orderRequest);
return;

}

// Order updating if request exists
// Order updating if request exists
if (_request != null)
{
if (_request.Quantity == 1)
Expand Down Expand Up @@ -105,7 +105,7 @@ public override void OnOrderEvent(OrderEvent orderEvent)

if (orderEvent.ToString() != expected)
{
throw new Exception($"orderEvent {orderEvent.Id} differed from {expected}");
throw new Exception($"orderEvent {orderEvent.Id} differed from {expected}. Actual {orderEvent}");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public override void OnOrderEvent(OrderEvent orderEvent)
{"Estimated Strategy Capacity", "$39000000.00"},
{"Lowest Capacity Asset", "ES VMKLFZIH2MTD"},
{"Portfolio Turnover", "33.59%"},
{"OrderListHash", "170df706887bac7764441b9368887a1d"}
{"OrderListHash", "8b3baab1411a6db34780601bdd68ef9e"}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ private static bool AreEqual(decimal expected, decimal actual)
{"Estimated Strategy Capacity", "$0"},
{"Lowest Capacity Asset", "GOOCV VP83T1ZUHROL"},
{"Portfolio Turnover", "50.31%"},
{"OrderListHash", "92d006d828b582a7cc78eeb4c0161356"}
{"OrderListHash", "eaa2b6ffada95de55b095ed2e9953e33"}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public override void OnData(Slice slice)
{"Estimated Strategy Capacity", "$0"},
{"Lowest Capacity Asset", "GOOCV W78ZFMEBBB2E|GOOCV VP83T1ZUHROL"},
{"Portfolio Turnover", "274.86%"},
{"OrderListHash", "0948e7066371a8aae356145cbac0741c"}
{"OrderListHash", "9aa5e8c352571222f77407c62a5795c4"}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public override void OnEndOfAlgorithm()
{"Estimated Strategy Capacity", "$26000.00"},
{"Lowest Capacity Asset", "AOL R735QTJ8XC9X"},
{"Portfolio Turnover", "12.68%"},
{"OrderListHash", "215c8a8ac308c82fb7b0b46f3be0972e"}
{"OrderListHash", "0241913ee2f0fa5af72e311a09c39f4e"}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public RegressionTestShortableProvider() : base("testbrokerage")
{"Estimated Strategy Capacity", "$99000000.00"},
{"Lowest Capacity Asset", "AIG R735QTJ8XC9X"},
{"Portfolio Turnover", "0.23%"},
{"OrderListHash", "32ac6dfd7b74518f14fb0210ce0360df"}
{"OrderListHash", "7d60ef344c55973a95131cdeefdbaeb3"}
};
}
}
2 changes: 1 addition & 1 deletion Algorithm.CSharp/SplitEquityRegressionAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public override void OnEndOfAlgorithm()
{"Estimated Strategy Capacity", "$0"},
{"Lowest Capacity Asset", ""},
{"Portfolio Turnover", "0%"},
{"OrderListHash", "7eca64a29976e1c2f206dd92b0930522"}
{"OrderListHash", "b1df545a12beb8868606e0da35c151d0"}
};
}
}
2 changes: 1 addition & 1 deletion Algorithm.CSharp/StopLimitOrderRegressionAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public override void OnEndOfAlgorithm()
{"Estimated Strategy Capacity", "$2700000000.00"},
{"Lowest Capacity Asset", "SPY R735QTJ8XC9X"},
{"Portfolio Turnover", "0.02%"},
{"OrderListHash", "60a73554224704136fff171dd50d8d28"}
{"OrderListHash", "ebb6f711f14449c0cf8dcee645b34956"}
};
}
}
3 changes: 2 additions & 1 deletion Algorithm.CSharp/StopLossOnOrderEventRegressionAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public override void OnOrderEvent(OrderEvent orderEvent)
{
// Entry short $2 below
var stopPrice = orderEvent.FillPrice - 2;
Debug($"Enter short at {orderEvent.FillPrice} set STOPLOSS at {stopPrice:C}");
var currencySymbol = Currencies.GetCurrencySymbol(order.PriceCurrency);
Debug($"Enter short at {orderEvent.FillPrice} set STOPLOSS at {currencySymbol}{stopPrice}");
StopMarketOrder(order.Symbol, -order.Quantity, stopPrice, "StopLoss");
}
}
Expand Down
2 changes: 1 addition & 1 deletion Algorithm.CSharp/TimeInForceAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public override void OnEndOfAlgorithm()
{"Estimated Strategy Capacity", "$44000000.00"},
{"Lowest Capacity Asset", "SPY R735QTJ8XC9X"},
{"Portfolio Turnover", "0.87%"},
{"OrderListHash", "4f94db1e287d9dd7026bda8c93a62876"}
{"OrderListHash", "9ce26e877ba1b5dbe19765fb5374334e"}
};
}
}
2 changes: 1 addition & 1 deletion Algorithm.CSharp/TrailingStopOrderRegressionAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public override void OnOrderEvent(OrderEvent orderEvent)
{"Estimated Strategy Capacity", "$36000000.00"},
{"Lowest Capacity Asset", "SPY R735QTJ8XC9X"},
{"Portfolio Turnover", "5.79%"},
{"OrderListHash", "52f67285c3f5ecdc66f89cb1c0d08421"}
{"OrderListHash", "fcfc3ecf4a3eff97be9f07305c97ad95"}
};
}
}
12 changes: 6 additions & 6 deletions Algorithm.Python/LimitIfTouchedRegressionAlgorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
### <meta name="tag" content="limit if touched order"/>
class LimitIfTouchedRegressionAlgorithm(QCAlgorithm):
_expectedEvents = deque([
"Time: 10/10/2013 13:31:00 OrderID: 72 EventID: 399 Symbol: SPY Status: Filled Quantity: -1 FillQuantity: -1 FillPrice: 144.6434 USD LimitPrice: 144.3551 TriggerPrice: 143.61 OrderFee: 1 USD",
"Time: 10/10/2013 15:57:00 OrderID: 73 EventID: 156 Symbol: SPY Status: Filled Quantity: -1 FillQuantity: -1 FillPrice: 145.6636 USD LimitPrice: 145.6434 TriggerPrice: 144.89 OrderFee: 1 USD",
"Time: 10/11/2013 15:37:00 OrderID: 74 EventID: 380 Symbol: SPY Status: Filled Quantity: -1 FillQuantity: -1 FillPrice: 146.7185 USD LimitPrice: 146.6723 TriggerPrice: 145.92 OrderFee: 1 USD" ])
"Time: 10/10/2013 13:31:00 OrderID: 72 EventID: 399 Symbol: SPY Status: Filled Quantity: -1 FillQuantity: -1 FillPrice: $144.6434 LimitPrice: $144.3551 TriggerPrice: $143.61 OrderFee: 1 USD",
"Time: 10/10/2013 15:57:00 OrderID: 73 EventID: 156 Symbol: SPY Status: Filled Quantity: -1 FillQuantity: -1 FillPrice: $145.6636 LimitPrice: $145.6434 TriggerPrice: $144.89 OrderFee: 1 USD",
"Time: 10/11/2013 15:37:00 OrderID: 74 EventID: 380 Symbol: SPY Status: Filled Quantity: -1 FillQuantity: -1 FillPrice: $146.7185 LimitPrice: $146.6723 TriggerPrice: $145.92 OrderFee: 1 USD" ])

def Initialize(self):
self.SetStartDate(2013, 10, 7)
Expand All @@ -51,11 +51,11 @@ def OnData(self, data):
return

new_quantity = int(self._request.Quantity - self._negative)
self._request.UpdateQuantity(new_quantity, f"LIT - Quantity: {new_quantity}")
self._request.UpdateQuantity(new_quantity, f"LIT - Quantity: {new_quantity}")
self._request.UpdateTriggerPrice(Extensions.RoundToSignificantDigits(self._request.Get(OrderField.TriggerPrice), 5));

def OnOrderEvent(self, orderEvent):
if orderEvent.Status == OrderStatus.Filled:
expected = self._expectedEvents.popleft()
if orderEvent.ToString() != expected:
raise Exception(f"orderEvent {orderEvent.Id} differed from {expected}")
if str(orderEvent) != expected:
raise Exception(f"orderEvent {orderEvent.Id} differed from {expected}. Actual {orderEvent}")
14 changes: 9 additions & 5 deletions Common/Currencies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,13 @@ public static class Currencies
{"LSK", "Ⱡ"},
{"NAV", "Ꞥ"}
};

/// <summary>
/// Stable pairs in GDAX. We defined them because they have different fees in GDAX market
/// </summary>
[Obsolete("StablePairsGDAX is deprecated. Use StablePairsCoinbase instead.")]
public static readonly HashSet<string> StablePairsGDAX = StablePairsCoinbase;

/// <summary>
/// Stable pairs in Coinbase. We defined them because they have different fees in Coinbase market
/// </summary>
Expand All @@ -174,7 +174,7 @@ public static class Currencies
"USTUSDT",
"WBTCBTC"
};

/// <summary>
/// Define some StableCoins that don't have direct pairs for base currencies in our SPDB in Coinbase market
/// This is because some CryptoExchanges do not define direct pairs with the stablecoins they offer.
Expand Down Expand Up @@ -272,8 +272,12 @@ public static bool IsStableCoinWithoutPair(string symbol, string market)
/// <returns>The currency symbol</returns>
public static string GetCurrencySymbol(string currency)
{
string currencySymbol;
return CurrencySymbols.TryGetValue(currency, out currencySymbol) ? currencySymbol : currency;
if (string.IsNullOrEmpty(currency))
{
return string.Empty;
}

return CurrencySymbols.TryGetValue(currency, out var currencySymbol) ? currencySymbol : currency;
}

/// <summary>
Expand Down
Loading

0 comments on commit 07ded61

Please sign in to comment.