Skip to content

Commit

Permalink
Initial version
Browse files Browse the repository at this point in the history
Committing the first version of the API
  • Loading branch information
theselfspace committed Jun 24, 2022
1 parent 7881b61 commit bd6c481
Show file tree
Hide file tree
Showing 36 changed files with 3,613 additions and 2 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2022 theselfspace
Copyright (c) 2022 theselfspace ([email protected])

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
82 changes: 81 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,81 @@
# fyers-java
# fyers-java

*fyers-java is an open source Java library that can talk to [Fyers](https://fyers.in/) 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.0</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:

String authCode = loginHandler.login("<LOGIN USERNAME>", "<LOGIN PASSWORD>", "<4 DIGIT MFA>");

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](https://myapi.fyers.in/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](https://twitter.com/Theself_Space)

98 changes: 98 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>space.theself</groupId>
<artifactId>fyers-java</artifactId>
<version>1.0.0</version>

<name>space.theself:fyers-java</name>

<licenses>
<license>
<name>MIT License</name>
<url>http://www.opensource.org/licenses/mit-license.php</url>
</license>
</licenses>

<properties>
<java.version>1.8</java.version>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>


<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.2.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.3</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.1-jre</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>

</dependencies>

</project>
Loading

0 comments on commit bd6c481

Please sign in to comment.