- 精华
- 0
- 在线时间
- 22 小时
- UID
- 187472
- 积分
- 9
- 帖子
- 8
- 阅读权限
- 10
- 注册时间
- 2014-5-26
- 最后登录
- 2015-4-30
- 精华
- 0
- UID
- 187472
- 积分
- 9
- 帖子
- 8
- 主题
- 1
- 阅读权限
- 10
- 注册时间
- 2014-5-26
- 最后登录
- 2015-4-30
|
米小兔 发表于 2014-7-13 11:48
仅看楼主描述写了一点代码, 如果知道策略上下文, 可能还可以简化写法.
Params
Numeric FastLength(12);
Numeric SlowLength(26);
Numeric MACDLength(9);
Numeric Length(20);//20日指数移动平均
Numeric M(4);//进场必须距离20日指数移动平均的距离,目的是过滤假突破;剩余一半仓位距离20日指数平均的距离后出场
Vars
NumericSeries MACDValue;
Numeric AvgMACD;
Numeric MACDDiff;
Bool con1;
Bool con2;
Bool con3;
Bool con4;
Numeric MinPoint;
NumericSeries EMA20;
Numeric myEntryprice;//进场价格
Numeric myExitPrice;
NumericSeries a(0);
NumericSeries b(0);
Begin
MinPoint=MinMove*PriceScale;
MinPoint = MinMove*PriceScale;
EMA20=XAverage(Close, Length);
PlotNumeric("EMA20",EMA20);
MACDValue = XAverage( Close, FastLength ) - XAverage( Close, SlowLength ) ;
AvgMACD = XAverage(MACDValue,MACDLength);
MACDDiff = MACDValue - AvgMACD;
Commentary("MACD="+Text(MACDDiff));
con1=(MACDDiff>0);
con2=(high>=(EMA20[1]+M*MinPoint));
con3=(MACDDiff<0);
con4=(low<=(EMA20[1]-M*MinPoint));
if(MarketPosition<>1 and (countif(con1,5))>=1 and con2)
{
myEntryprice=EMA20[1]+M*MinPoint;
If(Open>myEntryprice)myEntryprice=Open;
buy(1,myEntryprice);
a=0;
}
if(MarketPosition==1)
{
if(high>=entryprice+13*MinPoint and a==0)
{ myExitprice=entryprice+13*MinPoint;
if(Open>entryprice+13*MinPoint)myExitprice=Open;
sell(1,myExitPrice);
a=1;
}
if(low<=entryPrice)
{ myExitprice=entryprice;
if(open<entryPrice)myExitPrice=entryPrice;
sell(1,myExitPrice);
}
if(low<=entryPrice-8*MinPoint)
{
myExitprice=entryPrice-8*MinPoint;
if(open<entryPrice-8*MinPoint)myExitprice=open;
sell(0,myExitPrice);
}
}
If(MarketPosition<>-1 and (countif(con3,5))>=1 and con4)
{
myEntryprice=EMA20[1]-M*MinPoint;
If(Open<myEntryprice)myEntryprice=Open;
SellShort(1,myEntryprice);
}
if(MarketPosition==-1)
{
if(low<=entryprice-13*MinPoint and b==0)
{ myExitprice=entryprice-13*MinPoint;
if(Open<entryprice-13*MinPoint)myExitprice=Open;
buyToCover(1,myExitPrice);
b=1;
}
if(high>=entryPrice)
{ myExitprice=entryprice;
if(open>entryPrice)myExitPrice=entryPrice;
buyToCover(1,myExitPrice);
}
if(high>=entryPrice+8*MinPoint)
{
myExitprice=entryPrice+8*MinPoint;
if(open<entryPrice+8*MinPoint)myExitprice=open;
buyToCover(0,myExitPrice) ;
}
}
End |
|