<commit_before>package com.chariotinstruments.markets;

import java.util.ArrayList;

/**
 * Created by user on 1/30/16.
 */
public class CalcMACD {

    private MarketDay marketDay;
    private ArrayList<MarketCandle> marketCandles;

    public int count;
    public ArrayList<Double> periodTesterList;
    public double avg12;
    public double avg26;
    public String avgList;

    public CalcMACD(MarketDay marDay){
        marketDay = marDay;
        marketCandles = marketDay.getMarketCandles();

    }

    public String getCurrentMACD(){
        double twelveEMA = 0.0;
        double twentysixEMA = 0.0;
        double ret = 0.0;

        twelveEMA = get12PeriodEMA();
        twentysixEMA = get26PeriodEMA();
        ret = twelveEMA - twentysixEMA;

        return String.format("%.4f", ret);
    }

    private double get12PeriodEMA(){

        //Get an arraylist of 13 periods
        ArrayList<Double> periodList = new ArrayList<Double>();
        int startIndex = marketCandles.size()-21; // need 16 periods so we can subtract the first.
        int stopIndex = marketCandles.size()-1;
        double firstTwelveAvg = 0.0;
        double firstTwelveAvg2 = 0.0;
        double multiplier = 0.0;
        double lastAmount = 0.0;
        double secondToLastAmount = 0.0;
        double curEMA = 0.0;
        double retAmount = 0.0;

        //get all close values for 13 periods, sum/average the first 12.
        for(int i = startIndex; i<=stopIndex; i++){
            periodList.add(marketCandles.get(i).getClose());
            if(i <= stopIndex-9){
                avgList = avgList + "\n" + marketCandles.get(i).getClose();
                firstTwelveAvg += marketCandles.get(i).getClose();
            }
        }

        periodTesterList = periodList;

        secondToLastAmount = marketCandles.get(marketCandles.size()-2).getClose();
        lastAmount = marketCandles.get(marketCandles.size()-1).getClose();
        firstTwelveAvg2 = firstTwelveAvg/12.0d;

        //calculate multiplier
        multiplier = 2.0d/13.0d;

        int i = stopIndex-8;
        curEMA = (marketCandles.get(i).getClose() * multiplier) + (firstTwelveAvg2 * (1.0 - multiplier));
        while(i <= stopIndex) {
            curEMA = (marketCandles.get(i).getClose() * multiplier) + (curEMA * (1.0 - multiplier));
            i++;
        }

        avg12 = curEMA;

        return curEMA;

    }

    private double get26PeriodEMA(){
        ArrayList<Double> periodList = new ArrayList<Double>();
        int startIndex = marketCandles.size()-35; // need 16 periods so we can subtract the first.
        int stopIndex = marketCandles.size()-1;
        double firstTwentySixAvg = 0.0;
        double firstTwentySixAvg2 = 0.0;
        double lastAmount = 0.0;
        double secondToLastAmount = 0.0;
        double curEMA = 0.0;
        double multiplier = 0.0;
        double retAmount = 0.0;

        //get all close values for 27 periods, sum/average the first 26.
        for(int i = startIndex; i<=stopIndex; i++){

            periodList.add(marketCandles.get(i).getClose());
            if(i <= stopIndex-9){
                firstTwentySixAvg += marketCandles.get(i).getClose();
            }
        }

        secondToLastAmount = marketCandles.get(marketCandles.size()-2).getClose();
        lastAmount = marketCandles.get(marketCandles.size()-1).getClose();
        firstTwentySixAvg2 = firstTwentySixAvg/26.0d;

        multiplier = 2.0d/27.0d;

        int i = stopIndex-8;
        curEMA = (marketCandles.get(i).getClose() * multiplier) + (firstTwentySixAvg2 * (1.0d - multiplier));
        while(i <= stopIndex) {
            count += 1;
            curEMA = (marketCandles.get(i).getClose() * multiplier) + (curEMA * (1.0d - multiplier));
            i++;
        }

        avg26 = curEMA;

        return curEMA;
    }

    public String tester(){

        String output = "";
        for(double dbl : periodTesterList){
            output = output + "\n" + Double.toString(dbl);<commit_msg>More MACD Work<commit_after>package com.chariotinstruments.markets;

import java.util.ArrayList;

/**
 * Created by user on 1/30/16.
 */
public class CalcMACD {

    private MarketDay marketDay;
    private ArrayList<MarketCandle> marketCandles;

    public int count;
    public ArrayList<Double> periodTesterList;
    public double avg12;
    public double avg26;
    public String avgList;

    public CalcMACD(MarketDay marDay){
        marketDay = marDay;
        marketCandles = marketDay.getMarketCandles();

    }

    public String getCurrentMACD(){
        double twelveEMA = 0.0;
        double twentysixEMA = 0.0;
        double ret = 0.0;

        twelveEMA = get12PeriodEMA();
        twentysixEMA = get26PeriodEMA();
        ret = twelveEMA - twentysixEMA;

        return String.format("%.4f", ret);
    }

    private double get12PeriodEMA(){

        //Get an arraylist of 13 periods
        ArrayList<Double> periodList = new ArrayList<Double>();
        int startIndex = marketCandles.size()-21; // need 16 periods so we can subtract the first.
        int stopIndex = marketCandles.size()-1;
        double firstTwelveAvg = 0.0;
        double firstTwelveAvg2 = 0.0;
        double multiplier = 0.0;
        double lastAmount = 0.0;
        double secondToLastAmount = 0.0;
        double curEMA = 0.0;
        double retAmount = 0.0;

        //get all close values for 13 periods, sum/average the first 12.
        for(int i = startIndex; i<=stopIndex; i++){
            periodList.add(marketCandles.get(i).getClose());
            if(i <= stopIndex-9){
                firstTwelveAvg += marketCandles.get(i).getClose();
            }
        }

        periodTesterList = periodList;

        secondToLastAmount = marketCandles.get(marketCandles.size()-2).getClose();
        lastAmount = marketCandles.get(marketCandles.size()-1).getClose();
        firstTwelveAvg2 = firstTwelveAvg/12.0d;

        //calculate multiplier
        multiplier = 2.0d/13.0d;

        int i = 12;
        curEMA = (periodList.get(i) * multiplier) + (firstTwelveAvg2 * (1.0 - multiplier));
        i++;
        while(i <= periodList.size()-1) {
            curEMA = (periodList.get(i) * multiplier) + (curEMA * (1.0 - multiplier));
            i++;
        }

        avg12 = curEMA;

        return curEMA;

    }

    private double get26PeriodEMA(){
        ArrayList<Double> periodList = new ArrayList<Double>();
        int startIndex = marketCandles.size()-35; // need 16 periods so we can subtract the first.
        int stopIndex = marketCandles.size()-1;
        double firstTwentySixAvg = 0.0;
        double firstTwentySixAvg2 = 0.0;
        double lastAmount = 0.0;
        double secondToLastAmount = 0.0;
        double curEMA = 0.0;
        double multiplier = 0.0;
        double retAmount = 0.0;

        //get all close values for 27 periods, sum/average the first 26.
        for(int i = startIndex; i<=stopIndex; i++){

            periodList.add(marketCandles.get(i).getClose());
            if(i <= stopIndex-9){
                firstTwentySixAvg += marketCandles.get(i).getClose();
            }
        }

        secondToLastAmount = marketCandles.get(marketCandles.size()-2).getClose();
        lastAmount = marketCandles.get(marketCandles.size()-1).getClose();
        firstTwentySixAvg2 = firstTwentySixAvg/26.0d;

        multiplier = 2.0d/27.0d;

        int i = 26;
        curEMA = (periodList.get(i) * multiplier) + (firstTwentySixAvg2 * (1.0 - multiplier));
        i++;
        while(i <= periodList.size()-1) {
            count += 1;
            curEMA = (periodList.get(i) * multiplier) + (curEMA * (1.0 - multiplier));
            i++;
        }

        avg26 = curEMA;

        return curEMA;
    }

    public String tester(){

        String output = "";
        for(double dbl : periodTesterList){
            output = output + "\n" + Double.toString(dbl);
{'fraction_non_alphanumeric': 0.11692579951965618, 'fraction_numerical': 0.02957906712172924, 'mean_word_length': 2.1509358821186777, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2274939', 'n_tokens_mistral': 2695, 'n_tokens_neox': 2377, 'n_words': 555}