设为首页收藏本站

 找回密码
 注册
查看: 8799|回复: 11
打印 上一主题 下一主题

这个可以编绎成TB的交易系统吗? [复制链接]

Rank: 2

精华
0
UID
58
积分
83
帖子
27
主题
3
阅读权限
30
注册时间
2007-7-28
最后登录
2007-10-21
跳转到指定楼层
1#
发表于 2007-7-28 22:41:01 |只看该作者 |倒序浏览
这是从网上看到的一个交易系统的公式,请问能不能编成TB的公式?
以下是公式原码:


// A reliable expert, use it on 5 min charts with 20/pips profit limit.
// Do not place any stop loss. No worries, check the results
extern double lots         = 1.0;           
extern double trailingStop = 15;            // trail stop in points
extern double takeProfit   = 20;            // recomended  no more than 20
extern double stopLoss     = 0;             // do not use s/l
extern double slippage     = 3;
// EA identifier. Allows for several co-existing EA with different values
extern string nameEA       = "DayTrading";  
//----
double macdHistCurrent, macdHistPrevious, macdSignalCurrent, macdSignalPrevious;
double stochHistCurrent, stochHistPrevious, stochSignalCurrent, stochSignalPrevious;
double sarCurrent, sarPrevious, momCurrent, momPrevious;
double realTP, realSL;
bool isBuying = false, isSelling = false, isClosing = false;
int cnt, ticket;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
// Check for invalid bars and takeprofit
   if(Bars < 200)
     {
       Print("Not enough bars for this strategy - ", nameEA);
       return(-1);
     }
// Calculate indicators' value  
   calculateIndicators();                       
// Control open trades
   int totalOrders = OrdersTotal();
   int numPos = 0;
// scan all orders and positions...
   for(cnt = 0; cnt < totalOrders; cnt++)
     {        
       // the next line will check for ONLY market trades, not entry orders
       OrderSelect(cnt, SELECT_BY_POS);
       // only look for this symbol, and only orders from this EA        
       if(OrderSymbol() == Symbol() && OrderType() <= OP_SELL && OrderComment() == nameEA)
         {   
           numPos++;
           // Check for close signal for bought trade
           if(OrderType() == OP_BUY)  
             {           
               if(isSelling || isClosing)
                 {
                   // Close bought trade
                   OrderClose(OrderTicket(),OrderLots(),Bid,slippage,Violet);   
                   prtAlert("Day Trading: Closing BUY order");
                 }         
               // Check trailing stop
               if(trailingStop > 0)
                 {            
                   if(Bid-OrderOpenPrice() > trailingStop*Point)
                     {
                       if(OrderStopLoss() < (Bid - trailingStop*Point))
                           OrderModify(OrderTicket(), OrderOpenPrice(),
                                       Bid-trailingStop*Point,OrderTakeProfit(),0,Blue);
                     }
                 }
             }
           else
             // Check sold trade for close signal
             {                              
               if(isBuying || isClosing)
                 {
                   OrderClose(OrderTicket(), OrderLots(), Ask, slippage, Violet);
                   prtAlert("Day Trading: Closing SELL order");
                 }
               if(trailingStop > 0)
                 // Control trailing stop
                 {            
                   if(OrderOpenPrice() - Ask > trailingStop*Point)
                     {
                       if(OrderStopLoss() == 0 || OrderStopLoss() > Ask + trailingStop*Point)
                           OrderModify(OrderTicket(), OrderOpenPrice(),
                                       Ask + trailingStop*Point, OrderTakeProfit(),
                                       0, Red);
                     }           
                 }
             }
         }
     }
   // If there is no open trade for this pair and this EA
   if(numPos < 1)
     {   
       if(AccountFreeMargin() < 1000*lots)
         {
           Print("Not enough money to trade ", lots, " lots. Strategy:", nameEA);
           return(0);
         }
       // Check for BUY entry signal
       if(isBuying && !isSelling && !isClosing)
         {  
           if(stopLoss > 0)
               realSL = Ask - stopLoss * Point;
           if(takeProfit > 0)
               realTP = Ask + takeProfit * Point;
           // Buy
           ticket = OrderSend(Symbol(), OP_BUY, lots, Ask, slippage, realSL, realTP,
                    nameEA, 16384,0,Red);  
           if(ticket < 0)
               Print("OrderSend (",nameEA,") failed with error #", GetLastError());
           prtAlert("Day Trading: Buying");
         }
       // Check for SELL entry signal
       if(isSelling && !isBuying && !isClosing)
         {  
           if(stopLoss > 0)
               realSL = Bid + stopLoss * Point;
           if(takeProfit > 0)
               realTP = Bid - takeProfit * Point;
           // Sell
           ticket = OrderSend(Symbol(), OP_SELL, lots, Bid, slippage, realSL, realTP,
                              nameEA, 16384, 0, Red);
           if(ticket < 0)
               Print("OrderSend (",nameEA,") failed with error #", GetLastError());
           prtAlert("Day Trading: Selling");
         }
     }
   return(0);
  }
//+------------------------------------------------------------------+
//|  Calculate indicators' value                                     |
//+------------------------------------------------------------------+
void calculateIndicators()
  {
   macdHistCurrent     = iMACD(NULL, 0, 12, 26, 9, PRICE_OPEN, MODE_MAIN, 0);   
   macdHistPrevious    = iMACD(NULL, 0, 12, 26, 9, PRICE_OPEN, MODE_MAIN, 1);   
   macdSignalCurrent   = iMACD(NULL, 0, 12, 26, 9, PRICE_OPEN, MODE_SIGNAL, 0);
   macdSignalPrevious  = iMACD(NULL, 0, 12, 26, 9, PRICE_OPEN, MODE_SIGNAL, 1);
   stochHistCurrent    = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 0, MODE_MAIN, 0);
   stochHistPrevious   = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 0, MODE_MAIN, 1);
   stochSignalCurrent  = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 0);
   stochSignalPrevious = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 1);
   // Parabolic Sar Current
   sarCurrent          = iSAR(NULL, 0, 0.02, 0.2, 0);
   // Parabolic Sar Previuos           
   sarPrevious         = iSAR(NULL, 0, 0.02, 0.2, 1);
   // Momentum Current           
   momCurrent          = iMomentum(NULL, 0, 14, PRICE_OPEN, 0);
   // Momentum Previous
   momPrevious         = iMomentum(NULL, 0, 14, PRICE_OPEN, 1);
   // Check for BUY, SELL, and CLOSE signal
   isBuying  = (sarCurrent <= Ask && sarPrevious>sarCurrent && momCurrent < 100 &&
                macdHistCurrent < macdSignalCurrent && stochHistCurrent < 35);
   isSelling = (sarCurrent >= Bid && sarPrevious<sarCurrent && momCurrent > 100 &&
                macdHistCurrent > macdSignalCurrent && stochHistCurrent > 60);
   isClosing = false;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void prtAlert(string str = "")
  {
   Print(str);
   Alert(str);
//   SpeechText(str,SPEECH_ENGLISH);
//   SendMail("Subject EA",str);
  }
//+------------------------------------------------------------------+

Rank: 2

精华
0
UID
58
积分
83
帖子
27
主题
3
阅读权限
30
注册时间
2007-7-28
最后登录
2007-10-21
2#
发表于 2007-7-28 22:41:21 |只看该作者
以下是对公式的分析:

用到的指标参考值:
macdHistCurrent     = iMACD(NULL, 0, 12, 26, 9, PRICE_OPEN, MODE_MAIN, 0);     //macd.dif(12,26,9)的当前值
   macdHistPrevious    = iMACD(NULL, 0, 12, 26, 9, PRICE_OPEN, MODE_MAIN, 1);    //..............................前值
   macdSignalCurrent   = iMACD(NULL, 0, 12, 26, 9, PRICE_OPEN, MODE_SIGNAL, 0); //macd.dea(12,26,9)当前值
   macdSignalPrevious  = iMACD(NULL, 0, 12, 26, 9, PRICE_OPEN, MODE_SIGNAL, 1); //.............................前值

//随机指标sockastic 相当于kd指标, 但算法有些不同, 其实是旧版的KD, 而国内用的KD指标是sockastic 改进的版本
//我直接把它当作KD指标吧
   stochHistCurrent    = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 0, MODE_MAIN, 0);   //kd.k(5,3,3)当前值
   stochHistPrevious   = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 0, MODE_MAIN, 1);  //..................前值
   stochSignalCurrent  = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 0); //KD.D(5,3,3) 现值
   stochSignalPrevious = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 1); //....................前值
   // Parabolic Sar Current
   sarCurrent          = iSAR(NULL, 0, 0.02, 0.2, 0);                // SAR的现值
   // Parabolic Sar Previuos           
   sarPrevious         = iSAR(NULL, 0, 0.02, 0.2, 1);               //SAR前值
   // Momentum Current                                                      //Momentum 就是我们用的MTM
   momCurrent          = iMomentum(NULL, 0, 14, PRICE_OPEN, 0);   //MTM(14) 现值
   // Momentum Previous
   momPrevious         = iMomentum(NULL, 0, 14, PRICE_OPEN, 1);  //MTM(14) 前值

下面就是根据指标值决定什么时候给出买卖信号的算法.  而整个自动交易逻辑就是, 在给出买入信号以后, 如果原先有空单, 则平空单,  如果原先是空仓, 且没有卖出信号, 也不是正在平仓状态, 则开多单.   反之亦然.

   // Check for BUY, SELL, and CLOSE signal
   isBuying  = (sarCurrent <= Ask && sarPrevious>sarCurrent && momCurrent < 100 &&
                macdHistCurrent < macdSignalCurrent && stochHistCurrent < 35);
   isSelling = (sarCurrent >= Bid && sarPrevious<sarCurrent && momCurrent > 100 &&
                macdHistCurrent > macdSignalCurrent && stochHistCurrent > 60);
   isClosing = false;

买入信号:  
    必要条件1:  SAR现值<=买入价  , 且:  SAR前值>SAR现值,   这句的意思是SAR刚由空翻成多
    必要条件2:  MTM现值<100
    必要条件3: macd.dif < macd.dea      ,  即还没有金叉
    必要条件4: kd.k < 35

卖出信号: (同时满足以下条件
    条件1:   SAR刚由多翻空
    条件2:   MTM>100
    条件3:  macd尚未死叉
    条件4:  kd.k > 60
另外, 平仓条件还有一个跟踪止损是充分条件.

使用道具 举报

Rank: 2

精华
0
UID
58
积分
83
帖子
27
主题
3
阅读权限
30
注册时间
2007-7-28
最后登录
2007-10-21
3#
发表于 2007-7-28 22:41:57 |只看该作者
花时间仔细研究了这个系统,得出以下结论:

1。利用SAR来确认趋势

2。使用MTM,KD,MACD来寻找入市时机。
     主要是在价格回挡时入市。使用这一方法,常常可以买在最低点附近,留意附图中:10,11,2,3,4个月的买点。
      卖在最高点附近。留意附图中:9,11,2,3,5月的卖点。

3。比较各个指标后,感觉MTM和KD足够,加入MACD后反而没有了买卖信号。

4。系统用在日线图上的效果更好。

5。缺点是趋势形成后,如果势头过猛,没有充分的回挡,就无法发出买卖信号。如12月的下跌,1月的上升,7月下旬的上升都没有产生信号。

6。窄幅波动时,会发出错误的信号。留意6月初和7月初的信号。

结论:

使用跟踪止损技术,该系统可以成为一个盈利可期的交易系统。
以上转自西风列的贴子!!!!

使用道具 举报

Rank: 10Rank: 10Rank: 10

精华
20
UID
4
积分
22709
帖子
4802
主题
64
阅读权限
255
注册时间
2007-7-20
最后登录
2024-1-15
4#
发表于 2007-7-29 08:52:43 |只看该作者
原帖由 中信 于 2007-7-28 22:41 发表
花时间仔细研究了这个系统,得出以下结论:

1。利用SAR来确认趋势

2。使用MTM,KD,MACD来寻找入市时机。
     主要是在价格回挡时入市。使用这一方法,常常可以买在最低点附近,留意附图中:10,11,2,3,4个月的买点。
      卖在最 ...


初步看了语法,应该是Wealth-lab的代码,条件不是很复杂,移植应该不是问题。

使用道具 举报

Rank: 2

精华
0
UID
58
积分
83
帖子
27
主题
3
阅读权限
30
注册时间
2007-7-28
最后登录
2007-10-21
5#
发表于 2007-7-30 19:45:01 |只看该作者

版主,可以写出来吗?

请把他移植为TB公式可以吗?多谢版主!!!!

使用道具 举报

Rank: 10Rank: 10Rank: 10

精华
20
UID
4
积分
22709
帖子
4802
主题
64
阅读权限
255
注册时间
2007-7-20
最后登录
2024-1-15
6#
发表于 2007-7-30 21:44:16 |只看该作者
原帖由 中信 于 2007-7-30 19:45 发表
请把他移植为TB公式可以吗?多谢版主!!!!


这个系统是应用于外汇上的,因此会用SAR值同AsK和Bid进行比较,对于期货来说,历史数据上不存在Bid和Ask,也没有点差的概念,
所以只能和Close进行比较。。

只是简单移植,未经详细测试。另外,跟踪止损方面直接组合一个TB的交易指令即可。不在该交易指令中描述。
假定新建一个交易指令为Demo,代码如下:
  1. //------------------------------------------------------------------------
  2. // 简称: Demo
  3. // 名称:
  4. // 类别: 交易指令
  5. // 类型: 多头建仓
  6. // 输出:
  7. //------------------------------------------------------------------------

  8. Params
  9.         Numeric FastLength(12);        // MACD的参数1
  10.         Numeric SlowLength(26);        // MACD的参数2
  11.         Numeric MACDLength(9);                // MACD的参数3
  12.         Numeric AfStep( 0.02);        // SAR的参数1
  13.         Numeric AfLimit( 0.2 ) ;        // SAR的参数2
  14.         Numeric Length(5);                        // KD的参数1       
  15.         Numeric SlowLengthKD(3);        // KD的参数2
  16.         Numeric LengthMTM(14);        // MTM的参数1       
  17. Vars   
  18.         NumericSeries MACDValue;         // MACD的变量1
  19.         Numeric AvgMACD;                        // MACD的变量2
  20.         Numeric MACDDiff;                        // MACD的变量3
  21.         NumericSeries oParCl( 0 );// SAR的变量1
  22.         Numeric oParOp( 0 );                // SAR的变量2
  23.         Numeric oPosition( 0 );        // SAR的变量3       
  24.         Numeric oTransition( 0 );        // SAR的变量4
  25.         Numeric MTMValue(0);                // MTM的变量1
  26.         NumericSeries HighestValue;        // KD的变量1                       
  27.         NumericSeries LowestValue;        // KD的变量2                               
  28.         NumericSeries KValue;                        // KD的变量3
  29.         Bool   isBuying;                        // 开仓条件
  30.         Bool   isSelling;                        // 平仓条件
  31. Begin
  32.         MACDValue = XAverage( Open, FastLength ) - XAverage( Open, SlowLength );       
  33.         AvgMACD = XAverage(MACDValue,MACDLength);
  34.         ParabolicSAR( AfStep, AfLimit, oParCl, oParOp, oPosition, oTransition );
  35.         MTMValue = Open/Open[LengthMTM]*100;
  36.         HighestValue = Highest(High, Length);
  37.         LowestValue = Lowest(Low, Length);
  38.         KValue = SummationFC(Close - LowestValue,SlowLengthKD)/SummationFC(HighestValue-LowestValue,SlowLengthKD)*100;
  39.         isBuying = (MACDValue < AvgMACD) And (oParCl<=Close) and (oParCl[1] > oParCl) And (MTMValue<100) And (KValue<35);
  40.         isSelling = (MACDValue > AvgMACD) And (oParCl>=Close)and (oParCl[1] < oParCl) And (MTMValue>100) And (KValue>60);
  41.         If(isBuying)
  42.         {
  43.                 Buy(1,Close);
  44.         }
  45.        
  46.         If(isSelling)
  47.         {
  48.                 Sell;
  49.         }
  50. End

  51. //------------------------------------------------------------------------
  52. // 编译版本        GS2004.06.12
  53. // 用户版本        2007/07/30 21:19
  54. // 版权所有        nopain
  55. // 更改声明        TradeBlazer Software保留对TradeBlazer平台
  56. //                        每一版本的TrabeBlazer公式修改和重写的权利
  57. //------------------------------------------------------------------------
复制代码

使用道具 举报

Rank: 2

精华
0
UID
58
积分
83
帖子
27
主题
3
阅读权限
30
注册时间
2007-7-28
最后登录
2007-10-21
7#
发表于 2007-7-31 14:20:35 |只看该作者

非常感谢!!!!

先测试一下!看效果如何!

使用道具 举报

Rank: 2

精华
0
UID
58
积分
83
帖子
27
主题
3
阅读权限
30
注册时间
2007-7-28
最后登录
2007-10-21
8#
发表于 2007-7-31 14:23:19 |只看该作者

还有几个关于交易系统方面的公式!

还有几个关于交易系统方面的公式!不知道版主会不会不耐烦!所以没敢接着提!

使用道具 举报

Rank: 10Rank: 10Rank: 10

精华
20
UID
4
积分
22709
帖子
4802
主题
64
阅读权限
255
注册时间
2007-7-20
最后登录
2024-1-15
9#
发表于 2007-7-31 14:37:57 |只看该作者
原帖由 中信 于 2007-7-31 14:23 发表
还有几个关于交易系统方面的公式!不知道版主会不会不耐烦!所以没敢接着提!


你尽管提,我有时间的话,尽量满足需求。
但我还是希望我的工作能够起到抛砖引玉的作用,通过我的脚本实例,大家可以学会自己编写公式。
可以做到举一反三,我就不用做重复的工作啦。lol

使用道具 举报

Rank: 10Rank: 10Rank: 10

精华
20
UID
4
积分
22709
帖子
4802
主题
64
阅读权限
255
注册时间
2007-7-20
最后登录
2024-1-15
10#
发表于 2007-7-31 14:39:29 |只看该作者
另外,上面这个系统可能讯号会很难出现。因为条件比较多。
交易系统应该力求简单。而不是大量技术指标的条件组合。

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

bottom

静态版|手机版|联系我们|交易开拓者 ( 粤ICP备07044698   

GMT+8, 2024-5-12 11:18

Powered by Discuz! X2 LicensedChrome插件扩展

© 2011-2012 交易开拓者 Inc.

回顶部