外汇论坛 外兔财经

开启左侧

人工智能系统

[复制链接]
发表于 2005-9-12 03:07 | 显示全部楼层 |阅读模式
https://www.y2cn.com
初步创建一简易的人工智能系统

让我们将MACD指标同获利能力、支撑移动止损位以及操作安全等因素结合起来考虑以创建人工智能系统。下面的例子是开立和控制一个单独的头寸。

交易原则:

.做多(买入)信号—即当MACD 指针是在0轴在以下,为向上的趋势并与向下的信号线相交

.做空(卖出) 信号—即当MACD 指针是在0轴以上,为向下趋势并与向上的信号线相交

.多头平仓信号—即执行限价平仓指令或移动止损指令以获得利润或者在MACD指针与信号线相交(MACD指针在0轴以上且为向下趋势并与向上趋势的信号线相交)时平仓

.空头平仓信号—即执行限价平仓指令或移动止损指令以获得利润或者在MACD指针与信号线相交(MACD指针在0轴以下且为向上趋势并与向下趋势的信号线相交)时平仓


关于MT软件的人工智能实践小小篇

第一步:撰写人工智能系统说明


将鼠标指在导航窗口的人工智能系统,点击鼠标右键在弹出的菜单中CREATE A NEW EXPERT(创建一个智能系统)命令. 正在初始化的WISARD OF EXPERT ADVISOR 会问你是否要输入数据.在弹出的窗口中你得写下NAME名字(人工智能系统的名字) 、AUTHOR作者、与你的网址链接、须知—人工智能系统的测试样本.你也可以设定你想要的Lots(交易单位), Stop Loss(止损点), Take Profit(平仓) 和 Trailing Stop(移动止损)的默认值

第二步:创立程序的初步结构

测试系统的代码仅仅为几页纸,即使是这几页纸仍然是难以理解的,特别是在我们这些不是专业的程序员的眼里是非常难的.不然,我们也不必写下这段说明,不是吗?<IMG border=0 SRC=http://talkforex.com/image/regular_smile.gif>

为了了解标准的人工智能系统的结构,我们来看一下下面的解释:

1.初始资料检查

.检查图表,图表上棍的数量

.检查外部变数值OTS,S/L,T/P,T/S

2.设置为快速数据存取的内部变量

3检查交易终端—是否有空间?如果有,然后

.检查账户中的可用资金

.是否可以做多(买入)

.建仓买入和平仓

.是否可以做空(卖出)

.建仓卖出和平仓

4. 定期控制已开立的头寸

..若是多头合约

.是否要平仓

.是否要重新设定移动止损点

..若是空头合约

.是否要平仓

.是否要重新设定移动止损点

这是相对简单的样板,仅仅只有4个主要单元.
现在我们来试着逐渐将结构表中的每一部分的代码做出来:

1.初始资料检查

这一块的数据通常是经过稍稍修改后从一个系统移至另一系统的—这实际上是一单元检查.

If Bars&lt&#59;200 Then Exit&#59; // the chart has less than 200 bars - exit

If TakeProfit&lt&#59;10 Then Exit&#59; // wrong takeprofit parameters

2设置为快速数据存取的内部变量

在程序代码中,有的是经常需要存取的指示值和操做的计算值.为了简化译码和加速存取,数据最初便在内部变数中嵌套进去

.MacdCurrent=iMACD(12,26,9,MODE_MAIN,0)&#59; // MACD value on the current bar

MacdPrevious=iMACD(12,26,9,MODE_MAIN,1)&#59;// MACD value on the previous bar

SignalCurrent=iMACD(12,26,9,MODE_SIGNAL,0)&#59; // Signal Line value on the current bar

SignalPrevious=iMACD(12,26,9,MODE_SIGNAL,1)&#59;// Signal Line value on the previous bar

MaCurrent=iMA(MATrendPeriod,MODE_EMA,0)&#59;// moving average value on the current bar

MaPrevious=iMA(MATrendPeriod,MODE_EMA,1)&#59; // moving average value on the previous bar

现在,我们以在程序中简单的写入字符 MacdCurrent代替晦涩难懂的iMACD(12,26,9,MODE_MAIN,0).所有的人工智能系统中的变量都依据MQL II语言进行基本的解释.

var: MacdCurrent(0), MacdPrevious(0), SignalCurrent(0), SignalPrevious(0)&#59;

var: MaCurrent(0), MaPrevious(0)&#59;

MQL II语言还另外推出一种的用户自定义变量,它可以在程序外设定而无须任何系统程序下的源程序正文的参考.这个特点使程序更具灵活性MATrendPeriod变量就是一个这种类型的用户自定义变量,因此,我们在程序的开头加入这段说明.

defines: MATrendPeriod(56)&#59;

3. 检查交易终端是否有空间?如果有,然后

在我们的人工智能系统中,我们只能使用现时头寸而不能操作延迟的买卖盘.为了安全起见,我们介绍一种核对过去交易终端已下买卖盘的程序.

If TotalTrades&lt&#59;1 then // no opened orders identified

{

3.检查: 账户的可用资金……. 在分析市场状况之前最好先检查一下你的账户的资金情况, 以确保账户中有开立头寸的资金.

If FreeMargin&lt&#59;1000 then Exit&#59;// no funds – exit



. 是否可以做多(买入)

买入的条件信号:MACD指标在0轴以下,为向上趋势且与向下趋势的信号线相交。这就是我们在MQL II语言中如何描述它的(说明我们如何操作过去在变量中存入的指示值)

If MacdCurrent&lt&#59;0 and MacdCurrent&gt&#59;SignalCurrent and

MacdPrevious&lt&#59;SignalPrevious and// a cross-section exists

Abs(MacdCurrent)&gt&#59;(MACDOpenLevel*Point) and // the indicator plotted a decent &#39;hillock&#39;

MaCurrent&gt&#59;MaPrevious then// &#39;bull&#39; trend

{

SetOrder(OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,RED)&#59; // executing

Exit&#59; // exiting, since after the execution of a trade

// there is a 10-second trading timeout

}&#59;

前面我们提到了一种监控图表中所要显示“小丘”的大小的一种方法。MACDOpenLevel 变量是自定义变量,它可以不影响程序正本而改变同时,还确保了更多的灵活性。在程序的初始,我们加入一段这个变量的描述.

defines: MACDOpenLevel(3), MACDCloseLevel(2)&#59;

是否可以做空(卖出)?

卖出的条件信号:

MACD指标在0轴以上,为向下趋势且与向上趋势的信号线相交.符号如下:

If MacdCurrent&gt&#59;0 and MacdCurrent&lt&#59;SignalCurrent and

MacdPrevious&gt&#59;SignalPrevious and MacdCurrent&gt&#59;(MACDOpenLevel*Point) and

MaCurrent&lt&#59;MaPrevious then

{

SetOrder(OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,RED)&#59; // executing

Exit&#59; // exiting

}&#59;

Exit&#59; // no new positions opened - just exit

}&#59;

4.定期控制已开立的头寸

for cnt=1 to TotalTrades

{

if OrderValue(cnt,VAL_TYPE)&lt&#59;=OP_SELL and // is it an open position?

OrderValue(cnt,VAL_SYMBOL)=Symbol then// position from "our" chart?

{

CNT是周期变量,是在程序之开端进行描述的,方式如下:

var: Cnt(0)&#59;

. 若是买入合约

If OrderValue(cnt,VAL_TYPE)=OP_BUY then // long position opened

{

. 是否需平仓? 平仓的条件信号:

MACD指针与信号线相交,MACD指针在0轴以上,为向下趋势且与向上趋势的信号线相交.

If MacdCurrent&gt&#59;0 and MacdCurrent&lt&#59;SignalCurrent and

MacdPrevious&gt&#59;SignalPrevious and MacdCurrent&gt&#59;(MACDCloseLevel*Point) then

{

CloseOrder(OrderValue(cnt,VAL_TICKET),OrderValue(cnt,VAL_LOTS),Bid,3,Violet)&#59;

Exit&#59; // exit

}&#59;

. 是否需要重新设定移动止损点? 我们仅在持仓并已超过移动止损点数点还获利的情况下设定移动止损点, 即新的移动止损点比以前的更精确时才重设.

If TrailingStop&gt&#59;0 then // if trailing stops are used

{

If (Bid-OrderValue(cnt,VAL_OPENPRICE))&gt&#59;(Point*TrailingStop) then

{

If OrderValue(cnt,VAL_STOPLOSS)&lt&#59;(Bid-Point*TrailingStop) then

{

ModifyOrder(OrderValue(cnt,VAL_TICKET),OrderValue(cnt,VAL_OPENPRICE),

Bid-Point*TrailingStop,OrderValue(cnt,VAL_TAKEPROFIT),Red)&#59;

Exit&#59;

}&#59;

}&#59;

. 若是空头合约

else // otherwise it is a short position

{

. 是否需平仓? 平仓的条件信号:

MACD指针与信号线相交,MACD指针在0轴以下,为向上趋势且与向下趋势的信号线相交.

If MacdCurrent&lt&#59;0 and MacdCurrent&gt&#59;SignalCurrent and

MacdPrevious&lt&#59;SignalPrevious and Abs(MacdCurrent)&gt&#59;(MACDCloseLevel*Point) then

{

CloseOrder(OrderValue(cnt,VAL_TICKET),OrderValue(cnt,VAL_LOTS),Ask,3,Violet)&#59;

Exit&#59; // exit

}&#59;



.是否需要重新设定移动止损点? 我们仅在持仓并已超过移动止损点数点还获利的情况下设定移动止损点, 即新的移动止损点比以前的更精确时才重设.

If TrailingStop&gt&#59;0 then// the user has put a trailing stop in his settings

{// so, we set out to check it

If (OrderValue(cnt,VAL_OPENPRICE)-Ask)&gt&#59;(Point*TrailingStop) then

{

If OrderValue(cnt,VAL_STOPLOSS)=0 or

OrderValue(cnt,VAL_STOPLOSS)&gt&#59;(Ask+Point*TrailingStop) then

{

ModifyOrder(OrderValue(cnt,VAL_TICKET),OrderValue(cnt,VAL_OPENPRICE),

Ask+Point*TrailingStop,OrderValue(cnt,VAL_TAKEPROFIT),Red)&#59;

Exit&#59;

}&#59;

}&#59;

}&#59;

// end. Closing all the curly bracket which remain open.

}&#59;

}&#59;

}&#59;

这样,跟着这套初学渐进程序,我们就学会了编写自己的人工智能系统

第三步:将所有程序代码集合起来

我们将前面所有的代码集合过来

defines: MACDOpenLevel(3),MACDCloseLevel(2)&#59;

defines: MATrendPeriod(56)&#59;

var: MacdCurrent(0),MacdPrevious(0),SignalCurrent(0),SignalPrevious(0)&#59;

var: MaCurrent(0),MaPrevious(0)&#59;

var: cnt(0)&#59;

// initial data checks

// it is important to make sure that the Expert Advisor runs on a normal chart and that

// the user has correctly set the external variables (Lots, StopLoss,

// TakeProfit, TrailingStop)

// in our case we only check the TakeProfit

If Bars&lt&#59;200 or TakeProfit&lt&#59;10 then Exit&#59;// less than 200 bars on the chart

// to simplify and speed up the procedure, we store the necessary

// indicator data in temporary variables

MacdCurrent=iMACD(12,26,9,0,MODE_MAIN)&#59;

MacdPrevious=iMACD(12,26,9,1,MODE_MAIN)&#59;

SignalCurrent=iMACD(12,26,9,0,MODE_SIGNAL)&#59;

SignalPrevious=iMACD(12,26,9,1,MODE_SIGNAL)&#59;

MaCurrent=iMA(MATrendPeriod,MODE_EMA,0)&#59;

MaPrevious=iMA(MATrendPeriod,MODE_EMA,1)&#59;

// now we have to check the status of the trading terminal.

// we are going to see whether there are any previously opened positions or orders.

If TotalTrades&lt&#59;1 then

{// there are no opened orders

// just to be on the safe side, we make sure we have free funds on our account.

// the "1000" value is taken just as an example, usually it is possible to open 1 lot

If FreeMargin&lt&#59;1000 then Exit&#59;// no money - we exit

// checking for the possibility to take a long position (BUY)

If MacdCurrent&lt&#59;0 and MacdCurrent&gt&#59;SignalCurrent and

MacdPrevious&lt&#59;SignalPrevious and Abs(MacdCurrent)&gt&#59;(MACDOpenLevel*Point) and

MaCurrent&gt&#59;MaPrevious then

{

SetOrder(OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,RED)&#59; // executing

Exit&#59; // exiting, since after the execution of a trade

// there is a 10-second trading timeout

}&#59;

// checking for the possibility of taking a short position (SELL)

If MacdCurrent&gt&#59;0 and MacdCurrent&lt&#59;SignalCurrent and

MacdPrevious&gt&#59;SignalPrevious and MacdCurrent&gt&#59;(MACDOpenLevel*Point) and

MaCurrent&lt&#59;MaPrevious then

{

SetOrder(OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,RED)&#59; // executing

Exit&#59; // exiting

}&#59;

// here we completed the check for the possibility of opening new positions.

// no new positions were opened and we simply exit the programme using the Exit command, as

// there is nothing to analyze

Exit&#59;

}&#59;

// we come over to an important part of the Expert Advisor - the control of open positions

// &#39;it is important to enter the market correctly, but it is even more important to exit it...&#39;

for cnt=1 to TotalTrades

{

if OrderValue(cnt,VAL_TYPE)&lt&#59;=OP_SELL and // is this an open position? OP_BUY or OP_SELL

OrderValue(cnt,VAL_SYMBOL)=Symbol then// does the instrument match?

{

If OrderValue(cnt,VAL_TYPE)=OP_BUY then // long position opened

{

// we check - maybe, it&#39;s already time to close it?

If MacdCurrent&gt&#59;0 and MacdCurrent&lt&#59;SignalCurrent and

MacdPrevious&gt&#59;SignalPrevious and MacdCurrent&gt&#59;(MACDCloseLevel*Point) then

{

CloseOrder(OrderValue(cnt,VAL_TICKET),OrderValue(cnt,VAL_LOTS),Bid,3,Violet)&#59;

Exit&#59; // exiting

}&#59;

// we check - maybe, we already may or it&#39;s already time to set a trailing stop?

If TrailingStop&gt&#59;0 then// the user has put a trailing stop in his settings

{ // so, we set out to check it

If (Bid-OrderValue(cnt,VAL_OPENPRICE))&gt&#59;(Point*TrailingStop) then

{

If OrderValue(cnt,VAL_STOPLOSS)&lt&#59;(Bid-Point*TrailingStop) then

{

ModifyOrder(OrderValue(cnt,VAL_TICKET),OrderValue(cnt,VAL_OPENPRICE),

Bid-Point*TrailingStop,OrderValue(cnt,VAL_TAKEPROFIT),Red)&#59;

Exit&#59;

}&#59;

}&#59;

}&#59;

}

else // otherwise it is a long position

{

// we check - maybe, it&#39;s already time to close it?

If MacdCurrent&lt&#59;0 and MacdCurrent&gt&#59;SignalCurrent and

MacdPrevious&lt&#59;SignalPrevious and Abs(MacdCurrent)&gt&#59;(MACDCloseLevel*Point) then

{

CloseOrder(OrderValue(cnt,VAL_TICKET),OrderValue(cnt,VAL_LOTS),Ask,3,Violet)&#59;

Exit&#59; // exiting

}&#59;

// we check - maybe, we already may or it&#39;s already time to set a trailing stop?

If TrailingStop&gt&#59;0 then// the user has put a trailing stop in his settings

{// so, we set out to check it

If (OrderValue(cnt,VAL_OPENPRICE)-Ask)&gt&#59;(Point*TrailingStop) then

{

If OrderValue(cnt,VAL_STOPLOSS)=0 or

OrderValue(cnt,VAL_STOPLOSS)&gt&#59;(Ask+Point*TrailingStop) then

{

ModifyOrder(OrderValue(cnt,VAL_TICKET),OrderValue(cnt,VAL_OPENPRICE),

Ask+Point*TrailingStop,OrderValue(cnt,VAL_TAKEPROFIT),Red)&#59;

Exit&#59;

}&#59;

}&#59;

}&#59;

}&#59;

}&#59;

}&#59;

// the end.

现在,我们只需给以下外部变量赋值就可以完成安装人工智能系统的全过程.

LOTS=1,STOP LOSS(S/L)=0, TAKE PROFIT(T/P)=120(适用于一个小时的间

隔),TRAILING STOP(T/S)=30,当然你自己可以设定这些值.

按VERIFY按纽,若无别的错误就按SAVE按纽.
现在,我们来编辑人工智能系统.在MQL编辑器点击顶端的VERIFY图示(像一张有检查标记的纸.)。

评分

1

查看全部评分

让快乐永远保鲜!
 楼主| 发表于 2005-9-12 03:08 | 显示全部楼层
第四步: 检测人工智能系统的历史资料
我们已将人工智能系统编写完毕,现在我们已迫不及待地想用历史数据来对系统进行测试。让我们以EUR/USD的15分钟的间隔(大约4000棍)。为例,

打开交易平台中的EUR/USD15分钟图,用ATTACH TO A CHART命令将人工智能系统的MACD指针样本图粘贴在图表上(在导航窗口用鼠标选择MACD样本线, 点击鼠标右键选择所弹出菜单的命令). 然后,到系统设置里,在这我们可以改变预设变量和用户自定义变量,如LOTS、STOP LOSS、PROFIT、TRAILING STOP等. 为了让人工智能系统不仅只起到建议的作用,还能在营业账户上自动进行实时操作, 你需要击活ALLOWLIVETRADING按纽.现在,我们来进行历史数据的测试了,我们不改变设置,转接到STRATEGY TESTER标签,击活ALLOWS ON THE CHART标记(要能在图表上看到箭头),然后,按START按纽开始测试.

人工智能系统所有代码在Ready Expert Advisors页都可找到。

当你对人工智能系统作出修改时必须紧记:

修改和测试交易平台的人工智能系统时必须注意以下细节:

.在建立仓位之前,你必须检查你账户上可用保证金的有效性。假如可用保证金不足时,则开仓请求将会失败。必须注意的是,为了达到测试的目的,可用保证金最少应在1000元,另一张单的测试价格也是1000元。

If FreeMargin &lt&#59; 1000 Then Exit&#59; // no funds - exit

当开仓、平仓或者修改已有部位或删除预先设定的部位(即是执行以下任何的操作:SetOrder, CloseOrder, ModifyOrder or DeleteOrder)时,建议使用人工智能系统的Exit语句来完成这部分的操作,这将有10秒钟的限定时间间隔来执行该项操作。注意,10秒钟的限定时间不适用于测试模式(你可以在一行中做几次交易),另外,假如不是用Exit语句来完成人工智能系统的上述交易操作,人工智能系统的测试结果将和真实交易不同。

SetOrder(OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,RED)&#59; // executing

Exit&#59;// exiting

为了防止在测试模式中用少于10秒的间隔来执行几项交易,你只需确保从上一次交易到下一次的交易已经超过10秒。

//making sure that the current time value is greater than 10 seconds since the execution of the last trade

If CurTime &gt&#59; LastTradeTime + 10 Then Begin

SetOrder(OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,RED)&#59;// executing

Exit&#59;

End&#59;

.历史数据可以用已引索的预设变量OPEN、 CLOSE、 HIGH、 LOW、 VOLUME来存取.在这种情况下,指数是指必须回头计算的周期数. // if the Close on the last bar is less than the Close on the last but one bar

If Close[1] &lt&#59; Close[2] Then Exit&#59;

. 交易平台中的人工智能系统支持4种模式:

· OHLC 点(Open/High/Low/Close)模式. 因为系统在此的测试目的仅限于开盘价、收盘价、最高价、最低价,所以这个测试系统是相当快的.但是,这个测试结果与系统执行的实际交易的结果可能会不一样.

. 每3点一次模式:在测试人工智能系统时,每增加三点就会加一个烛台图示

.每2点一次模式: 在测试人工智能系统时,每增加二点就会加一个烛台图示

. 每1点一次模式: 在测试人工智能系统时,每增加一点就会加一个烛台图,这种模式是最慢的但是测试结果是最接近人工智能系统的实际交易的结果的。(每10秒钟间隔的交易观察结果)

. 在编写和检验一人工智能系统时,同测试其它的程序一样,有时, 需要输出一些额外的调试信息。MQL II语言就支持这类信息的输出

.ALERT函数将包含由用户自定义数据的对话框发送至屏幕

Alert("Free margin is ", FreeMargin)&#59;

.COMMENT函数将由用户定义的数据放置在图表的左上角。“&#92;N” 字符用于结束一行。

Comment("Free margin is ", FreeMargin, "&#92;nEquity is ", Equity)&#59;

.PRINT函数是将由用户定义的数据输至系统记录

Print("Total trades are ", TotalTrades, "&#59; Equity is ", Equity, "&#59; Credit is ", Credit)&#59;

.PrintTrade函数是将指定的未平仓合约的具体细节从系统记录中输出

PrintTrade(1)&#59;

.当 人工智能系统 已测试完毕,测试结果会以扩展名为.LOG的文件形式储藏在你安装該交易平台所在的目录的下级子目录中。如果你经常性地测试你的人工智能系统,别忘了定期删除log档,这种档往往会占用数兆字节。
让快乐永远保鲜!
 楼主| 发表于 2005-9-12 03:10 | 显示全部楼层
机械式交易系统介绍

    机械式交易系统是近十年来新兴的交易方法,利用技术分析工具、基本分析模型、商品价格波动特性及周期或是其它逻辑思考讯息等,作为研判趋势变动方向以及进出场的依据,以排除人为或主观的判断模式,避免暴露人性的四大心理错误:『做对不敢看』、『看对不敢做』、『看错一直做』以及『做错一直看』。

    机械式交易的买卖决策完全决定于机械化、制度化的逻辑判断规则,透过计算机的辅助,可以将各种讯息转化为程序语言,籍由计算机来代替人为发出买卖讯号,再根据系统发出的委托方式,执行下单程序,此种方法已广为国外专业经理人所采用,尤其是在外汇市场,机械式交易已是专业操盘人达成稳定获利目标最基本的方法。   

    大部份的投机者都会认同实际交易会比纸上或模拟交易更为困难,即使两者都采用相同的决策模型,但往往实际交易的过程会受到『情绪』干扰的负面影响,例如过度交易、坚持亏损部位等等,都是因为人性包含了恐惧、贪婪、迟疑及赌性等缺点,而机械式交易的最大功能就是用机器取代人性,消除交易中的情绪因子,简单来说,就是克服人性的四大心理障碍。

    在交易的过程中,除了心理面的问题外,如何严守既定的操作纪律或是交易的基本原则,会直接影响到绩效表现的好坏,因此透过计算机将既定的操作规范,获利以及风险管理等条件写成程序语言,计算机依程序发出进出场买卖的警讯,如此便可以确保交易方法的一致性。
让快乐永远保鲜!
 楼主| 发表于 2005-9-12 03:38 | 显示全部楼层

MQL II语言

MQL II语言中文

mt_htm.rar

183.79 KB, 下载次数: 50

让快乐永远保鲜!

本版积分规则

QQ|手机版 Mobile Version|Archiver|关于我们 About Us|联系我们 Contact Us|Y2外汇论坛 外兔财经

GMT+8, 2025-8-21 23:49 , Processed in 0.038730 second(s), 30 queries .

Powered by Discuz! X7.2

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表