【常见问题】:如何把TB、MT4、MC和金字塔的模型转化为wh8麦语言? (文华财经)

投资者咨询:【常见问题】:如何把TB、MT4、MC和金字塔的模型转化为wh8麦语言? (文华财经)
来源:文华财经  日期:2014-7-18 9:29

 麦语言采用“小语法,大函数”的构建模式,大幅提高编写效率,其他语言100多句的模型,用麦语言一般10几句就可以编出来。

 

其他语言能实现的模型,麦语言几乎都可以实现的。

 

以下是我们从论坛中整理出来的,TB、MC、MT4和金字塔的模型的转化案例,供大家参考。

 

 

               
技术人员回复
日期:2014-7-18 9:31

为了便于其他用户查看各楼的内容,请大家不要跟贴

技术人员回复
日期:2014-7-18 10:07
 tb源码
Bool bInitStatues(false); 
Numeric InitMyRealMp(0); 
Numeric FirstGrid(30); 
Numeric AddGrid(5); 
Numeric TotalGrids(10); 
Numeric TrailingGrid(30); 
Numeric EveryLots(1); 
Numeric OffSet(1); 
Numeric ExitOnCloseMins(14.58); 
Vars
Numeric HighAfterLongEntry;
Numeric LowAfterShortEntry;
Numeric MyRealMp(0);
Numeric MinPoint;
Numeric tmpPrice;
Numeric tmpLots;
Begin
MinPoint=Minmove*PriceScale;
MyRealMp=GetGlobalVar(0);
HighAfterLongEntry=GetGlobalVar(1);
LowAfterShortEntry=GetGlobalVar(2);
if (BarStatus==0 And (MyRealMp==InvalidNumeric || bInitStatues))
{
MyRealMp=InitMyRealMp;
}
if (Date<>Date[1])
{
HighAfterLongEntry=High;
LowAfterShortEntry=Low;
MyRealMp=0;
}Else
{
HighAfterLongEntry=Max(HighAfterLongEntry,High);
LowAfterShortEntry=Min(Low,LowAfterShortEntry);
}
If (Time<ExitOnCloseMins/100)
{
If ( MyRealMp>0 And HighAfterLongEntry-Low>=TrailingGrid*MinPoint
And(High-Low<TrailingGrid*MinPoint Or(High-Low>=TrailingGrid*MinPoint And close<Open)))
{
tmpPrice=Max(HighAfterLongEntry-(TrailingGrid-OffSet)*MinPoint,Low);
tmpLots=Abs(MyRealMp*EveryLots);
Sell(tmpLots,tmpPrice);
MyRealMp=0;
LowAfterShortEntry=Low;
}Else
If ( MyRealMp<0 And High-LowAfterShortEntry>=TrailingGrid*MinPoint
And(High-Low<TrailingGrid*MinPoint Or(High-Low>=TrailingGrid*MinPoint And close>Open)))
{
tmpPrice=Min(LowAfterShortEntry+(TrailingGrid+OffSet)*MinPoint,High);
tmpLots=Abs(MyRealMp*EveryLots);
BuyToCover(tmpLots,tmpPrice);
MyRealMp=0;
HighAfterLongEntry=0;
}
// 第一笔多单开仓
if (MyRealMp==0 And High-LowAfterShortEntry>=FirstGrid*MinPoint)
{
tmpPrice=Min(LowAfterShortEntry+(FirstGrid+OffSet)*MinPoint,High);
tmpLots=EveryLots;
Buy(tmpLots,tmpPrice);
MyRealMp=1;
HighAfterLongEntry=High;
}Else
// 第一笔空单开仓
if (MyRealMp==0 And HighAfterLongEntry-Low>=TrailingGrid*MinPoint )
{
tmpPrice=Max(HighAfterLongEntry-(FirstGrid-OffSet)*MinPoint,Low);;
tmpLots=EveryLots;
SellShort(tmpLots,tmpPrice);
MyRealMp=-1;
LowAfterShortEntry=Low ;
}Else
SetGlobalVar(0,MyRealMp);
SetGlobalVar(1,HighAfterLongEntry);
SetGlobalVar(2,LowAfterShortEntry);
Commentary("MyRealMp="+Text(MyRealMp));
Commentary("HighAfterLLowAfterShortEntry="+Text(LowAfterShortEntry));
//SetExitOnClose;

改为麦语言模型源码:

N:=BARSLAST(DATE<>REF(DATE,1))+1;//读取当天第一根k线到当前k线的周期数
HH:=HHV(H,N);//计算当天最高价
LL:=LLV(L,N);//计算当天最低价
H-LL>=30*MINPRICE,BK;//最高价与当天最低价差值超过30最小变动价位时,买开
HH-L>=30*MINPRICE,SK;//当天最高价与最低价差值超过30最小变动价位时,卖开
TIME<1458&&HH-L>=30*MINPRICE &&(H-L<30*MINPRICE ||(H-L>=30*MINPRICE &&C<O)),SP;//卖平仓
TIME<1458&&H-LL>=30*MINPRICE &&(H-L<30*MINPRICE ||(H-L>=30*MINPRICE &&C>O)),BP;//买平仓
TIME>=1458,CLOSEOUT;//盘尾14点58分全平
AUTOFILTER; 


//后的绿色字体为注释,不参与模型计算
    //14点58分之前当天最高价与最低价差值超过30最小变动价位且当根k线幅度小于30最小变动价位或者当根k线幅度超过30最小变动价位同时k线收阴时,卖平 
//14点58分之前最高价与当天最低价差值超过30最小变动价位且当根k线幅度小于30最小变动价位或者当根k线幅度超过30最小变动价位同时k线收阳时,买平    
             
技术人员回复
日期:2014-7-18 10:08
 

tb源码
Params
        Numeric ATRLength(14);        
        Numeric NATR(3);                        
Vars
        Numeric ATRValue;
        Numeric ARCValue;
        NumericSeries SICHigh;
        NumericSeries SICLow;
        Numeric SARLong;
        Numeric SARShort;
Begin
        If(BarStatus == 0)
        {
                SICHigh = Close;
                SICLow = Close;
        }Else
        {
                SICHigh = Max(Close,SICHigh[1]);
                SICLow = Min(Close,SICLow[1]);
        }
        ATRValue = AvgTrueRange(ATRLength);
        ARCValue = ATRValue * NATR;
        SARLong = SICLow + ARCValue;
        SARShort = SICHigh - ARCValue;
        Commentary("SICHigh="+Text(SICHigh));
        Commentary("SICLow="+Text(SICLow));
        Commentary("ATR="+Text(ATRValue));
        Commentary("ARC="+Text(ARCValue));
        Commentary("SARLSARShort="+Text(SARShort));
        
        If(MarketPosition ==0)
        {
                If(Close > SARLong)
                {
                        Buy(1,NextOpen,True);
                        SICHigh = Close;
                        SICLow = Close;
                }
                If(Close < SARShort)
                {
                        SellShort(1,NextOpen,True);
                        SICHigh = Close;
                        SICLow = Close;
                }
        }Else If(MarketPosition == 1)
        {
                If(Close < SARShort)
                {
                        SellShort(1,NextOpen,True);
                        SICHigh = Close;
                        SICLow = Close;
                }
        }Else If(MarketPosition == -1)
        {
                If(Close > SARLong)
                {
                        Buy(1,NextOpen,True);
                        SICHigh = Close;
                        SICLow = Close;
                }
        }
End

改为麦语言模型源码:

TR := MAX(MAX((HIGH-LOW),ABS(REF(CLOSE,1)-HIGH)),ABS(REF(CLOSE,1)-LOW));//定义TR
ATR := MA(TR,14);//计算14周期TR的均值
HH:=LOOP2(BARPOS=1,C,MAX(REF(HH,1),C)); //如果是历史第一根k线取收盘价,否则取前一周期HH与收盘价的最大值。
LL:=LOOP2(BARPOS=1,C,MIN(REF(LL,1),C));//如果是历史第一根k线取收盘价,否则取前一周期LL与收盘价的最小值
AV:=ATR*3;//计算3倍ATR值
SARL:=LL+AV;//定义LL+AV为上轨
SARS:=HH-AV;//定义HH+AV为下轨
C>SARL,BPK;  //最新价大于上轨,买平开
C<SARS,SPK;//最新价小于下轨,卖平开
AUTOFILTER;
       

//后的绿色字体为注释,不参与模型计算
   
技术人员回复
日期:2014-7-18 10:10
tb源码
Params
    Numeric ceilingAmt(60);
    Numeric floorAmt(20);
    Numeric bolBandTrig(2.00);
Vars
    Numeric lookBackDays(20);         
    Numeric todayVolatility(0);
    Numeric yesterDayVolatility(0);
    Numeric deltaVolatility(0);
    NumericSeries buyPoint(0);
    NumericSeries sellPoint(0);
    NumericSeries longLiqPoint(0);
    NumericSeries shortLiqPoint(0);
    Numeric upBand(0);
    Numeric dnBand(0);
    Numeric MidLine(0);
    Numeric Band(0);
    Begin
    todayVolatility = StandardDev(Close,30,1);
    yesterDayVolatility = StandardDev(Close[1],30,1);
    deltaVolatility = (todayVolatility - yesterDayVolatility)/todayVolatility;
    lookBackDays = lookBackDays * (1 + deltaVolatility);
    lookBackDays = Round(lookBackDays,0);
    lookBackDays = Min(lookBackDays,ceilingAmt);
    lookBackDays = Max(lookBackDays,floorAmt);
    MidLine = AverageFC(Close,lookBackDays);
    Band = StandardDev(Close,lookBackDays,bolBandTrig);
    upBand = MidLine + bolBandTrig * Band;
    dnBand = MidLine - bolBandTrig * Band;
    buyPoint = Highest(High[1],lookBackDays);
    sellPoint = Lowest(Low[1],lookBackDays);
    longLiqPoint = Average(Close[1],lookBackDays);
    shortLiqPoint = Average(Close[1],lookBackDays);
    if(Close > upBand)  
    {
         If(CrossOver(high,buyPoint))   Buy(1,max( buyPoint, Low ));
         Commentary("多头触发价:"+Text(buyPoint));
    }
    if(Close < dnBand)
    { 
         If(CrossUnder(Low,sellPoint )) SellShort(1,min( sellPoint , High ));
         Commentary("空头触发价:"+Text(sellPoint));
    }
    if(MarketPosition == 1)
    { 
         If(CrossUnder(Low,longLiqPoint )) Sell(1,min( longLiqPoint , High ));
         Commentary("多头退出:"+Text(longLiqPoint));
    }
    if(MarketPosition == -1)
    {
         If(CrossOver(high,shortLiqPoint)) BuyToCover(1,max( shortLiqPoint, Low          ));
         Commentary("多头退出:"+Text(shortLiqPoint));
    }
End

改为麦语言模型源码:

todayvolatility:=STD(CLOSE,30);//计算30周期收盘价的标准差
yesterdayvolatility:=STD(REF(CLOSE,1),30);//计算30周期前一根k线收盘价的标准差
deltavolatility:=(todayvolatility-yesterdayvolatility)/todayvolatility;//计算2个标准差的振幅
lookbackdays:=POW(20*(1+deltavolatility),BARPOS);//计算20*(1+deltavolatility) 的幂级数
lookbackdays1:=IFELSE(lookbackdays>60,60,IFELSE(lookbackdays<20,20,lookbackdays));//当幂级数大于60取60小于20取20在这2者之间取当前值
mid:=MA(CLOSE,lookbackdays1);//取幂级数周期收盘价均值。
upband:=mid+2*STD(CLOSE,lookbackdays1);//确定上轨
dnband:=mid-2*STD(CLOSE,lookbackdays1);//确定下轨
buypoint:=HV(HIGH,lookbackdays1);//计算前一周期lookbackdays1周期内最高价的最大值。
sellpoint:=LV(LOW,lookbackdays1);//计算前一周期lookbackdays1周期内最低价的最小值。
longliqpoint:=MA(CLOSE,lookbackdays1);//lookbackdays1周期收盘价均值
shortliqpoint:=MA(CLOSE,lookbackdays1);//lookbackdays1周期收盘价均值
CLOSE<dnband,SPK;//当最新价小于下轨,卖平开
LOW<=longliqpoint,SP;//当最低价不大于longliqpoint,卖平
CLOSE>upband,BPK;//当最新价大于上轨,买平开
HIGH>=shortliqpoint,BP;//当最高价不小于shortliqpoint,买平
AUTOFILTER;
         
技术人员回复
日期:2014-7-18 11:32
 tb代码
Params
Numeric K1(0.5);
Numeric K2(0.5);
Numeric Mday(1);
Numeric Nday(1);
Numeric lots(1);
Numeric offset(0);
Vars
Numeric BuyRange(0);
Numeric SellRange(0);
Numeric BuyTrig(0);
Numeric SellTrig(0);
Numeric HH;
Numeric LL;
Numeric HC;
Numeric LC;
Numeric i_offset;
Numeric BuyPosition;
Numeric SellPosition;
Begin
If(CurrentBar > 44*Max(Mday,Nday))
{
        i_offset = offset*MinMove*PriceScale;
        HH = Highest(HighD(1),Mday);
        HC = Highest(CloseD(1),Mday);
        LL = Lowest(LowD(1),Mday);
        LC = Lowest(CloseD(1),Mday);
        If((HH - LC) >= (HC - LL))
        {
                SellRange = HH - LC;
        }
        Else
        {
                SellRange = HC - LL;
        }
        HH = Highest(HighD(1),Nday);
        HC = Highest(CloseD(1),Nday);
        LL = Lowest(LowD(1),Nday);
        LC = Lowest(CloseD(1),Nday);
        If((HH - LC) >= (HC - LL))
        {
                BuyRange = HH - LC;
        }
        Else
        {
                BuyRange = HC - LL;
        }
        BuyTrig = K1*BuyRange;
        SellTrig = K2*SellRange;
        
        BuyPosition = OpenD(0)+BuyTrig;
        SellPosition = OpenD(0)-SellTrig;
        
        PlotNumeric("BuyPosition",BuyPosition);
        PlotNumeric("SellPosition",SellPosition);
        If(MarketPosition == 0)
        {
                If(High>=BuyPosition)
                {
                        Buy(lots,Max(Open,BuyPosition)+i_offset);
                        Return;
                }
                
                If(Low<=SellPosition)
                {
                        SellShort(lots,Min(Open,SellPosition)-i_offset);
                        Return;
                }
        }
        If(MarketPosition == -1)
        {
                If(High>=BuyPosition)
                {
                        Buy(lots,Max(Open,BuyPosition)+i_offset);
                        Return;
                }
        }
        If(MarketPosition == 1)
        {
                If(Low<=SellPosition)
                {
                        SellShort(lots,Min(Open,SellPosition)-i_offset);
                        Return;
                }
        }
}
End

改为麦语言模型源码: M M1 K1 K2参数为别为1、1、0.5、0.5,在参数栏中设置

N:=BARSLAST(DATE<>REF(DATE,1))+1;//分钟周期日内k线根数
OO:=VALUEWHEN(DATE<>REF(DATE,1),O); //取当天开盘价
HH1:=REF(HHV(H,N*M),N);//取M天内高点
LL1:=REF(LLV(L,N*M),N);//取M天内低点
HC1:=REF(HHV(C,N*M),N);//取M天内收盘价最大值
LC1:=REF(LLV(C,N*M),N);//取M天内收盘价最小值
HH2:=REF(HHV(H,N*M1),N);//取M1天内最高价最大值
LL2:=REF(LLV(L,N*M1),N);//取M1天内最低价最小值
HC2:=REF(HHV(C,N*M1),N);//取M1天内收盘价最大值
LC2:=REF(LLV(C,N*M1),N);//取M1天内收盘价最小值
SELLR:=IFELSE((HH1-LC1)>=(HC1-LL1),HH1-LC1,HC1-LL1);//如果(HH1-LC1)>=(HC1-LL1)取HH1-LC1的值,否则取HC1-LL1
BUYR:=IFELSE((HH2-LC2)>=(HC2-LL2),HH2-LC2,HC2-LL2);//如果(HH2-LC2)>=(HC2-LL2)取HH2-LC2的值,否则取HC2-LL2
BT:=K1*BUYR;//计算买波动幅度K1*BUYR
ST:=K2*SELLR;//计算卖波动幅度K2*SELLR
BUYP:=OO+BT;//计算上轨
SELLP:=OO-ST;//计算下轨
NOT(ISLASTKLINE)&&H>=BUYP&&C>=MAX(O,BUYP),BPK;//如果不是收盘最后一根k线且最高价不小于上轨且收盘价不小于上轨和开盘价最大值,买平开
NOT(ISLASTKLINE)&&L<=SELLP&&C<=MIN(O,SELLP),SPK;//如果不是收盘最后一根k线且最低价不大于下轨且收盘价不大于下轨和开盘价最小值,卖平开
ISLASTKLINE=1,SP;//收盘最后一根k线,卖平
ISLASTKLINE=1,BP;//收盘最后一根k线,买平
AUTOFILTER;

技术人员回复
日期:2014-7-18 13:22
 金字塔源码
input:trn(20,5,30),hn(20,5,30),ln(10,5,20); 
VARIABLE:
dayCount=1,PositionCount=1,SellSign=0,dK=0;//加多空标志,1:多,-1:空 0:空仓
VARIABLE:
EntAndExitSign=1,EntPoint=0,ExitPoint=0;
VARIABLE:N=0;
N:=MA(TR,trn);
BUYHHV:=HHV(H,hn);
SELLLLV:=LLV(L,ln);
sellshortllv:=llv(l,hn);
buyshorthhv:=hhv(h,ln);
IF BARPOS>=hn THEN
BEGIN
IF BARPOS=hn THEN
 IF DayCount=hn/2 OR BARPOS=hn  THEN
     BEGIN{hn/2天调整N值}
N:=((hn-1)*N+TR);{计算N值}
       DayCount:=2;
END
DayCount:=DayCount+1;
EntPoint:=ENTERBARS+1;
IF EntPoint=EntAndExitSign THEN
BEGIN{说明STOP指令买进头寸成功}
     PositionCount:=PositionCount+1;{头寸计数}
     SellSign:=True;{可以平仓信号,如果达到指定的价格}
END
IF PositionCount=1 THEN BEGIN{第一头寸}
    HOW:=CASH(0)*0.01/N;{波动性百分比决定头寸规模}
    if high=buyhhv then
BEGIN
   dk:=1;
   多开1:BUY(1,HOW,STOP,BUYHHV);{在20日新高STOP指令买进}
   END;
  if low=sellshortllv then
   begin
   dk:=-1;
   空开1:buyshort(1,HOW,STOP,sellshortllv);{在20日新低STOP指令空开}
   end;
 END
 IF PositionCount=2 THEN BEGIN{如到第二头寸}
     HOW:=CASH(0)*0.01/N;{波动性百分比决定头寸规模}
     if dk=1 then 多开2:BUY(1,HOW,STOP,ENTERPRICE+0.5*N);{在上头寸(即第一     头寸)+0.5个N以STOP指令买进}
     if dk=-1 then 空开2:buyshort(1,HOW,STOP,ENTERPRICE-0.5*N);
END
IF PositionCount=3 THEN BEGIN{如到第三头寸}
     HOW:=CASH(0)*0.01/N;
     if dk=1 then 多开3:BUY(1,HOW,STOP,ENTERPRICE+0.5*N);{在上头寸(即第二     头寸)+0.5个N以STOP指令买进}
     if dk=-1 then 空开3:buyshort(1,HOW,STOP,ENTERPRICE-0.5*N);
END
IF PositionCount=4 THEN BEGIN
    HOW:=CASH(0)*0.01/N;
    if dk=1 then 多开4:BUY(1,HOW,STOP,ENTERPRICE+0.5*N);
    if dk=-1 then 空开4:buyshort(1,HOW,STOP,ENTERPRICE-0.5*N); 
END
IF SellSign=True THEN
 BEGIN
      ExitPoint:=EXITBARS+1;
      if dk=1 then
   begin
     IF ExitPoint=EntAndExitSign THEN
     BEGIN {说明卖出成功}
     PositionCount:=1;{头寸计算复原}
     SellSign:=False;
     dk:=0;
     END
     IF ENTERPRICE-2*N then
         SELL(1,100%,STOP,SELLLLV);{退出离盈利头寸}
     ELSE
         SELL(1,100%,STOP,ENTERPRICE-2*N);{退出亏损头寸}
      end;
      if dk=-1 then
          begin
      IF ExitPoint=EntAndExitSign THEN
          BEGIN
           PositionCount:=1;
           SellSign:=False;
           dk:=0;
      END
      IF ENTERPRICE+2*N then
          sellSHORT(1,100%,STOP,BUYSHORTHHV);
      ELSE
          sellSHORT(1,100%,STOP,ENTERPRICE+2*N);
      END;
END
END;

改为麦语言模型源码:参数TRN为20,HN为20,LN1为10,在参数栏中设置即可。  
                                 
HH:HHV(H,20);//取20周期内最高价最大值
LL:LLV(L,10);//取10周期内最低价最小值
TR : =MAX(MAX((HIGH-LOW),ABS(REF(CLOSE,1)-HIGH)),ABS(REF(CLOSE,1)-LOW));//定义tr
ATR : IFELSE(BARPOS>=20&&MOD(BARPOS,10)=0,(19*MA(TR,26)+TR)/20,MA(TR,20));//赋值atr
H=HHV(H,HN),BK(0.1*MONEY/ATR);//创20周期新高,第一次买入一定头寸多单
H=HHV(H,HN),BK(0.1*MONEY/ATR);//创20周期新高,第二次买入一定头寸多单
H=HHV(H,HN),BK(0.1*MONEY/ATR);//创20周期新高,第三次买入一定头寸多单
H=HHV(H,HN),BK(0.1*MONEY/ATR);//创20周期新高,第四次买入一定头寸多单
C<=BKPRICE-2*ATR,SP(BKVOL);//最新价低于开仓价之下2*ATR,清多仓
L=LLV(L,LN1),SK(0.1*MONEY/ATR);//创10周期新低,第一次卖出一定头寸空单
L=LLV(L,LN1),SK(0.1*MONEY/ATR);//创10周期新低,第一次卖出一定头寸空单
L=LLV(L,LN1),SK(0.1*MONEY/ATR);//创10周期新低,第一次卖出一定头寸空单
L=LLV(L,LN1),SK(0.1*MONEY/ATR); //创10周期新低,第一次卖出一定头寸空单
C>=SKPRICE+2*ATR,BP(SKVOL);//最新价高于开仓价之上2*ATR,清空仓
   
技术人员回复
日期:2014-7-18 13:53

  tb源码
Params
    Numeric RiskRatio(1);          
    Numeric ATRLength(20);                 
    Numeric boLength(20);                
    Numeric fsLength(55);                  
    Numeric teLength(10);                  
    Bool LastProfitableTradeFilter(True); 
 Vars
 Numeric MinPoint;                      
 NumericSeries AvgTR;     
 Numeric N;                            
Numeric TotalEquity;                  
Numeric TurtleUnits;                   
    NumericSeries DonchianHi;             
    NumericSeries DonchianLo;             
    NumericSeries fsDonchianHi;           
    NumericSeries fsDonchianLo;            
    Numeric ExitHighestPrice;              
    Numeric ExitLowestPrice;               
    Numeric myEntryPrice;                  
    Numeric myExitPrice;                  
    Bool SendOrderThisBar(False);         
 NumericSeries preEntryPrice(0);      
 BoolSeries PreBreakoutFailure(false); 
Begin
    If(BarStatus == 0)
    {
  preEntryPrice = InvalidNumeric;
  PreBreakoutFailure = false;
 }Else
 {
  preEntryPrice = preEntryPrice[1];
        PreBreakoutFailure = PreBreakoutFailure[1];
    }
 MinPoint = MinMove*PriceScale;
    AvgTR = XAverage(TrueRange,ATRLength);
 N = AvgTR[1];
    TotalEquity = CurrentCapital()+ Abs(CurrentContracts()*Close*ContractUnit()*BigPointValue()*MarginRatio());
    TurtleUnits = (TotalEquity*RiskRatio/100) /(N * ContractUnit()*BigPointValue());
    TurtleUnits = IntPart(TurtleUnits);
    DonchianHi = HighestFC(High[1],boLength);
    DonchianLo = LowestFC(Low[1],boLength);
 fsDonchianHi = HighestFC(High[1],fsLength);
    fsDonchianLo = LowestFC(Low[1],fsLength);
 Commentary("N="+Text(N));
 Commentary("preEntryPrice="+Text(preEntryPrice));
 Commentary("PreBreakoutFailure="+IIFString(PreBreakoutFailure,"True","False"));
 
    If(MarketPosition == 0 && ((!LastProfitableTradeFilter) Or (PreBreakoutFailure)))
    {
        If(CrossOver(High,DonchianHi) && TurtleUnits >= 1)
        {
            myEntryPrice = min(high,DonchianHi + MinPoint);
            myEntryPrice = IIF(myEntryPrice < Open, Open,myEntryPrice);
   preEntryPrice = myEntryPrice;
            Buy(TurtleUnits,myEntryPrice);
   SendOrderThisBar = True;
   PreBreakoutFailure = False;
        }
        If(CrossUnder(Low,DonchianLo) && TurtleUnits >= 1)
        {
            myEntryPrice = max(low,DonchianLo - MinPoint);
            myEntryPrice = IIF(myEntryPrice > Open, Open,myEntryPrice);
            preEntryPrice = myEntryPrice;
            SendOrderThisBar = True;
            SellShort(TurtleUnits,myEntryPrice);
   SendOrderThisBar = True;
   PreBreakoutFailure = False;
        }
    }
    If(MarketPosition == 0)
    {
  Commentary("fsDfont-family: Simsun; font-size: 12px;">  Commentary("fsDfont-family: Simsun; font-size: 12px;">    If(MarketPosition == 1)
    {
        ExitLowestPrice = Lowest(Low[1],teLength);
  Commentary("ExitLowestPrice="+Text(ExitLowestPrice));
        If(Low < ExitLowestPrice)
        {
            myExitPrice = max(Low,ExitLowestPrice - MinPoint);
   myExitPrice = IIF(myExitPrice > Open, Open,myExitPrice);
            Sell(0,myExitPrice);   
        }Else
        {
            If(preEntryPrice!=InvalidNumeric && TurtleUnits >= 1)
            {
                If(Open >= preEntryPrice + 0.5*N)
                {
                    myEntryPrice = Open;
     preEntryPrice = myEntryPrice;
                    Buy(TurtleUnits,myEntryPrice);
     SendOrderThisBar = True;
                }
                while(High >= preEntryPrice + 0.5*N)
                {
                    myEntryPrice = preEntryPrice + 0.5 * N;
                    preEntryPrice = myEntryPrice;
                    Buy(TurtleUnits,myEntryPrice);
     SendOrderThisBar = True;    
                }
            }
   If(Low <= preEntryPrice - 2 * N && SendOrderThisBar == false)
   {
    myExitPrice = preEntryPrice - 2 * N;
    Sell(0,myExitPrice);
    PreBreakoutFailure = True;
   }
        }
    }Else If(MarketPosition ==-1)
    {
        ExitHighestPrice = Highest(High[1],teLength);
  Commentary("ExitHighestPrice="+Text(ExitHighestPrice));
        If(High > ExitHighestPrice)
        {
            myExitPrice = Min(High,ExitHighestPrice + MinPoint);
   myExitPrice = IIF(myExitPrice < Open, Open,myExitPrice);
            BuyToCover(0,myExitPrice);   
        }Else
        {
            If(preEntryPrice!=InvalidNumeric && TurtleUnits >= 1)
            {
                If(Open <= preEntryPrice - 0.5*N)
                {
                    myEntryPrice = Open;
     preEntryPrice = myEntryPrice;
                    SellShort(TurtleUnits,myEntryPrice);//
     SendOrderThisBar = True;
                }
                while(Low <= preEntryPrice - 0.5*N)
                {
                    myEntryPrice = preEntryPrice - 0.5 * N;
                    preEntryPrice = myEntryPrice;
                    SellShort(TurtleUnits,myEntryPrice);
     SendOrderThisBar = True;
                }
            }
   If(High >= preEntryPrice + 2 * N &&SendOrderThisBar==false)
   {
    myExitPrice = preEntryPrice + 2 * N;
    BuyToCover(0,myExitPrice);
    PreBreakoutFailure = True;
   }
        }
    }
End

改为麦语言模型源码:参数RiskRatio为1,ATRLength为20,boLength为20,fsLength为55,teLength为10,DW为300,在参数栏中设置即可。

 

TR:= MAX(MAX((HIGH-LOW),ABS(REF(CLOSE,1)-HIGH)),ABS(REF(CLOSE,1)-LOW));//定义tr
ATR:= MA(TR,ATRLength);// 计算20周期TR的均值
N:=REF(ATR,1);// 取前一根k线的TR值
TU:=FLOOR(MONEYTOT*RiskRatio/100)/(N*DW);// 按可用资金1%计算头寸
DonchianHi:= HHV(REF(H,1),boLength);//取前一周期20根k线最高价最大值
DonchianLo:= LLV(REF(L,1),boLength);//取前一周期20根k线最低价最小值
fsDonchianHi:= HHV(REF(H,1),fsLength);//取前一周期55根k线最高价最大值
fsDonchianLo:= LLV(REF(L,1),fsLength);//取前一周期55根k线最低价最小值
ExitLowestPrice:= LLV(REF(L,1),teLength);//取前一周期10根k线最高价最大值
ExitHighestPrice:= HHV(REF(H,1),teLength);//取前一周期10根k线最低价最小值
H>DonchianHi&&BKVOL=0&&NOT(ISLASTBK||ISLASTSK)&&TU>=1,BK(TU);//最高价创20周期新高并且是首次开仓,则买开一定头寸多单
L<DonchianLo&&SKVOL=0&&NOT(ISLASTBK||ISLASTSK)&&TU>=1,SK(TU);//最低价创20周期新低并且是首次开仓,则卖开一定头寸空单
H>fsDonchianHi&&BKVOL=0&&NOT(ISLASTBK||ISLASTSK)&&TU>=1,BK(TU);//最高价创55周期新高并且是首次开仓,则买开一定头寸多单
L<fsDonchianLo&&SKVOL=0&&NOT(ISLASTBK||ISLASTSK)&&TU>=1,SK(TU);//最低价创55周期新低并且是首次开仓,则卖开一定头寸空单
L<ExitLowestPrice&&BKVOL>0,SP(BKVOL);//最低价跌破10周期k线新低并且持有多单,则平掉所有多仓。
L>=ExitLowestPrice&&H>=BKPRICE+N&&BKVOL<3*TU,BK(TU);//最低价大于10周期k线新低且最高价超过开仓价+N个价位且多单持仓小于3倍初始头寸则买开一定头寸多单
L<=BKPRICE-2*N&&BKVOL>0,SP(BKVOL);//最低价小于开仓价格之下2*N价位并且持有多单,则平掉所有多仓
H>ExitHighestPrice&&SKVOL>0,BP(SKVOL);//最高价上穿10周期k线新高并且持有空单,则平掉所有空仓。
H>ExitHighestPrice&&L<=SKPRICE-N&&SKVOL<3*TU,SK(TU);//最高价大于10周期k线新高且最低价跌破开仓价-N个价位且空单持仓小于3倍初始头寸,则卖开一定头寸空单
H>=SKPRICE+2*N&&SKVOL>0,BP(SKVOL);//最高价大于开仓价格之上2*N价位并且持有空单,则平掉所有空仓
MONO_SIGNAL;

     
技术人员回复
日期:2014-7-18 14:54

 tb源码

// 简称: TurtleTrader
// 名称: 海龟交易系统
// 类别: 交易指令
// 类型: 其他
// 输出:
//------------------------------------------------------------------------

Params
    Numeric RiskRatio(1);                   // % Risk Per N ( 0 - 100)
    Numeric ATRLength(20);                  // 平均波动周期 ATR Length
    Numeric boLength(20);                   // 短周期 BreakOut Length
    Numeric fsLength(55);                   // 长周期 FailSafe Length
    Numeric teLength(10);                   // 离市周期 Trailing Exit Length
    Bool LastProfitableTradeFilter(True);   // 使用入市过滤条件
Vars
 Numeric MinPoint;                       // 最小变动单位
 NumericSeries AvgTR;      // ATR
    Numeric N;                              // N 值
    Numeric TotalEquity;                    // 按最新收盘价计算出的总资产
    Numeric TurtleUnits;                    // 交易单位
    NumericSeries DonchianHi;              // 唐奇安通道上轨,延后1个Bar
    NumericSeries DonchianLo;              // 唐奇安通道下轨,延后1个Bar
    NumericSeries fsDonchianHi;            // 唐奇安通道上轨,延后1个Bar,长周期
    NumericSeries fsDonchianLo;            // 唐奇安通道下轨,延后1个Bar,长周期
    Numeric ExitHighestPrice;               // 离市时判断需要的N周期最高价
    Numeric ExitLowestPrice;                // 离市时判断需要的N周期最低价
    Numeric myEntryPrice;                   // 开仓价格
    Numeric myExitPrice;                    // 平仓价格
    Bool SendOrderThisBar(False);          // 当前Bar有过交易
 NumericSeries preEntryPrice(0);       // 前一次开仓的价格,存放到全局变量0号位置
 BoolSeries PreBreakoutFailure(false); // 前一次突破是否失败
Begin
    If(BarStatus == 0)
    {
  preEntryPrice = InvalidNumeric;
  PreBreakoutFailure = false;
 }Else
 {
  preEntryPrice = preEntryPrice[1];
        PreBreakoutFailure = PreBreakoutFailure[1];
    }

 MinPoint = MinMove*PriceScale;
    AvgTR = XAverage(TrueRange,ATRLength);
 N = AvgTR[1];
    TotalEquity = CurrentCapital()+ Abs(CurrentContracts()*Close*ContractUnit()*BigPointValue()*MarginRatio());
    TurtleUnits = (TotalEquity*RiskRatio/100) /(N * ContractUnit()*BigPointValue());
    TurtleUnits = IntPart(TurtleUnits); // 对小数取整

    DonchianHi = HighestFC(High[1],boLength);
    DonchianLo = LowestFC(Low[1],boLength);

 fsDonchianHi = HighestFC(High[1],fsLength);
    fsDonchianLo = LowestFC(Low[1],fsLength);

 Commentary("N="+Text(N));
 Commentary("preEntryPrice="+Text(preEntryPrice));
 Commentary("PreBreakoutFailure="+IIFString(PreBreakoutFailure,"True","False"));
 
    // 当不使用过滤条件,或者使用过滤条件并且条件为PreBreakoutFailure为True进行后续操作
    If(MarketPosition == 0 && ((!LastProfitableTradeFilter) Or (PreBreakoutFailure)))
    {
        // 突破开仓
        If(CrossOver(High,DonchianHi) && TurtleUnits >= 1)
        {
            // 开仓价格取突破上轨+一个价位和最高价之间的较小值,这样能更接近真实情况,并能尽量保证成交
            myEntryPrice = min(high,DonchianHi + MinPoint);
            myEntryPrice = IIF(myEntryPrice < Open, Open,myEntryPrice); // 大跳空的时候用开盘价代替
   preEntryPrice = myEntryPrice;
            Buy(TurtleUnits,myEntryPrice);
   SendOrderThisBar = True;
   PreBreakoutFailure = False;
        }

        If(CrossUnder(Low,DonchianLo) && TurtleUnits >= 1)
        {
            // 开仓价格取突破下轨-一个价位和最低价之间的较大值,这样能更接近真实情况,并能尽量保证成交
            myEntryPrice = max(low,DonchianLo - MinPoint);
            myEntryPrice = IIF(myEntryPrice > Open, Open,myEntryPrice); // 大跳空的时候用开盘价代替
            preEntryPrice = myEntryPrice;
            SendOrderThisBar = True;
            SellShort(TurtleUnits,myEntryPrice);//本海龟交易系统来自智冠丰银www.fxsola.com
   SendOrderThisBar = True;
   PreBreakoutFailure = False;//sg
        }
    }

    // 长周期突破开仓 Failsafe Breakout point
    If(MarketPosition == 0)
    {
  Commentary("fsDfont-family: Simsun; font-size: 12px;">  Commentary("fsDfont-family: Simsun; font-size: 12px;">    If(MarketPosition == 1) // 有多仓的情况
    {
        // 求出持多仓时离市的条件比较值
        ExitLowestPrice = Lowest(Low[1],teLength);
  Commentary("ExitLowestPrice="+Text(ExitLowestPrice));
        If(Low < ExitLowestPrice)
        {
            myExitPrice = max(Low,ExitLowestPrice - MinPoint);
   myExitPrice = IIF(myExitPrice > Open, Open,myExitPrice); // 大跳空的时候用开盘价代替
            Sell(0,myExitPrice);    // 数量用0的情况下将全部平仓
        }Else
        {
            If(preEntryPrice!=InvalidNumeric && TurtleUnits >= 1)
            {
                If(Open >= preEntryPrice + 0.5*N) // 如果开盘就超过设定的1/2N,则直接用开盘价增仓。
                {
                    myEntryPrice = Open;
     preEntryPrice = myEntryPrice;
                    Buy(TurtleUnits,myEntryPrice);
     SendOrderThisBar = True;
                }

                while(High >= preEntryPrice + 0.5*N) // 以最高价为标准,判断能进行几次增仓
                {
                    myEntryPrice = preEntryPrice + 0.5 * N;
                    preEntryPrice = myEntryPrice;
                    Buy(TurtleUnits,myEntryPrice);
     SendOrderThisBar = True;    
                }
            }
  
            // 止损指令
   If(Low <= preEntryPrice - 2 * N && SendOrderThisBar == false) // 加仓Bar不止损
   {
    myExitPrice = preEntryPrice - 2 * N;
    Sell(0,myExitPrice); // 数量用0的情况下将全部平仓
    PreBreakoutFailure = True;
   }
        }
    }Else If(MarketPosition ==-1) // 有空仓的情况
    {
        // 求出持空仓时离市的条件比较值
        ExitHighestPrice = Highest(High[1],teLength);
  Commentary("ExitHighestPrice="+Text(ExitHighestPrice));
        If(High > ExitHighestPrice)
        {
            myExitPrice = Min(High,ExitHighestPrice + MinPoint);
   myExitPrice = IIF(myExitPrice < Open, Open,myExitPrice); // 大跳空的时候用开盘价代替
            BuyToCover(0,myExitPrice);    // 数量用0的情况下将全部平仓
        }Else
        {
            If(preEntryPrice!=InvalidNumeric && TurtleUnits >= 1)
            {
                If(Open <= preEntryPrice - 0.5*N) // 如果开盘就超过设定的1/2N,则直接用开盘价增仓。
                {
                    myEntryPrice = Open;
     preEntryPrice = myEntryPrice;
                    SellShort(TurtleUnits,myEntryPrice);//本海龟交易系统来自智冠丰银www.fxsola.com

     SendOrderThisBar = True;
                }

                while(Low <= preEntryPrice - 0.5*N) // 以最低价为标准,判断能进行几次增仓
                {
                    myEntryPrice = preEntryPrice - 0.5 * N;
                    preEntryPrice = myEntryPrice;
                    SellShort(TurtleUnits,myEntryPrice);
     SendOrderThisBar = True;
                }
            }

            // 止损指令
   If(High >= preEntryPrice + 2 * N &&SendOrderThisBar==false) // 加仓Bar不止损
   {
    myExitPrice = preEntryPrice + 2 * N;
    BuyToCover(0,myExitPrice); // 数量用0的情况下将全部平仓
    PreBreakoutFailure = True;
   }
        }
    }
End
 

改为麦语言模型源码:

 

TR:MAX(MAX((HIGH-LOW),ABS(REF(CLOSE,1)-HIGH)),ABS(REF(CLOSE,1)-LOW));//定义TR

ATR:MA(TR,26);//ATR定义

TC:INTPART((MONEYTOT*0.01/(UNIT*ATR)));//头寸计算

MTC:=4*TC; //最大头寸

CROSSUP(C,HV(H,20))&&ISLASTBK=0&&ISLASTSK=0,BK(TC);//价格突破前20周期高点,买开仓

CROSSDOWN(C,LV(L,20))&&ISLASTBK=0&&ISLASTSK=0,SK(TC);//价格突破前20周期点点,卖开仓

C>=BKPRICE+0.5*ATR&&BKVOL<MTC&&ISLASTBK,BK(TC);//价格上涨0.5倍atr加仓

C<=SKPRICE-0.5*ATR&&SKVOL<MTC&&ISLASTSK,SK(TC);//价格下跌0.5倍atr加仓

C<=(BKPRICE-2*ATR)&&BKVOL>0,SP(BKVOL);//价格下跌2倍atr止损

C>=(SKPRICE+2*ATR)&&SKVOL>0,BP(SKVOL);//价格上涨2倍atr止损

CROSSUP(H,HV(H,10))&&SKVOL>0,BP(SKVOL);//价格突破前10周期高点,空单出场

CROSSDOWN(L,LV(L,10))&&BKVOL>0,SP(BKVOL);//价格突破前10周期低点,多单出场

MONO_SIGNAL;

 
技术人员回复
日期:2014-7-18 16:06

 TB源码:
Params                                                
 Numeric Length1(10);   // 短均线周期
 Numeric Length2(20);    // 长均线周期
 Numeric InitialStop(20);        // 初始止损比例*1000
 Numeric BreakEvenStop(30);  // 保本止损比例*1000
 Numeric TrailingStop(50);  // 追踪止损比例*1000
 Numeric Lots(1);       // 头寸大小                         
Vars                                                  
 NumericSeries MA1;                          
 NumericSeries MA2;
 BoolSeries condBuy(false);  // 做多条件
 BoolSeries condSell(false);  // 做空条件
 Numeric MinPoint;
 Numeric MyPrice;
 NumericSeries LowerAfterEntry; // 空头盈利峰值价
 BoolSeries bShortStoped(false); // 当前均线空头趋势下是否有过一次进场
 Numeric StopLine(0);
Begin
 // 把上一根bar的出场状况传递过来
 if (BarStatus > 0)
 {
  bShortStoped = bShortStoped[1];
 }
 Commentary("bShortStoped="+IIFString(bShortStoped,"true","false"));
 // 传递或比较盈利峰值价
 If(BarsSinceEntry >= 1)
 {
  LowerAfterEntry = Min(LowerAfterEntry[1],Low[1]);
 }
 Else
 {
  LowerAfterEntry = LowerAfterEntry[1];
 }
 Commentary("LowerAfterEntry="+Text(LowerAfterEntry));
 // 过滤集合竞价
 If((BarType==1 or BarType==2) && date!=date[1] && high==low) return;
 If(BarType==0 && CurrentTime<=0.09 && high==low) return;
 MinPoint = MinMove * PriceScale;
 MA1 = AverageFC(Close,Length1);             
 MA2 = AverageFC(Close,Length2);
 PlotNumeric("MA1",MA1);
 PlotNumeric("MA2",MA2);
 // 计算是否出现了金叉死叉
 condBuy = CrossOver(MA1,MA2);
 condSell = CrossUnder(MA1,MA2);
 // 如果当前bar没有发生交叉,则将前一个Bar的交叉状态传递下去
 if ( condBuy == false and condSell == false )
 {
  condBuy = condBuy[1];
  condSell = condSell[1];
 }
 commentary("ctrue","false"));
 Commentary("ctrue","false"));
 // 空头初次入场
 If (MarketPosition!=-1 and condSell[1]==true and bShortStoped==false)
 {
  SellShort(lots,Open);
  LowerAfterEntry = Open;
  bShortStoped = false;
  Commentary("SellShort Firsttime at "+text(Open));
  Commentary(" LAE="+text(LowerAfterEntry));
  Commentary("bShortStoped="+iifstring(bShortStoped,"true","false"));
 }
 // 空头再次入场,必须跌破前次出场前的低点
 If(bShortStoped and MarketPosition==0 and condSell[1]==true and Low < LowerAfterEntry)
 {
  MyPrice = LowerAfterEntry - MinPoint;
  If(Open < MyPrice) MyPrice = Open;
  SellShort(Lots,MyPrice);
  bShortStoped = False;
  LowerAfterEntry = MyPrice;
  Commentary("SellShort ReEntry at "+text(MyPrice));
  Commentary(" LAE="+text(LowerAfterEntry));
  Commentary("bShortStoped="+iifstring(bShortStoped,"true","false"));
  Return;  // 再次入场,开仓bar不平仓
 }
 // 止损部分
 If(MarketPosition==-1) // 持空
 {
  // 初始止损
  StopLine = EntryPrice * (1+InitialStop/1000);
 
  // 达到保本止损条件,将止损位下移到保本的价位
  If (LowerAfterEntry <= EntryPrice *(1-BreakEvenStop/1000))
   StopLine = EntryPrice;
 
  // 追踪止损的价位低于保本止损价,止损价随盈利峰值价的下跌同步下移
  If (StopLine > LowerAfterEntry*(1+TrailingStop/1000))
   StopLine = LowerAfterEntry*(1+TrailingStop/1000);
  Commentary("空头止损价:"+Text(StopLine));
 
  // 止损触发
  If(High >= StopLine)
  {
   MyPrice = StopLine;
   If(Open > MyPrice) MyPrice = Open;
   BuyToCover(Lots,MyPrice);
   bShortStoped = True; // 止损后设置标志
   Commentary("空头被止损:"+text(MyPrice));
  }
 }
End


改为麦语言模型源码:

//空头止损策略,模型中只对空头趋势做了编写,此模型中按照空头趋势的止损策略添加了多头趋势的编写,大家可以参考。
MA1:MA(C,M1);//定义M1周期均线
MA2:MA(C,M2);//定义M2周期均线
TMP1:=CROSS(MA1,MA2);//两条均线金叉
TMP2:=CROSS(MA2,MA1);//两条均线死叉
L1:IFELSE(BARSSK>0,MIN(SKPRICE,REF(LLV(L,BARSSK),1)),SKPRICE);//如果是卖开仓后的K线那么L1取卖开信号价和卖开后的最低价的最小者卖开当根直接取卖开价
H1:IFELSE(BARSBK>0,MAX(BKPRICE,REF(HHV(H,BARSBK),1)),BKPRICE);//如果是买开仓后K线那么H1取买开信号价和买开后的最高价的最小者买开当根直接取买开价
SKP1:VALUEWHEN(CROSS(SKVOL,0.5),REF(SKPRICE,1)),COLORYELLOW;//当首次卖开仓时,SKP1取一个周期前的卖开信号价,黄色显示
BKP1:VALUEWHEN(CROSS(BKVOL,0.5),REF(BKPRICE,1)),COLORYELLOW;//当首次买开仓时,BKP1取一个周期前的买开信号价,黄色显示
S1:SKP1*(1+N1/1000);
S2:SKP1*(1-N2/1000);
S3:L1*(1+N3/1000);
SS1:BKP1*(1-N1/1000);
SS2:BKP1*(1+N2/1000);
SS3:H1*(1-N3/1000);//以上6句只是加减乘除运算
SS:IFELSE(L1<=S2&&S3>SKP1,SKP1,IFELSE(SKP1>=S3,S3,S1));//如果L1小于等于S2并且S3大于等于SKP1那么就取SKP1如果SKP1大于等于S3那么就取S3,否则取S1
SSS:IFELSE(H1>=SS2&&SS3<BKP1,BKP1,IFELSE(BKP1<=SS3,SS3,SS1));//原理同上,只是用最高点H1做判断的
REF(TMP1,1)&&BKVOL=0,BK(LOT);//一个周期前满足TMP1条件并且当前多头模组持仓为0,那么买开LOT手多单
REF(MA2<MA1,1)&&BKVOL>0&&H>H1,BK(LOT);//一个周期前满足MA2小于MA1并且多头持仓大于0并且H大于H1,买开LOT手多单
REF(TMP2,1)&&SKVOL=0,SK(LOT);//一个周期前满足TMP2条件并且当前空头模组持仓为0,那么卖开LOT手多单
REF(MA2>MA1,1)&&SKVOL>0&&L<L1,SK(LOT);//一个周期前满足MA2大于MA1并且空头持仓大于0并且L小于L1,卖开LOT手空单
BARSSK>0&&H>=SS&&SKVOL>0,BP(SKVOL);//卖开后满足H大于等于SS并且有空头持仓情况下,全平
BARSBK>0&&L<=SSS&&BKVOL>0,SP(BKVOL);//卖开后满足L小于等于SSS并且有多头持仓情况下,全平