9:05玉米有开空信号,但未成交也无异常提示




代码在这里
//------------------------------------------------------------------------
Params
Numeric FastLength(12); // MACDfast
Numeric SlowLength(26); // macdslow
Numeric MACDLength(9);
Numeric MAlong(30); //均线周期
Numeric TrailLength(4); // 止盈回落幅度
Numeric ProtectRatio(2); //启动保本所需比率
Numeric MaxRiskRatio(0.6); // 账户最大风险率
Numeric SingleMaxRatio(0.25); // 单品种最大资金占比
Vars
Global Bool IsTradTime(False);
Series<Numeric> Diff;
Series<Numeric> DEA;
Series<Numeric> MA60;
Series<Numeric> IsLongD;
Series<Numeric> MValue;
Series<Numeric> IsLongH;
Series<Numeric> IsLongM;
Series<Numeric> entry_price;
Series<Numeric> stop_loss_price;
Series<Numeric> tickSize;
Series<Bool> ConD;
Series<Bool> ConH;
Series<Bool> ConM;
Series<Bool> ConD_Short;
Series<Bool> ConH_Short;
Series<Bool> ConM_Short;
Series<Numeric> HighAfterEntry(0,2);
Series<Numeric> LowAfterEntry(99999999,2);
Series<Bool> StartTrail(False);
// 账户风控
Global Numeric CurrentRiskRatio;
Global Numeric FreeMargin; // 可用资金
Global Numeric LastEquity; //总权益
Global Numeric longCanSellV; //可平多数
Global Numeric shortCanCoverV; //可平空数
Global Numeric perMargin; // 每手保证金
Series<Numeric> singleMaxMoney; // 单个品种最大资金
Series<Numeric> calcLots; // 理论手数
Series<Numeric> finalLots; // 最终下单手数
Array<String> minLotsSyms; //临时调整最小手数的品种
Array<Numeric> ExMinLots; // 对应临时调整的品种的最小手数
Array<String> mRateSyms(["",""]); //临时调整保证金率的品种
Array<Numeric> mRateArr([0.13, 0.11]); //对应临时调整品种的保证金率
Dic<Array<String>> VolumeRate("TB_VolumeRate_Futures"); //获取最小开仓量限制
Series<Numeric> minLots; //最终最小交易手数
Account acc;
Position pos;
Defs
Numeric getStopPrice()
{
if(MarketPosition>0)
{
return HighAfterEntry*(1-TrailLength/100);
}Else If(MarketPosition<0)
{
return LowAfterEntry*(1+TrailLength/100);
}
return 0;
}
Events
OnInit()
{
SubscribeBar(Symbol, "H",20251201);
SubscribeBar(Symbol, "D",20251201);
Range[0:2]
{
SetOrderMap2MainSymbol(); //主力映射
//SetBasePeriod(Data0.Frequency); //周期基准:5min,如打开,则取不到其他图层的close
AddStrategyFlag(Enum_Data_AutoSwapPosition); //自动换月
AddDataFlag(Enum_Data_RolloverBackWard()); //后复权
AddDataFlag(Enum_Data_RolloverRealPrice); //映射真实价格,交易价格纠正为真实价
SetSlippage(Enum_Rate_PointPerHand, 2); //滑点设置
SetInitCapital(200000); //设置初始资金为20万
//设置手续费率为成交金额的5%%,不收平今
SetCommissionRate(BitOr(Enum_Rate_FreeOfExitToday,Enum_Rate_ByFillAmount),5);
Array<Numeric> timePoint;
timepoint[0] = 0.145950;
timepoint[1] = 0.225950;
SetTriggerBarClose(timePoint);
}
entry_price = 0;
stop_loss_price = 0;
}
OnReady()
{
Bool ret = A_GetAccount(acc,0);
Print("A_GetAccount:" + IIFString(ret,"True","False") + ",acc.available:" + Text(acc.available));
Bool ret1 = A_GetPosition(mainsymbol, pos, "", 0);
//A_GetPosition(relativesymbol, pos, "", i);
MarginRate mRate1;
//获取账户对应合约的保证金率
/*Bool ret2 = A_GetMarginRate(Symbol, mRate1);
Print("A_GetMarginRate:" + IIFString(ret2, "True", "False") + "," + Text(mRate1));
LMarginRate = mRate1.longMarginRatio;*/
CurrentRiskRatio = acc.risk;
FreeMargin = acc.available;
LastEquity = acc.currMargin + FreeMargin;
longCanSellV =pos.longCanSellVolume;
shortCanCoverV =pos.shortCanCoverVolume;
Print("A_GetPosition:" + IIFString(ret1, "True", "False") + "," + Text(longCanSellV)+ "||" + Text(shortCanCoverV));
}
OnBar(ArrayRef<Integer> indexs)
{
If(TrueDate(0)<>TrueDate(1)) //新一个交易日开始
{
//Print(DateTimeToString(date+Time) + "newDay");
tickSize = MinMove() * PriceScale();
array<string> lots;
GetDicValue("TB_VolumeRate_Futures",RelativeSymbol(),date+time,Lots); //最小开仓量限制查询,数据中心要定期更新
minLots = Value(lots[0]); //需要到数据中心定期更新最小开仓限制
If(minLots == 0) minLots = BaseShares();
perMargin = Data2.Close[1]/Rollover() * ContractUnit() * (MarginRatio() + 0.05); //期货公司对交易所规定的保证金另外加5%左右
// 每日重置所有状态
entry_price = 0;
stop_loss_price = 0;
StartTrail = False;
HighAfterEntry = 0;
LowAfterEntry = 99999999;
}
//计算交易手数
singleMaxMoney = 200000 * SingleMaxRatio;
calcLots = singleMaxMoney / perMargin;
//超过剩余可开总资金,重新修正
/*If(calcLots * perMargin > FreeMargin*0.9) //优化时关闭
{
calcLots = FreeMargin*0.9 / perMargin;
}*/
If(calcLots >= 1)
{
finalLots = IntPart(calcLots);
}
Else If(calcLots >= 0.6)
{
finalLots = 1;
}
Else
{
finalLots = 0;
}
Commentary("FreeMargin:" + Text(FreeMargin ) + "tickSize:" + Text(tickSize) + "minLots:" + Text(minLots));
Commentary(Left(Symbol,5) +":finalLots:" + Text(finalLots) + "手;每手保证金:" + Text(perMargin));
//finalLots = 1; //优化时打开
//If(1 - (FreeMargin - finalLots*perMargin)/LastEquity > 0.85) Return; //优化时关闭
Range[0:2]
{
Diff = XAverage(Close, FastLength) - XAverage(Close, SlowLength) ;
DEA = XAverage(Diff, MACDLength);
MValue = 2 * (Diff - DEA);
MA60 = MA(Close,MAlong);
}
IsLongD = Data2.Close - Data2.Open;
Numeric IsLongD1= Data2.Close[1] - Data2.Open[1];
Numeric IsLongD2= Data2.Close[2] - Data2.Open[2];
IsLongH = Data1.Close - Data1.Open;
Numeric IsLongH1= Data1.Close[1] - Data1.Open[1];
Numeric IsLongH2= Data1.Close[2] - Data1.Open[2];
IsLongM = Close - Open;
//Commentary("IsLongM[2]):"+Text((IsLongM[2]),0)+"(IsLongM[1]):"+Text(IsLongM[1],0)+ ",IsLongM:"+Text(IsLongM,0));
Commentary("MValue_H:"+Text(Data1.MValue,2) + ",DEA_H:"+Text(Data1.DEA,2));
//=== 日线多头条件 ===
Bool isMACDL = Data2.MValue > 0.05 And IsLongD1 > 0 And Data2.High[0] > Data2.High[1];
ConD = isMACDL And Data2.MA60[1] < Data2.MA60;
If(ConD) Data2.PlotString("Buy","^",Data2.Low - 10);
//=== 小时线多头条件 ===
Bool isMACDLh = Data1.MValue >= 0 And Data1.High[0] > Data1.High[1];
ConH = isMACDLh;
If(ConH) Data1.PlotString("Buy","^",Data1.Low - 10);
//=== 5分钟多头条件 ===
ConM = (IsLongM[2]<0 And IsLongM[1]>=0) Or (IsLongM[2]>0 And IsLongM[1]<0 And High[2] >High[1]);
If(ConM) PlotString("Buy","*",Low - 10);
//=== 空头日线条件 ===
Bool isMACDS_Short = Data2.MValue < -0.05 And IsLongD1 < 0 And Data2.Low[0] < Data2.Low[1];
ConD_Short = isMACDS_Short And Data2.MA60[1] > Data2.MA60;;
If(ConD_Short) Data2.PlotString("sell","v",Data2.High + 10);
Commentary("IsLongD:"+Text(IsLongD,2) + "IsLongD1:"+Text(IsLongD1,2) +
"Data2.Low[1]:"+Text(Data2.Low[1],2) + "Data2.MValue:"+Text(Data2.MValue,2)
+ "Data2.MValue[1]:"+Text(Data2.MValue[1],2) +
",ConD_Short:"+IIFString(ConD_Short,"Y","N"));
//=== 空头小时条件 ===
Bool isMACDS_ShortH = Data1.MValue <= 0 And Data1.Low[0] < Data1.Low[1];
ConH_Short = isMACDS_ShortH;
If(ConH_Short) Data1.PlotString("sell","v",Data1.High + 10);
//=== 空头5分钟条件 ===
ConM_Short = (IsLongM[2]>0 And IsLongM[1]<=0) Or (IsLongM[2]<0 And IsLongM[1]>0 And Low[2] < Low[1]);
If(ConM_Short) PlotString("sell","*",High + 10);
IsTradTime = !(Time>=0.1455 && Time<0.15);
Commentary("IsTradTime:"+IIFString(IsTradTime,"Y","N"));
// 开仓:多头
If(IsTradTime = True And ConM And ConD And ConH And MarketPosition<>1 And finalLots >= minLots)
{
Buy(finalLots, 0);
entry_price = EntryPrice;
stop_loss_price = Min(low[1],Low) - 2*tickSize;
Commentary("开多" + Text(finalLots) + "手,价:" + Text(entry_price,2));
PlotString("Buy","^",Low - 10);
StartTrail = False;
HighAfterEntry = High;
LowAfterEntry = 99999999;
}
// 空头
If(IsTradTime = True And ConM_Short And ConD_Short And ConH_Short And MarketPosition<>-1 And finalLots >= minLots)
{
SellShort(finalLots, 0);
entry_price = EntryPrice;
stop_loss_price = Max(High[1],High) + 2*tickSize;
Commentary("开空" + Text(finalLots) + "手,价:" + Text(entry_price,2));
PlotString("Short","V",High + 10);
StartTrail = False;
HighAfterEntry = 0;
LowAfterEntry = Low;
}
// 止损 & 保本
If(MarketPosition != 0 && stop_loss_price > 0)
{
}
// 移动止盈
If(MarketPosition != 0)
{
}
// 尾盘清仓
If(Time >= 0.1455 && Time < 0.15)
{
IsTradTime = False;
If(MarketPosition > 0 Or longCanSellV > 0)
{
Sell(0, 0);
Print(DateTimeToString(date+Time) + "尾盘清多,实仓:" + Text(longCanSellV) + "信号:" + Text(MarketPosition));
}
Else If(MarketPosition < 0 Or shortCanCoverV < 0)
{
BuyToCover(0, 0);
Print(DateTimeToString(date+Time) + "尾盘清空,实仓:" + Text(shortCanCoverV) + "信号:" + Text(MarketPosition));
}
}
If(Time == 0.15)
{
StartTrail = False;
entry_price = 0;
stop_loss_price = 0;
HighAfterEntry = 0;
LowAfterEntry = 99999999;
ConD = False;
ConH = False;
ConM = False;
ConD_Short = False;
ConH_Short = False;
ConM_Short = False;
}
}