com.algodefu.yeti.data.Pass.java Source code

Java tutorial

Introduction

Here is the source code for com.algodefu.yeti.data.Pass.java

Source

/*
 * Copyright 2014 Algodefu
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.algodefu.yeti.data;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;

import java.awt.*;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.HashMap;

public class Pass {

    private String id;
    private Trade[] trades = new Trade[0];
    private StrategyParamSet sps;
    private int netProfit;
    private double profitFactor;
    private double commission;
    private double volume;
    private int execTrades;
    private byte[] equityChartByteArray;
    private PassStatus passStatus = PassStatus.CREATED;
    private TradesSaveMode tradesSaveMode = TradesSaveMode.ALL;
    private double[][] equityArray;

    public Pass(String id, StrategyParamSet sps) {
        this.id = id;
        this.sps = sps;
    }

    public Pass(String id, StrategyParamSet sps, TradesSaveMode tradesSaveMode) {
        this.id = id;
        this.sps = sps;
        this.tradesSaveMode = tradesSaveMode;
    }

    public String getId() {
        return this.id;
    }

    public int getNetProfit() {
        return netProfit;
    }

    public void setNetProfit(int netProfit) {
        this.netProfit = netProfit;
    }

    public double getProfitFactor() {
        return profitFactor;
    }

    public void setProfitFactor(double profitFactor) {
        this.profitFactor = profitFactor;
    }

    public double getCommission() {
        return commission;
    }

    public void setCommission(double commission) {
        this.commission = commission;
    }

    public double getVolume() {
        return volume;
    }

    public void setVolume(double volume) {
        this.volume = volume;
    }

    public int getExecTrades() {
        return execTrades;
    }

    public void setExecTrades(int execTrades) {
        this.execTrades = execTrades;
    }

    public Trade[] getTrades() {
        return trades;
    }

    public StrategyParamSet getStrategyParamSet() {
        return sps;
    }

    public void setSps(StrategyParamSet sps) {
        this.sps = sps;
    }

    public void setTrades(Trade[] trades) {
        this.trades = trades;
    }

    public byte[] getEquityChartByteArray() {
        return equityChartByteArray;
    }

    public void setEquityChartByteArray(byte[] equityChartByteArray) {
        this.equityChartByteArray = equityChartByteArray;
    }

    public PassStatus getPassStatus() {
        return passStatus;
    }

    public void setPassStatus(PassStatus passStatus) {
        this.passStatus = passStatus;
    }

    public TradesSaveMode getTradesSaveMode() {
        return tradesSaveMode;
    }

    public void setTradesSaveMode(TradesSaveMode tradesSaveMode) {
        this.tradesSaveMode = tradesSaveMode;
    }

    public double[][] getEquityArray() {
        return equityArray;
    }

    public void setEquityArray(double[][] equityArray) {
        this.equityArray = equityArray;
    }

    public void calculate() throws IOException {
        if (trades.length == 0)
            return;
        int profit = 0;
        int loss = 0;
        this.execTrades = 0;
        this.commission = trades[0].getDeals().get(0).getCommission();
        for (Trade trade : trades) {
            if (trade.isWasCanceled())
                continue;
            execTrades++;
            if (trade.getProfit() > 0)
                profit += trade.getProfit();
            else
                loss += trade.getProfit();
            this.volume += trade.getVolume();
        }
        this.netProfit = profit + loss;
        this.profitFactor = (double) profit / Math.abs(loss);
        this.calculateEquityChartImg();
        this.passStatus = PassStatus.EXECUTED;
        switch (this.tradesSaveMode) {
        case ALL:
            break;
        case ONLY_POSITIVE:
            if (netProfit < 0)
                trades = new Trade[0];
            break;
        case NO:
            this.trades = new Trade[0];
            break;
        }
        ;
    }

    private void calculateEquityChartImg() throws IOException {
        int i = 0;
        double sum = 0;
        TimeSeries equity = new TimeSeries("Equity");

        // save values in temp array first, then use System.arraycopy to copy non empty values to this.equityArray
        double[][] tempEquityArr = new double[this.getTrades().length][4];

        for (Trade trade : this.getTrades()) {
            if (trade.getCloseDateTime() != null) {
                sum += trade.getProfit();
                equity.add(new Millisecond(Date.from(trade.getCloseDateTime().toInstant(ZoneOffset.UTC))), sum);
                tempEquityArr[i][0] = (double) trade.getCloseDateTime().toInstant(ZoneOffset.UTC).toEpochMilli();
                tempEquityArr[i][1] = sum;
                tempEquityArr[i][2] = trade.getTradeID();
                tempEquityArr[i][3] = trade.getProfit();
                i++;
            }
        }
        this.equityArray = new double[i][4];
        System.arraycopy(tempEquityArr, 0, this.equityArray, 0, i);

        TimeSeriesCollection dataset = new TimeSeriesCollection(equity);
        JFreeChart chart = ChartFactory.createTimeSeriesChart("", "", "", dataset, false, false, false);
        chart.getXYPlot().getDomainAxis().setTickLabelsVisible(false);
        chart.setBorderVisible(true);
        chart.getXYPlot().setRangeAxisLocation(AxisLocation.TOP_OR_RIGHT);
        chart.getXYPlot().getRenderer().setSeriesPaint(0, Color.blue);
        chart.getXYPlot().setBackgroundPaint(Color.white);
        chart.getXYPlot().setRangeGridlinePaint(Color.gray);
        chart.getXYPlot().setDomainGridlinePaint(Color.gray);
        try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
            ChartUtilities.writeChartAsPNG(baos, chart, 320, 180);
            baos.flush();
            this.equityChartByteArray = baos.toByteArray();
        } catch (IOException e) {
            throw e;
        }

    }
}