这是一个用软件内部储存来记录和读取10个独立单元的开仓数据,为了在隔日能自动读取并且自动读取数据对应独立单元进行独立计算平仓的。String PackPositions() 这里()因为是读取全局变量所以空的,但是系统一直报错。 麻烦老师帮我看一下哪里不对。谢谢!Params
String LogFileName("./UserData/StrategyLogs/ShortStrategy_Log.txt");
Vars
Bool PriceBelowAllMA_Confirmed;
Global Numeric RiseFlag;
// 持仓记录(10个独立槽位)
Global Numeric Cost0; Global Numeric Qty0;
Global Numeric Cost1; Global Numeric Qty1;
Global Numeric Cost2; Global Numeric Qty2;
Global Numeric Cost3; Global Numeric Qty3;
Global Numeric Cost4; Global Numeric Qty4;
Global Numeric Cost5; Global Numeric Qty5;
Global Numeric Cost6; Global Numeric Qty6;
Global Numeric Cost7; Global Numeric Qty7;
Global Numeric Cost8; Global Numeric Qty8;
Global Numeric Cost9; Global Numeric Qty9;
Numeric TotalPos;
Numeric PostTradePos;
String LabelText;
String LogLine;
String LogTimeStr;
Numeric ExitPrice;
Numeric EntryCost;
Numeric CurrentOpenPrice;
Numeric SumVal;
Integer i;
Integer SlotIndex;
// 存储键值
String storageSection;
String storageKey;
// 将当前所有槽位打包成字符串
String PackPositions()
{
String result = "";
result = result + "0," + Text(Qty0) + "," + Text(Cost0) + ";";
result = result + "1," + Text(Qty1) + "," + Text(Cost1) + ";";
result = result + "2," + Text(Qty2) + "," + Text(Cost2) + ";";
result = result + "3," + Text(Qty3) + "," + Text(Cost3) + ";";
result = result + "4," + Text(Qty4) + "," + Text(Cost4) + ";";
result = result + "5," + Text(Qty5) + "," + Text(Cost5) + ";";
result = result + "6," + Text(Qty6) + "," + Text(Cost6) + ";";
result = result + "7," + Text(Qty7) + "," + Text(Cost7) + ";";
result = result + "8," + Text(Qty8) + "," + Text(Cost8) + ";";
result = result + "9," + Text(Qty9) + "," + Text(Cost9) + ";";
return result;
}
// 从字符串解包到槽位
void UnpackPositions(String data)
{
if (data == "") return;
StringArray slots;
StringSplit(data, ";", slots);
Numeric slotCount = ArrayGetSize(slots);
for i = 0 to slotCount - 1
{
StringArray fields;
StringSplit(slots[i], ",", fields);
if (ArrayGetSize(fields) >= 3)
{
Numeric idx = Value(fields[0]);
Numeric qty = Value(fields[1]);
Numeric cost = Value(fields[2]);
if (idx == 0) { Qty0 = qty; Cost0 = cost; }
else if (idx == 1) { Qty1 = qty; Cost1 = cost; }
else if (idx == 2) { Qty2 = qty; Cost2 = cost; }
else if (idx == 3) { Qty3 = qty; Cost3 = cost; }
else if (idx == 4) { Qty4 = qty; Cost4 = cost; }
else if (idx == 5) { Qty5 = qty; Cost5 = cost; }
else if (idx == 6) { Qty6 = qty; Cost6 = cost; }
else if (idx == 7) { Qty7 = qty; Cost7 = cost; }
else if (idx == 8) { Qty8 = qty; Cost8 = cost; }
else if (idx == 9) { Qty9 = qty; Cost9 = cost; }
}
}
}
// 保存当前所有槽位到TBProfileString
void SavePositions()
{
String packed = PackPositions();
// 异步写入
SetTBProfileString(storageSection, storageKey, packed, True);
}
Events
{
OnInit()
{
RiseFlag = 0;
// 初始化所有槽位
Cost0=Cost1=Cost2=Cost3=Cost4=Cost5=Cost6=Cost7=Cost8=Cost9=0;
Qty0=Qty1=Qty2=Qty3=Qty4=Qty5=Qty6=Qty7=Qty8=Qty9=0;
// 构造存储区名(按合约隔离)
storageSection = "ShortStrategy_" + Data0.Symbol;
storageKey = "PositionData";
// 读取之前保存的持仓
String saved = GetTBProfileString(storageSection, storageKey, "");
if (saved != "")
{
UnpackPositions(saved);
Commentary("已恢复持仓记录");
}
// 日志
LogLine = "========================================\n";
LogLine = LogLine + "[" + DateTimeToString(SystemDateTime()) + "] 策略初始化完成 (SystemTime).\n";
LogLine = LogLine + "存储区:" + storageSection + "\n";
LogLine = LogLine + "日志将记录 K 线市场时间 (BarTime).\n";
LogLine = LogLine + "文件路径:" + LogFileName + "\n";
LogLine = LogLine + "========================================\n";
FileAppend(LogFileName, LogLine);
}
ai?
代码还未仔细看,但公式内函数要写在Defs下面吧
麻烦仔细看一下,可以复制到策略中编译一下 。麻烦您了!