Dukascopy Support Board
http://www.dukascopy.com/swiss/english/forex/jforex/forum/

Accessing the last 6 hour bars every six hours
http://www.dukascopy.com/swiss/english/forex/jforex/forum/viewtopic.php?f=77&t=50091
Page 1 of 2

Author:  us_copiosus [ Wed 25 Sep, 2013, 19:44 ]
Post subject:  Accessing the last 6 hour bars every six hours

Hi

Am still having problems getting the correct bars......................

Output is as follows:-
*************************
18:00:00 Date: 26-09-13,Time: 04,Open: 98.63,High: 98.671,Low: 98.587,Close: 98.626,Volume: 2382.76
18:00:00 Date: 26-09-13,Time: 03,Open: 98.682,High: 98.733,Low: 98.627,Close: 98.63,Volume: 2486.66
18:00:00 Date: 26-09-13,Time: 02,Open: 98.741,High: 98.815,Low: 98.624,Close: 98.688,Volume: 4047.14
18:00:00 Date: 26-09-13,Time: 01,Open: 98.681,High: 98.759,Low: 98.609,Close: 98.741,Volume: 3033.16
18:00:00 Date: 26-09-13,Time: 00,Open: 98.542,High: 98.723,Low: 98.439,Close: 98.681,Volume: 3541.63
18:00:00 Date: 25-09-13,Time: 23,Open: 98.49,High: 98.552,Low: 98.384,Close: 98.54,Volume: 2330.54

Here - I am one hour out of step with the bars (time should read 00, 01.....05)
***********************************************************************************
11:59:59 Date: 25-09-13,Time: 23,Open: 98.49,High: 98.552,Low: 98.384,Close: 98.54,Volume: 2330.54
11:59:59 Date: 25-09-13,Time: 22,Open: 98.559,High: 98.584,Low: 98.444,Close: 98.492,Volume: 2083.85
11:59:59 Date: 25-09-13,Time: 21,Open: 98.517,High: 98.625,Low: 98.442,Close: 98.557,Volume: 2352.63
11:59:59 Date: 25-09-13,Time: 20,Open: 98.54,High: 98.587,Low: 98.388,Close: 98.518,Volume: 3968.71
11:59:59 Date: 25-09-13,Time: 19,Open: 98.561,High: 98.675,Low: 98.486,Close: 98.54,Volume: 3798.54
11:59:59 Date: 25-09-13,Time: 18,Open: 98.616,High: 98.685,Low: 98.507,Close: 98.564,Volume: 2499.15

Here - output is OK
***********************************************************************************
06:00:00 Date: 25-09-13,Time: 17,Open: 98.682,High: 98.759,Low: 98.58,Close: 98.615,Volume: 1725.22
06:00:00 Date: 25-09-13,Time: 16,Open: 98.723,High: 98.767,Low: 98.644,Close: 98.684,Volume: 1686.82
06:00:00 Date: 25-09-13,Time: 15,Open: 98.614,High: 98.724,Low: 98.604,Close: 98.724,Volume: 1543.75
06:00:00 Date: 25-09-13,Time: 14,Open: 98.654,High: 98.676,Low: 98.602,Close: 98.616,Volume: 1307.18
06:00:00 Date: 25-09-13,Time: 13,Open: 98.646,High: 98.678,Low: 98.558,Close: 98.654,Volume: 2617.21
06:00:00 Date: 25-09-13,Time: 12,Open: 98.725,High: 98.801,Low: 98.637,Close: 98.646,Volume: 2603.3

Here - output is OK
********************************************************************************
00:00:00 Date: 25-09-13,Time: 10,Open: 98.732,High: 98.781,Low: 98.716,Close: 98.771,Volume: 719.1
00:00:00 Date: 25-09-13,Time: 09,Open: 98.735,High: 98.786,Low: 98.708,Close: 98.73,Volume: 508.72
00:00:00 Date: 25-09-13,Time: 08,Open: 98.744,High: 98.793,Low: 98.686,Close: 98.735,Volume: 1448.27
00:00:00 Date: 25-09-13,Time: 07,Open: 98.804,High: 98.859,Low: 98.732,Close: 98.742,Volume: 1480.58
00:00:00 Date: 25-09-13,Time: 06,Open: 98.928,High: 98.946,Low: 98.797,Close: 98.8,Volume: 1773.87
00:00:00 Date: 25-09-13,Time: 05,Open: 98.863,High: 99.003,Low: 98.838,Close: 98.934,Volume: 2263.48

Here - output is one hour out of step again!
*********************************************************************************
17:59:59 Date: 25-09-13,Time: 05,Open: 98.863,High: 99.003,Low: 98.838,Close: 98.934,Volume: 2263.48
17:59:59 Date: 25-09-13,Time: 04,Open: 98.757,High: 98.901,Low: 98.743,Close: 98.861,Volume: 2695.82
17:59:59 Date: 25-09-13,Time: 03,Open: 98.667,High: 98.808,Low: 98.667,Close: 98.758,Volume: 3040.41
17:59:59 Date: 25-09-13,Time: 02,Open: 98.753,High: 98.753,Low: 98.475,Close: 98.665,Volume: 4932.95
17:59:59 Date: 25-09-13,Time: 01,Open: 98.695,High: 98.76,Low: 98.657,Close: 98.749,Volume: 3110.74
17:59:59 Date: 25-09-13,Time: 00,Open: 98.589,High: 98.699,Low: 98.573,Close: 98.695,Volume: 3652.55

Here - output is OK
**************************************************************
code is as follows in onBar method
if (!period.equals(myPeriod)) return; // [myPeriod is ONE_HOUR]
if (!((hour==5) || (hour==11) || (hour==17) || (hour==23))) return;

Any suggestions ?

Bob M

Author:  jlongo [ Wed 25 Sep, 2013, 23:50 ]
Post subject:  Re: Accessing the last 6 hour bars every six hours

Hi:

The hour variable have assigned what value ?

System ?

IBar.getTime() ?

Trade well

JL

Author:  us_copiosus [ Thu 26 Sep, 2013, 00:07 ]
Post subject:  Re: Accessing the last 6 hour bars every six hours

hour variable = 17, 11, 5, 23, 17 [latest to oldest set of output]

system? = not sure what you are asking
Using demo account - and real time

Bob M

Author:  us_copiosus [ Thu 26 Sep, 2013, 00:13 ]
Post subject:  Re: Accessing the last 6 hour bars every six hours

OK

public double [][] IBarsToArray(List,IBar. inputBars)
{
double[][] ohlcvArray = new double[6] inputBars.size()];

for (int i=0, i<inputBars.size(); i++){

ohlcvArray[0][i] = inputBars.get(i).getTime();
etc
}
return ohlcvArray;
}

Bob M

Author:  jlongo [ Thu 26 Sep, 2013, 00:56 ]
Post subject:  Re: Accessing the last 6 hour bars every six hours

You are comparing the content of the variable hour with that 6 hours periods 5, 11, 17, 23...

What you are ssigning tom the variable hour ?

The code shown don't have this.

Author:  us_copiosus [ Thu 26 Sep, 2013, 01:06 ]
Post subject:  Re: Accessing the last 6 hour bars every six hours

In the onBar method

int hour = calendar.get(Calendar.HOUR_OF_DAY);

Is that what you are asking?

Bob M

Author:  jlongo [ Thu 26 Sep, 2013, 01:25 ]
Post subject:  Re: Accessing the last 6 hour bars every six hours

and what you have inside calendar object ?

Author:  us_copiosus [ Thu 26 Sep, 2013, 01:31 ]
Post subject:  Re: Accessing the last 6 hour bars every six hours

Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT + 12"));
calendar.setTimeInMillis(bidBar.getTime());
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
int day_of_week = calendar.get(Calendar.DAY_OF_WEEK);
int hour = calendar.get(Calendar.HOUR_OF_DAY);

Author:  jlongo [ Thu 26 Sep, 2013, 01:45 ]
Post subject:  Re: Accessing the last 6 hour bars every six hours

Seems ok to me...

how do you put the date on the output ? this for example -> "17:59:59 Date: 25-09-13,Time: 05...."

Author:  us_copiosus [ Thu 26 Sep, 2013, 02:28 ]
Post subject:  Re: Accessing the last 6 hour bars every six hours

myConsole.getOut().println("Date: " + df.format(ohlcvArray[0][i]) + "," + "Time: " + tf.format(ohlcvArray[0][i])....etc.

private SimpleDateFormat df = new SimpleDateFormat("dd-MM-yy");
private SimpleDateFormat tf = new SimpleDateFormat("HH");

Author:  us_copiosus [ Thu 26 Sep, 2013, 03:27 ]
Post subject:  Re: Accessing the last 6 hour bars every six hours

Just to clarify a little.....................

I am not so concerned about the time shown on the bars

What I am concerned about is the fact that in two of the instances shown above, I am NOT getting the latest of the six bars!

Bob M

Author:  jlongo [ Thu 26 Sep, 2013, 09:47 ]
Post subject:  Re: Accessing the last 6 hour bars every six hours

Hi:

Try one thing: use the default time zone, just to see if don't is a problem with time zone conversion.
Also, you have compareted the values of the bars to see if they math with what you expect ?

JL

Author:  us_copiosus [ Thu 26 Sep, 2013, 10:58 ]
Post subject:  Re: Accessing the last 6 hour bars every six hours

Hi

Have deleted the "+12" in the TimeZone

Yes - I have checked the bar values with the graph of USDJPY and found the bars one hour late in my noted incorrect output

Bob M

Author:  jlongo [ Thu 26 Sep, 2013, 12:00 ]
Post subject:  Re: Accessing the last 6 hour bars every six hours

And with "GMT" time zone happens the same ?

Author:  us_copiosus [ Thu 26 Sep, 2013, 19:55 ]
Post subject:  Re: Accessing the last 6 hour bars every six hours

Here is the latest output
***************************
[GMT = 2013-09-26 17:59:59]
***************************
17:59:59 Hour_nominal: 6pm
17:59:59 Hour_numeric: 18
17:59:59 DOW_nominal: Thursday
17:59:59 DOW_numeric: 4
17:59:59 time_current: 18
17:59:59 date_current: 26-9-2013

[the following bars are 1 hour out of sync]
17:59:59 Date: 27-09-13,Time: 04,Open: 98.958,High: 99.078,Low: 98.954,Close: 99.051,Volume: 2399.74
17:59:59 Date: 27-09-13,Time: 03,Open: 98.949,High: 98.973,Low: 98.892,Close: 98.956,Volume: 2607.65
17:59:59 Date: 27-09-13,Time: 02,Open: 98.932,High: 99.132,Low: 98.87,Close: 98.947,Volume: 5175.28
17:59:59 Date: 27-09-13,Time: 01,Open: 98.922,High: 98.977,Low: 98.884,Close: 98.933,Volume: 2728.73
17:59:59 Date: 27-09-13,Time: 00,Open: 98.736,High: 98.997,Low: 98.73,Close: 98.923,Volume: 3460.23
17:59:59 Date: 26-09-13,Time: 23,Open: 98.638,High: 98.75,Low: 98.625,Close: 98.737,Volume: 1964.28
17:59:59 DOW: 5, Hour: 17

Note: the first bar listed above should show Open: 99.050, High 99.073 etc.
the second bar should read Open: 98.958, High 99.078 etc
*********************************************************************************
[GMT = 2013-09-26 11:59:59]
***************************
11:59:59 Hour_nominal: Midday
11:59:59 Hour_numeric: 12
11:59:59 DOW_nominal: Thursday
11:59:59 DOW_numeric: 4
11:59:59 time_current: 12
11:59:59 date_current: 26-9-2013
11:59:59 Date: 26-09-13,Time: 23,Open: 98.638,High: 98.75,Low: 98.625,Close: 98.737,Volume: 1964.28
11:59:59 Date: 26-09-13,Time: 22,Open: 98.649,High: 98.655,Low: 98.506,Close: 98.637,Volume: 2264.55
11:59:59 Date: 26-09-13,Time: 21,Open: 98.662,High: 98.725,Low: 98.56,Close: 98.648,Volume: 3151.28
11:59:59 Date: 26-09-13,Time: 20,Open: 98.911,High: 98.987,Low: 98.645,Close: 98.668,Volume: 3732.36
11:59:59 Date: 26-09-13,Time: 19,Open: 98.992,High: 99.02,Low: 98.88,Close: 98.912,Volume: 2538.71
11:59:59 Date: 26-09-13,Time: 18,Open: 98.91,High: 99.089,Low: 98.904,Close: 98.989,Volume: 3607.6
11:59:59 DOW: 5, Hour: 11
Output is correct ????????

Bob M

Author:  us_copiosus [ Thu 26 Sep, 2013, 21:18 ]
Post subject:  Re: Accessing the last 6 hour bars every six hours

This whole business of instability in the output every so often begs the question:-

Is there a better coding approach to having STABLE output EVERY six hours ?

All I am seeking is the last six hour bars every six hours at 6, 12, 18, and 24 hours

Bob M

Author:  jlongo [ Thu 26 Sep, 2013, 21:22 ]
Post subject:  Re: Accessing the last 6 hour bars every six hours

By what i've read you want the information of the 6 last 1 hour bars ending at 05:59:59:9999, 11:59:59:9999, 17:59:59:9999 and 23:59:59:9999 ?

Is that ?

JL

Author:  us_copiosus [ Thu 26 Sep, 2013, 21:26 ]
Post subject:  Re: Accessing the last 6 hour bars every six hours

yes - spot on!

Bob M

Author:  jlongo [ Thu 26 Sep, 2013, 23:13 ]
Post subject:  Re: Accessing the last 6 hour bars every six hours

Hi:

See this code:

package tests;

import com.dukascopy.api.Filter;
import com.dukascopy.api.IAccount;
import com.dukascopy.api.IBar;
import com.dukascopy.api.IContext;
import com.dukascopy.api.IHistory;
import com.dukascopy.api.IMessage;
import com.dukascopy.api.IStrategy;
import com.dukascopy.api.ITick;
import com.dukascopy.api.Instrument;
import com.dukascopy.api.JFException;
import com.dukascopy.api.OfferSide;
import com.dukascopy.api.Period;
import java.util.Calendar;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Us_copiosus implements IStrategy{
    private Instrument myInstrument = Instrument.EURUSD;
    private Period myPeriod = Period.ONE_HOUR;
    private IContext myContext;
    private IHistory myHistory;

    @Override
    public void onStart(IContext context) throws JFException {
        myContext = context;
        myHistory = context.getHistory();
        Set SubInstruments = new HashSet();
        SubInstruments.add(myInstrument);
        myContext.setSubscribedInstruments(SubInstruments);
    }

    @Override
    public void onTick(Instrument instrument, ITick tick) throws JFException {
       
    }

    @Override
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
       
        if(myInstrument.equals(instrument) && myPeriod.equals(period)){
            // one way but weekends not filtered
            long barTime = askBar.getTime();
            Calendar cal = Calendar.getInstance();
            cal.setTimeInMillis(barTime);
            int hours = cal.get(Calendar.HOUR_OF_DAY);
            if (hours == 0 || hours == 6 || hours == 12 || hours == 18){
                IBar[] lastSixBar = new IBar[6];
                int y = 0;
                for (int x = 7; x > 1 ; x-- , y++){
                    lastSixBar[y] = myHistory.getBar(instrument, period, OfferSide.BID, x);
                }
                myContext.getConsole().getOut().println("Hour: " + hours);
                for (IBar bar : lastSixBar){
                    myContext.getConsole().getOut().println(bar.toString());
                }
            }
            // other way
            if (hours == 0 || hours == 6 || hours == 12 || hours == 18){
                List<IBar> lastSixBar = myHistory.getBars(instrument, period, OfferSide.BID, Filter.WEEKENDS, barTime - 1000*60*60*6, barTime - 1000*60*60);
                myContext.getConsole().getOut().println("Hour (with history array): " + hours);
                for (IBar bar : lastSixBar){
                    myContext.getConsole().getOut().println(bar.toString());
                }
                lastSixBar.clear();
            }
            // other way
            if (hours == 0 || hours == 6 || hours == 12 || hours == 18){
                List<IBar> lastSixBar = myHistory.getBars(instrument, period, OfferSide.BID, Filter.WEEKENDS, 6, barTime - 1000*60*60, 0);
                myContext.getConsole().getOut().println("Hour (with history array version 2): " + hours);
                for (IBar bar : lastSixBar){
                    myContext.getConsole().getOut().println(bar.toString());
                }
                lastSixBar.clear();
            }
        }
       
    }

    @Override
    public void onMessage(IMessage message) throws JFException {
       
    }

    @Override
    public void onAccount(IAccount account) throws JFException {
       
    }

    @Override
    public void onStop() throws JFException {
       
    }
   
}


And the resulting text messages saved in historical tester for last month period.
You will see some discrepancies at weekends but that is because of way 3 gives always last 6 available bars.


Trade well

JL

Attachments:
Us_copiosus_26092013_220623.csv [252.05 KiB]
Downloaded 148 times

Author:  us_copiosus [ Fri 27 Sep, 2013, 02:54 ]
Post subject:  Re: Accessing the last 6 hour bars every six hours

Hi JL

Yet again, thank you very much for your time and understanding of the problem

I shall look at all this over the weekend and try to move on

Way 2 looks like the one I want

Many thanks

Bob M

  Page 1 of 2