//------------------------------------------------------------------------
// 简称: MA_Offset_Strategy
// 名称: 基于5日均线偏移的买卖策略(带止损)
// 类别: 公式应用
// 类型: 用户应用
// 输出: Void
//------------------------------------------------------------------------
Params
Numeric Lots(3); // 每次交易手数
Numeric OffsetBuyIn(6); // 偏移点数
Numeric OffsetSellIn(6);
Numeric OffsetSellCover(6); // 偏移点数
Numeric OffsetBuyCover(8);
Numeric StopLoss(10); // 止损点数
Vars
Numeric MA5; // 5日均线
Numeric BuyInLevel; // 买入价格
Numeric SellCoverLevel; // 卖出价格
Numeric SellInLevel; // 做空价格
Numeric BuyCoverLevel; // 平空价格
Numeric EntryPrice; // 开仓价(用于止损计算)
Events
OnInit()
{
Commentary("基于5日均线偏移的买卖策略启动(带止损)");
}
OnBar(ArrayRef<Integer> indexs)
{
MA5 = Average(Close, 5);
BuyInLevel = MA5 - OffsetBuyIn;
SellCoverLevel = MA5 + OffsetSellCover;
SellInLevel = MA5 + OffsetSellIn;
BuyCoverLevel = MA5 - OffsetBuyCover;
// 开仓
if (MarketPosition == 0)
{
if (High >= SellInLevel)
{
SellShort(Lots, SellInLevel);
EntryPrice = SellInLevel;
Commentary("做空开仓,价格:" + Text(SellInLevel));
}
if (Low <= BuyInLevel)
{
Buy(Lots, BuyInLevel);
EntryPrice = BuyInLevel;
Commentary("做空开仓,价格:" + Text(BuyInLevel));
}
}
// 持有多仓时
if (MarketPosition == 1)
{
// 原有平仓逻辑
if (High >= SellCoverLevel)
{
Sell(Lots, SellCoverLevel);
Commentary("卖出平多,价格:" + Text(SellCoverLevel));
}
// 止损逻辑(当前价低于开仓价 StopLoss 点)
else if (Low <= EntryPrice - StopLoss)
{
Sell(Lots, EntryPrice - StopLoss);
Commentary("多仓止损,价格:" + Text(EntryPrice - StopLoss));
}
}
// 持有空仓时
if (MarketPosition == -1)
{
// 原有平仓逻辑
if (Low <= BuyCoverLevel)
{
BuyToCover(Lots, BuyCoverLevel);
Commentary("平空仓,价格:" + Text(BuyCoverLevel));
}
// 止损逻辑(当前价高于开仓价 StopLoss 点)
else if (High >= EntryPrice + StopLoss)
{
BuyToCover(Lots, EntryPrice + StopLoss);
Commentary("空仓止损,价格:" + Text(EntryPrice + StopLoss));
}
}
}
以上,看了一圈没有可以闪烁的地方啊,都是high和low,没有多图层,请各位大神们解答
SellInLevel 这个用了最新close吧 盲猜是这个
还有 buy是这个两个
👍
那我应该如何实时更新ma5的值呀,不能用Average(Close[1], 5)吧
均线你回溯了才会不闪烁Average(Close[1], 5)
那这样得到的就不是包括当日的ma5日均线了吧?是前6天到前一天的,我的代码逻辑是不是要改了