Last active
November 12, 2025 22:43
-
-
Save flying3615/91ce1d04d2a2d50b9feabc2b0825313f to your computer and use it in GitHub Desktop.
yahoo finance2 calculate Volatility
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import yahooFinance from 'yahoo-finance2'; | |
| async function analyzeStock(symbol, period) { | |
| try { | |
| const today = new Date(); | |
| const startDate = new Date(); | |
| startDate.setMonth(today.getMonth() - period); | |
| const queryOptions = { | |
| period1: startDate.toISOString(), // 使用 toISOString() 获取正确的日期字符串 | |
| period2: today.toISOString(), // 使用 toISOString() 获取正确的日期字符串 | |
| interval: '1d' | |
| }; | |
| const result = await yahooFinance.historical(symbol, queryOptions); | |
| if (!result || !result.length) { | |
| console.log("未找到历史数据或数据为空。"); | |
| return; | |
| } | |
| const closes = result.map(item => item.close); // 获取收盘价数组 | |
| const volatility = calculateVolatility(closes); // 计算波动率 | |
| const ma10 = calculateMovingAverage(closes, 10); // 计算10日均线 | |
| const ma30 = calculateMovingAverage(closes, 30); // 计算30日均线 | |
| const ma60 = calculateMovingAverage(closes, 60); // 计算60日均线 | |
| // 前一日收盘价 | |
| const lastClose = closes[closes.length - 1] | |
| const oneDayBeforeClose = closes[closes.length - 2] | |
| // 计算乖离值 | |
| const deviation10 = lastClose - ma10[ma10.length -2] | |
| const deviation30 = lastClose - ma30[ma30.length -2] | |
| const deviation60 = lastClose - ma60[ma60.length -2] | |
| // 计算乖离率 | |
| const deviation10Rate = Math.abs(deviation10)/ma10[ma10.length -2] | |
| const deviation30Rate = Math.abs(deviation30)/ma30[ma30.length -2] | |
| const deviation60Rate = Math.abs(deviation60)/ma60[ma60.length -2] | |
| console.log(`股票代码: ${symbol}`); | |
| console.log(`过去${period}个月的波动率: ${volatility.toFixed(4)}`); | |
| console.log(`最近的收盘价: ${lastClose}`); | |
| console.log(`前一日的收盘价: ${oneDayBeforeClose}`); | |
| console.log(`最近的10日均线乖离率: ${deviation10Rate.toFixed(4)}, 乖离值: ${deviation10}`); | |
| console.log(`最近的30日均线乖离率: ${deviation30Rate.toFixed(4)}, 乖离值: ${deviation30}`); | |
| console.log(`最近的60日均线乖离率: ${deviation60Rate.toFixed(4)}, 乖离值: ${deviation60}`); | |
| //你可以进一步分析这些结果,例如: | |
| // 1. 波动率较小,且乖离值较小,可能处于盘整状态 | |
| // 2. 波动率较大,且乖离值较大,可能处于趋势行情 | |
| } catch (error) { | |
| console.error('发生错误:', error); | |
| } | |
| } | |
| // 计算波动率 | |
| function calculateVolatility(prices) { | |
| if(prices.length < 2) return 0; | |
| const returns = prices.slice(1).map((price, index) => Math.log(price / prices[index])); | |
| const sum = returns.reduce((acc, curr) => acc + curr, 0); | |
| const mean = sum / returns.length; | |
| const squaredDifferences = returns.map(ret => (ret - mean) ** 2); | |
| const variance = squaredDifferences.reduce((acc, curr) => acc + curr, 0) / (squaredDifferences.length - 1); | |
| return Math.sqrt(variance) | |
| } | |
| // 计算移动平均线 | |
| function calculateMovingAverage(prices, window) { | |
| const ma = []; | |
| for (let i = window - 1; i < prices.length; i++) { | |
| let sum = 0; | |
| for (let j = 0; j < window; j++) { | |
| sum += prices[i - j]; | |
| } | |
| ma.push(sum / window); | |
| } | |
| return ma; | |
| } | |
| function isConsolidation(ppo10, ppo30, ppo60, threshold) { | |
| /** | |
| * 判断股票是否处于盘整阶段。 | |
| * | |
| * 参数: | |
| * ppo10 (number): 10日均线乖离率。 | |
| * ppo30 (number): 30日均线乖离率。 | |
| * ppo60 (number): 60日均线乖离率。 | |
| * threshold (number): 乖离率阈值,当所有乖离率的绝对值都小于此值时,判断为盘整。 | |
| * | |
| * 返回: | |
| * boolean: True 如果股票处于盘整阶段,False 反之。 | |
| */ | |
| const ppoValues = [ppo10, ppo30, ppo60]; | |
| const absPpoValues = ppoValues.map(Math.abs); | |
| return absPpoValues.every(value => value < threshold); | |
| } | |
| // 调用示例 | |
| const symbol = 'MSTU'; // 你要分析的股票代码 | |
| const period = 3; // 你要分析的时间区间,单位为月 | |
| analyzeStock(symbol,period); | |
| // 1. 波动率较小,且乖离值较小,可能处于盘整状态 | |
| // 2. 波动率较大,且乖离值较大,可能处于趋势行情 | |
| // 股票代码: CLS | |
| // 过去3个月的波动率: 0.0354 | |
| // 最近的收盘价: 107.5 | |
| // 前一日的收盘价: 103.11000061035156 | |
| // 最近的10日均线乖离率: 0.0938, 乖离值: 9.219999694824224 | |
| // 最近的30日均线乖离率: 0.1342, 乖离值: 12.722666931152347 | |
| // 最近的60日均线乖离率: 0.2591, 乖离值: 22.120333226521808 | |
| // 股票代码: C | |
| // 过去3个月的波动率: 0.0187 | |
| // 最近的收盘价: 78.2699966430664 | |
| // 前一日的收盘价: 73.5 | |
| // 最近的10日均线乖离率: 0.0885, 乖离值: 6.362996673583979 | |
| // 最近的30日均线乖离率: 0.0978, 乖离值: 6.974996439615879 | |
| // 最近的60日均线乖离率: 0.1366, 乖离值: 9.405163319905597 | |
| // 股票代码: AMD | |
| // 过去3个月的波动率: 0.0266 | |
| // 最近的收盘价: 119.95999908447266 | |
| // 前一日的收盘价: 116.08999633789062 | |
| // 最近的10日均线乖离率: 0.0146, 乖离值: -1.78000106811524 | |
| // 最近的30日均线乖离率: 0.0582, 乖离值: -7.4126670837402315 | |
| // 最近的60日均线乖离率: 0.1228, 乖离值: -16.78633486429851 | |
| // 股票代码: MSTU | |
| // 过去3个月的波动率: 0.1466 | |
| // 最近的收盘价: 11.010000228881836 | |
| // 前一日的收盘价: 10 | |
| // 最近的10日均线乖离率: 0.1588, 乖离值: 1.5090002536773675 | |
| // 最近的30日均线乖离率: 0.1135, 乖离值: -1.4099331061045337 | |
| // 最近的60日均线乖离率: 0.0768, 乖离值: -0.915299781163533 | |
| // 股票代码: TSLA | |
| // 过去3个月的波动率: 0.0486 | |
| // 最近的收盘价: 428.2200012207031 | |
| // 最近的10日均线乖离率: 0.0690, 乖离值: 27.647006225585926 | |
| // 最近的30日均线乖离率: 0.0474, 乖离值: 19.3923360188802 | |
| // 最近的60日均线乖离率: 0.2273, 乖离值: 79.29966862996417 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment