请问一下,这里显示有发送平空订单,账户没有交易动作,是怎么回事?
你提供的内容和发没发单并没有关系
请提供可复现的代码,或者你能把逻辑说清楚
//------------------------------------------------------------------------
// 简称: ClosePositionStrategy_RB
// 名称: 螺纹钢平仓策略(基于持仓成本平仓)
// 类别: 公式应用
// 类型: 用户应用
// 输出: Void
//------------------------------------------------------------------------
Params
// 交易参数
Numeric Interval(2); // 执行间隔(秒)
String Contract("RB2601"); // 交易合约
Numeric ProfitOffset(3); // 盈利偏移点数
// 账户参数
Integer AccountIndex(0); // 账户索引
Vars
// 状态变量
Numeric LastRunTime(0); // 上次执行时间戳
Bool IsFirstTick(True); // 是否该Bar的第一个Tick
String LastTradingDate(""); // 上次交易日
Bool OrderSentForLong(False); // 多单平仓订单是否已发送标记
Bool OrderSentForShort(False); // 空单平仓订单是否已发送标记
// 持仓信息
Numeric LongPositions; // 多单持仓量
Numeric ShortPositions; // 空单持仓量
Numeric MRCB; // 多单持仓成本(开多买入成本)
Numeric MCCB; // 空单持仓成本(开空卖出成本)
// 临时变量
Numeric CurrentTimeSec(0);
Numeric ElapsedTime(0);
Numeric ClosePrice(0);
String CurrentDateStr("");
Numeric Hours(0);
Numeric Minutes(0);
Numeric Seconds(0);
Numeric i(0);
// 价格相关变量
Numeric PriceTemp;
Numeric AdjustedPrice;
Events
// 初始化事件
OnInit()
{
Commentary("【螺纹钢平仓策略】启动 | 合约: " + Contract + " | 间隔: " + Text(Interval) + "秒");
Commentary("平仓规则: 多单以MRCB+" + Text(ProfitOffset) + "平仓 | 空单以MCCB-" + Text(ProfitOffset) + "平仓");
Commentary("合约规格: 10吨/手, 最小变动1元/吨, 乘数10元/点");
LastRunTime = 0;
LastTradingDate = "";
// 初始化变量
LongPositions = 0;
ShortPositions = 0;
MRCB = 0;
MCCB = 0;
IsFirstTick = True;
OrderSentForLong = False;
OrderSentForShort = False;
Commentary("策略初始化完成:基于持仓成本平仓");
}
// 每个Bar事件(主交易逻辑)
OnBar(ArrayRef<Integer> indexs)
{
// 只在指定合约上运行
If(CurrentBar == 0 )
{
Return;
}
// 检查交易日变化
CurrentDateStr = Text(Date);
If(CurrentDateStr != LastTradingDate)
{
Commentary("[" + CurrentDateStr + "] 交易日开始");
LastTradingDate = CurrentDateStr;
// 重置状态
LastRunTime = 0;
MRCB = 0;
MCCB = 0;
IsFirstTick = True;
OrderSentForLong = False;
OrderSentForShort = False;
}
// 如果是该Bar的第一个Tick,重置状态
If(IsFirstTick)
{
LastRunTime = 0;
IsFirstTick = False;
}
// 获取当前时间(转换为秒)
Hours = IntPart(CurrentTime/10000);
Minutes = IntPart((CurrentTime - Hours*10000)/100);
Seconds = CurrentTime - Hours*10000 - Minutes*100;
CurrentTimeSec = Hours*3600 + Minutes*60 + Seconds;
// 执行间隔控制
If(LastRunTime > 0)
{
ElapsedTime = CurrentTimeSec - LastRunTime;
If(ElapsedTime < Interval)
{
Return;
}
}
// 更新上次执行时间
LastRunTime = CurrentTimeSec;
// ---- 获取账户持仓信息 ----
If (A_AccountDataExist(AccountIndex))
{
// 获取持仓数量
LongPositions = A_BuyPosition(AccountIndex);
ShortPositions = A_SellPosition(AccountIndex);
// 获取持仓成本
MRCB = A_BuyAvgPriceO(AccountIndex); // 开多买入成本
MCCB = A_SellAvgPriceO(AccountIndex); // 开空卖出成本
}
Else
{
Commentary("警告:账户数据不存在,尝试查找可用账户...");
// 尝试查找可用账户
For i = 0 To 9
{
If(A_AccountDataExist(i))
{
Commentary("发现可用账户: 索引=" + Text(i) + " 名称=" + A_AccountID(i));
}
}
LongPositions = 0;
ShortPositions = 0;
MRCB = 0;
MCCB = 0;
Return;
}
// 当持仓变为0时重置订单发送标记
If(LongPositions == 0)
{
OrderSentForLong = False;
}
If(ShortPositions == 0)
{
OrderSentForShort = False;
}
// 输出持仓状态
Commentary("========== 持仓状态 ==========");
Commentary("时间:" + Text(Time) + " | 合约:" + Symbol);
Commentary("持多单: " + Text(LongPositions) + "手 | 成本MRCB:" + Text(MRCB,2));
Commentary("持空单: " + Text(ShortPositions) + "手 | 成本MCCB:" + Text(MCCB,2));
Commentary("当前价格: Close=" + Text(Close,2));
Commentary("平仓规则: 多单MRCB+" + Text(ProfitOffset) + " | 空单MCCB-" + Text(ProfitOffset));
Commentary("==============================");
// ---- 平多仓逻辑 ----
If(LongPositions > 0 And MRCB > 0 And Not OrderSentForLong)
{
// 计算平仓价:开多买入成本MRCB + ProfitOffset
ClosePrice = MRCB + ProfitOffset;
// 检查平仓价是否有效
If(ClosePrice > 0)
{
// 计算调整后价格(取整,滑点为0)
PriceTemp = IntPart(ClosePrice / MinMove) * MinMove;
AdjustedPrice = PriceTemp;
Commentary("【平多仓操作】");
Commentary("成本MRCB:" + Text(MRCB,2) + " -> 平仓价:" + Text(ClosePrice,2));
Commentary("取整后价格:" + Text(AdjustedPrice) + " | 手数:" + Text(LongPositions));
// 发送平多仓订单:Enum_Sell表示卖出,Enum_Exit表示平仓
A_SendOrder(Enum_Sell, Enum_Exit, LongPositions, AdjustedPrice);
OrderSentForLong = True;
Commentary("平多仓订单已发送 | 价格:" + Text(AdjustedPrice) + " | 数量:" + Text(LongPositions));
}
Else
{
Commentary("[ERROR] 平多仓价格无效: " + Text(ClosePrice,2));
}
}
// ---- 平空仓逻辑 ----
If(ShortPositions > 0 And MCCB > 0 And Not OrderSentForShort)
{
// 计算平仓价:开空卖出成本MCCB - ProfitOffset
ClosePrice = MCCB - ProfitOffset;
// 检查平仓价是否有效
If(ClosePrice > 0)
{
// 计算调整后价格(取整,滑点为0)
PriceTemp = IntPart(ClosePrice / MinMove) * MinMove;
AdjustedPrice = PriceTemp;
Commentary("【平空仓操作】");
Commentary("成本MCCB:" + Text(MCCB,2) + " -> 平仓价:" + Text(ClosePrice,2));
Commentary("取整后价格:" + Text(AdjustedPrice) + " | 手数:" + Text(ShortPositions));
// 发送平空仓订单:Enum_Buy表示买入,Enum_Exit表示平仓
A_SendOrder(Enum_Buy, Enum_Exit, ShortPositions, AdjustedPrice);
OrderSentForShort = True;
Commentary("平空仓订单已发送 | 价格:" + Text(AdjustedPrice) + " | 数量:" + Text(ShortPositions));
}
Else
{
Commentary("[ERROR] 平空仓价格无效: " + Text(ClosePrice,2));
}
}
// 无持仓情况
If(LongPositions == 0 And ShortPositions == 0)
{
Commentary("当前无持仓,无需平仓操作");
}
Else If(LongPositions > 0 And (MRCB <= 0 Or OrderSentForLong))
{
If(MRCB <= 0)
{
Commentary("有多单但持仓成本无效或为0: MRCB=" + Text(MRCB,2));
}
If(OrderSentForLong)
{
Commentary("已发送过多单平仓订单");
}
}
Else If(ShortPositions > 0 And (MCCB <= 0 Or OrderSentForShort))
{
If(MCCB <= 0)
{
Commentary("有空单但持仓成本无效或为0: MCCB=" + Text(MCCB,2));
}
If(OrderSentForShort)
{
Commentary("已发送过空单平仓订单");
}
}
}
// 策略退出事件
OnExit()
{
Commentary("【螺纹钢平仓策略】停止运行");
Commentary("最终持仓: 多单=" + Text(LongPositions) + " | 空单=" + Text(ShortPositions));
}A函数不要和图表混用
我的开仓策略是能够正常运行的,我的策略是在策略交易上运行。
麻烦帮忙看看要怎么处理?谢谢!
处理不了,A函数靠自己调试。
你要想办法保证图表条件满足的情况下,同时A函数又能下单
代码是你自己写的吗
我只是为了调试才在图表上运行,我的策略很简单,就是读取账户的持仓和持仓成本,如果持多仓就是在持仓成本的基础上+3直接下单平仓。
平仓不能平 ,则找到平仓的条件检查
条件是符合的,账户里面没有平仓。
你输出的内容,并不一定能使A函数下单