Skip to content

Commit

Permalink
Time zones.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikasoukhov committed Feb 22, 2020
1 parent 3b8d956 commit d0e9f26
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 25 deletions.
39 changes: 31 additions & 8 deletions MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>

<Label Grid.Column="0" Grid.Row="0" Margin="2.5">QSH:</Label>
<TextBlock Grid.Column="0" Grid.Row="0" Margin="2.5" VerticalAlignment="Center">QSH:</TextBlock>
<devExp:FolderBrowserPicker x:Name="QshFolder" Grid.Column="1" Grid.Row="0" FolderChanged="OnFolderChanged" Margin="2.5"/>

<Label Grid.Column="0" Grid.Row="1" Margin="2.5">S#:</Label>
<TextBlock Grid.Column="0" Grid.Row="1" Margin="2.5" VerticalAlignment="Center">S#:</TextBlock>
<devExp:FolderBrowserPicker x:Name="StockSharpFolder" Grid.Column="1" Grid.Row="1" FolderChanged="OnFolderChanged" Margin="2.5"/>
</Grid>

Expand All @@ -50,14 +50,37 @@
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="5"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="5"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Label Grid.Column="0" Grid.Row="0" Margin="2.5">Board code:</Label>
<xaml:ExchangeBoardComboBox x:Name="Board" Grid.Column="1" Grid.Row="0" SelectionChanged="Board_OnSelectionChanged" MinWidth="100" Margin="2.5"/>
<TextBlock Grid.Column="0" Grid.Row="0" VerticalAlignment="Center">Board code:</TextBlock>
<xaml:ExchangeBoardComboBox x:Name="Board" Grid.Column="1" Grid.Row="0" SelectionChanged="Board_OnSelectionChanged" MinWidth="100"/>

<Label Grid.Column="0" Grid.Row="1" Margin="2.5">Security Code Masks:</Label>
<dxe:TextEdit x:Name="SecurityLike" Grid.Column="1" Grid.Row="1" Margin="2.5"
<TextBlock Grid.Column="0" Grid.Row="2" VerticalAlignment="Center">Security Code Masks:</TextBlock>
<dxe:TextEdit x:Name="SecurityLike" Grid.Column="1" Grid.Row="2"
ToolTip="Masks, separated by commas. For examle '*BR*,CHMF-9.14,LKOH'. '*BR*' mask will leads to conversion all found securities, contains BR sumbols in code, like 'BR','SBRF','VTBR' and etc."/>

<GroupBox Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="4" Header="{x:Static loc:LocalizedStrings.TimeZone}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="5"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>

<TextBlock Text="{x:Static loc:LocalizedStrings.Str203}" VerticalAlignment="Center" Grid.Column="0" Grid.Row="0" />
<devExp:TimeZoneEditor x:Name="TimeStampZone" Grid.Column="1" Grid.Row="0" />

<TextBlock Text="{x:Static loc:LocalizedStrings.ServerTime}" VerticalAlignment="Center" Grid.Column="0" Grid.Row="2" />
<devExp:TimeZoneEditor x:Name="MarketDataZone" Grid.Column="1" Grid.Row="2" />
</Grid>
</GroupBox>
</Grid>
</GroupBox>
<GroupBox x:Name="StorageSettingsBox" Grid.Column="1" Header="Convertion settings" Padding="5" Margin="2.5">
Expand All @@ -72,7 +95,7 @@
<RowDefinition Height="*"/>
</Grid.RowDefinitions>

<Label Grid.Column="0" Grid.Row="0" Margin="2.5">Output format:</Label>
<TextBlock Grid.Column="0" Grid.Row="0" Margin="2.5" VerticalAlignment="Center">Output format:</TextBlock>
<ComboBox x:Name="Format" Grid.Column="1" Grid.Row="0" Width="60" HorizontalAlignment="Left" Margin="2.5"/>

<dxe:CheckEdit x:Name="MultiThread" Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2" VerticalAlignment="Center" Margin="2.5">Multi thread convertion</dxe:CheckEdit>
Expand Down
57 changes: 40 additions & 17 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ private class Settings
public bool MultiThread { get; set; }
public bool OrderLog2OrderBook { get; set; }
public int OrderBookMaxDepth { get; set; } = 5;
public string TimeStampZone { get; set; }
public string MarketDataZone { get; set; }
}

private readonly LogManager _logManager = new LogManager();
Expand All @@ -68,6 +70,9 @@ public MainWindow()

Directory.CreateDirectory(_settingsDir);

TimeStampZone.TimeZone = TimeZoneInfo.Utc;
MarketDataZone.TimeZone = TimeHelper.Moscow;

_logManager.Application.LogLevel = LogLevels.Verbose;

_logManager.Listeners.Add(new GuiLogListener(LogControl));
Expand Down Expand Up @@ -96,6 +101,12 @@ public MainWindow()
settings.Board.IsEmpty()
? ExchangeBoard.Forts
: Board.Boards.FirstOrDefault(b => b.Code.CompareIgnoreCase(settings.Board)) ?? ExchangeBoard.Forts;

if (!settings.TimeStampZone.IsEmpty())
TimeStampZone.TimeZone = TimeZoneInfo.FindSystemTimeZoneById(settings.TimeStampZone);

if (!settings.MarketDataZone.IsEmpty())
MarketDataZone.TimeZone = TimeZoneInfo.FindSystemTimeZoneById(settings.MarketDataZone);
}

if (File.Exists(_convertedFilesFile))
Expand Down Expand Up @@ -126,6 +137,8 @@ private Settings SaveSettings()
Board = Board.SelectedBoard?.Code,
MultiThread = MultiThread.IsChecked == true,
OrderLog2OrderBook = OrderLog2OrderBook.IsEnabled && OrderLog2OrderBook.IsChecked == true,
TimeStampZone = TimeStampZone.TimeZone.Id,
MarketDataZone = MarketDataZone.TimeZone.Id,
};

try
Expand Down Expand Up @@ -165,6 +178,8 @@ private void Convert_OnClick(object sender, RoutedEventArgs e)
var board = Board.SelectedBoard;

var orderLog2OrderBookBuilders = settings.OrderLog2OrderBook ? new Dictionary<SecurityId, IOrderLogMarketDepthBuilder>() : null;
var tz = TimeStampZone.TimeZone;
var mz = MarketDataZone.TimeZone;

Task.Factory.StartNew(() =>
{
Expand All @@ -179,7 +194,7 @@ private void Convert_OnClick(object sender, RoutedEventArgs e)

_startConvertTime = DateTimeOffset.Now;

ConvertDirectory(settings.QshFolder, registry, settings.Format, board, settings.SecurityLike, settings.MultiThread, orderLog2OrderBookBuilders, settings.OrderBookMaxDepth);
ConvertDirectory(settings.QshFolder, registry, settings.Format, board, settings.SecurityLike, settings.MultiThread, orderLog2OrderBookBuilders, settings.OrderBookMaxDepth, tz, mz);
})
.ContinueWith(t =>
{
Expand Down Expand Up @@ -216,25 +231,25 @@ private void Convert_OnClick(object sender, RoutedEventArgs e)
}, TaskScheduler.FromCurrentSynchronizationContext());
}

private void ConvertDirectory(string path, IStorageRegistry registry, StorageFormats format, ExchangeBoard board, string securityLike, bool multithread, Dictionary<SecurityId, IOrderLogMarketDepthBuilder> orderLog2OrderBookBuilders, int orderBookMaxDepth)
private void ConvertDirectory(string path, IStorageRegistry registry, StorageFormats format, ExchangeBoard board, string securityLike, bool multithread, Dictionary<SecurityId, IOrderLogMarketDepthBuilder> orderLog2OrderBookBuilders, int orderBookMaxDepth, TimeZoneInfo tz, TimeZoneInfo mz)
{
if (!_isStarted)
return;

var files = Directory.GetFiles(path, "*.qsh");

if (!multithread)
files.ForEach(f => ConvertFile(f, registry, format, board, securityLike, orderLog2OrderBookBuilders, orderBookMaxDepth));
files.ForEach(f => ConvertFile(f, registry, format, board, securityLike, orderLog2OrderBookBuilders, orderBookMaxDepth, tz, mz));
else
{
Parallel.ForEach(files, file => ConvertFile(file, registry, format, board, securityLike, orderLog2OrderBookBuilders, orderBookMaxDepth));
Parallel.ForEach(files, file => ConvertFile(file, registry, format, board, securityLike, orderLog2OrderBookBuilders, orderBookMaxDepth, tz, mz));
}

//пишем имена сконвертированных в деректории файлов qsh, в файл
File.AppendAllLines(_convertedFilesFile, _convertedPerTaskPoolFiles);
_convertedPerTaskPoolFiles.Clear();

Directory.GetDirectories(path).ForEach(d => ConvertDirectory(d, registry, format, board, securityLike, multithread, orderLog2OrderBookBuilders, orderBookMaxDepth));
Directory.GetDirectories(path).ForEach(d => ConvertDirectory(d, registry, format, board, securityLike, multithread, orderLog2OrderBookBuilders, orderBookMaxDepth, tz, mz));
}

private void TryFlushData<TMessage>(IStorageRegistry registry, SecurityId securityId, StorageFormats format, object arg, List<TMessage> messages, QshReader reader)
Expand All @@ -251,7 +266,7 @@ private void TryFlushData<TMessage>(IStorageRegistry registry, SecurityId securi
messages.Clear();
}

private void ConvertFile(string fileName, IStorageRegistry registry, StorageFormats format, ExchangeBoard board, string securityLike, Dictionary<SecurityId, IOrderLogMarketDepthBuilder> orderLog2OrderBookBuilders, int orderBookMaxDepth)
private void ConvertFile(string fileName, IStorageRegistry registry, StorageFormats format, ExchangeBoard board, string securityLike, Dictionary<SecurityId, IOrderLogMarketDepthBuilder> orderLog2OrderBookBuilders, int orderBookMaxDepth, TimeZoneInfo tz, TimeZoneInfo mz)
{
if (!_isStarted)
return;
Expand All @@ -267,6 +282,9 @@ private void ConvertFile(string fileName, IStorageRegistry registry, StorageForm

var data = new Dictionary<SecurityId, Tuple<List<QuoteChangeMessage>, List<ExecutionMessage>, List<Level1ChangeMessage>, List<ExecutionMessage>>>();

DateTimeOffset ToLocalDto(DateTime dt) => dt.ApplyTimeZone(tz);
DateTimeOffset ToServerDto(DateTime dt) => dt.ApplyTimeZone(mz);

using (var qr = QshReader.Open(fileName))
{
var reader = qr;
Expand Down Expand Up @@ -339,15 +357,18 @@ private void ConvertFile(string fileName, IStorageRegistry registry, StorageForm
bids.Add(new QuoteChange(priceStep * q.Price, q.Volume));
break;
default:
throw new ArgumentException(q.Type.ToString());
{
continue;
//throw new ArgumentException(q.Type.ToString());
}
}
}

var md = new QuoteChangeMessage
{
LocalTime = reader.CurrentDateTime.ApplyTimeZone(TimeHelper.Moscow),
LocalTime = ToLocalDto(reader.CurrentDateTime),
SecurityId = securityId,
ServerTime = reader.CurrentDateTime.ApplyTimeZone(TimeHelper.Moscow),
ServerTime = ToLocalDto(reader.CurrentDateTime),
Bids = bids.ToArray(),
Asks = asks.ToArray(),
};
Expand All @@ -370,15 +391,15 @@ private void ConvertFile(string fileName, IStorageRegistry registry, StorageForm
{
secData.Item2.Add(new ExecutionMessage
{
LocalTime = reader.CurrentDateTime.ApplyTimeZone(TimeHelper.Moscow),
LocalTime = ToLocalDto(reader.CurrentDateTime),
HasTradeInfo = true,
ExecutionType = ExecutionTypes.Tick,
SecurityId = securityId,
OpenInterest = deal.OI == 0 ? (long?)null : deal.OI,
ServerTime = deal.DateTime.ApplyTimeZone(TimeHelper.Moscow),
ServerTime = ToServerDto(deal.DateTime),
TradeVolume = deal.Volume,
TradeId = deal.Id == 0 ? (long?)null : deal.Id,
TradePrice = deal.Price,
TradePrice = deal.Price * priceStep,
OriginSide =
deal.Type == DealType.Buy
? Sides.Buy
Expand All @@ -402,13 +423,13 @@ private void ConvertFile(string fileName, IStorageRegistry registry, StorageForm

var msg = new ExecutionMessage
{
LocalTime = reader.CurrentDateTime.ApplyTimeZone(TimeHelper.Moscow),
LocalTime = ToLocalDto(reader.CurrentDateTime),
ExecutionType = ExecutionTypes.OrderLog,
SecurityId = securityId,
OpenInterest = ol.OI == 0 ? (long?)null : ol.OI,
OrderId = ol.OrderId,
OrderPrice = priceStep * ol.Price,
ServerTime = ol.DateTime.ApplyTimeZone(TimeHelper.Moscow),
ServerTime = ToServerDto(ol.DateTime),
OrderVolume = ol.Amount,
Balance = ol.AmountRest,
TradeId = ol.DealId == 0 ? (long?)null : ol.DealId,
Expand Down Expand Up @@ -531,15 +552,17 @@ private void ConvertFile(string fileName, IStorageRegistry registry, StorageForm
{
var l1Msg = new Level1ChangeMessage
{
LocalTime = reader.CurrentDateTime.ApplyTimeZone(TimeHelper.Moscow),
LocalTime = ToLocalDto(reader.CurrentDateTime),
SecurityId = securityId,
ServerTime = reader.CurrentDateTime.ApplyTimeZone(TimeHelper.Moscow),
ServerTime = ToLocalDto(reader.CurrentDateTime),
}
.TryAdd(Level1Fields.LastTradePrice, priceStep * info.Price)
.TryAdd(Level1Fields.BidsVolume, (decimal)info.BidTotal)
.TryAdd(Level1Fields.AsksVolume, (decimal)info.AskTotal)
.TryAdd(Level1Fields.HighPrice, priceStep * info.HiLimit)
.TryAdd(Level1Fields.LowPrice, priceStep * info.LoLimit)
.TryAdd(Level1Fields.StepPrice, (decimal)info.Rate)
.TryAdd(Level1Fields.OperatingMargin, (decimal)info.Deposit)
.TryAdd(Level1Fields.OpenInterest, (decimal)info.OI);

if (l1Msg.Changes.Count == 0)
Expand Down Expand Up @@ -606,7 +629,7 @@ private void ConvertFile(string fileName, IStorageRegistry registry, StorageForm
_logManager.Application.AddInfoLog("Завершена конвертация файла {0}.", fileName);
}

private SecurityId GetSecurityId(QScalp.Security security, string boardCode)
private static SecurityId GetSecurityId(QScalp.Security security, string boardCode)
{
return new SecurityId
{
Expand Down

0 comments on commit d0e9f26

Please sign in to comment.