mirror of
https://github.com/StockSharp/StockSharp.git
synced 2026-12-05 17:11:52 +00:00
Sample optimization. Random search supported.
This commit is contained in:
1 parent
f80ea4812c
commit
f4e8367eec
2 files changed
+88
-11
No files matched your search
@@ -4,7 +4,12 @@
|
||||
xmlns:loc="clr-namespace:StockSharp.Localization;assembly=StockSharp.Localization"
|
||||
xmlns:sx="http://schemas.stocksharp.com/xaml"
|
||||
xmlns:ex="clr-namespace:Ecng.Xaml;assembly=StockSharp.Xaml"
|
||||
xmlns:exc="clr-namespace:Ecng.Xaml.Converters;assembly=StockSharp.Xaml"
|
||||
xmlns:sys="clr-namespace:System;assembly=mscorlib"
|
||||
Title="{x:Static loc:LocalizedStrings.SampleOptimizationApp}" Height="430" Width="525">
|
||||
<Window.Resources>
|
||||
<exc:BoolAllConverter x:Key="boolAll" />
|
||||
</Window.Resources>
|
||||
<Grid>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto" />
|
||||
@@ -27,14 +32,40 @@
|
||||
</Grid>
|
||||
|
||||
<Grid x:Name="OptimizeTypeGrid" Grid.Row="2">
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="5" />
|
||||
<RowDefinition Height="Auto" />
|
||||
</Grid.RowDefinitions>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="Auto" />
|
||||
<ColumnDefinition Width="5" />
|
||||
<ColumnDefinition Width="Auto" />
|
||||
<ColumnDefinition Width="5" />
|
||||
<ColumnDefinition Width="Auto" />
|
||||
<ColumnDefinition Width="Auto" />
|
||||
</Grid.ColumnDefinitions>
|
||||
|
||||
<RadioButton x:Name="BruteForce" GroupName="OptimizerType" IsChecked="True" Content="{x:Static loc:LocalizedStrings.BruteForce}" />
|
||||
<RadioButton x:Name="Genetic" GroupName="OptimizerType" Grid.Column="2" Content="{x:Static loc:LocalizedStrings.Genetic}" />
|
||||
|
||||
<CheckBox x:Name="RandomMode" Grid.Row="2" Grid.Column="0" Content="Random" IsEnabled="{Binding ElementName=BruteForce, Path=IsChecked}" />
|
||||
<Label Grid.Row="2" Grid.Column="2" Content="Count:">
|
||||
<Label.IsEnabled>
|
||||
<MultiBinding Converter="{StaticResource boolAll}">
|
||||
<Binding ElementName="BruteForce" Path="IsChecked" />
|
||||
<Binding ElementName="RandomMode" Path="IsChecked" />
|
||||
</MultiBinding>
|
||||
</Label.IsEnabled>
|
||||
</Label>
|
||||
<TextBox x:Name="RandomCount" Grid.Row="2" Grid.Column="4" Width="60" Text="10">
|
||||
<TextBox.IsEnabled>
|
||||
<MultiBinding Converter="{StaticResource boolAll}">
|
||||
<Binding ElementName="BruteForce" Path="IsChecked" />
|
||||
<Binding ElementName="RandomMode" Path="IsChecked" />
|
||||
</MultiBinding>
|
||||
</TextBox.IsEnabled>
|
||||
</TextBox>
|
||||
</Grid>
|
||||
|
||||
<Grid Grid.Row="4">
|
||||
|
||||
@@ -4,7 +4,6 @@ using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows.Media;
|
||||
using System.Collections.Generic;
|
||||
|
||||
using Ecng.Xaml;
|
||||
@@ -52,7 +51,9 @@ public partial class MainWindow
|
||||
|
||||
OptimizeTypeGrid.IsEnabled = false;
|
||||
|
||||
if (HistoryPath.Folder.IsEmpty() || !Directory.Exists(HistoryPath.Folder))
|
||||
var folder = HistoryPath.Folder;
|
||||
|
||||
if (folder.IsEmpty() || !Directory.Exists(folder))
|
||||
{
|
||||
MessageBox.Show(this, LocalizedStrings.WrongPath);
|
||||
return;
|
||||
@@ -74,15 +75,60 @@ public partial class MainWindow
|
||||
(TimeSpan min, TimeSpan max, TimeSpan step) tfRange = new(TimeSpan.FromMinutes(5), TimeSpan.FromMinutes(15), TimeSpan.FromMinutes(5));
|
||||
|
||||
// SMA periods
|
||||
var periods = new List<(int longMa, int shortMa, TimeSpan tf, Color color)>();
|
||||
var periods = new List<(int longMa, int shortMa, TimeSpan tf)>();
|
||||
|
||||
for (var l = longRange.max; l >= longRange.min; l -= longRange.step)
|
||||
var isRandomMode = RandomMode.IsChecked == true;
|
||||
var randomCount = isRandomMode ? int.Parse(RandomCount.Text) : 0;
|
||||
|
||||
if (isRandomMode)
|
||||
{
|
||||
for (var s = shortRange.max; s >= shortRange.min; s -= shortRange.step)
|
||||
// Random mode: generate randomCount random combinations using GetRandomOptimizeValue()
|
||||
// Create temporary strategy to use StrategyParam.GetRandomOptimizeValue()
|
||||
var tempStrategy = new History.SmaStrategy();
|
||||
|
||||
var longParam = (StrategyParam<int>)tempStrategy.Parameters[nameof(tempStrategy.LongSma)];
|
||||
var shortParam = (StrategyParam<int>)tempStrategy.Parameters[nameof(tempStrategy.ShortSma)];
|
||||
var tfParam = (StrategyParam<TimeSpan?>)tempStrategy.Parameters[nameof(tempStrategy.CandleTimeFrame)];
|
||||
|
||||
var longValues = new HashSet<int>();
|
||||
var shortValues = new HashSet<int>();
|
||||
var tfValues = new HashSet<TimeSpan>();
|
||||
|
||||
for (var i = 0; i < randomCount; i++)
|
||||
longValues.Add(longParam.GetRandom());
|
||||
|
||||
for (var i = 0; i < randomCount; i++)
|
||||
shortValues.Add(shortParam.GetRandom());
|
||||
|
||||
for (var i = 0; i < randomCount; i++)
|
||||
{
|
||||
for (var t = tfRange.max; t >= tfRange.min; t -= tfRange.step)
|
||||
if (tfParam.GetRandom() is TimeSpan tf)
|
||||
tfValues.Add(tf);
|
||||
}
|
||||
|
||||
// Create all combinations from random samples
|
||||
foreach (var l in longValues)
|
||||
{
|
||||
foreach (var s in shortValues)
|
||||
{
|
||||
periods.Add((l, s, t, Color.FromRgb((byte)RandomGen.GetInt(255), (byte)RandomGen.GetInt(255), (byte)RandomGen.GetInt(255))));
|
||||
foreach (var t in tfValues)
|
||||
{
|
||||
periods.Add((l, s, t));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Step-based mode: generate with step
|
||||
for (var l = longRange.max; l >= longRange.min; l -= longRange.step)
|
||||
{
|
||||
for (var s = shortRange.max; s >= shortRange.min; s -= shortRange.step)
|
||||
{
|
||||
for (var t = tfRange.max; t >= tfRange.min; t -= tfRange.step)
|
||||
{
|
||||
periods.Add((l, s, t));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -91,7 +137,7 @@ public partial class MainWindow
|
||||
var storageRegistry = new StorageRegistry
|
||||
{
|
||||
// set historical path
|
||||
DefaultDrive = new LocalMarketDataDrive(HistoryPath.Folder)
|
||||
DefaultDrive = new LocalMarketDataDrive(folder)
|
||||
};
|
||||
|
||||
// create test security
|
||||
@@ -121,10 +167,10 @@ public partial class MainWindow
|
||||
settings.MaxIterations = 100;
|
||||
|
||||
// 1 cent commission for trade
|
||||
settings.CommissionRules = new ICommissionRule[]
|
||||
{
|
||||
settings.CommissionRules =
|
||||
[
|
||||
new CommissionTradeRule { Value = 0.01m },
|
||||
};
|
||||
];
|
||||
|
||||
// count of parallel testing strategies
|
||||
// if not set, then CPU count * 2
|
||||
|
||||
Reference in new issue
Block a user