public static Double createMFI( List<Double> highs, List<Double> lows, List<Double> closes, List<Long> volumes, int period) { if (period <= 0) { throw new IllegalArgumentException("period must be greater than 0"); } if (highs.size() != lows.size() || highs.size() != closes.size() || highs.size() != volumes.size()) { throw new IllegalArgumentException("input list must be same size"); } final int size = highs.size(); final Core core = new Core(); final int allocationSize = size - core.mfiLookback(period); if (allocationSize <= 0) { return null; } final double[] output = new double[allocationSize]; final MInteger outBegIdx = new MInteger(); final MInteger outNbElement = new MInteger(); double[] _highs = ArrayUtils.toPrimitive(highs.toArray(new Double[0])); double[] _lows = ArrayUtils.toPrimitive(lows.toArray(new Double[0])); double[] _closes = ArrayUtils.toPrimitive(closes.toArray(new Double[0])); long[] _volumes = ArrayUtils.toPrimitive(volumes.toArray(new Long[0])); double[] dv = new double[_volumes.length]; for (int i = 0; i < dv.length; i++) { dv[i] = _volumes[i]; } core.mfi( 0, _highs.length - 1, _highs, _lows, _closes, dv, period, outBegIdx, outNbElement, output); return output[outNbElement.value - 1]; }
/* (non-Javadoc) * @see org.eclipsetrader.ui.charts.IChartObjectFactory#createObject(org.eclipsetrader.core.charts.IDataSeries) */ @Override public IChartObject createObject(IDataSeries source) { if (source == null) { return null; } IAdaptable[] values = source.getValues(); Core core = Activator.getDefault() != null ? Activator.getDefault().getCore() : new Core(); int lookback = core.cdl3OutsideLookback(); if (values.length < lookback) { return null; } int startIdx = 0; int endIdx = values.length - 1; double[] inOpen = Util.getValuesForField(values, OHLCField.Open); double[] inHigh = Util.getValuesForField(values, OHLCField.High); double[] inLow = Util.getValuesForField(values, OHLCField.Low); double[] inClose = Util.getValuesForField(values, OHLCField.Close); MInteger outBegIdx = new MInteger(); MInteger outNbElement = new MInteger(); int[] outInteger = new int[values.length - lookback]; core.cdl3Outside( startIdx, endIdx, inOpen, inHigh, inLow, inClose, outBegIdx, outNbElement, outInteger); return new PatternChart(getName(), values, lookback, outInteger); }
public static MACD.Result createMACDFix(List<Double> values, int period) { if (period <= 0) { throw new IllegalArgumentException("period must be greater than 0"); } final int size = values.size(); final Core core = new Core(); final int allocationSize = size - core.macdFixLookback(period); if (allocationSize <= 0) { return null; } final double[] outMACD = new double[allocationSize]; final double[] outMACDSignal = new double[allocationSize]; final double[] outMACDHist = new double[allocationSize]; final MInteger outBegIdx = new MInteger(); final MInteger outNbElement = new MInteger(); double[] _values = ArrayUtils.toPrimitive(values.toArray(new Double[0])); core.macdFix( 0, values.size() - 1, _values, period, outBegIdx, outNbElement, outMACD, outMACDSignal, outMACDHist); return MACD.Result.newInstance( outMACD[outNbElement.value - 1], outMACDSignal[outNbElement.value - 1], outMACDHist[outNbElement.value - 1]); }
/* (non-Javadoc) * @see org.eclipsetrader.core.ats.javascript.IndicatorFunction#calculate() */ @Override protected void calculate() { IAdaptable[] values = source.getValues(); Core core = Activator.getDefault() != null ? Activator.getDefault().getCore() : new Core(); int lookback = core.adLookback(); if (values.length < lookback) { series = new NumericDataSeries(getClassName(), new Number[0], source); return; } int startIdx = 0; int endIdx = values.length - 1; double[] inHigh = Util.getValuesForField(values, OHLCField.High); double[] inLow = Util.getValuesForField(values, OHLCField.Low); double[] inClose = Util.getValuesForField(values, OHLCField.Close); double[] inVolume = Util.getValuesForField(values, OHLCField.Volume); MInteger outBegIdx = new MInteger(); MInteger outNbElement = new MInteger(); double[] outReal = new double[values.length - lookback]; core.ad(startIdx, endIdx, inHigh, inLow, inClose, inVolume, outBegIdx, outNbElement, outReal); series = new NumericDataSeries(getClassName(), outReal, source); }
public List<Double[]> compute( double[] close, int fastPeriod, int slowPeriod, int signalPeriod, IHInformer informer) { MInteger begin = new MInteger(); MInteger length = new MInteger(); int resultLegnth = close.length - (slowPeriod - 1) - (signalPeriod - 1); double[] values = new double[resultLegnth]; double[] signals = new double[resultLegnth]; double[] histograms = new double[resultLegnth]; List<Double[]> result = new ArrayList<Double[]>(); Core core = new Core(); core.macd( 0, close.length - 1, close, fastPeriod, slowPeriod, signalPeriod, begin, length, values, signals, histograms); for (int i = 0; i < resultLegnth; i++) { result.add(new Double[] {values[i], signals[i], histograms[i]}); } return result; }
public void calculate() { Dataset initial = getDataset(); int count = 0; if (initial != null && !initial.isEmpty()) count = initial.getItemsCount(); /** ******************************************************************* */ // This entire method is basically a copy/paste action into your own // code. The only thing you have to change is the next few lines of code. // Choose the 'lookback' method and appropriate 'calculation function' // from TA-Lib for your needs. You'll also need to ensure you gather // everything for your calculation as well. Everything else should stay // basically the same // prepare ta-lib variables output = new double[count]; outBegIdx = new MInteger(); outNbElement = new MInteger(); core = TaLibInit.getCore(); // needs to be here for serialization issues // [your specific indicator variables need to be set first] period = properties.getPeriod(); volumeFactor = properties.getVolumeFactor(); // now do the calculation over the entire dataset // [First, perform the lookback call if one exists] // [Second, do the calculation call from TA-lib] lookback = core.movingAverageLookback(period, MAType.T3); core.t3( 0, count - 1, initial.getCloseValues(), period, volumeFactor, outBegIdx, outNbElement, output); // Everything between the /***/ lines is what needs to be changed. // Everything else remains the same. You are done with your part now. /** ******************************************************************* */ // fix the output array's structure. TA-Lib does NOT match // indicator index and dataset index automatically. That's what // this function does for us. output = TaLibUtilities.fixOutputArray(output, lookback); calculatedDataset = Dataset.EMPTY(initial.getItemsCount()); for (int i = 0; i < output.length; i++) calculatedDataset.setDataItem(i, new DataItem(initial.getTimeAt(i), output[i])); addDataset(HASHKEY, calculatedDataset); }
public static Double createEMA(java.util.List<Double> values, int period) { if (period <= 0) { throw new IllegalArgumentException("period must be greater than 0"); } final int size = values.size(); final Core core = new Core(); final int allocationSize = size - core.emaLookback(period); if (allocationSize <= 0) { return null; } final double[] output = new double[allocationSize]; final MInteger outBegIdx = new MInteger(); final MInteger outNbElement = new MInteger(); double[] _values = ArrayUtils.toPrimitive(values.toArray(new Double[0])); core.ema(0, values.size() - 1, _values, period, outBegIdx, outNbElement, output); return output[outNbElement.value - 1]; }
public static double[] computeMA(double[] input, int period, int maType) { MInteger begin = new MInteger(); MInteger length = new MInteger(); double[] out = new double[input.length - period + 1]; switch (maType) { case MA_TYPE_SMA: core.sma(0, input.length - 1, input, period, begin, length, out); break; case MA_TYPE_EMA: core.ema(0, input.length - 1, input, period, begin, length, out); break; case MA_TYPE_WMA: core.wma(0, input.length - 1, input, period, begin, length, out); break; default: throw new RuntimeException("unsupported MaType " + maType); } return out; }
// MOM public MethodResults performMOM(String stockName, double[] input) { double[] output = null; MInteger outBegIdx = null; MInteger outNbElement = null; int period = 0; this.loadInputs(stockName); final Core core = new Core(); period = input.length - 1; final int allocationSize = period - core.momLookback(config.inputMOMPeriod); if (allocationSize <= 0) { System.err.printf("No data for period (%d)\n", allocationSize); return null; } output = new double[allocationSize]; outBegIdx = new MInteger(); outNbElement = new MInteger(); RetCode code = core.mom(0, period - 1, input, config.inputMOMPeriod, outBegIdx, outNbElement, output); if (code.compareTo(RetCode.Success) != 0) { // Error return empty method results System.err.printf("SMI failed!\n"); return new MethodResults(this.getMethName()); } methodResults.putResult(stockName, output[output.length - 1]); if (config.inputPrintResults) { sender = new GraphSender(stockName); sender.setSeriesName(this.getMethName()); DateIterator dateIterator = new DateIterator(portfolioConfig.inputStartingDate, portfolioConfig.inputEndingDate); dateIterator.move(outBegIdx.value); for (int i = 0; i < outNbElement.value && dateIterator.hasNext(); i++) { Date stockDate = dateIterator.next(); // System.out.printf("Date:"+stockDate+" // Time:"+inputEndingDate.getTime()+"Time2:"+inputStartingDate.getTime()+"\n"); sender.addForSending(stockDate, output[i]); } sender.sendAllStored(); } // ************* DECISION TEST *************// if (config.inputPerfomDecision) { double amoutOfStocks = 0; double bestAssumedBudjet = 0; double bestPeriod = 0; double assumedBudjet = 0.0f; int decMOMPeriod = 0; NumberRangeIter numberIter = new NumberRangeIter("MOMRange"); numberIter.setRange(config.inputMOMDecisionPeriod); while (numberIter.hasNext()) { amoutOfStocks = 0; assumedBudjet = portfolioConfig.inputAssumedBudjet; decMOMPeriod = numberIter.next().intValue(); final int allocationSizeDecision = period - core.momLookback(decMOMPeriod); if (allocationSizeDecision <= 0) { System.err.printf("No data for period (%d)\n", allocationSizeDecision); return null; } double[] outputDec = new double[allocationSizeDecision]; MInteger outBegIdxDec = new MInteger(); MInteger outNbElementDec = new MInteger(); RetCode decCode = core.mom(0, period - 1, input, decMOMPeriod, outBegIdxDec, outNbElementDec, outputDec); if (decCode.compareTo(RetCode.Success) != 0) { // Error return empty method results throw new java.lang.IllegalStateException("MOM failed"); } // TODO: Evaluate and store best config int direction = 0; boolean changed = true; for (int elem = 1; elem < outNbElementDec.value; elem++) { // [elem-1] > [elem] => ln([elem-1]/[elem]) > 0) if (outputDec[elem - 1] > outputDec[elem]) { // stock is falling if (direction == 1 && amoutOfStocks != 0) { // selling, Price went to the top and starts to fall changed = true; // Price is going down assumedBudjet = amoutOfStocks * input[elem + outBegIdxDec.value]; amoutOfStocks = 0; System.out.printf( "%s selling for:" + input[elem + outBegIdxDec.value] + " budjet:%f per:%d bestper:%f\n", stockName, assumedBudjet, decMOMPeriod, bestPeriod); if (bestAssumedBudjet < assumedBudjet) { bestAssumedBudjet = assumedBudjet; bestPeriod = decMOMPeriod; } } direction = -1; } else { // buying, Price went to the buttom and raising if (direction == -1 && amoutOfStocks == 0) { changed = true; // Price is going up amoutOfStocks = assumedBudjet / input[elem + outBegIdxDec.value]; System.out.printf( "%s buying for:" + input[elem + outBegIdxDec.value] + " budjet:%f period:%d bestper:%f\n", stockName, assumedBudjet, decMOMPeriod, bestPeriod); } direction = 1; } if (changed) { if (config.inputPrintResults && decMOMPeriod == config.inputMOMPeriod) { DateIterator dateIterator = new DateIterator( portfolioConfig.inputStartingDate, portfolioConfig.inputEndingDate); dateIterator.move(elem + outBegIdxDec.value); sender.setSeriesName("Sell/Buy signals"); sender.addForSending( dateIterator.getCurrent(), input[elem + outBegIdxDec.value] + 0.1); } } } } double successRate = ((bestAssumedBudjet / portfolioConfig.inputAssumedBudjet) - 1) * 100; System.out.printf( "%s:The best period:%f best budjet:%f pros:%f\n", stockName, bestPeriod, bestAssumedBudjet, successRate); totalStocksAnalyzed++; this.avgSuccessRate += successRate; this.config.outputSuccessRate = successRate; this.config.inputMOMPeriod = (int) bestPeriod; this.configFile.storeConfig(config); } methodResults.setAvrSuccessRate(avgSuccessRate / totalStocksAnalyzed); System.out.printf( "%s has %d successrate\n", this.getMethName(), methodResults.getAvrSuccessRate().intValue()); return methodResults; }
public int getRequiredHistorySize() { if (this.function == Function.EMA) { Core core = new Core(); int lookback = core.emaLookback(day); return ((lookback + 1) << 2); } else if (function == Function.RSI) { Core core = new Core(); int lookback = core.rsiLookback(day); return ((lookback + 1) << 2); } else if (this.function == Function.MFI) { Core core = new Core(); int lookback = core.mfiLookback(day); return ((lookback + 1) << 2); } else if (this.function == Function.MACD) { Core core = new Core(); int lookback = core.macdFixLookback(day); return ((lookback + 1) << 2); } else if (this.function == Function.MACDSignal) { Core core = new Core(); int lookback = core.macdFixLookback(day); return ((lookback + 1) << 2); } else if (this.function == Function.MACDHist) { Core core = new Core(); int lookback = core.macdFixLookback(day); return ((lookback + 1) << 2); } else { return day; } }