Dukascopy
 
 
Wiki JStore Search Login

Attention! Read the forum rules carefully before posting a topic.

    Try to find an answer in Wiki before asking a question.
    Submit programming questions in this forum only.
    Off topics are strictly forbidden.

Any topics which do not satisfy these rules will be deleted.

Issue with customising trading hours - after 1400 GMT
 Post subject: Issue with customising trading hours - after 1400 GMT Post rating: 0   New post Posted: Mon 27 Aug, 2012, 15:27 
User avatar

User rating: 0
Joined: Mon 20 Aug, 2012, 07:37
Posts: 8
Location: AustraliaAustralia
Hi,

I'm new to Jforex. Trying to run EA between 0700 and 1600 GMT, and then close all open positions at the end of the day,

public void onBar(Instrument instrument, Period period, IBar askbar, IBar bidbar) throws JFException {
       
        if (isValidTime(7, 0, 16, 0, Calendar.MONDAY, Calendar.TUESDAY, Calendar.WEDNESDAY, Calendar.THURSDAY, Calendar.FRIDAY) == false)
            return;
        if (instrument != Instrument.EURUSD || period != Period.FIVE_MINS)
            return;


I've done some testing to close all positions BEFORE 1400GMT and they were all successful. The following code is just one example to close all positions at 1400 GMT,
if (isValidTime(13, 55, 14, 0, Calendar.MONDAY, Calendar.TUESDAY, Calendar.WEDNESDAY, Calendar.THURSDAY, Calendar.FRIDAY) == true){
            for (IOrder orderInMarket : engine.getOrders())
                orderInMarket.close();       
        }


Attachment:
Screen Shot 2012-08-28 at 12.20.43 AM.png [36.34 KiB]
Downloaded 336 times


However, if I change it to any time AFTER 1400 GMT, none of the open positions got closed out at the time expected, for instance 1405 GMT,

if (isValidTime(14, 0, 14, 5, Calendar.MONDAY, Calendar.TUESDAY, Calendar.WEDNESDAY, Calendar.THURSDAY, Calendar.FRIDAY) == true){
            for (IOrder orderInMarket : engine.getOrders())
                orderInMarket.close();       
        }


Attachment:
Screen Shot 2012-08-28 at 12.18.50 AM.png [36.6 KiB]
Downloaded 331 times


I use calendar to customise trading hours. Can someone please advise what I did was wrong? Thanks!
    //use of calendar
    private boolean isValidTime(int fromHour, int fromMin, int toHour, int toMin, Integer... days) throws JFException {
       
        long lastTickTime = history.getLastTick(instrument).getTime();
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(lastTickTime);
        calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
        calendar.set(Calendar.HOUR_OF_DAY, fromHour);
        calendar.set(Calendar.MINUTE, fromMin);
        calendar.set(Calendar.SECOND, 0);
        long from = calendar.getTimeInMillis();
       
        calendar.setTimeInMillis(lastTickTime);
        calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
        calendar.set(Calendar.HOUR_OF_DAY, toHour);
        calendar.set(Calendar.MINUTE, toMin);
        calendar.set(Calendar.SECOND, 0);
        long to = calendar.getTimeInMillis();
       
        boolean isDayOk = (Arrays.asList(days)).contains(calendar.get(Calendar.DAY_OF_WEEK));
        boolean timeOk = lastTickTime > from  && lastTickTime < to ;
        print(String.format("calendar: %s - %s last tick: %s, isDayOk=%s, timeOk=%s",
                gmtSdf.format(from), gmtSdf.format(to), gmtSdf.format(lastTickTime), isDayOk, timeOk));       

        return isDayOk && timeOk;
    }


 
 Post subject: Re: Issue with customising trading hours - after 1400 GMT Post rating: 0   New post Posted: Tue 28 Aug, 2012, 01:34 
User avatar

User rating: 0
Joined: Mon 20 Aug, 2012, 07:37
Posts: 8
Location: AustraliaAustralia
I changed the subject as the issue is actually with isValidTime method by using calendar option. I don't think it has anything to do with order closing function.

I just did more tests with two strategies.
This is the one with customising trading hours working,
Attachment:
SMAStrategy_test_before1400gmt.java [5.64 KiB]
Downloaded 288 times


again, in Strategy "SMAStrategy_test_before1400gmt", I used the following code to close all orders in market after 13:59 GMT,
        boolean isValid = isValidTime(13, 59, 16, 0, Calendar.MONDAY, Calendar.TUESDAY, Calendar.WEDNESDAY, Calendar.THURSDAY, Calendar.FRIDAY);
        if (isValid == true){
            for (IOrder orderInMarket : engine.getOrders())
                orderInMarket.close();       
        }


and you may notice all filled/open orders got closed at 13:59. Here is the screenshot after the testing,
Attachment:
Screen Shot 2012-08-28 at 10.17.12 AM.png [27.79 KiB]
Downloaded 292 times


But in strategy "SMAStrategy_test_after1400gmt", the "customising trading hours" didn't work for some reason,
Attachment:
SMAStrategy_test_before1400gmt.java [5.64 KiB]
Downloaded 288 times


Please see the screenshot as below. None of the filled/open orders got closed around 14:01,
Attachment:
Screen Shot 2012-08-28 at 10.20.07 AM.png [28.1 KiB]
Downloaded 390 times


The only difference between these two strategies is the codes as following,
SMAStrategy_test_before1400gmt
boolean isValid = isValidTime(13, 59, 16, 0, Calendar.MONDAY, Calendar.TUESDAY, Calendar.WEDNESDAY, Calendar.THURSDAY, Calendar.FRIDAY);

SMAStrategy_test_after1400gmt
boolean isValid = isValidTime(14, 1, 16, 0, Calendar.MONDAY, Calendar.TUESDAY, Calendar.WEDNESDAY, Calendar.THURSDAY, Calendar.FRIDAY);


Can someone kindly advise what I missed in the tests above please? Thanks!


 
 Post subject: Re: Issue with customising trading hours - after 1400 GMT Post rating: 0   New post Posted: Wed 29 Aug, 2012, 06:56 
User avatar

User rating: 0
Joined: Mon 20 Aug, 2012, 07:37
Posts: 8
Location: AustraliaAustralia
Issue resolved after I put "calendar.setTimeZone(TimeZone.getTimeZone("GMT"));" in front of "calendar.setTimeInMillis(lastTickTime);".

Can someone at Dukascopy please review the codes posted on the Wiki site?
https://www.dukascopy.com/wiki/#Customize_trading_hours

long lastTickTime = history.getLastTick(instrument).getTime();
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
        calendar.setTimeInMillis(lastTickTime);
//        calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
        calendar.set(Calendar.HOUR_OF_DAY, fromHour);
        calendar.set(Calendar.MINUTE, fromMin);
        calendar.set(Calendar.SECOND, 0);
        long from = calendar.getTimeInMillis();
         
        calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
        calendar.setTimeInMillis(lastTickTime);
//        calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
        calendar.set(Calendar.HOUR_OF_DAY, toHour);
        calendar.set(Calendar.MINUTE, toMin);
        calendar.set(Calendar.SECOND, 0);
        long to = calendar.getTimeInMillis();


 
 Post subject: Re: Issue with customising trading hours - after 1400 GMT Post rating: 0   New post Posted: Wed 29 Aug, 2012, 12:49 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
The article has been updated.


 
 Post subject: Re: Issue with customising trading hours - after 1400 GMT Post rating: 0   New post Posted: Thu 30 Aug, 2012, 00:47 
User avatar

User rating: 0
Joined: Mon 20 Aug, 2012, 07:37
Posts: 8
Location: AustraliaAustralia
Cool. Thanks!


 

Jump to:  

  © 1998-2025 Dukascopy® Bank SA
On-line Currency forex trading with Swiss Forex Broker - ECN Forex Brokerage,
Managed Forex Accounts, introducing forex brokers, Currency Forex Data Feed and News
Currency Forex Trading Platform provided on-line by Dukascopy.com