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.

Automatically trade in certain hours.
 Post subject: Automatically trade in certain hours. Post rating: 0   New post Posted: Thu 24 Jun, 2010, 08:43 

User rating: -
Is there someone can do me a favour, the attached is my EA strategy, I’d like to make it automatically trade in certain hours: GMT 06:00 – 18:00 and close any open positions after 18:00.

It’s supposed to be easy but I couldn’t figure it out, it’s highly appreciated if someone can modify if for me.

Thanks in advance.


Attachments:
MACD50.java [4.73 KiB]
Downloaded 492 times
DISCLAIMER: Dukascopy Bank SA's waiver of responsability - Documents, data or information available on this webpage may be posted by third parties without Dukascopy Bank SA being obliged to make any control on their content. Anyone accessing this webpage and downloading or otherwise making use of any document, data or information found on this webpage shall do it on his/her own risks without any recourse against Dukascopy Bank SA in relation thereto or for any consequences arising to him/her or any third party from the use and/or reliance on any document, data or information found on this webpage.
 
 Post subject: Re: Automatically trade in certain hours. Post rating: 0   New post Posted: Mon 28 Jun, 2010, 20:34 

User rating: 0
Joined: Fri 07 May, 2010, 02:59
Posts: 61
I won't do the work for you, but I'll give you a description of what I did to give you a nudge in my direction at least, if not the 'right' direction.

What I did originally was something like the following:

import java.util.*;

public class SomeClass {
//...

  public GregorianCalendar  currentDate = new GregorianCalendar();

  // constructor
  public void SomeClass () {
    currentDate.setTimeZone ( TimeZone.getTimeZone ( "UTC" ) );
  }

  public void onBar ( Instrument instrument,
                      Period period,
                      IBar askBar,
                      IBar bidBar )
          throws JFException,
                 IllegalStateException {
    if ( period == Period.TEN_SECS ) { // or whatever period you're working in
      currentDate.setTimeInMillis ( bidBar.getTime() );

      int hourOfDay = currentDate.get ( Calendar.HOUR_OF_DAY );

      if ( hourOfDay >= 6 && hourOfDay <= 18 ) {
        // continue with strategy execution
      }
    }
  }

//...
}


This is not a bad approach to solving the problem, but it can be pretty limited. It can also lead to spaghetti code because you could end up adding conditions all over the place to test things: if this time, then this, else if this time do that, etc etc.

Depending on how complex you want your strategy to be, you might consider adding a state machine. You'd still have date/time checks in there, but if you follow that design strategy correctly you'll only be checking date/time stuff under certain states, analyzing data in another, closing/opening orders in yet another, etc. It helps keep your code separate, reduces complexity, and generally makes your life simpler.

-Brian


 

Jump to:  

cron
  © 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