double yearlyMedianDailyIncrease = getMedianHighOpenSpread(symbol, 365);
GoogleQuoteFetcher fetcher = new GoogleQuoteFetcher();
TimeSeries series = fetcher.fetchAndParse(symbol, days, 60); // one minute
BigDecimal firstPrice = series.getPrices().firstEntry().getValue();
BigDecimal lastBidAskSpread = new YahooQuoteFetcher().fetchBidAskSpread(symbol);
final double minBuy = 0; // allow buying at open price
final double minSell = firstPrice.multiply(new BigDecimal("0.002")).doubleValue(); // 0.2% of first price to sell (which is hopefully on the order of twice the bid-ask spread)
final double minStop = minSell;
final double maxBuy = yearlyMedianDailyIncrease;
final double maxSell = yearlyMedianDailyIncrease;
final double maxStop = yearlyMedianDailyIncrease;
logger.info("Minimum buy:" + minBuy);
logger.info("Minimum sell: " + minSell);
logger.info("Minimum stop: " + minStop);
logger.info("Max buy: " + maxBuy);
logger.info("Max sell: " + maxSell);
logger.info("Max stop: " + maxStop);
final double commission = 10.0; // $10.00 a trade
final double slippage = 1.0E-3; // 0.1% mean slippage on each side of a trade, which should also account for bid-ask spread
final int initialBalance = 100000; // $100,000 to start with
final double accountAllocation = 0.75; // risk 75% of capital
final int generations = 100; // generations to train for
DateTime today = DateTime.now();
double[] bestOffsets = optimizeTriggers(series, symbol, days, commission,
slippage, initialBalance, accountAllocation, minBuy,
minSell, minStop, maxBuy, maxSell, maxStop, generations);
double buyTrigger = bestOffsets[0];
double sellTrigger = bestOffsets[1];
double stopLoss = bestOffsets[2];
System.out.println("\n\nBuy trigger: " + buyTrigger);
System.out.println("Sell trigger: " + sellTrigger);
System.out.println("Stop loss: " + stopLoss);
Asset asset = new Asset(symbol, series);
Account account = new Account(new BigDecimal(initialBalance), today.minusDays(days));
Conditions conditions = new Conditions(new BigDecimal(commission), new BigDecimal(slippage));
MoneyManagementStrategy moneyManager = new FixedPercentageAllocationStrategy(accountAllocation, asset);
BuyZoneModel instance = new BuyZoneModel(account, asset, conditions,
moneyManager, buyTrigger, sellTrigger, stopLoss);
Session session = instance.generateSignals(series.beginningOfSeries(), series.lastOfSeries());
session.dumpTo(".", symbol);
}