Cosine
systemic:core-cosMathInputs
| Name | Type | Description |
|---|---|---|
| Data | Number series | A series of angles in radians; the module takes the cosine of each value. |
Outputs
| Name | Type | Description |
|---|---|---|
| Cos | Number series | The cosine of each input angle, a value from -1 to +1. |
Cost
0.199 + 0.00569·n credits for n bars, rounded up. The marks are each plan's limit of bars.
The Cosine module computes the cosine of each input value, read as an angle in radians.
The Formula
For each input value , the output is:
The cosine function maps an angle to the x-coordinate of the corresponding point on the unit circle. This simple operation is useful in finance for encoding cyclical patterns and analyzing market phases.
The Unit Circle: Cosine's Geometric Heart
The unit circle—a circle with radius 1 centered at the origin—provides the most intuitive way to understand cosine. As an angle sweeps counterclockwise from the positive x-axis, the cosine value equals the x-coordinate of the point where the angle's terminal side intersects the circle.
| Angle (radians) | Angle (degrees) | Cosine Value | Position on Circle |
|---|---|---|---|
| 0° | +1.0 | Rightmost point (3 o'clock) | |
| 90° | 0.0 | Top (12 o'clock) | |
| 180° | -1.0 | Leftmost point (9 o'clock) | |
| 270° | 0.0 | Bottom (6 o'clock) | |
| 360° | +1.0 | Back to start |
Key Mathematical Properties
| Property | Description |
|---|---|
| Period | radians (360°) — the function repeats every full rotation |
| Range | — output is always bounded |
| Even Function | — symmetric about the y-axis |
| Continuous | Infinitely differentiable, producing smooth transitions |
| Phase Relationship | — leads sine by 90° |
Critical Note: Radians, Not Degrees
⚠️ The Cosine module interprets all input values as radians.
If your data is in degrees, convert it first using the formula:
| Degrees | Radians | Cosine |
|---|---|---|
| 0° | 0 | 1.0 |
| 45° | 0.707 | |
| 60° | 0.5 | |
| 90° | 0.0 | |
| 180° | -1.0 |
Creative Applications in Finance
1. Cyclical Time Encoding for Machine Learning
Financial markets show cyclical patterns: daily trading sessions, weekly flows (Fridays often see different behavior than Mondays), monthly portfolio rebalancing, and annual tax-driven effects like the January Effect.
Traditional approaches encode time features as categorical variables (e.g., "January", "February"...), but this creates an artificial discontinuity: December 31 appears completely unrelated to January 1, when in reality they're adjacent points in a continuous cycle.
Cosine encoding solves this. By transforming time into an angle and taking its cosine (paired with sine), we create a smooth, continuous representation where December 31 naturally connects to January 1.
The Encoding Formula
For any cyclical feature with period :
Then apply cosine (and sine for the complementary component):
| Time Feature | Period () | Conversion |
|---|---|---|
| Hour of day | 24 | |
| Day of week | 7 | |
| Month of year | 12 | |
| Day of year | 365 |
Why This Matters
Research from NVIDIA demonstrates that cyclical sine/cosine encoding captures temporal patterns that traditional dummy variables miss. When building predictive models for asset prices or trading signals, this encoding:
- Preserves the continuity between cycle endpoints
- Reduces feature dimensionality (2 features vs. 12 for months)
- Works naturally with linear models and neural networks
- Captures where you are within any repeating cycle
2. Bitcoin's Four-Year Halving Cycle
One of the most documented cycles in cryptocurrency markets is Bitcoin's approximately four-year halving cycle. Every 210,000 blocks (roughly 4 years), the block reward is cut in half, creating a supply shock that has historically preceded major bull runs.
Using cosine encoding, we can represent where we are within this cycle and potentially identify whether we're in an "accumulation" phase (cycle trough) or "distribution" phase (cycle peak).
The halving dates are:
- November 28, 2012 — Block reward: 50 → 25 BTC
- July 9, 2016 — Block reward: 25 → 12.5 BTC
- May 11, 2020 — Block reward: 12.5 → 6.25 BTC
- April 19, 2024 — Block reward: 6.25 → 3.125 BTC
xychart-beta
title "Bitcoin Monthly Prices (2020-2025)"
x-axis ["Jan20", "Apr20", "Jul20", "Oct20", "Jan21", "Apr21", "Jul21", "Oct21", "Jan22", "Apr22", "Jul22", "Oct22", "Jan23", "Apr23", "Jul23", "Oct23", "Jan24", "Apr24", "Jul24", "Oct24", "Jan25"]
y-axis "Price (USD)" 0 --> 130000
bar [9336, 8607, 11335, 13795, 33136, 57758, 41701, 61340, 38472, 37660, 23314, 20504, 23153, 29311, 29226, 34650, 42611, 60762, 64666, 70340, 102411]
The May 2020 halving (at approximately $9,000) was followed by an 11-month climb to all-time highs above $60,000. By encoding time since the last halving as a cosine value, models can learn this cyclical relationship without memorizing specific dates.
💡 Experiment Idea: Create a strategy that combines Bitcoin price with a halving cycle phase indicator (using cosine of the days-since-halving angle). Observe whether extremes in the phase indicator correlate with major market tops or bottoms.
3. Fourier Decomposition: The Building Blocks of Price Movement
A key result from mathematics is Fourier's theorem: any periodic signal can be decomposed into a sum of sines and cosines of different frequencies.
In principle, a price series could be viewed as a combination of:
- Long-term cycles (secular bull/bear markets)
- Medium-term cycles (business cycles, months)
- Short-term cycles (weekly patterns, daily volatility)
- Noise (random fluctuations)
flowchart LR
subgraph "Price Signal Decomposition"
A[Raw Price Data] --> B[Trend Component]
A --> C["Long Cycle<br/>cos(annual phase)"]
A --> D["Medium Cycle<br/>cos(monthly phase)"]
A --> E["Short Cycle<br/>cos(weekly phase)"]
A --> F[Residual Noise]
end
B --> G[Reconstructed<br/>Smooth Signal]
C --> G
D --> G
E --> G
While Fourier analysis has mixed results for price prediction (markets are too noisy and non-stationary), understanding this framework helps explain why cyclical encoding works: you're essentially providing the model with the phase information for known, real-world cycles (time of day, day of week, etc.) rather than trying to discover unknown cycles in noisy data.
📚 Further Study: John Ehlers' work on Digital Signal Processing in trading provides deeper exploration of these concepts, including the Hilbert Transform for phase detection.
Pairing Cosine with Sine: Complete Circular Representation
A critical insight: cosine alone cannot uniquely identify a position within a cycle. Consider that , yet these represent completely different positions on the unit circle.
The solution is to use both cosine and sine together, creating a 2D representation where every point in the cycle maps to a unique coordinate:
flowchart TD
subgraph "Why You Need Both"
A["θ = π/3<br/>(60°)"] --> B["cos = 0.5<br/>sin = 0.87"]
C["θ = 5π/3<br/>(300°)"] --> D["cos = 0.5<br/>sin = -0.87"]
end
B --> E["Upper right<br/>of circle"]
D --> F["Lower right<br/>of circle"]
G["Same cosine value<br/>but different positions!"] -.-> A
G -.-> C
When encoding cyclical features for machine learning:
- Use the Cosine module for the x-component
- Use the Sine module for the y-component
- Connect both to the same angle input
This creates what researchers call a "circular embedding" that machine learning models can interpret correctly.
Input Behavior in Systemic
The Cosine module has one input. The output has one value for each input bar.
Example: If the angle comes from a daily time-phase series that is repeated across hourly bars, the same value appears for each hour until the next day's value.
For the Cosine module, this means:
- A constant angle gives a constant cosine
- Phase values from a slower series "step" through the day rather than change smoothly
- Compute time phases at the same period as your main data for smoother results
Missing Data
When an input value is missing (empty) — for example, a stock series has no bar on a weekend or holiday — the output at that bar is empty too. Gaps are never filled: every output value is the cosine of a real input value. The output keeps the same length as the input, so every value stays on its own bar.
Handling Invalid Inputs
An infinite input (±∞) has no cosine — the function keeps oscillating and never settles as the angle grows without bound. This differs from missing data, where there is no value at all. Both give an empty output, but they mean different things:
- Missing input → empty: there was no value to compute from
- ±Infinity input → empty: there was a value, but its cosine is undefined
Every finite angle, however large, produces a valid cosine (effectively reduced modulo ).
Edge Cases and Special Values
| Input | Cosine Output | Notes |
|---|---|---|
| 0 | 1.0 | Starting position |
| Missing | Empty | Gap kept at the same bar — never filled |
| ±Infinity | Empty | Cosine is undefined for an infinite angle |
| Very large numbers | Valid result | Effectively reduced modulo |
| Negative angles | Same as positive | Even function symmetry |
Further Reading
- Trigonometric Functions — Mathematical foundations
- Fourier Analysis in Finance — Time series decomposition
- Cyclical Encoding for Time Series — NVIDIA technical guide
- Digital Signal Processing in Trading — Phase detection and cycle analysis
- Bitcoin Halving Cycles — Understanding crypto seasonality