Dukascopy
 
 
Wiki JStore Search Login

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.

Backtesting. Order duplication
 Post subject: Backtesting. Order duplication Post rating: 0   New post Posted: Sun 23 Aug, 2009, 11:55 

User rating: 0
Joined: Wed 08 Jul, 2009, 00:42
Posts: 14
Within a strategy I am only interested in having one open position at a time, which under 'real-time' conditions is fine.

The problem is, when I run my strategies using the back-tester I get multiple positions. It looks like the back-tester engine is calling my onBar() mehod either asyncronously and having 'n' onBar()'s exec'ing at the same time, or it's syncronous but the IOrder.State.FILLED is delayed and the waitForUpdate() ins't doing anything because we are in simulation mode (understandable).

The steps I take to ensure I have only one position at a time are:

(On e.g. BUY signal) and provided I am not currently LONG.

1: pBuyOrder = engine.submitOrder(getLabel(instrument), instrument, IEngine.OrderCommand.BUY, iLots);

2: pBuyOrder.waitForUpdate(10000);

3: if (pBuyOrder.getState() == IOrder.State.FILLED)
{
do some stuff...
}
else
{
we have a problem...
}

Is this the prefered method to test for failure? I guess the waitForUpdate(...) does nothing in the back-tester? If so, will the order be filled instantaniously or will there be a delay either actual or artificial?

Will the backtester engine call my onBar() before it has exited?
Will the order be in a FILLED status by the time the next onBar() is called? (In real-time I use the waitForUpdate).

Once I have an open position, with each bar received I manage my position like the following snippit:

for (IOrder order : engine.getOrders(instrument))
{
bOpenOrders = true;
if (order.getState() == IOrder.State.FILLED)
{
if(order.isLong())
{
do some stuff for the long side...
}
}
} // Same for the short side...

Now, this works fine in 'real-time' and I only ever have 1 position at a time. My question is - what can I put in place to ensure all the mechanisms also work in the back-tester?

I have tried putting in semaphores or flags, but it seems this is multi-threaded (just a guess) and there doesn't seem to be a way of handling mutex rights in the dukas JForex environment and within the normal trading session this isn't an issue?

Sorry for the ramble, but if someone could point me in the right direction it would be most appreciated.

Thanks!


 
 Post subject: Re: Backtesting. Order duplication Post rating: 0   New post Posted: Mon 24 Aug, 2009, 09:20 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
All strategy methods onTick onBar onMessage etc are called in one strategy thread.

waitForUpdate() is working in tester pretty much like in real time environment, it's not waiting real 10 seconds of course, but it skips ticks for that time like real engine would do. In your case order executed by market price, so it should return immideatly

problem with your code is that order goes through two states when you submit it. First it changes it's state to OPENED which means server accepted it, and then to FILLED when server fills it. In real time environment server usually sends only FILLED status for market orders or rejects them that's why it works, but in theory it could happen that it first sends OPENED and then strategy will fail like in tester.

You could change your code like that:
pBuyOrder = engine.submitOrder(getLabel(instrument), instrument, IEngine.OrderCommand.BUY, iLots);
while (pBuyOrder.getState() == IOrder.State.CREATED || pBuyOrder.getState() == IOrder.State.OPENED) {
pBuyOrder.waitForUpdate(10000);
}
if (pBuyOrder.getState() == IOrder.State.FILLED)
{
do some stuff...
}
else
{
we have a problem...
}


You can add some check also if you want to be sure it will not turn to infinite cycle
int i = 3; //no more than three times
while ((pBuyOrder.getState() == IOrder.State.CREATED || pBuyOrder.getState() == IOrder.State.OPENED) && i > 0) {
pBuyOrder.waitForUpdate(10000);
--i;
}


Quote:
Will the backtester engine call my onBar() before it has exited?

Do you mean onStop()? If so, then yes, onStop will be called


 
 Post subject: Re: Backtesting. Order duplication Post rating: 0   New post Posted: Mon 24 Aug, 2009, 13:50 

User rating: 0
Joined: Wed 08 Jul, 2009, 00:42
Posts: 14
I added the logic as you suggested but I still get multiple orders created. The code snippet now looks like:

else if ( iSignal == SIG_SELL ) //Short iSignal == SIG_SELL &&
{
console.getOut().println("Go short");

if(positionsLong(instrument) != 0)
{
closePositionsLong(instrument);
}

pSellOrder = engine.submitOrder(getLabel(instrument), instrument, IEngine.OrderCommand.SELL, iLots);
//pSellOrder.waitForUpdate(10000);
while ((pSellOrder.getState() == IOrder.State.CREATED || pSellOrder.getState() == IOrder.State.OPENED) && i > 0)
{
pSellOrder.waitForUpdate(10000);
--i;
}
if (pSellOrder.getState() == IOrder.State.FILLED)
{
console.getOut().println("Opened a SHORT position");
bOpenPosition = true;

pSellOrder.setStopLossPrice(pSellOrder.getOpenPrice() + iStopLoss);
}
else
{
console.getOut().println("problem opening - SHORT position!!!");
pSellOrder.close();
}
}
if(positionsTotal(instrument) != 0)
{
console.getOut().println("We have " + positionsTotal(instrument) + " open orders...");
}

} catch
.
.
.
The only other section that manipulated the order contains code such as:

for (IOrder order : engine.getOrders(instrument))
{
bOpenOrders = true;
if (order.getState() == IOrder.State.FILLED)
{
counter++;
double iOpenPrice = order.getOpenPrice();
if(order.isLong())
{
.
.
.

As my order created only completes (with the modification) as a result of a successful order creation or fail. I would imagine this section is fine as the State will be FILLED - is that correct? However, even if not, this section simply manages an open order - it doesn't create any more trades.

Can you advise any other areas that may be causing this multi order issue?

Thanks again.


 
 Post subject: Re: Backtesting. Order duplication Post rating: 0   New post Posted: Mon 24 Aug, 2009, 15:09 

User rating: 0
Joined: Wed 08 Jul, 2009, 00:42
Posts: 14
I have another way I can ensure only one position so no problem. Just interesting to know why this method caused the dup.

Thanks


 
 Post subject: Re: Backtesting. Order duplication Post rating: 0   New post Posted: Mon 24 Aug, 2009, 15:56 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Maybe something in this block:
if(positionsLong(instrument) != 0)
{
closePositionsLong(instrument);
}

if order not closed then it could lead to orders duplication


 
 Post subject: Re: Backtesting. Order duplication Post rating: 0   New post Posted: Mon 24 Aug, 2009, 16:23 

User rating: 0
Joined: Wed 08 Jul, 2009, 00:42
Posts: 14
Support wrote:
Maybe something in this block:
if(positionsLong(instrument) != 0)
{
closePositionsLong(instrument);
}

if order not closed then it could lead to orders duplication


Maybe:

Here are the two func:

protected int positionsLong(Instrument instrument) throws JFException {
int counter = 0;
for (IOrder order : engine.getOrders(instrument)) {
if (order.getState() == IOrder.State.FILLED && order.isLong()) {
counter++;
}
}
return counter;
}
//-----------------------------
protected int closePositionsLong(Instrument instrument) throws JFException {
int counter = 0;
for (IOrder order : engine.getOrders(instrument)) {
if (order.getState() == IOrder.State.FILLED && order.isLong()) {
counter++;
order.close();
}
}
return counter;
}
//-----------------------

Do you see any issues with those funcs?


 
 Post subject: Re: Backtesting. Order duplication Post rating: 0   New post Posted: Tue 25 Aug, 2009, 08:06 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
order.close() sends message to the server, it doesn't close order instantly. Try to add waitForUpdate there, don't think it's the reason though. Try to add more message to the console to find the place and conditions when new order appears while previous is still open


 

Jump to:  

cron
  © 1998-2024 Dukascopy® Bank SA
On-line Currency forex trading with Swiss Forex Broker - ECN Forex Brokerage,
Managed Forex Accounts, introducing forex brokers, Currency Forex Data Feed and News
Currency Forex Trading Platform provided on-line by Dukascopy.com