📄 testrunnerview.java
字号:
private int signals = 0; private int successCount = 0; private int failureCount = 0; private boolean firstBuy = true; private double savedBalance = 0; private Transaction lastBuyTransaction = null; public SimulatorJob(TradingSystem system, Date begin, Date end) { super("Trading System Simulator"); // Get the original history original = system.getSecurity().getHistory(); int first = -1, last = -1; for (int i = 0; i < original.size() && (first == -1 || last == -1); i++) { Bar bar = (Bar)original.get(i); if (first == -1 && (bar.getDate().equals(begin) || bar.getDate().after(begin))) first = i; if (last == -1 && (bar.getDate().equals(end) || bar.getDate().after(end))) last = i; } if (first != -1 && last != -1) { original = new History(); original.addAll(system.getSecurity().getHistory().getList().subList(first, last)); } // Create a fake subclass of the security item that returns our history data security = new Security(system.getSecurity().getId()) { public History getHistory() { return history; } }; security.setCode(system.getSecurity().getCode()); security.setDescription(system.getSecurity().getDescription()); // Create a fake account to hold the simulation transactions try { account = (Account)system.getAccount().clone(); } catch(Exception e) { CorePlugin.logException(e); account = new DefaultAccount(system.getAccount()); } // Create a new instance of the trading system plugin to run the simulation plugin = TradingPlugin.createTradingSystemPlugin(system.getPluginId()); plugin.setAccount(account); plugin.setSecurity(security); plugin.setParameters(system.getParameters()); plugin.setMaxExposure(system.getMaxExposure()); plugin.setMinAmount(system.getMinAmount()); plugin.setMaxAmount(system.getMaxAmount()); } /* (non-Javadoc) * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor) */ protected IStatus run(IProgressMonitor monitor) { fProgressBar.setMaximum(original.size()); for (int i = 0; i < original.size(); i++) { final Bar quote = (Bar)original.get(i); security.setQuote(new Quote(quote.getClose(), quote.getClose(), quote.getClose())); if (plugin.getSignal() == TradingSystem.SIGNAL_BUY) { if (firstBuy) { savedBalance = account.getBalance(); firstBuy = false; } final Transaction transaction = new Transaction(quote.getDate(), security, plugin.getQuantity(), quote.getOpen()); double expenses = account.getExpenses(security, plugin.getQuantity(), quote.getOpen()); transaction.setExpenses(expenses); account.getTransactions().add(transaction); lastBuyTransaction = transaction; fLog.getDisplay().syncExec(new Runnable() { public void run() { TableItem tableItem = new TableItem(fLog, SWT.NONE); tableItem.setText(0, dateFormatter.format(transaction.getDate())); tableItem.setText(1, "Buy"); tableItem.setText(2, numberFormatter.format(plugin.getQuantity()) + " at " + priceFormatter.format(quote.getOpen())); tableItem.setData(transaction); for (int i = 0; i < fLog.getColumnCount(); i++) fLog.getColumn(i).pack(); } }); } else if (plugin.getSignal() == TradingSystem.SIGNAL_SELL) { final Transaction transaction = new Transaction(quote.getDate(), security, - plugin.getQuantity(), quote.getOpen()); double expenses = account.getExpenses(security, plugin.getQuantity(), quote.getOpen()); transaction.setExpenses(expenses); account.getTransactions().add(transaction); transaction.setData(lastBuyTransaction); lastBuyTransaction = null; fLog.getDisplay().syncExec(new Runnable() { public void run() { TableItem tableItem = new TableItem(fLog, SWT.NONE); tableItem.setText(0, dateFormatter.format(transaction.getDate())); tableItem.setText(1, "Sell"); tableItem.setText(2, numberFormatter.format(plugin.getQuantity()) + " at " + priceFormatter.format(quote.getOpen())); tableItem.setData(transaction); for (int i = 0; i < fLog.getColumnCount(); i++) fLog.getColumn(i).pack(); } }); if (account.getBalance() > savedBalance) successCount++; else failureCount++; firstBuy = true; } fProgressBar.getDisplay().syncExec(new Runnable() { public void run() { fCounterPanel.setRunValue(signals); fCounterPanel.setGainsValue(successCount); fCounterPanel.setLossesValue(failureCount); fProgressBar.step(0); } }); history = new History(); history.addAll(original.getList().subList(0, i)); plugin.run(); if (plugin.getSignal() != TradingSystem.SIGNAL_NONE) { fLog.getDisplay().syncExec(new Runnable() { public void run() { TableItem tableItem = new TableItem(fLog, SWT.NONE); tableItem.setText(0, dateFormatter.format(quote.getDate())); tableItem.setText(1, "Signal"); switch(plugin.getSignal()) { case TradingSystem.SIGNAL_BUY: tableItem.setText(2, "Buy"); break; case TradingSystem.SIGNAL_SELL: tableItem.setText(2, "Sell"); break; case TradingSystem.SIGNAL_HOLD: tableItem.setText(2, "Hold"); break; case TradingSystem.SIGNAL_NEUTRAL: tableItem.setText(2, "Neutral"); break; } for (int i = 0; i < fLog.getColumnCount(); i++) fLog.getColumn(i).pack(); } }); signals++; } } fLog.getDisplay().syncExec(new Runnable() { public void run() { double initial = account.getInitialBalance(); double current = account.getBalance() + account.getPortfolio(security).getMarketValue(); double gain = current - initial; TableItem tableItem = new TableItem(fLog, SWT.NONE); tableItem.setText(0, ""); tableItem.setText(1, "Total"); tableItem.setText(2, amountFormatter.format(gain) + " (" + percentFormatter.format(gain / initial * 100) + "%)"); for (int i = 0; i < fLog.getColumnCount(); i++) fLog.getColumn(i).pack(); } }); return Status.OK_STATUS; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -