|
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.
False MACD signals? |
[Kirilla]
|
Post subject: False MACD signals? |
Post rating: 0
|
Posted: Fri 11 Feb, 2011, 10:06
|
|
User rating: 5
Joined: Fri 22 Oct, 2010, 09:54 Posts: 24
|
Hi! I've made a very simple MACD strategy, but I get fake signals and different values between the macd on the chart and macd in the code. package jforex;
import java.util.*; import java.text.*;
import com.dukascopy.api.*;
public class MACDStrategy implements IStrategy { private IEngine engine; private IConsole console; private IHistory history; private IContext context; private IIndicators indicators; private IUserInterface userInterface; private final Period[] periodsa = new Period[]{ Period.FIVE_MINS, Period.TEN_MINS, Period.FIFTEEN_MINS, Period.THIRTY_MINS, Period.ONE_HOUR, Period.FOUR_HOURS }; // Stores the periods private transient final SortedSet<Period> periods = new TreeSet<Period>(Arrays.asList(periodsa));
public void onStart(IContext context) throws JFException { this.engine = context.getEngine(); this.console = context.getConsole(); this.history = context.getHistory(); this.context = context; this.indicators = context.getIndicators(); this.userInterface = context.getUserInterface(); }
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 { } public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException { if(instrument.equals(Instrument.EURUSD) && periods.contains(period)) { double[][] res = indicators.macd( instrument, period, OfferSide.BID, IIndicators.AppliedPrice.CLOSE, 12, 26, 9, Filter.ALL_FLATS, 2, bidBar.getTime(), 0); double macd_current = res[0][1]; double signal_current = res[1][1]; double macd_prev = res[0][0]; double signal_prev = res[1][0]; this.console.getOut().println("macd_prev: " + macd_prev + " signal_prev:" + signal_prev); this.console.getOut().println("macd_current: " + macd_current + " signal_current:" + signal_current); if( macd_current > signal_current && macd_prev < signal_prev) this.console.getOut().println("BUY " + period); if( macd_current < signal_current && macd_prev > signal_prev) this.console.getOut().println("SELL " + period); } } } I've cheked the Filter settings and macd offerside and applied price, but they are the same on chart and indicator too. I get fake cross signals in the code and when I checked it on the chart's macd the values are different. Another question is, should I care about threading issues inside the onBar method? Best regards, Kirilla
|
|
|
|
 |
[Frank]
|
Post subject: Re: False MACD signals? |
Post rating: 0
|
Posted: Fri 11 Feb, 2011, 11:48
|
|
User rating: 0
Joined: Thu 02 Sep, 2010, 05:01 Posts: 49
|
Hi Kirilla, I think that "macd_current" should have to have the second index equal to 0 (and so "signal_current") so try: double macd_current = res[0][0]; double signal_current = res[1][0]; double macd_prev = res[0][1]; double signal_prev = res[1][1];
Best regards, Frank
|
|
|
|
 |
[Kirilla]
|
Post subject: Re: False MACD signals? |
Post rating: 0
|
Posted: Fri 11 Feb, 2011, 12:14
|
|
User rating: 5
Joined: Fri 22 Oct, 2010, 09:54 Posts: 24
|
Hi Frank!
Thanks for your answer. I will check it again, but I think array is indexed from back, so If I want the last 3 macd values, the index of 2 will be the latest and not the index of 0.
regards, Kirilla
|
|
|
|
 |
[Frank]
|
Post subject: Re: False MACD signals? |
Post rating: 0
|
Posted: Fri 11 Feb, 2011, 13:54
|
|
User rating: 0
Joined: Thu 02 Sep, 2010, 05:01 Posts: 49
|
Hi Kirilla, If you want to semplify the thing you can use the following function: double[] macd(Instrument instrument, Period period, OfferSide side, IIndicators.AppliedPrice appliedPrice, int fastPeriod, int slowPeriod, int signalPeriod, int shift)
where shift is "shift - number of candles back starting from the current bar. 0 - current candle(currently generated from ticks), 1 - previous candle(last formed candle), 2 - current candle minus 2 candles and so on" So you can call the previous function twice, one with shift =0 and one with shift =1. I found it at https://www.dukascopy.com/wiki/index.php?title=IndicatorsBest regards, Frank
|
|
|
|
 |
[Kirilla]
|
Post subject: Re: False MACD signals? |
Post rating: 0
|
Posted: Fri 11 Feb, 2011, 14:05
|
|
User rating: 5
Joined: Fri 22 Oct, 2010, 09:54 Posts: 24
|
Hi Frank!
Thank you for your answer. Yes, I know this function, but It has not Filter options and as you mentioned I have to call it twice, which is not so good for performance reasons. I didn't test it, but I wonder if it would give different results than the other one.
best regards, Kirilla
|
|
|
|
 |
[Kirilla]
|
Post subject: Re: False MACD signals? |
Post rating: 0
|
Posted: Sun 13 Feb, 2011, 23:43
|
|
User rating: 5
Joined: Fri 22 Oct, 2010, 09:54 Posts: 24
|
Hi! I got different macd values on my indicator and the indicator on my chart. I have no idea, I've checked the following parameters. 1. Historical data 2011.01.31-2011.02.07. EURUSD Ticks. 2. Chart Preferences -> Filter All Flats and Filter All Sunday Daily Candles 3. Chart -> Bid prices 4. MACD preferences -> 12, 26, 9 apllied for CLOSED price Here is the code package jforex;
import java.util.*; import java.text.*;
import com.dukascopy.api.*;
public class MACDStrategy implements IStrategy { private IEngine engine; private IConsole console; private IHistory history; private IContext context; private IIndicators indicators; private IUserInterface userInterface; private final Period[] periodsa = new Period[]{ Period.FIFTEEN_MINS }; // Stores the periods private transient final SortedSet<Period> periods = new TreeSet<Period>(Arrays.asList(periodsa));
public void onStart(IContext context) throws JFException { this.engine = context.getEngine(); this.console = context.getConsole(); this.history = context.getHistory(); this.context = context; this.indicators = context.getIndicators(); this.userInterface = context.getUserInterface(); }
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 { } public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException { if(instrument.equals(Instrument.EURUSD) && periods.contains(period)) { double[][] res = indicators.macd( instrument, period, OfferSide.BID, IIndicators.AppliedPrice.CLOSE, 12, 26, 9, Filter.ALL_FLATS, 2, bidBar.getTime(), 0); DecimalFormat newForm = new DecimalFormat("#.#######"); double macd_current = res[0][1];
this.console.getOut().println("Time:" + new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").format(bidBar.getTime()) + " " + instrument + " " + period + " macd: " + newForm.format(macd_current)); } } } It looks like I got the same macd values from 2011.02.01 16:00 one and half day after 2011.01.31. Look the screenshot below. Ooops. I can't upload. (Bad gateway) Not a lucky day. regards, Kirilla
|
|
|
|
 |
[Kirilla]
|
Post subject: Re: False MACD signals? |
Post rating: 0
|
Posted: Mon 14 Feb, 2011, 16:11
|
|
User rating: 5
Joined: Fri 22 Oct, 2010, 09:54 Posts: 24
|
PLEASE, PLEASE HELP ME!!!
|
|
|
|
 |
[simplex]
|
Post subject: Re: False MACD signals? |
Post rating: 0
|
Posted: Mon 14 Feb, 2011, 18:37
|
|
User rating: 3
Joined: Mon 30 Aug, 2010, 13:06 Posts: 27
|
Kirilla wrote: PLEASE, PLEASE HELP ME!!! Ok  the code seems to be correct, have you considered the 1 hour difference between the the time in jforex demo and the testdata? The values should be on the chart, just one hour earlier. So for example 15:00 will be 14:00 on the chart. best, simplex
|
|
|
|
 |
[Kirilla]
|
Post subject: Re: False MACD signals? |
Post rating: 0
|
Posted: Mon 14 Feb, 2011, 20:30
|
|
User rating: 5
Joined: Fri 22 Oct, 2010, 09:54 Posts: 24
|
Hi simplex, Thank you. Yes, i had. But it seems on M15 tf after one and half day will be the same values on chart's indicator and the strategy's indicator. Attachment:
macd01.JPG [141.8 KiB]
Downloaded 515 times
and after one and half day Attachment:
macd03.JPG [171.31 KiB]
Downloaded 415 times
Look tha values at the bottom of the picture and the macd values on the chart. The refrence point is at the black vertical line. Best regards, Kirilla
|
|
|
|
 |
|
Pages: [
1
]
|
|
|
|
|