展会信息港展会大全

关于MT软件的人工智能实践小小篇
来源:互联网   发布日期:2011-09-07 13:31:34   浏览:6356次  

导读:文前说明 :本资料选自某交易平台的说明 .请勿授权而转载. 初步创建一简易的人工智能系统让我们将MACD指标同获利能力、支撑移动止损位以及操作安全等因素结合起来...

我也来说两句 查看全部回复 最新回复

  • 逆风飞扬 (2004-1-05 02:11:00)

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


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



    [此帖子已被 老正 在 2004-8-12 19:45:22 编辑过]

  • 逆风飞扬 (2004-1-05 02:12:00)



    [此帖子已被 老正 在 2004-8-12 19:48:20 编辑过]

  • 逆风飞扬 (2004-1-05 02:13:00)

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

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

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

    1.初始资料检查

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

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

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

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

    .检查账户中的可用资金

    .是否可以做多(买入)

    .建仓买入和平仓

    .是否可以做空(卖出)

    .建仓卖出和平仓

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

    ..若是多头合约

    .是否要平仓

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

    ..若是空头合约

    .是否要平仓

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

    这是相对简单的样板,仅仅只有4个主要单元.

  • 逆风飞扬 (2004-1-05 02:13:00)

    现在我们来试着逐渐将结构表中的每一部分的代码做出来:

    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;

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

  • 逆风飞扬 (2004-1-05 02:15:00)

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

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

    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按纽.

  • 逆风飞扬 (2004-1-05 02:18:00)

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



    [此帖子已被 老正 在 2004-8-12 19:50:40 编辑过]

  • 逆风飞扬 (2004-1-05 02:20:00)

    第四步: 检测人工智能系统的历史资料


    [此帖子已被 老正 在 2004-8-12 19:51:25 编辑过]

  • 逆风飞扬 (2004-1-05 02:21:00)

    我们已将人工智能系统编写完毕,现在我们已迫不及待地想用历史数据来对系统进行测试。让我们以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按纽开始测试.



    [此帖子已被 老正 在 2004-8-12 19:52:33 编辑过]

  • 逆风飞扬 (2004-1-05 02:23:00)

    人工智能系统所有代码在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档,这种档往往会占用数兆字节。
    [ 此消息由 逆风飞扬 在 2004-01-05.11:23:45 编辑过 ]

  • 老正 (2004-1-05 09:37:00)

    ..............
    【动作】老正来回地踱着方步,陷入沉思中。

  • fforex (2004-1-07 10:04:00)

    Summary::::::::::-170
    损失惨重呀!!!
    【动作】fforex惊叫一声,吓了别人一大跳!【动作】fforex大叫:狼来了!狼来了!救命啊!救命啊!

  • 逆风飞扬 (2004-1-07 16:39:00)

    <IMG border=0 SRC=http://talkforex.com/image/tounge_smile.gif>那有一口吃個大胖子的呀【动作】逆风飞扬惊讶得两眼瞪得贼大!

  • chenxing (2004-2-21 20:20:00)

    ding

  • newboy (2004-2-25 02:19:00)

    非常感谢!

  • 金融小小子 (2004-3-26 15:39:00)

    呵呵~~

  • 汉唐投资 (2004-4-01 13:27:00)

    好文章。【动作】汉唐投资抱拳团团一拜,讨好地说道:敝人对各位的景仰之情,有如涛涛江水连绵不绝。

  • ycai (2004-4-02 15:02:00)

    我将软件自带的一个例子macd sample 通过attach to a chart加载到图中,但是在图上没有什么反映啊?是不是到了一个进场点会出现一个符号呢?盼楼主帮助<A HREF=http://talkforex.com/image/2.gif TARGET=_blank><IMG border=0 title=开新窗口浏览 SRC=http://talkforex.com/image/2.gif onload="javascript:if(this.width>screen.width-366)this.width=screen.width-366"></A>

  • luvalo (2004-4-02 17:09:00)
  • zd (2004-4-10 15:13:00)
  • ncyyu (2004-4-11 12:54:00)
  • 赞助本站

    人工智能实验室
    AiLab云推荐
    展开

    热门栏目HotCates

    Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港