All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ta4jexamples.bots.TradingBotOnMovingTimeSeries Maven / Gradle / Ivy

There is a newer version: 0.17
Show newest version
/*
  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.bots;

import org.ta4j.core.*;
import org.ta4j.core.indicators.SMAIndicator;
import org.ta4j.core.indicators.helpers.ClosePriceIndicator;
import org.ta4j.core.trading.rules.OverIndicatorRule;
import org.ta4j.core.trading.rules.UnderIndicatorRule;
import ta4jexamples.loaders.CsvTradesLoader;

import java.time.ZonedDateTime;

/**
 * This class is an example of a dummy trading bot using ta4j.
 * 

*/ public class TradingBotOnMovingTimeSeries { /** Close price of the last bar */ private static Decimal LAST_BAR_CLOSE_PRICE; /** * Builds a moving time series (i.e. keeping only the maxBarCount last bars) * @param maxBarCount the number of bars to keep in the time series (at maximum) * @return a moving time series */ private static TimeSeries initMovingTimeSeries(int maxBarCount) { TimeSeries series = CsvTradesLoader.loadBitstampSeries(); System.out.print("Initial bar count: " + series.getBarCount()); // Limitating the number of bars to maxBarCount series.setMaximumBarCount(maxBarCount); LAST_BAR_CLOSE_PRICE = series.getBar(series.getEndIndex()).getClosePrice(); System.out.println(" (limited to " + maxBarCount + "), close price = " + LAST_BAR_CLOSE_PRICE); return series; } /** * @param series a time series * @return a dummy strategy */ private static Strategy buildStrategy(TimeSeries series) { if (series == null) { throw new IllegalArgumentException("Series cannot be null"); } ClosePriceIndicator closePrice = new ClosePriceIndicator(series); SMAIndicator sma = new SMAIndicator(closePrice, 12); // Signals // Buy when SMA goes over close price // Sell when close price goes over SMA return new BaseStrategy( new OverIndicatorRule(sma, closePrice), new UnderIndicatorRule(sma, closePrice) ); } /** * Generates a random decimal number between min and max. * @param min the minimum bound * @param max the maximum bound * @return a random decimal number between min and max */ private static Decimal randDecimal(Decimal min, Decimal max) { Decimal randomDecimal = null; if (min != null && max != null && min.isLessThan(max)) { randomDecimal = max.minus(min).multipliedBy(Decimal.valueOf(Math.random())).plus(min); } return randomDecimal; } /** * Generates a random bar. * @return a random bar */ private static Bar generateRandomBar() { final Decimal maxRange = Decimal.valueOf("0.03"); // 3.0% Decimal openPrice = LAST_BAR_CLOSE_PRICE; Decimal minPrice = openPrice.minus(openPrice.multipliedBy(maxRange.multipliedBy(Decimal.valueOf(Math.random())))); Decimal maxPrice = openPrice.plus(openPrice.multipliedBy(maxRange.multipliedBy(Decimal.valueOf(Math.random())))); Decimal closePrice = randDecimal(minPrice, maxPrice); LAST_BAR_CLOSE_PRICE = closePrice; return new BaseBar(ZonedDateTime.now(), openPrice, maxPrice, minPrice, closePrice, Decimal.ONE); } public static void main(String[] args) throws InterruptedException { System.out.println("********************** Initialization **********************"); // Getting the time series TimeSeries series = initMovingTimeSeries(20); // Building the trading strategy Strategy strategy = buildStrategy(series); // Initializing the trading history TradingRecord tradingRecord = new BaseTradingRecord(); System.out.println("************************************************************"); /* We run the strategy for the 50 next bars. */ for (int i = 0; i < 50; i++) { // New bar Thread.sleep(30); // I know... Bar newBar = generateRandomBar(); System.out.println("------------------------------------------------------\n" + "Bar "+i+" added, close price = " + newBar.getClosePrice().doubleValue()); series.addBar(newBar); int endIndex = series.getEndIndex(); if (strategy.shouldEnter(endIndex)) { // Our strategy should enter System.out.println("Strategy should ENTER on " + endIndex); boolean entered = tradingRecord.enter(endIndex, newBar.getClosePrice(), Decimal.TEN); if (entered) { Order entry = tradingRecord.getLastEntry(); System.out.println("Entered on " + entry.getIndex() + " (price=" + entry.getPrice().doubleValue() + ", amount=" + entry.getAmount().doubleValue() + ")"); } } else if (strategy.shouldExit(endIndex)) { // Our strategy should exit System.out.println("Strategy should EXIT on " + endIndex); boolean exited = tradingRecord.exit(endIndex, newBar.getClosePrice(), Decimal.TEN); if (exited) { Order exit = tradingRecord.getLastExit(); System.out.println("Exited on " + exit.getIndex() + " (price=" + exit.getPrice().doubleValue() + ", amount=" + exit.getAmount().doubleValue() + ")"); } } } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy