I ran into the same problem when I started using JBT and wanted a way to
automate the backtesting of a strategy, so I didn't have to do all that
clicking.  I ended up creating a couple of classes that let me bypass the UI
and backtest a strategy and return the results.  This way I could run it
from a command line or integrate it with another program.  This also gives
you a way to backtest multiple strategies (one after another) and return the
results to compare them.

Here is the main class that does the work.  I haven't used it since early
July, so it has not been updated to work with the latest versions.

public class MyBackTester {
    private final Strategy strategy;
    private final BackTestFileReader backTestFileReader;

    public MyBackTester(Strategy strategy, BackTestFileReader
backTestFileReader) {
        this.strategy = strategy;
        this.backTestFileReader = backTestFileReader;

    }
     public void execute() throws JBookTraderException {
        MarketBook marketBook = strategy.getMarketBook();
        PositionManager positionManager = strategy.getPositionManager();
        PerformanceManager performanceManager =
strategy.getPerformanceManager();
        TradingSchedule tradingSchedule = strategy.getTradingSchedule();

        long marketDepthCounter = 0;
        LinkedList<MarketDepth> marketDepths = backTestFileReader.getAll();
        int size = marketDepths.size();
        for (MarketDepth marketDepth : marketDepths) {
            marketDepthCounter++;
            marketBook.add(marketDepth);
            performanceManager.update(marketDepth.getMidPrice(),
positionManager.getPosition());
            long instant = marketBook.getLastMarketDepth().getTime();
            strategy.setTime(instant);
            strategy.updateIndicators();
            if (strategy.hasValidIndicators()) {
                strategy.onBookChange();
            }

            if (!tradingSchedule.contains(instant)) {
                strategy.closePosition();// force flat position
            }

            positionManager.trade();
            if (marketDepthCounter % 1000 == 0) {
                System.out.print("*");
            }
        }

        // go flat at the end of the test period to finalize the run
        strategy.closePosition();
        marketBook.getAll().clear();
        marketBook.reset();
        marketBook.clear();
        strategy.getIndicators().clear();
        positionManager.trade();
        strategy.setIsActive(false);
        Dispatcher.fireModelChanged(ModelListener.Event.StrategyUpdate,
strategy);
    }
}


So basically, you save the strategy text file to the strategy source folder,
then you do an inplace compile and save the class file to the strategy class
file folder.  Then you do something like this.

MyBackTester backTester = new MyBackTester(strategy, backTestFileReader);
backTester.execute();

netProfit = strategy.getPerformanceManager().getNetProfit();
numTrades = strategy.getPerformanceManager().getTrades();
maxDD = strategy.getPerformanceManager().getMaxDrawdown();

There's a few more little details that need to be set, but that gives you
the basic idea.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"JBookTrader" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/jbooktrader?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to