Footprint bars computation (an example of API usage)

Custom indicators, trading strategies, data export and recording and more...
Serg
Posts: 113
Joined: Mon Jun 11, 2018 12:40 pm
Has thanked: 15 times
Been thanked: 35 times

Footprint bars computation (an example of API usage)

Post by Serg » Fri Oct 11, 2019 7:56 pm

Here is a demonstration of Bookmap API usage to compute footprint bars. Notes:
  • Adding HistoricalDataListener allows the indicator to receive data from the instrument subscription moment
  • It's a demonstration of computation, without visualization
  • Attached the source code, and compiled jar file ready for importing into Bookmap

Code: Select all

@Layer1SimpleAttachable
@Layer1StrategyName("FP Bars")
@Layer1ApiVersion(Layer1ApiVersionValue.VERSION1)
public class FpBars implements CustomModuleAdapter, TradeDataListener, HistoricalDataListener {

    private static class FpBar {

        static private final long THRESHOLD = 5000;
        private final Map<Long, Long> bids = new TreeMap<>(Comparator.reverseOrder());
        private final Map<Long, Long> asks = new TreeMap<>();
        private long volume = 0;

        private boolean addTrade(boolean isBuy, long price, long size) {
            (isBuy ? asks : bids).merge(price, size, (x, y) -> x + y); // add buy volume to asks, and vice versa
            return (volume += size) >= THRESHOLD;
        }
    }

    private FpBar currentBar = new FpBar();
    private final List<FpBar> bars = new ArrayList<>(Arrays.asList(currentBar));
    private final Gson gson = new Gson(); // just for logging

    @Override
    public void onTrade(double price, int size, TradeInfo tradeInfo) {
        boolean isBarCompleted = currentBar.addTrade(tradeInfo.isBidAggressor, (long) Math.round(price), size);
        if (isBarCompleted) {
            Log.info(String.format("FP Bars: volume: %d; bids: %s; asks: %s", currentBar.volume,
                    gson.toJson(currentBar.bids), gson.toJson(currentBar.asks)));
            currentBar = new FpBar();
            bars.add(currentBar);
        }
    }
}

We can see its log entries in corresponding log file (in C:\Bookmap\Logs) or copy them into another file via a command line:

Code: Select all

grep "FP Bars" <path to the log file> > fp-bars.txt
which produces a file like this:

Code: Select all

20191011 19:06:52.367(UTC) INFO: FP Bars: volume: 5000; bids: {"12024":321,"12023":85,"12022":523,"12021":468,"12020":436,"12019":112}; asks: {"12019":155,"12020":519,"12021":407,"12022":408,"12023":245,"12024":594,"12025":245,"12026":281,"12027":201}
20191011 19:06:52.382(UTC) INFO: FP Bars: volume: 5000; bids: {"12042":32,"12041":16,"12040":81,"12039":8,"12038":8,"12037":38,"12036":30,"12035":57,"12034":36,"12033":186,"12032":141,"12031":8,"12030":18}; asks: {"12019":0,"12027":86,"12028":256,"12029":293,"12030":329,"12031":290,"12032":202,"12033":244,"12034":673,"12035":492,"12036":621,"12037":138,"12038":208,"12039":88,"12040":128,"12041":132,"12042":157,"12043":4}
20191011 19:06:52.408(UTC) INFO: FP Bars: volume: 5000; bids: {"12054":120,"12053":112,"12052":79,"12051":319,"12050":32,"12049":288,"12048":166,"12047":62,"12046":6,"12045":3,"12044":60,"12043":284,"12042":359,"12041":41}; asks: {"12042":144,"12043":641,"12044":250,"12045":101,"12046":103,"12047":163,"12048":220,"12049":241,"12050":193,"12051":294,"12052":222,"12053":96,"12054":271,"12055":130}
20191011 19:06:52.451(UTC) INFO: FP Bars: volume: 5001; bids: {"12062":53,"12061":35,"12060":53,"12059":204,"12058":324,"12057":146,"12056":176,"12055":176,"12054":165,"12053":214,"12052":171,"12051":142,"12050":130,"12049":36}; asks: {"12049":21,"12050":54,"12051":102,"12052":193,"12053":200,"12054":175,"12055":260,"12056":260,"12057":270,"12058":531,"12059":407,"12060":291,"12061":72,"12062":111,"12063":29}
20191011 19:06:52.492(UTC) INFO: FP Bars: volume: 5001; bids: {"12075":6,"12074":76,"12073":250,"12072":232,"12071":165,"12070":80,"12069":134,"12068":287,"12067":42,"12066":127,"12065":83,"12064":138,"12063":161,"12062":88,"12061":68,"12060":62,"12059":25,"12058":22}; asks: {"12059":29,"12060":34,"12061":169,"12062":99,"12063":224,"12064":239,"12065":143,"12066":228,"12067":209,"12068":191,"12069":225,"12070":110,"12071":113,"12072":168,"12073":336,"12074":277,"12075":161}
20191011 19:06:52.531(UTC) INFO: FP Bars: volume: 5000; bids: {"12078":88,"12077":469,"12076":440,"12075":204,"12074":368,"12073":267,"12072":157,"12071":170,"12070":106,"12069":304,"12068":128,"12067":111,"12066":87}; asks: {"12066":30,"12067":45,"12068":66,"12069":189,"12070":130,"12071":61,"12072":73,"12073":161,"12074":222,"12075":149,"12076":426,"12077":297,"12078":221,"12079":31}
20191011 19:06:52.567(UTC) INFO: FP Bars: volume: 5000; bids: {"12070":35,"12069":165,"12068":361,"12067":319,"12066":515,"12065":320,"12064":352,"12063":104}; asks: {"12063":62,"12064":401,"12065":471,"12066":432,"12067":437,"12068":590,"12069":370,"12070":66}
20191011 19:06:52.601(UTC) INFO: FP Bars: volume: 5000; bids: {"12065":7,"12064":305,"12063":307,"12062":181,"12061":335,"12060":184,"12059":296,"12058":211,"12057":218,"12056":231,"12055":199,"12054":97,"12053":90,"12052":101,"12051":129,"12050":55}; asks: {"12050":19,"12051":73,"12052":28,"12053":39,"12054":102,"12055":143,"12056":250,"12057":178,"12058":67,"12059":62,"12060":144,"12061":140,"12062":122,"12063":363,"12064":137,"12065":75,"12066":112}
20191011 19:06:52.635(UTC) INFO: FP Bars: volume: 5000; bids: {"12057":54,"12056":105,"12055":125,"12054":209,"12053":163,"12052":118,"12051":222,"12050":284,"12049":201,"12048":180,"12047":116,"12046":55,"12045":38,"12044":133,"12043":221,"12042":274,"12041":152,"12040":65}; asks: {"12040":29,"12041":56,"12042":196,"12043":170,"12044":142,"12045":84,"12046":45,"12047":133,"12048":106,"12049":204,"12050":262,"12051":198,"12052":188,"12053":50,"12054":126,"12055":129,"12056":63,"12057":96,"12058":8}
20191011 19:06:52.670(UTC) INFO: FP Bars: volume: 5001; bids: {"12059":51,"12058":18,"12057":1,"12056":25,"12055":96,"12054":87,"12053":113,"12052":891,"12051":433,"12050":116,"12049":219,"12048":58,"12047":55,"12046":63,"12045":4,"12044":116,"12043":74,"12042":129,"12041":39}; asks: {"12042":125,"12043":109,"12044":141,"12045":56,"12046":72,"12047":44,"12048":147,"12049":170,"12050":142,"12051":342,"12052":365,"12053":232,"12054":92,"12055":83,"12056":85,"12057":35,"12058":63,"12059":29,"12060":81}
20191011 19:06:52.706(UTC) INFO: FP Bars: volume: 5032; bids: {"12067":180,"12066":325,"12065":246,"12064":570,"12063":252,"12062":46,"12060":54,"12059":92,"12058":285,"12057":75,"12056":231,"12055":192,"12054":126,"12053":44}; asks: {"12054":62,"12055":17,"12056":226,"12057":172,"12058":67,"12059":149,"12060":118,"12061":51,"12062":126,"12063":245,"12064":353,"12065":218,"12066":304,"12067":198,"12068":8}
20191011 19:06:52.735(UTC) INFO: FP Bars: volume: 5003; bids: {"12070":107,"12069":314,"12068":744,"12067":488,"12066":279,"12065":139,"12064":277}; asks: {"12064":44,"12065":195,"12066":341,"12067":530,"12068":508,"12069":470,"12070":482,"12071":85}

Attached are the source code and the compiled jar file ready for importing into Bookmap
Attachments
fp-bars.jar
(2.9 KiB) Downloaded 418 times
FpBars.java
(2.12 KiB) Downloaded 411 times

jm1111enator
Posts: 3
Joined: Tue Jun 18, 2019 3:59 pm

Re: Footprint bars computation (an example of API usage)

Post by jm1111enator » Sun Oct 13, 2019 10:55 am

Thank you Serg! 
May I ask you a couple of questions?
1: Could you please do a video on how to import the attached files into Bookmap for new users?
I have read the documentation on how to import the .jar entension into Bookmap (fp-bars.jar), but it does not include the .Java extension (FpBars.java). 
2: Bruce does an amazing job on the daily platform and advanced webinars for using Bookmap. Could your development team possibly do educational videos on coding in Java for Bookmap? At least one video or webinar on how to get started programming in Bookmap, download the IDE, unzip the API Javadoc, and maybe one webinar on best practices, learning resources, and step by step building of an indicator or a strategy. 
Have a great day,
Jim

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

Re: Footprint bars computation (an example of API usage)

Post by Andry API support » Sun Oct 13, 2019 7:47 pm

Hi jm1111enator,
You do not need to import .java to Bookmap. Only .jar is imported.
.java file is a source file. If you have some some software developing background you can read the source and maybe you'll want to change something so it'll work better for you. Even with no experience, if you want to get the way it works you may open it with any text editor and see its code.
 

jm1111enator
Posts: 3
Joined: Tue Jun 18, 2019 3:59 pm

Re: Footprint bars computation (an example of API usage)

Post by jm1111enator » Mon Oct 14, 2019 3:57 pm

AndreyR wrote:
Sun Oct 13, 2019 7:47 pm
Hi jm1111enator,
You do not need to import .java to Bookmap. Only .jar is imported.
.java file is a source file. If you have some some software developing background you can read the source and maybe you'll want to change something so it'll work better for you. Even with no experience, if you want to get the way it works you may open it with any text editor and see its code.
Thanks Andrey.
I see where the original posted footprint code does not have a graphic display.
If I insert it into Bookmap, I am only going to read the information in some kind of a text editor?
I am curious as to what a display on the Bookmap would look like.
Is that possible?
Have a great day,
Jim 
 

luis
Posts: 6
Joined: Mon Aug 19, 2019 11:09 pm
Has thanked: 1 time

Re: Footprint bars computation (an example of API usage)

Post by luis » Mon Oct 14, 2019 8:06 pm

Hi Andrey and Serg,

What is the recommended way to have this data in a chart? 

Does BM API have any class/interface that we can use for that?

Thanks,
Last edited by luis on Fri Nov 01, 2019 9:06 pm, edited 1 time in total.

mpxtreme
Posts: 33
Joined: Thu Oct 25, 2018 12:20 am
Been thanked: 6 times

Re: Footprint bars computation (an example of API usage)

Post by mpxtreme » Mon Oct 14, 2019 8:53 pm

If we could have 1 example of how to actually build a candle/bar it would be helpful for devs to build alternative data candles/bars..like range/volume etc.

Then we could just substitute a custom data event that triggers the "open"  price and then simply use the high/low/close prices provided in the standard candle code.

Thanks
 
Last edited by mpxtreme on Fri Nov 01, 2019 9:11 pm, edited 2 times in total.

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

Re: Footprint bars computation (an example of API usage)

Post by Andry API support » Tue Oct 15, 2019 8:51 am

hi everyone,
does not have a graphic display...read the information in some kind of a text editor
The example Serg has made only produces text output. You can see it in the Combined log window (Bookmap: File->Show log file)
Having the code that draws open/high/low/close on the BM chart would be most helpful
Currently there's no ready-made solution for drawing candles with API, but...
recommended way to have this data in a chart... BM API have any class/interface
... you may want to explore screen space painters which are used to draw in coordinates.
https://github.com/BookmapAPI/DemoStrat ... acepainter
It is a valid tool although it may take some effort to paint bars with.

Post Reply