pyIIFLapis 1.0.0
Python SDK for IIFL Trading APIs
Requires Python: >=3.6
Keywords: pyIIFLapis

# IIFL Securities Python SDK

Python SDK for IIFL Trading APIs

![IIFL logo](./docs/images/iifl-logo.png)

#### Documentation

Read the docs hosted [here](https://api.iiflsecurities.com/)

#### Features

-   Order placement, modification and cancellation
-   Fetching user info including holdings, positions, margin and order book.
-   Fetching live market streaming.
-   Placing, modifying and deleting Bracket Order.
-   Fetching order status and trade information.
-   Getting live data streaming using websockets.

### Installation

`pip install pyIIFLapis`

### Usage

#### Configuring API keys

Get your API keys from https://api.iiflsecurities.com/api-keys.html

Configure these keys in a file named `keys.conf` in the same directory as your python script exists

A sample `keys.conf` is given below:

```conf
[KEYS]
APP_NAME=YOUR_APP_NAME_HERE
APP_SOURCE=YOUR_APP_SOURCE_HERE
USER_ID=YOUR_USER_ID_HERE
PASSWORD=YOUR_PASSWORD_HERE
USER_KEY=YOUR_USER_KEY_HERE
ENCRYPTION_KEY=YOUR_ENCRYPTION_KEY_HERE
OCP_KEY=YOUR_OCP_KEY_HERE
```


#### Authentication

```py
from pyIIFLapis import IIFLClient

client = IIFLClient(client_code="client_code", passwd="password", dob="YYYYMMDD", email_id="email",contact_number="Contact Number")
client.client_login() #For Customer Login
client.partner_login() #For Partner Login
```


After successful authentication, you should get a `Logged in!!` message
#### Market Feed

```py
#NOTE : Symbol has to be in the same format as specified in the example below.

req_list_=[{"Exch":"N","ExchType":"C","ScripCode":"22"},
            {"Exch":"N","ExchType":"C","ScripCode":"2885"}]
            
client.fetch_market_feed(req_list=req_list_, count=2,client_id="client_code")
```

#### Fetching user info

```py
# Fetches holdings
client.holdings(client_id = "client_code")

# Fetches margin
client.margin(client_id = "client_code")

# Fetches net positions
client.net_positions(client_id = "client_code")

# Fetches net wise positions
client.net_position_netwise(client_id = "client_code")

# Fetches the order book of the client
client.order_book(client_id = "client_code")

# Fetches the trade book of the client
client.trade_book(client_id = "client_code")
```

Scrip codes reference:

Note : Use these Links for getting scrip codes

CSV Scrip Dump: https://api.iiflsecurities.com/scrip-master.html

#### Placing an order

```py
# Note: This is an indicative order.

from pyIIFLapis.order import Order, OrderType, Exchange, ExchangeType

test_order = Order(order_type='B',exchange='N',exchange_segment='C', scrip_code=1660, quantity=1, price=205,is_intraday=True,atmarket=False)
client.place_order(order=test_order,client_id='client_code',order_requester_code='order_requester_code')

```

#### Modifying an order

```py
test_order = Order(order_type='B', scrip_code=1660, quantity=1, price=205,is_intraday=False,exchange='N',exchange_segment='C',atmarket=True,exch_order_id="12345678" )
client.modify_order(order=test_order,client_id='client_code',order_requester_code='order_requester_code')
```

#### Canceling an order

```py
client.cancel_order(order_type='B', scrip_code=1660, quantity=1,exchange='N',exchange_segment='C',exch_order_id='12345678', client_id='client_code',order_requester_code='order_requester_code')
```
#### Bracket Order 



For placing Braket order
```py
test_order=bo_co_order(scrip_code=1660,BuySell='B',Qty=1, LimitPriceInitialOrder=204,TriggerPriceInitialOrder=0,LimitPriceProfitOrder=208.0,TriggerPriceForSL=202,RequestType='P',AtMarket=False)

client.bo_order(boco=test_order,client_id='client_code',order_requester_code='order_requester_code')
```
Note:For placing Bracket order in FNO segment pass ExchType='D'

For Modifying Bracket Order only for Initial order (entry)
```py
test_order=bo_co_order(scrip_code=1660,BuySell='B',Qty=1, LimitPriceInitialOrder=203,TriggerPriceInitialOrder=0,LimitPriceProfitOrder=208.0,TriggerPriceForSL=202,RequestType='M',AtMarket=False,ExchOrderId='12345678')

client.bo_order(boco=test_order,client_id='client_code',order_requester_code='order_requester_code')

#Note : For cover order just pass LimitPriceProfitOrder equal to Zero.
```

For Modifying LimitPriceProfitOrder 
```py
test_order=Order(order_type='S', scrip_code=1660, quantity=1, price=208.50,is_intraday=True,exchange='N',exchange_segment='C',atmarket=False,exch_order_id="12345678" ,order_for=OrderFor.MODIFY)

client.mod_bo_order(order=test_order,client_id='client_code',order_requester_code='order_requester_code')
```
For Modifying TriggerPriceForSL
```py
test_order=Order(order_type='S', scrip_code=1660, quantity=1, price=0,is_intraday=True,exchange='N',exchange_segment='C',atmarket=True,exch_order_id="123456789" ,stoploss_price=201.50,is_stoploss_order=True,order_for=OrderFor.MODIFY)

client.mod_bo_order(order=test_order,client_id='client_code',order_requester_code='order_requester_code')

#Note : You have pass atmarket=true while modifying stoploss price, Pass ExchorderId for the particular leg to modify.
```

#### Fetching Order Status and Trade Information

```py
from pyIIFLapis.order import  Exchange

req_list= [

        {
            "Exch": "N",
            "ExchType": "C",
            "ScripCode": 20374,
            "ExchOrderID": "1000000015310807"
        }]

# Fetches the trade details
client.fetch_trade_info(req_list=req_list,client_id='client_code')

req_list_= [

        {
            "Exch": "N",
            "ExchType": "C",
            "ScripCode": 20374,
            "RemoteOrderID": "90980441"
        }]
# Fetches the order status
client.fetch_order_status(req_list=req_list_,client_id='client_code')

```

#### TODO
 - Write tests.


#### Credits

This package was created with
[Cookiecutter](https://github.com/audreyr/cookiecutter) and the
[audreyr/cookiecutter-pypackage](https://github.com/audreyr/cookiecutter-pypackage)
project template.