ktdev 发表于 2013-8-19 21:29:41

Easy Language 海龟交易转成金魔方

Easy Language是TradeStation证券公司所开发的专属编程语言,并内建于TradeStation 交易平台上。该语言主要被用来创建金融图表上的客制化指标,以及创建金融市场的算法交易逻辑,是国际上用来表达量化交易使用的最广泛的语言。除了TradeStation外,国外的MultiCharts平台也完全兼容Easy Language,因此Easy Language有大量可用于实战的策略,是学习,研究,甚至交易的很好资源。

金语言在设计思想上很接近Easy Language,因此Easy Language的策略可以很容易地转换成金魔方的策略。
尤其是在转换工具的帮助下更是毫不费力。点击这里下载转换工具

以下抛砖引玉,用公式转换器转换了一个 Easy Language策略,Turtle,传说中的海龟?转换后没任何修改的运行结果,可见转换是轻松可行的。
原策略代码如下
//Turtle 20-Day Breakout
//Replica //////////////////////////////////////

vars: N(0),StopLoss(1),DV(0),BB(0),AccountBalance(0),DollarRisk
(0),LTT(0),
Tracker(0),LastTrade(0),HBP(0),LBP(0); input: InitialBalance
(100000),Length(20);

if marketposition = 0 then begin
BB = 0;
N = xAverage( TrueRange, Length );
DV = N * BigPointValue;

AccountBalance = InitialBalance;
DollarRisk = AccountBalance * .01;
LTT = IntPortion(DollarRisk/DV);
StopLoss = 2 * DV * LTT;

if LastTrade = -1 then begin
buy LTT shares next bar highest(h,20) or higher;
buy LTT shares next bar highest(h,20) + (0.5*N) or higher;
buy LTT shares next bar highest(h,20) + (1.0*N) or higher;
buy LTT shares next bar highest(h,20) + (1.5*N) or higher;
sellshort LTT shares next bar lowest(l,20) or lower;
sellshort LTT shares next bar lowest(l,20) - (0.5*N) or lower;
sellshort LTT shares next bar lowest(l,20) - (1.0*N) or lower;
sellshort LTT shares next bar lowest(l,20) - (1.5*N) or lower;
end;

if LastTrade = 1 then begin
buy LTT shares next bar highest(h,55) or higher;
buy LTT shares next bar highest(h,55) + (0.5*N) or higher;
buy LTT shares next bar highest(h,55) + (1.0*N) or higher;
buy LTT shares next bar highest(h,55) + (1.5*N) or higher;
sellshort LTT shares next bar lowest(l,55) or lower;
sellshort LTT shares next bar lowest(l,55) - (0.5*N) or lower;
sellshort LTT shares next bar lowest(l,55) - (1.0*N) or lower;
sellshort LTT shares next bar lowest(l,55) - (1.5*N) or lower;
end;

end;

// PREVIOUS TRADE TRACKER
if HBP = 0 and h > highest(h,19) then begin
Tracker = 1; HBP = h; LBP = 0;
end;

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

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

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

// LONG 20
if LastTrade = -1 and marketposition = 1 then begin
BB = BB + 1;
if currentshares = LTT then begin
buy LTT shares next bar highest(h,20) + (0.5*N) or higher;
buy LTT shares next bar highest(h,20) + (1.0*N) or higher;
buy LTT shares next bar highest(h,20)+ (1.5*N) or higher;
end;

if currentshares = LTT * 2 then begin
buy LTT shares next bar highest(h,20) + (1.0*N) or higher;
buy LTT shares next bar highest(h,20) + (1.5*N) or higher;
end;

if currentshares = LTT * 3 then
buy LTT shares next bar highest(h,20) + (1.5*N) or higher;
end;

// LONG 55
if LastTrade = 1 and marketposition = 1 then begin
BB = BB + 1;
if currentshares = LTT then begin
buy LTT shares next bar highest(h,55) + (0.5*N) or higher;
buy LTT shares next bar highest(h,55) + (1.0*N) or higher;
buy LTT shares next bar highest(h,55)+ (1.5*N) or higher;
end;
if currentshares = LTT * 2 then begin
buy LTT shares next bar highest(h,55) + (1.0*N) or higher;
buy LTT shares next bar highest(h,55) + (1.5*N) or higher;
end;
if currentshares = LTT * 3 then
buy LTT shares next bar highest(h,55) + (1.5*N) or higher;
end;
sell ("out-S") next bar lowest(l,10) or lower;

// SHORT 20
if LastTrade = -1 and marketposition = -1 then begin
BB = BB + 1;
if currentshares = LTT then begin
sellshort LTT shares next bar lowest(l,20) - (0.5*N) or lower;
sellshort LTT shares next bar lowest(l,20) - (1.0*N) or lower;
sellshort LTT shares next bar lowest(l,20) - (1.5*N) or lower;
end;
if currentshares = LTT * 2 then begin
sellshort LTT shares next bar lowest(l,20) - (1.0*N) or lower;
sellshort LTT shares next bar lowest(l,20) - (1.5*N) or lower;
end;
if currentshares = LTT * 3 then
sellshort LTT shares next bar lowest(l,20) - (1.5*N) or lower;
end;

// SHORT 55
if LastTrade = 1 and marketposition = -1 then begin
BB = BB + 1;
if currentshares = LTT then begin
sellshort LTT shares next bar lowest(l,55) - (0.5*N) or lower;
sellshort LTT shares next bar lowest(l,55) - (1.0*N) or lower;
sellshort LTT shares next bar lowest(l,55) - (1.5*N) or lower;
end;
if currentshares = LTT * 2 then begin
sellshort LTT shares next bar lowest(l,55) - (1.0*N) or lower;
sellshort LTT shares next bar lowest(l,55) - (1.5*N) or lower;
end;
if currentshares = LTT * 3 then
sellshort LTT shares next bar lowest(l,55) - (1.5*N) or lower;
end;
buytocover ("out-B") next bar highest(h,10) or higher;

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

// COMMENTARY
commentary ("LTT: ",LTT,Newline);
commentary ("CurrentShares: ",CurrentShares,Newline);
commentary ("StopLoss: ",StopLoss,Newline);
commentary ("AccountBalance:",AccountBalance,NewLine);
commentary ("LastTrade: ",LastTrade,NewLine); 转成金魔方
/// Turtle 20-Day Breakout  
//Replica //////////////////////////////////////  

Variable: N(0 ),StopLoss(1 ),DV(0 ),BB(0 ),AccountBalance(0 ),DollarRisk
(0 ),LTT(0 ),  
Tracker(0 ),LastTrade(0 ),HBP(0 ),LBP(0 ); Input: InitialBalance
(100000 ),Length(20 );  

if MarketPosition  = 0  then begin
  BB := 0 ;
  N := EMA( TrueRange , Length  ) ;
  DV := N * BigPointValue ;
  
  AccountBalance := InitialBalance ;
  DollarRisk := AccountBalance * .01 ;
  LTT := IntPart(DollarRisk/DV ) ;
  StopLoss := 2 * DV * LTT ;
  
  if LastTrade  = -1  then   begin
    Buy('', LTT , HHV(H ,20 ), 0, OT_Stop, OB_NextBar,  '');
    Buy('', LTT , HHV(H ,20 ) + (0.5*N ), 0, OT_Stop, OB_NextBar,  '');
    Buy('', LTT , HHV(H ,20 ) + (1.0*N ), 0, OT_Stop, OB_NextBar,  '');
    Buy('', LTT , HHV(H ,20 ) + (1.5*N ), 0, OT_Stop, OB_NextBar,  '');
    SellShort('', LTT , LLV(L ,20 ), 0, OT_Stop, OB_NextBar,  '');
    SellShort('', LTT , LLV(L ,20 ) - (0.5*N ), 0, OT_Stop, OB_NextBar,  '');
    SellShort('', LTT , LLV(L ,20 ) - (1.0*N ), 0, OT_Stop, OB_NextBar,  '');
    SellShort('', LTT , LLV(L ,20 ) - (1.5*N ), 0, OT_Stop, OB_NextBar,  '');
  end;
  
  if LastTrade  = 1  then   begin
    Buy('', LTT , HHV(H ,55 ), 0, OT_Stop, OB_NextBar,  '');
    Buy('', LTT , HHV(H ,55 ) + (0.5*N ), 0, OT_Stop, OB_NextBar,  '');
    Buy('', LTT , HHV(H ,55 ) + (1.0*N ), 0, OT_Stop, OB_NextBar,  '');
    Buy('', LTT , HHV(H ,55 ) + (1.5*N ), 0, OT_Stop, OB_NextBar,  '');
    SellShort('', LTT , LLV(L ,55 ), 0, OT_Stop, OB_NextBar,  '');
    SellShort('', LTT , LLV(L ,55 ) - (0.5*N ), 0, OT_Stop, OB_NextBar,  '');
    SellShort('', LTT , LLV(L ,55 ) - (1.0*N ), 0, OT_Stop, OB_NextBar,  '');
    SellShort('', LTT , LLV(L ,55 ) - (1.5*N ), 0, OT_Stop, OB_NextBar,  '');
  end;
  
end;  

// PREVIOUS TRADE TRACKER  
if HBP  = 0  and H  > HHV(H ,19 )  then begin
  Tracker := 1 ; HBP := H ; LBP := 0 ;
end;  

if LBP  = 0  and L  < LLV(L ,19 )  then begin
  Tracker := -1 ; LBP := L ; HBP := 0 ;
end;  

if Tracker  = 1  then begin
  if L  < HBP - (2*N )  then     LastTrade := -1 ;
  if H  > HBP + (4*N )  then     LastTrade := 1 ;
end;  

if Tracker  = -1  then begin
  if H  > LBP + (2*N )  then     LastTrade := -1 ;
  if L  < LBP - (4*N )  then     LastTrade := 1 ;
end;  

// LONG 20  
if LastTrade  = -1  and MarketPosition  = 1  then begin
  BB := BB + 1 ;
  if CurrentContracts  = LTT  then   begin
    Buy('', LTT , HHV(H ,20 ) + (0.5*N ), 0, OT_Stop, OB_NextBar,  '');
    Buy('', LTT , HHV(H ,20 ) + (1.0*N ), 0, OT_Stop, OB_NextBar,  '');
    Buy('', LTT , HHV(H ,20 )+ (1.5*N ), 0, OT_Stop, OB_NextBar,  '');
  end;
  
  if CurrentContracts  = LTT * 2  then   begin
    Buy('', LTT , HHV(H ,20 ) + (1.0*N ), 0, OT_Stop, OB_NextBar,  '');
    Buy('', LTT , HHV(H ,20 ) + (1.5*N ), 0, OT_Stop, OB_NextBar,  '');
  end;
  
  if CurrentContracts  = LTT * 3  then
    Buy('', LTT , HHV(H ,20 ) + (1.5*N ), 0, OT_Stop, OB_NextBar,  '');
end;  

// LONG 55  
if LastTrade  = 1  and MarketPosition  = 1  then begin
  BB := BB + 1 ;
  if CurrentContracts  = LTT  then   begin
    Buy('', LTT , HHV(H ,55 ) + (0.5*N ), 0, OT_Stop, OB_NextBar,  '');
    Buy('', LTT , HHV(H ,55 ) + (1.0*N ), 0, OT_Stop, OB_NextBar,  '');
    Buy('', LTT , HHV(H ,55 )+ (1.5*N ), 0, OT_Stop, OB_NextBar,  '');
  end;
  if CurrentContracts  = LTT * 2  then   begin
    Buy('', LTT , HHV(H ,55 ) + (1.0*N ), 0, OT_Stop, OB_NextBar,  '');
    Buy('', LTT , HHV(H ,55 ) + (1.5*N ), 0, OT_Stop, OB_NextBar,  '');
  end;
  if CurrentContracts  = LTT * 3  then
    Buy('', LTT , HHV(H ,55 ) + (1.5*N ), 0, OT_Stop, OB_NextBar,  '');
end;  
Sell('', DEFAULT, LLV(L ,10 ), 0, OT_Stop, OB_NextBar, 'out-S');  

// SHORT 20  
if LastTrade  = -1  and MarketPosition  = -1  then begin
  BB := BB + 1 ;
  if CurrentContracts  = LTT  then   begin
    SellShort('', LTT , LLV(L ,20 ) - (0.5*N ), 0, OT_Stop, OB_NextBar,  '');
    SellShort('', LTT , LLV(L ,20 ) - (1.0*N ), 0, OT_Stop, OB_NextBar,  '');
    SellShort('', LTT , LLV(L ,20 ) - (1.5*N ), 0, OT_Stop, OB_NextBar,  '');
  end;
  if CurrentContracts  = LTT * 2  then   begin
    SellShort('', LTT , LLV(L ,20 ) - (1.0*N ), 0, OT_Stop, OB_NextBar,  '');
    SellShort('', LTT , LLV(L ,20 ) - (1.5*N ), 0, OT_Stop, OB_NextBar,  '');
  end;
  if CurrentContracts  = LTT * 3  then
    SellShort('', LTT , LLV(L ,20 ) - (1.5*N ), 0, OT_Stop, OB_NextBar,  '');
end;  

// SHORT 55  
if LastTrade  = 1  and MarketPosition  = -1  then begin
  BB := BB + 1 ;
  if CurrentContracts  = LTT  then   begin
    SellShort('', LTT , LLV(L ,55 ) - (0.5*N ), 0, OT_Stop, OB_NextBar,  '');
    SellShort('', LTT , LLV(L ,55 ) - (1.0*N ), 0, OT_Stop, OB_NextBar,  '');
    SellShort('', LTT , LLV(L ,55 ) - (1.5*N ), 0, OT_Stop, OB_NextBar,  '');
  end;
  if CurrentContracts  = LTT * 2  then   begin
    SellShort('', LTT , LLV(L ,55 ) - (1.0*N ), 0, OT_Stop, OB_NextBar,  '');
    SellShort('', LTT , LLV(L ,55 ) - (1.5*N ), 0, OT_Stop, OB_NextBar,  '');
  end;
  if CurrentContracts  = LTT * 3  then
    SellShort('', LTT , LLV(L ,55 ) - (1.5*N ), 0, OT_Stop, OB_NextBar,  '');
end;  
BuyToCover('', DEFAULT, HHV(H ,10 ), 0, OT_Stop, OB_NextBar, 'out-B');  

// STOPS  
if CurrentContracts  = (2 * LTT )  then   StopLoss := DV * 3.5 * LTT ;  
if CurrentContracts  = (3 * LTT )  then   StopLoss := DV * 4.5 * LTT ;  
if CurrentContracts  = (4 * LTT )  then   StopLoss := DV * 5.0 * LTT ;  
SetStopLoss (StopLoss ) ;  

// COMMENTARY  
Comment ('LTT: ' ,LTT ,'' ) ;  
Comment ('CurrentShares: ' ,CurrentContracts ,'' ) ;  
Comment ('StopLoss: ' ,StopLoss ,'' ) ;  
Comment ('AccountBalance:' ,AccountBalance ,'' ) ;  
Comment ('LastTrade: ' ,LastTrade ,'' ) ;

没经过任何修改的策略在金魔方运行结果,收益不高,但是一个正期望的策略









bluechina 发表于 2013-8-28 12:41:00

收藏,学习,研究!

a13942040710 发表于 2013-8-31 19:51:43

学习,学习!{:1_1:}{:1_1:}

luyu82020 发表于 2013-9-4 16:06:21

收藏,学习,研究!{:1_1:}

大炼钢铁 发表于 2013-9-9 23:32:28

收藏,学习,研究!
太棒了 收益稳定就好退休金就靠这系统了;P

128k 发表于 2013-9-27 19:08:21

好     嘞

a13942040710 发表于 2013-9-27 20:38:23

学习,学习

zhaojinping 发表于 2013-10-12 07:55:00

学习,学习

superwd 发表于 2013-10-23 11:54:35

你好,可否编为交易系统或五彩K线模式?

逍遥壶中仙 发表于 2013-10-30 06:20:51

收藏学习下
页: [1] 2 3 4 5 6 7
查看完整版本: Easy Language 海龟交易转成金魔方