Page 1 of 1

How to add voice alert in custom indicator

Posted: Wed Aug 28, 2019 9:30 pm
by trades4x
hi,

I want to add a voice alert in BarBuilder indicator when BuyVolume > SellVolume by X%, can anyone point to sample code for adding voice alerts.

Thanks,
trades4x

Re: How to add voice alert in custom indicator

Posted: Thu Aug 29, 2019 1:39 pm
by Andry API support
Here is a slightly modified BarBuilder which performes a voice alert any time a new bar comes. Feel free to modify it so it meets the desired conditions. Explore the SoundSynthHelper and Layer1ApiSoundAlertMessage classes if you need more info.
Please note that text processing should be performed in a separate thread.
Also note the BarBuilder no longer implements the HistoricalDataListener so voice alerts start right at the moment the module is activated.

Code: Select all

public class BarBuilder extends DataRecorderBase implements CustomModule, TradeDataListener, TimeListener {

    protected Long barTime = null;
    protected final long barInterval = Intervals.INTERVAL_2_SECONDS;
    private Bar bar = new Bar();
    int num;
    ExecutorService executor = Executors.newSingleThreadExecutor();

    @Override
    public void onTimestamp(long t) {
        if (barTime == null || barTime > t) {
            barTime = barInterval * (t / barInterval);
        }
        while (barTime + barInterval < t) {
            barTime += barInterval;
            onBar();
            bar.startNext();
        }
    }

    @Override
    public void onTrade(double price, int size, TradeInfo tradeInfo) {
        bar.addTrade(tradeInfo.isBidAggressor, size, price);
        writeObjects(getDateTime(barTime), tradeInfo.isBidAggressor ? "Buy" : "Sell", price, size);
    }

    @Override
    public void initialize(String alias, InstrumentInfo info, Api api, InitialState initialState) {
    }

    protected void onBar() {
        String datetime = getDateTime(barTime);
        String info = String.format("onBar time: %s. OHLC: %.0f, %.0f, %.0f, %.0f. Volume Buy Sell: %d, %d", datetime,
                bar.getOpen(), bar.getHigh(), bar.getLow(), bar.getClose(), bar.getVolumeBuy(), bar.getVolumeSell());
        Log.info(info);
        
        byte[] bytes = SoundSynthHelper.synthesize("Bar number " + ++num);

        Thread t = new Thread(() -> {
            try (AudioInputStream stream = AudioSystem.getAudioInputStream(new ByteArrayInputStream(bytes))) {
                AudioFormat format = stream.getFormat();
                DataLine.Info line = new DataLine.Info(Clip.class, format);
                Clip clip = (Clip) AudioSystem.getLine(line);
                clip.open(stream);
                clip.start();
            } catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        });
        executor.execute(t);
    }

    @Override
    protected String getFilename() {
        return "Volume_" + System.currentTimeMillis() + ".txt";
    }
    @Override
    public void stop(){
        executor.shutdownNow();
    }
}

Re: How to add voice alert in custom indicator

Posted: Sun Sep 01, 2019 3:53 am
by trades4x
Yeah I am facing this problem that it starts activating as soon as it is loaded and often it finds events in excess of 13000+ and calling these voice alerts freezes the bookmap.

Any hints or sample code to avoid activating voice alert code forĀ  historical data except when replaying..

really need help on this ...

Re: How to add voice alert in custom indicator

Posted: Sun Sep 01, 2019 8:28 am
by Andry API support
Any hints or sample code to avoid activating voice alert code for historical data
Your class must not implement HistoricalDataListener.
public class BarBuilder extends DataRecorderBase implements CustomModule, TradeDataListener, TimeListener, HistoricalDataListener{

Re: How to add voice alert in custom indicator

Posted: Sun Sep 01, 2019 10:15 pm
by trades4x
thks, that suggestion did the job.