Where to place Update(period) in pivot.lua

Moderator: admin

Re: Where to place Update(period) in pivot.lua

Postby Nikolay.Gekht » Thu May 27, 2010 12:39 pm

And here is improved example, which can catch the situation when the last candle of the specified period absent. Look at this debugger snapshot, there is not last candle (@ 20:59, but the actual last candle, @20:58 is still detected). The example, I provided in the previous post is not able to detect this.
lostcandle.png


Code: Select all
function Init()
    indicator:name("Demo: Mark last candle in the bigger time frame");
    indicator:description("");
    indicator:requiredSource(core.Bar);
    indicator:type(core.Indicator);

    indicator.parameters:addString("TF", "Bigger time frame", "", "D1");
    indicator.parameters:setFlag("TF", core.FLAG_PERIODS);
    indicator.parameters:addColor("CLR", "Label color", "", core.COLOR_LABEL);
end

local source;
local out;
local TF;
local currLength;   -- length of the current chart bar in seconds
local tfLength;     -- length of the bigger time frame in seconds
local dayOffset;
local weekOffset;
local candle;       -- the stream which keeps the candle of the bigger time frame
local flag;         -- the stream when keeps marks that the period is already marked as the last

function Prepare()
    TF = instance.parameters.TF;
    source = instance.source;

    dayOffset = core.host:execute("getTradingDayOffset");
    weekOffset = core.host:execute("getTradingWeekOffset");

    local s, e;
    -- get length of the current chart time frame in seconds
    -- note: in fact, for getting the length the dayOffset and weekOffset does not matter.
    s, e = core.getcandle(source:barSize(), core.now(), dayOffset, weekOffset);
    currLength = math.floor((e - s) * 86400 + 0.5);
    -- get length of the bigger time frame in seconds
    s, e = core.getcandle(TF, core.now(), dayOffset, weekOffset);
    tfLength = math.floor((e - s) * 86400 + 0.5);

    assert(currLength < tfLength, "The chosen time frame must be longer than the chart time frame");

    local name = profile:id();
    instance:name(name);
    out = instance:createTextOutput ("out", "out", "Wingdings", 10, core.H_Center, core.V_Top, instance.parameters.CLR, 0);
    candle = instance:addInternalStream(0, 0);
    flag = instance:addInternalStream(0, 0);
end

function Update(period, mode)
    local s, e, l, c;

    -- get beginning of the candle of the bigger time frame to which this
    -- canlde belongs (s) and the beginning of the next candle (e)
    s, e = core.getcandle(TF, source:date(period), dayOffset, weekOffset);
    s = math.floor(s * 86400 + 0.5);
    candle[period] = s;
    e = math.floor(e * 86400 + 0.5);
    -- calculate the beginning of the last candle of the current timeframe
    -- inside the bigger time frame candle
    l = e - currLength;
    -- convert current candle from days to seconds
    c = math.floor(source:date(period) * 86400 + 0.5);
    if c == l then
        out:set(period, source.median[period], "\164");
        flag[period] = 1;
    elseif period > 1 and candle[period - 1] ~= candle[period] and flag[period - 1] == 0 then
        -- if the candle is changed but the previous period is not market yet
        out:set(period - 1, source.median[period - 1], "\164");
        flag[period - 1] = 1;
    else
        flag[period] = 0;
    end
end
Nikolay.Gekht
FXCodeBase: Site Admin
 
Posts: 1235
Joined: Wed Dec 16, 2009 6:39 pm
Location: Cary, NC

Re: Where to place Update(period) in pivot.lua

Postby LordTwig » Fri May 28, 2010 2:49 am

Wow.... your quick, I like how you marked the candle, looks like I can play around with it to see how it goes.

Have looked at it and I have a couple of questions,

1) If I want to use 10 periods before end of day (instead of the (1 period) used here), which values do I only change?
1).1) Can it be easily made into user selectable choice?

2) Also, lets say I need the same to happen again, but at a different earlier time ((so twice) for different condition) but 10 hours before, How would that effect the code, ie, can I use most of it or do I have to write it as though it was separate from each other?

example: trigger for a possible trade activated, but 10 hours pass by and no Buy or Sell is activated, so I want to stop trade taking place after this amount of time(10 Hours). and preferably user settable any time amount.

Is there any way to use Time format (HH:MM:SS) for any of this (or convert what you have done to easily use Time formats), so it can be used several times throughout the same indicator?

for example, if EndDay >= 16:59:59 then....
or if StopTrade >= 5:00:00 then .....
or ...... and so on.

Cheers, LordTwig

Thank you,
LordTwig
Cheers
LordTwig
LordTwig
FXCodeBase: Confirmed User
 
Posts: 157
Joined: Fri Mar 12, 2010 3:15 am

Re: Where to place Update(period) in pivot.lua

Postby Nikolay.Gekht » Fri May 28, 2010 2:43 pm

LordTwig wrote:1) If I want to use 10 periods before end of day (instead of the (1 period) used here), which values do I only change?
1).1) Can it be easily made into user selectable choice?

The 10th period before the end is
Code: Select all
l = e - currLength * 10;

So, the check for the current candle is after the l candle but before the end of the day is
Code: Select all
if c >= l and c < e then
    ...


LordTwig wrote:2) Also, lets say I need the same to happen again, but at a different earlier time ((so twice) for different condition) but 10 hours before, How would that effect the code, ie, can I use most of it or do I have to write it as though it was separate from each other?

example: trigger for a possible trade activated, but 10 hours pass by and no Buy or Sell is activated, so I want to stop trade taking place after this amount of time(10 Hours). and preferably user settable any time amount.

Just remember the time of the start and then check it against the current time. Let's suggest that the Marketscope time is converted to the seconds, i.e. via math.floor(x * 86400 + 0.5).
So, the check is
Code: Select all
-- 10 hours is 10 x 60 (minutes) x 60 (seconds) = 36000
if currentTime - startTime >= 36000 then
   ...


LordTwig wrote:Is there any way to use Time format (HH:MM:SS) for any of this (or convert what you have done to easily use Time formats), so it can be used several times throughout the same indicator?

for example, if EndDay >= 16:59:59 then....
or if StopTrade >= 5:00:00 then .....
or ...... and so on.

There is no such datatype as a time in lua (as well as in the most programming languages, even used for the trading. So, nohow. However, you can a couple of simple functions such as:
Code: Select all
function hms_seconds(hour, minute, seconds)
    return hour * 60 * 60 + minute * 60 + seconds;
end
...
function date_seconds(date)
    return math.floor(date * 86400 + 0.5);
end

So, if you wanna check whether the current candle is more than 5 hours 12 minutes and 35 seconds past the collection start, just use:
Code: Select all
if date_second(source:date(period)) - date_second(source:date(0)) > hms_seconds(5, 12, 35) then
Nikolay.Gekht
FXCodeBase: Site Admin
 
Posts: 1235
Joined: Wed Dec 16, 2009 6:39 pm
Location: Cary, NC

Re: Where to place Update(period) in pivot.lua

Postby LordTwig » Fri May 28, 2010 11:35 pm

Nikolay.Gekht wrote:The 10th period before the end is

Code: Select all
l = e - currLength * 10;

So, the check for the current candle is after the l candle but before the end of the day is

Code: Select all
if c >= l and c < e then
...


Okay that works (although it does check the whole last 10 candles not just the 10th)

Nikolay.Gekht wrote:Just remember the time of the start and then check it against the current time. Let's suggest that the Marketscope time is converted to the seconds, i.e. via math.floor(x * 86400 + 0.5).
So, the check is

Code: Select all
-- 10 hours is 10 x 60 (minutes) x 60 (seconds) = 36000
if currentTime - startTime >= 36000 then


Okay, Thanks for that, (remembering start time is 17:00) easier than I thought it would be :) . once :idea: i figured out 'c' was currentTime and 's' was startTime.

Nikolay.Gekht wrote:There is no such datatype as a time in lua (as well as in the most programming languages, even used for the trading. So, nohow. However, you can a couple of simple functions such as:

Code: Select all
function hms_seconds(hour, minute, seconds)
return hour * 60 * 60 + minute * 60 + seconds;
end
...
function date_seconds(date)
return math.floor(date * 86400 + 0.5);
end

So, if you wanna check whether the current candle is more than 5 hours 12 minutes and 35 seconds past the collection start, just use:

Code: Select all
if date_second(source:date(period)) - date_second(source:date(0)) > hms_seconds(5, 12, 35) then


Can you show an example of this in the indicator, because it looks like real easy and less complicated to be able to use the times values as you have shown above.(thats what I am after easy) I will have a go at it in the meantime.

Thanks for your continued help throughout this whole process.
LordTwig
Cheers
LordTwig
LordTwig
FXCodeBase: Confirmed User
 
Posts: 157
Joined: Fri Mar 12, 2010 3:15 am

Previous

Return to Indicator Development

Who is online

Users browsing this forum: No registered users and 8 guests