Dukascopy
 
 
Wiki JStore Search Login

Attention! Read the forum rules carefully before posting a topic.

    Try to find an answer in Wiki before asking a question.
    Submit programming questions in this forum only.
    Off topics are strictly forbidden.

Any topics which do not satisfy these rules will be deleted.

How can I get the current trend from an indicator based of output colors ??
 Post subject: How can I get the current trend from an indicator based of output colors ?? Post rating: 0   New post Posted: Thu 28 Dec, 2017, 14:39 
User avatar

User rating: 0
Joined: Sun 04 Sep, 2016, 05:22
Posts: 5
Location: AustraliaAustralia
Hi there, I am trying to get in jforex code the value of the current color of any indicator using the chart.getIndicatorApperanceInfos()
and the method getOutputColors()
The indicator on the chart is SMA(21) and the setup of output colors are trend up = blue and trend down = red.

Image


I am using the code provided by jforex wiki:

-----------------------------

package jforex.charts.ind;
import java.util.Arrays;
import com.dukascopy.api.DataType;
import com.dukascopy.api.IAccount;
import com.dukascopy.api.IBar;
import com.dukascopy.api.IChart;
import com.dukascopy.api.IConsole;
import com.dukascopy.api.IContext;
import com.dukascopy.api.IHistory;
import com.dukascopy.api.IIndicators;
import com.dukascopy.api.IIndicators.AppliedPrice;
import com.dukascopy.api.IMessage;
import com.dukascopy.api.IStrategy;
import com.dukascopy.api.ITick;
import com.dukascopy.api.ITimedData;
import com.dukascopy.api.Instrument;
import com.dukascopy.api.JFException;
import com.dukascopy.api.OfferSide;
import com.dukascopy.api.Period;
import com.dukascopy.api.feed.IFeedDescriptor;
import com.dukascopy.api.indicators.IIndicator;
import com.dukascopy.api.indicators.IIndicatorAppearanceInfo;
import com.dukascopy.api.indicators.OutputParameterInfo;
import java.awt.Color;

import java.util.HashSet;
import com.dukascopy.api.feed.*;
import com.dukascopy.api.feed.util.*;

/**
* The strategy calculates indicators with the same parameters
* as they appear on the last selected chart
*
*/
public class CalculateIndicatorsFromChart implements IStrategy {

private IIndicators indicators;
private IHistory history;
private IConsole console;
private IChart chart;

public IFeedDescriptor feedDescriptor = new TimePeriodAggregationFeedDescriptor(Instrument.EURUSD, Period.TICK, OfferSide.ASK);
private int dataCount = 3;

@Override
public void onStart(IContext context) throws JFException {
indicators = context.getIndicators();
history = context.getHistory();
console = context.getConsole();
chart = context.getLastActiveChart();
if (chart == null) {
console.getErr().println("No chart opened!");
return;
}

context.setSubscribedInstruments(new HashSet<Instrument>(Arrays.asList(new Instrument[] { feedDescriptor.getInstrument() })), true);



IFeedDescriptor feedDescriptor = chart.getFeedDescriptor();
if(feedDescriptor.getDataType() == DataType.TICKS){
console.getWarn().println("Tick charts need to get calculate with from-to method");
return;
}

for (IIndicatorAppearanceInfo info : chart.getIndicatorApperanceInfos()) {
AppliedPrice[] appliedPrices = new AppliedPrice[info.getDrawingStyles().length];
Arrays.fill(appliedPrices, AppliedPrice.CLOSE);
OfferSide[] offerSides = new OfferSide[info.getDrawingStyles().length];
Arrays.fill(offerSides, chart.getSelectedOfferSide());
IIndicator indicator = indicators.getIndicator(info.getName());
ITimedData feedData = history.getFeedData(feedDescriptor, 0);

Color[] testColor = info.getOutputColors(); // check if give the color of indicator each tick/bar

Object[] result = indicators.calculateIndicator(feedDescriptor, offerSides, info.getName(), appliedPrices, info.getOptParams(), dataCount, feedData
.getTime(), 0);
for (int i = 0; i < indicator.getIndicatorInfo().getNumberOfOutputs(); i++) {
OutputParameterInfo.Type outputType = indicator.getOutputParameterInfo(i).getType();
String resultStr =
outputType == OutputParameterInfo.Type.DOUBLE ? Arrays.toString((double[]) result[i])
: outputType == OutputParameterInfo.Type.INT ? Arrays.toString((int[]) result[i])
: "object outputs need special processing";
console.getOut().format("%s %s last %s values: %s", info.getName(), indicator.getOutputParameterInfo(i).getName(), dataCount, resultStr).println();
console.getOut().println("testColor:"+testColor[i]); //print the color

}

}
context.stop();
}

@Override
public void onTick(Instrument instrument, ITick tick) throws JFException { }

@Override
public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {}

@Override
public void onMessage(IMessage message) throws JFException {}

@Override
public void onAccount(IAccount account) throws JFException {}

@Override
public void onStop() throws JFException {}

}


-----------------------------

For now I am printing only at the start to check the results.
but the code is printing the same color all the time, below the log:

13:29:13 testColor:java.awt.Color[r=64,g=64,b=255]
13:29:13 SMA Line last 3 values: [113.09614285714287, 113.07490476190478, 113.05504761904764]
13:16:49 testColor:java.awt.Color[r=64,g=64,b=255]
13:16:49 SMA Line last 3 values: [112.86409523809525, 112.86580952380953, 112.86761904761906]


Can anyone suggest the proper way to get that value ?
Or any other idea to get a flag to get informed every tick / bar when the indicator changes from uptrend to downtrend and downtrend to uptrend to trigger any action ??


Attachments:
Chart_USD_JPY_1 Min_snapshot.png [13.41 KiB]
Downloaded 240 times
DISCLAIMER: Dukascopy Bank SA's waiver of responsability - Documents, data or information available on this webpage may be posted by third parties without Dukascopy Bank SA being obliged to make any control on their content. Anyone accessing this webpage and downloading or otherwise making use of any document, data or information found on this webpage shall do it on his/her own risks without any recourse against Dukascopy Bank SA in relation thereto or for any consequences arising to him/her or any third party from the use and/or reliance on any document, data or information found on this webpage.
 
 Post subject: Re: How can I get the current trend from an indicator based of output colors ?? Post rating: 0   New post Posted: Fri 05 Jan, 2018, 17:32 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Here is an example how you can compare sma values:

import com.dukascopy.api.Filter;
import com.dukascopy.api.IAccount;
import com.dukascopy.api.IBar;
import com.dukascopy.api.IConsole;
import com.dukascopy.api.IContext;
import com.dukascopy.api.IIndicators;
import com.dukascopy.api.IIndicators.AppliedPrice;
import com.dukascopy.api.IMessage;
import com.dukascopy.api.IStrategy;
import com.dukascopy.api.ITick;
import com.dukascopy.api.Instrument;
import com.dukascopy.api.JFException;
import com.dukascopy.api.OfferSide;
import com.dukascopy.api.Period;

public class StrategyForDetectingTrend implements IStrategy {
  private IConsole console;
  private IContext context;
  private IIndicators indicators;

  Instrument instrument = Instrument.EURUSD;
  Period period = Period.ONE_MIN;
  int timePeriod = 20;

  @Override
  public void onStart(IContext context) throws JFException {
    this.context = context;
    this.console = context.getConsole();
    this.indicators = context.getIndicators();
  }

  @Override
  public void onTick(Instrument instrument, ITick tick) throws JFException {
  }

  @Override
  public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar)
      throws JFException {
    if (instrument != this.instrument || period != this.period) return;
    double[] sma = indicators.sma(instrument, period, OfferSide.ASK, AppliedPrice.CLOSE, timePeriod,
        Filter.NO_FILTER, 3, askBar.getTime(), 0);
    if (sma[1] > sma[0] && sma[2] < sma[1]) {
      //Switched from uptrend to downtrend
      //Logic for this case
    }
    if (sma[1] < sma[0] && sma[2] > sma[1]) {
      //Switched from downtrend to uptrend
      //Logic for this case
    }
  }

  @Override
  public void onMessage(IMessage message) throws JFException {

  }

  @Override
  public void onAccount(IAccount account) throws JFException {

  }

  @Override
  public void onStop() throws JFException {

  }
}


 

Jump to:  

  © 1998-2025 Dukascopy® Bank SA
On-line Currency forex trading with Swiss Forex Broker - ECN Forex Brokerage,
Managed Forex Accounts, introducing forex brokers, Currency Forex Data Feed and News
Currency Forex Trading Platform provided on-line by Dukascopy.com