Ya, same thing. Here's the output I get:
log4j:WARN No appenders could be found for logger (com.jforex.singlejartest.MyTesterMain).
log4j:WARN Please initialize the log4j system properly.
Connecting
Connected
Order placed: MyOrder
Order state: CREATED
Order state: CREATED
Order state: CREATED
Order state: CREATED
Order state: CREATED
Order state: CREATED
Order state: CREATED
Order state: CREATED
Order state: CREATED
Order state: CREATED
Order state: CREATED
Order state: CREATED
Order state: CREATED
Order state: CREATED
Strategy tester: com.dukascopy.api.JFException: Cannot close order in CLOSED or CANCELED state @ com.jforex.singlejartest.TestStrategy.onStop(TestStrategy.java:29)
Cannot close order in CLOSED or CANCELED state: com.dukascopy.api.JFException: Cannot close order in CLOSED or CANCELED state
at com.dukascopy.dds2.greed.agent.strategy.tester.ad.close(Unknown Source)
at com.jforex.singlejartest.TestStrategy.onStop(TestStrategy.java:29)
at com.dukascopy.dds2.greed.agent.strategy.tester.g.run(Unknown Source)
at java.lang.Thread.run(Thread.java:619)
Here's the code I used:
// Strategy code
package com.jforex.singlejartest;
import com.dukascopy.api.*;
import com.dukascopy.api.IEngine.OrderCommand;
public class TestStrategy implements IStrategy {
private IEngine engine;
private IOrder order;
public void onStart(IContext context) throws JFException {
this.engine = context.getEngine();
}
public void onAccount(IAccount account) throws JFException {
}
public void onMessage(IMessage message) throws JFException {
}
public void onStop() throws JFException {
if( order != null
&& !order.getState().equals ( IOrder.State.CLOSED )
&& !order.getState().equals ( IOrder.State.CANCELED ) ) {
order.close();
}
}
public void onTick(Instrument instrument, ITick tick) throws JFException {
}
public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
if ( instrument == Instrument.USDJPY && period == Period.WEEKLY ) {
if ( order == null ){
order = engine.submitOrder("MyOrder", instrument, OrderCommand.SELL, 0.1);
System.out.println("Order placed: "+order.getLabel());
} else {
System.out.println("Order state: "+order.getState());
}
}
}
}
// contains 'main', makes use of ITesterClient and TesterFactory
package com.jforex.singlejartest;
import com.jforex.singlejartest.TestStrategy;
import com.jforex.singlejartest.*;
import com.dukascopy.api.system.*;
import com.dukascopy.api.*;
import java.util.HashSet;
import java.util.Set;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.io.*;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
/**
*
* @author brian
*/
public class MyTesterMain {
private static final Logger LOGGER = LoggerFactory.getLogger ( MyTesterMain.class );
//url of the DEMO jnlp
private static String jnlpUrl = "https://www.dukascopy.com/client/demo/jclient/jforex.jnlp";
private static String userName = "***";
private static String password = "***";
private static long testPID = -1;
private static ITesterClient testClient;
public static void main ( String[] args )
throws Exception {
//get the instance of the IClient interface
testClient = com.dukascopy.api.system.TesterFactory.getDefaultInstance();
//set the listener that will receive system events
testClient.setSystemListener ( new MySystemListener ( LOGGER, testClient, userName, password ) );
LOGGER.info ( "Connecting..." );
System.out.println ( "Connecting" );
//connect to the server using jnlp, user name and password
testClient.connect ( jnlpUrl, userName, password );
//wait for it to connect
// This is probably useless because all of the logic to do the connection
// is complete by this point.
/*
for ( int i = 0; i < 10 && !testClient.isConnected(); ++i ) {
System.out.print ( "." );
Thread.sleep ( 1000 );
}
*/
if ( !testClient.isConnected() ) {
LOGGER.error ( "Unable to connect" );
System.exit ( 1 );
} else {
System.out.println ( "Connected" );
}
//subscribe to the instruments
Set<Instrument> instruments = new HashSet<Instrument>();
LOGGER.info ( "Subscribing instruments..." );
instruments.add ( Instrument.USDJPY );
testClient.setSubscribedInstruments ( instruments );
File cacheDir = new File ( "/projects/jforex/cache" );
cacheDir.mkdir();
testClient.setCacheDirectory ( cacheDir );
Calendar fromDate = new GregorianCalendar();
Calendar toDate = new GregorianCalendar();
fromDate.setTimeZone ( java.util.TimeZone.getTimeZone ( "UTC" ) );
fromDate.set ( 2010, // year
01, // month
04, // day
01, // hour
00 ); // min
toDate.setTimeZone ( java.util.TimeZone.getTimeZone ( "UTC" ) );
toDate.set ( 2010, // year
05, // month
07, // day
12, // hour
00 ); // min
testClient.setDataInterval ( Period.TEN_SECS,
OfferSide.BID,
ITesterClient.InterpolationMethod.FOUR_TICKS,
fromDate.getTimeInMillis(),
toDate.getTimeInMillis() );
testClient.setInitialDeposit ( java.util.Currency.getInstance ( "USD" ), 50000 );
testClient.setLeverage ( 100 );
testClient.setMCEquity ( 0 ); // this is a lower threshold. If the equity goes below
// this value, positions/orders are closed
// Logging stuff
testClient.setProcessingStatsEnabled ( false );
testClient.setEventLogEnabled ( false );
testClient.setGatherReportData ( false );
//start the strategy
LOGGER.info ( "Starting strategy" );
IStrategy blahstrat = new TestStrategy ();
testPID = testClient.startStrategy ( blahstrat, new MyTestProgressListener() );
// Do we just loop here for awhile, or what? Based on the documentation,
// it sounds like startStrategy spawns a new process then returns immediately.
// If main() returns, will this process exit?
}
}