feat: 阶梯提醒支持连续同向确认,基准改为仅内存不恢复
- 阶梯提醒新增「连续同向确认次数」ladderStreak(1–20):填 1 只按步长判断, 填 N 还要求连续 N 次刷新都同向变动才提醒,方向反转重新计数,价格不变则计数不推进 - 弹窗新增该字段与 1–20 校验,预览条件和规则名带上次数,README 补 780/781/782/783 例子 - 简化状态维护:基准价、前一次价格、连续计数只放内存,persistRuleStates 与 loadRuleStates 跳过 ::ladder:: 键,重启后按启动后首个价格重建基准,连续计数与冷却归零 - .gitignore 忽略 config/*.json 与 config/backups/,避免把导出配置(含 WebHook 地址)提交上去
This commit is contained in:
@@ -3,6 +3,9 @@ node_modules/
|
|||||||
|
|
||||||
# 本地配置(含邮箱授权码,不要提交)
|
# 本地配置(含邮箱授权码,不要提交)
|
||||||
config/config.json
|
config/config.json
|
||||||
|
# 网页上「导出配置」生成的文件(含 WebHook 地址 / 关键词,可能带密钥),不提交
|
||||||
|
config/*.json
|
||||||
|
config/backups/
|
||||||
|
|
||||||
# 运行数据
|
# 运行数据
|
||||||
data/
|
data/
|
||||||
|
|||||||
@@ -86,8 +86,10 @@ npm start
|
|||||||
- 步长口径:「具体变动值(元/克)」填 2 就是「每涨/跌 2 元/克提醒一次」;也可按百分比(填 0.5 即 0.5%);
|
- 步长口径:「具体变动值(元/克)」填 2 就是「每涨/跌 2 元/克提醒一次」;也可按百分比(填 0.5 即 0.5%);
|
||||||
- 方向:「每上涨一个步长」「每下跌一个步长」「每涨/跌一个步长」;
|
- 方向:「每上涨一个步长」「每下跌一个步长」「每涨/跌一个步长」;
|
||||||
- 例(步长 2 元、每上涨):780 建基准(不提醒)→ 782.5 提醒一次(基准变 782.5)→ 784.7 再提醒;
|
- 例(步长 2 元、每上涨):780 建基准(不提醒)→ 782.5 提醒一次(基准变 782.5)→ 784.7 再提醒;
|
||||||
|
- **连续确认**:「连续同向确认次数」填 1 表示只按步长判断(默认);填 3 则还要求连续 3 次刷新都比上一次高(或低),中途反向会重新计数,价格没变则计数保持不动 —— 用来过滤一两笔跳动造成的假突破;
|
||||||
|
例(步长 2 元、每上涨、连续 3 次):780 建基准 → 781(连续 1 次)→ 782(连续 2 次,步长已到但不提醒)→ 783(连续 3 次)才提醒;
|
||||||
- 价格回落不提醒、也不重置基准;冷却时间依然生效(默认切到阶梯时提示改成 60 秒),避免来回震荡刷屏;
|
- 价格回落不提醒、也不重置基准;冷却时间依然生效(默认切到阶梯时提示改成 60 秒),避免来回震荡刷屏;
|
||||||
- 基准价会写进 `data/rule-state.json`,重启后接着用;第一次见到该品种只用当前价建基准,不打扰。
|
- 基准价只在内存里维护,**不落盘**:程序重启后第一次抓到该品种,就用当时的价格重新建基准(不提醒),连续计数从 0 开始,冷却也重新计时;
|
||||||
- **定时提醒**:到点按当时的行情发一条通知,不看价格条件。适合「每天 09:00 报一下开盘价」「工作日 15:30 收盘播报」。
|
- **定时提醒**:到点按当时的行情发一条通知,不看价格条件。适合「每天 09:00 报一下开盘价」「工作日 15:30 收盘播报」。
|
||||||
- 提醒时刻:`09:00, 15:30`(24 小时制,逗号分隔,最多 12 个,会自动去重排序);
|
- 提醒时刻:`09:00, 15:30`(24 小时制,逗号分隔,最多 12 个,会自动去重排序);
|
||||||
- 重复方式:「每天」「工作日(周一至周五)」「每周指定几天」(勾选周一~周日,至少选一天);
|
- 重复方式:「每天」「工作日(周一至周五)」「每周指定几天」(勾选周一~周日,至少选一天);
|
||||||
|
|||||||
+23
-1
@@ -446,7 +446,9 @@ function ruleConditionText(rule) {
|
|||||||
? `${fmt(Math.abs(rule.value), left.digits)} ${left.unit}`
|
? `${fmt(Math.abs(rule.value), left.digits)} ${left.unit}`
|
||||||
: `${Math.abs(rule.value).toFixed(2)}%`;
|
: `${Math.abs(rule.value).toFixed(2)}%`;
|
||||||
const dirText = rule.direction === 'down' ? '每下跌' : rule.direction === 'both' ? '每涨/跌' : '每上涨';
|
const dirText = rule.direction === 'down' ? '每下跌' : rule.direction === 'both' ? '每涨/跌' : '每上涨';
|
||||||
return `${left.name} ${dirText} ${valueText} 提醒一次(基价随提醒更新)${rangeSuffix}`;
|
// 连续确认次数 > 1 时,把「还要连续几次同向」写进条件描述
|
||||||
|
const streakText = Number(rule.ladderStreak) > 1 ? `,需连续 ${Math.round(Number(rule.ladderStreak))} 次同向` : '';
|
||||||
|
return `${left.name} ${dirText} ${valueText} 提醒一次${streakText}(基价随提醒更新)${rangeSuffix}`;
|
||||||
}
|
}
|
||||||
if (rule.type === 'schedule') {
|
if (rule.type === 'schedule') {
|
||||||
// 定时提醒:重复方式 + 时刻,必要时带上每周哪几天
|
// 定时提醒:重复方式 + 时刻,必要时带上每周哪几天
|
||||||
@@ -933,6 +935,8 @@ function syncRuleFields() {
|
|||||||
document.getElementById('ruleCooldownField').hidden = isSchedule || isFailure;
|
document.getElementById('ruleCooldownField').hidden = isSchedule || isFailure;
|
||||||
// 阶梯提醒按步长计算,没有「持续满足重复提醒」的概念(重复字段收起)
|
// 阶梯提醒按步长计算,没有「持续满足重复提醒」的概念(重复字段收起)
|
||||||
document.getElementById('ruleRepeatField').hidden = isSchedule || type === 'ladder';
|
document.getElementById('ruleRepeatField').hidden = isSchedule || type === 'ladder';
|
||||||
|
// 阶梯提醒专用:连续同向确认次数
|
||||||
|
document.getElementById('ruleStreakField').hidden = type !== 'ladder';
|
||||||
// 重复间隔文案:价格类规则是「持续满足」,失败告警是「持续失败」
|
// 重复间隔文案:价格类规则是「持续满足」,失败告警是「持续失败」
|
||||||
document.getElementById('ruleRepeatLabel').textContent = isFailure
|
document.getElementById('ruleRepeatLabel').textContent = isFailure
|
||||||
? '持续失败时每 N 秒重复提醒(0 只提醒一次)'
|
? '持续失败时每 N 秒重复提醒(0 只提醒一次)'
|
||||||
@@ -1041,6 +1045,8 @@ function updateRuleSummary() {
|
|||||||
compareInstrumentId: document.getElementById('ruleCompare').value,
|
compareInstrumentId: document.getElementById('ruleCompare').value,
|
||||||
mode: document.getElementById('ruleMode').value,
|
mode: document.getElementById('ruleMode').value,
|
||||||
changeBasis: document.getElementById('ruleBasis').value,
|
changeBasis: document.getElementById('ruleBasis').value,
|
||||||
|
// 阶梯提醒的连续确认次数也进预览文案
|
||||||
|
ladderStreak: Number(document.getElementById('ruleStreak').value) || 1,
|
||||||
direction: document.getElementById('ruleDirection').value,
|
direction: document.getElementById('ruleDirection').value,
|
||||||
value: Number(document.getElementById('ruleValue').value) || 0,
|
value: Number(document.getElementById('ruleValue').value) || 0,
|
||||||
cooldownSec: Number(document.getElementById('ruleCooldown').value) || 0,
|
cooldownSec: Number(document.getElementById('ruleCooldown').value) || 0,
|
||||||
@@ -1095,6 +1101,8 @@ function openRuleModal(id) {
|
|||||||
document.getElementById('ruleMode').value = rule?.mode || 'abs';
|
document.getElementById('ruleMode').value = rule?.mode || 'abs';
|
||||||
document.getElementById('ruleBasis').value = rule?.changeBasis === 'amount' ? 'amount' : 'percent';
|
document.getElementById('ruleBasis').value = rule?.changeBasis === 'amount' ? 'amount' : 'percent';
|
||||||
document.getElementById('ruleValue').value = rule?.value ?? '';
|
document.getElementById('ruleValue').value = rule?.value ?? '';
|
||||||
|
// 阶梯提醒的连续确认次数,默认 1(只按步长判断)
|
||||||
|
document.getElementById('ruleStreak').value = rule?.ladderStreak ?? 1;
|
||||||
document.getElementById('ruleCooldown').value = rule?.cooldownSec ?? 1800;
|
document.getElementById('ruleCooldown').value = rule?.cooldownSec ?? 1800;
|
||||||
document.getElementById('ruleRepeat').value = rule?.repeatIntervalSec ?? 0;
|
document.getElementById('ruleRepeat').value = rule?.repeatIntervalSec ?? 0;
|
||||||
|
|
||||||
@@ -1183,6 +1191,14 @@ async function saveRule() {
|
|||||||
setResult('ruleResult', '请填写有效的阈值', false);
|
setResult('ruleResult', '请填写有效的阈值', false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
// 阶梯提醒的连续确认次数:后端限制 1–20 次
|
||||||
|
if (type === 'ladder') {
|
||||||
|
const streak = Number(document.getElementById('ruleStreak').value);
|
||||||
|
if (!Number.isFinite(streak) || streak < 1 || streak > 20) {
|
||||||
|
setResult('ruleResult', '连续确认次数请填 1–20(填 1 表示只按步长判断)', false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const inst = instruments.find((i) => i.id === instrumentId);
|
const inst = instruments.find((i) => i.id === instrumentId);
|
||||||
const nameInput = document.getElementById('ruleName').value.trim();
|
const nameInput = document.getElementById('ruleName').value.trim();
|
||||||
@@ -1215,6 +1231,8 @@ async function saveRule() {
|
|||||||
type, direction, value, instrumentId, compareInstrumentId,
|
type, direction, value, instrumentId, compareInstrumentId,
|
||||||
mode: document.getElementById('ruleMode').value,
|
mode: document.getElementById('ruleMode').value,
|
||||||
changeBasis: document.getElementById('ruleBasis').value,
|
changeBasis: document.getElementById('ruleBasis').value,
|
||||||
|
// 阶梯提醒的名称里带上连续确认次数,便于区分同一步长的多条规则
|
||||||
|
ladderStreak: Number(document.getElementById('ruleStreak').value) || 1,
|
||||||
windowSec, activeRange,
|
windowSec, activeRange,
|
||||||
// 失败告警的名称里也带上重复间隔,避免多条同类规则名字撞车
|
// 失败告警的名称里也带上重复间隔,避免多条同类规则名字撞车
|
||||||
repeatIntervalSec: Number(document.getElementById('ruleRepeat').value) || 0,
|
repeatIntervalSec: Number(document.getElementById('ruleRepeat').value) || 0,
|
||||||
@@ -1228,6 +1246,8 @@ async function saveRule() {
|
|||||||
mode: document.getElementById('ruleMode').value,
|
mode: document.getElementById('ruleMode').value,
|
||||||
// 涨跌幅 / 阶梯的阈值口径:percent(百分比)/ amount(具体变动值)
|
// 涨跌幅 / 阶梯的阈值口径:percent(百分比)/ amount(具体变动值)
|
||||||
changeBasis: (type === 'change' || type === 'ladder') ? document.getElementById('ruleBasis').value : 'percent',
|
changeBasis: (type === 'change' || type === 'ladder') ? document.getElementById('ruleBasis').value : 'percent',
|
||||||
|
// 阶梯提醒的连续确认次数(仅阶梯用,其它类型固定 1)
|
||||||
|
ladderStreak: type === 'ladder' ? (Number(document.getElementById('ruleStreak').value) || 1) : 1,
|
||||||
direction: type === 'schedule' ? 'abs' : direction,
|
direction: type === 'schedule' ? 'abs' : direction,
|
||||||
value: type === 'schedule' ? 0 : value,
|
value: type === 'schedule' ? 0 : value,
|
||||||
// 失败告警不用冷却,冷却置 0,节流交给「持续失败重复间隔」
|
// 失败告警不用冷却,冷却置 0,节流交给「持续失败重复间隔」
|
||||||
@@ -1371,6 +1391,8 @@ function bindEvents() {
|
|||||||
document.getElementById('ruleCooldown').addEventListener('input', updateRuleSummary);
|
document.getElementById('ruleCooldown').addEventListener('input', updateRuleSummary);
|
||||||
// 失败告警的重复间隔改动也要立刻反映到预览文案
|
// 失败告警的重复间隔改动也要立刻反映到预览文案
|
||||||
document.getElementById('ruleRepeat').addEventListener('input', updateRuleSummary);
|
document.getElementById('ruleRepeat').addEventListener('input', updateRuleSummary);
|
||||||
|
// 阶梯提醒的连续确认次数改动同理
|
||||||
|
document.getElementById('ruleStreak').addEventListener('input', updateRuleSummary);
|
||||||
// 统计区间与生效时段:改动后同步字段显隐和预览
|
// 统计区间与生效时段:改动后同步字段显隐和预览
|
||||||
document.getElementById('ruleWindow').addEventListener('change', () => {
|
document.getElementById('ruleWindow').addEventListener('change', () => {
|
||||||
syncWindowCustom();
|
syncWindowCustom();
|
||||||
|
|||||||
+7
-3
@@ -4,7 +4,7 @@
|
|||||||
<meta charset="utf-8" />
|
<meta charset="utf-8" />
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||||
<title>金价提醒 · Gold Price Watcher</title>
|
<title>金价提醒 · Gold Price Watcher</title>
|
||||||
<link rel="stylesheet" href="/styles.css?v=20261124w" />
|
<link rel="stylesheet" href="/styles.css?v=20261124y" />
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<header class="topbar">
|
<header class="topbar">
|
||||||
@@ -325,6 +325,10 @@
|
|||||||
<label class="field" id="ruleWindowCustomField" hidden><span>自定义分钟数(最长 720)</span>
|
<label class="field" id="ruleWindowCustomField" hidden><span>自定义分钟数(最长 720)</span>
|
||||||
<input type="number" id="ruleWindowCustom" min="1" max="720" step="1" placeholder="20" />
|
<input type="number" id="ruleWindowCustom" min="1" max="720" step="1" placeholder="20" />
|
||||||
</label>
|
</label>
|
||||||
|
<!-- 阶梯提醒专用:连续确认次数(连续 N 次同向变动才允许提醒) -->
|
||||||
|
<label class="field" id="ruleStreakField" hidden><span>连续同向确认次数(次)</span>
|
||||||
|
<input type="number" id="ruleStreak" min="1" max="20" step="1" value="1" />
|
||||||
|
</label>
|
||||||
<!-- 4) 生效与提醒频率 -->
|
<!-- 4) 生效与提醒频率 -->
|
||||||
<!-- 生效时段:留空表示全天,支持跨零点(如 22:00 ~ 02:00) -->
|
<!-- 生效时段:留空表示全天,支持跨零点(如 22:00 ~ 02:00) -->
|
||||||
<label class="field wide" id="ruleActiveRangeField"><span>生效时段(可选,留空表示全天)</span>
|
<label class="field wide" id="ruleActiveRangeField"><span>生效时段(可选,留空表示全天)</span>
|
||||||
@@ -372,7 +376,7 @@
|
|||||||
<p class="hint" id="ruleSummary"></p>
|
<p class="hint" id="ruleSummary"></p>
|
||||||
<p class="hint" id="ruleWindowHint">提示:统计区间选「日内」时,涨跌幅相对昨收计算;选「近 N 分钟」时,用本机缓存的行情点回算滚动区间,重启后会接着用已保存的历史。</p>
|
<p class="hint" id="ruleWindowHint">提示:统计区间选「日内」时,涨跌幅相对昨收计算;选「近 N 分钟」时,用本机缓存的行情点回算滚动区间,重启后会接着用已保存的历史。</p>
|
||||||
<p class="hint" id="ruleScheduleHint" hidden>提示:定时提醒会在指定时刻按当时的行情发一条通知,不受价格条件影响;同一时刻每天只响一次,程序当时没运行则不会补发。</p>
|
<p class="hint" id="ruleScheduleHint" hidden>提示:定时提醒会在指定时刻按当时的行情发一条通知,不受价格条件影响;同一时刻每天只响一次,程序当时没运行则不会补发。</p>
|
||||||
<p class="hint" id="ruleLadderHint" hidden>提示:阶梯提醒以「上次提醒时的价格」为基准,价格每偏离一个步长就提醒一次,随后基准更新为当前价。例如步长 2 元/克:782 提醒后基准变 782,涨到 784 再提醒;价格回落不会提醒,也不会重置基准。</p>
|
<p class="hint" id="ruleLadderHint" hidden>提示:阶梯提醒以「上次提醒时的价格」为基准,价格每偏离一个步长就提醒一次,随后基准更新为当前价。例如步长 2 元/克:782 提醒后基准变 782,涨到 784 再提醒;价格回落不会提醒,也不会重置基准。「连续同向确认次数」填 1 表示只按步长判断;填 3 则还要求连续 3 次刷新都是同向变动(每次刷新比上一次高/低),中途反向会重新计数,用来过滤一两笔跳动的假突破。</p>
|
||||||
<p class="hint" id="ruleFailureHint" hidden>提示:请求失败告警盯的是「数据源还能不能取到价」,与价格高低无关。连续查询失败达到设定次数时提醒一次;一旦某次抓取成功,失败计数清零,下次再连续失败达到次数还会重新提醒。数据源长时间不可用时,可在「持续失败时每 N 秒重复提醒」里填间隔,避免只响一次被忽略。</p>
|
<p class="hint" id="ruleFailureHint" hidden>提示:请求失败告警盯的是「数据源还能不能取到价」,与价格高低无关。连续查询失败达到设定次数时提醒一次;一旦某次抓取成功,失败计数清零,下次再连续失败达到次数还会重新提醒。数据源长时间不可用时,可在「持续失败时每 N 秒重复提醒」里填间隔,避免只响一次被忽略。</p>
|
||||||
</div>
|
</div>
|
||||||
<div class="modal-foot">
|
<div class="modal-foot">
|
||||||
@@ -403,7 +407,7 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="toast-wrap" id="toastWrap"></div>
|
<div class="toast-wrap" id="toastWrap"></div>
|
||||||
<script src="/app.js?v=20261124w"></script>
|
<script src="/app.js?v=20261124y"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
||||||
|
|||||||
@@ -212,6 +212,8 @@ function normalizeRule(raw, index) {
|
|||||||
// 涨跌幅的阈值口径:percent = 按百分比;amount = 按具体价格变动值(元/克等)
|
// 涨跌幅的阈值口径:percent = 按百分比;amount = 按具体价格变动值(元/克等)
|
||||||
// 只有显式传 amount 才算具体值,老规则(缺字段)一律按百分比,行为不变
|
// 只有显式传 amount 才算具体值,老规则(缺字段)一律按百分比,行为不变
|
||||||
changeBasis: raw?.changeBasis === 'amount' ? 'amount' : 'percent',
|
changeBasis: raw?.changeBasis === 'amount' ? 'amount' : 'percent',
|
||||||
|
// 阶梯提醒的「连续确认次数」:要求连续 N 次刷新同向变动才提醒,1 = 不启用
|
||||||
|
ladderStreak: Math.round(clampNumber(raw?.ladderStreak, 1, 1, 20)),
|
||||||
// 定时提醒:每天要响的时刻("HH:mm"),到点按当前行情发一条提醒
|
// 定时提醒:每天要响的时刻("HH:mm"),到点按当前行情发一条提醒
|
||||||
scheduleTimes: normalizeScheduleTimes(raw?.scheduleTimes),
|
scheduleTimes: normalizeScheduleTimes(raw?.scheduleTimes),
|
||||||
// 定时提醒的重复方式:daily=每天;weekday=工作日(周一至周五);weekly=每周指定几天
|
// 定时提醒的重复方式:daily=每天;weekday=工作日(周一至周五);weekly=每周指定几天
|
||||||
|
|||||||
+61
-16
@@ -668,7 +668,10 @@ class Monitor {
|
|||||||
/**
|
/**
|
||||||
* 阶梯提醒:以「上次提醒时的价格」为基准,每偏离一个步长就提醒一次,然后把基准更新到当前价。
|
* 阶梯提醒:以「上次提醒时的价格」为基准,每偏离一个步长就提醒一次,然后把基准更新到当前价。
|
||||||
* 例:步长 2 元、方向「每上涨」;780 建基准 → 782 提醒(基准变 782)→ 784 再提醒。
|
* 例:步长 2 元、方向「每上涨」;780 建基准 → 782 提醒(基准变 782)→ 784 再提醒。
|
||||||
* 基价与上次提醒时间存在 ruleStates 里(key = 规则::ladder::品种),会随 rule-state.json 一起落盘。
|
* 可选「连续确认」:要求连续 N 次刷新都是同一方向(涨/跌),才允许触发,用来过滤一两笔跳动的假突破;
|
||||||
|
* 方向反转时连续计数从头算,价格没变则计数保持不动。
|
||||||
|
* 基价、前一次价格与连续计数都只保存在内存里(key = 规则::ladder::品种),重启后不恢复:
|
||||||
|
* 重启后第一次抓到该品种就按当时的价格重新建基准、连续计数从 0 开始,冷却也重新计时。
|
||||||
* @param {object} rule 规则
|
* @param {object} rule 规则
|
||||||
* @param {object} inst 品种
|
* @param {object} inst 品种
|
||||||
* @param {object} cfg 配置
|
* @param {object} cfg 配置
|
||||||
@@ -681,9 +684,11 @@ class Monitor {
|
|||||||
|
|
||||||
let saved = this.ruleStates.get(key);
|
let saved = this.ruleStates.get(key);
|
||||||
// 第一次见到这个品种:先把当前价记成基准,不打扰
|
// 第一次见到这个品种:先把当前价记成基准,不打扰
|
||||||
|
// (阶梯状态只在内存里,重启后就是「第一次见到」,会以启动后查到的价格重新建基准)
|
||||||
if (!saved || typeof saved.anchor !== 'number') {
|
if (!saved || typeof saved.anchor !== 'number') {
|
||||||
this.ruleStates.set(key, { anchor: price, lastTriggerAt: 0 });
|
this.ruleStates.set(key, {
|
||||||
this.persistRuleStates();
|
anchor: price, lastTriggerAt: 0, lastPrice: price, streak: 0, streakDir: '',
|
||||||
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -693,21 +698,54 @@ class Monitor {
|
|||||||
/** 相对基准的变动量(amount 为价格差,percent 为百分数) */
|
/** 相对基准的变动量(amount 为价格差,percent 为百分数) */
|
||||||
const delta = basis === 'amount' ? price - saved.anchor : ((price - saved.anchor) / saved.anchor) * 100;
|
const delta = basis === 'amount' ? price - saved.anchor : ((price - saved.anchor) / saved.anchor) * 100;
|
||||||
|
|
||||||
let hit = false;
|
// ---- 连续同向计数 ----
|
||||||
if (rule.direction === 'down') hit = delta <= -step;
|
// 与「上一次刷新的价格」比较:涨/跌各自累计,方向反转就重新从 1 开始;价格没变则不推进也不清零。
|
||||||
else if (rule.direction === 'both') hit = Math.abs(delta) >= step;
|
const prevPrice = typeof saved.lastPrice === 'number' ? saved.lastPrice : null;
|
||||||
else hit = delta >= step; // 默认「每上涨」
|
let streak = Number(saved.streak) || 0;
|
||||||
if (!hit) return;
|
let streakDir = saved.streakDir || '';
|
||||||
|
if (prevPrice !== null && price !== prevPrice) {
|
||||||
|
const dir = price > prevPrice ? 'up' : 'down';
|
||||||
|
streak = streakDir === dir ? streak + 1 : 1;
|
||||||
|
streakDir = dir;
|
||||||
|
}
|
||||||
|
/** 需要连续多少次同向变动才确认提醒(1 = 不启用连续确认) */
|
||||||
|
const needStreak = Math.max(1, Math.round(Number(rule.ladderStreak) || 1));
|
||||||
|
|
||||||
|
// 先看相对基准有没有走完一个步长
|
||||||
|
let reached = false;
|
||||||
|
/** 本次偏离的方向:up / down */
|
||||||
|
let triggerDir = 'up';
|
||||||
|
if (rule.direction === 'down') {
|
||||||
|
reached = delta <= -step;
|
||||||
|
triggerDir = 'down';
|
||||||
|
} else if (rule.direction === 'both') {
|
||||||
|
reached = Math.abs(delta) >= step;
|
||||||
|
triggerDir = delta >= 0 ? 'up' : 'down';
|
||||||
|
} else {
|
||||||
|
reached = delta >= step; // 默认「每上涨」
|
||||||
|
}
|
||||||
|
|
||||||
|
// 连续次数不够、或者当前连续方向与偏离方向相反,都还不提醒(假突破过滤);
|
||||||
|
// needStreak = 1 表示不启用连续确认,保持原来的「只看步长」行为
|
||||||
|
const streakOk = needStreak <= 1 || (streak >= needStreak && streakDir === triggerDir);
|
||||||
|
|
||||||
// 冷却期内先不提醒、也不移动基准,等冷却过了再判断
|
// 冷却期内先不提醒、也不移动基准,等冷却过了再判断
|
||||||
const cooldownMs = (rule.cooldownSec || 0) * 1000;
|
const cooldownMs = (rule.cooldownSec || 0) * 1000;
|
||||||
if (cooldownMs > 0 && Date.now() - Number(saved.lastTriggerAt || 0) < cooldownMs) return;
|
const cooling = cooldownMs > 0 && Date.now() - Number(saved.lastTriggerAt || 0) < cooldownMs;
|
||||||
|
|
||||||
|
if (!reached || !streakOk || cooling) {
|
||||||
|
// 只更新观察字段(前价、连续计数),基准和上次提醒时间保持不动
|
||||||
|
this.ruleStates.set(key, { ...saved, lastPrice: price, streak, streakDir });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const anchor = saved.anchor;
|
const anchor = saved.anchor;
|
||||||
this.ruleStates.set(key, { anchor: price, lastTriggerAt: Date.now() });
|
// 提醒后基准更新为当前价,连续计数从头开始
|
||||||
this.persistRuleStates();
|
this.ruleStates.set(key, {
|
||||||
|
anchor: price, lastTriggerAt: Date.now(), lastPrice: price, streak: 0, streakDir: '',
|
||||||
|
});
|
||||||
|
|
||||||
const word = delta >= 0 ? '上涨' : '下跌';
|
const word = triggerDir === 'up' ? '上涨' : '下跌';
|
||||||
const moveText = basis === 'amount'
|
const moveText = basis === 'amount'
|
||||||
? `${fmtPrice(Math.abs(delta), inst.digits)} ${inst.unit}`
|
? `${fmtPrice(Math.abs(delta), inst.digits)} ${inst.unit}`
|
||||||
: `${Math.abs(delta).toFixed(2)}%`;
|
: `${Math.abs(delta).toFixed(2)}%`;
|
||||||
@@ -715,14 +753,17 @@ class Monitor {
|
|||||||
? `${fmtPrice(step, inst.digits)} ${inst.unit}`
|
? `${fmtPrice(step, inst.digits)} ${inst.unit}`
|
||||||
: `${step.toFixed(2)}%`;
|
: `${step.toFixed(2)}%`;
|
||||||
const anchorText = `${fmtPrice(anchor, inst.digits)} ${inst.unit}`;
|
const anchorText = `${fmtPrice(anchor, inst.digits)} ${inst.unit}`;
|
||||||
|
// 启用了连续确认时,正文里说明「连续几次」这个附加条件
|
||||||
|
const streakLine = needStreak > 1 ? `(连续 ${streak} 次${word}确认)` : '';
|
||||||
|
|
||||||
await this.raise(rule, {
|
await this.raise(rule, {
|
||||||
title: `${inst.name} 阶梯提醒:较上次提醒${word} ${moveText}`,
|
title: `${inst.name} 阶梯提醒:较上次提醒${word} ${moveText}`,
|
||||||
message: `最新价 ${fmtPrice(price, inst.digits)} ${inst.unit},较上次提醒基准 ${anchorText} ${word} ${moveText},已达到步长 ${stepText}。基准已更新,下次再偏离 ${stepText} 时再提醒。`,
|
message: `最新价 ${fmtPrice(price, inst.digits)} ${inst.unit},较上次提醒基准 ${anchorText} ${word} ${moveText},已达到步长 ${stepText}${streakLine}。基准已更新,下次再偏离 ${stepText} 时再提醒。`,
|
||||||
lines: [
|
lines: [
|
||||||
`${inst.name} 最新价:<b>${fmtPrice(price, inst.digits)} ${inst.unit}</b>`,
|
`${inst.name} 最新价:<b>${fmtPrice(price, inst.digits)} ${inst.unit}</b>`,
|
||||||
`上次提醒基准:${anchorText}`,
|
`上次提醒基准:${anchorText}`,
|
||||||
`本次${word}:${moveText}(步长 ${stepText})`,
|
`本次${word}:${moveText}(步长 ${stepText})`,
|
||||||
|
needStreak > 1 ? `连续确认:连续 ${streak} 次${word}(要求 ≥ ${needStreak} 次)` : '连续确认:未启用',
|
||||||
`触发时间:${nowText()}`,
|
`触发时间:${nowText()}`,
|
||||||
],
|
],
|
||||||
snapshot: this.snapshot(),
|
snapshot: this.snapshot(),
|
||||||
@@ -936,8 +977,9 @@ class Monitor {
|
|||||||
const saved = readJson(this.stateFile, null);
|
const saved = readJson(this.stateFile, null);
|
||||||
if (!saved) return;
|
if (!saved) return;
|
||||||
for (const [key, value] of Object.entries(saved)) {
|
for (const [key, value] of Object.entries(saved)) {
|
||||||
|
// 阶梯提醒的状态不落盘也不恢复:重启后按启动时查到的价格重新建基准、连续计数归零
|
||||||
|
if (key.includes('::ladder::')) continue;
|
||||||
if (value && typeof value === 'object') {
|
if (value && typeof value === 'object') {
|
||||||
// 保留各类型自己的附加字段(阶梯提醒要保住 anchor 基准价)
|
|
||||||
this.ruleStates.set(key, {
|
this.ruleStates.set(key, {
|
||||||
...value,
|
...value,
|
||||||
met: value.met === true ? true : (value.met === false ? false : null),
|
met: value.met === true ? true : (value.met === false ? false : null),
|
||||||
@@ -947,13 +989,16 @@ class Monitor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 延迟落盘规则状态,避免频繁写文件 */
|
/** 延迟落盘规则状态,避免频繁写文件(阶梯提醒的基准价只在内存里,不写盘) */
|
||||||
persistRuleStates() {
|
persistRuleStates() {
|
||||||
if (this.saveTimer) return;
|
if (this.saveTimer) return;
|
||||||
this.saveTimer = setTimeout(() => {
|
this.saveTimer = setTimeout(() => {
|
||||||
this.saveTimer = null;
|
this.saveTimer = null;
|
||||||
const plain = {};
|
const plain = {};
|
||||||
for (const [key, value] of this.ruleStates) plain[key] = value;
|
for (const [key, value] of this.ruleStates) {
|
||||||
|
if (key.includes('::ladder::')) continue;
|
||||||
|
plain[key] = value;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
writeJson(this.stateFile, plain);
|
writeJson(this.stateFile, plain);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
@@ -239,6 +239,8 @@ function createApp({ monitor }) {
|
|||||||
windowSec: toNum(body.windowSec) ?? 0,
|
windowSec: toNum(body.windowSec) ?? 0,
|
||||||
// 涨跌幅的阈值口径:percent=按百分比,amount=按具体价格变动值
|
// 涨跌幅的阈值口径:percent=按百分比,amount=按具体价格变动值
|
||||||
changeBasis: body.changeBasis === 'amount' ? 'amount' : 'percent',
|
changeBasis: body.changeBasis === 'amount' ? 'amount' : 'percent',
|
||||||
|
// 阶梯提醒的连续确认次数(1 = 不启用)
|
||||||
|
ladderStreak: toNum(body.ladderStreak) ?? 1,
|
||||||
// 定时提醒:到点时刻、重复方式、每周生效的星期
|
// 定时提醒:到点时刻、重复方式、每周生效的星期
|
||||||
scheduleTimes: Array.isArray(body.scheduleTimes) ? body.scheduleTimes : [],
|
scheduleTimes: Array.isArray(body.scheduleTimes) ? body.scheduleTimes : [],
|
||||||
scheduleRepeat: ['daily', 'weekday', 'weekly'].includes(body.scheduleRepeat) ? body.scheduleRepeat : 'daily',
|
scheduleRepeat: ['daily', 'weekday', 'weekly'].includes(body.scheduleRepeat) ? body.scheduleRepeat : 'daily',
|
||||||
|
|||||||
Reference in New Issue
Block a user