| name | futures |
| description | Use whenever a strategy trades futures — position sizing (notional/contracts), continuous-contract history and its py`MultiIndex DataFrame`cs`IEnumerable<TradeBar>`, and warm-up. Load it for any futures sizing, history, or warm-up step. |
Futures: sizing, history, warm-up
Position sizing (notional, not pyset_holdingscsSetHoldings)
To take a target notional (portfolio-fraction) exposure in a future, do NOT use pyset_holdingscsSetHoldings: for a future it sizes off (already-leveraged) buying power, so a target near 1.0 takes on many times 1x notional — margin calls and blow-ups — and |target| > 1 is rejected outright. Also, the continuous/canonical future symbol (e.g. /ES) is a DATA symbol and is NOT tradable — pymarket_ordercsMarketOrder or portfolio reads on it silently do nothing, so symbol below must be a real contract (the front month via pyfuture.mappedcsfuture.Mapped, or the specific contract the strategy selects). Size by notional with explicit contract math:
price = self.securities[symbol].price
multiplier = self.securities[symbol].symbol_properties.contract_multiplier
target_contracts = round(self.portfolio.total_portfolio_value * target_weight / (price * multiplier))
delta = target_contracts - self.portfolio[symbol].quantity; self.market_order(symbol, delta)
var price = Securities[symbol].Price;
var multiplier = Securities[symbol].SymbolProperties.ContractMultiplier;
var targetContracts = Math.Round(Portfolio.TotalPortfolioValue * targetWeight / (price * multiplier));
var delta = targetContracts - Portfolio[symbol].Quantity; MarketOrder(symbol, delta);
History — use self.history(); do NOT rebuild a DataFrame from TradeBars
Seeding a raw look-back buffer (e.g. an expanding regression window) is a HISTORY REQUEST, not a warm-up (warm-up pumps data through on_data and updates indicators; a history request hands you a DataFrame to process yourself).
History — use History<TradeBar>(); do NOT hand-build any other structure from the bars
Seeding a raw look-back buffer (e.g. an expanding regression window) is a HISTORY REQUEST, not a warm-up (warm-up pumps data through OnData and updates indicators; a history request hands you the bars to process yourself).
- Request continuous-contract history on the canonical py
future.symbolcsfuture.Symbol. Roll behavior is set by the pydata_mapping_modecsdataMappingMode / pydata_normalization_modecsdataNormalizationMode / pycontract_depth_offsetcscontractDepthOffset passed to pyadd_futurecsAddFuture (e.g. pyDataNormalizationMode.BACKWARDS_RATIOcsDataNormalizationMode.BackwardsRatio for a roll-adjusted price/return series).
self.history(symbol, n, Resolution.DAILY) ALREADY returns a DataFrame — do NOT iterate self.history[TradeBar](...) and build one by hand. For a future it comes back MultiIndexed by (expiry, symbol, time); get a clean time-indexed series by dropping the leading levels:
closes = self.history(symbol, n, Resolution.DAILY)["close"].droplevel(["expiry", "symbol"])
then group/resample by month on that Series. (Do NOT use history.loc[symbol] — that indexes the expiry level and fails.)
History<TradeBar>(symbol, n, Resolution.Daily) ALREADY yields the bars directly — iterate them and read the fields you need (e.g. bar.Close); do NOT hand-build any other structure from them.
- A request can return fewer bars than asked when available history is limited; check the row count and widen the lookback if short.
- A request can return fewer bars than asked when available history is limited; check the bar count and widen the lookback if short.
Warm-up (for indicators, not raw buffers)
To ready indicators before the start date: pyset_warm_up(n[, resolution])csSetWarmUp(n[, resolution]) or pyset_warm_up(timedelta(...))csSetWarmUp(TimeSpan.FromDays(...)), gated with pyif self.is_warming_up: returncsif (IsWarmingUp) return;; or pywarm_up_indicator(symbol, indicator[, resolution])csWarmUpIndicator(symbol, indicator[, resolution]) / pysettings.automatic_indicator_warm_upcsSettings.AutomaticIndicatorWarmUp / pyindicator_history(indicator, symbol, n, resolution)csIndicatorHistory(indicator, symbol, n, resolution). After warming, check readiness and widen the lookback if short.