mirror of
https://github.com/StockSharp/StockSharp.git
synced 2026-12-05 09:02:09 +00:00
Instrument lookups, configuration, credentials, CSV entities, snapshots and the alert processing service are read and written asynchronously; the blocking synchronous forms are obsolete wrappers. IAsyncPersistable replaces IPersistable: every type that saves and loads its settings has LoadAsync and SaveAsync only, and nested objects are read with GetValueAsync, LoadEntireAsync and LoadAllAsync. The synchronous Load and Save are the obsolete Ecng extensions. The MultiConnect sample keeps IPersistable for two settings classes, because the settings window of the published StockSharp.Xaml package takes it. Copies that were made by saving and loading are made asynchronously. IMessageAdapter, IIndicator, the risk, commission, PnL, latency, slippage and position managers, IStorageBuffer, Strategy, DiagramElement, ICompositionModel and its nodes get CloneAsync and no longer implement ICloneable; Clone is an obsolete blocking wrapper. A strategy copy keeps every security and portfolio parameter the original holds. Messages, FieldMapping, BankDetails and WithdrawInfo are copied in memory. A CommissionRuleMessage shares its rule and the emulator takes a copy of it. Envelope gets a constructor that takes all three lines and CreateAsync. ToBruteForce and ToBruteForceRandom become ToBruteForceAsync and ToBruteForceRandomAsync and return IAsyncEnumerable; BruteForceOptimizer runs over it and takes the next strategy through an asynchronous delegate. Diagram elements keep their own state in SaveState, OnLoading and LoadState. Undo restores an in-memory snapshot; a chart element holds its series and axes in it by reference. An alert rule keeps its value in the form a message carries it. A diagram strategy applies composition settings that were loaded before the composition was set in SetCompositionAsync or at the start, and saves them while they wait. Strategies get an OnStartedAsync hook; diagram strategies resolve securities, underlyings and strikes before the start so value processing never looks up instruments. Paths.Serialize, SerializeInvariant, AppStartSettings.TrySave, ICredentialsProvider.Save, ISnapshotSerializer.Serialize and the composition registry gain async forms. ICredentialsProvider.Delete becomes DeleteAsync, so loading, saving and deleting the credentials wait for one another. Paths.InstalledVersion and AppNameWithVersion are obsolete in favour of GetInstalledVersionAsync and GetAppNameWithVersionAsync. A file is opened for writing only after the last cancellation check, because opening it empties it; a cancelled read reports the cancellation instead of an empty result. A snapshot flush that failed leaves what it took due for the next one. The indicator templates for C#, F# and Python override LoadAsync and SaveAsync.
262 lines
6.6 KiB
C#
262 lines
6.6 KiB
C#
namespace StockSharp.Tests;
|
|
|
|
using StockSharp.Algo.Strategies;
|
|
|
|
[TestClass]
|
|
public class StrategyNameGeneratorTests : BaseTestClass
|
|
{
|
|
private class MyTestStrategy : Strategy { }
|
|
|
|
private class SmaStrategy : Strategy { }
|
|
|
|
private class BollingerBandsMeanReversionStrategy : Strategy { }
|
|
|
|
[TestMethod]
|
|
public void DefaultPattern_ShortName()
|
|
{
|
|
var s = new MyTestStrategy();
|
|
// ShortName = upper-case letters of "MyTestStrategy" => "MTS"
|
|
s.NameGenerator.ShortName.AssertEqual("MTS");
|
|
s.Name.AssertEqual("MTS");
|
|
}
|
|
|
|
[TestMethod]
|
|
public void DefaultPattern_SmaStrategy()
|
|
{
|
|
var s = new SmaStrategy();
|
|
s.NameGenerator.ShortName.AssertEqual("SS");
|
|
s.Name.AssertEqual("SS");
|
|
}
|
|
|
|
[TestMethod]
|
|
public void DefaultPattern_LongName()
|
|
{
|
|
var s = new BollingerBandsMeanReversionStrategy();
|
|
s.NameGenerator.ShortName.AssertEqual("BBMRS");
|
|
s.Name.AssertEqual("BBMRS");
|
|
}
|
|
|
|
[TestMethod]
|
|
public void FullNamePattern()
|
|
{
|
|
var s = new MyTestStrategy();
|
|
s.NameGenerator.Pattern = "{FullName}";
|
|
s.Name.AssertEqual("MyTestStrategy");
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ShortNamePattern()
|
|
{
|
|
var s = new MyTestStrategy();
|
|
s.NameGenerator.Pattern = "{ShortName}";
|
|
s.Name.AssertEqual("MTS");
|
|
}
|
|
|
|
[TestMethod]
|
|
public void CustomPattern_WithSecurity()
|
|
{
|
|
var s = new MyTestStrategy();
|
|
|
|
var security = Helper.CreateStorageSecurity();
|
|
s.Security = security;
|
|
|
|
s.NameGenerator.Pattern = "{ShortName}{Security:_{0.Security}|}";
|
|
|
|
s.Name.Contains("MTS").AssertTrue();
|
|
s.Name.Contains(security.Id).AssertTrue();
|
|
}
|
|
|
|
[TestMethod]
|
|
public void CustomPattern_WithPortfolio()
|
|
{
|
|
var s = new MyTestStrategy();
|
|
|
|
s.Portfolio = new Portfolio { Name = "TestPortfolio" };
|
|
|
|
s.NameGenerator.Pattern = "{ShortName}{Portfolio:_{0.Portfolio}|}";
|
|
|
|
s.Name.AssertEqual("MTS_TestPortfolio");
|
|
}
|
|
|
|
[TestMethod]
|
|
public void CustomPattern_WithSecurityAndPortfolio()
|
|
{
|
|
var s = new MyTestStrategy();
|
|
|
|
var security = Helper.CreateStorageSecurity();
|
|
s.Security = security;
|
|
s.Portfolio = new Portfolio { Name = "MyAccount" };
|
|
|
|
// default pattern
|
|
s.NameGenerator.Pattern = "{ShortName}{Security:_{0.Security}|}{Portfolio:_{0.Portfolio}|}";
|
|
|
|
s.Name.Contains("MTS").AssertTrue();
|
|
s.Name.Contains(security.Id).AssertTrue();
|
|
s.Name.Contains("MyAccount").AssertTrue();
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ManualName_DisablesAutoGeneration()
|
|
{
|
|
var s = new MyTestStrategy();
|
|
s.Name.AssertEqual("MTS");
|
|
|
|
s.Name = "CustomName";
|
|
s.NameGenerator.AutoGenerateStrategyName.AssertFalse();
|
|
s.Name.AssertEqual("CustomName");
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ManualName_IgnoresPatternChange()
|
|
{
|
|
var s = new MyTestStrategy();
|
|
s.Name = "CustomName";
|
|
|
|
// changing pattern should NOT regenerate since auto is off
|
|
s.NameGenerator.Pattern = "{FullName}";
|
|
s.Name.AssertEqual("CustomName");
|
|
}
|
|
|
|
[TestMethod]
|
|
public void AutoGeneration_ReenableAfterManual()
|
|
{
|
|
var s = new MyTestStrategy();
|
|
s.Name = "CustomName";
|
|
|
|
s.NameGenerator.AutoGenerateStrategyName = true;
|
|
s.NameGenerator.Pattern = "{FullName}";
|
|
|
|
s.Name.AssertEqual("MyTestStrategy");
|
|
}
|
|
|
|
[TestMethod]
|
|
public void PatternChange_RegeneratesName()
|
|
{
|
|
var s = new MyTestStrategy();
|
|
s.Name.AssertEqual("MTS");
|
|
|
|
s.NameGenerator.Pattern = "{FullName}";
|
|
s.Name.AssertEqual("MyTestStrategy");
|
|
|
|
s.NameGenerator.Pattern = "{ShortName}";
|
|
s.Name.AssertEqual("MTS");
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ChangedEvent_Fires()
|
|
{
|
|
var s = new MyTestStrategy();
|
|
var names = new List<string>();
|
|
s.NameGenerator.Changed += n => names.Add(n);
|
|
|
|
s.NameGenerator.Pattern = "{FullName}";
|
|
|
|
names.Count.AssertEqual(1);
|
|
names[0].AssertEqual("MyTestStrategy");
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Dispose_UnsubscribesFromPropertyChanged()
|
|
{
|
|
var s = new MyTestStrategy();
|
|
var names = new List<string>();
|
|
s.NameGenerator.Changed += n => names.Add(n);
|
|
|
|
s.NameGenerator.Dispose();
|
|
|
|
// after dispose, changing security should not trigger name change
|
|
var countBefore = names.Count;
|
|
s.Security = Helper.CreateStorageSecurity();
|
|
|
|
names.Count.AssertEqual(countBefore);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void UnderscoreInPattern()
|
|
{
|
|
var s = new MyTestStrategy();
|
|
s.NameGenerator.Pattern = "{ShortName}_v2";
|
|
s.Name.AssertEqual("MTS_v2");
|
|
}
|
|
|
|
[TestMethod]
|
|
public void StaticTextPattern()
|
|
{
|
|
var s = new MyTestStrategy();
|
|
s.NameGenerator.Pattern = "Hello World";
|
|
s.Name.AssertEqual("Hello World");
|
|
}
|
|
|
|
[TestMethod]
|
|
public void EmptySecurity_NoSuffix()
|
|
{
|
|
var s = new MyTestStrategy();
|
|
// default pattern includes conditional Security part
|
|
s.NameGenerator.Pattern = "{ShortName}{Security:_{0.Security}|}";
|
|
|
|
// no security set — conditional block should produce empty
|
|
s.Name.AssertEqual("MTS");
|
|
}
|
|
|
|
[TestMethod]
|
|
public void SecurityChange_UpdatesName()
|
|
{
|
|
var s = new MyTestStrategy();
|
|
s.NameGenerator.Pattern = "{ShortName}{Security:_{0.Security}|}";
|
|
|
|
s.Name.AssertEqual("MTS");
|
|
|
|
var security = Helper.CreateStorageSecurity();
|
|
s.Security = security;
|
|
|
|
s.Name.Contains(security.Id).AssertTrue();
|
|
}
|
|
|
|
/// <summary>
|
|
/// A name the caller gave the strategy is the caller's, and a saved strategy comes back under it.
|
|
/// Lost on the round trip, the strategy a user named and put away is not the one they open: it
|
|
/// comes back named after whatever instrument and portfolio it happens to hold.
|
|
/// </summary>
|
|
[TestMethod]
|
|
public async Task ANameTheCallerGaveSurvivesTheRoundTrip()
|
|
{
|
|
var s = new MyTestStrategy
|
|
{
|
|
Security = Helper.CreateStorageSecurity(),
|
|
Portfolio = Portfolio.CreateSimulator(),
|
|
Name = "Overnight carry",
|
|
};
|
|
|
|
var storage = await s.SaveAsync(CancellationToken);
|
|
|
|
var restored = new MyTestStrategy();
|
|
await restored.LoadAsync(storage, CancellationToken);
|
|
|
|
// Assigned after the load, as a strategy being put back to work is: the generator must not
|
|
// take the name back off a strategy that was named by hand.
|
|
restored.Security = s.Security;
|
|
restored.Portfolio = s.Portfolio;
|
|
|
|
restored.Name.AssertEqual("Overnight carry",
|
|
"the name the caller gave was not saved, so the strategy comes back named after its instrument and portfolio");
|
|
}
|
|
|
|
/// <summary>
|
|
/// And the other half of it: a strategy nobody named keeps generating its own name after a round
|
|
/// trip, rather than freezing on whatever it was called when it was put away.
|
|
/// </summary>
|
|
[TestMethod]
|
|
public async Task AStrategyNobodyNamedKeepsGeneratingAfterTheRoundTrip()
|
|
{
|
|
var s = new MyTestStrategy();
|
|
|
|
var restored = new MyTestStrategy();
|
|
await restored.LoadAsync(await s.SaveAsync(CancellationToken), CancellationToken);
|
|
|
|
restored.Security = Helper.CreateStorageSecurity();
|
|
|
|
restored.Name.Contains(restored.Security.Id).AssertTrue(
|
|
"nobody named this strategy, so its name still follows what it trades");
|
|
}
|
|
}
|