已完成棒线与实时棒线

From FxCodeBaseWiki
Jump to: navigation, search

问题

您可能已注意到,应用于特定时间框架的大多数信号仅在棒线完全完成时才会检查市场条件并且显示提示。例如,当信号应用于 1 小时棒型图时,在 15:00 的棒线上出现的信号将显示在 16:00 的棒线上,换言之,该信号只会在 15:00 的棒线刚刚完成时显示。这看起来非常不方便,因为看上去就像信号有一小时的延迟。

是这样吗?或许是的。但让我们试试在实时棒线上检查市场条件时,实际会发生什么情况,每当出现新价格(亦称为 tick,即分笔成交点)时,实时棒线就会发生永久性的变化。让我们创建一个简单的指标,如果某根棒线的收盘价高于前一根棒线的收盘价,该指标就会在这根棒线上显示一个箭头。

function Init()
    indicator:name("Show bars where current close is higher than previous");
    indicator:description("");
    indicator:requiredSource(core.Bar);
    indicator:type(core.Indicator);

    indicator.parameters:addColor("clr", "Label Color", "", core.COLOR_LABEL);
end

local source;
local mark;
local first;

function Prepare()
    source = instance.source;
    local name = profile:id();
    instance:name(name);
    first = source:first() + 1;
    mark = instance:createTextOutput ("Mark", "Mark", "Wingdings", 10, core.H_Center, core.V_Top, instance.parameters.clr, 0);
end

function Update(period, mode)
    if period > first then
        if source.close[period] > source.close[period - 1] then
            mark:set(period, source.close[period], "\226");
        end
    end
end

然后在图表上应用指标。建议选择 1 分钟时间框架,因为这样只需几分钟您就可以得到我想展示给您的结果。刚刚应用指标时,看起来一切正常。收盘价更高的棒线具有箭头标记。但现在观察指标几分钟。哦...出现了奇怪的错误信号。为什么?要弄清真相很容易。每当出现新价格时就会调用指标。在调用的那一刻,最新价格显示为最后一根棒线的收盘价...因此,即使棒线在最后与市场条件不匹配,在时间框架的中间部分,棒线还是有可能匹配市场条件。请看抓图:

CloseandlivebarZH.png

(单击查看实际大小的图像)

将指标应用于实时数据后,您会立即看到错误信号。

那么,让我们来看看。将指标应用于历史数据时,仅调用一次更新函数,并得到棒线的最终版本。但是对于实时棒线,在棒线期间,出现几个分笔成交点,就会调用几次指标。如果想象一下在棒线未完成时棒线将是什么样的(请看上面截图中最后一个错误信号)— 您将看到有时棒线的确是与信号条件匹配的。

因此,简言之,对几乎所有信号和许多指标而言,指标会产生“噪点”— 换言之,会产生错误信号,即使该信号并未出现在棒线的最终版本中。这种噪点信号可能会使您做出错误的市场决策,因此,最好的做法就是等待棒线完成,这样您可以稳妥地检查在棒线期间是否符合信号条件。

如何处理这种情况?

在指标中

最好的方法是仅在棒线完成时处理棒线。要检查棒线是否已完成,可以检查棒线的“序号”,对于具有实时历史数据的最后一根棒线,仅在该棒线完全完成时才处理该棒线(此时该棒线不再是最后一根棒线)。让我们以这种方式更改上面的指标。

function Init()
    indicator:name("Show bars where current close is higher than previous");
    indicator:description("");
    indicator:requiredSource(core.Bar);
    indicator:type(core.Indicator);

    indicator.parameters:addColor("clr", "Label Color", "", core.COLOR_LABEL);
end

local source;
local mark;
local first;

function Prepare()
    source = instance.source;
    local name = profile:id();
    instance:name(name);
    first = source:first() + 1;
    mark = instance:createTextOutput ("Mark", "Mark", "Wingdings", 10, core.H_Center, core.V_Top, instance.parameters.clr, 0);
end

local serial = nil;

function Update(period, mode)
    if period > first then
        if source:isAlive() and                                 -- 如果我们应用的历史数据是实时的
           period == source:size() - 1 then                     -- 且这是最后一个值(实时棒线)
           if serial ~= source:serial(source:size() - 1) then   -- 且最后一根棒线是一根新的棒线
                serial = source:serial(source:size() - 1);      -- 记住最后一根新棒线
                period = period - 1;                            -- 并强制处理前一根棒线(例如最近
                                                                -- 完成的棒线)
            else
                return;                                         -- 棒线未变化,因此
                                                                -- 不要处理它
            end
        end
        if source.close[period] > source.close[period - 1] then
            mark:set(period, source.close[period], "\226");
        end
    end
end


瞧,不再有噪点了!

在策略中

如果使用 helper.lua 进行订阅管理,您无需执行任何操作。仅当棒线完全完成时,帮助程序才会通知您。

如果您自己实现订阅,操作方式与指标的操作方式相同 — 不要检查市场条件,直到订阅的时间框架内出现新的棒线。


Language: English  • español • français • русский • 中文 • 中文(繁體)‎