Option Alpha Docs
  • Intro to Option Alpha
  • TOOLS
    • Bots
      • Creating a Bot
      • Safeguards
      • Automation Basics
      • Using Loops
      • Decisions
      • Automated Trades
      • SmartPricing
      • Exit Options
      • Using Tags
      • Using Inputs
      • Automation Scheduling
      • Bot Logs
      • Templates and Cloning
      • Managing Positions
      • Autotrading Best Practices
    • Trade Ideas
      • Manual Trading
    • 0DTE Oracle
    • Earnings Edge
    • Settings
      • Webhooks
      • Live Trading
        • Connecting to Tradier
        • Connecting to tastytrade
        • Connecting to TradeStation
        • Connecting to Schwab
  • PLATFORM
    • Infrastructure & Security
    • Automations
      • Automation Behavior
    • Data Feeds
    • Order Handling
    • Bot Limitations
    • Supported Ticker Symbols
      • Supported Browsers
      • Supported Countries
    • Ex Dividend & Earnings Dates
    • Troubleshooting
      • Testing Automations
      • Broker Rejection Errors
        • Invalid Authorization
        • Overlapping Strikes Failsafe
        • Duplicate Orders Error
      • Capital Warnings
      • Position Limit Warnings
      • Trade Enforcements
      • Pricing Anomaly Warning
      • Missing or Invalid Input
      • Daily Symbol Limit Error
      • Excessive Errors Failsafe
      • Bot Event Loops
      • Option & Expiration Availability
  • Calculations
    • Profit and Loss
    • Decision Properties
    • Decision Calculations
    • Parameter Selection
    • Probability
      • Probability Theory
      • Understanding Alpha and Expected Value
    • Indicators
      • ADX
      • ATR
      • BOLLINGER BANDS
      • BOP
      • CCI
      • CMO
      • DX
      • EMA
      • KAMA
      • MACD
      • MFI
      • MOM
      • ROC
      • RSI
      • SMA
      • STOCH
      • Stoch RSI
      • TRIMA
      • ULTIMATE OSCILLATOR
      • WILLIAMS %R
  • Resources
    • 'Fast Track' Video Series
    • Live & On-Demand Events
    • Videos
    • Education
    • Blog
    • Podcast
Powered by GitBook
On this page

Was this helpful?

  1. Calculations
  2. Indicators

TRIMA

Overview of the Triangular Moving Average calculation.

PreviousStoch RSINextULTIMATE OSCILLATOR

Last updated 4 years ago

Was this helpful?

The Triangular Moving Average (TRIMA) represents an average of prices but places weight on the middle prices of the time period. The calculations double-smooths the data using a window width that is one-half the length of the series.

The source code for the TRIMA function is available .

/* TRIMA Description
 * =================
 * The triangular MA is a weighted moving average. Instead of the
 * TA_WMA who put more weigth on the latest price bar, the triangular
 * put more weigth on the data in the middle of the specified period.
 *
 * Examples:
 *   For TimeSerie={a,b,c,d,e,f...} ('a' is the older price)
 *
 *   1st value for TRIMA 4-Period is:  ((1*a)+(2*b)+(2*c)+(1*d)) / 6
 *   2nd value for TRIMA 4-Period is:  ((1*b)+(2*c)+(2*d)+(1*e)) / 6
 *
 *   1st value for TRIMA 5-Period is:  ((1*a)+(2*b)+(3*c)+(2*d)+(1*e)) / 9
 *   2nd value for TRIMA 5-Period is:  ((1*b)+(2*c)+(3*d)+(2*e)+(1*f)) / 9
 *
 * Generally Accepted Implementation
 * ==================================
 * Using algebra, it can be demonstrated that the TRIMA is equivalent to
 * doing a SMA of a SMA. The following explain the rules:
 *
 *  (1) When the period is even, TRIMA(x,period)=SMA(SMA(x,period/2),(period/2)+1)
 *  (2) When the period is odd,  TRIMA(x,period)=SMA(SMA(x,(period+1)/2),(period+1)/2)
 *
 * In other words:
 *  (1) A period of 4 becomes TRIMA(x,4) = SMA( SMA( x, 2), 3 )
 *  (2) A period of 5 becomes TRIMA(x,5) = SMA( SMA( x, 3), 3 )
 *
 * The SMA of a SMA is the algorithm generaly found in books.
 *
 * Tradestation Implementation
 * ===========================
 * Tradestation deviate from the generally accepted implementation by
 * making the TRIMA to be as follow:
 *    TRIMA(x,period) = SMA( SMA( x, (int)(period/2)+1), (int)(period/2)+1 );
 * This formula is done regardless if the period is even or odd.
 *
 * In other word:
 *  (1) A period of 4 becomes TRIMA(x,4) = SMA( SMA( x, 3), 3 )
 *  (2) A period of 5 becomes TRIMA(x,5) = SMA( SMA( x, 3), 3 )
 *  (3) A period of 6 becomes TRIMA(x,5) = SMA( SMA( x, 4), 4 )
 *  (4) A period of 7 becomes TRIMA(x,5) = SMA( SMA( x, 4), 4 )
 *
 * It is not clear to me if the Tradestation approach is a bug or a deliberate
 * decision to do things differently.
 *    
 * Metastock Implementation
 * ========================
 * Output is the same as the generally accepted implementation.
 *
 * TA-Lib Implementation
 * =====================
 * Output is also the same as the generally accepted implementation.
 *
 * For speed optimization and avoid memory allocation, TA-Lib use
 * a better algorithm than the usual SMA of a SMA.
 *
 * The calculation from one TRIMA value to the next is done by doing 4
 * little adjustment (the following show a TRIMA 4-period):
 *
 * TRIMA at time 'd': ((1*a)+(2*b)+(2*c)+(1*d)) / 6
 * TRIMA at time 'e': ((1*b)+(2*c)+(2*d)+(1*e)) / 6
 * 
 * To go from TRIMA 'd' to 'e', the following is done:
 *       1) 'a' and 'b' are substract from the numerator.
 *       2) 'd' is added to the numerator.
 *       3) 'e' is added to the numerator.
 *       4) Calculate TRIMA by doing numerator / 6
 *       5) Repeat sequence for next output
 *
 * These operations are the same steps done by TA-LIB:
 *       1) is done by numeratorSub
 *       2) is done by numeratorAdd.
 *       3) is obtain from the latest input
 *       4) Calculate and write TRIMA in the output
 *       5) Repeat for next output.
 *
 * Of course, numerotrAdd and numeratorSub needs to be
 * adjusted for each iteration.
 *
 * The update of numeratorSub needs values from the input at
 * the trailingIdx and middleIdx position.
 *
 * The update of numeratorAdd needs values from the input at
 * the middleIdx and todayIdx.
 */
here