Skip to content

Fix weights calculation in VBaseSignalExport#9258

Open
vb-vlb wants to merge 2 commits intoQuantConnect:masterfrom
validityBase:master
Open

Fix weights calculation in VBaseSignalExport#9258
vb-vlb wants to merge 2 commits intoQuantConnect:masterfrom
validityBase:master

Conversation

@vb-vlb
Copy link
Contributor

@vb-vlb vb-vlb commented Feb 2, 2026

Fix weights calculation in VBaseSignalExport.

Description

In the VBaseSignalExport.BuildCsv method, prices were not taken into account when calculating weights.
This change fixes the weight calculation logic.

Related Issue

#9259

Motivation and Context

This change is required because the current implementation of weight calculation is incorrect, as it does not take into account position prices.

Requires Documentation Change

As this change fixes a bug, no documentation changes are needed.

How Has This Been Tested?

Tested using a test algorithm.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • Refactor (non-breaking change which improves implementation)
  • Performance (non-breaking change which improves performance. Please add associated performance test and results)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Non-functional change (xml comments/documentation/etc)

Checklist:

  • My code follows the code style of this project.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • My branch follows the naming convention bug-<issue#>-<description> or feature-<issue#>-<description>

@vb-vlb vb-vlb marked this pull request as ready for review February 2, 2026 17:55
var csv = "sym,wt\n";

var targets = parameters.Targets.Select(target =>
PortfolioTarget.Percent(algorithm, target.Symbol, target.Quantity)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @vb-vlb!
The original implementation is expecting percentages/weights to be passed in, as documented at https://www.quantconnect.com/docs/v2/writing-algorithms/live-trading/signal-exports/vbase#01-Introduction see also example algorithm https://github.com/QuantConnect/Lean/blob/master/Algorithm.Python/VBaseSignalExportDemonstrationAlgorithm.py .
If the API is expecting percentages/weights too this means this implementation shouldn't be doing any math at all but just passing through the values?
Please take a look at SignalExport.SetTargetPortfolioFromPortfolio() too which will call Send on VBaseSignalExport passing percent's too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @Martin-Molinero, thanks for the prompt reply.

In the VBaseSignalExportDemonstrationAlgorithm, the portfolio targets collection consists of a single record: SPY with a quantity of 0.25.
Since we only have one position with a quantity of 0.25, the weights output should consist of a single record with SPY having a weight of 1.
like this
sym,wt
SPY,1

In fact, because PortfolioTarget.Percent creates a new target for the specified percentage, we end up receiving a quantity of SPY that needs to be bought to make it 25% of the portfolio. This results in a stamped weights file like:

sym,wt
SPY,172

the documentation states:

“Under the hood, it uses PortfolioTarget.Percent to convert your absolute target quantities into portfolio weights.”
Which implies that the expected inputs are absolute target quantities, not derived quantities.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the portfolio targets collection consists of a single record: SPY with a quantity of 0.25.
Since we only have one position with a quantity of 0.25, the weights output should consist of a single record with SPY having a weight of 1.

hm don't think this is right really, 0.25 mean 25% if the available portfolio, that shouldn't translate to 1 IMHO. The remaining 75% of the portfolio can be allocated to something else in the next minute for example

the documentation states:

The documentation could be wrong, need an update 💯, but what matters I think is what it should/expected to be by the vbase api and consumer?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants