|
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.
multiple strategies question |
DoubleDutch
|
Post subject: multiple strategies question |
Post rating: 0
|
Posted: Tue 06 Mar, 2012, 15:20
|
|
User rating: 0
Joined: Sat 24 Sep, 2011, 16:10 Posts: 16 Location: Netherlands, Rotterdam
|
Dear Support, Could you please explain me how to run two strategies simultaneously on one account. One strategy should not open more than 1 order. I assume the following code does that trick. But now if one strategy has opened an order the second strategy won't work. Quote: protected int Positions(Instrument instrument) throws JFException { int counter = 0; activeOrder = null; for (IOrder order : engine.getOrders(instrument)) { if ((order.getState() == IOrder.State.FILLED || order.getState() == IOrder.State.CREATED || order .getState() == IOrder.State.OPENED)) { activeOrder = order; counter++; } } return counter; I look forward to your reply.
|
|
|
|
 |
API Support
|
Post subject: Re: multiple strategies question |
Post rating: 0
|
Posted: Tue 06 Mar, 2012, 15:52
|
|
User rating: ∞
Joined: Fri 31 Aug, 2007, 09:17 Posts: 6139
|
- If you want your strategy to work with just one order then, you can always keep a reference of the order and submit order only if the reference is null, e.g.:
public class OneOrder implements IStrategy { IOrder order2; //... public void onTick(Instrument instrument, ITick tick) throws JFException { if(order2 == null){ order2 = engine.submitOrder("order2", Instrument.EURUSD, OrderCommand.BUY, 0.01); } ...
- The state check in your snippet is redundant, see:
https://www.dukascopy.com/client/javadoc/com/dukascopy/api/IEngine.html#getOrders(com.dukascopy.api.Instrument)
|
|
|
|
 |
DoubleDutch
|
Post subject: Re: multiple strategies question |
Post rating: 0
|
Posted: Wed 07 Mar, 2012, 14:40
|
|
User rating: 0
Joined: Sat 24 Sep, 2011, 16:10 Posts: 16 Location: Netherlands, Rotterdam
|
Thank you for the quick reply. It looks so easy but if I look at the strategy, that I like to use, its all very different. It's a strategy from the Strategy Contest by Flyswing. But when an order is filled or opened by another strategy it won't open any orders. Could you please adjust this for me? I tried using your instructions but I cant figure it out. package jforex;
import com.dukascopy.api.Configurable; import java.util.*; import com.dukascopy.api.*; import com.dukascopy.api.IEngine.OrderCommand; import com.dukascopy.api.IIndicators.AppliedPrice;
public class Fly implements IStrategy { private IEngine engine; private IIndicators indicators; private IConsole console; private IHistory history; private int counter = 0; private IOrder activeOrder; // this strategy works well with EURUSD, so only this pair is traded public Instrument selectedInstrument = Instrument.EURUSD; // the UT choosed may be changed, but in this cased, the TP and SL must be changed public Period selectedPeriod = Period.ONE_HOUR; @Configurable("Amount") public double theAmount = 0.01; // these SL and TP are not used public int StopLoss = 50; public int TakeProfit = 100; // these SL and TP must be backtested in demo mode @Configurable("Take profit pips") public double MyTakeProfit = 100; @Configurable("Stoploss pips") public double MyStopLoss = 50; // max slippage*/ @Configurable("Slippage") public double slippage = 2; @Configurable("EntryGap") public int EntryGAP = 2; // the RSI is calculated with 14 periods @Configurable("RSiN") public int RSiN = 14; // RSI values. Must be backtested evry month @Configurable("UPBnd") public double UPBnd = 56d; @Configurable("DOWNBnd") public double DOWNBnd = 56d; private Filter indicatorFilter = Filter.NO_FILTER; private double BUYprice = 0.0; private double BUYprice1 = 0.0; private double SELLprice = 0.0; private double SELLprice1 = 0.0; // 2 RSI are calculated private double[] RSi1; private double[] RSi2; int selctedPeriod = 0; private HashMap<String, IOrder> createdOrders = new HashMap<String, IOrder>(); private String parsedStartTime; @Override public void onStart(IContext context) throws JFException { String stoploss = "Fly" + Fly.class.getName() + "Flymar"; Scanner s = new Scanner(stoploss); while (s.hasNext()) { String selctedInstrument = s.next(); if (selctedInstrument == null) return; for (int i = 0; i < selctedInstrument.length(); i++) { if (Character.isLetter(selctedInstrument.charAt(i))) selctedPeriod++; } } engine = context.getEngine(); indicators = context.getIndicators(); console = context.getConsole(); history = context.getHistory(); context.getAccount(); Set<Instrument> subscribedInstruments = new HashSet<Instrument>(); subscribedInstruments.add(Instrument.EURUSD); context.setSubscribedInstruments(subscribedInstruments); }
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(selectedInstrument)) { return; } IBar prevBar = history.getBar(instrument, selectedPeriod, OfferSide.BID, 1); IBar prevBar1 = history.getBar(instrument, selectedPeriod, OfferSide.BID, 2); IBar prevBar2 = history.getBar(instrument, selectedPeriod, OfferSide.BID, 3); RSi1 = indicators.rsi(instrument, selectedPeriod, OfferSide.BID, AppliedPrice.CLOSE, RSiN, indicatorFilter, 2, prevBar.getTime(), 0); RSi2 = indicators.rsi(instrument, selectedPeriod, OfferSide.BID, AppliedPrice.CLOSE, RSiN, indicatorFilter, 2, prevBar1.getTime(), 0); double LOTs = theAmount; if (LOTs > 0.01) { LOTs = 0.01; } double askPrice = askbar.getClose(); double bidPrice = bidbar.getClose(); IEngine.OrderCommand OrderOp; if (Positions(instrument) > 0) { if (BUYprice > 0.0 && askPrice > BUYprice) { activeOrder.setStopLossPrice(bidPrice - (EntryGAP * instrument.getPipValue())); } if (SELLprice > 0.0 && bidPrice < SELLprice) { activeOrder.setStopLossPrice(askPrice + (EntryGAP * instrument.getPipValue())); } } if (instrument.equals(Instrument.EURUSD) && period.equals(selectedPeriod)) { if (Positions(instrument) > 0) { if (BUYprice > 0.0 && askPrice < BUYprice1) { activeOrder.setStopLossPrice(askPrice - (EntryGAP * instrument.getPipValue())); BUYprice1 = bidPrice - (MyStopLoss * instrument.getPipValue()); } if (SELLprice > 0.0 && askPrice > SELLprice1) { activeOrder.setStopLossPrice(askPrice + (EntryGAP * instrument.getPipValue())); SELLprice1 = bidPrice + (MyStopLoss * instrument.getPipValue()); } } /* conditions of entrys*/ if (selctedPeriod == 33) { if (Positions(instrument) == 0 && RSi1[1] > UPBnd && bidPrice < prevBar2.getClose() || Positions(instrument) == 0 && RSi2[1] < DOWNBnd && RSi1[1] > DOWNBnd) { OrderOp = OrderCommand.BUY; SELLprice = 0.0; BUYprice1 = bidPrice - (MyStopLoss * instrument.getPipValue()); SELLprice1 = 0.0; BUYprice = askPrice + (MyTakeProfit * instrument.getPipValue()); createdOrders.put("", engine.submitOrder( getLabel(instrument), instrument, OrderOp, LOTs, 0, slippage, prevBar.getClose() + (EntryGAP * instrument.getPipValue()) - (StopLoss * instrument.getPipValue()), prevBar.getClose() + (EntryGAP * instrument.getPipValue()) + (TakeProfit * instrument.getPipValue()))); } if (Positions(instrument) == 0 && RSi1[1] < DOWNBnd && askPrice > prevBar2.getClose() || Positions(instrument) == 0 && RSi2[1] > UPBnd && RSi1[1] < UPBnd) { OrderOp = OrderCommand.SELL; BUYprice = 0.0; SELLprice1 = bidPrice + (MyStopLoss * instrument.getPipValue()); BUYprice1 = 0.0; SELLprice = bidPrice - (MyTakeProfit * instrument.getPipValue()); createdOrders.put("", engine.submitOrder( getLabel(instrument), instrument, OrderOp, LOTs, 0, slippage, prevBar.getClose() - (EntryGAP * instrument.getPipValue()) + (StopLoss * instrument.getPipValue()), prevBar.getClose() - (EntryGAP * instrument.getPipValue()) - (TakeProfit * instrument.getPipValue()))); } } } }
protected String getLabel(Instrument instrument) { String label = instrument.name(); label = label + (counter++); label = label.toUpperCase(); return label; }
public void onMessage(IMessage message) throws JFException { }
public void onAccount(IAccount account) throws JFException { }
protected int Positions(Instrument instrument) throws JFException { int counter = 0; activeOrder = null; for (IOrder order : engine.getOrders(instrument)) { if ((order.getState() == IOrder.State.FILLED || order.getState() == IOrder.State.CREATED || order .getState() == IOrder.State.OPENED)) { activeOrder = order; counter++; } } return counter; }
public void print(String message) { console.getOut().println(message); } }
|
|
|
|
 |
API Support
|
Post subject: Re: multiple strategies question |
Post rating: 0
|
Posted: Thu 08 Mar, 2012, 10:29
|
|
User rating: ∞
Joined: Fri 31 Aug, 2007, 09:17 Posts: 6139
|
change Positions(instrument) > 0 to engine.getOrders(instrument).contains(activeOrder) and Positions(instrument) == 0 to !engine.getOrders(instrument).contains(activeOrder)
|
|
|
|
 |
DoubleDutch
|
Post subject: Re: multiple strategies question |
Post rating: 0
|
Posted: Thu 08 Mar, 2012, 14:22
|
|
User rating: 0
Joined: Sat 24 Sep, 2011, 16:10 Posts: 16 Location: Netherlands, Rotterdam
|
Thank you for the reply. But when I make the changes it just keeps making orders non-stop. I would like it to make only one order (per strategy). When I only change Positions(instrument) == 0 for !engine.getOrders(instrument).contains(activeOrder) it seems to work fine, but I guess it won't make any orders if an order is already opened or filled by another strategy, right?
|
|
|
|
 |
API Support
|
Post subject: Re: multiple strategies question |
Post rating: 0
|
Posted: Thu 08 Mar, 2012, 16:17
|
|
User rating: ∞
Joined: Fri 31 Aug, 2007, 09:17 Posts: 6139
|
DoubleDutch wrote: it seems to work fine, but I guess it won't make any orders if an order is already opened or filled by another strategy, right? No, since activeOrder gets created only from your strategy, thus, the expression is true only if there is no CREATED, OPENED or FILLED order that has been created by your strategy. Essentially, you can test that this is the case.
|
|
|
|
 |
DoubleDutch
|
Post subject: Re: multiple strategies question |
Post rating: 0
|
Posted: Thu 08 Mar, 2012, 16:33
|
|
User rating: 0
Joined: Sat 24 Sep, 2011, 16:10 Posts: 16 Location: Netherlands, Rotterdam
|
Thank you. My question now is: how do I stop the strategy from opening orders with the suggested changes? With the changes you suggested it keep opening orders even if the previous order hasn't been closed yet.
|
|
|
|
 |
API Support
|
Post subject: Re: multiple strategies question |
Post rating: 0
|
Posted: Mon 12 Mar, 2012, 10:30
|
|
User rating: ∞
Joined: Fri 31 Aug, 2007, 09:17 Posts: 6139
|
DoubleDutch wrote: My question now is: how do I stop the strategy from opening orders with the suggested changes? With the changes you suggested it keep opening orders even if the previous order hasn't been closed yet. In your previous post you claimed to have come up with the possible solution: DoubleDutch wrote: it seems to work fine, but I guess it won't make any orders if an order is already opened or filled by another strategy, right? And we already replied that another running strategy should not affect the strategy in subject.
|
|
|
|
 |
DoubleDutch
|
Post subject: Re: multiple strategies question |
Post rating: 0
|
Posted: Wed 14 Mar, 2012, 08:31
|
|
User rating: 0
Joined: Sat 24 Sep, 2011, 16:10 Posts: 16 Location: Netherlands, Rotterdam
|
Ok thank you, I misunderstood you then. Sorry to bother you about this again. But I've been testing if the strategy will open an order with the adjustments but it still doesn't work.
I opened two demo accounts. I run the strategy on both accounts with the same settings. On one account I opened a small order manually. On the first account, without any orders or positions open, the strategy opens trades normally. On the other account, with orders (sl & tp) and a position open, the strategy doesn't make a single trade.
|
|
|
|
 |
API Support
|
Post subject: Re: multiple strategies question |
Post rating: 0
|
Posted: Thu 15 Mar, 2012, 10:01
|
|
User rating: ∞
Joined: Fri 31 Aug, 2007, 09:17 Posts: 6139
|
Test the following version: Attachment:
Fly2.java [8.37 KiB]
Downloaded 277 times
If it does not work as expected, please provide a precise case - configuration settings under which the order opening is certain shortly after the strategy launch such that the strategy trades when there are no other orders and it does not when there are some other orders.
|
|
|
|
 |
|
Pages: [
1
]
|
|
|
|
|