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.

Keep strategy from running when the market is closed
 Post subject: Keep strategy from running when the market is closed Post rating: 0   New post Posted: Thu 19 Jul, 2012, 00:44 

User rating: 0
Joined: Thu 19 Jul, 2012, 00:32
Posts: 6
Hi,

I would like to keep my strategies from running when the market is closed on the weekend, because that causes a number of problems. On your Wiki I found something, that, with slight modifications, should do the trick. However my strategies kept running over the weekends as before. I wrote a little test strategy to show the problem. In this test strategy I am simply counting all 5 minute candles.
I ran this test strategy in the historical tester from Saturday 7/7/2012 to Saturday 7/14/2012. In my understanding candles should only be counted from Sunday 7/8/2012 9pm GMT to Friday 7/13/2012 9pm GMT by the strategy. In these five days I would expect 1440 5 minute candles. The strategy however prints 2016 counted candles at the and. This clearly indicates to me that the strategy run outside the specified time.

Thanks
Markus

Here is the strategy:

package jforex;

import java.util.*;

import com.dukascopy.api.*;

public class TestWeekend implements IStrategy {
    private IEngine engine;
    private IConsole console;
    private IHistory history;
    private IContext context;
    private IIndicators indicators;
    private IUserInterface userInterface;
   
    private int CandleCount = 0;
   
    public void onStart(IContext context) throws JFException {
        this.engine = context.getEngine();
        this.console = context.getConsole();
        this.history = context.getHistory();
        this.context = context;
        this.indicators = context.getIndicators();
        this.userInterface = context.getUserInterface();
    }

    public void onAccount(IAccount account) throws JFException {
    }

    public void onMessage(IMessage message) throws JFException {
    }

    public void onStop() throws JFException {
        Print("Candles counted: "+CandleCount);
    }

    public void onTick(Instrument instrument, ITick tick) throws JFException {
       
        if (instrument != Instrument.EURUSD)
        {
            return;
        }
    }
   
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
        if ( (instrument != Instrument.EURUSD) || (period != Period.FIVE_MINS) )
        {
            return;
        }
       
        if (isMarketClosed())
        {
            return;
        }
       
        CandleCount++;
    }
   
    private boolean isMarketClosed() throws JFException
    {
        long lastTickTime = history.getLastTick(Instrument.EURUSD).getTime();
        Calendar calendar = Calendar.getInstance();
        int day;
        long time;
       
        calendar.setTimeInMillis(lastTickTime);
        calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
        day = calendar.get(Calendar.DAY_OF_WEEK);
       
        if (day == Calendar.SATURDAY) // no trading on Saturday
        {
            return true;
        }
       
        if (day == Calendar.FRIDAY)
        {
            calendar.set(Calendar.HOUR_OF_DAY, 21);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            time = calendar.getTimeInMillis();
            if (lastTickTime > time) // No trading on Friday past 9pm
            {
                return true;
            }
        }
       
        if (day == Calendar.SUNDAY)
        {
            calendar.set(Calendar.HOUR_OF_DAY, 21);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            time = calendar.getTimeInMillis();
            if (lastTickTime < time) // No trading on Sunday before 9pm
            {
                return true;
            }
        }
       
        // if not Friday, Saturday or Sunday, market is always open
        return false;
    }
   
    private void Print(String message)
    {
        console.getOut().println(message);
    }
}


 
 Post subject: Re: Keep strategy from running when the market is closed Post rating: 0   New post Posted: Thu 19 Jul, 2012, 09:55 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
We added a couple of logging statements such that you can see that the time comparisons are proper:
Attachment:
TestWeekend.java [3.63 KiB]
Downloaded 305 times
JForex-API also contains interfaces for checking more conveniently if the market is Offline, see:
https://www.dukascopy.com/wiki/#Market_Hours/Check_if_market_is_offline


 
 Post subject: Re: Keep strategy from running when the market is closed Post rating: 0   New post Posted: Thu 19 Jul, 2012, 22:45 

User rating: 0
Joined: Thu 19 Jul, 2012, 00:32
Posts: 6
Hi,

thanks for your fast reply. It maybe me, but I still don't understand the behavior of the program. My first attempt was to copy the isOffline() function from the Wiki reference in your answer into my strategy, but this did not compile. I got the error:

21:10:40 ----------
21:10:40 dataService cannot be resolved
21:10:40 ^^^^^^^^^^^
21:10:40 Set<ITimeDomain> offlines = dataService.getOfflineTimeDomains(time - Period.WEEKLY.getInterval(), time + Period.WEEKLY.getInterval());
21:10:40 1. ERROR in C:\Users\base4\AppData\Local\Temp\jfxide\tmp\compile\TestWeekend.java (at line 125)
21:10:40 ----------

So I commented that again and went back to my old isMarketClosed() function. To make sure I understand your print commands right I added a little to it.
Now here is what I get in the Message window of the historical tester:

21:02:27 Now I am in onBar! {T:1341744600000(2012-07-08 10:50:00.000+0000)E:falseO:1.229C:1.229L:1.229H:1.229V0.0}
21:02:27 SUNDAY tick: 2012-07-08 21:00:01.396 calendar: 2012-07-08 21:00:00.000
21:02:27 Now I am in onBar! {T:1341744300000(2012-07-08 10:45:00.000+0000)E:falseO:1.229C:1.229L:1.229H:1.229V0.0}
21:02:27 SUNDAY tick: 2012-07-08 21:00:01.396 calendar: 2012-07-08 21:00:00.000

My two questions are:

When I am in a SUNDAY tick, isMarketClosed() should return true. So how can onBar get past the return statement in line 60 to the print statement in line 68?

Why does the solution from the Wiki (which is doubtlessly more elegant) not compile?

Thanks
Markus

Here is the latest version of my strategy:

package jforex.requests;
 
import java.text.SimpleDateFormat;
import java.util.*;
 
import com.dukascopy.api.*;
 
public class TestWeekend implements IStrategy {
    private IEngine engine;
    private IConsole console;
    private IHistory history;
    private IContext context;
    private IIndicators indicators;
    private IUserInterface userInterface;
     
    private int CandleCount = 0;
   
    @SuppressWarnings("serial")
    public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS") {
        {
            setTimeZone(TimeZone.getTimeZone("GMT"));
        }
    };
     
    public void onStart(IContext context) throws JFException {
        this.engine = context.getEngine();
        this.console = context.getConsole();
        this.history = context.getHistory();
        this.context = context;
        this.indicators = context.getIndicators();
        this.userInterface = context.getUserInterface();
    }
 
    public void onAccount(IAccount account) throws JFException {
    }
 
    public void onMessage(IMessage message) throws JFException {
    }
 
    public void onStop() throws JFException {
        print("Candles counted: "+CandleCount);
    }
 
    public void onTick(Instrument instrument, ITick tick) throws JFException {
         
        if (instrument != Instrument.EURUSD)
        {
            return;
        }
    }
     
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
        if ( (instrument != Instrument.EURUSD) || (period != Period.FIVE_MINS) )
        {
            return;
        }
       
        if (isMarketClosed())
        {
            return;
        }
        /*
        if (isOffline(history.getLastTick(Instrument.EURUSD).getTime()))
        {
            return;
        }
        */
        print("Now I am in onBar! "+askBar.toString());
        CandleCount++;
    }
     
    private boolean isMarketClosed() throws JFException
    {
        long lastTickTime = history.getLastTick(Instrument.EURUSD).getTime();
        Calendar calendar = Calendar.getInstance();
        int day;
        long time;
         
        calendar.setTimeInMillis(lastTickTime);
        calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
        day = calendar.get(Calendar.DAY_OF_WEEK);
       
         
        if (day == Calendar.SATURDAY) // no trading on Saturday
        {
             print("SATURDAY tick: " + sdf.format(lastTickTime) + " calendar: " + sdf.format(calendar.getTime()) );
            return true;
        }
         
        if (day == Calendar.FRIDAY)
        {
            calendar.set(Calendar.HOUR_OF_DAY, 21);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            time = calendar.getTimeInMillis();
            print("FRIDAY tick: " + sdf.format(lastTickTime) + " calendar: " + sdf.format(calendar.getTime()) );
            if (lastTickTime > time) // No trading on Friday past 9pm
            {
                return true;
            }
        }
         
        if (day == Calendar.SUNDAY)
        {
            calendar.set(Calendar.HOUR_OF_DAY, 21);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            time = calendar.getTimeInMillis();
            print("SUNDAY tick: " + sdf.format(lastTickTime) + " calendar: " + sdf.format(calendar.getTime()) );
            if (lastTickTime < time) // No trading on Sunday before 9pm
            {
                return true;
            }
        }
         
        // if not Friday, Saturday or Sunday, market is always open
        return false;
    }
   
    /*
    private boolean isOffline(long time) throws JFException{
    Set<ITimeDomain> offlines = dataService.getOfflineTimeDomains(time - Period.WEEKLY.getInterval(), time + Period.WEEKLY.getInterval());
    for(ITimeDomain offline : offlines){
        if( time > offline.getStart() &&  time < offline.getEnd()){
            return true;
        }
    }
    return false;
    }
    */
     
    private void print(String message)
    {
        console.getOut().println(message);
    }
}


 
 Post subject: Re: Keep strategy from running when the market is closed Post rating: 0   New post Posted: Fri 20 Jul, 2012, 00:51 

User rating: 0
Joined: Thu 19 Jul, 2012, 00:32
Posts: 6
Sorry for my first reply, declaring a variable that is used really helps :oops: .

So I got the Wiki solution to compile, but the result is still the same.The print statement in line 68 is executed on Saturday 7/7/2012:


23:35:54 Now I am in onBar! {T:1341702900000(2012-07-07 23:15:00.000+0000)E:falseO:1.229C:1.229L:1.229H:1.229V0.0}
23:35:54 Now I am in onBar! {T:1341702600000(2012-07-07 23:10:00.000+0000)E:falseO:1.229C:1.229L:1.229H:1.229V0.0}
23:35:54 Now I am in onBar! {T:1341702300000(2012-07-07 23:05:00.000+0000)E:falseO:1.229C:1.229L:1.229H:1.229V0.0}
23:35:54 Now I am in onBar! {T:1341702000000(2012-07-07 23:00:00.000+0000)E:falseO:1.229C:1.229L:1.229H:1.229V0.0}
23:35:54 Now I am in onBar! {T:1341701700000(2012-07-07 22:55:00.000+0000)E:falseO:1.229C:1.229L:1.229H:1.229V0.0}
23:35:54 Now I am in onBar! {T:1341701400000(2012-07-07 22:50:00.000+0000)E:falseO:1.229C:1.229L:1.229H:1.229V0.0}
23:35:54 Now I am in onBar! {T:1341701100000(2012-07-07 22:45:00.000+0000)E:falseO:1.229C:1.229L:1.229H:1.229V0.0}
...

And on Friday 7/13/2012 after 9pm:

23:42:32 Now I am in onBar! {T:1342214400000(2012-07-13 21:20:00.000+0000)E:falseO:1.22503C:1.22503L:1.22503H:1.22503V0.0}
23:42:32 Now I am in onBar! {T:1342214100000(2012-07-13 21:15:00.000+0000)E:falseO:1.22503C:1.22503L:1.22503H:1.22503V0.0}
23:42:32 Now I am in onBar! {T:1342213800000(2012-07-13 21:10:00.000+0000)E:falseO:1.22503C:1.22503L:1.22503H:1.22503V0.0}
23:42:32 Now I am in onBar! {T:1342213500000(2012-07-13 21:05:00.000+0000)E:falseO:1.22503C:1.22503L:1.22503H:1.22503V0.0}
23:42:32 Now I am in onBar! {T:1342213200000(2012-07-13 21:00:00.000+0000)E:falseO:1.22503C:1.22503L:1.22503H:1.22503V0.0}
23:42:32 Now I am in onBar! {T:1342212900000(2012-07-13 20:55:00.000+0000)E:falseO:1.22477C:1.22503L:1.22462H:1.22504V437.47}
...

So strike question 2 from my previous reply, but question 1 remains still active:
How can the onBar() method get past the return statement in line 65 (of the latest version below) and hence to the print statement in line 68 at obvious off-line hours?

Thanks
Markus

Here the latest version of my code:
package jforex.requests;
 
import java.text.SimpleDateFormat;
import java.util.*;
 
import com.dukascopy.api.*;
 
public class TestWeekend implements IStrategy {
    private IEngine engine;
    private IConsole console;
    private IHistory history;
    private IContext context;
    private IIndicators indicators;
    private IUserInterface userInterface;
    private IDataService dataService;
     
    private int CandleCount = 0;
   
    @SuppressWarnings("serial")
    public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS") {
        {
            setTimeZone(TimeZone.getTimeZone("GMT"));
        }
    };
     
    public void onStart(IContext context) throws JFException {
        this.engine = context.getEngine();
        this.console = context.getConsole();
        this.history = context.getHistory();
        this.context = context;
        this.indicators = context.getIndicators();
        this.userInterface = context.getUserInterface();
        this.dataService = context.getDataService();
       
        print("-------------------------------------------------------------------------------------------------------------------------------");
    }
 
    public void onAccount(IAccount account) throws JFException {
    }
 
    public void onMessage(IMessage message) throws JFException {
    }
 
    public void onStop() throws JFException {
        print("Candles counted: "+CandleCount);
    }
 
    public void onTick(Instrument instrument, ITick tick) throws JFException {
         
        if (instrument != Instrument.EURUSD)
        {
            return;
        }
    }
     
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
        if ( (instrument != Instrument.EURUSD) || (period != Period.FIVE_MINS) )
        {
            return;
        }
       
        if (isOffline(history.getLastTick(Instrument.EURUSD).getTime()))
        {
           
            return;
        }
       
        print("Now I am in onBar! "+askBar.toString());
        CandleCount++;
    }
   
    private boolean isOffline(long time) throws JFException{
    Set<ITimeDomain> offlines = dataService.getOfflineTimeDomains(time - Period.WEEKLY.getInterval(), time + Period.WEEKLY.getInterval());
    for(ITimeDomain offline : offlines){
        if( time > offline.getStart() &&  time < offline.getEnd()){
            return true;
        }
    }
    return false;
    }
     
    private void print(String message)
    {
        console.getOut().println(message);
    }
}


 
 Post subject: Re: Keep strategy from running when the market is closed Post rating: 0   New post Posted: Fri 20 Jul, 2012, 01:57 
User avatar

User rating: 94
Joined: Mon 06 Feb, 2012, 12:22
Posts: 357
Location: Portugal, Castelo Branco
Hi FxJohn:

IMHO you are using not the best aproach...

I made for one of my strategy one function to avoid strategy action at defined intervals including weekends...
Is not what you want, but this may help you.

/**
     * Controls time paramenters and sets if it's possible to trade at given time paramenters
     * Also close all orders if we are outside time interval
     *
     * @param barTime
     * @param bHour
     * @param eHour
     * @param weekend
     * @return
     */
    private boolean timeAllowsTrades(long barTime, int bHour, int eHour, boolean weekend) throws JFException{
        boolean allowsTrades = false;
        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
        cal.setTimeInMillis(barTime);
        int day = cal.get(Calendar.DAY_OF_WEEK);
        int hour = cal.get(Calendar.HOUR_OF_DAY);
       
        if (hour >= bHour && hour < eHour) allowsTrades = true;
        if (weekend && (day == 1 || day == 7)) allowsTrades = false;
        if (!allowsTrades) closeAllOrders();
        return allowsTrades;
    }// End timeAllowsTrades


Trade well and prospers in your way

JL


 
 Post subject: Re: Keep strategy from running when the market is closed Post rating: 0   New post Posted: Fri 20 Jul, 2012, 12:21 

User rating: 0
Joined: Thu 19 Jul, 2012, 00:32
Posts: 6
Hi JLongo,

Thanks for your reply. Unfortunately that does not solve my problem. There are a number of ways to write a function that returns true or false under certain conditions.
My problem is in the onBar() function. Consider this code snippet (from onBar()):

if (myFunction())
{
    return;
}

print("I am in onBar()!");


Even though myFunction() obviously returns true, the onBar() function prints "I am in onBar()" in the message window. This becomes very obvious with the version the guys from Dukascopy returned on my very first question.

Any kind of help is appreciated.

Thanks
FxJohn


 
 Post subject: Re: Keep strategy from running when the market is closed Post rating: 0   New post Posted: Fri 20 Jul, 2012, 23:15 
User avatar

User rating: 94
Joined: Mon 06 Feb, 2012, 12:22
Posts: 357
Location: Portugal, Castelo Branco
Hi again:

Have you tried the inverse ?

if (!myFunction())
{
print("I am in onBar()!");
}


Some times i experience some weird beaviors, but investigating is allways programming error, not taking care of all variables or bad implementation.

i have tried the following code:

package jforex;

import java.util.*;

import com.dukascopy.api.*;

public class Strategy implements IStrategy {
    private IEngine engine;
    private IConsole console;
    private IHistory history;
    private IContext context;
    private IIndicators indicators;
    private IUserInterface userInterface;
    private int x = 0;
    private boolean test = true;
    public void onStart(IContext context) throws JFException {
        this.engine = context.getEngine();
        this.console = context.getConsole();
        this.history = context.getHistory();
        this.context = context;
        this.indicators = context.getIndicators();
        this.userInterface = context.getUserInterface();
    }

    public void onAccount(IAccount account) throws JFException {
    }

    public void onMessage(IMessage message) throws JFException {
    }

    public void onStop() throws JFException {
    }

    public void onTick(Instrument instrument, ITick tick) throws JFException {
    }
   
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
        console.getOut().println("--");
        if (test){
            x++;
            console.getOut().println("i'm inside first if - try:" + x);
            test = false;
            return;
        }
        console.getOut().println("||");
        x++;
        console.getOut().println("i'm outside if - try:" + x);
        test = true;
    }
}


And seems working as expected by the log:

23:39:31 i'm outside if - try:48
23:39:31 ||
23:39:31 --
23:39:31 i'm inside first if - try:47
23:39:31 --
23:39:31 i'm outside if - try:46
23:39:31 ||
23:39:31 --
23:39:31 i'm inside first if - try:45
23:39:31 --
23:39:21 i'm outside if - try:44
23:39:21 ||
23:39:21 --
23:39:21 i'm inside first if - try:43
23:39:21 --
23:39:21 i'm outside if - try:42
23:39:21 ||
23:39:21 --
23:39:21 i'm inside first if - try:41
23:39:21 --
23:39:11 i'm outside if - try:40
23:39:11 ||
23:39:11 --
23:39:11 i'm inside first if - try:39
23:39:11 --
23:39:11 i'm outside if - try:38
23:39:11 ||
23:39:11 --
23:39:11 i'm inside first if - try:37
23:39:11 --
23:39:01 i'm outside if - try:36
23:39:01 ||
23:39:01 --
23:39:01 i'm inside first if - try:35
23:39:01 --
23:39:01 i'm outside if - try:34
23:39:01 ||
23:39:01 --
23:39:01 i'm inside first if - try:33
23:39:01 --
23:39:01 i'm outside if - try:32
23:39:01 ||
23:39:01 --
23:39:01 i'm inside first if - try:31
23:39:01 --
23:39:01 i'm outside if - try:30
23:39:01 ||
23:39:01 --
23:39:01 i'm inside first if - try:29
23:39:01 --
23:38:51 i'm outside if - try:28
23:38:51 ||
23:38:51 --
23:38:51 i'm inside first if - try:27
23:38:51 --
23:38:51 i'm outside if - try:26
23:38:51 ||
23:38:51 --
23:38:51 i'm inside first if - try:25
23:38:51 --
23:38:41 i'm outside if - try:24
23:38:41 ||
23:38:41 --
23:38:41 i'm inside first if - try:23
23:38:41 --
23:38:41 i'm outside if - try:22
23:38:41 ||
23:38:41 --
23:38:41 i'm inside first if - try:21
23:38:41 --
23:38:31 i'm outside if - try:20
23:38:31 ||
23:38:31 --
23:38:31 i'm inside first if - try:19
23:38:31 --
23:38:31 i'm outside if - try:18
23:38:31 ||
23:38:31 --
23:38:31 i'm inside first if - try:17
23:38:31 --
23:38:21 i'm outside if - try:16
23:38:21 ||
23:38:21 --
23:38:21 i'm inside first if - try:15
23:38:21 --
23:38:21 i'm outside if - try:14
23:38:21 ||
23:38:21 --
23:38:21 i'm inside first if - try:13
23:38:21 --
23:38:11 i'm outside if - try:12
23:38:11 ||
23:38:11 --
23:38:11 i'm inside first if - try:11
23:38:11 --
23:38:11 i'm outside if - try:10
23:38:11 ||
23:38:11 --
23:38:11 i'm inside first if - try:9
23:38:11 --
23:38:01 i'm outside if - try:8
23:38:01 ||
23:38:01 --
23:38:01 i'm inside first if - try:7
23:38:01 --
23:38:01 i'm outside if - try:6
23:38:01 ||
23:38:01 --
23:38:01 i'm inside first if - try:5
23:38:01 --
23:38:01 i'm outside if - try:4
23:38:01 ||
23:38:01 --
23:38:01 i'm inside first if - try:3
23:38:01 --
23:38:01 i'm outside if - try:2
23:38:01 ||
23:38:01 --
23:38:01 i'm inside first if - try:1
23:38:01 --


You can debug the value returned by your function associating it with one boolean variable, print the value of this variable and use this variable on the if condition...

I hope that helps.

Trade well and prospers in your way

JL


 
 Post subject: Re: Keep strategy from running when the market is closed Post rating: 0   New post Posted: Sat 21 Jul, 2012, 00:02 
User avatar

User rating: 94
Joined: Mon 06 Feb, 2012, 12:22
Posts: 357
Location: Portugal, Castelo Branco
Hi fxJohn:

Maybe this code do what you want, i don't have tested it.

package JForex.strategies.examples;

import com.dukascopy.api.IAccount;
import com.dukascopy.api.IBar;
import com.dukascopy.api.IConsole;
import com.dukascopy.api.IContext;
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.Period;
import java.util.Calendar;
import java.util.TimeZone;

public class fxJohn implements IStrategy {
    private IContext myContext;
    private IConsole myConsole;
    @Override
    public void onStart(IContext context) throws JFException {
        myContext = context;
        myConsole = context.getConsole();
    }

    @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 (marketClose(askBar.getTime())){
            myConsole.getOut().println("Market is closed!");
            return;
        }
        myConsole.getOut().println("Market is open!");
    }

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

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

    @Override
    public void onStop() throws JFException {
       
    }
   
    private boolean marketClose(long time){
        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
        cal.setTimeInMillis(time);
        int day = cal.get(Calendar.DAY_OF_WEEK);
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        if (day == 7) { // saturday
            return true;
        }
        if (day == 1 && hour < 21) { // Sunday
            return true;
        }
        if (day == 6 && hour >= 21) { // Friday
            return true;
        }
        return false;
    }
}



I hope that helps

Trade well and prospers in your way.

JL


 
 Post subject: Function from Wiki does not work Post rating: 0   New post Posted: Sat 21 Jul, 2012, 00:48 

User rating: 0
Joined: Thu 19 Jul, 2012, 00:32
Posts: 6
I copied the function isOffline from https://www.dukascopy.com/wiki/#Market_Hours/Check_if_market_is_offline into a simple strategy to test it and then ran it through the historical tester.
On Staurday 7/7/2012 isOffline returned false!

2012-07-20 23:43:13 isOffline returned false
2012-07-20 23:43:13 Now I am in onBar! {T:1341703200000(2012-07-07 23:20:00.000+0000)E:falseO:1.229C:1.229L:1.229H:1.229V0.0}

Any kind of help is appreciated.

Thanks
FxJohn

Here is the strategy that produced the above output.

package jforex;

import java.text.SimpleDateFormat;
import java.util.*;

import com.dukascopy.api.*;
 
public class testIsOffline implements IStrategy {
    private IEngine engine;
    private IConsole console;
    private IHistory history;
    private IContext context;
    private IIndicators indicators;
    private IUserInterface userInterface;
    private IDataService dataService;
     
    @SuppressWarnings("serial")
    public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS") {
        {
            setTimeZone(TimeZone.getTimeZone("GMT"));
        }
    };
     
    public void onStart(IContext context) throws JFException {
        this.engine = context.getEngine();
        this.console = context.getConsole();
        this.history = context.getHistory();
        this.context = context;
        this.indicators = context.getIndicators();
        this.userInterface = context.getUserInterface();
        this.dataService = context.getDataService();
       
        print("-------------------------------------------------------------------------------------------------------------------------------");
    }
 
    public void onAccount(IAccount account) throws JFException {
    }
 
    public void onMessage(IMessage message) throws JFException {
    }
 
    public void onStop() throws JFException {
       
    }
 
    public void onTick(Instrument instrument, ITick tick) throws JFException {
       
    }
     
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
        if ( (instrument != Instrument.EURUSD) || (period != Period.FIVE_MINS) )
        {
            return;
        }
       
        boolean b = isOffline(history.getLastTick(Instrument.EURUSD).getTime());
     
        print("isOffline returned "+b);
        if (b)
        {
            return;
        }
       
        print("Now I am in onBar! "+askBar.toString());
    }
   
    private boolean isOffline(long time) throws JFException{
    Set<ITimeDomain> offlines = dataService.getOfflineTimeDomains(time - Period.WEEKLY.getInterval(), time + Period.WEEKLY.getInterval());
    for(ITimeDomain offline : offlines){
        if( time > offline.getStart() &&  time < offline.getEnd()){
            return true;
        }
    }
    return false;
    }
   
    private void print(String message)
    {
        console.getOut().println(message);
    }
   
};


 
 Post subject: Re: Function from Wiki does not work Post rating: 0   New post Posted: Sat 21 Jul, 2012, 12:01 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
The flaw is in your code, not wiki, you should compare the bar time, instead of tick time, because tick can't arrive while market is closed, hence during the market offline time the last tick will be the last tick on Friday. You can check this by changing your print statement to the following:
        print("isOffline returned "+b + " last tick: " + history.getLastTick(Instrument.EURUSD));


 
 Post subject: Re: Function from Wiki does not work Post rating: 0   New post Posted: Sat 21 Jul, 2012, 14:20 

User rating: 0
Joined: Thu 19 Jul, 2012, 00:32
Posts: 6
Yeap, that was it. Thanks a lot, great job guys. It also helps my strategy a lot knowing that onBar is called over the weekend, but not onTick.
Also thanks to you jlongo, your comments were also very helpful.

Good Pips to all of you,
FxJohn


 

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