You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C++中为电子表格同一行补全不同时间生成的变量数据

问题解决:警报触发后补全当日K线高低价并计算差值

核心问题分析

  • 警报触发时(如15:10)先写入部分数据到指定行,但当日dayHighClose和dayLowClose需到17:59才生成,需补回同一行而非新增行
  • 当前代码错误地在17:59时新增行写入高低价,导致数据行错位,且未实现currentPrice与最高价的差值计算

修正后的代码实现

// 定义持久化结构,存储未补全高低价的行信息
struct PendingRowData {
    int RowIndex;
    SCDateTime SignalDate;
    double TriggerPrice;
};
std::vector<PendingRowData> PendingRows;

if ((LongAlert || ShortAlert) && Subgraph_SignalState[sc.Index] == 0)
{   
    sc.AddMessageToLog("多空警报触发", 1);
    
    // 写入即时可用数据
    sc.SetSheetCellAsString(SheetHandle, 0, rowIndex, symbol.GetChars());
    sc.SetSheetCellAsString(SheetHandle, 1, rowIndex, formattedDateTime);
    sc.SetSheetCellAsDouble(SheetHandle, 2, rowIndex, currentPrice);
    sc.SetSheetCellAsDouble(SheetHandle, 7, rowIndex, stn240Array[sc.Index]);
    
    float currentValue = stn240Array[sc.Index];
    Subgraph_FormulaResult[sc.Index] = static_cast<float>(currentValue);
    
    // 记录当前行的索引、日期和触发价,等待后续补全
    PendingRowData newPending;
    newPending.RowIndex = rowIndex;
    newPending.SignalDate = currentDateTime;
    newPending.TriggerPrice = currentPrice;
    PendingRows.push_back(newPending);
    
    rowIndex++;
}

// 每日17:59后处理当日待补全记录
SCDateTime targetTime;
targetTime.SetTime(HMS_TIME(17, 59, 0));
if (currentDateTime >= targetTime)
{
    // 遍历待补全列表,匹配当日记录
    for (auto it = PendingRows.begin(); it != PendingRows.end(); )
    {
        if (it->SignalDate.GetYear() == currentDateTime.GetYear() &&
            it->SignalDate.GetMonth() == currentDateTime.GetMonth() &&
            it->SignalDate.GetDay() == currentDateTime.GetDay())
        {
            // 补全高低价到对应行
            sc.SetSheetCellAsDouble(SheetHandle, 3, it->RowIndex, dayHighClose);
            sc.SetSheetCellAsDouble(SheetHandle, 4, it->RowIndex, dayLowClose);
            
            // 计算触发价与最高价的差值,写入第5列(可自行调整列索引)
            double priceDiff = it->TriggerPrice - dayHighClose;
            sc.SetSheetCellAsDouble(SheetHandle, 5, it->RowIndex, priceDiff);
            
            // 移除已处理的记录
            it = PendingRows.erase(it);
        }
        else
        {
            ++it;
        }
    }
}

return;

关键修正点

  • 新增PendingRowData结构,持久存储警报触发时的行索引、日期和触发价格,彻底避免行索引错位问题
  • 将高低价补全逻辑独立拆分,每日17:59后统一处理当日待补全记录
  • 匹配当日记录后写入到触发时的同一行,同时计算并写入触发价与最高价的差值
  • 处理完成后从待补全列表移除记录,防止重复执行

内容的提问来源于stack exchange,提问作者philip hoy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 18:05:21