Let's develop together: Yesterday's Close Value

Moderator: admin

Let's develop together: Yesterday's Close Value

Postby Nikolay.Gekht » Thu Aug 19, 2010 1:24 pm

Update Sep, 15 The small bug in the code below fixed. The date of the oldest available bar must be obtained as source:date(source:first()). The wrongful line used source[source:first()]. This mistake didn't prevent indicator from working, but forced indicator to read all the available data. I apologize for the inconvenience.

Note: This article uses the latest API version, so the indicator developed will work only in the August 27 2010 release version. Please read notes at the end of the article to know how to make the indicator working in the current production release. I will remove this note as soon as the current beta version comes to the production.

Note: This point is, probably, the most complex in the indicator development. So, if you can handle this, there is nothing you cannot do.

Sometimes an indicator requires data of a bigger time frame, for example, we could use for close of the previous day for calculations. However, it’s possible to try and find this data in the price history the indicator is applied to, but… Just imagine. To get yesterday’s high/low, the whole yesterday’s data must be loaded. If the current history is a minute history, in the worst case we have to have at least 2880 bars to be able find that data (1440 minute bars for today and then 1440 minute bars for the whole yesterday). This is a huge amount to load and then to calculate. But the system has access to the day data, so why don’t we use it? Let’s do it.

However, there are a number of points we should handle carefully. Ok. Let’s develop an indicator which shows yesterday’s close price on the chart and discuss each point in detail.

Point 1. What is “yesterday”?

The handling of the date in indicators is pretty simple. A date value is a number, which is just a number of days past midnight of Dec, 30 1899. The format is also known as Windows Ole Automation date format. The integer part of this number is a number of days and the fractional part is a port of the day. So, to find a calendar midnight date and time, we must just floor the value (cut away the fractional part). To shift the date to N days ago, we must just subtract N from the date. So, if FXCM used the “calendar clock” for the trading days (i.e. the trading day starts at midnight), the finding of yesterday for bar x of the source would be pretty simple: math.floor(source:date(x)) - 1.

Unfortunately, FXCM’s trading day does not match the calendar trading day. Currently, the trading day starts at 17:00EST yesterday and lasts until 17:00EST today. And for different configurations this date can differ. Hopefully, the indicator core helps us to handle calculation of the candle borders for a particular date. There is a core.getcandle function which returns the date and time of the candle of the specified time frame to which the specified date and time belong. As you can see, this function requires two parameters which let us “tune” this function for a certain configuration: an offset of the trading day and an offset of the trading week against calendar. You must use host:execute("getTradingDayOffset") and
host:execute("getTradingWeekOffset") host’s calls to get the settings of the trading day and trading week for particular host application and connection.

So, the function to get yesterday’s date properly can look as follows:
Code: Select all
function getYesterday(date)
    local offset;
    local weekoffset;

    offset = core.host:execute("getTradingDayOffset");
    weekoffset = core.host:execute("getTradingWeekOffset");

    local today, yesterday;
    today = core.getcandle("D1", date, offset, weekoffset);
    yesterday = today - 1;
    return yesterday;
end


Good? Not yet. There is another point we should handle. There are “non-trading” periods when neither trading is possible nor prices are received. So, in fact, during a week only Monday trough Friday candles exist. There are no “Saturday” candles at all. So, when our minute belongs to a Monday day candle, the function will return us a Sunday candle which just does not exist! So, for Monday’s data we must take previous Friday’s close price.

To handle that correctly, there is another useful function: core.isnontrading which checks whether the date belongs to a non-trading period (Friday 17:00EST-Sunday 17:00EST) and returns the date and time when this non-trading period is finished. So, to find the proper “yesterday” candle we have to check whether the calendar yesterday falls into non-trading period and shift the “yesterday” date into past to skip this non-trading period.

Let’s modify our function a bit:
Code: Select all
function getYesterday(date)
    local offset;
    local weekoffset;

    offset = core.host:execute("getTradingDayOffset");
    weekoffset = core.host:execute("getTradingWeekOffset");

    local today, yesterday;
    today = core.getcandle("D1", date, offset, weekoffset);
    yesterday = today - 1;

    local nontrading, nontradingend;
    nontrading, nontradingend = core.isnontrading(yesterday, offset);
    if nontrading then
        -- if the calendar yesterday date is non-trading (Friday 17:00 EST - Sunday 17:00 EST)
        -- shift three days back against the non-trading period end (Sunday 17:00 EST).
        yesterday = nontradingend - 3;
    end

    return yesterday;
end


So, now, having the getYesterday() function, we can easily find how many days we need. The oldest day is getYesterday(source:date(source:first())), i.e. the “yesterday” bar which corresponds to the oldest available bar. And what about the newest one? There are two cases. The source collection could be either a historical (i.e. a collection loaded until the specified date, which will be never changed for the chart window) or “alive” (i.e. loaded till “now” and continuously growing as new bars come). According to these two cases, the “to” date could be either a “yesterday” day of the latest available bar (getYesterday(source:date(source:size() - 1)) or … now as well. To specify “now”, we use 0 value.

Point 2. How to get the day price data?

Ok, now we know which data we need. The next step is to get this data. Marketscope provides a host interface which lets you execute an application-specific functions. One of these functions is “getHistory”. By this call Marketscope loads the specified instrument in the specified time frame and in the specified time range. See host:execute(“getHistory”, …) for details.

Code: Select all
local days;     -- a variable to keep days

-- the function loads the proper day history
function loadData()
    if source:size() < source:first() then
        return ;
    end

    local from, to;
    -- load the data from the date of proper yesterday for
    -- the first available bar of the source
    from = getYesterday(source:date(source:first()));
    -- load to "now" is the source is up to "now" or
    -- to the day which corresponds to the latest
    -- day of the collection.
    if source:isAlive() then
        to = 0;
    else
        to = getYesterday(source:date(source:size() – 1));
    end
    days = host:execute("getHistory", 1, source:instrument(), "D1", from, to, source:isBid());
end


It is important that after the “getHistory” call, Marketscope only starts to load the data. It takes some time, but Marketscope does not wait until the data is completely loaded to avoid hanging of the indicator. When data loading is finished, Marketscope will notify the indicator by calling the AsyncOperationFinished function. Moreover, Marketscope does not care about the indicator status and will continue calling indicator to be updated.

So, there are two points to do.

First, we must protect the indicator from updating while the data is loaded.

To protect the indicator from being recalculated, we can just return from the update function while data is being loaded. Just add the “loading” global variable so that all functions could “know” that data is being loaded.

Code: Select all
local days;         -- day source data
local loading;      -- the loading day data flag

function Prepare()
    ...
    days = nil;
    loading = false;
    ...
end

function Update(period, mode)
    if loading then
        -- do nothing if data is being loaded
        return ;
    end

    if days == nil then
        loadData();
        return ;
    end
    ...
end       

function AsyncOperationFinished(cookie, success, error)
    if cookie == 1 then
        assert(success, error);
        loading = false;
    end
    return 0;
end

function loadData()
    if loading then
        return ;
    end

    local from, to;

    -- load the data from the date of proper yesterday for
    -- the first available bar of the source
    from = getYesterday(source:date(source:first()));
    -- load to "now" is the source is up to "now" or
    -- to the day which corresponds to the latest
    -- day of the collection.
    if source:isAlive() then
        to = 0;
    else
        to = getYesterday(source:date[source:size() - 1]);
    end
    loading = true;
    days = host:execute("getHistory", 1, source:instrument(), "D1", from, to, source:isBid());
end


Well, now we need to force the indicator update when data is loaded. There is function instance:updateFrom() which forces the indicator to be recalculated from the specified period. Also, when AsyncOperationFinished returns the core.ASYNC_REDRAW value, Marketscope redraws the indicator in the chart window.

So, since we haven’t calculated anything yet, we have to update our indicator right from the oldest data. Just fix AsyncOperationFinished a bit.

Code: Select all
function AsyncOperationFinished(cookie, success, error)
    if cookie == 1 then
        assert(success, error);
        loading = false;
        instance:updateFrom(source:first());
        return core.ASYNC_REDRAW;
    end
    return 0;
end


Almost finished. The last question is how to get the data. We can calculate the day date. Of course, we can just loop through the day history and find the date, but… It’s a bit long, isn’t it? Please, take into consideraion this article, there is a good recipe there.

So, we can build the first version of our indicator. Let’s do so and try it.
Code: Select all
function Init()
    indicator:name("Show Yesterday Close");
    indicator:description("The indicator shows yesterday's close value on the chart");
    indicator:requiredSource(core.Tick);
    indicator:type(core.Indicator);

    indicator.parameters:addColor("clr", "Indicator Line Color", "", core.rgb(255, 255, 0));
end

local source;       -- indicator source data
local first;        -- first available source data
local days;         -- day source data
local loading;      -- the loading day data flag
local loadedFrom;   -- the date from which the days was loaded
local SYC;          -- yesterday close output

local host;
local offset;
local weekoffset;


function Prepare()
    local name;

    name = profile:id() .. "(" .. instance.source:name() .. ")";
    instance:name(name);

    source = instance.source;
    first = source:first();
    loading = false;
    days = nil;

    host = core.host;
    offset = host:execute("getTradingDayOffset");
    weekoffset = host:execute("getTradingWeekOffset");

    SYC = instance:addStream("SYC", core.Line, name, "SYC", instance.parameters.clr, first);
end

function Update(period, mode)
    if period < first then
        return ;
    end
    if loading then
        return ;
    end

    -- load the data if there is no day
    if days == nil then
        SYC:setBookmark(1, source:first());
        loadData();
        return ;
    end

    -- try to find the value in the days collection
    local yesterday;
    yesterday = getYesterday(source:date(period));
    if yesterday < loadedFrom then
        SYC:setBookmark(1, period);
        loadData();
        return ;
    end

    local i;
    i = findDateFast(days, yesterday, false);
    if i >= 0 then
        SYC[period] = days.close[i];
    end
end

function AsyncOperationFinished(cookie, success, error)
    if cookie == 1 then
        assert(success, error);
        loading = false;
        local i = SYC:getBookmark(1);
        if i < source:first() then
            i = source:first();
        end
        instance:updateFrom(i);
        return core.ASYNC_REDRAW;
    end
    return 0;
end

-- the function loads the proper day history
function loadData()
    if source:size() < source:first() then
        return ;
    end

    if loading then
        return ;
    end

    local from, to;

    if days == nil then
        -- initial data loading

        -- load the data from the date of proper yesterday for
        -- the first available bar of the source
        from = getYesterday(source:date(source:first()));
        -- load to "now" is the source is up to "now" or
        -- to the day which corresponds to the latest
        -- day of the collection.
        if source:isAlive() then
            to = 0;
        else
            to = getYesterday(source:date(source:size() - 1));
        end
        loading = true;
        loadedFrom = from;
        days = host:execute("getHistory", 1, source:instrument(), "D1", from, to, source:isBid());
    else
        from = getYesterday(source:date(source:first()));
        to = days:date(0);
        loading = true;
        loadedFrom = from;
        host:execute("extendHistory", 1, days, from, to);
    end
end

-- returns the beginning of the "yesterday" date for
-- the specified data
function getYesterday(date)
    local today, yesterday;

    -- get beginning of the today's candle and round it up to the second
    today = core.getcandle("D1", date, offset, weekoffset);
    today = math.floor(today * 86400 + 0.5) / 86400;

    -- get calendar yesterday
    yesterday = today - 1;

    local nontrading, nontradingend;
    nontrading, nontradingend = core.isnontrading(yesterday, offset);
    if nontrading then
        -- if the calendar yesterday date is non-trading (Friday 17:00 EST - Sunday 17:00 EST)
        -- shift three days back against the non-trading period end (Sunday 17:00 EST).
        yesterday = nontradingend - 3;
    end
    return yesterday;
end

-- ------------------------------------------------------------------------------
-- Find the specified date in the specified stream
--
-- The function uses the binary search algorithm, so it requires only O(n) = log2(n) operations
-- to find (or to do not find) the value.
--
-- The function compares the date and time rounded to the whole seconds.
--
-- Parameters:
-- stream       The price stream to find the date in
-- date         Date and time value to be found
-- precise      The search mode
--              In case the value of the parameter is true, the function
--              Searches for the the exact date and returns not found in the
--              date is not found.
--              In case the value of the parameter is false, the function
--              returns the period with the biggest time value which is smaller
--              than the value of the date parameter.
-- Returns:
-- < 0          The value is not found
-- >= 0         The index of the the period in the stream
-- ----------------------------------------------------------------------------------
function findDateFast(stream, date, precise)
    local datesec = nil;
    local periodsec = nil;
    local min, max, mid;

    datesec = math.floor(date * 86400 + 0.5)

    min = 0;
    max = stream:size() - 1;

    while true do
        mid = math.floor((min + max) / 2);
        periodsec = math.floor(stream:date(mid) * 86400 + 0.5);
        if datesec == periodsec then
            return mid;
        elseif datesec > periodsec then
            min = mid + 1;
        else
            max = mid - 1;
        end
        if min > max then
            if precise then
                return -1;
            else
                return min - 1;
            end
        end
    end
end


Now apply the indicator to the chart.
img1.png


It works!!!! But stop, stop, stop… When we try to load more data into past it apparently stopped to work!

img2.png


Point 3. How to get the day price data?

What happened?! Ok. Remember, we loaded the data starting from yesterday for the oldest of the existing candles. When we started to scroll our chart back, more data was loaded. But who updated our collection of day bars? Of course, nobody did it. So, we have to force Marketscope to load new data when the source collection is extended into past. Good news is that there is another cool call of the host interface: host:execute(“extendHistory”) which helps us just load more data into already loaded history instead of loading the whole history again. Because the original source history can be extended only into past, it is too easy to check whether new data is loaded. When it happens, the “yesterday” value we are trying to find is earlier than the date we previously loaded the day history from.

So, do the following modification in our code:

a) Keep the “from” date we loaded the day history earlier.
b) In case the day collection already exists, extend it instead of loading older data.

So, add new loadedFrom global variable. And change the Update and loadData functions.

There is new indicator version:
Code: Select all
function Init()
    indicator:name("Show Yesterday Close");
    indicator:description("The indicator shows yesterday's close value on the chart");
    indicator:requiredSource(core.Tick);
    indicator:type(core.Indicator);

    indicator.parameters:addColor("clr", "Indicator Line Color", "", core.rgb(255, 255, 0));
end

local source;       -- indicator source data
local first;        -- first available source data
local days;         -- day source data
local loading;      -- the loading day data flag
local loadedFrom;   -- the oldest yesterday bar we already loaded
local SYC;          -- yesterday close output

local host;
local offset;
local weekoffset;

function Prepare()
    local name;

    name = profile:id() .. "(" .. instance.source:name() .. ")";
    instance:name(name);

    source = instance.source;
    first = source:first();
    loading = false;
    days = nil;

    host = core.host;
    offset = host:execute("getTradingDayOffset");
    weekoffset = host:execute("getTradingWeekOffset");
    SYC = instance:addStream("SYC", core.Line, name, "SYC", instance.parameters.clr, first);
end

function Update(period, mode)
    if period < first then
        return ;
    end
    if loading then
        return ;
    end

    -- load the data if there is no day
    if days == nil then
        loadData();
        return ;
    end

    -- try to find the value in the days collection
    local yesterday;
    yesterday = getYesterday(source:date(period));

    -- check whether such day bar is not loaded yet
    if yesterday < loadedFrom then
        loadData();
        return ;
    end

    -- find the day data
    local i;
    i = findDateFast(days, yesterday, false);
    if i >= 0 then
        SYC[period] = days.close[i];
    end
end

function AsyncOperationFinished(cookie, success, error)
    if cookie == 1 then
        assert(success, error);
        loading = false;
        instance:updateFrom(source:first());
        return core.ASYNC_REDRAW;
    end
    return 0;
end

-- the function loads the proper day history
function loadData()
    if source:size() < source:first() then
        return ;
    end

    if loading then
        return ;
    end

    local from, to;

    if days == nil then
        -- initial data loading

        -- load the data from the date of proper yesterday for
        -- the first available bar of the source
        from = getYesterday(source:date(source:first()));
        -- load to "now" is the source is up to "now" or
        -- to the day which corresponds to the latest
        -- day of the collection.
        if source:isAlive() then
            to = 0;
        else
            to = getYesterday(source:date(source:size() - 1));
        end
        loading = true;
        loadedFrom = from;
        days = host:execute("getHistory", 1, source:instrument(), "D1", from, to, source:isBid());
    else
        from = getYesterday(source:date(source:first()));
        to = days:date(0);
        loading = true;
        loadedFrom = from;
        host:execute("extendHistory", 1, days, from, to);
    end
end

-- returns the beginning of the "yesterday" date for
-- the specified data
function getYesterday(date)
    local today, yesterday;

    -- get beginning of the today's candle and round it up to the second
    today = core.getcandle("D1", date, offset, weekoffset);
    today = math.floor(today * 86400 + 0.5) / 86400;

    -- get calendar yesterday
    yesterday = today - 1;

    local nontrading, nontradingend;
    nontrading, nontradingend = core.isnontrading(yesterday, offset);
    if nontrading then
        -- if the calendar yesterday date is non-trading (Friday 17:00 EST - Sunday 17:00 EST)
        -- shift three days back against the non-trading period end (Sunday 17:00 EST).
        yesterday = nontradingend - 3;
    end
    return yesterday;
end

-- ------------------------------------------------------------------------------
-- Find the specified date in the specified stream
--
-- The function uses the binary search algorithm, so it requires only O(n) = log2(n) operations
-- to find (or to do not find) the value.
--
-- The function compares the date and time rounded to the whole seconds.
--
-- Parameters:
-- stream       The price stream to find the date in
-- date         Date and time value to be found
-- precise      The search mode
--              In case the value of the parameter is true, the function
--              Searches for the the exact date and returns not found in the
--              date is not found.
--              In case the value of the parameter is false, the function
--              returns the period with the biggest time value which is smaller
--              than the value of the date parameter.
-- Returns:
-- < 0          The value is not found
-- >= 0         The index of the the period in the stream
-- ----------------------------------------------------------------------------------
function findDateFast(stream, date, precise)
    local datesec = nil;
    local periodsec = nil;
    local min, max, mid;

    datesec = math.floor(date * 86400 + 0.5)

    min = 0;
    max = stream:size() - 1;

    while true do
        mid = math.floor((min + max) / 2);
        periodsec = math.floor(stream:date(mid) * 86400 + 0.5);
        if datesec == periodsec then
            return mid;
        elseif datesec > periodsec then
            min = mid + 1;
        else
            max = mid - 1;
        end
        if min > max then
            if precise then
                return -1;
            else
                return min - 1;
            end
        end
    end
end


Install the indicator and try it. Wow. Now, when the previous version stopped working during the scroll, the new version works anyway! Congratulation!

img3.png
Nikolay.Gekht
FXCodeBase: Site Admin
 
Posts: 1235
Joined: Wed Dec 16, 2009 6:39 pm
Location: Cary, NC

Re: Let's develop together: Yesterday's Close Value

Postby Nikolay.Gekht » Thu Aug 19, 2010 1:25 pm

Notes

Note 1. How to debug such indicators using SDK debugger?

First, you must prepare the data. You can save price data files using Marketscope. Just open some instrument in both resolutions (for example 30 minutes and 1 day) and then save both histories in the Indicore SDK data format (File->Export to Indicore…). With the new version of Indicore SDK you can also download the data automatically via SDK Debugger using the quotes manager server (Tools->Load Quotes).

When you start the indicator, choose the smaller timeframe history in the indicator parameters. Please, also check the “Predeliver Data” checkbox in the indicator parameters to let the debugger show the whole available history to the indicator, like Marketscope does. Without this option, the debugger will show the history only up to the currently simulated bar. When the indicator executes “getHistory” call, the prompt for data appears. Choose the day data previously saved from Marketscope in that prompt or use data from the quotes manager server.

Note 2. How to handle timeframes other than 1 day?

To make the example indicator simpler, I used a 1-day time frame. However, you would want to do the same for other time frame data, for example, use 1-hour close price instead of a day close price. To do that, you can:

a) Let the user choose the time frame. Add a string parameter to the indicators parameter list and then set the time frame selector flag to that parameter. As a result, in the Prepare function, the corresponding parameter will contain the code of the time frame chosen by the user.

Code: Select all
function Init()
    ...
    indicator.parameters:addString("TF", "Timeframe", "", "m1");
    indicator.parameters:setFlag("TF", core.FLAG_PERIODS);
    ...
end


b) Calculate the length of a candle of the specified time frame in day. Use the core.getcandle() function to get the start date of any candle and the start date of the next candle. The difference between these two values is the length of the candle in days. I also strongly recommend calculating all dates in seconds rather than in days to avoid rounding errors.

Code: Select all
local candleLength;  -- length of candle in seconds

function Prepare()
    ...
    offset = host:execute("getTradingDayOffset");
    weekoffset = host:execute("getTradingWeekOffset");
    ...
    local thisCandle, nextCandle;
    thisCandle, nextChandle = core.getcandle(instance.parameters.TF, core.now(), offset, weekoffset);
    candleLength = math.floor((nextCandle - thisCandle) * 86400 + 0.5);
    ...
end


c) Use previously calculated candle length instead of 1 day value in your “getPreviousBar” function. Please note that while the candle length could be different, the non-trading period is still in days. So, handle it carefully.

Code: Select all
function getPreviousBar(date)
    local curr, prev;

    -- get beginning of the today's candle and round it up to the second
    curr = core.getcandle(instance.parameters.TF, date, offset, weekoffset);
    today = math.floor(today * 86400 + 0.5);

    -- get calendar yesterday
    prev = (curr – candleLength) / 86400;

    local nontrading, nontradingend;
    nontrading, nontradingend = core.isnontrading(prev, offset);
    if nontrading then
        prev = prev - 2;
        prev = math.floor(prev * 86400 + 0.5);

        -- get calendar yesterday
        prev = (prev – candleLength) / 86400;
    end
    return prev;
end
Last edited by Anonymous on Thu Jan 12, 2012 12:43 am, edited 1 time in total.
Reason: Updated for the new version of Indicore (2.1)
Nikolay.Gekht
FXCodeBase: Site Admin
 
Posts: 1235
Joined: Wed Dec 16, 2009 6:39 pm
Location: Cary, NC

Re: Let's develop together: Yesterday's Close Value

Postby Nikolay.Gekht » Wed Sep 15, 2010 9:48 am

Updated. The source[source:fist()] line replace with source:date(source:fist()) line.
Nikolay.Gekht
FXCodeBase: Site Admin
 
Posts: 1235
Joined: Wed Dec 16, 2009 6:39 pm
Location: Cary, NC

Re: Let's develop together: Yesterday's Close Value

Postby boss_hogg » Sat Nov 24, 2012 11:03 am

Hi Nikolay,

just a few thoughts on your 1st post; I had two problems with the indicator code:

1) it would not redraw when scrolling to the past; actually, marketscope would freeze. After I got some understanding of the get/setBookmark commands, I changed i < source:first() with i > source:first() in the following code and it works:
Code: Select all
if i < source:first() then
            i = source:first();
end


2) it would not compute well for the weekends. For the second value that core.isnontrading returns, in "Yesterday's Close Value" you mention "-- shift three days back against the non-trading period END"

but in http://www.fxcodebase.com/documents/Ind ... ading.html it says:
"The second value is the date and time when the non-trading period BEGINS or nil in case the date and time does not belong to the non-trading period."

So, assuming that the non-trading day BEGINS a second after the trading day ends, I changed the following:
Code: Select all
 local nontrading, nontradingend;
    nontrading, nontradingend = core.isnontrading(yesterday, offset);
    if nontrading then
        -- if the calendar yesterday date is non-trading (Friday 17:00 EST - Sunday 17:00 EST)
        -- shift three days back against the non-trading period end (Sunday 17:00 EST).
        yesterday = nontradingend - 3;
    end

into
Code: Select all
local nonTradingHours, startOfNonTradingHours;
nonTradingHours, startOfNonTradingHours=core.isnontrading(yesterday, dayOffset);
if nonTradingHours then
    yesterday=startOfNonTradingHours-1;
-- I also tried  yesterday=startOfNonTradingHours;  and it still works
end

and it seeems to work. But I'm puzzled because it works for both yesterday=startOfNonTradingHours and yesterday=startOfNonTradingHours-1 , shouldn't it work only for the latter ?
boss_hogg
FXCodeBase: Confirmed User
 
Posts: 48
Joined: Fri Oct 26, 2012 3:03 am

Re: Let's develop together: Yesterday's Close Value

Postby Nikolay.Gekht » Mon Dec 03, 2012 2:03 pm

boss_hogg wrote:1) it would not redraw when scrolling to the past;

Did you use the latest version, not the first one? How to support redraw when scroll back properly is discussed separately.

boss_hogg wrote:2) it would not compute well for the weekends.
...
and it seeems to work. But I'm puzzled because it works for both yesterday=startOfNonTradingHours and yesterday=startOfNonTradingHours-1 , shouldn't it work only for the latter ?

Your method is good too.

The lines you mentioned works in the same manner because the call of findDateFast is executed with precise parameter equal to false. In this case the function returns either the date specified OR the previous date if date is not found.

So, for yesterday=startOfNonTradingHours it cannot find the exact date and returns the previous, which is - startOfNonTradingHours-1

Two notes:

1) This example is quite old, the newer version of the same article can be found here:
wiki

2) Since SDK 2.1 specification is introduced in the production there is much simpler way to do this job. I'll update the article to reflect these changes today.

Oh, update: 2.1 specification is not in the production yet, this release delayed because of the recent hurricane. :-(
Nikolay.Gekht
FXCodeBase: Site Admin
 
Posts: 1235
Joined: Wed Dec 16, 2009 6:39 pm
Location: Cary, NC

Re: Let's develop together: Yesterday's Close Value

Postby boss_hogg » Thu Dec 13, 2012 10:59 am

Nikolay thanks for the clarifications, things are clear now :)

I would really appreciate a simpler way of implementing different time frames on an indicator; eventually, my goal is a strategy with indicators running on different time frames.

Btw, could I do this by using extSubscribe , like this:
Code: Select all
fastSource=ExtSubscribe(1, instrument, "H1", bid, kind)
slowSource=ExtSubscribe(2, instrument, "H4", bid, kind)
fastIndicator = core.indicators:create("xxx", fastSource, instance.parameters.xxx_Period);
slowIndicator = core.indicators:create("xxx", slowSource, instance.parameters.xxx_Period);
...
ExtUpdate(1, source, period)
...
ExtUpdate(2, source, period)
...

Would the above result in the fastIndicator running for 1h candles and the slowIndicator running for 4h candles?
boss_hogg
FXCodeBase: Confirmed User
 
Posts: 48
Joined: Fri Oct 26, 2012 3:03 am

Re: Let's develop together: Yesterday's Close Value

Postby Victor.Tereschenko » Wed Dec 19, 2012 11:23 pm

boss_hogg wrote:Nikolay thanks for the clarifications, things are clear now :)

I would really appreciate a simpler way of implementing different time frames on an indicator; eventually, my goal is a strategy with indicators running on different time frames.

Btw, could I do this by using extSubscribe , like this:
Code: Select all
fastSource=ExtSubscribe(1, instrument, "H1", bid, kind)
slowSource=ExtSubscribe(2, instrument, "H4", bid, kind)
fastIndicator = core.indicators:create("xxx", fastSource, instance.parameters.xxx_Period);
slowIndicator = core.indicators:create("xxx", slowSource, instance.parameters.xxx_Period);
...
ExtUpdate(1, source, period)
...
ExtUpdate(2, source, period)
...

Would the above result in the fastIndicator running for 1h candles and the slowIndicator running for 4h candles?

Yes, you can do like this:
Code: Select all
fastSource=ExtSubscribe(1, instrument, "H1", bid, kind)
slowSource=ExtSubscribe(2, instrument, "H4", bid, kind)
fastIndicator = core.indicators:create("xxx", fastSource, instance.parameters.xxx_Period);
slowIndicator = core.indicators:create("xxx", slowSource, instance.parameters.xxx_Period);
...
ExtUpdate(id, source, period)
    if id == 1 then
        -- H1
    elseif id == 2 then
        -- H4
    end
...
“There are only three sports: bullfighting, motor racing, and mountaineering; all the rest are merely games.” (c) Ernest Hemingway
Victor.Tereschenko
FXCodeBase: Confirmed User
 
Posts: 144
Joined: Fri Nov 19, 2010 8:55 am

Re: Let's develop together: Yesterday's Close Value

Postby boss_hogg » Fri Dec 21, 2012 6:30 am

Thanks Victor, extUpdate seems to be the way to go ;)
boss_hogg
FXCodeBase: Confirmed User
 
Posts: 48
Joined: Fri Oct 26, 2012 3:03 am


Return to Indicator Development

Who is online

Users browsing this forum: No registered users and 11 guests