|
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.
Difference in calculated stochastic data vs value from chart |
[foodchaln]
|
Post subject: Difference in calculated stochastic data vs value from chart |
Post rating: 0
|
Posted: Thu 22 Jul, 2010, 18:55
|
|
User rating: 0
Joined: Mon 19 Jul, 2010, 20:32 Posts: 5
|
Hi all, I starting playing with jforex and trying to calculate slow stochastics from my own strategy. However I think the data I calculated is different from what the chart shows. I used 14, 3, 3 in both code and chart, and I disabled all the filtering in chart. I found that in calculating the daily values, although the values are different, the shape and trend are almost the same, i.e, they both go up and down around the same day. If I calculate the one minute or one hour stochastic value, the values are way off, the chart shows 90 while my code says 10, etc. Here is what my code look like: public class TestStrategy implements IStrategy { ... public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException { int fastK = 14; int slowK = 3; int slowD = 3;
if (period == Period.DAILY) { double[] slowStoch = indicators.stoch(instrument, period, OfferSide.ASK, fastK, slowK, IIndicators.MaType.SMA, slowD, IIndicators.MaType.SMA, 1); double dailyStoch = slowStoch[0]; console.getOut().println(new Date(askBar.getTime()) + ": daily: " + dailyStoch); } }
Can someone enlighten me a bit please?
|
|
|
|
 |
API Support
|
Post subject: Re: Difference in calculated stochastic data vs value from c |
Post rating: 0
|
Posted: Tue 27 Jul, 2010, 09:31
|
|
User rating: ∞
Joined: Fri 31 Aug, 2007, 09:17 Posts: 6139
|
Hi, have you considered filtering bars? i.e. public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException { if (!instrument.equals(this.instrument)) return; ... }
|
|
|
|
 |
[foodchaln]
|
Post subject: Re: Difference in calculated stochastic data vs value from c |
Post rating: 0
|
Posted: Thu 29 Jul, 2010, 03:04
|
|
User rating: 0
Joined: Mon 19 Jul, 2010, 20:32 Posts: 5
|
Yes I did. Basically I check to see if it is EURUSD before I proceed: if(instrument != Instrument.EURUSD) { console.getOut().println("Intrument not matching, do nothing: " + instrument); return; }
Any help is greatly appreciated!!!
|
|
|
|
 |
API Support
|
Post subject: Re: Difference in calculated stochastic data vs value from c |
Post rating: 0
|
Posted: Thu 29 Jul, 2010, 12:52
|
|
User rating: ∞
Joined: Fri 31 Aug, 2007, 09:17 Posts: 6139
|
Hi, Please copy the code below in the onBar() method of your strategy and have a look at the attached picture. if(!instrument.equals(Instrument.EURUSD) || period != Period.ONE_HOUR) return; double[] slowStoch = indicators.stoch(instrument, period, OfferSide.ASK, 14, 3, IIndicators.MaType.SMA, 3, IIndicators.MaType.SMA, 1); double stochValue0 = slowStoch[0]; double stochValue1 = slowStoch[1]; SimpleDateFormat format = new SimpleDateFormat("HH:mm"); format.setTimeZone(TimeZone.getTimeZone("GMT")); console.getOut().println("Stochastic value for " + format.format(askBar.getTime()) + " bar: " + stochValue0 + ", " + stochValue1);
When testing make sure EUR/USD hourly chart is opened and the appropriate offer side is selected. You can check indicator values for certain bar by moving mouse cursor over it.
Attachments: |
stoch.PNG [3.86 KiB]
Downloaded 586 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.
|
|
|
|
|
 |
[foodchaln]
|
Post subject: Re: Difference in calculated stochastic data vs value from c |
Post rating: 0
|
Posted: Fri 30 Jul, 2010, 07:36
|
|
User rating: 0
Joined: Mon 19 Jul, 2010, 20:32 Posts: 5
|
Hi, Finally those numbers match, thanks so much!! The problem is the timezone, I printed them in my local time zone which is PST and I assumed the chart is also in local time. Now I understand they are all GMT based. Another question, I wanted to calculate both daily and hourly stochastics, and I found that if I do the following, the daily number will not be accurate, I think the problem is I'm using the same instance of the stochastics indicator for both time series and it messes up the calculation: public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException { if(period == Period.ONE_HOUR) { double[] slowStoch = indicators.stoch(instrument, period, OfferSide.ASK, 14, 3, IIndicators.MaType.SMA, 3, IIndicators.MaType.SMA, 1); double stochValue0 = slowStoch[0]; double stochValue1 = slowStoch[1]; console.getOut().println("Hourly stochastic value for " + dateFormat.format(askBar.getTime()) + " bar: " + stochValue0 + ", " + stochValue1); } else if(period == Period.DAILY) { double[] slowStoch = indicators.stoch(instrument, period, OfferSide.ASK, 14, 3, IIndicators.MaType.SMA, 3, IIndicators.MaType.SMA, 1); double stochValue0 = slowStoch[0]; double stochValue1 = slowStoch[1]; console.getOut().println("---------daily stochastic value for " + dateFormat.format(askBar.getTime()) + " bar: " + stochValue0 + ", " + stochValue1); } }
The only way I can think of is to create two instances of IIndicator, such as this: public void onStart(IContext context) throws JFException { IIndicators indicators = context.getIndicators(); IIndicator dailyIndicator = indicators.getIndicator("STOCH"); IIndicator hourlyIndicator = indicators.getIndicator("STOCH"); }
And call calculateIndicator() accordingly for each time frame. Is this correct? If so, how can I call calculateIndicator() with the same effect as "double[] slowStoch = indicators.stoch(instrument, period, OfferSide.ASK, 14, 3, IIndicators.MaType.SMA, 3, IIndicators.MaType.SMA, 1);"? How can I get the result back? The API looks very sophisticated.  Really appreciate your help!!
|
|
|
|
 |
API Support
|
Post subject: Re: Difference in calculated stochastic data vs value from c |
Post rating: 0
|
Posted: Fri 30 Jul, 2010, 08:40
|
|
User rating: ∞
Joined: Fri 31 Aug, 2007, 09:17 Posts: 6139
|
Please consider the following code: if(!instrument.equals(Instrument.EURUSD) || (period != Period.ONE_HOUR && period != Period.DAILY)) return; double[] slowStoch = indicators.stoch(instrument, period, OfferSide.ASK, 14, 3, IIndicators.MaType.SMA, 3, IIndicators.MaType.SMA, 1); double stochValue0 = slowStoch[0]; double stochValue1 = slowStoch[1]; SimpleDateFormat format = new SimpleDateFormat("HH:mm"); format.setTimeZone(TimeZone.getTimeZone("GMT")); console.getOut().println(period + " stochastic value for " + format.format(askBar.getTime()) + " bar: " + stochValue0 + ", " + stochValue1);
Make sure you use the appropriate chart period when testing ( daily chart for daily stochastic values and hourly chart for hourly stochastic values respectively).
|
|
|
|
 |
[foodchaln]
|
Post subject: Re: Difference in calculated stochastic data vs value from c |
Post rating: 0
|
Posted: Fri 30 Jul, 2010, 21:23
|
|
User rating: 0
Joined: Mon 19 Jul, 2010, 20:32 Posts: 5
|
I think maybe I didn't ask the question clear enough. I wanted to calculate BOTH daily and hourly stochastic value at the same time in one method, onBar(). If I feed the data to "indicators.stoch(instrument, period, OfferSide.ASK, 14, 3, IIndicators.MaType.SMA, 3, IIndicators.MaType.SMA, 1);" with both timeframe then eventually the single instance of stoch indicator will not be accurate. My test shows that the first 24 hourly data is accurate, then feed one daily value, then another 24 hourly data, then another daily data, ....., eventually the calculation is "polluted" from the mixed data.
So I was thinking about using two instances of IIndicator and feed the data to them separately, i.e., hourly data to hourly stochIndicator and daily data to dailyIndicator. But I'm stuck with the "sophisticated" method call--calculateIndicator(). What parameters should I use to achieve the same thing as calling "indicators.stoch(instrument, period, OfferSide.ASK, 14, 3, IIndicators.MaType.SMA, 3, IIndicators.MaType.SMA, 1)"?
Thanks so much!
|
|
|
|
 |
API Support
|
Post subject: Re: Difference in calculated stochastic data vs value from c |
Post rating: 0
|
Posted: Mon 02 Aug, 2010, 07:21
|
|
User rating: ∞
Joined: Fri 31 Aug, 2007, 09:17 Posts: 6139
|
Hi, foodchaln wrote: I think maybe I didn't ask the question clear enough. I wanted to calculate BOTH daily and hourly stochastic value at the same time in one method, onBar() The above code calculates stochastic value for both hourly and daily bars just as you requested. Could you please provide the code you use for testing?
|
|
|
|
 |
[foodchaln]
|
Post subject: Re: Difference in calculated stochastic data vs value from c |
Post rating: 0
|
Posted: Tue 03 Aug, 2010, 02:05
|
|
User rating: 0
Joined: Mon 19 Jul, 2010, 20:32 Posts: 5
|
Hi, Here is my code: private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MMM-dd HH:mm");
public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException { if(!instrument.equals(Instrument.EURUSD)) return;
if(period == Period.ONE_HOUR) { double[] slowStoch = indicators.stoch(instrument, period, OfferSide.ASK, 14, 3, IIndicators.MaType.SMA, 3, IIndicators.MaType.SMA, 1); double stochValue0 = slowStoch[0]; double stochValue1 = slowStoch[1]; console.getOut().println("Hourly stochastic value for " + dateFormat.format(askBar.getTime()) + " bar: " + stochValue0 + ", " + stochValue1); } else if(period == Period.DAILY) { double[] slowStoch = indicators.stoch(instrument, period, OfferSide.ASK, 14, 3, IIndicators.MaType.SMA, 3, IIndicators.MaType.SMA, 1); double stochValue0 = slowStoch[0]; double stochValue1 = slowStoch[1]; console.getOut().println("---------daily stochastic value for " + dateFormat.format(askBar.getTime()) + " bar: " + stochValue0 + ", " + stochValue1); } }
For some reason I ran the above code again and the data all look fine and matched. I think maybe some configuration of the chart didn't match the code. Anyway, this support forum is very helpful and I'm glad I found Dukascopy!
|
|
|
|
 |
|
Pages: [
1
]
|
|
|
|
|