项目作者: DenDen747

项目描述 :
Java stock API
高级语言: Java
项目地址: git://github.com/DenDen747/Stock4j.git
创建时间: 2021-03-23T17:24:20Z
项目社区:https://github.com/DenDen747/Stock4j

开源协议:MIT License

下载


Stock4j

A java stock API built with Yahoo Finance API

Getting started

  1. Download the latest release from here
  2. Add it to your java project as a dependency

Usage

Creating a stock

To create a stock, you have to provide a name. For example, I’ll be using AAPL for demonstration.

  1. Stock stock = new Stock("AAPL");

Accessing stock details

There are many things you can access. If you see anything that is not in the API yet, please create an issue.

  1. Stock stock = new Stock("AAPL");
  2. stock.getPrice(); //returns the price
  3. stock.getVolume(); //returns the volume
  4. stock.getChange().getChangeFromYearLow().getAmountInPercent(); //returns the change percent from the year's low
  5. //etc...

Accessing the market

For example, if you want to access all of the stocks on Yahoo Finance to maybe create a scanner, do the following.

  1. Market.getAllTickers(); //returns a string array with the names of all stocks
  2. Market.getAllTickersNumber(); //returns the number of all stocks
  3. Market.getAllStocks(); //returns a stock array with all stocks

Creating a running API

To create a running API, you have to do the following.

  1. public class Main {
  2. public static void main(String[] args) throws NoSuchMethodException, ClassNotFoundException {
  3. API api = new API();
  4. api.await("onMarketUpdate", "Main"); //The name of the method you're registering, and the name of the current class
  5. }
  6. public void onMarketUpdate(MarketUpdateEvent event) {
  7. //Do something every time the market updates
  8. }
  9. }

Creating a scanner

You can use everything together, to, for example, create a simple scanner.

  1. import coin.Stock4j.API.API;
  2. import coin.Stock4j.API.event.MarketUpdateEvent;
  3. import coin.Stock4j.data.Stock;
  4. import coin.Stock4j.data.statics.Market;
  5. import coin.Stock4j.util.arrays.Modification;
  6. import java.io.IOException;
  7. public class Main {
  8. Stock[] qualified = new Stock[]{}; //These are the stocks that meet the requirements of the scanner
  9. public static void main(String[] args) throws IOException, NoSuchMethodException, ClassNotFoundException {
  10. API api = new API();
  11. api.await("scanner", "Main");
  12. }
  13. public void scanner(MarketUpdateEvent event) throws IOException {
  14. for(Stock stock : Market.getAllStocks()) {
  15. if(stock.getPrice() < 15 && stock.getPrice() > 5) {
  16. qualified = Modification.appendElement(qualified, stock); //Modification.appendElement() adds the element given in the second parameter to the array in the first parameter and returns the new array
  17. }
  18. }
  19. }
  20. }

For now, using a scanner is fairly slow, but we are planning to speed it up. We are also planning to add more types of event, rather than just one that updates every minute.

Events

There are 4 types of events. There is MarketUpdateEvent(), which is the default. It fires every minute.

  1. public void onMarketUpdate(MarketUpdateEvent event) {
  2. //Do something every minute
  3. }

There is MarketUpdatePerSecondEvent(), which fires every second.

  1. public void onMarketUpdate(MarketUpdatePerSecondEvent event) {
  2. //Do something every second
  3. }

There is MarketUpdatePerTenMinutesEvent(), which fires every 10 minutes.

  1. public void onMarketUpdate(MarketUpdatePerTenMinutesEvent event) {
  2. //Do something every 10 minutes
  3. }

There is MarketUpdatePerHourEvent(), which fires every hour.

  1. public void onMarketUpdate(MarketUpdatePerHourEvent event) {
  2. //Do something every hour
  3. }