Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
The MIT License (MIT)
Copyright (c) 2014-2017 Marc de Verdelhan & respective authors (see AUTHORS)
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package ta4jexamples.strategies;
import org.ta4j.core.*;
import org.ta4j.core.analysis.criteria.TotalProfitCriterion;
import org.ta4j.core.indicators.EMAIndicator;
import org.ta4j.core.indicators.MACDIndicator;
import org.ta4j.core.indicators.StochasticOscillatorKIndicator;
import org.ta4j.core.indicators.helpers.ClosePriceIndicator;
import org.ta4j.core.trading.rules.CrossedDownIndicatorRule;
import org.ta4j.core.trading.rules.CrossedUpIndicatorRule;
import org.ta4j.core.trading.rules.OverIndicatorRule;
import org.ta4j.core.trading.rules.UnderIndicatorRule;
import ta4jexamples.loaders.CsvTradesLoader;
/**
* Moving momentum strategy.
*
* @see
* http://stockcharts.com/help/doku.php?id=chart_school:trading_strategies:moving_momentum
*/
public class MovingMomentumStrategy {
/**
* @param series a time series
* @return a moving momentum strategy
*/
public static Strategy buildStrategy(TimeSeries series) {
if (series == null) {
throw new IllegalArgumentException("Series cannot be null");
}
ClosePriceIndicator closePrice = new ClosePriceIndicator(series);
// The bias is bullish when the shorter-moving average moves above the longer moving average.
// The bias is bearish when the shorter-moving average moves below the longer moving average.
EMAIndicator shortEma = new EMAIndicator(closePrice, 9);
EMAIndicator longEma = new EMAIndicator(closePrice, 26);
StochasticOscillatorKIndicator stochasticOscillK = new StochasticOscillatorKIndicator(series, 14);
MACDIndicator macd = new MACDIndicator(closePrice, 9, 26);
EMAIndicator emaMacd = new EMAIndicator(macd, 18);
// Entry rule
Rule entryRule = new OverIndicatorRule(shortEma, longEma) // Trend
.and(new CrossedDownIndicatorRule(stochasticOscillK, Decimal.valueOf(20))) // Signal 1
.and(new OverIndicatorRule(macd, emaMacd)); // Signal 2
// Exit rule
Rule exitRule = new UnderIndicatorRule(shortEma, longEma) // Trend
.and(new CrossedUpIndicatorRule(stochasticOscillK, Decimal.valueOf(80))) // Signal 1
.and(new UnderIndicatorRule(macd, emaMacd)); // Signal 2
return new BaseStrategy(entryRule, exitRule);
}
public static void main(String[] args) {
// Getting the time series
TimeSeries series = CsvTradesLoader.loadBitstampSeries();
// Building the trading strategy
Strategy strategy = buildStrategy(series);
// Running the strategy
TimeSeriesManager seriesManager = new TimeSeriesManager(series);
TradingRecord tradingRecord = seriesManager.run(strategy);
System.out.println("Number of trades for the strategy: " + tradingRecord.getTradeCount());
// Analysis
System.out.println("Total profit for the strategy: " + new TotalProfitCriterion().calculate(series, tradingRecord));
}
}