Skip to content

Commit 039ef98

Browse files
committed
Fix Strategy.CopyTo: Load() was overwriting Security/Portfolio
Security and Portfolio were set before Load(Save()), so deserialization wiped them with empty values. Moved assignments after Load. Added Clone_PreservesSecurityAndPortfolio test.
1 parent 236acaa commit 039ef98

2 files changed

Lines changed: 31 additions & 4 deletions

File tree

Algo.Strategies/Strategy.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2862,15 +2862,16 @@ protected virtual void CopyTo(Strategy copy)
28622862
if (copy is null)
28632863
throw new ArgumentNullException(nameof(copy));
28642864

2865+
var id = copy.Id;
2866+
copy.Load(this.Save());
2867+
copy.Id = id;
2868+
2869+
// set after Load to avoid being overwritten by deserialization
28652870
//copy.Connector = Connector;
28662871
copy.Security = Security;
28672872
copy.Portfolio = Portfolio;
28682873
copy.PortfolioProvider = PortfolioProvider;
28692874

2870-
var id = copy.Id;
2871-
copy.Load(this.Save());
2872-
copy.Id = id;
2873-
28742875
copy.Environment.AddRange(Environment);
28752876
}
28762877

Tests/StrategyParamTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
namespace StockSharp.Tests;
22

3+
using StockSharp.Designer;
4+
35
[TestClass]
46
public class StrategyParamTests : BaseTestClass
57
{
@@ -799,6 +801,30 @@ public void Step_Decimal_CommaCulture()
799801
}
800802
}
801803

804+
[TestMethod]
805+
public void Clone_PreservesSecurityAndPortfolio()
806+
{
807+
var security = new Security { Id = "AAPL@NASDAQ", PriceStep = 0.01m };
808+
var portfolio = Portfolio.CreateSimulator();
809+
810+
var original = new SmaStrategy
811+
{
812+
Security = security,
813+
Portfolio = portfolio,
814+
Volume = 1,
815+
CandleType = TimeSpan.FromMinutes(5).TimeFrame(),
816+
Long = 80,
817+
Short = 30,
818+
};
819+
820+
var clone = (SmaStrategy)original.Clone();
821+
822+
AreEqual(security.Id, clone.Security?.Id, "Security must survive Clone");
823+
AreEqual(portfolio.Name, clone.Portfolio?.Name, "Portfolio must survive Clone");
824+
AreEqual(80, clone.Long, "Long param must survive Clone");
825+
AreEqual(30, clone.Short, "Short param must survive Clone");
826+
}
827+
802828
private class TestStrategy : Strategy
803829
{
804830
// Simple test strategy for testing parameter functionality

0 commit comments

Comments
 (0)