2

有没有人有任何示例代码来说明如何在 Metatrader 4 中生成基于时间的入场信号?例如在每天的特定小时和分钟

4

2 回答 2

1

TimeLocal()为您提供自 1970 年 1 月 1 日午夜以来本地计算机(终端客户端)的秒数。

TimeCurrent()为您提供自 1970 年 1 月 1 日午夜以来您的经纪人计算机(服务器)的秒数。

您可以将其中任何一个转换为字符串,如下所示:

string ct = TimeToStr(TimeLocal(),TIME_DATE|TIME_SECONDS);
Print("Client Time: ", ct);

string st = TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS);
Print("Server Time: ", st);

您还可以像这样获取日期编号的各个元素:

Print("Year:",TimeYear(TimeCurrent())," Month:",TimeMonth(TimeCurrent()));

MQL4 在线程序员指南可在此处找到。

希望这可以帮助。

干杯,

标记

于 2011-02-24T09:35:59.037 回答
1

我一直在研究这样的功能。它必须进行大量定制——我正在制作一个更通用的版本,它会变得如此庞大和乏味,以至于需要自己的include文件。但是能够在不更改代码的情况下将其粘贴在不同的图表上是值得的。这基本上是一个大案例陈述,我在这里已经针对每个单独的时间框架进行了定制。

你一直在做什么?感谢您对时间和外汇的关注!

bool existordertime( datetime time, int otype = -37 ) {
    // +---------------------------------------------------------------+
    // | this function is intended for use inside of if() and other conditionals
    // | usually called with TimeCurrent() example:
    // | 
    // | if ( !existordertime( TimeCurrent() ) )
    // | 
    // | it accepts a datetime. A datetime is:
    // | a number of seconds elapsed from 00:00 January 1, 1970
    // | they can be treated as integers as such or accessed with other functions
    // | so that if statements can be commented in and out easily based on what 
    // | timeframe we plan on looking at.
    // | there is an optional parameter for an order type if you need it.
    // | 
    // | KEEP IN MIND if you want to use this to trade something like a 5min 15min
    // | or 4hr your gonna need a lot of if statements like:
    // | 
    // | if (  MathMod( Minute() + 5, 5 ) == 0 )
    // | 
    // +------------------------------------------------------------------+
    for (int cnt = 0; cnt < OrdersTotal(); cnt++) {

        OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

        if (OrderType() == otype || -37 == otype)

        // COMMENT OUT THE if()S YOU DON'T NEED HERE: 
        //                       also add new lines as needed
        // if (  MathMod( TimeMinute( time ) +  5,  5 ) == 0 )   //  5min chart
        // if (  MathMod( TimeMinute( time ) + 15, 15 ) == 0 )   // 15min chart
        // if (  MathMod( TimeMinute( time ) + 30, 30 ) == 0 )   // 30min chart
        // if (  MathMod( TimeHour(   time ) +  4,  4 ) == 0 )   // 4hour chart

        int dbOrderOpenTime = OrderOpenTime();                   // re-use SAVEs dbPOOL-access time ...

        if (                      TimeSeconds( time ) == TimeSeconds( dbOrderOpenTime ) )
            if (                  TimeMinute(  time ) == TimeMinute(  dbOrderOpenTime ) )
                if (              TimeHour(    time ) == TimeHour(    dbOrderOpenTime ) )
                    if (          TimeDay(     time ) == TimeDay(     dbOrderOpenTime ) )
                        if (      TimeMonth(   time ) == TimeMonth(   dbOrderOpenTime ) )
                            if (  TimeYear(    time ) == TimeYear(    dbOrderOpenTime ) )
                                return (TRUE);
    }

    for (cnt = 0; cnt < OrdersHistoryTotal(); cnt++) {

        OrderSelect(cnt, SELECT_BY_POS, MODE_HISTORY);

        if (OrderType() == otype || -37 == otype)
        // COMMENT OUT THE if()S YOU DON'T NEED HERE: 
        //                       also add new lines as needed
        // if (  MathMod( TimeMinute( time ) +  5,  5 ) == 0 )   //  5min chart
        // if (  MathMod( TimeMinute( time ) + 15, 15 ) == 0 )   // 15min chart
        // if (  MathMod( TimeMinute( time ) + 30, 30 ) == 0 )   // 30min chart
        // if (  MathMod( TimeHour(   time ) +  4,  4 ) == 0 )   // 4hour chart

        int dbOrderOpenTime = OrderOpenTime();                   // re-use SAVEs dbPOOL-access time ...

        if (                      TimeSeconds( time ) == TimeSeconds( dbOrderOpenTime ) )
            if (                  TimeMinute(  time ) == TimeMinute(  dbOrderOpenTime ) )
                if (              TimeHour(    time ) == TimeHour(    dbOrderOpenTime ) )
                    if (          TimeDay(     time ) == TimeDay(     dbOrderOpenTime ) )
                        if (      TimeMonth(   time ) == TimeMonth(   dbOrderOpenTime ) )
                            if (  TimeYear(    time ) == TimeYear(    dbOrderOpenTime ) )
                                return (TRUE);
    }

    return (FALSE);
}
于 2011-12-09T03:50:09.970 回答