|
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.
Trying to implement a strategy.... |
werenothere23
|
Post subject: Trying to implement a strategy.... |
Post rating: 0
|
Posted: Thu 04 Nov, 2010, 07:50
|
|
User rating: -
|
Hello, I am trying to implement a basic Moving Average Crossover Strategy. I was able to download and compile a sample MA strategy that was provided in a previous post, but the example does not execute orders. Would someone be able to modify this code so it may execute orders? /** * MA Cross * * @author defc0n1 * @version 0.1 */ package jforex;
import java.util.concurrent.TimeUnit;
import com.dukascopy.api.*; import com.dukascopy.api.IEngine.OrderCommand; import com.dukascopy.api.IIndicators.AppliedPrice; import com.dukascopy.api.IIndicators.MaType;
public class MaCross implements IStrategy { @Configurable("Instrument") public Instrument instrument = Instrument.EURUSD; @Configurable("Amount") public double amount = 0.25; @Configurable("Slippage") public double slippage = 5; @Configurable("Time frame") public Period timeFrame = Period.TEN_MINS; @Configurable("MA1 type") public MaType ma1Type = MaType.SMA; @Configurable("MA1 time period") public int ma1TimePeriod = 20; @Configurable("MA2 type") public MaType ma2Type = MaType.SMA; @Configurable("MA2 time period") public int ma2TimePeriod = 50; @Configurable("MA filter") public Filter maFilter = Filter.WEEKENDS; @Configurable("Offer side") public OfferSide offerSide = OfferSide.ASK; @Configurable("Applied price") public AppliedPrice appliedPrice = AppliedPrice.CLOSE;
private IEngine engine; private IConsole console; private IHistory history; private IContext context; private IIndicators indicators; private IUserInterface userInterface; private IOrder order = null; 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(this.instrument) || period != this.timeFrame) return; double MaFast ; double MaFastprev; double MaFastnext; double MaSlow ; double MaSlowprev ; double MaSlownext; String EntryMa = "false" ; // previous bar int shift2 = 2 ; //actual bar int shift1 = 1 ; //next bar int shift0 = 0 ; MaFast = indicators.ma(this.instrument,this.timeFrame,this.offerSide,this.appliedPrice,this.ma1TimePeriod,this.ma1Type,shift1); MaFastprev= indicators.ma(this.instrument,this.timeFrame,this.offerSide,this.appliedPrice,this.ma1TimePeriod,this.ma1Type,shift2); MaFastnext = indicators.ma(this.instrument,this.timeFrame,this.offerSide,this.appliedPrice,this.ma1TimePeriod,this.ma1Type,shift0); MaSlow = indicators.ma(this.instrument,this.timeFrame,this.offerSide,this.appliedPrice,this.ma2TimePeriod,this.ma2Type,shift1); MaSlowprev = indicators.ma(this.instrument,this.timeFrame,this.offerSide,this.appliedPrice,this.ma2TimePeriod,this.ma2Type,shift2); MaSlownext = indicators.ma(this.instrument,this.timeFrame,this.offerSide,this.appliedPrice,this.ma2TimePeriod,this.ma2Type,shift0); if (MaFast > MaSlow && MaFastprev < MaSlowprev && MaFastnext > MaSlownext) { engine.submitOrder(OrderCommand.BUY, this.instrument, orderCmd, amount, 0, 20, stopLossPrice, takeProfitPrice); console.getOut().println("Long Entry"); } else if (MaSlow > MaFast && MaSlowprev < MaFastprev && MaSlownext >MaFastnext){ engine.submitOrder(OrderCommand.SELL, this.instrument, orderCmd, amount, 0, 20, stopLossPrice, takeProfitPrice); console.getOut().println("Short Entry"); } } } Thank you!
|
|
|
|
 |
[astro orbitor]
|
Post subject: Re: Trying to implement a strategy.... |
Post rating: 0
|
Posted: Fri 05 Nov, 2010, 10:21
|
|
User rating: 0
Joined: Tue 27 Jul, 2010, 20:57 Posts: 49
|
You are "trying"? You have to have the latest API documentation, and then fill in the parameters according to the API. The action happens here: if (MaFast > MaSlow && MaFastprev < MaSlowprev && MaFastnext > MaSlownext) { engine.submitOrder(OrderCommand.BUY, this.instrument, orderCmd, amount, 0, 20, stopLossPrice, takeProfitPrice); console.getOut().println("Long Entry"); } else if (MaSlow > MaFast && MaSlowprev < MaFastprev && MaSlownext >MaFastnext){ engine.submitOrder(OrderCommand.SELL, this.instrument, orderCmd, amount, 0, 20, stopLossPrice, takeProfitPrice); console.getOut().println("Short Entry"); }
Choose an IEngine.submitOrder() that fits your needs and fill in the required parameters.. Example: a buy @market order for 10000 units of EURUSD, with slippage of max 5 pips: IEngine.submitOrder("market buy", Instrument.EURUSD, IEngine.OrderCommand.BUY, 0.01, 0, 5)
|
|
|
|
 |
[traderworld]
|
Post subject: Re: Trying to implement a strategy.... |
Post rating: 0
|
Posted: Wed 01 Dec, 2010, 13:27
|
|
User rating: 0
Joined: Wed 01 Dec, 2010, 08:23 Posts: 1
|
astro orbitor wrote: You are "trying"? You have to have the latest API documentation, and then fill in the parameters according to the API. The action happens here: if (MaFast > MaSlow && MaFastprev < MaSlowprev && MaFastnext > MaSlownext) { engine.submitOrder(OrderCommand.BUY, this.instrument, orderCmd, amount, 0, 20, stopLossPrice, takeProfitPrice); console.getOut().println("Long Entry"); } else if (MaSlow > MaFast && MaSlowprev < MaFastprev && MaSlownext >MaFastnext){ engine.submitOrder(OrderCommand.SELL, this.instrument, orderCmd, amount, 0, 20, stopLossPrice, takeProfitPrice); console.getOut().println("Short Entry"); }
Choose an IEngine.submitOrder() that fits your needs and fill in the required parameters.. Example: a buy @market order for 10000 units of EURUSD, with slippage of max 5 pips: IEngine.submitOrder("market buy", Instrument.EURUSD, IEngine.OrderCommand.BUY, 0.01, 0, 5) com.dukascopy.api.JFException: Label inconsistent (Label must not be null, longer than 64 symbols, contain restricted symbols or start with number) @ jforex
|
|
|
|
 |
|
Pages: [
1
]
|
|
|
|
|