開拓者隨機(jī)發(fā)買賣單的模型源碼
作者:開拓者 TB 來源:cxh99.com 發(fā)布時(shí)間:2013年04月11日
- 思路: 投資學(xué)里經(jīng)典的一句話 - 限制損失,讓盈利奔跑。只要盈虧比達(dá)到一個(gè)理想的值,那勝率低也一樣能賺到錢。為了驗(yàn)證這個(gè)理論,我寫了一個(gè)隨機(jī)發(fā)買賣單的模型,并且限制每次損失的上限,對盈利進(jìn)行追蹤止損。模型為日內(nèi)交易,每3分鐘發(fā)一個(gè)單。
- 源碼:(程序化交易網(wǎng) www.kzuj.com.cn 轉(zhuǎn)載請保留出處)
- 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){//如果要實(shí)盤測試,把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) // 有多倉的情況( www.kzuj.com.cn )
- {
- StopLine = AvgEntryPrice * (1-InitialStop/1000);
-
- If(HighestAfterEntry[1] >= MyEntryPrice *(1 + BreakEvenStop/1000))
- Stopline = MyEntryPrice;
-
- If(Stopline < HighestAfterEntry[1]*(1 - TrailStop/1000))
- Stopline = HighestAfterEntry[1]*(1 - TrailStop/1000);
-
- if(Low <= Stopline)
- {
- MyExitPrice = Stopline;
- If(Open < MyExitPrice) MyExitPrice = Open; // 如果該Bar開盤價(jià)有跳空觸發(fā),則用開盤價(jià)代替
- Sell(0,MyExitPrice);
- return;
- }
- }else if(MarketPosition==-1) // 有空倉的情況
- {
- StopLine = AvgEntryPrice * (1+InitialStop/1000);
-
- If(LowestAfterEntry[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開盤價(jià)有跳空觸發(fā),則用開盤價(jià)代替 來源 CXH99.COM
- BuyToCover(0,MyExitPrice);
- return;
- }
- }
-
- RandNum = Rand(0,100); //產(chǎn)生0到100的隨機(jī)數(shù)
- BuyOrSell = Mod(RandNum,2); //產(chǎn)生0到1的隨機(jī)數(shù)
-
- Commentary("BuyOrSell:"+TEXT(BuyOrSell));
-
- If(BuyOrSell == 0 && Mod(Minute,3) == 0 && MarketPosition!= -1) //如果BuyOrSell為0,每3分鐘發(fā)一次多單
- Buy(1,Close);
- else If(BuyOrSell == 1 && Mod(Minute,3) == 0 && MarketPosition!=1)//如果BuyOrSell為1,每3分鐘發(fā)一次空單
- SellShort(1,Close);
- End