Params
// === 资金管理参数 ===
Numeric Money(100); // 账户资金(万元)
Numeric CapitalRatio(80); // 资金使用比例(%),默认为80%
// === 三分钟周期参数 ===
Numeric Boll_Length(3); // BOLL均线周期
Numeric Boll_Width(5); // BOLL带宽倍数
Numeric Boll_Shift(1); // BOLL偏移量
Numeric Macd_Fast(6); // MACD快线周期
Numeric Macd_Slow(13); // MACD慢线周期
Numeric Macd_Signal(9); // MACD信号线周期
Vars
// === 三分钟周期指标 ===
Series<Numeric> Boll_Mid; // BOLL中轨
Series<Numeric> Boll_Std; // BOLL标准差
Series<Numeric> Boll_Top; // BOLL上轨
Series<Numeric> Boll_Bottom; // BOLL下轨
Series<Numeric> Macd_FastMA; // MACD快EMA
Series<Numeric> Macd_SlowMA; // MACD慢EMA
Series<Numeric> Macd_Diff; // MACD差值
Series<Numeric> Macd_Dea; // MACD信号线
Series<Numeric> Macd_Histogram; // MACD柱状图
Series<Bool> Macd_GoldenCross; // MACD金叉标志
Series<Bool> Macd_DeadCross; // MACD死叉标志
// === 综合交易信号 ===
Bool LongEntrySignal; // 多头入场信号
Bool ShortEntrySignal; // 空头入场信号
Bool LongExitSignal; // 多头出场信号
Bool ShortExitSignal; // 空头出场信号
// === 时间管理 ===
Numeric CurrentHour; // 当前小时
Numeric CurrentMinute; // 当前分钟
Numeric CurrentTime; // 当前时间(HHMM格式)
Bool AllowTrading; // 允许交易标志
// === 仓位管理 ===
Numeric PositionSize; // 持仓数量
Numeric TradingCapital; // 实际交易资金
// === 调试变量 ===
Bool DebugMode; // 调试模式
Integer BarCounter; // Bar计数器
Integer SignalCounter; // 信号计数器
Events
// 初始化事件
OnInit()
{
// 只订阅3分钟数据,简化调试
SubscribeBar(Symbol, "3m", BeginDateTime);
// 初始化调试变量
DebugMode = True;
BarCounter = 0;
SignalCounter = 0;
}
// 主要的Bar事件处理
OnBar(ArrayRef<Integer> indexs)
{
// 在OnBar事件最开头,时间过滤之后加入:
If (DebugMode && BarStatus == 2 && BarCounter == 30 && MarketPosition == 0) {
Buy(1, Close);
Commentary("====== 强制测试开仓!Bar计数=" + Text(BarCounter) + " ======");
Return; // 执行后直接返回,避免干扰
}
BarCounter = BarCounter + 1;
// -----------------------------------------------------------
// 第一部分:时间过滤条件(放宽条件)
// -----------------------------------------------------------
CurrentHour = Hour;
CurrentMinute = Minute;
CurrentTime = CurrentHour * 100 + CurrentMinute;
// 默认允许交易(放宽时间过滤)
AllowTrading = True;
// 调试输出:每根Bar都输出
If (DebugMode)
{
Commentary("Bar#" + Text(BarCounter) +
" 时间:" + Text(CurrentTime) +
" 价格:" + Text(Close) +
" 持仓:" + Text(MarketPosition));
}
// -----------------------------------------------------------
// 第二部分:三分钟周期指标计算
// -----------------------------------------------------------
// 1. 计算BOLL指标
If (CurrentBar >= Boll_Length)
{
Boll_Mid = Average(Close, Boll_Length);
Boll_Std = StandardDev(Close, Boll_Length, 1);
Boll_Top = Boll_Mid + Boll_Width * Boll_Std;
Boll_Bottom = Boll_Mid - Boll_Width * Boll_Std;
// 应用偏移量
Boll_Top = Boll_Top + Boll_Shift * AvgTrueRange(Boll_Length);
Boll_Bottom = Boll_Bottom - Boll_Shift * AvgTrueRange(Boll_Length);
}
// 2. 计算MACD指标
Macd_FastMA = XAverage(Close, Macd_Fast);
Macd_SlowMA = XAverage(Close, Macd_Slow);
Macd_Diff = Macd_FastMA - Macd_SlowMA;
Macd_Dea = XAverage(Macd_Diff, Macd_Signal);
Macd_Histogram = 2 * (Macd_Diff - Macd_Dea);
// 判断MACD金叉死叉
Macd_GoldenCross = CrossOver(Macd_Diff, Macd_Dea);
Macd_DeadCross = CrossUnder(Macd_Diff, Macd_Dea);
// -----------------------------------------------------------
// 第三部分:交易信号生成(简化版)
// -----------------------------------------------------------
LongEntrySignal = False;
ShortEntrySignal = False;
// 方案A:简单的MACD金叉死叉交易(测试用)
// If (AllowTrading And MarketPosition == 0)
// {
// LongEntrySignal = Macd_GoldenCross;
// ShortEntrySignal = Macd_DeadCross;
// }
// 方案B:增加一些过滤条件
If (AllowTrading And MarketPosition == 0 And CurrentBar > 10)
{
// 简单的多头条件:MACD金叉且价格在BOLL中轨之上
If (CurrentBar >= Boll_Length)
{
LongEntrySignal = Macd_GoldenCross And Close > Boll_Mid;
ShortEntrySignal = Macd_DeadCross And Close < Boll_Mid;
}
Else
{
LongEntrySignal = Macd_GoldenCross;
ShortEntrySignal = Macd_DeadCross;
}
}
// 平仓条件
LongExitSignal = False;
ShortExitSignal = False;
If (MarketPosition == 1)
{
// 多头平仓:MACD死叉或价格跌破BOLL中轨
If (CurrentBar >= Boll_Length)
{
LongExitSignal = Macd_DeadCross Or Close < Boll_Mid;
}
Else
{
LongExitSignal = Macd_DeadCross;
}
// 或者固定止损止盈
If (Close > EntryPrice * 1.02) // 盈利2%平仓
{
LongExitSignal = True;
}
Else If (Close < EntryPrice * 0.98) // 亏损2%平仓
{
LongExitSignal = True;
}
}
If (MarketPosition == -1)
{
// 空头平仓:MACD金叉或价格突破BOLL中轨
If (CurrentBar >= Boll_Length)
{
ShortExitSignal = Macd_GoldenCross Or Close > Boll_Mid;
}
Else
{
ShortExitSignal = Macd_GoldenCross;
}
// 或者固定止损止盈
If (Close < EntryPrice * 0.98) // 盈利2%平仓
{
ShortExitSignal = True;
}
Else If (Close > EntryPrice * 1.02) // 亏损2%平仓
{
ShortExitSignal = True;
}
}
// -----------------------------------------------------------
// 第四部分:交易指令执行
// -----------------------------------------------------------
// 计算开仓手数
Numeric contractValue = Close * ContractUnit * BigPointValue;
Numeric totalCapital = Money * 10000;
TradingCapital = totalCapital * CapitalRatio / 100;
PositionSize = IntPart(TradingCapital / contractValue);
If (PositionSize < 1)
{
PositionSize = 1;
}
// 只在Bar结束时执行交易
If (BarStatus == 2)
{
// 1. 多头开仓
If (LongEntrySignal)
{
Buy(PositionSize, Close);
SignalCounter = SignalCounter + 1;
// 输出详细信息
If (DebugMode)
{
Commentary("========== 多头开仓 #" + Text(SignalCounter) + " ==========");
Commentary("时间: " + Text(Date) + " " + Text(Time));
Commentary("价格: " + Text(Close) + " | 手数: " + Text(PositionSize));
}
// 在K线上标注
PlotString("B", "B", Low * 0.999, Cyan, 0);
}
// 2. 空头开仓
If (ShortEntrySignal)
{
SellShort(PositionSize, Close);
SignalCounter = SignalCounter + 1;
// 输出详细信息
If (DebugMode)
{
Commentary("========== 空头开仓 #" + Text(SignalCounter) + " ==========");
Commentary("时间: " + Text(Date) + " " + Text(Time));
Commentary("价格: " + Text(Close) + " | 手数: " + Text(PositionSize));
}
// 在K线上标注
PlotString("S", "S", High * 1.001, Magenta, 0);
}
// 3. 多头平仓
If (LongExitSignal And MarketPosition == 1)
{
Sell(0, Close);
// 输出详细信息
If (DebugMode)
{
Commentary("========== 多头平仓 ==========");
Commentary("时间: " + Text(Date) + " " + Text(Time));
Commentary("价格: " + Text(Close));
}
// 在K线上标注
PlotString("XL", "XL", High * 1.001, Yellow, 0);
}
// 4. 空头平仓
If (ShortExitSignal And MarketPosition == -1)
{
BuyToCover(0, Close);
// 输出详细信息
If (DebugMode)
{
Commentary("========== 空头平仓 ==========");
Commentary("时间: " + Text(Date) + " " + Text(Time));
Commentary("价格: " + Text(Close));
}
// 在K线上标注
PlotString("XS", "XS", Low * 0.999, White, 0);
}
}
// -----------------------------------------------------------
// 第五部分:调试输出
// -----------------------------------------------------------
// 每50根Bar输出一次统计信息
If (DebugMode And BarCounter % 50 == 0)
{
Commentary("统计信息 - 总Bar数:" + Text(BarCounter) +
" 总信号数:" + Text(SignalCounter) +
" 当前持仓:" + Text(MarketPosition));
}
// 输出MACD状态(每30根Bar)
If (DebugMode And BarCounter % 30 == 0)
{
String macdGoldenStr;
If (Macd_GoldenCross)
{
macdGoldenStr = "是";
}
Else
{
macdGoldenStr = "否";
}
String macdDeadStr;
If (Macd_DeadCross)
{
macdDeadStr = "是";
}
Else
{
macdDeadStr = "否";
}
Commentary("MACD状态 - DIFF:" + Text(Macd_Diff) +
" DEA:" + Text(Macd_Dea) +
" 金叉:" + macdGoldenStr +
" 死叉:" + macdDeadStr);
}
}
@管理员来捞捞捞我