期货交易好多年,新接触程序化;自己编了好久没有成功,请老师指教,不吝感激.
策略思想:学习唐奇安通道(以做多为例;上轨20日(最高价),下轨10日)
1.破上轨开仓做多2手
2.记录:开仓bar当日上轨和下轨的距离H和开仓bar当日的最低点KL
3.分批离场:
a:自建仓日起,3个交易日内以KL为止损,之后取消
b:最高价大于H时,止损价提高到进场价(保本)
c:最高价大于2H时,平一手(部分止盈)
b:破10日全平(最大优先级)
程序如下:
Params
Numeric bolength(20); //进场周期
Numeric telength(10); //离市周期
Numeric OpenIntlots(50000); //持仓量设定
Numeric ExitBar(1); //开仓后交易日
Numeric oo(2); //开仓手数
Vars
NumericSeries upline;//上轨,延后1个bar
NumericSeries lowline;//下轨
Numeric myentryprice; //进场价
Numeric myexitprice; //平仓价
NumericSeries KL;
Numeric minpoint; //最小变动单位
Begin
If(!CallAuctionFilter()) Return;
upline=HighestFC(high[1],bolength);
lowline=LowestFC(low[1],telength);
minpoint=MinMove*pricescale;
PlotNumeric("upline",upline,0,Red);
PlotNumeric("lowline",lowline,0,Red);
// 进场
If(MarketPosition==0 and OpenInt[1]>OpenIntlots)
{
If(High>upline )
{
myentryprice=Min(high,upline+minpoint);
myentryprice=IIF(myentryprice<open,open,myentryprice);
Buy(oo,myentryprice);
}
}
//记录开仓日上下轨的距离
If(MarketPosition==1 And BarsSinceEntry == 0){
KL=upline-lowline;
}
//持仓量小于设定持仓量离场
If(MarketPosition==1 And OpenInt[1]<OpenIntlots ){
Sell(0,open);
}
//止损设置
If(MarketPosition==1 and OpenInt[1]>OpenIntlots)
{
Commentary("lowline="+Text(lowline));
//前三个交易日以开仓日的最低价为止损
If(BarsSinceEntry == 1){
If(Low<Low[1]){
myexitprice=Max(Low,Low[1]-minpoint);
myexitprice=IIF(myexitprice>open,open,myexitprice);
Sell(0,myexitprice);
}
}
If(BarsSinceEntry == 2){
If(Low<Low[2]){
myexitprice=Max(Low,Low[2]-minpoint);
myexitprice=IIF(myexitprice>open,open,myexitprice);
Sell(0,myexitprice);
}
}
//取消3个交易日限制
If(BarsSinceEntry > 3){
If(Low<lowline){
myexitprice=Max(low,lowline-minpoint);
myexitprice=IIF(myexitprice>open,open,myexitprice);
Sell(0,myexitprice);
}
}
}
//止盈设置
If(MarketPosition==1 and OpenInt[1]>OpenIntlots ){
//平仓一半
If(High>=EntryPrice+2*KL And A_BuyPosition==oo){
sell(oo/2,Close);
}
//保本
If(High>EntryPrice+KL-minpoint*5){
If(EntryPrice>=lowline-minpoint){
If(Low<EntryPrice){
myexitprice=Max(Low,EntryPrice-minpoint);
myexitprice=IIF(myexitprice>open,open,myexitprice);
Sell(0,myexitprice);
}Else{
If(Low<lowline){
myexitprice=Max(Low,lowline-minpoint);
myexitprice=IIF(myexitprice>open,open,myexitprice);
Sell(0,myexitprice);
}
}
}
}
}
End
谢谢
A函数只有实时交易才能取到值,不能混在这里。可以用CurrrentContracts代替 。
另外策略中,有多个出场,要做一些判断的,把容易触及的出场条件写在前面。