#034easyStatistics
Moving Average Crossover Signal
Time Limit: 2sMemory: 256MB
Problem
Implement a Moving Average Crossover signal generator.
Given a series of historical prices and two window sizes (short and long), determine whether the latest price action generates a BUY, SELL, or HOLD signal.
Signal Rules:
- BUY: The short MA just crossed above the long MA (i.e., at the previous step, short MA ≤ long MA, and at the current step, short MA > long MA)
- SELL: The short MA just crossed below the long MA (i.e., at the previous step, short MA ≥ long MA, and at the current step, short MA < long MA)
- HOLD: No crossover occurred (or not enough data)
A Simple Moving Average (SMA) of window at position is the arithmetic mean of prices from index to (inclusive).
The "current" step uses the last price in the array; the "previous" step uses the second-to-last.
Input Format
- Line 1: Three space-separated integers
N sw lw - Line 2: N space-separated floats representing prices
Output Format
Exactly one of: BUY, SELL, or HOLD
Examples
Example 1
Input(Line 1: Three space-separated integers N sw lw)
7 2 4 50 48 46 44 42 45 50
Output
BUY
Sharp recovery causes short MA (47.50) to cross above long MA (45.25). Previous step had short (43.50) <= long (44.25).
Example 2
Input(Line 1: Three space-separated integers N sw lw)
7 2 4 40 42 44 46 48 45 40
Output
SELL
Sharp decline causes short MA (42.50) to cross below long MA (44.75). Previous step had short (46.50) >= long (45.75).
Constraints
- •1 ≤ sw < lw (short window < long window)
- •lw + 1 ≤ N ≤ 10000 (enough prices for at least one comparison)
- •0.01 ≤ prices[i] ≤ 100000.0
- •Output exactly one of: BUY, SELL, or HOLD
Loading interactive editor…