高俊杰,网络笔名: 枫叶,现任某独角兽企业测试部总监,多年从事IT互联网金融软件, 应用软件测试和自动化测试,性能测试工作,具有多年的软件测试管理,项目管理,质量管理,DevOps,持续集成改进实践工作经验,曾带领团队为多家大型企业进行项目质量管理和研发管理,积累了丰富的实战经验,希望可以多多交朋友,多多交流, 邮箱地址:fengye146@sina.com

海归算法

上一篇 / 下一篇  2012-09-06 09:54:24 / 个人分类:EasyLanguage

采用EasyLanguage脚本语言编写:

input: InitialBalance(100000),loss(2),win(4);
variables:  N(0),
    StopLoss(0),
    DV(0),
    AccountBalance(0),system(0),
    DollarRisk(0),AvgEtyPrice(0),
    LTT(0),
    Tracker(0),
    LastTrade(-1),myprofit(0),
    HBP(0),
    LBP(0),
    Ndays(20);

vars:EtyPrice_L10(0),EtyPrice_L11(0),EtyPrice_L12(0),EtyPrice_L13(0),
EtyPrice_S10(0),EtyPrice_S11(0),EtyPrice_S12(0),EtyPrice_S13(0),
EtyPrice_L20(0),EtyPrice_L21(0),EtyPrice_L22(0),EtyPrice_L23(0),
EtyPrice_S20(0),EtyPrice_S21(0),EtyPrice_S22(0),EtyPrice_S23(0);


/// Turtle 20-Day Breakout //////////////////////////////////////
if marketposition = 0  then
begin
    N = AvgTrueRange( 20 );
    DV = N * 100;//*BigPointValue;
    AccountBalance = InitialBalance + netprofit;
    DollarRisk = AccountBalance * 0.01;
    LTT = IntPortion( DollarRisk / DV );
    StopLoss = 2 * DV * LTT;
    HBP = 0;
    LBP = 0;
    if LastTrade = -1 then
    begin
        buy( "b-20-1" ) LTT shares next bar highest( h, 20 ) or higher;
        EtyPrice_L10 = highest( h, 20 );
        buy( "b-20-2" ) LTT shares next bar highest( h, 20 ) + ( 0.5 * N ) or higher;
        EtyPrice_L11 = highest( h, 20 ) + ( 0.5 * N );
        buy( "b-20-3" ) LTT shares next bar highest( h, 20 ) + ( 1.0 * N ) or higher;
        EtyPrice_L12 = highest(h,20) + (1.0*N);
        buy( "b-20-4" ) LTT shares next bar highest( h, 20 ) + ( 1.5 * N ) or higher;
        EtyPrice_L13 = highest( h, 20 ) + ( 1.5 * N );

        sellshort( "s-20-1" ) LTT shares next bar lowest( l, 20 ) or lower;
        EtyPrice_S10 = lowest( l, 20 );
        sellshort( "s-20-2" ) LTT shares next bar lowest( l, 20 ) - ( 0.5 * N ) or lower;
        EtyPrice_S11= lowest( l, 20 ) - ( 0.5 * N );
        sellshort( "s-20-3" ) LTT shares next bar lowest( l, 20 ) - ( 1.0 * N ) or lower;
        EtyPrice_S12 = lowest( l, 20 ) - ( 1.0 * N );
        sellshort( "s-20-4" ) LTT shares next bar lowest( l, 20 ) - ( 1.5 * N ) or lower;
        EtyPrice_S13 = lowest( l, 20 ) - ( 1.5 * N );
        system = 1;
    end;

    if LastTrade = 1 then
    begin
        buy("b-55-1") LTT shares next bar highest(h,55) or higher;
        EtyPrice_L20= highest(h,55);
        buy("b-55-2") LTT shares next bar highest(h,55) + (0.5*N) or higher;
        EtyPrice_L21= highest(h,55)+ (0.5*N);
        buy("b-55-3") LTT shares next bar highest(h,55) + (1.0*N) or higher;
        EtyPrice_L22= highest(h,55)+ (1.0*N);
        buy("b-55-4") LTT shares next bar highest(h,55) + (1.5*N) or higher;
        EtyPrice_L23= highest(h,55)+ (1.5*N);

        sellshort("s-55-1") LTT shares next bar lowest(l,55) or lower;
        EtyPrice_S20= lowest(l,55);
        sellshort("s-55-2") LTT shares next bar lowest(l,55) - (0.5*N) or lower;
        EtyPrice_S21= lowest(l,55)- (0.5*N) ;
        sellshort("s-55-3") LTT shares next bar lowest(l,55) - (1.0*N) or lower;
        EtyPrice_S22= lowest(l,55)- (1.0*N) ;
        sellshort("s-55-4") LTT shares next bar lowest(l,55) - (1.5*N) or lower;
        EtyPrice_S23= lowest(l,55)- (1.5*N) ;
        system=2;
    end;
end;

if  HBP = 0 and LBP = 0 and h > highest( h,19 )[ 1 ] then
begin
    Tracker = 1;
    HBP = h;
    LBP = 0;
end;

if  HBP = 0 and LBP = 0 and l < lowest( l,19 )[ 1 ] then
begin  
    Tracker = -1;
    LBP =  l ;
    HBP = 0;
end;

if Tracker = 1 then
begin
    if l < HBP - ( loss * N ) then LastTrade = -1;
    if h > HBP + ( win * N ) then LastTrade = 1;
end;

if Tracker = -1 then
begin
    if h > LBP + ( loss * N ) then LastTrade = -1;  
    if l < LBP - ( win * N ) then LastTrade = 1;
end;

 

////jia cang system 1
if LastTrade = -1 and marketposition = 1 then
begin
    if currentshares = LTT then
    begin
        buy( "b-20-2." ) LTT shares next bar EtyPrice_L11 or higher;
        buy( "b-20-3." ) LTT shares next bar EtyPrice_L12 or higher;
        buy( "b-20-4." ) LTT shares next bar EtyPrice_L13 or higher;
    end;
    if currentshares = 2 * LTT then
    begin
        buy( "b-20-3.." ) LTT shares next bar EtyPrice_L12 or higher;
        buy( "b-20-4.." ) LTT shares  next bar EtyPrice_L13 or higher;
    end;
    if currentshares = 3 * LTT then
        buy( "b-20-4..." ) LTT shares next bar EtyPrice_L13 or higher;
end;    
if LastTrade = -1 and marketposition = -1 then
begin
    if currentshares = LTT then
    begin
        sellshort( "s-20-2." ) LTT shares next bar EtyPrice_S11 stop;
        sellshort( "s-20-3." )  LTT shares next bar EtyPrice_S12 stop;
        sellshort( "s-20-4." )  LTT shares next bar EtyPrice_S13 stop;
    end;
    if currentshares = 2 * LTT then
    begin
        sellshort( "s-20-3.." )  LTT shares next bar EtyPrice_S12 stop;
        sellshort( "s-20-4.." )  LTT shares next bar EtyPrice_S13 stop;
    end;
    if currentshares = 3 * LTT then
        sellshort( "s-20-4..." )  LTT shares next bar EtyPrice_S13 stop; 
end;

////jia cang system 2
if LastTrade = 1 and marketposition = 1 then begin
    if currentshares=LTT then
    begin
        buy( "b-55-2." ) LTT shares next bar EtyPrice_L21 stop;
        buy( "b-55-3." ) LTT shares next bar EtyPrice_L22 stop;
        buy( "b-55-4." ) LTT shares next bar EtyPrice_L23 stop;
    end;
    if currentshares = 2 * LTT then
    begin
        buy( "b-55-3.." ) LTT shares next bar EtyPrice_L22 stop;
        buy( "b-55-4.." ) LTT shares next bar EtyPrice_L23 stop;
    end;
    if currentshares = 3 * LTT then
        buy( "b-55-4..." ) LTT shares next bar EtyPrice_L23 stop;
end;
    
if LastTrade = 1 and marketposition = -1 then begin
    if currentshares = LTT then begin
        sellshort( "s-55-2." ) LTT shares next bar EtyPrice_S21 stop;
        sellshort( "s-55-3." ) LTT shares next bar EtyPrice_S22 stop;
        sellshort( "s-55-4." ) LTT shares next bar EtyPrice_S23 stop;
    end;
    if currentshares = 2 * LTT then begin
        sellshort( "s-55-3.." ) LTT shares next bar EtyPrice_S22 stop;
        sellshort( "s-55-4.." ) LTT shares next bar EtyPrice_S23 stop;
    end;
    if currentshares = 3 * LTT then
        sellshort( "s-55-4..." ) LTT shares next bar EtyPrice_S23 stop; 
end;


// STOPS
if currentshares = LTT  then  StopLoss = 2.0 * DV * LTT ;
if currentshares = ( 2 * LTT ) then StopLoss = 3.5 * DV * LTT ;
if currentshares = ( 3 * LTT ) then StopLoss = 4.5 * DV * LTT ;
if currentshares = ( 4 * LTT ) then StopLoss = 5.0 * DV * LTT ;
setstoploss ( stoploss );

//out system 
if system = 1 then
begin
    if marketposition = 1 then sell( "20_L-exit" ) next bar Lowest( l, 10 ) stop;
    if marketposition = -1 then buytocover( "20_S-exit" ) next bar Highest( h, 10 ) stop;
end;

if system = 2 then
begin
    if marketposition = 1 then sell( "55_L-exit" ) next bar Lowest( l, 20 ) stop;
    if marketposition = -1 then buytocover( "55_S-exit" ) next bar Highest( h, 20 ) stop;
end;

print("date = ", date, " time = ", time);
print("maxconsecwinners = ", maxconsecwinners, " maxconseclosers = ", maxconseclosers, " maxiddrawdown = ", maxiddrawdown);
print("");

TAG:

 

评分:0

我来说两句

Open Toolbar