Params
Numeric MACDFastLength(12);
Numeric MACDSlowLength(26);
Numeric MACDLength(9);
Numeric ATRLength(20);
Numeric nATR(20); //n倍的ATR止盈
Numeric StopLoss(200); //200个点止损
Numeric Lots(1);//手数
Vars
NumericSeries MACDValue;
Numeric AvgMACD;
NumericSeries MACDDiff;
NumericSeries AvgTR;
Bool BuyCon1(False);
Bool SellShortCon1(False);
NumericSeries StopProf;
Begin
//过滤集合竞价
If(!CallAuctionFilter()) return;
//MACD
MACDValue = XAverage( Close, MACDFastLength ) - XAverage( Close, MACDSlowLength ) ;
AvgMACD = XAverage(MACDValue,MACDLength);
MACDDiff = MACDValue - AvgMACD;
//ATR
AvgTR = XAverage(TrueRange,ATRLength);
StopProf = nATR*AvgTR[1];
//开平仓条件
BuyCon1 = MACDValue[2]<0 && MACDValue[1]>0;
SellShortCon1 = MACDValue[2]>0 && MACDValue[1]<0;
//开平仓1
If(MarketPosition!=1 && BuyCon1)
{
Buy(Lots,Open);
Commentary("开多1");
}
If(MarketPosition!=-1 && SellShortCon1)
{
SellShort(Lots,Open);
Commentary("开空1");
}
//止损
If(MarketPosition==1 && Low<=LastEntryPrice-StopLoss)
{
Sell(0,Min(Open,LastEntryPrice-StopLoss));
Commentary("多头止损");
}
If(MarketPosition==-1 && High>=LastEntryPrice+StopLoss)
{
BuyToCover(0,Max(Open,LastEntryPrice+StopLoss));
Commentary("空头止损");
}
//止赢
If(MarketPosition==1 && High>=LastEntryPrice+StopProf)
{
Sell(0,Max(Open,LastEntryPrice+StopProf));
Commentary("多头止赢");
}
If(MarketPosition==-1 && Low<=LastEntryPrice-StopProf)
{
BuyToCover(0,Min(Open,LastEntryPrice-StopProf));
Commentary("空头止赢");
}
End
IB代码,万分感谢
您可以到文华官网下载MQ软件体验下:http://www.wenhua.com.cn/ Params Numeric MACDFastLength(12); Numeric MACDSlowLength(26); Numeric MACDLength(9); Numeric ATRLength(20); Numeric nATR(20); //n倍的ATR止盈 Numeric StopLoss(200); //200个点止损 Numeric Lots(1);//手数 Vars NumericSeries MACDValue; Numeric AvgMACD; NumericSeries MACDDiff; NumericSeries AvgTR; Numeric BuyCon1; Numeric SellShortCon1; NumericSeries StopProf; Begin //MACD MACDValue = XAverage( Close, MACDFastLength ) - XAverage( Close, MACDSlowLength ) ; AvgMACD = XAverage(MACDValue,MACDLength); MACDDiff = MACDValue - AvgMACD; //ATR AvgTR = XAverage(TrueRange,ATRLength); StopProf = nATR*AvgTR[1]; //开平仓条件 BuyCon1 = MACDValue[2]<0 && MACDValue[1]>0; SellShortCon1 = MACDValue[2]>0 && MACDValue[1]<0; //开平仓1 If(MarketPosition!=1 && BuyCon1) { Buy(Lots,Open); Commentary("开多1"); } If(MarketPosition!=-1 && SellShortCon1) { SellShort(Lots,Open); Commentary("开空1"); } //止损 If(MarketPosition==1 && Low<=LastEntryPrice-StopLoss) { Sell(0,Min(Open,LastEntryPrice-StopLoss)); Commentary("多头止损"); } If(MarketPosition==-1 && High>=LastEntryPrice+StopLoss) { BuyToCover(0,Max(Open,LastEntryPrice+StopLoss)); Commentary("空头止损"); } //止赢 If(MarketPosition==1 && High>=LastEntryPrice+StopProf) { Sell(0,Max(Open,LastEntryPrice+StopProf)); Commentary("多头止赢"); } If(MarketPosition==-1 && Low<=LastEntryPrice-StopProf) { BuyToCover(0,Min(Open,LastEntryPrice-StopProf)); Commentary("空头止赢"); } End |