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.

Failing to unsubscribe to an instrument
 Post subject: Failing to unsubscribe to an instrument Post rating: 0   New post Posted: Fri 01 Jan, 2021, 18:15 

User rating: 0
Joined: Fri 01 Jan, 2021, 17:58
Posts: 1
Location: LuxembourgLuxembourg
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{}
}



 
 Post subject: Re: Failing to unsubscribe to an instrument Post rating: 0   New post Posted: Tue 09 Feb, 2021, 17:53 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
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.


 

Jump to:  

cron
  © 1998-2024 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