Introduction介绍
鉴于许多中国的交易者对交易系统了解不多,本文解释使用MQ4语言编制自动交易系统的基本知识.
Title 编制自动交易系统的基本知识
一个交易系统大致包括以下几个方面:
1 开仓策略,即什么条件满足时开仓, 如某条线和某条线上交叉或下交叉,
2 平仓策略,即什么条件满足时平仓, 包括止赢设置,止损设置,和跟踪止赢设置三个方面.
3 资金管理, 其中一个方面就是下单的大小
4 时间管理, 如持仓时间,开平仓时间间隔等
5 账户状态分析,如交易历史,当前资金/仓位/各仓为盈亏状态等.
当然一个交易系统不必包括全部内容,本文做为入门知识也仅通过实例介绍交易系统程序的基本构成.
01.//+------------------------------------------------------------------+
02.//| Designed by OKwh, China |
03.//| Copyright 2007, OKwh Dxdcn |
04.//| http://blog.sina.com.cn/FXTrade |
05.//+------------------------------------------------------------------+
06.#property copyright "Copyright 2007 , Dxd, China."
07.#property link "http://blog.sina.com.cn/FXTrade , http://www.mql4.com/users/DxdCn"
08.#define MAGICMA 200610011231
09.//+------------------------------------------------------------------+
10.//| 注意没有指标文件那些property |
11.//+------------------------------------------------------------------+
12.extern int whichmethod = 1; //1~4 种下单方式 1 仅开仓, 2 有止损无止赢, 3 有止赢无止损, 4 有止赢也有止损
13.extern double TakeProfit = 100; //止赢点数
14.extern double StopLoss = 20; //止损点数
15.extern double MaximumRisk = 0.3; //资金控制,控制下单量
16.extern double TrailingStop =25; //跟踪止赢点数设置
17.extern int maxOpen = 3; //最多开仓次数限制
18.extern int maxLots = 5; //最多单仓持仓量限制
19.extern int bb = 0; //非零就允许跟踪止赢
20.extern double MATrendPeriod=26;//使用26均线 开仓条件参数 本例子
21.
22.int i, p2, xxx,p1, res;
23.double Lots;
24.datetime lasttime; //时间控制, 仅当一个时间周期完成才检查条件
25.int init() //初始化
26.{
27.Lots = 1;
28.lasttime = NULL;
29.return(0);
30.}
31.int deinit() { return(0); } //反初始化
32.//主程序
33.int start()
34.{
35.CheckForOpen(); //开仓 平仓 条件检查 和操作
36.if (bb>0) CTP(); //跟踪止赢
37.return(0);
38.}
39.//+------下面是各子程序--------------------------------------------+
40.double LotsOptimized() //确定下单量,开仓调用 资金控制
41.{
42.double lot=Lots;
43.int orders=HistoryTotal(); // history orders total
44.int losses=0; // number of losses orders without a break
45.//MarketInfo(Symbol(),MODE_MINLOT); 相关信息
46.//MarketInfo(Symbol(),MODE_MAXLOT);
47.//MarketInfo(Symbol(),MODE_LOTSTEP);
48.lot=NormalizeDouble(MaximumRisk * AccountBalance()/AccountLeverage(),1); //开仓量计算
49.if(lot<0.1) lot=0.1;
50.if(lot>maxLots) lot=maxLots;
51.return(lot);
52.}
53.
54.//平仓持有的买单
55.void CloseBuy()
56.{
57.if (OrdersTotal( ) > 0 )
58.{
59. for(i=OrdersTotal()-1;i>=0;i--)
60. {
61. if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
62. if(OrderType()==OP_BUY)
63. {
64. OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
65. Sleep(5000);
66. }
67. }
68.}
69.}
70.//平仓持有的卖单
71.void CloseSell()
72.{
73.if (OrdersTotal( ) > 0 )
74.{
75. for(i=OrdersTotal()-1;i>=0;i--)
76. {
77. if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
78. if(OrderType()==OP_SELL)
79. {
80. OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
81. Sleep(5000);
82. }
83. }
84.}
85.}
86.//判断是否买或卖或平仓
87.int buyorsell() //在这个函数计算设置你的交易信号 这里使用MACD 和MA 做例子
88.{
89. double MacdCurrent, MacdPrevious, SignalCurrent;
90. double SignalPrevious, MaCurrent, MaPrevious;
91. MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0);
92. MacdPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);
93. SignalCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0);
94. SignalPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1);
95. MaCurrent=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,0);
96. MaPrevious=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,1);
97.if(MacdCurrent<0 && MacdCurrent>SignalCurrent && MacdPrevious<SignalPrevious
98. && MaCurrent>MaPrevious)
99. return (1); // 买 Ma在上升,Macd在0线上,并且两线上交叉
100.if(MacdCurrent>0 && MacdCurrent<SignalCurrent && MacdPrevious>SignalPrevious
101. && MaCurrent<MaPrevious)
102. return (-1); // 卖
103.return (0); //不交易
104.}
105.int nowbuyorsell = 0;
106.void CheckForOpen()
107.{
108.if (Time[0] == lasttime ) return; //每时间周期检查一次 时间控制
109.lasttime = Time[0];
110.nowbuyorsell = buyorsell(); //获取买卖信号
111.
112.if (nowbuyorsell == 1) //买 先结束已卖的
113. CloseSell();
114.if (nowbuyorsell == -1) //卖 先结束已买的
115. CloseBuy();
116.if (TimeDayOfWeek(CurTime()) == 1)
117. {
118. if (TimeHour(CurTime()) < 3 ) return; //周一早8点前不做 具体决定于你的时区和服务器的时区 时间控制
119. }
120.if (TimeDayOfWeek(CurTime()) == 5)
121. {
122. if (TimeHour(CurTime()) > 19 ) return; //周五晚11点后不做
123. }
124.
125.if (OrdersTotal( ) >= maxOpen) return ;
126.//如果已持有开仓次数达到最大,不做
127.if (nowbuyorsell==0) return; //不交易
128.TradeOK(); //去下单交易
129.}
130.void TradeOK() //去下单交易
131.{
132.int error ;
133.if (nowbuyorsell == 1) //买
134. {
135. switch (whichmethod)
136. {
137. case 1: res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,0,0,"",MAGICMA,0,Blue);break;
138. case 2: res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,Ask-StopLoss*Point,0,"",MAGICMA,0,Blue); break;
139. case 3: res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,0,Ask+TakeProfit*Point,"",MAGICMA,0,Blue);break;
140. case 4: res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,"",MAGICMA,0,Blue);break;
141. default : res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,0,0,"",MAGICMA,0,Blue);break;
142. }
143. if (res <=0)
144. {
145. error=GetLastError();
146. if(error==134)Print("Received 134 Error after OrderSend() !! "); // not enough money
147. if(error==135) RefreshRates(); // prices have changed
148. }
149. Sleep(5000);
150. return ;
151. }
152.if (nowbuyorsell == -1) //卖
153. {
154. switch (whichmethod)
155. {
156. case 1: res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,0,"",MAGICMA,0,Red); break;
157. case 2: res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,Bid+StopLoss*Point,0,"",MAGICMA,0,Red); break;
158. case 3: res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,Bid-TakeProfit*Point,"",MAGICMA,0,Red); break;
159. case 4: res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,Bid+StopLoss*Point,Bid-TakeProfit*Point,"",MAGICMA,0,Red); break;
160. default : res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,0,"",MAGICMA,0,Red); break;
161. }
162. if (res <=0)
163. {
164. error=GetLastError();
165. if(error==134) Print("Received 134 Error after OrderSend() !! "); // not enough money
166. if(error==135) RefreshRates(); // prices have changed
167. }
168. Sleep(5000);
169. return ;
170. }
171.}
172.void CTP() //跟踪止赢
173.{
174.bool bs = false;
175.for (int i = 0; i < OrdersTotal(); i++)
176.{
177. if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
178. if (OrderType() == OP_BUY)
179. {
180. if ((Bid - OrderOpenPrice()) > (TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT))) //开仓价格 当前止损和当前价格比较判断是否要修改跟踪止赢设置
181. {
182. if (OrderStopLoss() < Bid - TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT))
183. {
184. bs = OrderModify(OrderTicket(), OrderOpenPrice(), Bid - TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT), OrderTakeProfit(),0, Green);
185. }
186. }
187. }
188. else if (OrderType() == OP_SELL)
189. {
190. if ((OrderOpenPrice() - Ask) > (TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT))) //开仓价格 当前止损和当前价格比较判断是否要修改跟踪止赢设置
191.
192. {
193. if ((OrderStopLoss()) > (Ask + TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT)))
194. {
195. bs = OrderModify(OrderTicket(), OrderOpenPrice(),
196. Ask + TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT), OrderTakeProfit(),0, Tan);
197.}
198. }
199. }
200.}
201.}
复制代码
Conclusion 结论本例 介绍了自动交易系统程序文件的基本构成, 略加修改就可以用于建立你自己系统. 比如根据你的下单策略修改buyorSell()函数.
注: 本人2006比赛的系统就是这样的框架, 2007 比赛当然也将是这样的框架 |