How to get lastbarend from other timeframe

Hello all..
please allow me to put this topic separately, from the previous one -
[ Status (lastbartimeleft) - show negative value ], as i believe although this is one will be quite similar, but i think the approach & solution would be far different.

.
Background of my current challenge:

  • I mostly work with M1 data
  • and mostly, i only use M1 chart when doing live trade (still manual trade)
  • i already put some basic support & resistance level from other timeframe and can already plot it all inside what i called "multitimeframe map" (plotted at the blank bar space, on the right side)

Challenge:
Now im trying to get "Seconds to go" by status("lastbarend") from other timeframe, to be put also in my multitimeframe map in M1 chart.
.
Questions:
Is there any approach available? my current understanding is that all TimeFrame functions family, mostly only refer to OHLC, V, OI, Aux data.

Or is there any reference that i can started to learn?
i already tried to search, but still cant find anything similar to my needs. :pray:

.
thanks

if I understand the question correctly this would be 1 way to do it (see below). So in this example I assume you are displaying 1Min data and want to know how many seconds are left on the last 5Min bar.

tf = in5Minute;
timeinseconds = Hour() * 60 * 60 + Minute() * 60 + Second();

lastbarOfPeriod = TimeFrameExpand( 1, tf, expandPoint );
lastbarOfPeriod[BarCount - 1] = 0;

t1 = LastValue( ValueWhen( lastbarOfPeriod, timeinseconds, 1 ) );
t2 = LastValue( timeinseconds );
timeleft = int( tf - ( t2 - t1 ) );

Title = "Time left in Period  " +  timeleft;
Plot( lastbarOfPeriod, "", ColorRGB( 0, 0, 255 ), styleHistogram | styleOwnScale | styleNoLabel, 0, 1, 0, 1 );

just tested this in IQFeed and 1 line needs to be adjusted. timeleft should be:

timeleft = int( tf - ( t2 - t1 ) ) + Status( "lastbartimeleftrt" );

this works for me in IQFeed. Again I assume your chart is in 1Min and tf is in5Minute or in15Minute. I did not test it for other timeframes.

Thanks a lot @empottasch !
you actually captured my needs perfectly. :+1:

But i still find result differences if i take it from any TF higher than EOD (this is known, as Tomasz explained at other thread.

Not trying to steal your work, please allow me to modify your code. My goal is to make it also works for any base TF in finding "any" higher TF (esp. => EOD) of lastbarend.

Well I know this modified code (below) is not the most elegant solution, since there are many constants that i need to put in hardcoded (although i don't know the real logic behind it) :ghost:
So this is only a workaround approach for me,... and might not works for:

  • other RT datafeed timestamp (i use custom unofficial/unsupported MT4 plugin)
  • different timeshift offset (mine is +5)
  • different intraday settings at preferences (i use default start time of interval & override weekly/montly bars)

For someone wants to try using this code, please BE AWARE, this is working for my needs with above settings. I post this here, hoping that maybe someone else are interested to make/modified it to be more generic and proven to be used in any condition.

TimeShift = 5;

function GetSecLeftAtTF( OtherTF, timeshift )
{
    timeinseconds = Hour() * 60 * 60 + Minute() * 60 + Second();

    lastbarOfPeriod = TimeFrameExpand( 1, OtherTF, expandPoint );
    lastbarOfPeriod[BarCount - 1] = 0;

    t1 = LastValue( ValueWhen( lastbarOfPeriod, timeinseconds, 1 ) );
    t2 = LastValue( timeinseconds );

    if( OtherTF != 432001 OR OtherTF != 86400)
    {
        VarSet( "SecsToGoTF" + OtherTF, Status( "lastbartimeleft" ) ); 

        if( OtherTF == 86400 )
        {

            if( Interval( 0 ) == 60 )
            {
                VarSet( "SecsToGoTF" + OtherTF, Status( "lastbartimeleft" ) + timeshift*3600 - 60) ; // dont know why -60 :D
            }
            else
                if( Interval( 0 ) == 14400 )
                {
                    VarSet( "SecsToGoTF" + OtherTF, Status( "lastbartimeleft" ) - 3600 ) ; // dont know why -3600 :D
                }
                else
                {
                    VarSet( "SecsToGoTF" + OtherTF, Status( "lastbartimeleft" ) ) ;
                }

        }

        else
            if( OtherTF == 432001 )
            {
                if( Interval( 0 ) == 60 )
                {
                    VarSet( "SecsToGoTF" + OtherTF, Status( "lastbartimeleft" ) - 86400 + timeshift*3600 - 60 ) ; // dont know why -86400 & -60 :D
                }
                else
                    if( Interval( 0 ) == 300 )
                    {
                        VarSet( "SecsToGoTF" + OtherTF, Status( "lastbartimeleft" ) - 86400 + timeshift*3600 - 300 ) ; // dont know why -86400 & -300 :D
                    }
                else
                    if( Interval( 0 ) == 14400 )
                    {
                        VarSet( "SecsToGoTF" + OtherTF, Status( "lastbartimeleft" ) - 86400 - 3600 ) ; // dont know why -86400-3600 :D
                    }
                    else

                        VarSet( "SecsToGoTF" + OtherTF, Status( "lastbartimeleft" ) - 86400 ); // dont know why -86400 :D
            }

        VarSet( "TimeLeftAtTF" + OtherTF, int( OtherTF - ( t2 - t1 ) ) + VarGet( "SecsToGoTF" + OtherTF ) );
    }

    return VarGet( "TimeLeftAtTF" + OtherTF );
}


/*
Notes: in this case, you are trying to access data from higher timeframe.
so this can only show VALID seconds left, IF your "base timeframe" is lower than target timeframe
*/

OtherTF = StrToNum(ParamList("Target Timeframe (seconds)", "60|300|900|1800|3600|14400|86400|432001", 300 ));
GetSecLeftAtTF(OtherTF, TimeShift);
Title="";

GfxTextOut("Time Shift: "+timeshift,0,0);
GfxTextOut("Local Time: "+Now(2),0,10);
GfxTextOut("BaseChart: s"+Interval(0),0,30);
GfxTextOut("Seconds Left at : s" + OtherTF +" Chart :"+ GetSecLeftAtTF(OtherTF, TimeShift) + " seconds",0,40);

yes my solution only works intraday.

If you extend beyond a day you might look at:

in combination with the expandPoint approach. Then you use the datetime at the lastBarOfPeriod.

So if you have tf = inWeekly; then you calculate the difference in seconds between the datetime at the lastBarOfPeriod with the current datetime. Not sure if that will work. Maybe I will try later.

so like this. You need to check if it works.

tf = in5Minute;

lastbarOfPeriod = TimeFrameExpand( 1, tf, expandPoint );
lastbarOfPeriod[BarCount - 1] = 0;

arg2 = LastValue( ValueWhen( lastbarOfPeriod, DateTime() ) );
arg1 = LastValue( DateTime() );
timeleft = tf - DateTimeDiff( arg1, arg2 ) + Status( "lastbartimeleftrt" );

Title = "Time left in Period  " +  timeleft;
Plot( lastbarOfPeriod, "", ColorRGB( 0, 0, 255 ), styleHistogram | styleOwnScale | styleNoLabel, 0, 1, 0, 1 );

Thanks for the heads-up about DateTimeDiff.
i tried your code, but it think this is more difficult to handle the calculation. i can't get "better" result than your first code.

reading from the reference:
"It is important to understand that DateTime is not a simple number but rather bitset and two datetime values can only be reliably compared for equlity or inequality using == or != operators. Any other comparisions (less than/greater then), using normal operators > < may sometimes lead to wrong results (if one of dates compared is pre-1964), therefore to compare two datetime numbers reliably you should use DateTimeDiff."

.
Anyway, i also want to retract my approach above, in 4th post
that approach breaks down miserably when local time goes beyond 24:00. :man_facepalming:

just now, in M1 chart at 00:00:00, the seconds left became 86400 :man_facepalming:
although it is back to normal (correct) at 00:01:00 (countdown back from 60s)

it happens at all other timeframe (M5 will become correct after 00:05:00, m15 will become correct at 00:15:00, etc. i believe 4H will become correct later at 04:00:00, and D1 at 05:00:00 (because my timeshift is +5 hours), and so on...

Silly me. I'm sorry guys.

.
I hope someone in this forum can make a solid one :pray: :pray:

Im sorry guys. this code will show incorrect result after 24:00 (eod)
sorry to post this too soon.

inline explanation of the breaks down at the end of 7th post

the last code I posted seems to work. I edited it after I initially posted it. So maybe try it again maybe you used the code before I edited it.

i tried it again..

at my side, yes it works from any intraday TF higher than base interval...
but if i try to get data from above EOD, it still give incorrect seconds left.. like the first one

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.