Type Conversion
The type conversion component plays an important role in ensuring compatibility between the data types used in StockSharp and the formats specific to a particular exchange.
Main Functions
- Converting StockSharp types (e.g., Sides, OrderTypes, TimeInForce) to string representations used by the exchange.
- Reverse conversion of data received from the exchange to StockSharp types.
- Converting instrument identifiers between StockSharp and exchange formats.
- Converting time formats and timeframes.
Implementation Example
Below is an example of a class with extension methods for type conversion:
static class Extensions
{
// Converting StockSharp order side to exchange string representation
public static string ToNative(this Sides side)
{
return side switch
{
Sides.Buy => "buy",
Sides.Sell => "sell",
_ => throw new ArgumentOutOfRangeException(nameof(side), side, LocalizedStrings.InvalidValue),
};
}
// Converting exchange order side string representation to StockSharp type
public static Sides ToSide(this string side)
=> side?.ToLowerInvariant() switch
{
"buy" or "bid" => Sides.Buy,
"sell" or "ask" or "offer" => Sides.Sell,
_ => throw new ArgumentOutOfRangeException(nameof(side), side, LocalizedStrings.InvalidValue),
};
// Converting StockSharp order type to exchange string representation
public static string ToNative(this OrderTypes? type)
{
return type switch
{
null => null,
OrderTypes.Limit => "limit",
OrderTypes.Market => "market",
OrderTypes.Conditional => "stop",
_ => throw new ArgumentOutOfRangeException(nameof(type), type, LocalizedStrings.InvalidValue),
};
}
// Converting exchange order type string representation to StockSharp type
public static OrderTypes ToOrderType(this string type)
=> type?.ToLowerInvariant() switch
{
"limit" => OrderTypes.Limit,
"market" => OrderTypes.Market,
"stop" or "stop limit" => OrderTypes.Conditional,
_ => throw new ArgumentOutOfRangeException(nameof(type), type, LocalizedStrings.InvalidValue),
};
// Other conversion methods...
// Dictionary for mapping StockSharp timeframes to exchange string representations
public static readonly PairSet<TimeSpan, string> TimeFrames = new()
{
{ TimeSpan.FromMinutes(1), "ONE_MINUTE" },
{ TimeSpan.FromMinutes(5), "FIVE_MINUTE" },
// Other timeframes...
};
// Converting StockSharp timeframe to exchange string representation
public static string ToNative(this TimeSpan timeFrame)
=> TimeFrames.TryGetValue(timeFrame) ?? throw new ArgumentOutOfRangeException(nameof(timeFrame), timeFrame, LocalizedStrings.InvalidValue);
// Converting exchange timeframe string representation to TimeSpan
public static TimeSpan ToTimeFrame(this string name)
=> TimeFrames.TryGetKey2(name) ?? throw new ArgumentOutOfRangeException(nameof(name), name, LocalizedStrings.InvalidValue);
}
Recommendations
- Use extension methods for convenient usage of conversion functions.
- Handle all possible enumeration values, including
nulland unknown values. - Use
switchexpressions (C# 8.0+) for cleaner and more readable code. - Add checks for invalid values and throw exceptions with clear error messages.
- Consider using dictionaries for mapping values, especially for complex or frequently changing mappings (e.g., for timeframes).
Proper implementation of type conversion significantly simplifies working with data in other parts of the connector and reduces the likelihood of errors related to format mismatches.