//------------------------------------------------------------------------
// 简称: TrendStrategy
//------------------------------------------------------------------------
Params
Numeric MA_Len(21);
Numeric TrailingStopPercent(25);
Numeric ReduceLots(1);
Vars
NumericSeries MA;
Numeric BuyStopPrice;
Numeric SellStopPrice;
Numeric MaxSinceEntry;
Numeric MinSinceEntry;
Numeric PreLow;
Numeric PreHigh;
Begin
MA = AverageFC(Close,MA_Len);
If(BarStatus == 2) {
PreLow = Low[1];
PreHigh = High[1];
}
If(MarketPosition == 0 && Close > MA && Close[1] <= MA[1]) {
Buy(0,Close);
BuyStopPrice = Lowest(Low[1],4);
MaxSinceEntry = High;
}
If(MarketPosition == 0 && Close < MA && Close[1] >= MA[1]) {
SellShort(0,Close);
SellStopPrice = Highest(High[1],4);
MinSinceEntry = Low;
}
If(MarketPosition == 1) {
MaxSinceEntry = Max(MaxSinceEntry,High);
If(Low <= BuyStopPrice) {
Sell(0,Min(Open,BuyStopPrice));
}
If(Close < PreLow) {
Sell(ReduceLots,Close);
}
If(Close < MA) {
Sell(0,Close);
}
If((MaxSinceEntry - Close)/MaxSinceEntry*100 >= TrailingStopPercent) {
Sell(0,Close);
}
}
If(MarketPosition == -1) {
MinSinceEntry = Min(MinSinceEntry,Low);
If(High >= SellStopPrice) {
BuyToCover(0,Max(Open,SellStopPrice));
}
If(Close > PreHigh) {
BuyToCover(ReduceLots,Close);
}
If(Close > MA) {
BuyToCover(0,Close);
}
If((Close - MinSinceEntry)/MinSinceEntry*100 >= TrailingStopPercent) {
BuyToCover(0,Close);
}
}
End
旗舰版语法,在quant以上版本需要用工具栏的代码升级转换成新语法规范。