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 to read indicator value directly from chart? (not from FeedDescriptor)
 Post subject: How to read indicator value directly from chart? (not from FeedDescriptor) Post rating: 0   New post Posted: Thu 15 Aug, 2013, 17:22 

User rating: 0
Joined: Wed 12 Jun, 2013, 16:19
Posts: 18
Location: PolandPoland
Hi

It's possible to read indicator value directy from chart?
For example:

//...
public void onStart(IContext context) throws JFException {
       
        this.chart = context.getChart(instrument);
       
        for (IIndicator indicator : this.chart.getIndicators()) {
            if( indicator.getIndicatorInfo().getName().equals("OsMA") ) {
                    // how can I read indicator value here?   
            }
        }
}


 
 Post subject: Re: How to read indicator value directly from chart? (not from FeedDescriptor) Post rating: 1   New post Posted: Fri 16 Aug, 2013, 09:17 
User avatar

User rating: 164
Joined: Mon 08 Oct, 2012, 10:35
Posts: 676
Location: NetherlandsNetherlands
If I am not mistaken, IChartPanel.getIndicatorApperanceInfos() was created not long ago for this purpose.

Edit: I think I misunderstood the question.
The function I mentioned is providing you the indicator parameters.
If you need indicator value, check out this wiki page, or the other dozens of examples available on this forum...


 
 Post subject: Re: How to read indicator value directly from chart? (not from FeedDescriptor) Post rating: 0   New post Posted: Fri 16 Aug, 2013, 14:49 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
tcsabina wrote:
If I am not mistaken, IChartPanel.getIndicatorApperanceInfos() was created not long ago for this purpose.
This is the case. We added a new example in the wiki:
https://www.dukascopy.com/wiki/#Add_indicators_on_chart/Use_on-chart_indicator_parameters


 
 Post subject: Re: How to read indicator value directly from chart? (not from FeedDescriptor) Post rating: 0   New post Posted: Thu 22 Aug, 2013, 16:54 

User rating: 0
Joined: Wed 12 Jun, 2013, 16:19
Posts: 18
Location: PolandPoland
API Support wrote:


But this is not resolve my problem. Because my problem is placed in a low application performance - that is the reason why i need to read a indicator value directly from chart.
For example:
If I add the Osma indicator too chart (manual, using JForex GUI) and after that I try to read the same value using "indicators.calculateIndicator(..)" method then (probably) I initialize the same indicator object twice in the same time (tick). I'ts not a good solution because that gives me a very low app performance (try to imagine - what happend when I add 3 different OSMA's to chart and after that I wanna try to read their values? :) That is a very bad idea because it is a too slowwww )

Hmm.. I have an idea - maybe there's a way to read the current indicator values directly from OHLC window? Eg: https://www.dukascopy.com/wiki/#Add_indi ... de_in_OHLC


 
 Post subject: Re: How to read indicator value directly from chart? (not from FeedDescriptor) Post rating: 0   New post Posted: Tue 27 Aug, 2013, 13:38 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Could you please provide an example case for your underperforming indicator calculation?


 
 Post subject: Re: How to read indicator value directly from chart? (not from FeedDescriptor) Post rating: 0   New post Posted: Tue 27 Aug, 2013, 15:57 

User rating: 0
Joined: Wed 12 Jun, 2013, 16:19
Posts: 18
Location: PolandPoland
API Support wrote:
Could you please provide an example case for your underperforming indicator calculation?


For example:
private IFeedDescriptor chartFeed;
private IChart          chart;

@Override
public void onStart(IContext context) throws JFException {
        this.chart = context.getLastActiveChart();
       
        // Read feed from opened chart
        this.chartFeed = chart.getFeedDescriptor();
}

@Override
public void onTick(Instrument instrument, ITick tick) throws JFException {
        Object[] osmaFeed =  indicators.calculateIndicator(  this.chartFeed, new OfferSide[]{chartFeed.getOfferSide()}, "OSMA", new AppliedPrice[]{AppliedPrice.CLOSE}, new Object[]{12,26,5}, 0);
        Object[] osmaFeed =  indicators.calculateIndicator(  this.chartFeed, new OfferSide[]{chartFeed.getOfferSide()}, "OSMA", new AppliedPrice[]{AppliedPrice.CLOSE}, new Object[]{50,110,25}, 0);
        Object[] osmaFeed =  indicators.calculateIndicator(  this.chartFeed, new OfferSide[]{chartFeed.getOfferSide()}, "OSMA", new AppliedPrice[]{AppliedPrice.CLOSE}, new Object[]{290,630,120}, 0);
}


When I try to get a 3 different OSMA values in the same time then my strategy is slowing down (I tested it in strategy tester). Finally when I disable these code and run test without initialize a calculateIndicator() metods (but with the same OSMAs - inserted with template) then tester works very fast


 
 Post subject: Re: How to read indicator value directly from chart? (not from FeedDescriptor) Post rating: 0   New post Posted: Tue 27 Aug, 2013, 16:03 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
You need to subscribe to the feeds that you use in order to increase the performance, see the last sentence in the description here:
https://www.dukascopy.com/wiki/#Indicator_Calculation/Universal_price_feed
Also there you will find the example how to do it.


 
 Post subject: Re: How to read indicator value directly from chart? (not from FeedDescriptor) Post rating: 0   New post Posted: Tue 27 Aug, 2013, 17:00 

User rating: 0
Joined: Wed 12 Jun, 2013, 16:19
Posts: 18
Location: PolandPoland
API Support wrote:
You need to subscribe to the feeds that you use in order to increase the performance, see the last sentence in the description here:
https://www.dukascopy.com/wiki/#Indicator_Calculation/Universal_price_feed
Also there you will find the example how to do it.


Thanks for the quick answer. I try to Your solution and my strategy is still working slow. Here is a modified example code (with added lines):
private IFeedDescriptor chartFeed;
private IChart          chart;
private IFeedDescriptor rangeBarFeedDescriptor; // added line

@Override
public void onStart(IContext context) throws JFException {
        //this.chart = context.getLastActiveChart(); // removed
        this.rangeBarFeedDescriptor = new RenkoFeedDescriptor(instrument, PriceRange.valueOf(10), OfferSide.BID);  // added line

        // Read feed from opened chart
        this.chartFeed = this.rangeBarFeedDescriptor; // modified line
        context.subscribeToFeed(this.chartFeed, this); // added line
}

@Override
public void onTick(Instrument instrument, ITick tick) throws JFException {
        Object[] osmaFeed =  indicators.calculateIndicator(  this.chartFeed, new OfferSide[]{chartFeed.getOfferSide()}, "OSMA", new AppliedPrice[]{AppliedPrice.CLOSE}, new Object[]{12,26,5}, 0);
        Object[] osmaFeed =  indicators.calculateIndicator(  this.chartFeed, new OfferSide[]{chartFeed.getOfferSide()}, "OSMA", new AppliedPrice[]{AppliedPrice.CLOSE}, new Object[]{50,110,25}, 0);
        Object[] osmaFeed =  indicators.calculateIndicator(  this.chartFeed, new OfferSide[]{chartFeed.getOfferSide()}, "OSMA", new AppliedPrice[]{AppliedPrice.CLOSE}, new Object[]{290,630,120}, 0);
}


Maybe the problem is placed in chart type - I use the renko chart


 
 Post subject: Re: How to read indicator value directly from chart? (not from FeedDescriptor) Post rating: 1   New post Posted: Tue 27 Aug, 2013, 20:25 
User avatar

User rating: 164
Joined: Mon 08 Oct, 2012, 10:35
Posts: 676
Location: NetherlandsNetherlands
As a workaround:
Do you really need to do this in onTick()? When you work with charts, do you have them with tick period? So your strategy is based on tick data?
You can have many (3-4, or even more?) ticks per second, which means that the onTick() function will be executed as many times (which you might already know).

I better place would be the onBar() function, which is only called during bar/candle creation/close.

Of course, if you do need actions to be taken on tick manner, this is out from the question.

Edit: something else:
As you stated that you have tested with the strategy tester, you have to keep in mind that the speed that you see during historical test is depend on other things as well. For example if you test with visual mode, the test is slower. And if you place an indicator on the chart of the historical tester, the speed decreases dramatically (if you don't run it on a super-duper computer).
Do you see issues with execution on the onTick() function if you run the strategy live? If that is ' normal', you could try to not to add indicators on the chart during historical test, or don't use at all the visual mode of the tester.

Hope this helps.


 
The Best Answer  Post subject: Re: How to read indicator value directly from chart? (not from FeedDescriptor) Post rating: 0   New post Posted: Wed 28 Aug, 2013, 16:03 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
The biggest calculation time is for the osma with slow period 630, which commands loading of 730 candles (+100 because of unstable period parameter) on each indicator calculation - on our machine it takes around 2 seconds for the calcuation to complete. In comparison the OsMA with optional inputs {12,26,5} usually takes less than 0.001 seconds to complete. On chart the indicator calculation takes the same time, meaning if the indicator calculates in 2 seconds, you won't see "fresher" values on the chart than through JForex-API. You don't see the delay in the platform during these 2 seconds of calculation since the indicators get calculated asynchronously - as consequence of that the on-chart values of big-timePeriod indicators don't get refreshed on every new tick, rather as soon as the calculation is complete.

There are several ways how you can go about solving this performance issue:
  1. Moving the calculation from onTick to onFeedData - calculate only when a feed element gets formed.
  2. Calculate the indicators asynchronously and always work with the last available calculated values. Here you would need to be careful while doing historical testing as if the calculation completes in 2 seconds, the historical time might be already a couple of minutes ahead. See the example below.
  3. Calculate indicators on array (note that OsMA is of unstable period - you would need to fetch extra 100 feed input prices),see: https://www.dukascopy.com/wiki/#Indicator_Calculation/Calculate_indicator_on_array
Thus you should weigh those options depending on the strategy logic and choose the most suitable one.

Consider calculating the indicators asynchronously:
public class FeedOsmaAsync implements IStrategy, IFeedListener {

    @Configurable("")
    public IFeedDescriptor feedDescriptor = new RenkoFeedDescriptor(Instrument.EURUSD, PriceRange.valueOf(10), OfferSide.BID);

    private Object[][] optInputSets = new Object[][] { new Object[] { 12, 26, 5 }, new Object[] { 50, 110, 25 }, new Object[] { 290, 630, 120 } };
    private Map<Object[], Double> lastOsmas = new HashMap<Object[], Double>();
    private Set<ExecutorService> executors = new HashSet<ExecutorService>();

    //...
   
    /**
     * We calculate every indicator in its own thread
     *
     * @throws JFException
     */
    private void scheduleOsmasCalculation() throws JFException {
        for (final Object[] optInputs : optInputSets) {
            ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
            executors.add(exec);
            //every 100 millis we recalculate the last osma value in case a new tick has arrived
            exec.scheduleAtFixedRate(new Runnable() {
               
                ITick lastTick = null;
               
                @Override
                public void run() {                   
                    long start = System.nanoTime();
                    try {
                        ITick tick = history.getLastTick(feedDescriptor.getInstrument());
                        if(tick.equals(lastTick)){
                            return; //same tick
                        }
                        lastTick = tick;
                         Object[] osmaResult  = indicators.calculateIndicator(feedDescriptor, new OfferSide[] { feedDescriptor.getOfferSide() }, "OSMA",
                                new AppliedPrice[] { AppliedPrice.CLOSE }, optInputs, shift);
                        double osmalLast = (Double) osmaResult[0];
                        synchronized(lastOsmas){
                            lastOsmas.put(optInputs, osmalLast);
                        }
                        print("shift=%s last osma%s=%.5f in %.6f secs", shift, Arrays.toString(optInputs), osmalLast,
                                ((double) (System.nanoTime() - start)) / 1000000000);
                    } catch (JFException e) {
                        console.getErr().println(e);
                        e.printStackTrace();
                    }

                }
            }, 0, 100, TimeUnit.MILLISECONDS);
        }
    }
    //...
    @Override
    public void onStop() throws JFException {
        for(ExecutorService exec : executors){
            exec.shutdown();
        }
    }
}


Attachments:
FeedOsmaAsync.java [6.09 KiB]
Downloaded 1144 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 to read indicator value directly from chart? (not from FeedDescriptor) Post rating: 0   New post Posted: Wed 28 Aug, 2013, 16:07 

User rating: 0
Joined: Wed 12 Jun, 2013, 16:19
Posts: 18
Location: PolandPoland
tcsabina wrote:
As a workaround:
Hope this helps.


API Support wrote:
The biggest calculation time is for the osma with slow period 630, which commands loading of 730 candles (+100 because of unstable period parameter) on each indicator calculation - on our machine it takes around 2 seconds for the calcuation to complete. In comparison the OsMA with optional inputs {12,26,5} usually takes less than 0.001 seconds to complete. On chart the indicator calculation takes the same time, meaning if the indicator calculates in 2 seconds, you won't see "fresher" values than you see on the chart. You don't see the delay in the platform during these 2 seconds of calculation since the indicators get calculated asynchronously - as consequence of that the on-chart values of big-timePeriod indicators don't get refreshed on every new tick, rather as soon as the calculation is complete.


Thank You for many useful informations - i try to use this solutions in my strategy :).


 
 Post subject: Re: How to read indicator value directly from chart? (not from FeedDescriptor) Post rating: 0   New post Posted: Wed 28 Aug, 2013, 21:06 
User avatar

User rating: 164
Joined: Mon 08 Oct, 2012, 10:35
Posts: 676
Location: NetherlandsNetherlands
Dear Support,

Could you make a wiki page out from this? I think this is quite usefull, and would be nice for further references.


 
 Post subject: Re: How to read indicator value directly from chart? (not from FeedDescriptor) Post rating: 0   New post Posted: Fri 30 Aug, 2013, 16:09 

User rating: 0
Joined: Wed 12 Jun, 2013, 16:19
Posts: 18
Location: PolandPoland
tcsabina wrote:
Dear Support,

Could you make a wiki page out from this? I think this is quite usefull, and would be nice for further references.


That's true :)


 

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