//------------------------------------------------------------------------
// 简称: StrictYinYangBreak
// 名称: 严格阴阳K线突破策略
// 类别: 公式应用
// 类型: 内建应用
// 输出:
//------------------------------------------------------------------------
/*
策略说明:
1. 开仓手数固定为1手
2. 平空&做多条件:
- 记录所有阴K线收盘价
- 当最新收盘价末次大于阴K线收盘价10元时
- 先平空仓(如有),再开多仓
- 开多仓后,必须平多仓后才能再次开多仓
3. 平多&做空条件:
- 记录所有阳K线收盘价
- 当最新收盘价末次小于阳K线收盘价10元时
- 先平多仓(如有),再开空仓
- 开空仓后,必须平空仓后才能再次开空仓
*/
Params
Numeric Lots(1); // 固定交易手数
Numeric BreakThreshold(10); // 突破阈值,即10元
Vars
Series<Numeric> yinCloses; // 存储阴K线收盘价
Series<Numeric> yangCloses; // 存储阳K线收盘价
Bool canEnterLong(True); // 允许开多标志
Bool canEnterShort(True); // 允许开空标志
Numeric yinCount(0); // 阴K线计数
Numeric yangCount(0); // 阳K线计数
Bool lastBreakYang(False); // 记录上次阳突破状态
Bool lastBreakYin(False); // 记录上次阴突破状态
Events
OnBar(ArrayRef<Integer> indexs)
{
// 收集阴K线收盘价(Close < Open)
If(Close < Open) {
yinCloses = Close;
yinCount = yinCount + 1;
} Else {
yinCloses = yinCloses[1]; // 保持前值
}
// 收集阳K线收盘价(Close > Open)
If(Close > Open) {
yangCloses = Close;
yangCount = yangCount + 1;
} Else {
yangCloses = yangCloses[1]; // 保持前值
}
//============== 平空&做多条件 ==============
// 当前突破状态
Bool currentBreakYang = yinCount > 0 And Close > yinCloses + BreakThreshold;
// 末次突破条件:上次未突破且当前突破
If(Not lastBreakYang And currentBreakYang And canEnterLong) {
// 先平空仓(如有)
If(MarketPosition == -1) {
BuyToCover(Lots, Open);
}
// 开多仓
Buy(Lots, Open);
// 设置仓位控制标志
canEnterLong = False; // 禁止再次开多
canEnterShort = True; // 允许开空
}
// 更新突破状态记录
lastBreakYang = currentBreakYang;
//============== 平多&做空条件 ==============
// 当前突破状态
Bool currentBreakYin = yangCount > 0 And Close < yangCloses - BreakThreshold;
// 末次突破条件:上次未突破且当前突破
If(Not lastBreakYin And currentBreakYin And canEnterShort) {
// 先平多仓(如有)
If(MarketPosition == 1) {
Sell(Lots, Open);
}
// 开空仓
SellShort(Lots, Open);
// 设置仓位控制标志
canEnterShort = False; // 禁止再次开空
canEnterLong = True; // 允许开多
}
// 更新突破状态记录
lastBreakYin = currentBreakYin;
//============== 仓位状态监控 ==============
// 当仓位归零时重置开仓标志
If(MarketPosition == 0) {
canEnterLong = True;
canEnterShort = True;
}
}
//------------------------------------------------------------------------
// 编译版本 GS2023.01.01
// 版权所有 TradeBlazer Software 2003-2025
//------------------------------------------------------------------------
你随便三个条件,你检查能不能同时满足