// 名称: 日内短线,
记录30分钟内的高低点,突破后开仓;增加跟踪止损条件后,不能正常开仓了
// 类别: 公式应用
// 类型: 用户应用
// 输出: Void
//------------------------------------------------------------------------
Params
//此处添加参数
Numeric length(30);
Numeric end_time(0.1455);
Vars
//此处添加变量
Series<Numeric> bar_in_day;
Series<Numeric> highest_in_day;
Series<Numeric> lowest_in_day;
Series<Numeric> trade_count;
Numeric MinPoint; // 一个最小变动单位,也就是一跳
Numeric MyEntryPrice; // 开仓价格,本例为开仓均价,可设置为某次入场价
Numeric TrailingStart1(0.005); // 跟踪止损启动设置1
Numeric TrailingStart2(0.001); // 跟踪止损启动设置2
Numeric TrailingStop1(0.002); // 跟踪止损设置1
Numeric TrailingStop2(0.005); // 跟踪止损设置2
Numeric StopLossSet(0.005); // 止损设置
Numeric MyExitPrice; // 平仓价格
Series<Numeric> HighestAfterEntry; // 开仓后出现的最高价
Series<Numeric> LowestAfterEntry; // 开仓后出现的最低价
Defs
//此处添加公式函数
Events
//此处实现事件函数
//初始化事件函数,策略运行期间,首先运行且只有一次,应用在订阅数据等操作
OnBar(ArrayRef<Integer> indexs)
{
If(truedate(0)<>truedate(1))
{
bar_in_day =0;
highest_in_day = high;
lowest_in_day = low;
trade_count =0;
}
bar_in_day = bar_in_day + 1;
Commentary("当天第几根bar:"+text(bar_in_day));
If(bar_in_day<=length)
{
highest_in_day = max(highest_in_day,high);
lowest_in_day = min(lowest_in_day,low);
}
PlotNumeric("highest_in_day",highest_in_day);
PlotNumeric("lowest_in_day",lowest_in_day);
//突破上轨做到,突破下轨做空,交易次数不超过3次
If(MarketPosition==1 and low<=highest_in_day-10*minmove*pricescale)
{
sell(1,min(open,highest_in_day-10*minmove*pricescale));
}
If(MarketPosition==-1 and high>=lowest_in_day+10*minmove*pricescale)
{
BuyToCover(1,max(open,lowest_in_day+10*minmove*pricescale));
}
if(MarketPosition<>1 and high>=highest_in_day and bar_in_day>length and trade_count <2 and time<end_time)
{
buy(1,max(open,highest_in_day));
trade_count =trade_count +1;
}
if(MarketPosition<>-1 and low<=lowest_in_day and bar_in_day>length and trade_count <2 and time<end_time)
{
sellshort(1,min(open,lowest_in_day));
trade_count=trade_count+1;
}
Commentary("trade_count:"+text(trade_count));
If(time>=end_time and time<0.15)
{
buytocover(0,open);
Sell(0,open);
}
If(BarsSinceEntry == 0) // 条件满足:开仓Bar
{
HighestAfterEntry = Close;
LowestAfterEntry = Close; // 赋初值为当前最新价格
If(MarketPosition <> 0) // 有持仓时执行以下代码
{
// 开仓Bar,将开仓价和当时的收盘价的较大值保留到HighestAfterEntry
HighestAfterEntry = Max(HighestAfterEntry,AvgEntryPrice);
// 开仓Bar,将开仓价和当时的收盘价的较小值保留到LowestAfterEntry
LowestAfterEntry = Min(LowestAfterEntry,AvgEntryPrice);
}
}Else // 非开仓Bar时进行以下运算
{
// 记录下当前Bar的最高点,用于下一个Bar的跟踪止损判断
HighestAfterEntry = Max(HighestAfterEntry,High);
// 记录下当前Bar的最低点,用于下一个Bar的跟踪止损判断
LowestAfterEntry = Min(LowestAfterEntry,Low);
}
Commentary("HighestAfterEntry = "+Text(HighestAfterEntry));
Commentary("LowestAfterEntry = "+Text(LowestAfterEntry));
MinPoint = MinMove*PriceScale;
MyEntryPrice = AvgEntryPrice;
If(MarketPosition == 1 And BarsSinceEntry >= 1) // 有多仓的情况
{
// 第二级跟踪止损的条件表达式
If(HighestAfterEntry[1] >= MyEntryPrice + TrailingStart2*MyEntryPrice)
{
If(Low <= HighestAfterEntry[1] - TrailingStop2*MyEntryPrice)
{
MyExitPrice = HighestAfterEntry[1] - TrailingStop2*MyEntryPrice;// 如果该Bar开盘价即跳空触发,则用开盘价代替
If(Open < MyExitPrice) MyExitPrice = Open;
Sell(0,MyExitPrice);
}
}Else If(HighestAfterEntry[1] >= MyEntryPrice + TrailingStart1*MyEntryPrice) // 第一级跟踪止损的条件表达式
{
If(Low <= HighestAfterEntry[1] - TrailingStop1*MyEntryPrice)
{
MyExitPrice = HighestAfterEntry[1] - TrailingStop1*MyEntryPrice;
// 如果该Bar开盘价即跳空触发,则用开盘价代替
If(Open < MyExitPrice) MyExitPrice = Open;
Sell(0,MyExitPrice);
}
}Else If(Low <= MyEntryPrice - StopLossSet*MyEntryPrice) //可在此写初始止损处理
{
MyExitPrice = MyEntryPrice - StopLossSet*MyEntryPrice;
// 如果该Bar开盘价即跳空触发,则用开盘价代替
If(Open < MyExitPrice) MyExitPrice = Open;
Sell(0,MyExitPrice);
}
}Else If(MarketPosition == -1 And BarsSinceEntry >= 1) // 有空仓的情况
{
// 第二级跟踪止损的条件表达式
If(LowestAfterEntry[1] <= MyEntryPrice - TrailingStart2*MyEntryPrice)
{
If(High >= LowestAfterEntry[1] + TrailingStop2*MyEntryPrice)
{
MyExitPrice = LowestAfterEntry[1] + TrailingStop2*MyEntryPrice;
If(Open > MyExitPrice) MyExitPrice = Open;
BuyToCover(0,MyExitPrice);
}
}Else If(LowestAfterEntry[1] <= MyEntryPrice - TrailingStart1*MyEntryPrice)// 第一级跟踪止损的条件表达式
{
If(High >= LowestAfterEntry[1] + TrailingStop1*MyEntryPrice)
{
MyExitPrice = LowestAfterEntry[1] + TrailingStop1*MyEntryPrice;
If(Open > MyExitPrice) MyExitPrice = Open;
BuyToCover(0,MyExitPrice);
}
}Else If(High >= MyEntryPrice + StopLossSet*MyEntryPrice) //可在此写初始止损处理
{
MyExitPrice = MyEntryPrice + StopLossSet*MyEntryPrice;
If(Open > MyExitPrice) MyExitPrice = Open;
BuyToCover(0,MyExitPrice);
}
}
//...
}
策略是日内短线,
记录30分钟内的高低点,突破后开仓;增加跟踪止损条件后,不能正常开仓了。在标记位置应该开仓并触发一次跟踪止损,结果没有开仓;请教老师是哪里出了问题。
看置顶帖,要么投稿,要么看收费代编服务