|
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.
Indicators with different timeframes |
gdobson
|
Post subject: Indicators with different timeframes |
Post rating: 0
|
Posted: Mon 24 Oct, 2011, 09:50
|
|
User rating: 0
Joined: Mon 24 Oct, 2011, 09:40 Posts: 5 Location: GB
|
Hello Dukascopy team,
I would like to run an indicator within the same strategy but to produce results for different time frames.
I have currently coded the following:
double[][] stoch = indicators.stoch(instrument, period. FIVE_MINS, OfferSide.BID, 5, 3, MaType.SMA, 3, MaType.SMA, 2, 1);
double[][] stoch = indicators.stoch(instrument, period. FOUR_HOURS, OfferSide.BID, 5, 3, MaType.SMA, 3, MaType.SMA, 2, 1);
However, when I process this nothing helps. May you please assist?
|
|
|
|
 |
API Support
|
Post subject: Re: Indicators with different timeframes |
Post rating: 0
|
Posted: Mon 24 Oct, 2011, 11:37
|
|
User rating: ∞
Joined: Fri 31 Aug, 2007, 09:17 Posts: 6139
|
gdobson wrote: However, when I process this nothing helps. May you please assist? The indicator calls look ok. Please specify what are you trying to achieve with the values (i.e. how you want to use them) and from which methods you call them.
|
|
|
|
 |
gdobson
|
Post subject: Re: Indicators with different timeframes |
Post rating: 0
|
Posted: Mon 24 Oct, 2011, 12:08
|
|
User rating: 0
Joined: Mon 24 Oct, 2011, 09:40 Posts: 5 Location: GB
|
I wish to use the values on four hours to determine market bias hen use the five min chart for entry. I call the method from onBar and I use the values as a strategy for entering trades but the trades fail to open. However when I run the indicators on the same time frame throughout without using a higher time frame for bias the trades execute fine. Please see coding below: package strategy; import com.dukascopy.api.Configurable; import com.dukascopy.api.IEngine; import com.dukascopy.api.IAccount; import com.dukascopy.api.IConsole; import com.dukascopy.api.IContext; import com.dukascopy.api.IIndicators; import com.dukascopy.api.IMessage; import com.dukascopy.api.IOrder; import com.dukascopy.api.IStrategy; import com.dukascopy.api.ITick; import com.dukascopy.api.Instrument; import com.dukascopy.api.JFException; import com.dukascopy.api.OfferSide; import com.dukascopy.api.IBar; import com.dukascopy.api.Period; import com.dukascopy.api.IEngine.OrderCommand; import com.dukascopy.api.IIndicators.MaType; import com.dukascopy.api.IHistory; import com.dukascopy.api.IIndicators.AppliedPrice;
public class StochV6 implements IStrategy { private IEngine engine; private IIndicators indicators; private IConsole console; private IHistory history; private IBar bar;
//Configurable parameters @Configurable("Amount") public double amount = 0.01; @Configurable("Period") public Period fixedPeriod = Period.ONE_MIN; @Configurable("Instrument") public Instrument selectedInstrument = Instrument.EURUSD;
public void onStart(IContext context) throws JFException { engine = context.getEngine(); indicators = context.getIndicators(); console = context.getConsole(); history = context.getHistory (); } public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException { if (selectedInstrument.equals(instrument) && period.equals(fixedPeriod)) {
double[][] stoch = indicators.stoch(instrument, period. FIVE_MINS, OfferSide.BID, 5, 3, MaType.SMA, 3, MaType.SMA, 2, 1);
double[][] stoch4 = indicators.stoch(instrument, period. FOUR_HOURS, OfferSide.BID, 5, 3, MaType.SMA, 3, MaType.SMA, 2, 1);
createOrderOnAll(instrument, bidBar, stoch, stoch4);
} }
private void createOrderOnAll(Instrument instrument, IBar bidBar, double[][]stoch, double[][]stoch4) throws JFException { OrderCommand orderCommand;
if (stoch[1][0] > stoch[0][0] && stoch4[1][1] < stoch4[0][1]); orderCommand = OrderCommand.BUY;
} else if (stoch[1][0] < stoch[0][0] && stoch4[1][1] > stoch4[0][1] orderCommand = OrderCommand.SELL;
} }
Many thanks
|
|
|
|
 |
API Support
|
Post subject: Re: Indicators with different timeframes |
Post rating: 0
|
Posted: Mon 24 Oct, 2011, 12:25
|
|
User rating: ∞
Joined: Fri 31 Aug, 2007, 09:17 Posts: 6139
|
You are using the wrong method calls, which causes the exception: 11:21:27 Strategy tester: com.dukascopy.api.JFException: Interval from [1970.01.01 00:00:00 002] to [1970.01.01 00:00:00 001] GMT is not valid @ jforex.strategies.StochV6.onBar(StochV6.java:49)see Stochastic indicator usage here: https://www.dukascopy.com/wiki/index.php ... Stochastic
|
|
|
|
 |
gdobson
|
Post subject: Re: Indicators with different timeframes |
Post rating: 0
|
Posted: Mon 24 Oct, 2011, 12:57
|
|
User rating: 0
Joined: Mon 24 Oct, 2011, 09:40 Posts: 5 Location: GB
|
Hello, Thank you for your response. I have made adjustments as you stated above but the trades do not execute, please see full code below: Many thanks package strategy;
import com.dukascopy.api.Configurable; import com.dukascopy.api.IEngine; import com.dukascopy.api.IAccount; import com.dukascopy.api.IConsole; import com.dukascopy.api.IContext; import com.dukascopy.api.IIndicators; import com.dukascopy.api.IMessage; import com.dukascopy.api.IOrder; import com.dukascopy.api.IStrategy; import com.dukascopy.api.ITick; import com.dukascopy.api.Instrument; import com.dukascopy.api.JFException; import com.dukascopy.api.OfferSide; import com.dukascopy.api.IBar; import com.dukascopy.api.Period; import com.dukascopy.api.IEngine.OrderCommand; import com.dukascopy.api.IIndicators.MaType; import com.dukascopy.api.IHistory;
public class Stochastic implements IStrategy { private IEngine engine; private IIndicators indicators; private IConsole console; private IHistory history; private IBar bar;
@Configurable("Amount") public double amount = 0.2; @Configurable("Period") public Period fixedPeriod = Period.ONE_MIN; @Configurable("Instrument") public Instrument selectedInstrument = Instrument.EURUSD;
private OfferSide side = OfferSide.BID; private int fastKPeriod = 8; private MaType slowDMaType = MaType.SMA; private int slowKPeriod = 3; private MaType slowKMaType = MaType.SMA; private int slowDPeriod = 3; private int shift = 0; private int counter = 0;
public void onStart(IContext context) throws JFException { engine = context.getEngine(); indicators = context.getIndicators(); console = context.getConsole(); history = context.getHistory (); context.getChart(selectedInstrument).addIndicator(indicators.getIndicator("STOCH")); }
public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException { if (selectedInstrument.equals(instrument) && period.equals(fixedPeriod)) { long lastBarTime = history.getBar(instrument, period, OfferSide.BID,1).getTime(); long nextToLastBarTime = history.getBar(instrument, period, OfferSide.BID, 2).getTime(); double[][] stoch = indicators.stoch(instrument, period, OfferSide.BID, fastKPeriod, slowKPeriod, slowKMaType, slowDPeriod, slowDMaType, nextToLastBarTime, lastBarTime); double[][] stoch240 = indicators.stoch(instrument, period.THIRTY_MINS, OfferSide.BID, fastKPeriod, slowKPeriod, slowKMaType, slowDPeriod, slowDMaType, nextToLastBarTime, lastBarTime); createOrderOnStochastic(instrument, bidBar, stoch, stoch240 ); } }
private void createOrderOnStochastic(Instrument instrument, IBar bidBar, double[][]stoch, double[][]stoch240 ) throws JFException { OrderCommand orderCommand; if (stoch[1][0] > stoch[0][0] && stoch[1][1] < stoch[0][1] && stoch240[0][0] > stoch240[1][0] && stoch240[0][1] > stoch240[1][1]) { orderCommand = OrderCommand.BUY; closeOppositeIfExist(orderCommand); createOrder(instrument, bidBar, orderCommand); } else if (stoch[0][0] > stoch[1][0] && stoch[1][1] > stoch[0][1] && stoch240[1][0] > stoch240[0][0] && stoch240[1][1] > stoch240[0][1]) { orderCommand = OrderCommand.SELL; closeOppositeIfExist(orderCommand); createOrder(instrument, bidBar, orderCommand); } }
private void closeOppositeIfExist(OrderCommand command) throws JFException { if (engine.getOrders().size() == 0) { return; } for (IOrder order: engine.getOrders()) { if (!order.getOrderCommand().equals(command)) { order.close(); } } }
private void createOrder(Instrument instrument, IBar bidBar, OrderCommand orderCommand) throws JFException { if (engine.getOrders().size() > 0) { return; } engine.submitOrder(getLabel(instrument), instrument, orderCommand, amount); }
public void onAccount(IAccount account) throws JFException { }
public void onMessage(IMessage message) throws JFException {
}
public void onStop() throws JFException { for (IOrder order : engine.getOrders()) { order.close(); } } public void onTick(Instrument instrument, ITick tick) throws JFException { }
public void print(String string) { console.getOut().println(string); } protected String getLabel(Instrument instrument) { String label = instrument.name(); label = label + (counter ++); label = label.toUpperCase(); return label; } }
|
|
|
|
 |
API Support
|
Post subject: Re: Indicators with different timeframes |
Post rating: 0
|
Posted: Mon 24 Oct, 2011, 16:35
|
|
User rating: ∞
Joined: Fri 31 Aug, 2007, 09:17 Posts: 6139
|
Start and end times should be divisible by period's interval. Hence, the onBar method should be changed to the following: public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException { if (selectedInstrument.equals(instrument) && period.equals(fixedPeriod)) { long lastBarTime = history.getBar(instrument, period, OfferSide.BID, 1).getTime(); long nextToLastBarTime = history.getBar(instrument, period, OfferSide.BID, 2).getTime();
double[][] stoch = indicators.stoch(instrument, period, OfferSide.BID, fastKPeriod, slowKPeriod, slowKMaType, slowDPeriod, slowDMaType, nextToLastBarTime, lastBarTime); lastBarTime = history.getBar(instrument, Period.THIRTY_MINS, OfferSide.BID, 1).getTime(); nextToLastBarTime = history.getBar(instrument, Period.THIRTY_MINS, OfferSide.BID, 2).getTime(); double[][] stoch240 = indicators.stoch(instrument, Period.THIRTY_MINS, OfferSide.BID, fastKPeriod, slowKPeriod, slowKMaType, slowDPeriod, slowDMaType, nextToLastBarTime, lastBarTime); createOrderOnStochastic(instrument, bidBar, stoch, stoch240); } }
|
|
|
|
 |
|
Pages: [
1
]
|
|
|
|
|