奉上代码与大家共享
Params
Numeric DataDepth(10); // K线数据深度
String BarPeriod("1d"); // K线周期
Vars
Plot plt; // 图表对象
Global String eventName("contract_switch_button"); // 按钮事件名称
Global String mainContract(""); // 主合约(MainSymbol)
Global String secondContract(""); // 次合约(SecondMainSymbol)
Global Integer currIndex(-1); // 当前订阅索引
Global String currCode(""); // 当前合约代码
Defs
// 订阅指定合约K线
Integer SubscribeContract(String code)
{
if(code == "" || code == currCode) return 0;
Integer idx = SubscribeBar(code, BarPeriod, DataDepth);
if(idx > 0)
{
Data[idx].Show(); // 显示K线
return idx;
}
else
{
return 0;
}
}
// 取消当前订阅
String UnsubscribeCurrent()
{
if(currIndex > 0)
{
UnsubscribeBar(currIndex);
currIndex = -1;
currCode = "";
}
Return "";
}
// 切换到目标合约
String SwitchTo(String targetCode)
{
UnsubscribeCurrent();
Integer newIdx = SubscribeContract(targetCode);
if(newIdx > 0)
{
currIndex = newIdx;
currCode = targetCode;
}
Return "";
}
Events
// 初始化事件:创建按钮+订阅事件
OnInit()
{
// 获取系统合约(MainSymbol和SecondMainSymbol)
mainContract = MainSymbol;
secondContract = SecondMainSymbol;
// 校验合约有效性
if(mainContract == "" || secondContract == "" || mainContract == secondContract)
{
Alert("合约无效:需两个不同的MainSymbol和SecondMainSymbol");
Return;
}
// 订阅事件(与案例一致的事件订阅方式)
SubscribeEvent(eventName);
// 在工具栏添加单个切换按钮(与案例一致的toolBar参数格式)
// 按钮文本固定为"切换合约",点击触发事件
// 初始化默认订阅主合约
SwitchTo(mainContract);
Data0.Hide;//隐藏D0
}
OnReady()
{
Range[0:DataSourceSize - 1]
{
plt.toolBar(eventName, "主次切换", "切换合约");//在图层显示按钮
}
}
// 按钮点击事件处理(与案例一致的事件处理逻辑)
OnEvent(StringRef evtName, MapRef<String, String> evtValue)
{
if(evtName == eventName)
{
// 核心切换逻辑:当前是主合约则切到次合约,反之切回
if(currCode == mainContract)
{
SwitchTo(secondContract);
PushStatusMsg("切换至次主力合约:" + secondContract);
}
else if(currCode == secondContract)
{
SwitchTo(mainContract);
PushStatusMsg("切换至主力合约:" + mainContract);
}
}
}
理论上你自己打品种代码就行,
或者你拼个行情报价组件就行,
如果你愣要写代码, 可以看下plot类里的combbox
有一点,就是点到这个品种,我想同时看主力和次主力,但是说行情报价,要不就是主力,要不就是次主力,最好是可以直接在组件直接切换,省的还得去打字或者去找
自己写公式, 根据品种,订阅次主力
系统公式有案例, 如何订阅次主力
我想知道怎么在工具栏里切换合约,plot类里的combbox,不知道如何实现
Params
Numeric DataDepth(10); // K线数据深度
String BarPeriod("1d"); // K线周期(如"1d"日线、"1h"小时线)
Vars
Plot plt; // 图表对象
Global String eventName("contract_radio_switch"); // 单选事件名称
Array<String> radioOptions; // 单选按钮选项(仅含两个系统合约)
Global Integer currIndex(-1); // 当前订阅索引
Global String currCode(""); // 当前合约代码
Defs
// 订阅指定合约K线(返回订阅索引,失败返回0)
Integer SubscribeContract(String code)
{
if(code == "" || code == currCode) return 0; // 空合约或重复订阅直接返回
Integer idx = SubscribeBar(code, BarPeriod, DataDepth);
if(idx > 0)
{
Print("订阅成功:" + code + ",索引:" + Text(idx));
Data[idx].Show(); // 显示该合约K线
return idx;
}
else
{
Alert("订阅失败:" + code);
return 0;
}
}
// 取消当前订阅(无实际返回值,返回空字符串)
String UnsubscribeCurrent()
{
if(currIndex > 0)
{
UnsubscribeBar(currIndex);
Print("取消订阅:" + currCode + ",索引:" + Text(currIndex));
currIndex = -1;
currCode = "";
}
Return "";
}
// 切换到选中合约(无实际返回值,返回空字符串)
String SwitchToSelected(String targetCode)
{
UnsubscribeCurrent();
Integer newIdx = SubscribeContract(targetCode);
if(newIdx > 0)
{
currIndex = newIdx;
currCode = targetCode;
}
Return "";
}
Events
OnInit()
{
ArrayClear(radioOptions); // 初始化单选选项数组
// 获取系统合约(仅保留MainSymbol和SecondMainSymbol,去重)
String main = MainSymbol;
String second = SecondMainSymbol;
if(main != "") ArrayPushBack(radioOptions, main);
if(second != "" && second != main) ArrayPushBack(radioOptions, second);
// 校验单选选项有效性(至少1个有效合约)
if(GetArraySize(radioOptions) == 0)
{
Alert("无有效合约(MainSymbol和SecondMainSymbol均为空)");
Return;
}
// 按正确参数顺序调用:eventName, name, values, selectIndex, component
plt.toolBar(eventName, "合约选择", radioOptions, 1, "radio"); // 0表示默认选中第一个选项
SubscribeEvent(eventName); // 订阅单选事件
// 初始化默认订阅第一个合约
SwitchToSelected(radioOptions[0]);
Print("单选按钮组创建成功,选项:" + TextArray(radioOptions));
}
OnEvent(StringRef evtName, MapRef<String, String> evtValue)
{
if(evtName == eventName)
{
String selectedCode = evtValue["value"]; // 获取单选选中的合约代码
if(selectedCode != "")
{
Print("单选选中合约:" + selectedCode);
SwitchToSelected(selectedCode); // 切换到选中合约
}
}
}
现在我已经进行到这一步了,还是不能切换,只显示出来了
该怎么继续
搞定了!
👍人才