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.

Open Bar at a specific time & trading Hours
 Post subject: Open Bar at a specific time & trading Hours Post rating: 0   New post Posted: Tue 13 Dec, 2011, 07:58 

User rating: 0
Joined: Tue 13 Dec, 2011, 07:53
Posts: 4
Location: GB
Hello,

I am new to using JFOREX and would like some assistance please. :D

Can you assist in providing code that will provide me with the open price at 14:00 GMT every day?

Is it also possible to then code for a strategy to only trade between certian hours (14:00 & 17:00)?

Thank you


 
 Post subject: Re: Open Bar at a specific time & trading Hours Post rating: 0   New post Posted: Tue 13 Dec, 2011, 08:45 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Here is sample code

    private boolean hasPrinted = false;
    public void onTick(Instrument instrument, ITick tick) throws JFException {
        if (!instrument.equals(this.instrument)) {
            return;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(tick.getTime());
        // col.setTimeInMillis(tick.askBar.getTime()); // in case you want to use this in onBar method
        int hour = cal.get(Calendar.HOUR_OF_DAY);
       
        // print open price at 14:00 GMT every day
        if (hour == 14) {
            if (!hasPrinted) {
                console.getOut().println("ask price at " + hour + ": " + tick.getAsk());
                console.getOut().println("bid price at " + hour + ": " + tick.getBid());
            }
        } else {
            hasPrinted = false;
        }
       
        // only trade between certian hours (14:00 & 17:00)
        if (hour < 14 || hour > 17) {
            return;
        }
       
        // do trade
    }


Please note that you should add the following import:
import java.util.Calendar;


 
 Post subject: Re: Open Bar at a specific time & trading Hours Post rating: 0   New post Posted: Tue 13 Dec, 2011, 10:46 

User rating: 0
Joined: Tue 13 Dec, 2011, 07:53
Posts: 4
Location: GB
Thanks for this information.

When applying this code in onbar how do i make alterations to the code below to prevent compiler error as it references to tick.

console.getOut().println("ask price at " + hour + ": " + tick.getAsk());
console.getOut().println("bid price at " + hour + ": " + tick.getBid());

Thanks again


 
 Post subject: Re: Open Bar at a specific time & trading Hours Post rating: 0   New post Posted: Tue 13 Dec, 2011, 11:45 

User rating: 0
Joined: Tue 13 Dec, 2011, 07:53
Posts: 4
Location: GB
Also, would this code be applicable when running backtests?


 
 Post subject: Re: Open Bar at a specific time & trading Hours Post rating: 0   New post Posted: Tue 13 Dec, 2011, 13:01 

User rating: 0
Joined: Tue 13 Dec, 2011, 07:53
Posts: 4
Location: GB
Sorry, so many questions but if I also wish to use the open price at 14:00 as part my strategy, how would I do this instead of using println?


 
 Post subject: Re: Open Bar at a specific time & trading Hours Post rating: 0   New post Posted: Tue 13 Dec, 2011, 17:22 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
1) The following code can be used
console.getOut().println("ask price at " + hour + ": " + askBar.getOpen());
console.getOut().println("bid price at " + hour + ": " + bidBar.getOpen() );

2) Yes, this code can be run using historical tester.
3) to use tick.getAsk() and tick.getBid() in strategy code you can define two global variables in the beginning of the strategy:
double askPrice;
double bidPrice;

Then the variables should be assigned, to do that insert the following code after println`s:
askPrice = tick.getAsk());
bidPrice = tick.getBid());

Values askPrice and bidPrice can now be used in the strategy.


 

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