SDT Shared Variables: Fixed Memory That Saves Resources and Lets Instruments in the Same Strategy Pass Data to Each Other

TL;DR: SDT shared variables take up a fixed amount of memory no matter how many candlesticks there are, which saves resources, improves execution efficiency, and lets instruments in the same strategy pass data to each other; the cost is that you cannot read past values and cannot backtest.

Key ideas

  • Ordinary variables are serialized, so every candlestick needs another slice of memory; SDT just keeps overwriting the fixed positions you declared. In a test with 8 moving-average lines, ordinary variables used 2.5G more and were not fully released after stopping, while SDT used only a bit more than 1G, a large part of which was the candlestick data itself.
  • The cost of SDT is that you cannot use square brackets to get past values, but most cases can be solved with math; the other limitation is that it cannot be backtested. If you do not care about memory and need backtesting or past values, traditional variables are actually better.
  • Usage is like an Excel sheet in memory: row names and column names act as coordinates, you store values with SetValue and read them with GetValue; it is recommended to use text for all coordinates, and to add a default value of 0 when reading so the program does not crash if nothing is found. The table is drawn in memory only as far as it is used, not filled up front.
  • After adding L to a function, all stocks monitored by the same strategy share one table, which is what lets stocks communicate with each other. Practical uses include calculating the market index moving average only once, checking in pairs arbitrage whether the other leg has been filled, and ranking intraday real-time PR values; the cross-strategy version has not been released yet.
量化交易科學園區 - 江加宏Length 49:35Quant Trading Science Park
Episodes
  1. Mon, Nov 28Custom SwingHighBar: Finding Perfect Peaks with Rising Left Side and Falling Right Side
  2. Sat, Dec 31Automatically Detecting an M-Top with XScript: Swing Highs, Neckline and Multiple Filter Conditions
  3. Thu, Mar 16Detecting Volume-Price Divergence with Code: Comparing Price and Volume at Two Swing Highs
  4. Wed, Mar 29Recreating the Day-Trading Three Lines and a Short Alert for Breakouts That Fall Back, Using XS
  5. Tue, Apr 11RSI Bearish Divergence Algorithm and Tolerant Sampling: Syncing Price Highs with Indicator Highs
  6. Thu, Apr 20Measuring Bullish Strength from a Single Candlestick
  7. Fri, Apr 28Manual Entry, Automatic Exit: Using XQ Semi-Automated Trading to Free Up Office Workers
  8. Sun, May 14Automated Trading Without Coding Skills: Split a Strategy into Four Building Blocks of Stock Selection, Entry, Exit and Position Control
  9. Mon, Jun 5Is a High-Volume Long Upper Shadow a Reversal or a Bullish Shakeout? A 3-Year Backtest
  10. Sat, Jun 17Picking Stocks at the Second-Wave Breakout: Big Prior Rally, Low-Volume Consolidation, Then a High-Volume Long Red Candle
  11. Wed, Sep 23Disposition stocks no longer pull back under the new rules: buy those that close strong on day 1 and sell on day 5
  12. Wed, Oct 7A Live Trading Strategy Under the New Disposition Stock Rules: Buy on Day 1, Sell on the Last Day, and Use Stop-Losses
  13. Wed, Oct 21Foreign Capital Rotates Into Defensive Stocks, and a Review of Live Disposition Stock Trades
  14. Wed, Oct 28SDT Shared Variables: Fixed Memory That Saves Resources and Lets Instruments in the Same Strategy Pass Data to Each Other
  15. Wed, Nov 4Disposition Stock Strategy Flips From Bull to Bear: Why It Is Paused When the Market Weakens
  16. Wed, Nov 18Auditing Online Course Strategies with AI: Out-of-Sample Backtesting and Building Your Own XQ Minute-Bar Database
  17. Wed, Nov 25Options Long Straddle Backtest: Trading It Every Day Loses Over the Long Run, With Chances to Profit Only in High-Volatility Periods
▶ Watch the original on YouTube
Ask AI about this episode

Copy the prompt, paste it into an AI service, and add the question. The AI reads the notes on this site and links to the page and the video timestamps.

Or open directly:ChatGPTClaudeGrokPerplexity

Section summaries

  1. 0:05SDT Variables Take Up Just 5 Memory Slots

    No matter how many candlesticks the program runs, SDT takes up only a fixed 5 memory slots, in contrast to ordinary variables, which use more memory as candlesticks increase. The instructor gives the example of storing stock codes and price changes, then ranking them intraday to compute PR values, to show that SDT's first benefit is better execution efficiency.

    ▶ Watch from here
    Slide: SDT Variables Take Up Just 5 Memory Slots
  2. 0:55SDT's Second Benefit: Passing Data Across Instruments

    SDT's second benefit is that it lets different instruments pass data to each other, which the instructor says will be explained later; but first you need to understand that SDT is essentially a kind of variable, so the talk starts with ordinary variables.

    ▶ Watch from here
  3. 1:15Trade-offs Between Ordinary Variables and SDT

    An ordinary variable is like a container: the more candlesticks, the more memory it takes. SDT uses only the fixed slots you declared and does not grow with candlesticks. The cost is that you cannot use square brackets to get past values, but 90% of cases can be solved with math.

    ▶ Watch from here
  4. 10:55Adding L to a Function Lets Stocks Share Data

    Adding L after the function name means that SDT memory position belongs not to a single stock but can be used by all stocks monitored by the same strategy, so stocks can communicate with each other. Currently only 2 kinds are available: a single stock's own variables, and sharing across stocks within the same strategy; the cross-strategy G version has not been released.

    ▶ Watch from here
    Slide: Adding L to a Function Lets Stocks Share Data
  5. 14:45Memory Test Comparing 8 Moving-Average Variables

    In a test of a program that only writes 8 moving-average variables: ordinary variables raised memory from 3.4G to 5.9G, using 2.5G more, and after the program was stopped it was still at 5.6G, so it was not fully released; with SDT, memory rose from about 4.5G to 5.5G, only a bit more than 1G, a large part of which was the candlestick data itself, and the system also ran more smoothly.

    ▶ Watch from here
    Slide: Memory Test Comparing 8 Moving-Average Variables
  6. 18:55SDT Is Like an Excel Sheet in Memory

    SDT is like an Excel sheet in memory, with up to 5000 rows and 100 columns. Row names must be text, while column names can be names or the column number, but using text for all is strongly recommended. Use SDT_SetValue to store values and SDT_GetValue to read them; add a default value of 0 when reading, so that if nothing is found it returns 0 and the program does not crash.

    ▶ Watch from here
    Slide: SDT Is Like an Excel Sheet in Memory
  7. 26:15Single-Instrument Use: Optimizing Efficiency and Saving Resources

    SDT's first use is to optimize program efficiency and save resources for a single instrument; since it does not have to handle large amounts of data, it runs a bit faster. The limitation is that you cannot use square brackets to get past values, but more than 90% of cases can be solved by calculation. The talk then turns to cross-instrument applications within the same strategy.

    ▶ Watch from here
    Slide: Single-Instrument Use: Optimizing Efficiency and Saving Resources
  8. 27:45Demonstration of Sharing Data Across Instruments

    The demonstration uses SDT_SetValue_L to share data across instruments: the market index moving average is calculated once by one stock and the other stocks simply read it; in pairs arbitrage, it checks whether the other instrument has been filled; and it ranks real-time PR values intraday. You can also read a file at startup and write back every 10 seconds to continue calculations such as OBV. However, SDT cannot be backtested, and cross-strategy is not yet supported.

    ▶ Watch from here
    Slide: Demonstration of Sharing Data Across Instruments

This page summarizes the video; it is not an official transcript. Original video: SDT Shared Variables: Fixed Memory That Saves Resources and Lets Instruments in the Same Strategy Pass Data to Each Other (量化交易科學園區 - 江加宏), copyright belongs to the original creator.