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); // 最大允许开仓手数
numeric timepoint(20260319.1637); //希望出信号的起始时间
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编号(用于计算持仓时长)
//以下为修改部分
//global bool send_mark(False);//用作当前已发单的状态变量
global Numeric entryBar; // 实际开仓时的Bar编号(用于计算持仓时长)
Events
/* OnBarOpen(ArrayRef<Integer> indexes)
{
send_mark = False;//对已报单的状态变量做重置
}*/
OnBar(ArrayRef<Integer> indexs)
{
if (CurrentBar != lastBar)//
{
enteredThisBar = False;
lastBar = CurrentBar;
}//完全不对
MinPoint = MinMove * PriceScale;
MA20 = Average(Close, MALength);
MA60 = Average(Close, LongMAPeriod);
if (MarketPosition == 0)
{
shortInitialStop = 0;//空头初始止损价shortInitialStop
shortFarFromMA = False;//
entryBar = 0;
}
if (MarketPosition == -1 && entryBar == 0)
{
entryBar = CurrentBar; //
}
//if (A_SellPosition == 0 && !enteredThisBar)
if (MarketPosition <> -1)// && send_mark)//sendorder后注意关闭sned_mark开关
{
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 and date+time>=timepoint )
{
tradeLots = Min(maxLotsByRisk, MaxLots);
tradeLots = Max(tradeLots, 1);//开仓手数
Commentary("开仓手数:" + Text(tradeLots));
// 执行开空(SellShort 自动关联账户)
SellShort(tradeLots, actualPrice);
/*Array<integer> orderid;
A_SendOrderEx(Enum_Sell,Enum_Entry,tradeLots,actualPrice,orderid);*/
// 记录初始止损和状态
shortInitialStop = High[1]; //
shortFarFromMA = False;//
enteredThisBar = True;//
entryBar = CurrentBar; // 记录开仓Bar编号
Commentary("开仓Bar编号:" + Text(entryBar));
}
}
}
}
}
//Commentary("A_SellPosition=" + Text(A_SellPosition));
if (MarketPosition==-1)
{
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));
}
}
}