思路: 投资学里经典的一句话 - 限制损失,让盈利奔跑。只要盈亏比达到一个理想的值,那胜率低也一样能赚到钱。为了验证这个理论,我写了一个随机发买卖单的模型,并且限制每次损失的上限,对盈利进行追踪止损。模型为日内交易,每3分钟发一个单。
Params
Numeric InitialStop(2);
Numeric BreakEvenStop(3);
Numeric TrailStop(5);
Vars
Numeric RandNum;
Numeric BuyOrSell;
Numeric StopLine;
Numeric MyEntryPrice;
Numeric MyExitPrice;
NumericSeries HighestAfterEntry;
NumericSeries LowestAfterEntry;
Begin
If(Time>=0.1455){//如果要实盘测试,把Time改为CurrentTime
If(MarketPosition == 1)
Sell(0,Close);
If(MarketPosition == -1)
BuyToCover(0,Close);
return;
}
If(BarsSinceentry == 0)
{
HighestAfterEntry = Close;
LowestAfterEntry = Close;
If(MarketPosition 0)
{
HighestAfterEntry = Max(HighestAfterEntry,AvgEntryPrice);
LowestAfterEntry = Min(LowestAfterEntry,AvgEntryPrice);
}
}else
{
HighestAfterEntry = Max(HighestAfterEntry,High);
LowestAfterEntry = Min(LowestAfterEntry,Low);
}
Commentary("HighestAfterEntry="+Text(HighestAfterEntry));
Commentary("LowestAfterEntry="+Text(LowestAfterEntry));
MyEntryPrice = AvgEntryPrice;
If(MarketPosition==1) // 有多仓的情况()
{
StopLine = AvgEntryPrice * (1-InitialStop/1000);
If(HighestAfterEntry[1] >= MyEntryPrice *(1 + BreakEvenStop/1000))
Stopline = MyEntryPrice;
If(Stopline LowestAfterEntry[1]*(1 + TrailStop/1000))
Stopline = LowestAfterEntry[1]*(1 + TrailStop/1000);
Commentary("StopLine="+Text(Stopline));
If(High >= Stopline)
{
MyExitPrice = Stopline;
If(Open > MyExitPrice) MyExitPrice = Open;
// 如果该Bar开盘价有跳空触发,则用开盘价代替 来源
BuyToCover(0,MyExitPrice);
return;
}
}
RandNum = Rand(0,100);
//产生0到100的随机数
BuyOrSell = Mod(RandNum,2);
//产生0到1的随机数
Commentary("BuyOrSell:"+TEXT(BuyOrSell));
If(BuyOrSell == 0 Mod(Minute,3) == 0 MarketPosition!= -1) //如果BuyOrSell为0,每3分钟发一次多单
Buy(1,Close);
else If(BuyOrSell == 1 Mod(Minute,3) == 0 MarketPosition!=1)//如果BuyOrSell为1,每3分钟发一次空单
SellShort(1,Close);
End