GithubHelp home page GithubHelp logo

namit567 / fyers-java Goto Github PK

View Code? Open in Web Editor NEW

This project forked from theselfspace/fyers-java

0.0 0.0 0.0 47 KB

An open source Java API for Fyers end point

License: MIT License

Java 100.00%

fyers-java's Introduction

fyers-java

fyers-java is an open source Java library that can talk to Fyers trading endpoint.

How to use it?

1. Download and build the code

	git clone https://github.com/theselfspace/fyers-java
	mvn clean install

2. Add following dependency in your project's pom.xml

    <dependency>
		<groupId>space.theself</groupId>
		<artifactId>fyers-java</artifactId>
		<version>1.0.1</version>
    </dependency>

3. Login to Fyers

Login is done by LoginHandler class that needs app id and redirect URL, you can get this from Fyers app configuration portal.

	LoginHandler loginHandler = new LoginHandler();
	loginHandler.setClientCode("<YOUR APP ID>");
	loginHandler.setRedirectUrl("<YOUR APP SPECIFIC REDIRECT URL>");

Once you provide that, you can call the login method that requires parameters similar to what you would need to login Fyers via web:

Fyers have strengthened the login process which is a good thing. MFA is to make the access a bit more secure. We have updated the approach where we now expect user to enable time-based OTP in the account. Once that is done, we can use the 32 char TOTP secret to generate 6 digit TOTP.

	totp = new TotpProvider("<YOUR TOTP KEY>");
	String authCode = loginHandler.login("<LOGIN USERNAME>", "<4 DIGIT MFA>", totp.getTotp());

Once it generates the auth code successfully, you can now trigger the API calls.

4. Trigger API

API calls are handled by FyersFly class. APP SECRET is generated by the Fyers API portal when the app is created.

	FyersFly fly = new FyersFly("<YOUR APP ID>", "<APP SECRET>");

Now using the authCode that was generated by LoginHandler we can generate the access token.

	String token = fly.generateAccessToken(authCode);
	fly.setAccessToken(token);

After this, we can start calling the available APIs

	System.out.println("GET PROFILE");
	Profile profile = fly.getProfile();
	System.out.println(gson.toJson(profile));

 	System.out.println("GET HOLDING");
	Holding[] holdings = fly.getHolding();
	System.out.println(gson.toJson(holdings));

	System.out.println(gson.toJson(fly.getMarketDepth("NSE:SBIN-EQ", 1)));

Please be careful while placing a buy or sell order as it requires instrument name in a specific format: For buy, the format is <EXCHANGE>:<INSTRUMENT>

	fly.placeBuyOrder("NSE:NIFTY2262315300PE", 50, FyerParam.ORDER_TYPE_MARKET, FyerParam.ORDER_PRODUCTTYPE_INTRADAY);

For sell, the format is <EXCHANGE>:<INSTRUMENT>-<PRODUCT TYPE>

	fly.placeExitPositionOrderById("NSE:NIFTY2262315300PE-INTRADAY");				

PLEASE NOTE

**

** 1. Please read the code or test case first to know the pre-requisite before using any command **

** 2. The code is written for a specific purpose and can further be generalized. Especially Login flow needs to be followed as per current implementation. Login flow is quite similar to how is traversed through web portal (i.e. login via user and password and provide 4 digit pre-defined otp) **

** 3. Logout flow still requires some more exploration **

** 4. Commands that are related to EDIS are not tested well, please test it before using those commands. **

** 5. Read the official docs very well! Sometimes you might see a failure in API response but it MIGHT have actually got executed successfully. **

**

For any queries, please write me at [email protected] or ping at Theself_Space

Some more work from me - https://medium.com/@theself.space

fyers-java's People

Contributors

dependabot[bot] avatar theselfspace avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.