利用双均线系统,设置开仓位置前5根K线的最低点的0.99倍止损,采用以下编码,为什么报错,该怎么修改。
Params
Numeric EFast(5); //短均线周期参数
Numeric ESlow(10); //长均线周期参数
Numeric StopLength(5); //止损统计周期数
Numeric PostionLots(1);//头寸
Vars
Series<Numeric> MAEFast; //短均线
Series<Numeric> MAESlow; //长均线
Series<Numeric> Opint1; // 逆解交叉点值
Series<Numeric> AvgTR;
Numeric ProtectStopPrice; //保护性止损价
Numeric TrailStopPrice; //跟踪性止损价
Series<Numeric> MyEntryPrice; // 开仓价格
Events
OnBar(ArrayRef<Integer> indexs)
{
MAEFast = Average(Close, EFast); //短均线
MAESlow = Average(Close, ESlow); //长均线
Opint1 = ((ESlow - 1) * Average(Close[1], ESlow - 1) * EFast - (EFast - 1) * Average(Close[1], EFast - 1) * ESlow) / (ESlow - EFast);
ProtectStopPrice = Lowest(Low[1],StopLength) * 0.99;
TrailStopPrice = Highest(High[1],StopLength) * 1.01;
If(MarketPosition <> 1 And MAEFast[1]<MAESlow[1] and High >= Opint1)
{
Buy(PostionLots, Max(Open, Opint1));
MyEntryPrice = Max(Open, Opint1);
ProtectStopPrice = Lowest(Low[1],StopLength) * 0.99;
TrailStopPrice = Highest(High[1],StopLength) * 1.01;
}
If(MarketPosition<>1 And Low[1]<=Opint1[1] And Close[1]>=Opint1[1] And High >= Opint1) //过滤假死叉
{
Buy(PostionLots,Max(Open,Opint1));
ProtectStopPrice = Lowest(Low[1],StopLength) * 0.99;
TrailStopPrice = Highest(High[1],StopLength) * 1.01;
}
If(MarketPosition == 1 And BarsSinceEntry > 0 And Low <= ProtectStopPrice)
{
Sell(PostionLots, ProtectStopPrice);
}
If(MarketPosition == 1 And BarsSinceEntry > 0 And High[1]>=Opint1[1] And Close[1]<=Opint1[1] And Low <= Opint1) //过滤假金叉
{
Sell(PostionLots, Min(Open, opint1));
}
If(MarketPosition == 1 And BarsSinceEntry > 0 And MAEFast[1]> MAESlow[1] And Low<Opint1)
{
Sell(PostionLots,Min(Open,Opint1));
}
Range[0:DataSourceSize() - 1]
{
PlotNumeric("MAEFast", MAEFast);
PlotNumeric("MAESlow", MAESlow);
}
}
//------------------------------------------------------------------------
// 编译版本 2025/3/28 202727
// 版权所有 winter110
// 更改声明 TradeBlazer Software保留对TradeBlazer平台
在分支语句中执行有序列数据参与的函数,容易出现一些bar的序列数据不是你想象中的样子
老师这节课讲的很详细
https://video.tbquant.net/video?id=video443
(给我自己也找补一下 看了也容易忘)
我的意思是后续行情发展后,固定开仓当根bar的前5根K线的最低点止损,而不是跟随到当根K的前5根的低点,该怎么编写。
你把这个处理掉就可以了呀
在分支语句外面,每一个bar都计算一下这个值;
在开仓的时候,取到开仓时刻的这个值不就行了
ProtectStopPrice = Lowest(Low[1],StopLength) * 0.99;
TrailStopPrice = Highest(High[1],StopLength) * 1.01;
If(MarketPosition <> 1 And MAEFast[1]<MAESlow[1] and High >= Opint1)
{
Buy(PostionLots, Max(Open, Opint1));
MyEntryPrice = Max(Open, Opint1);
ProtectStopPrice_buy = ProtectStopPrice; //这个变量类型为序列类型,止损用这个
TrailStopPrice_buy = TrailStopPrice;
}