|
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.
onBar timing question |
us_copiosus
|
Post subject: onBar timing question |
Post rating: 0
|
Posted: Tue 17 Sep, 2013, 05:20
|
|
User rating: 2
Joined: Fri 02 Mar, 2012, 22:08 Posts: 200 Location: New Zealand, Dunedin
|
The following code provides data (last six bars OHLCV) as expected BUT............. I ran the strategy just prior to 0000 hours 17th Sept 2013 GMT The data was time stamped 00:59:59 whereas I expected the time stamp to be 00:00:00 ----------------------------------------------------------------------------------- package jforex;
import java.util.logging.Level; import java.util.logging.Logger; import java.util.*; import java.text.*;
import com.dukascopy.api.*;
public class Strategy111 implements IStrategy { // Strategy base objects private IContext myContext = null; private IAccount myAccount = null; private IEngine myEngine = null; private IOrder myOrder = null; private IHistory myHistory = null; private IIndicators myIndicators = null; private IConsole myConsole = null;
List<IBar> barList;
// strategy parameters @Configurable("Currency Pair to use:") public Instrument myInstrument = Instrument.USDJPY; // Instrument object // from API @Configurable("Time frame to use:") public Period myPeriod = Period.ONE_HOUR; // Period object from API @Configurable("Position value to use:") public double myPositionValue = 1.000; @Configurable("Take Profit to use:") public int myTakeProfit = 9; @Configurable("SL/TP ratio to use:") public int mySLTPRatio = 3; @Configurable("Stop Loss to use:") public int myStopLoss = 27;
private IEngine.OrderCommand myOrderDirection = null; private boolean myCanTrade = false;
public void onStart(IContext context) throws JFException {
myContext = context; myEngine = myContext.getEngine(); myAccount = myContext.getAccount(); myHistory = myContext.getHistory(); myIndicators = myContext.getIndicators(); myConsole = context.getConsole();
Set subscribedInstruments = new HashSet(); subscribedInstruments.add(Instrument.USDJPY); myContext.setSubscribedInstruments(subscribedInstruments);
}
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 { }
/** * Executed on every bar completed * * @param instrument * - instrument for the bar received * @param period * - period of the bar received * @param askBar * - OHLC and volume for the ask bar * @param bidBar * - OHLC and volume for the bid bar * @throws JFException */ @Override public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT")); int day_of_week = calendar.get(Calendar.DAY_OF_WEEK); int hour = calendar.get(Calendar.HOUR_OF_DAY);
// if not USDJPY, then exit if (!instrument.equals(myInstrument)) return; // exit
// if period not hourly, then exit if (!period.equals(myPeriod)) return; // exit
// if hour is not 6am, midday, 6pm or midnight, then exit if (!((hour == 6) || (hour == 12) || (hour == 18) || (hour == 0))) return; // exit
// if previous period trade still open, then close it myOrder = myEngine.getOrder("MyStrategyOrder"); if (myOrder != null && myEngine.getOrders().contains(myOrder)) { myOrder.close(); myOrder.waitForUpdate(IOrder.State.CLOSED); // wait till the order // is closed myConsole.getOut().println("Order " + myOrder.getLabel() + " is closed"); } else if (myOrder == null) { myConsole.getOut().println("No order to close"); } myCanTrade = true;
// get the last six hour bars data: OHLC and volume
try {
barList = GetBars(); } catch (java.text.ParseException ex) { Logger.getLogger(Strategy111.class.getName()).log(Level.SEVERE, null, ex); }
// save the data to an array
FullData = IBarsToArray(barList);
}
public double[][] IBarsToArray(List<IBar> inputBars) { double[][] ohlcvArray = new double[5][inputBars.size()]; for (int i = 0; i < inputBars.size(); i++) { ohlcvArray[0][i] = inputBars.get(i).getOpen(); ohlcvArray[1][i] = inputBars.get(i).getHigh(); ohlcvArray[2][i] = inputBars.get(i).getLow(); ohlcvArray[3][i] = inputBars.get(i).getClose(); ohlcvArray[4][i] = inputBars.get(i).getVolume();
myConsole.getOut().println( "Open:" + ohlcvArray[0][i] + "," + "High: " + ohlcvArray[1][i] + "," + "Low: " + ohlcvArray[2][i] + "," + "Close: " + ohlcvArray[3][i] + "," + "Volume: " + ohlcvArray[4][i]);
} return ohlcvArray; }
public List<IBar> GetBars() throws java.text.ParseException, JFException { SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyy HH:mm:ss"); dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
Date dateFrom = dateFormat.parse("16/09/2013 18:00:00"); Date dateTo = dateFormat.parse("16/09/2013 23:00:00"); List<IBar> bars = myContext.getHistory().getBars(myInstrument, myPeriod, OfferSide.BID, dateFrom.getTime(), dateTo.getTime());
return bars; }
public double[][] FullData = new double[5][6];
}
|
|
|
|
 |
API Support
|
Post subject: Re: onBar timing question |
Post rating: 1
|
Posted: Tue 17 Sep, 2013, 08:21
|
|
User rating: ∞
Joined: Fri 31 Aug, 2007, 09:17 Posts: 6139
|
You should set the calendar the bar's time, i.e.,: calendar.setTimeInMillis(bidBar.getTime());
|
|
|
|
 |
us_copiosus
|
Post subject: Re: onBar timing question |
Post rating: 0
|
Posted: Tue 17 Sep, 2013, 08:33
|
|
User rating: 2
Joined: Fri 02 Mar, 2012, 22:08 Posts: 200 Location: New Zealand, Dunedin
|
|
|
|
 |
us_copiosus
|
Post subject: Re: onBar timing question |
Post rating: 0
|
Posted: Tue 17 Sep, 2013, 19:51
|
|
User rating: 2
Joined: Fri 02 Mar, 2012, 22:08 Posts: 200 Location: New Zealand, Dunedin
|
Hi Support
Did what you suggested............
Expected six bar retrieval at 17th Sep 2013 12:00:00 BUT got data at 13:00:00 instead (i.e. an hour later than required)
So still not working................
Bob M
|
|
|
|
 |
tcsabina
|
Post subject: Re: onBar timing question |
Post rating: 1
|
Posted: Tue 17 Sep, 2013, 20:46
|
|
User rating: 164
Joined: Mon 08 Oct, 2012, 10:35 Posts: 676 Location: NetherlandsNetherlands
|
Hi,
If you run this strategy in the Historical Tester with Intraday as testing period, you will see that it is calling your GetBars() function 4 times: at 0, 6, 12, and 18 hours. Where and how do you see that at 13:00 it is being 'executed' instead of 12:00?
|
|
|
|
 |
us_copiosus
|
Post subject: Re: onBar timing question |
Post rating: 0
|
Posted: Tue 17 Sep, 2013, 21:37
|
|
User rating: 2
Joined: Fri 02 Mar, 2012, 22:08 Posts: 200 Location: New Zealand, Dunedin
|
|
|
|
 |
us_copiosus
|
Post subject: Re: onBar timing question |
Post rating: 0
|
Posted: Tue 17 Sep, 2013, 21:40
|
|
User rating: 2
Joined: Fri 02 Mar, 2012, 22:08 Posts: 200 Location: New Zealand, Dunedin
|
I should have stated that I am running the strategy on a demo account in real time
Bob M
|
|
|
|
 |
tcsabina
|
Post subject: Re: onBar timing question |
Post rating: 2
|
Posted: Tue 17 Sep, 2013, 21:59
|
|
User rating: 164
Joined: Mon 08 Oct, 2012, 10:35 Posts: 676 Location: NetherlandsNetherlands
|
Hi, I've modified the strategy a bit, just the print out the hour variable, and for me it is 0, 6, 12, 18. Do I miss something? 
Attachments: |
File comment: modified to print hour
Strategy111.java [5.23 KiB]
Downloaded 183 times
|
File comment: screenshot of the historical tester
Screenshot.png [119.33 KiB]
Downloaded 599 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.
|
|
|
|
|
 |
us_copiosus
|
Post subject: Re: onBar timing question |
Post rating: 0
|
Posted: Wed 18 Sep, 2013, 20:22
|
|
User rating: 2
Joined: Fri 02 Mar, 2012, 22:08 Posts: 200 Location: New Zealand, Dunedin
|
Hi
Here is the latest ouput - still 1 hour out of phase (late) I am seeking output at 12:00:00 and 18:00:00 ------------------------------------------------ 18:59:59 sma2:9877391666666665,sma6: 99.04708333333332,sma10: 99.04330000000003 18:59:59 Open:98.836,High: 98.836,Low: 97.853,Close: 97.97,Volume: 9719.16 18:59:59 Open:98.991,High: 98.995,Low: 98.83,Close: 98.83,Volume: 3205.87 18:59:59 Open:99.05,High: 99.101,Low: 98.978,Close: 98.99,Volume: 2870.29 18:59:59 Open:98.931,High: 99.059,Low: 98.93,Close: 99.05,Volume: 3085.08 18:59:59 Open:98.901,High: 98.994,Low: 98.87,Close: 98.93,Volume: 5202.06 18:59:59 Open:98.931,High: 99.005,Low: 98.892,Close: 98.901,Volume: 4259.23 18:59:59 No order to close 18:59:59 hour: 18 12:59:59 sma2:99.06383333333332,sma6: 99.1445277777778,sma10: 99.06833333333334 12:59:59 Open:99.01,High: 99.029,Low: 98.884,Close: 98.93,Volume: 3623.33 12:59:59 Open:98.934,High: 99.043,Low: 98.933,Close: 99.011,Volume: 2553.51 12:59:59 Open:98.936,High: 98.989,Low: 98.876,Close: 98.934,Volume: 2385.97 12:59:59 Open:98.849,High: 98.944,Low: 98.835,Close: 98.937,Volume: 2951.59 12:59:59 Open:99.082,High: 99.113,Low: 98.785,Close: 98.847,Volume: 5073.27 12:59:59 Open:99.184,High: 99.217,Low: 99.068,Close: 99.079,Volume: 3177.58 12:59:59 No order to close 12:59:59 hour: 12
Bob M
|
|
|
|
 |
tcsabina
|
Post subject: Re: onBar timing question |
Post rating: 0
|
Posted: Wed 18 Sep, 2013, 21:03
|
|
User rating: 164
Joined: Mon 08 Oct, 2012, 10:35 Posts: 676 Location: NetherlandsNetherlands
|
Ahh, finally I understand what is your concern...
The timestamp on the console is the server time, which is GMT. The 'hour' that is used to trigger the bar-save is also GMT, as far as I can see.
I don't know why there is a difference...
|
|
|
|
 |
us_copiosus
|
Post subject: Re: onBar timing question |
Post rating: 0
|
Posted: Wed 18 Sep, 2013, 21:44
|
|
User rating: 2
Joined: Fri 02 Mar, 2012, 22:08 Posts: 200 Location: New Zealand, Dunedin
|
|
|
|
 |
CriticalSection
|
Post subject: Re: onBar timing question |
Post rating: 1
|
Posted: Wed 18 Sep, 2013, 23:14
|
|
User rating: 70
Joined: Sat 22 Sep, 2012, 17:43 Posts: 118 Location: Brazil, Fortaleza, Ceará
|
The fundamental thing to understand first is why and when onBar() is called. This function will be called only after the nominal period of the bar has elapsed. The nominal Period ( nP) in this case is 60 minutes. Then there are two things to consider: - <<WHAT>> bars you would like to capture
- <<WHEN>> you would like to capture them
I will demonstrate that the output above is fine and all that is left is to consider what you really want to do. 0. The time now is 12:00:00 PM GMT . 1. The time now is 12:00:00 PM GMT You start your strategy and the onStart() method is called 2. The time now is 12:00:00 PM GMT Nothing happens 3. The time now is 12:15:00 PM GMT Nothing happens 4. The time now is 12:30:00 PM GMT Nothing happens 5. The time now is 12:45:00 PM GMT Nothing happens 6. The time now is 12:59:59 PM GMT onBar() is called providing ask/bid bars stamped/commencing formation at 12:00:007. The time now is 12:59:59 PM GMT In one second new ask and bid bars will commence forming at 13:00:00 8. The time now is 12:59:59 PM GMT You read the ask/bid bar to use in your strategy setting the calendar to the bar time 12:00:009. The time now is 12:59:59 PM GMT Line 8 implies that your <<WHAT>> is 60 minute (1 Hour) ask/bid bars A. The time now is 12:59:59 PM GMT Line 8 implies that your <<WHEN>> is after they have completed - hence using onBar() vs getBar(,,,shift=0)B. The time now is 12:59:59 PM GMT Your <<WHEN>> guarantees that the time at which you get the bars will be nP minutes later than their start time C. The time now is 12:59:59 PM GMT You output the 'hour' variable you collected and see '12' D. The time now is 12:59:59 PM GMT The time you must see in the message log at this point is the time now (in blue) E. The time now is 12:59:59 PM GMT . So, Is your strategy to "Look at the last completed hour bars at the following times of the day 00:00:00, 06:00:00, 12:00:00 and 18:00:00"? What you seem to be saying you are looking for which would give u bars starting at 23:00:00, 05:00:00, 11:00:00 and 17:00:00 and ending at 23:59:59, 05:59:59, 11:59:59 and 17:59:59 Is your strategy to "Look at the hour bars for the following times after they complete 00:00:00, 06:00:00, 12:00:00 and 18:00:00"? What you have now and can only be achieved after the entire period nP spent populating the bars is over (60 minutes after start) You must agree your fundamental strategy, appreciate the role onBar() is playing within it and then you're good.
|
|
|
|
 |
us_copiosus
|
Post subject: Re: onBar timing question |
Post rating: 0
|
Posted: Wed 18 Sep, 2013, 23:28
|
|
User rating: 2
Joined: Fri 02 Mar, 2012, 22:08 Posts: 200 Location: New Zealand, Dunedin
|
So,
Is your strategy to "Look at the last completed hour bars at the following times of the day 00:00:00, 06:00:00, 12:00:00 and 18:00:00"? What you seem to be saying you are looking for which would give u bars starting at 23:00:00, 05:00:00, 11:00:00 and 17:00:00 and ending at 23:59:59, 05:59:59, 11:59:59 and 17:59:59
Is your strategy to "Look at the hour bars for the following times after they complete 00:00:00, 06:00:00, 12:00:00 and 18:00:00"? What you have now and can only be achieved after the entire period nP spent populating the bars is over -----------------------------------------------------------------
For example: At 00:00:00 I am seeking 6 completed hour bar data 1) 1800 - 1800:59:59 2) 1900 - 1900:59:59 ................... 6) 2300 - 2300:59:59
Bob M
|
|
|
|
 |
CriticalSection
|
Post subject: Re: onBar timing question |
Post rating: 1
|
Posted: Wed 18 Sep, 2013, 23:45
|
|
User rating: 70
Joined: Sat 22 Sep, 2012, 17:43 Posts: 118 Location: Brazil, Fortaleza, Ceará
|
long getStartTimeOfCurrentBar(Instrument instrument, Period period) will tell you (while you are within onBar) the start time of the current forming bar. You can do a check on this value (while in onBar) with Calendar methods until this returns the 00:00 you are looking for. At that point the ask/bid bars sent in as parameters to onBar() will be for the last hour period starting at 23:00 and completing at 23:59 ( of course a safer way in this particular example is to check that the incoming bidBar is the 23:00 bar i.e. current time >= 23:59:59) One option available then to get your 6 candles is: List<IBar> getBars(Instrument instrument, Period period, OfferSide side, Filter filter, int numberOfCandlesBefore, long time, int numberOfCandlesAfter) throws JFExceptiongetBars( myInstrument, myPeriod, ASK/BID, NONE/WEEKEND/ETC, 6, bidBar.getTime(), 0) will return to you 6 bars inclusive of the bar starting at the 'time' parameter so bars: 18:00(-18:59) 19:00(-19:59) 20:00(-20:59) 21:00(-21:59) 22:00(-22:59) 23:00(-23:59) meanwhile the current bar at 00:00 will be forming - and the time when all of this is happening will be +00:00:00
|
|
|
|
 |
us_copiosus
|
Post subject: Re: onBar timing question |
Post rating: 0
|
Posted: Thu 19 Sep, 2013, 07:15
|
|
User rating: 2
Joined: Fri 02 Mar, 2012, 22:08 Posts: 200 Location: New Zealand, Dunedin
|
Hi Critical Section Thank you for your reply I understand some of it only................... I wonder why it should be so difficult to simply get the last 6 hour bars at 4 times per day  If I take my original code and change the hour check to 5, 11, 17 or 23 what is wrong with the out then? The bars look correct and the time is 6, 12, 18 or 00 Bob M
|
|
|
|
 |
CriticalSection
|
Post subject: Re: onBar timing question |
Post rating: 0
|
Posted: Thu 19 Sep, 2013, 09:43
|
|
User rating: 70
Joined: Sat 22 Sep, 2012, 17:43 Posts: 118 Location: Brazil, Fortaleza, Ceará
|
Quote: If I take my original code and change the hour check to 5, 11, 17 or 23 what is wrong with the out then?
Nothing is wrong with the output, you now have what you are looking for (a solution to the first "Is your strategy to..." question). Quote: The bars look correct and the time is 6, 12, 18 or 00
So why are we still here? 
|
|
|
|
 |
us_copiosus
|
Post subject: Re: onBar timing question |
Post rating: 0
|
Posted: Thu 19 Sep, 2013, 09:47
|
|
User rating: 2
Joined: Fri 02 Mar, 2012, 22:08 Posts: 200 Location: New Zealand, Dunedin
|
Thank's for your patience.............. I was just checking to see if I had misunderstood anything  Bob M Dunedin New Zealand (America's Cup is coming home to New Zealand - yeah!)
|
|
|
|
 |
|
Pages: [
1
]
|
|
|
|
|