Skip to content

Commit a25e041

Browse files
JosueNinavb-vlb
andauthored
Follow up: Fix weights calculation in VBaseSignalExport (#9281)
* Fix weights calculation in VBaseSignalExport * fix empty portfolio handling * Refactored signal export to compute and export weights for all portfolio positions, not just update targets. * Update BuildCsv XML doc to clarify CSV output details * Clarify comments on portfolio value and empty weights * Stamp targets as is; we expect that users send all the weights they want to stamp. Extended the demo algorithm to use two positions to better demonstrate the intended usage of the VBaseSignalExport class. * Minor changes to address PR review comments. * Minor fix --------- Co-authored-by: vb-vlb <volodymyr@vbase.com> Co-authored-by: Volodymyr <vb-vlb@users.noreply.github.com>
1 parent dfa3fd3 commit a25e041

3 files changed

Lines changed: 20 additions & 19 deletions

File tree

Algorithm.CSharp/VBaseSignalExportDemonstrationAlgorithm.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ public class VBaseSignalExportDemonstrationAlgorithm : QCAlgorithm, IRegressionA
3535
private bool _sentSignal;
3636
private List<Symbol> _symbols = new()
3737
{
38-
QuantConnect.Symbol.Create("SPY", SecurityType.Equity, Market.USA)
38+
QuantConnect.Symbol.Create("SPY", SecurityType.Equity, Market.USA),
39+
QuantConnect.Symbol.Create("IBM", SecurityType.Equity, Market.USA),
3940
};
4041

41-
4242
/// <summary>
4343
/// Stamping of predefined portfolio targets
4444
/// </summary>
@@ -65,11 +65,12 @@ public override void OnData(Slice slice)
6565
}
6666
_sentSignal = true;
6767

68-
var targets = new PortfolioTarget[_symbols.Count];
69-
for (var index = 0; index < _symbols.Count; index++)
68+
var targets = new[]
7069
{
71-
targets[index] = new PortfolioTarget(_symbols[index], (decimal)0.25);
72-
}
70+
new PortfolioTarget(_symbols[0], 0.25m), // 0.25 of the portfolio in SPY
71+
new PortfolioTarget(_symbols[1], 0.75m) // 0.75 of the portfolio in IBM
72+
};
73+
7374
SignalExport.SetTargetPortfolio(targets);
7475
}
7576

@@ -86,7 +87,7 @@ public override void OnData(Slice slice)
8687
/// <summary>
8788
/// Data Points count of all timeslices of algorithm
8889
/// </summary>
89-
public long DataPoints => 3943;
90+
public long DataPoints => 7843;
9091

9192
/// <summary>
9293
/// Data Points count of the algorithm history

Algorithm.Python/VBaseSignalExportDemonstrationAlgorithm.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ def initialize(self):
2929
self.vbase_collection_name = "YOUR VBASE COLLECTION NAME"
3030

3131
self._symbols = [
32-
Symbol.create("SPY", SecurityType.EQUITY, Market.USA)
32+
Symbol.create("SPY", SecurityType.EQUITY, Market.USA),
33+
Symbol.create("IBM", SecurityType.EQUITY, Market.USA)
3334
]
3435

3536
for symbol in self._symbols:
@@ -44,6 +45,8 @@ def on_data(self, data):
4445
self._sentSignal = True
4546

4647
self.targets = []
47-
for symbol in self._symbols:
48-
self.targets.append(PortfolioTarget(symbol, 0.25))
48+
49+
self.targets.append(PortfolioTarget(self._symbols[0], 0.25)) # SPY 25% of the portfolio
50+
self.targets.append(PortfolioTarget(self._symbols[1], 0.75)) # IBM 75% of the portfolio
51+
4952
self.signal_export.set_target_portfolio(self.targets)

Common/Algorithm/Framework/Portfolio/SignalExports/VBaseSignalExport.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
using System.Net.Http.Headers;
2323
using System.Security.Cryptography;
2424
using System.Text;
25+
using System.Threading;
2526

2627
namespace QuantConnect.Algorithm.Framework.Portfolio.SignalExports
2728
{
@@ -118,21 +119,15 @@ public override bool Send(SignalExportTargetParameters parameters)
118119
}
119120

120121
/// <summary>
121-
/// Builds a CSV (sym,wt) for the given targets converting percent holdings into absolute quantity using PortfolioTarget.Percent
122+
/// Builds a CSV (sym,wt) for the given targets
122123
/// </summary>
123124
/// <param name="parameters">Signal export parameters</param>
124125
/// <returns>Resulting CSV string</returns>
125126
protected virtual string BuildCsv(SignalExportTargetParameters parameters)
126127
{
127-
var algorithm = parameters.Algorithm;
128128
var csv = "sym,wt\n";
129129

130-
var targets = parameters.Targets.Select(target =>
131-
PortfolioTarget.Percent(algorithm, target.Symbol, target.Quantity)
132-
)
133-
.Where(absoluteTarget => absoluteTarget != null);
134-
135-
foreach (var target in targets)
130+
foreach (var target in parameters.Targets)
136131
{
137132
csv += $"{target.Symbol.Value},{target.Quantity.ToStringInvariant()}\n";
138133
}
@@ -161,7 +156,7 @@ private bool Stamp(string csv, IAlgorithm algorithm)
161156
};
162157
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _apiKey);
163158

164-
var response = HttpClient.SendAsync(request).Result;
159+
using var response = HttpClient.SendAsync(request).Result;
165160
var body = response.Content.ReadAsStringAsync().Result;
166161
if (!response.IsSuccessStatusCode)
167162
{
@@ -179,3 +174,5 @@ private bool Stamp(string csv, IAlgorithm algorithm)
179174
}
180175
}
181176
}
177+
178+

0 commit comments

Comments
 (0)