<commit_before>package com.jbooktrader.platform.startup;

import com.birosoft.liquid.*;
import com.jbooktrader.platform.backtest.CommandLineBackTester;
import com.jbooktrader.platform.model.*;
import com.jbooktrader.platform.model.Dispatcher.Mode;
import com.jbooktrader.platform.optimizer.CommandLineOptimizer;
import com.jbooktrader.platform.optimizer.PerformanceMetric;
import com.jbooktrader.platform.report.ReportFactoryConsole;
import com.jbooktrader.platform.report.ReportFactoryFile;
import com.jbooktrader.platform.strategy.CommandLineStrategyRunner;
import com.jbooktrader.platform.util.*;
import com.jbooktrader.platform.web.*;
//import com.jbooktrader.platform.web.WebServer;

import javax.swing.*;
import javax.swing.plaf.*;
import java.io.*;
import java.nio.channels.*;


/**
 * Application starter.
 */
public class JBookTrader {
    public static final String APP_NAME = "JBookTrader";
    public static final String VERSION = "5.09";
    public static final String RELEASE_DATE = "September 12, 2008";
    private static String appPath;

    /**
     * Instantiates the necessary parts of the application: the application model,
     * views, and controller.
     */
    private JBookTrader() throws JBookTraderException {
        try {
            LiquidLookAndFeel.setLiquidDecorations(true, "mac");
            UIManager.setLookAndFeel("com.birosoft.liquid.LiquidLookAndFeel");
        } catch (Throwable t) {
            String msg = t.getMessage() + ": Unable to set custom look & feel. The default L&F will be used.";
            MessageDialog.showMessage(null, msg);
        }

        // Set the color scheme explicitly
        ColorUIResource color = new ColorUIResource(102, 102, 153);
        UIManager.put("Label.foreground", color);
        UIManager.put("TitledBorder.titleColor", color);

        new MainFrameController();
    }


    public static void showUsage() throws JBookTraderException {

        StringBuilder pmList = new StringBuilder();
        for(PerformanceMetric pm : PerformanceMetric.values()) {
            pmList.append("  '"+pm.getName()+"'\n");
        }

        throw new JBookTraderException("Usage: JBookTrader <JBookTraderDirectory>\n" +
                "  * Add the following options to run one or more strategy in forward test mode:\n" +
                "    --forwardtest OneStrategy [AnotherStrategy ...]\n" +
                "  * Add the following options to live trade one or more strategy:\n" +
                "    --trade OneStrategy [AnotherStrategy ...]\n" +
                "  * Add the following options to run a backtest from the command line :\n" +
                "    --backtest StrategyName DataFileName\n" +
                "  * Add the followind options to run the optimizer from the command line:\n" +
                "    --optimize StrategyName DataFileName SortCriteria MinTrades OptimizerMethod\n" +
                "\n" +
                "Examples:\n" +
                "$> JBookTrader /work/jbt\n" +
                "$> JBookTrader /work/jbt --forwardtest Strategy1 Strategy2\n" +
                "$> JBookTrader /work/jbt --trade Strategy1 Strategy2\n" +
                "$> JBookTrader /work/jbt --backtest MyStrategy /marketdata/ES.txt\n" +
                "$> JBookTrader /work/jbt --optimize MyStrategy /marketdata/ES.txt 'Profit Factor' 100 bf\n" +
                "\n" +
                "Available SortCriteria are:\n" + pmList.toString() +
                "\n" +
                "Available OptimizerMethod are:\n" +
                "  'bf' : for brute force omptimizer method\n" +
                "  'dnc' : for divide and conquer optimizer method\n" );
    }


    private static void runCommandLineTrade(Mode mode, String[] args) throws JBookTraderException {
        Dispatcher.setReportFactory(new ReportFactoryConsole());
        Dispatcher.setReporter("EventReport");
        String[] strategyNames = new String[args.length-2];
        System.arraycopy(args, 2, strategyNames, 0, args.length-2);
        new CommandLineStrategyRunner(mode, strategyNames);		
    }


    /**
     * Starts JBookTrader application.
     *
     * @param args
     */
    public static void main(String[] args) {
        try {
            File file = new File(System.getProperty("user.home"), APP_NAME + ".tmp");
            FileChannel channel = new RandomAccessFile(file, "rw").getChannel();

            if (channel.tryLock() == null) {
                MessageDialog.showMessage(null, APP_NAME + " is already running.");
                return;
            }

            if(args.length >=1 ) {
                JBookTrader.appPath = args[0];
            }

            // Launch JBT GUI
            if(args.length == 1) {
                Dispatcher.setReportFactory(new ReportFactoryFile());
                Dispatcher.setReporter("EventReport");
                new JBookTrader();	
            }
            // Launch command line backtester
            else if(args.length == 4 && args[1].equals("--backtest")){
                Dispatcher.setReportFactory(new ReportFactoryConsole());
                Dispatcher.setReporter("EventReport");
                new CommandLineBackTester(args[2], args[3]);            		
            }
            // Launch command line optimizer
            else if(args.length == 7 && args[1].equals("--optimize") ){
                Dispatcher.setReportFactory(new ReportFactoryConsole());
                Dispatcher.setReporter("EventReport");
                new CommandLineOptimizer(args[2],args[3],args[4],args[5],args[6]);                
            }
            // Launch command line forward test
            else if(args.length>2 && args[1].equals("--forwardtest") ) {
                runCommandLineTrade(Mode.ForwardTest, args);
            }
            // Launch command line live trading
            else if(args.length>2 && args[1].equals("--trade") ) {
                runCommandLineTrade(Mode.Trade, args);
            }
            // Show Usage help
            else if (args.length != 1) {
                showUsage();
            }

        } catch (Throwable t) {
            MessageDialog.showError(null, t.getMessage());
            if(Dispatcher.getReporter()!=null) {
                Dispatcher.getReporter().report(t);
            }
        }
    }

    public static String getAppPath() {
        return JBookTrader.appPath;
    }

}

/* $Id$ */<commit_msg>refactored app starter<commit_after>package com.jbooktrader.platform.startup;

import com.birosoft.liquid.*;
import com.jbooktrader.platform.model.*;
import com.jbooktrader.platform.report.*;
import com.jbooktrader.platform.util.*;

import javax.swing.*;
import javax.swing.plaf.*;
import java.io.*;
import java.nio.channels.*;


/**
 * Application starter.
 */
public class JBookTrader {
    public static final String APP_NAME = "JBookTrader";
    public static final String VERSION = "5.09";
    public static final String RELEASE_DATE = "September 12, 2008";
    private static String appPath;

    /**
     * Instantiates the necessary parts of the application: the application model,
     * views, and controller.
     */
    private JBookTrader() throws JBookTraderException {
        try {
            LiquidLookAndFeel.setLiquidDecorations(true, "mac");
            UIManager.setLookAndFeel("com.birosoft.liquid.LiquidLookAndFeel");
        } catch (Throwable t) {
            String msg = t.getMessage() + ": Unable to set custom look & feel. The default L&F will be used.";
            MessageDialog.showMessage(null, msg);
        }

        // Set the color scheme explicitly
        ColorUIResource color = new ColorUIResource(102, 102, 153);
        UIManager.put("Label.foreground", color);
        UIManager.put("TitledBorder.titleColor", color);

        new MainFrameController();
    }

    /**
     * Starts JBookTrader application.
     *
     * @param args
     */
    public static void main(String[] args) {
        try {
            File file = new File(System.getProperty("user.home"), APP_NAME + ".tmp");
            FileChannel channel = new RandomAccessFile(file, "rw").getChannel();

            if (channel.tryLock() == null) {
                MessageDialog.showMessage(null, APP_NAME + " is already running.");
                return;
            }

            if (args.length >= 1) {
                JBookTrader.appPath = args[0];
            }

            if (args.length == 1) {
                Dispatcher.setReportFactory(new ReportFactoryFile());
                Dispatcher.setReporter("EventReport");
                // Launch JBT GUI
                new JBookTrader();
            } else {
                CommandLineStarter.start(args);
            }
        } catch (Throwable t) {
            MessageDialog.showError(null, t.getMessage());
            if (Dispatcher.getReporter() != null) {
                Dispatcher.getReporter().report(t);
            }
        }
    }

    public static String getAppPath() {
        return JBookTrader.appPath;
    }

}

/* $Id$ */
{'fraction_non_alphanumeric': 0.113477537437604, 'fraction_numerical': 0.007653910149750416, 'mean_word_length': 2.1078938297138916, 'pattern_counts': {'":': 2, '<': 4, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1459103', 'n_tokens_mistral': 2599, 'n_tokens_neox': 2432, 'n_words': 568}