//------------------------------------------------------------------------
// 名称: RB5M_MACD_DiffDea_Cross_FillSafe
// 说明: 螺纹钢 5 分钟
// 以 DIFF/DEA 金叉死叉为信号
// 允许同一根K线:先平仓,再反手开仓
// 使用追价限价提高成交概率(不使用撤单函数)
// TBQuant 1.4.4.5 风格:Params / Vars / Events / OnReady / OnBar(ArrayRef<Integer> indexs)
//------------------------------------------------------------------------
Params
Numeric FastEMA(12);
Numeric SlowEMA(26);
Numeric SignalEMA(9);
Numeric Lots(1);
Numeric PriceOffsetTick(5); // 追价跳数(你要求先用5)
Numeric TickSize(1); // RB最小变动一般为1(不确定就保持1)
Numeric MinBars(60); // 指标预热K线数(建议>= SlowEMA*2)
Numeric PrintEachBar(0); // 0=不打印;1=每根打印(排查用)
Vars
Numeric DIFF(0);
Numeric DEA(0);
Numeric LastDIFF(0); // 上一根 DIFF(手工保存,避免序列类型问题)
Numeric LastDEA(0); // 上一根 DEA
Bool Inited(False);
Numeric LastBar(0);
Numeric PxBuy(0);
Numeric PxSell(0);
Bool Golden(False);
Bool Dead(False);
Events
OnReady()
{
DIFF = 0;
DEA = 0;
LastDIFF = 0;
LastDEA = 0;
Inited = True;
LastBar = -1;
Commentary("RB5M DIFF/DEA Cross FillSafe 启动");
}
OnBar(ArrayRef<Integer> indexs)
{
If (Not Inited) Return;
// 防止同一根K线重复执行
If (CurrentBar == LastBar)
Return;
LastBar = CurrentBar;
// ----------------------------
// 每根K线都先计算指标(收盘值)
// ----------------------------
DIFF = XAverage(Close, FastEMA) - XAverage(Close, SlowEMA);
DEA = XAverage(DIFF, SignalEMA);
// 预热期:只更新上一根值,不交易
If (CurrentBar < MinBars)
{
LastDIFF = DIFF;
LastDEA = DEA;
If (PrintEachBar == 1)
Commentary("WarmUp Bar=" + Text(CurrentBar) + " DIFF=" + Text(DIFF) + " DEA=" + Text(DEA));
Return;
}
// ----------------------------
// 追价限价(尽量做成可成交限价)
// ----------------------------
PxBuy = Close + PriceOffsetTick * TickSize;
PxSell = Close - PriceOffsetTick * TickSize;
// ----------------------------
// 金叉/死叉(用上一根手工值)
// 金叉:DIFF 上穿 DEA
// 死叉:DIFF 下穿 DEA
// ----------------------------
Golden = (DIFF > DEA And LastDIFF <= LastDEA);
Dead = (DIFF < DEA And LastDIFF >= LastDEA);
If (PrintEachBar == 1)
Commentary("Bar=" + Text(CurrentBar)
+ " MP=" + Text(MarketPosition)
+ " DIFF=" + Text(DIFF)
+ " DEA=" + Text(DEA)
+ " LDIFF=" + Text(LastDIFF)
+ " LDEA=" + Text(LastDEA));
// ----------------------------
// 交易逻辑:同一根K线允许 先平后反手
// ----------------------------
// 金叉:做多
If (Golden)
{
If (MarketPosition == 0)
{
Commentary("GoldenCross -> Buy");
Buy(Lots, PxBuy);
}
Else If (MarketPosition == -1)
{
Commentary("GoldenCross -> Cover + Buy");
BuyToCover(Lots, PxBuy);
Buy(Lots, PxBuy);
}
}
// 死叉:做空
If (Dead)
{
If (MarketPosition == 0)
{
Commentary("DeadCross -> SellShort");
SellShort(Lots, PxSell);
}
Else If (MarketPosition == 1)
{
Commentary("DeadCross -> Sell + SellShort");
Sell(Lots, PxSell);
SellShort(Lots, PxSell);
}
}
// ----------------------------
// 最后:更新上一根值
// ----------------------------
LastDIFF = DIFF;
LastDEA = DEA;
}
这是AI写的吗?
谁写的问谁。ai写的问题ai