How to add voice alert in custom indicator

Custom indicators, trading strategies, data export and recording and more...
trades4x
Posts: 10
Joined: Sat Jul 13, 2019 8:52 pm
Has thanked: 5 times
Been thanked: 3 times

How to add voice alert in custom indicator

Post by trades4x » Wed Aug 28, 2019 9:30 pm

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

Andry API support
Posts: 548
Joined: Mon Jul 09, 2018 11:18 am
Has thanked: 25 times
Been thanked: 85 times

Re: How to add voice alert in custom indicator

Post by Andry API support » Thu Aug 29, 2019 1:39 pm

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();
    }
}

trades4x
Posts: 10
Joined: Sat Jul 13, 2019 8:52 pm
Has thanked: 5 times
Been thanked: 3 times

Re: How to add voice alert in custom indicator

Post by trades4x » Sun Sep 01, 2019 3:53 am

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 ...

Andry API support
Posts: 548
Joined: Mon Jul 09, 2018 11:18 am
Has thanked: 25 times
Been thanked: 85 times

Re: How to add voice alert in custom indicator

Post by Andry API support » Sun Sep 01, 2019 8:28 am

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{

trades4x
Posts: 10
Joined: Sat Jul 13, 2019 8:52 pm
Has thanked: 5 times
Been thanked: 3 times

Re: How to add voice alert in custom indicator

Post by trades4x » Sun Sep 01, 2019 10:15 pm

thks, that suggestion did the job.

Post Reply