|
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.
It is valid time JForex function. |
Pirate
|
Post subject: It is valid time JForex function. |
Post rating: 0
|
Posted: Mon 15 Aug, 2011, 06:10
|
|
User rating: 0
Joined: Sat 06 Aug, 2011, 23:17 Posts: 27 Location: Russian Federation,
|
Hi all, check this function for error: import java.text.SimpleDateFormat; import java.util.TimeZone;
....
@Configurable("GMT") public String GMT = "+0";
....
TimeZone serverTimeZone; SimpleDateFormat sdf;
.....
public void onStart(IContext context) throws JFException {
this.context = context; this.engine = context.getEngine(); this.history = context.getHistory(); this.indicators = context.getIndicators(); this.serverTimeZone = TimeZone.getTimeZone("GMT" + GMT);
.....
if (isValidTime (18, 0, 10, 0)) ... Open Order rules (from 18:00 to 10.00 server time)
//+--------------------------------------------------------------------------------------------------------------+ //| isValidTime. Функция проверки торгового канала. //| Если время подходит для торговли, функция даёт разрешение на открытие. //+--------------------------------------------------------------------------------------------------------------+ //| Параметры: //| hb - часы времени начала канала //| mb - минуты времени начала канала //| he - часы времени окончания канала //| me - минуты времени окончания канала //+--------------------------------------------------------------------------------------------------------------+ private boolean isValidTime(int hb, int mb, int he, int me) throws JFException { //+--------------------------------------------------------------------------------------------------------------+ //--- Приводим временной канал к виду String String startTime = hb + ":" + mb; String endTime = he + ":" + me; long tickTime = timeCurrent(); //--- Делаем реплейс String parsedStartTime = startTime.replace(":", ""); String parsedEndTime = endTime.replace(":", "");
//--- Переводим милисекунды в sdf формат String formattedTickTime = sdf.format(tickTime); formattedTickTime = formattedTickTime.replace(":", ""); //--- int tickTimeValue = Integer.parseInt(formattedTickTime); int startTimeValue = Integer.parseInt(parsedStartTime); int endTimeValue = Integer.parseInt(parsedEndTime); //--- Если время начала меньше времени окончания, то if (startTimeValue < endTimeValue){ if ((tickTimeValue > startTimeValue) && (tickTimeValue < endTimeValue)){ return true; } //--- Если же время начала больше времени окончания, то } else { int tmpTimeValue = startTimeValue; startTimeValue = endTimeValue; endTimeValue = tmpTimeValue; if ((tickTimeValue < startTimeValue) || (tickTimeValue >= endTimeValue)) { return true; } } //--- return false; }
//+--------------------------------------------------------------------------------------------------------------+ //| timeCurrent. Текущее время в миллисекундах. //+--------------------------------------------------------------------------------------------------------------+ public long timeCurrent() throws JFException{ //+--------------------------------------------------------------------------------------------------------------+
return history.getLastTick(instrument).getTime(); }
This function is true?
|
|
|
|
 |
API Support
|
Post subject: Re: It is valid time JForex function. |
Post rating: 0
|
Posted: Mon 15 Aug, 2011, 09:22
|
|
User rating: ∞
Joined: Fri 31 Aug, 2007, 09:17 Posts: 6139
|
There are java libraries that deal with such cases. You can either use SimpleDateFormat or Calendar. For example: package jforex.test;
import com.dukascopy.api.*;
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.TimeZone;
/** * The strategy checks on every 10 sec bar if the last tick was within user-defined market hours. * Two approaches get used - string operations with Simple data format and use of Calendar. * */ public class CheckMarketHours implements IStrategy { private IConsole console; private IHistory history; private Period period = Period.TEN_SECS; private Instrument instrument = Instrument.EURUSD;
private SimpleDateFormat localSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); private SimpleDateFormat gmtSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @Override public void onStart(IContext context) throws JFException { console = context.getConsole(); history = context.getHistory();
gmtSdf.setTimeZone(TimeZone.getTimeZone("GMT")); } //use of string operations private boolean isValidTimeFromSdf(int fromHour, int fromMin, int toHour, int toMin) throws JFException {
long lastTickTime = history.getLastTick(instrument).getTime(); //you want to work with the date of the last tick - in a case you are back-testing String fromStr = localSdf.format(lastTickTime).substring(0, 11) + String.valueOf(fromHour)+":"+String.valueOf(fromMin) + ":00"; String toStr = localSdf.format(lastTickTime).substring(0, 11) + String.valueOf(toHour)+":"+String.valueOf(toMin) + ":00"; try { long from = localSdf.parse(fromStr).getTime(); long to = localSdf.parse(toStr).getTime(); print("sdf: " + gmtSdf.format(from) + " - " + gmtSdf.format(to) + " last tick: " + gmtSdf.format(lastTickTime)); return lastTickTime > from && lastTickTime < to; } catch (ParseException e) { e.printStackTrace(); } return false; } //use of calendar private boolean isValidTimeFromCalendar(int fromHour, int fromMin, int toHour, int toMin) throws JFException { long lastTickTime = history.getLastTick(instrument).getTime(); Calendar calendar = Calendar.getInstance(); //you want to work with the date of the last tick - in a case you are back-testing calendar.setTimeInMillis(lastTickTime); calendar.set(Calendar.HOUR, fromHour); calendar.set(Calendar.MINUTE, fromMin); calendar.set(Calendar.SECOND, 0); long from = calendar.getTimeInMillis(); calendar.setTimeInMillis(lastTickTime); calendar.set(Calendar.HOUR, toHour); calendar.set(Calendar.MINUTE, toMin); calendar.set(Calendar.SECOND, 0); long to = calendar.getTimeInMillis(); print("calendar: " + gmtSdf.format(from) + " - " + gmtSdf.format(to) + " last tick: " + gmtSdf.format(lastTickTime)); return lastTickTime > from && lastTickTime < to; }
@Override public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException { if (period != this.period || instrument != this.instrument) return;
print ( "Is valid time? SimpleDateFormat: " + isValidTimeFromSdf (10, 0, 18, 0) + ". Calendar: " + isValidTimeFromCalendar(10, 0, 18, 0)); }
private void print(Object o) { console.getOut().println(o); }
@Override public void onTick(Instrument instrument, ITick tick) throws JFException { } @Override public void onMessage(IMessage message) throws JFException { } @Override public void onAccount(IAccount account) throws JFException { } @Override public void onStop() throws JFException { }
}
|
|
|
|
 |
Pirate
|
Post subject: Re: It is valid time JForex function. |
Post rating: 0
|
Posted: Thu 18 Aug, 2011, 03:08
|
|
User rating: 0
Joined: Sat 06 Aug, 2011, 23:17 Posts: 27 Location: Russian Federation,
|
If i set isValidTimeFromCalendar (10, 0, 12, 0) expert open order in time interval from 7:00 to 9:00.
Why? This function use server or local time (time from my pc)? I need server dukascopy time.
|
|
|
|
 |
API Support
|
Post subject: Re: It is valid time JForex function. |
Post rating: 0
|
Posted: Thu 18 Aug, 2011, 07:12
|
|
User rating: ∞
Joined: Fri 31 Aug, 2007, 09:17 Posts: 6139
|
Pirate wrote: If i set isValidTimeFromCalendar (10, 0, 12, 0) expert open order in time interval from 7:00 to 9:00.
Why? Because the method assumes that you pass the local time to it. Pirate wrote: I need server dukascopy time. Consider the following adjusted strategy: viewtopic.php?f=31&t=42386
|
|
|
|
 |
Pirate
|
Post subject: Re: It is valid time JForex function. |
Post rating: 0
|
Posted: Mon 22 Aug, 2011, 10:59
|
|
User rating: 0
Joined: Sat 06 Aug, 2011, 23:17 Posts: 27 Location: Russian Federation,
|
Thanks for help! Realy, this new function is work on server time.
|
|
|
|
 |
|
Pages: [
1
]
|
|
|
|
|