海龜法則策略(MC版)_第1頁
海龜法則策略(MC版)_第2頁
海龜法則策略(MC版)_第3頁
海龜法則策略(MC版)_第4頁
海龜法則策略(MC版)_第5頁
已閱讀5頁,還剩15頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

海龜法則策略(MC版)一、海龜交易法基礎資金管理:采用波動幅度(ATR)管理資金。入市信號:多頭:當價格超過20日高點時,入市做多。空頭:當價格跌破20日低點時,入市做空。二、增倉法則初始建倉:入市時,只建立一個單位的頭寸。加倉條件:在建立頭寸之后,以0.5ATR的間隔增加頭寸,直到達到最大許可建倉單位數(shù)(LTT)。三、離場法則清倉條件:對于多頭頭寸,當價格跌破10日低點時清倉。對于空頭頭寸,當價格超過10日高點時清倉。四、止損法則最大止損風險:任何一筆交易的風險不超過2%。止損點設置:初始止損點設置在價格波動2ATR的位置。每次加倉后,之前建倉頭寸的止損點均向加倉點位移動0.5ATR,以保證全部頭寸的風險最小。五、代碼邏輯概覽輸入?yún)?shù):ATRlen:計算ATR的周期長度(20)Flen:計算20日高點和低點的周期長度(20)LTT:最大交易單位數(shù)Percent:增倉時每次增加的頭寸比例(0.5)StopATRCnt:止損時使用的ATR倍數(shù)(2)主要變量:Myfhigh:20日高點Myflow:20日低點mp:市場位置(多頭或空頭)Myatr:計算得到的ATR值StopPrice:止損價格AveragePrice:持倉均價邏輯流程:初始化:計算ATR值,檢查市場位置。入場:根據(jù)20日高點和低點決定買賣。增倉:在已有頭寸基礎上,根據(jù)市場位置和ATR值增加頭寸。止損:根據(jù)市場位置和ATR值設置止損價格,并隨著加倉移動止損點。離場:根據(jù)10日突破法則清倉。策略代碼注解:輸入?yún)?shù)

ATRlen

:計算ATR的周期長度,這里設為20。

Flen

:計算20日高點和低點的周期長度,這里也設為20。

LTT

:最大交易單位數(shù)。

Percent

:增倉時每次增加的頭寸比例,這里設為0.5。

StopATRCnt

:止損時使用的ATR倍數(shù),這里設為2。變量定義

Myfhigh

:20日高點。

Myflow

:20日低點。

mp

:市場位置(多頭或空頭)。

Myatr

:計算得到的ATR值。

StopPrice

:止損價格。

AveragePrice

:持倉均價。初始化操作在每次交易開始時,計算ATR值,并檢查市場位置。日志打印打印當前日期、時間、價格、買賣價、當前K線索引、ATR值、市場位置、合約數(shù)、各頭寸的入場價格。入場邏輯當市場位置為0(無持倉)時,計算20日高點和低點。如果價格超過20日高點,則買入(做多)。如果價格跌破20日低點,則賣出(做空)。增倉邏輯當當前合約數(shù)等于

LTT

時,根據(jù)市場位置和ATR值增加頭寸。每次增倉的間隔為0.5N(0.5倍ATR)。止損邏輯計算持倉均價。根據(jù)市場位置和ATR值設置止損價格。每增加一次頭寸,之前的止損點均向加倉點位移動0.5N。離場邏輯使用10日突破退出法則。對于多頭頭寸,當價格跌破10日低點時清倉。對于空頭頭寸,當價格超過10日高點時清倉。代碼邏輯1.

初始化:計算ATR值,檢查市場位置。2.

入場:當無持倉時,根據(jù)20日高點和低點決定買賣。3.

增倉:在已有頭寸基礎上,根據(jù)市場位置和ATR值增加頭寸。4.

止損:根據(jù)市場位置和ATR值設置止損價格。5.

離場:根據(jù)10日突破法則清倉。本策略實現(xiàn)了海龜交易法的基本邏輯,通過計算和比較價格的移動平均和ATR值來決定買賣時機和頭寸管理。策略代碼://輸入?yún)?shù)?Input:ATRlen(20),Flen(20),LTT(10),Percent(0.5),StopATRCnt(2);//變量定義?var:Myfhigh(0),Myflow(0),mp(0),Myatr(0),StopPrice(0),AveragePrice(0);//初始化操作mp=marketposition;ifmp<>0andmp[1]=0thenbeginMyatr=AvgTrueRange(ATRlen);//計算ATRend;//打印?ifdom_isconnected=truethenbeginprint(date,"",CurrentTime_s,"PRICE=",close,"ASK1=",q_ask,"BID=",q_bid,"BarIndex=",currentbar,"ATR=",Myatr,"MP=",mp,"Cnt=",currentcontracts,"EntryPrice1=",PosTradeEntryPrice(0,0),"EntryPrice2=",PosTradeEntryPrice(0,1),"EntryPrice3=",PosTradeEntryPrice(0,2),"EntryPrice4=",PosTradeEntryPrice(0,3));endelsebeginprint(date,"",CurrentTime_s,"PRICE=",close,"ASK1=",q_ask,"BID=",q_bid,"BarIndex=",currentbar,"ATR=",Myatr,"MP=",mp,"Cnt=",currentcontracts,"EntryPrice1=",PosTradeEntryPrice(0,0),"EntryPrice2=",PosTradeEntryPrice(0,1),"EntryPrice3=",PosTradeEntryPrice(0,2),"EntryPrice4=",PosTradeEntryPrice(0,3));end;//入場ifmp=0thenbeginMyfhigh=Highest(high,Flen);//20日高點Myflow=Lowest(low,Flen);//20日低點buy("buy-entry")LTTsharesnextbaratMyfhighstop;sellshort("Sell-entry")LTTsharesnextbaratMyflowstop;end;//增倉ifcurrentcontracts=LTTthenbeginifmp=1thenbeginbuy("buy-add1")LTTsharesnextbaratPosTradeEntryPrice(0,0)+(Percent*Myatr)stop;end;ifmp=-1thenbeginsellshort("Sell-add1")LTTsharesnextbaratPosTradeEntryPrice(0,0)-(Percent*Myatr)stop;end;end;ifcurrentcontracts=2*LTTthenbeginifmp=1thenbeginifPosTradeEntryPrice(0,1)<>0thenbeginAveragePrice=(PosTradeEntryPrice(0,0)+PosTradeEntryPrice(0,1))/2;StopPrice=AveragePrice+Percent*Myatr+(Percent/2)*Myatr;buy("buy-add2")LTTsharesnextbaratStopPricestop;end;ifPosTradeEntryPrice(0,1)=0thenbeginStopPrice=PosTradeEntryPrice(0,0)+Percent*Myatr+(Percent/2)*Myatr;buy("buy-add2")LTTsharesnextbaratStopPricestop;end;end;ifmp=-1thenbeginifPosTradeEntryPrice(0,1)<>0thenbeginAveragePrice=(PosTradeEntryPrice(0,0)+PosTradeEntryPrice(0,1))/2;StopPrice=AveragePrice-Percent*Myatr-(Percent/2)*Myatr;sellshort("Sell-add2")LTTsharesnextbaratStopPricestop;end;ifPosTradeEntryPrice(0,1)=0thenbeginStopPrice=PosTradeEntryPrice(0,0)-Percent*Myatr-(Percent/2)*Myatr;sellshort("Sell-add2")LTTsharesnextbaratStopPricestop;end;end;end;ifcurrentcontracts=3*LTTthenbeginifmp=1thenbeginifPosTradeEntryPrice(0,2)<>0thenbeginAveragePrice=(PosTradeEntryPrice(0,0)+PosTradeEntryPrice(0,1)+PosTradeEntryPrice(0,2))/3;StopPrice=AveragePrice+Percent*Myatr+2*(Percent/2)*Myatr;buy("buy-add3")LTTsharesnextbaratStopPricestop;end;ifPosTradeEntryPrice(0,2)=0thenbeginifPosTradeEntryPrice(0,1)<>0thenbeginAveragePrice=(PosTradeEntryPrice(0,0)+PosTradeEntryPrice(0,1))/2;StopPrice=AveragePrice+Percent*Myatr+2*(Percent/2)*Myatr;buy("buy-add3")LTTsharesnextbaratStopPricestop;end;ifPosTradeEntryPrice(0,1)=0thenbeginStopPrice=PosTradeEntryPrice(0,0)+Percent*Myatr+2*(Percent/2)*Myatr;buy("buy-add3")LTTsharesnextbaratStopPricestop;end;end;end;ifmp=-1thenbeginifPosTradeEntryPrice(0,2)<>0thenbeginAveragePrice=(PosTradeEntryPrice(0,0)+PosTradeEntryPrice(0,1)+PosTradeEntryPrice(0,2))/3;StopPrice=AveragePrice-Percent*Myatr-2*(Percent/2)*Myatr;sellshort("Sell-add3")LTTsharesnextbaratStopPricestop;end;ifPosTradeEntryPrice(0,2)=0thenbeginifPosTradeEntryPrice(0,1)<>0thenbeginAveragePrice=(PosTradeEntryPrice(0,0)+PosTradeEntryPrice(0,1))/2;StopPrice=AveragePrice-Percent*Myatr-2*(Percent/2)*Myatr;sellshort("Sell-add3")LTTsharesnextbaratStopPricestop;end;ifPosTradeEntryPrice(0,1)=0thenbeginStopPrice=PosTradeEntryPrice(0,0)-Percent*Myatr-2*(Percent/2)*Myatr;sellshort("Sell-add3")LTTsharesnextbaratStopPricestop;end;end;end;end;//止損法則//計算持倉均價ifPosTradeEntryPrice(0,3)<>0thenbeginAveragePrice=(PosTradeEntryPrice(0,3)+PosTradeEntryPrice(0,2)+PosTradeEntryPrice(0,1)+PosTradeEntryPrice(0,0))/4;endelseifPosTradeEntryPrice(0,2)<>0thenbeginAveragePrice=(PosTradeEntryPrice(0,2)+PosTradeEntryPrice(0,1)+PosTradeEntryPrice(0,0))/3;endelseifPosTradeEntryPrice(0,1)<>0thenbeginAveragePrice=(PosTradeEntryPrice(0,1)+PosTradeEntryPrice(0,0))/2;endelseifPosTradeEntryPrice(0,0)<>0thenbeginAveragePrice=PosTradeEntryPrice(0,0);end;ifcurrentcontracts=LTTthenbeginifmp=1thenbeginsell("Sell-Stop1")allsharesnextbaratAveragePrice-StopATRCnt*Myatrstop;end;ifmp=-1thenbeginbuytocover("buy-Stop1")allsharesnextbaratAveragePrice+StopATRCnt*Myatrstop;end;end;ifcurrentcontracts=2*LTTthenbeginifmp=1thenbeginsell("Sell-Stop2")allsharesnextbaratAveragePrice-StopATRCnt*Myatrstop;end;ifmp=-1thenbeginbuytocover("buy-Stop2")allsharesnextbaratAveragePrice+StopATRCnt*Myatrstop;end;end;ifcurrentcontracts=3*LTTthenbeginifmp=1thenbeginsell("Sell-Stop3")allsharesnextbaratAveragePrice-StopATRCnt*Myatrstop;end

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論