//------------------------------------------------------------------------
// 简称: MT582
// 名称: 5081
// 类别: 公式应用
// 类型: 用户应用
// 输出: Void
//------------------------------------------------------------------------
Params
// MA参数
Numeric FastMA(5); // 快速EMA周期
Numeric MediumMA(10); // 中速EMA周期
Numeric SlowMA(60); // 慢速EMA周期
Numeric HourFastMA(5); // 1小时快速EMA周期
Numeric HourSlowMA(20); // 1小时慢速EMA周期
// MACD参数
Numeric MACDFast(6); // MACD快线周期
Numeric MACDSlow(13); // MACD慢线周期
Numeric MACDSignal(5); // MACD信号周期
// 动态ATR风控
Numeric ATRPeriod(14);
Numeric EntryATRFactor(1.3); // 入场ATR倍数
Numeric StopLossATR(2); // 止损ATR倍数
Numeric TakeProfitATR(5); // 止盈ATR倍数
// 成交量过滤
Numeric VolumeLength(5); // 成交量均线周期
Numeric VolumeFactor(1.2); // 成交量放大阈值
// 资金管理
Numeric MinLots(1); // 开仓手数
Numeric RiskPercent(1); // 单笔风险百分比
Numeric MaxLots(100); // 最大手数限制
Numeric ContractNumber(1); //品种数量
Numeric MyPosition(0.618); //品种仓位
Numeric millsecs(1000);
Vars
// MA变量
Series<Numeric> MA10; //小周期
Series<Numeric> MA20;
Series<Numeric> MA60;
Series<Numeric> H_MA5; //大周期
Series<Numeric> H_MA20;
//MACD变量
Series<Numeric> MACDDiff;
Series<Numeric> AvgMACD;
Series<Numeric> MACDValue;
// ATR变量
Series<Numeric> ATRVal;
Series<Numeric> Lots(1); // 开仓手数
Series<Numeric> MyLots(1); // 手数
Series<Numeric> MyLots2(1); // 手数
// 成交量均线
Series<Numeric> VolMA;
// 开仓价格
Series<Bool> LongTrend(False); //多头趋势
Series<Numeric> LongCount(0); //多头入场计数
Series<Numeric> LongEntryPrice1; //多头入场价
Series<Numeric> LongEntryPrice2;
Series<Numeric> LongEntryPrice3;
Series<Numeric> ShortEntryPrice1; //空头入场价
Series<Numeric> ShortEntryPrice2;
Series<Numeric> ShortEntryPrice3;
Series<Numeric> LongStopLossPrice1; //多头止损价
Series<Numeric> LongStopLossPrice2;
Series<Numeric> LongStopLossPrice3;
Series<Numeric> LongTakeProfitPrice1; //多头止盈价
Series<Numeric> LongTakeProfitPrice2;
Series<Numeric> LongTakeProfitPrice3;
Series<Numeric> myEntryPrice; //入场价
Series<Numeric> myStopLossPrice; //止损价
Series<Numeric> myTakeProfitPrice; //止盈价
Series<Numeric> dynamicStopLoss; // 动态移动止损价
Numeric myStopLossPrice_S; //空头止损
// Numeric myTakeProfitPrice; //止盈
Bool UpTrend(False); //多头
Bool DnTrend(False); //空头
Events
//此处实现事件函数
//初始化事件函数,策略运行期间,首先运行且只有一次,应用在订阅数据等操作
OnInit()
{
}
//在所有的数据源准备完成后调用,应用在数据源的设置等操作
OnReady()
{
SetBackBarMaxCount(1+Max(Max(FastMA,MediumMA),SlowMA));
}
//在新bar的第一次执行之前调用一次,参数为新bar的图层数组
OnBarOpen(ArrayRef<Integer> indexs)
{
}
//Bar更新事件函数,参数indexs表示变化的数据源图层ID数组
OnBar(ArrayRef<Integer> indexs)
{
Range[0:DataSourceSize() - 1]
{
//小周期均线计算
MA10 = AverageFC(Close,FastMA);
MA20 = AverageFC(Close,MediumMA);
MA60 = AverageFC(Close,SlowMA);
PlotNumeric("MA10",MA10);
PlotNumeric("MA20",MA20);
PlotNumeric("MA60",MA60);
//大周期均线计算
H_MA5 = AverageFC(Data1.Close,HourFastMA);
H_MA20 = AverageFC(Data1.Close,HourSlowMA);
//MACD计算
MACDDiff = XAverage(Close, MACDFast ) - XAverage(Close, MACDSlow ) ;
AvgMACD = XAverage(MACDDiff,MACDSignal);
MACDValue = 2 * (MACDDiff - AvgMACD);
// 波动性与成交量
ATRVal = AvgTrueRange(ATRPeriod);
VolMA = Average(Vol, VolumeLength);
}
//多空趋势判断处理
If(H_MA5>H_MA20)
{
LongTrend=True;
}
Else If(H_MA5<H_MA20)
{
LongTrend=False ;
}
//多头1次止损止盈
If(MarketPosition == 1 And BarsSinceEntry >= 1)
{
If(Low <= LongAvgEntryPrice - ATRVal*StopLossATR)
{
Sell(0,Close);
LongCount=0;
}
Else If(High >= LongAvgEntryPrice + ATRVal*TakeProfitATR)
{
Sell(0,Close);
LongCount=0;
}
}
//多头1次补仓
If(MarketPosition == 1 And LongTrend And LongCount == 1)
{
If(Low<LongAvgEntryPrice-ATRVal)
{
LongEntryPrice2=LongAvgEntryPrice-ATRVal;
Buy(Lots*2,LongEntryPrice2);
LongCount=2;
}
}
//多头2次补仓
If(MarketPosition == 1 And LongTrend And LongCount == 2)
{
If(Low<LongAvgEntryPrice-ATRVal)
{
LongEntryPrice3=LongAvgEntryPrice-ATRVal;
//Buy(Lots*4,LongEntryPrice3);
//LongCount=3;
}
}
//空头1次止损止盈
If(MarketPosition == -1 And BarsSinceEntry >= 1)
{
If(High >= shortAvgEntryPrice + ATRVal*StopLossATR)
{
BuyToCover(0,Close);
LongCount=0;
}
Else If(Low <= shortAvgEntryPrice - ATRVal*TakeProfitATR)
{
BuyToCover(0,Close);
LongCount=0;
}
}
//空头1次补仓
If(MarketPosition == -1 And LongTrend==False And LongCount == 1)// And BarsSinceEntry >= 1)
{
If(High >shortAvgEntryPrice+ATRVal)
{
ShortEntryPrice2=shortAvgEntryPrice+ATRVal;
SellShort(Lots*2,ShortEntryPrice2);
LongCount=2;
}
}
//空头2次补仓
If(MarketPosition == -1 And LongTrend==False And LongCount == 2)
{
If(High >shortAvgEntryPrice+ATRVal)
{
ShortEntryPrice3=shortAvgEntryPrice+ATRVal;
//SellShort(Lots*4,ShortEntryPrice3);
//LongCount=3;
}
}
}
//下一个Bar开始前,重新执行当前bar最后一次,参数为当前bar的图层数组
OnBarClose(ArrayRef<Integer> indexs)
{
Numeric MyCurrentEquity; //可用资金
Numeric MyClose; //当前价
Numeric MyMarginRatio; //保证金
Numeric MyContractUnit; //合约倍数
MyCurrentEquity=Portfolio_CurrentEquity();
MyClose=Close;
MyMarginRatio=marginRatio;
MyContractUnit=ContractUnit;
MyLots2=IntPart((MyCurrentEquity*MyPosition)/(MyClose*MyMarginRatio*MyContractUnit));
MyLots=IntPart(MyLots2/3/ContractNumber);
//Lots=MinLots;
Lots= Min(MaxLots,MyLots);
Lots= Max(MinLots,Lots);
Bool B_CON1=H_MA5>H_MA20;
Bool B_CON2=CrossOver(MA10,MA20);
//Bool B_CON2=MA10[1]<=MA20[1] And MA10>MA20;
Bool S_CON1=H_MA5<H_MA20;
Bool S_CON2=CrossUnder(MA10,MA20);
//Bool S_CON2=MA10[1]>=MA20[1] And MA10<MA20;
//多头入场
If(MarketPosition != 1 And H_MA5>H_MA20 And B_CON2)
//If(H_MA5>H_MA20 And B_CON2)
{
LongEntryPrice1=Close;
Buy(Lots,LongEntryPrice1);
LongCount=1;
}
//多头出场
If(MarketPosition == 1 And BarsSinceEntry >= 1)
{
If(H_MA5<H_MA20)
{
Sell(0,Close);
LongCount=0;
}
}
//空头入场
If(MarketPosition != -1 And H_MA5<H_MA20 And S_CON2)
//If(H_MA5<H_MA20 And S_CON2)
{
ShortEntryPrice1 = Close;
SellShort(Lots,ShortEntryPrice1);
LongCount=1;
}
//}
//空头出场
If(MarketPosition == -1 And BarsSinceEntry >= 1)
{
If(H_MA5>H_MA20)
{
BuyToCover(0, Close);
LongCount=0;
}
}
}
//------------------------------------------------------------------------
// 编译版本 2025/08/30 091932
// 版权所有 Billszhan9702
// 更改声明 TradeBlazer Software保留对TradeBlazer平台
// 每一版本的TradeBlazer公式修改和重写的权利
//------------------------------------------------------------------------
您好!老师,我应该是想要处理跨周期,就是15分钟加入1小时周期方向判断,所以把增加图层误认为是跨周期处理方法,请老师指导我学习跨周期处理方法。谢谢!
If(MarketPosition != 1 And H_MA5>H_MA20 And B_CON2)
首先可以看到开仓的代码
那么逐个检查
H_MA5>H_MA20 And B_CON2
后发现
要跨周期,主贴内你也没有提到这个问题,还是你不知道这个事?
然后自己叠加一个图层
但又有一个问题,这个信号的出现是你描述的问题吗?
综上所述,用户需要学会准确描述自己的问题。