📄 rangebouncer.java
字号:
package com.jsystemtrader.strategy;
import com.ib.client.*;
import com.jsystemtrader.indicator.*;
import com.jsystemtrader.platform.*;
import com.jsystemtrader.util.*;
/**
* This sample strategy trades S&P E-Mini futures using moving average crossover
* as an indicator. When the fast EMA is below the slow EMA, it buys. Otherwise,
* it sells short. This is an anti-trend type of a system. The moving averages
* are calculated from the priceBar history of the 1-minute bars.
*/
public class RangeBouncer extends Strategy {
private final int maFastLength = 5, maSlowLength = 70;
private final IndicatorHistory fastEMAHistory, slowEMAHistory;
private double fastEMA, slowEMA;
public RangeBouncer() throws JSystemTraderException {
// add strategy-specific headers to standard headers
strategyLogHeaders.add("fastEMA");
strategyLogHeaders.add("slowEMA");
Contract contract = ContractFactory.makeContract("ES", "FUT", "GLOBEX", MostLiquidContract.getMostLiquid(), null);
setStrategy(contract, PriceBar.BAR_1_MINUTE, true, 1);
/*
Create indicator histories, so that they can be shown on the
strategy performance chart for the subsequent analysis.
*/
int subChart = 0; // same subchart as the price chart
fastEMAHistory = new IndicatorHistory("Fast EMA", subChart);
slowEMAHistory = new IndicatorHistory("Slow EMA", subChart);
addIndicatorHistory(fastEMAHistory);
addIndicatorHistory(slowEMAHistory);
// see javadocs for the TradingInterval class
addTradingInterval("9:35", "15:55", 15);
}
/**
* This method is invoked when a new bar is completed.
*/
public void onBar() {
// First, let the super strategy decide if we can trade at all
super.onBar();
if (decision == DECISION_NONE) { // the super strategy has no objections
if (fastEMA > slowEMA) {
decision = DECISION_SHORT;
}
if (fastEMA < slowEMA) {
decision = DECISION_LONG;
}
}
}
/**
* This method is invoked when a new tick comes in.
*/
public void onTick() {
super.onTick();
double lastTick = quoteHistory.getLast();
// do something
}
/**
* Instance of NumberFormat is shared by multiple threads,
* so the access must be synchronized.
*/
public void updateState() {
super.updateState();
String msg = this.getName() + ": state updated" + "<br>";
msg += "Last PriceBar: " + quoteHistory.getLastPriceBar() + "<br>";
msg += "fastEMA: " + nf4.format(fastEMA) + " slowEMA: " + nf4.format(slowEMA) + "<br>";
eventLog.write(msg, "Info", 1);
strategyLogColumns.add(nf4.format(fastEMA));
strategyLogColumns.add(nf4.format(slowEMA));
strategyLog.write(strategyLogColumns, getCalendar(), "Info", 1);
}
/**
* Called from the super class
*/
public void updateIndicators() throws IndicatorCalculationException {
long date = quoteHistory.getLastPriceBar().getDate();
// calculate fast and slow EMAs
EMA fastEMAInd = new EMA(date, quoteHistory, maFastLength);
EMA slowEMAInd = new EMA(date, quoteHistory, maSlowLength);
fastEMA = fastEMAInd.calculate();
slowEMA = slowEMAInd.calculate();
fastEMAHistory.addIndicator(fastEMAInd);
slowEMAHistory.addIndicator(slowEMAInd);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -