用的A函数持仓,买入时候没有问题,卖出时候,系统一直不发单,每个代码都研究了没有研究出来是哪个问题,barsSinceEntry最有可能的问题是出在这里,但不知道如何解决,麻烦帮忙看看,该如何修改,感谢,如果可以将正确的代码发一下
Params
Numeric MALength(20); // 短期均线周期(20)
Numeric LongMAPeriod(60); // 长期均线周期(60)
Numeric BreakTicks(2); // 突破跳数(向下突破)
Numeric FarTicks(5); // 远离均线跳数(基于最高价,向下远离)
Numeric BelowTicks(2); // 突破均线上方跳数(平空用)
Numeric MaxDeviationTicks(15); // 前一根K线最高点允许低于前两根均线的最大跳数(实际用 >= 均线-跳数)
Numeric MaxLossPercent(0.01); // 前一根K线最大跌幅(1%)
Numeric MaxJumpPercent(0.01); // 当前K线开盘相对于前一根K线最低点的最大跳空幅度
Numeric RiskAmount(600); // 每笔交易最大允许亏损金额(元)
Numeric MaxLots(20); // 最大允许开仓手数
Vars
Series<Numeric> MA20; // 20日均线
Series<Numeric> MA60; // 60日均线
Numeric MinPoint; // 最小变动价位
Series<Numeric> shortInitialStop; // 空头初始止损价(前一根K线最高点)
Series<Bool> shortFarFromMA; // 是否曾远离均线(基于某根K线最高价,向下远离)
Bool stopSig;//止损信号标志
Numeric stopPrice;//最终确定的止损价格。
Numeric dynamicStop;//动态止损计算出的临时价格。
Numeric lossPercent; // 前一根K线跌幅百分比
Bool enteredThisBar; // 同一Bar内是否已开仓(防重复开单)
Numeric lastBar; // 上一根Bar的编号
Numeric tradeLots; // 实际开仓手数
Numeric entryBar; // 实际开仓时的Bar编号(用于计算持仓时长)
Events
OnBar(ArrayRef<Integer> indexs)
{
if (CurrentBar != lastBar)
{
enteredThisBar = False;
lastBar = CurrentBar;
}
MinPoint = MinMove * PriceScale;
MA20 = Average(Close, MALength);
MA60 = Average(Close, LongMAPeriod);
if (A_SellPosition == 0)
{
shortInitialStop = 0;//空头初始止损价shortInitialStop
shortFarFromMA = False;//
entryBar = 0; }
。
if (A_SellPosition > 0 && entryBar == 0)
{
entryBar = CurrentBar; //
}
if (A_SellPosition == 0 && !enteredThisBar)
{
lossPercent = (Open[1] - Close[1]) / Open[1];
Bool entryCondition =
MA20[1] < MA60[1] && Close[1] < MA20[1] && Close[1] < Open[1] && High[1] >= MA20[2] - MaxDeviationTicks * MinPoint &&
lossPercent <= MaxLossPercent;
if (entryCondition)
{
Numeric shortEntryPrice = Low[1] - BreakTicks * MinPoint;
// 跳空限制:开盘价不低于前低 * (1 - MaxJumpPercent)
Bool noExcessiveGap = Open >= Low[1] * (1 - MaxJumpPercent);//
if (Low <= shortEntryPrice && noExcessiveGap)
{
Numeric actualPrice = Min(Open, shortEntryPrice);//入场价格
Numeric lossPerLot = (High[1] - actualPrice) * ContractUnit;//
if (lossPerLot > 0)
{
Numeric maxLotsByRisk = IntPart(RiskAmount / lossPerLot);//计算可买手数
Commentary("1手止损金额:" + Text(lossPerLot));
Commentary("含义:" + Text(MinPoint * ContractUnit));
if (maxLotsByRisk >= 1)
{
tradeLots = Min(maxLotsByRisk, MaxLots);
tradeLots = Max(tradeLots, 1);//开仓手数
Commentary("开仓手数:" + Text(tradeLots));
// 执行开空(SellShort 自动关联账户)
SellShort(tradeLots, actualPrice);
// 记录初始止损和状态
shortInitialStop = High[1]; //
shortFarFromMA = False;//
enteredThisBar = True;//
entryBar = CurrentBar; // 记录开仓Bar编号
Commentary("开仓Bar编号:" + Text(entryBar));
}
}
}
}
}
Commentary("A_SellPosition=" + Text(A_SellPosition));
if (A_SellPosition > 0)
{
Numeric barsSinceEntry = IIF(entryBar > 0, CurrentBar - entryBar, 9999);
Commentary("持仓经过的Bar数:" + Text(barsSinceEntry));
if (barsSinceEntry >= 1)
{
Commentary("持仓经过的Bar数:" + Text(barsSinceEntry));
if (High <= MA20 - FarTicks * MinPoint)
shortFarFromMA = True;// 是否曾远离均线(基于某根K线最高价,向下远离)
else
shortFarFromMA = shortFarFromMA[1];
stopSig = False;//
stopPrice = 0; //
if (High >= shortInitialStop)// //空头初始止损价shortInitialStop
{
stopSig = True;
stopPrice = shortInitialStop;
}
if (barsSinceEntry >= 4)
{
Commentary("3根K线" + Text(barsSinceEntry));
if (shortFarFromMA && High >= MA20[1] + BelowTicks * MinPoint)
{
dynamicStop = MA20[1] + BelowTicks * MinPoint;
if (stopSig)
stopPrice = Max(stopPrice, dynamicStop);
else
{
stopSig = True;
stopPrice = dynamicStop;
}
}
}
if (stopSig)
BuyToCover(0, Max(Open, stopPrice));
}
}
}
代码分析,要么发付费代写板块,要么投稿比较好。社区回帖很难说清楚。要么下午直播的时候讲一下
好的,如果可以的话,要么直播的时候说下,我晚上回去看回放
已发邮箱