Dukascopy Support Board
http://www.dukascopy.com/swiss/english/forex/jforex/forum/

Failing to unsubscribe to an instrument
http://www.dukascopy.com/swiss/english/forex/jforex/forum/viewtopic.php?f=65&t=57517
Page 1 of 1

Author:  pouet300 [ Fri 01 Jan, 2021, 18:15 ]
Post subject:  Failing to unsubscribe to an instrument

Hello,

I'm struggling to unsubscribe to instruments with my strategy :? .
I need to fetch historic data on many markets and perform a scoring, then to enter market on best fitted instruments only.
Basically, I would like to subscribe to instruments one by one, fetch history then unsubscribe.
When scoring is done, only subscribe to the p best...

The below sample code reproduce the issue with minimal steps, when I run the strategy, the subscribed instruments grows (not expected).
And the bars still continue to come such as message related to those instruments

This cause significative performance issues and I can't implement my strategy because of that.
Do you have any recommendation how to properly unsubscribe?

I have tried with all charts closed, I guess there are implied data feed subscribed with IContext.setSubscribedInstruments

Many thanks for your help.

package lu.panacea.stock;

import java.util.Collections;
import java.util.Set;

import com.dukascopy.api.IAccount;
import com.dukascopy.api.IBar;
import com.dukascopy.api.IConsole;
import com.dukascopy.api.IContext;
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.Period;
import com.dukascopy.api.instrument.IFinancialInstrument;
import com.dukascopy.api.instrument.IInstrumentGroup;

public class Issue implements IStrategy{
    private IConsole console;
    private static final String STOCK_GROUP="STK_CASH";
   
    @Override
    public void onStart(IContext context) throws JFException{
        console=context.getConsole();
        @SuppressWarnings("deprecation")
        IInstrumentGroup group=context.getFinancialInstrumentProvider().getGroup(STOCK_GROUP);
        int nb=0;
        for(IFinancialInstrument instrument:group.getInstruments()){
            Instrument instr=(Instrument)instrument;
            if(instr.getCountry().equals("US")) continue;
            if(nb==10) break;
            Set<Instrument> s=Collections.singleton(instr);
            context.setSubscribedInstruments(s,true);
            /*
                Technical analysis on price history here
            */
            context.unsubscribeInstruments(s);
            nb++;
        }
    }
   
    @Override
    public void onTick(Instrument instrument, ITick tick) throws JFException{
        console.getInfo().println("Tick >>>"+instrument+" -- "+tick);
    }

    @Override
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException{
        console.getInfo().println("Bar >>> "+instrument+" "+period+" "+askBar+" "+bidBar);
    }

    @Override
    public void onMessage(IMessage message) throws JFException{
        console.getInfo().println("MSG >>> "+message);
    }

    @Override
    public void onAccount(IAccount account) throws JFException{
        console.getInfo().println("Account >>> "+account);
    }

    @Override
    public void onStop() throws JFException{}
}


Author:  API Support [ Tue 09 Feb, 2021, 17:53 ]
Post subject:  Re: Failing to unsubscribe to an instrument

Greetings.

When strategy subscribes to instrument, request for full-depth subscription is sent asynchronously. Therefore, it can interleave with subsequent unsubscription request, with the result that instrument remains subscribed.

Strategy should postpone unsubscription request to avoid interleaving with request for full-depth subscription:

@Override
public void onStart(IContext context) throws JFException{
console=context.getConsole();
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
@SuppressWarnings("deprecation")
IInstrumentGroup group=context.getFinancialInstrumentProvider().getGroup(STOCK_GROUP);
int nb=0;
for(IFinancialInstrument instrument:group.getInstruments()){
Instrument instr=(Instrument)instrument;
if(instr.getCountry().equals("US")) continue;
if(nb==10) break;
Set<Instrument> s=Collections.singleton(instr);
context.setSubscribedInstruments(s,true);
/*
Technical analysis on price history here
*/
scheduler.schedule(() -> context.unsubscribeInstruments(s), 10, TimeUnit.SECONDS);
nb++;
}
scheduler.shutdown();
}
Instruments for conversion to account currency are automatically subscribed when strategy subscribes to some instrument. They remain subscribed after executing code above, because strategy does not sent request for unsubscribing from them.

Regards.

  Page 1 of 1