Python Last updated: 2020-11-28

Presenting a functional python wrapper for algomojo trading api. Algomojo is a multi broker python library for the Algomojo Free API + Free Algo Trading Platform . It allows rapid trading algo development easily, with support for both REST-API interfaces.

Basic Features of Python Trading API

Execute Orders in Realtime
Single Client Execution Orders
Multi-Client Execution of Orders
Multi-Broker Execution of Orders
Place ATM/ITM/OTM Option Orders
Modify/Cancel Orders
Retrieve Orderbook
Retrieve Order History
Retrieve Open Positions
Square off Open Positions
Access Fund/Margin Details and many more functionalities.

For more details of each API behavior, Please see the Algomojo API documentation.

Visit here for more detailed Algomojo python documentation for supporting functions

Installation

How to Install Algomojo Python Library?

Install from PyPI using the simple PIP command

It is highly recommended to use Python 3.x versions

pip install algomojo

Place Order

How to Send Orders using Algomojo Python Library?

Once the Python Library is installed the next step is to import the python library using the import command and set the api_key and api_secret key with broker shortcode in your python program.


from algomojo import *

#set the API Key and API secret key, Broker Code and the Version
#Broker Code "ab" = aliceblue, "tj" = tradejini, "zb" = zebu, "en" = enrich for the list of short codes supported kindly check with algomojo team
#default version used Ver = 1.0

tj=api("86cbef19e7e61ccee91e497690d5814e","a256707821d7b97988eef6b90a230e35",broker="tj",version=1.0)

print(tj)

#mandatory parameters are send to place Market Orders. 
#by default execution is set to Market Orders in "MIS" mode. For custom orders see the complete python documentation

tj.place_order("RELIANCE-EQ","NSE","B",1) 

#placing limit orders
tj.place_order("CRUDEOIL20DECFUT","MCX","B",qty=1,order_type='L',price='3100',product_type='NRML')


Bulk Orders

How to Send Bulk Orders using Algomojo

Here is the Jupyter notebook implementation of Transmitting Multiple Orders to Multiple Brokers. Sample code is provided for sending Bulk Market Orders and Bulk Bracket Orders


from algomojo import *

#set the API Key and API secret key, Broker Code and the Version
#Broker Code "ab" = aliceblue, "tj" = tradejini, "zb" = zebu, "en" = enrich for the list of short codes supported kindly check with algomojo team
#default version used Ver = 1.0

tj=api("86cbef19e7e61ccee91e497690d5814e","a256707821d7b97988eef6b90a230e35",broker="tj",version=1.0) #set the API_key and API_secret_key for broker tradejini
ab=api("e59db3b62ab6528f6ced1103e5b0c4a8","e795f8ed449a31009825d43bae8ca0ef",broker="ab",version=1.0) #set the API_key and API_secret_key for broker Aliceblue

print(tj)
print(ab)

#mandatory parameters are send to Bulk orders to the same account

tj.place_multi_order([{'ticker':'BHEL-EQ','exchange':'NSE','qty':1,'action':'B'},{'ticker':'NHPC-EQ','exchange':'NSE','qty':'1','action':'B'},{'ticker':'RELIANCE-EQ','exchange':'NSE','qty':'1','action':'B'}])

Output:

[{'NOrdNo': '201120000011376', 'stat': 'Ok'},
 {'NOrdNo': '201120000011377', 'stat': 'Ok'},
 {'NOrdNo': '201120000011378', 'stat': 'Ok'}]

 ab.place_multi_order([{'ticker':'BHEL-EQ','exchange':'NSE','qty':1,'action':'B'},{'ticker':'NHPC-EQ','exchange':'NSE','qty':'1','action':'B'},{'ticker':'RELIANCE-EQ','exchange':'NSE','qty':'1','action':'B'}])

Output:

 [{'NOrdNo': '201120000162520', 'stat': 'Ok'},
 {'NOrdNo': '201120000162521', 'stat': 'Ok'},
 {'NOrdNo': '201120000162533', 'stat': 'Ok'}]

#sending multi Bracket Orders. Token ID is mandatory for executing the Bracket Order. Get the Token ID logging into Algomojo -> Watchlist -> Goto Security info
order1=[{"ticker":"438","exchange":"NSE","action":"B","qty":"1","profit":"5","stoploss":"5"},{"ticker":"11184","exchange":"NSE","action":"B","qty":"1","profit":"10","stoploss":"10"}]

tj.place_multi_bo(order1)

Output:

[{'nestOrderNumber': '201120000011405', 'stat': 'Ok'},
 {'nestOrderNumber': '201120000011407', 'stat': 'Ok'}]

order2=[{"ticker":"2388","exchange":"NSE","action":"B","qty":"1","profit":"5","stoploss":"5"},{"ticker":"438","exchange":"NSE","action":"B","qty":"1","profit":"10","stoploss":"10"}]
ab.place_multi_bo(order2)

Output:

[{'nestOrderNumber': '201120000162708', 'stat': 'Ok'},
 {'nestOrderNumber': '201120000162710', 'stat': 'Ok'}]


Options Orders

How to Send ATM/ITM/OTM Option Orders


from algomojo import *

#set the API Key and API secret key, Broker Code and the Version
#Broker Code "ab" = aliceblue, "tj" = tradejini, "zb" = zebu, "en" = enrich for the list of short codes supported kindly check with algomojo team
#default version used Ver = 1.0

tj=api("86cbef19e7e61ccee91e497690d5814e","a256707821d7b97988eef6b90a230e35",broker="tj",version=1.0)

print(tj)
print(ab)

#Executes ATM order with offset = 0
tj.place_option_order(spot_sym="NIFTY",expiry="26NOV20",opt_type="CE",action="B",qty="75",strike_int=50,offset=0)

Output:

{'NOrdNo': '201120000013807', 'stat': 'Ok'}

#Executes OTM Call 3 strike wide from ATM Options, Offset = +3
tj.place_option_order(spot_sym="NIFTY",expiry="26NOV20",opt_type="CE",action="B",qty="75",strike_int=50,offset=3)

Output:

{'NOrdNo': '201120000013809', 'stat': 'Ok'}

#Executes OTM PE 3 strike wide from ATM Options, Offset = -3
tj.place_option_order(spot_sym="NIFTY",expiry="26NOV20",opt_type="PE",action="B",qty="75",strike_int=50,offset=-3)

Output:

{'NOrdNo': '201120000013810', 'stat': 'Ok'}

#Executes ITM Call 2 strike wide from ATM Options, Offset = -2
tj.place_option_order(spot_sym="NIFTY",expiry="26NOV20",opt_type="CE",action="B",qty="75",strike_int=50,offset=-2)

Output:

{'NOrdNo': '201120000013812', 'stat': 'Ok'}

#Executes ITM Put 2 strike wide from ATM Options, Offset = +2
tj.place_option_order(spot_sym="NIFTY",expiry="26NOV20",opt_type="PE",action="B",qty="75",strike_int=50,offset=2)

Output:

{'NOrdNo': '201120000013813', 'stat': 'Ok'}

#Executing Multi legged Option Strategies - Short Straddle
tj.place_option_order(spot_sym="NIFTY",expiry="26NOV20",opt_type="CE",action="S",qty="75",strike_int=50,offset=0)
tj.place_option_order(spot_sym="NIFTY",expiry="26NOV20",opt_type="PE",action="S",qty="75",strike_int=50,offset=0)

Output:

{'NOrdNo': '201120000013816', 'stat': 'Ok'}

#Executing Multi legged Option Strategies - Iron Condor
tj.place_option_order(spot_sym="NIFTY",expiry="26NOV20",opt_type="CE",action="S",qty="75",strike_int=50,offset=1)
tj.place_option_order(spot_sym="NIFTY",expiry="26NOV20",opt_type="PE",action="S",qty="75",strike_int=50,offset=-1)

tj.place_option_order(spot_sym="NIFTY",expiry="26NOV20",opt_type="CE",action="B",qty="75",strike_int=50,offset=3)
tj.place_option_order(spot_sym="NIFTY",expiry="26NOV20",opt_type="PE",action="B",qty="75",strike_int=50,offset=-3)

Output:

{'NOrdNo': '201120000013825', 'stat': 'Ok'}

Cancel / Modify Orders

How to Cancel/Modify Orders using Algomojo


from algomojo import *

#set the API Key and API secret key, Broker Code and the Version
#Broker Code "ab" = aliceblue, "tj" = tradejini, "zb" = zebu, "en" = enrich for the list of short codes supported kindly check with algomojo team
#default version used Ver = 1.0

tj=api("86cbef19e7e61ccee91e497690d5814e","a256707821d7b97988eef6b90a230e35",broker="tj",version=1.0)
client_id = "TS2499"

print(tj)

#mandatory parameters are send to place Limit Orders. 

tj.place_order("RELIANCE-EQ","NSE","S",qty=1,order_type='L',price='2030',product_type='MIS')

Output:

{'NOrdNo': '201120000012092', 'stat': 'Ok'}

#Token ID is mandatory for Modifying the Order. Get the Token ID logging into Algomojo -> Watchlist -> Goto Security info
tj.modify_order(client_id,'201120000012092','RELIANCE-EQ','NSE','S','L','1950',1,2885)

Output:

{'Result': ' NEST Order Number :201120000012092', 'stat': 'Ok'}

#cancel the pending orders, Pass the pending order id, symbol and the exchange to cancel the order
tj.cancelorder(client_id,'201120000012092','RELIANCE-EQ','NSE')

Output:

{'Result': ' NEST Order Number :201120000012092', 'stat': 'Ok'}

Square off Open Positions

How to Square off Open Positions using Algomojo


from algomojo import *

#set the API Key and API secret key, Broker Code and the Version
#Broker Code "ab" = aliceblue, "tj" = tradejini, "zb" = zebu, "en" = enrich for the list of short codes supported kindly check with algomojo team
#default version used Ver = 1.0

tj=api("86cbef19e7e61ccee91e497690d5814e","a256707821d7b97988eef6b90a230e35",broker="tj",version=1.0)
client_id = "TS2499"

print(tj)

#Place Market Order
tj.place_order("TCS-EQ","NSE","B",1,product_type='MIS')

Output:

{'NOrdNo': '201120000013145', 'stat': 'Ok'}

#Squareoff Open Position, Get the Token ID logging into Algomojo -> Watchlist -> Goto Security info
tj.squareoff(client_id,'TCS-EQ','11536',1,product_type='MIS')

Output:

{'Result': 'ok', 'stat': 'Ok'}

SecurityInfo

How to Retrieve Token ID & Security Info


from algomojo import *

#set the API Key and API secret key, Broker Code and the Version
#Broker Code "ab" = aliceblue, "tj" = tradejini, "zb" = zebu, "en" = enrich for the list of short codes supported kindly check with algomojo team
#default version used Ver = 1.0

tj=api("86cbef19e7e61ccee91e497690d5814e","a256707821d7b97988eef6b90a230e35",broker="tj",version=1.0)
client_id = "TS2499"

print(tj)

#Get Token No using symbol info
tj.symbol_info(symbol="BHEL-EQ")#

Output:

[{'symbol_segment': 'NSE_EQ',
  'symbol_product': 'EQUITY',
  'symbol': 'BHEL',
  'symbol_token': '438',
  'trading_symbol': 'BHEL-EQ',
  'trading_symbol_desc': 'BHEL',
  'expiry_date': None,
  'expiry_date_desc': None,
  'strike_price': None,
  'lot_size': '1',
  'seg_seq': '1',
  'order_seq': '1'}]

tj.security_info(security_dict={"uid":client_id,"Exchange":"NSE","SrchFor":"438"})

Output:

{'IntPayDate': '1 Jan, 1980',
 'PriceQuatation': 'NA',
 'SellCryCost': 0,
 'Blq': 1,
 'HCircuitLimit': '32.05',
 'BuyCryCost': 0,
 'IMSpreadBenefit': 'ON',
 'Totalopeninterest': 105063000,
 'DeliveryUnits': 'NA',
 'DeliveryEndDate': 'NA',
 'OtherSellMargin': 'NA',
 'NoDelEndTime': '1 Jan, 1980',
 'NoDelStartDate': '1 Jan, 1980',
 'TickSize': '5',
 'ExpiryDte': 'NA',
 'OtherBuyMargin': 'NA',
 'Comments': 'ANNUAL GENERAL MEETING',
 'TenderPeriod': 'NA',
 'ListingDate': '1 Jan, 1980 - 0:0:0',
 'Symbol': 'BHEL-EQ',
 'Series': 'EQ',
 'CreditRating': '26.25-32.05',
 'Exposuremargin': 'NA',
 'LocalUpdateTime': '19 Nov, 2020',
 'IssedCapital': '348.20634 (Crs)',
 'PriceNumerator': '1',
 'IsuStartDate': '24 Oct, 1994',
 'PermitedtoTrade': 0,
 'TenderPeriodEnd': 'NA',
 'Warning': 0,
 'Remarks': 'NA',
 'DPR1': '26.25',
 'BuyVarMargin': 'NA',
 'DPR2': '32.05',
 'BookClsStartTime': '1 Jan, 1980',
 'Perchgopeninterest': 0,
 'ExpulsionDate': '1 Jan, 1980',
 'Exchange': 'NSE',
 'Issuerate': 0,
 'QuantityUnit': 'NA',
 'ReAdminDate': '1 Jan, 1980',
 'SellVarMargin': 'NA',
 'TenderPeriodStart': 'NA',
 'GeneralDenomenator': '1',
 'openinterest': 0,
 'ExchSeg': 'nse_cm',
 'Spotprc': 'NA',
 'MarketType': 'NA',
 'RecordDate': '21 Sep, 2020',
 'ValueTradedToday': '75.41628 (Crs)',
 'LTT': '20/11/2020 13:49:39',
 'PriceUnits': 'NA',
 'GeneralNumerator': '1',
 'MaxOrderSize': '3429832',
 'stat': 'Ok',
 'PriceDenomenator': '1',
 'DeliveryStartDate': 'NA',
 'LastTradingDate': 'NA',
 'LCircuitLimit': '26.25',
 'Freeze': 3429832,
 'Instrument': '',
 'IsuMaturityDate': 'NA'}

Orderbook

How to Retrieve Token ID & Security Info


from algomojo import *

#set the API Key and API secret key, Broker Code and the Version
#Broker Code "ab" = aliceblue, "tj" = tradejini, "zb" = zebu, "en" = enrich for the list of short codes supported kindly check with algomojo team
#default version used Ver = 1.0

tj=api("86cbef19e7e61ccee91e497690d5814e","a256707821d7b97988eef6b90a230e35",broker="tj",version=1.0)
client_id = "TS2499"

print(tj)

tj.order_book(client_id)

Output:

[{'Cancelqty': 1,
  'AlgoCategory': 'NA',
  'modifiedBy': 'TS2499',
  'RefLmtPrice': 0,
  'Mktpro': 'NA',
  'Qty': 1,
  'ExpSsbDate': 'NA',
  'Trgprc': '00.00',
  'ExchOrdID': '1300000011749668',
  'COPercentage': 0,
  'RequestID': '2',
  'usecs': '118136',
  'orderentrytime': 'Nov 20 2020 12:56:25',
  'Avgprc': '00.00',
  'Fillshares': 0,
  'optionType': 'XX',
  'ordergenerationtype': '--',
  'iSinceBOE': 1605857199,
  'InstName': '',
  'Nstordno': '201120000012092',
  'Unfilledsize': 0,
  'Usercomments': 'NA',
  'mpro': '1',
  'ticksize': '5',
  'strikePrice': '00.00',
  'OrderedTime': '20/11/2020 12:56:39',
  'panNo': 'HEPPK3111A',
  'defmktproval': '3',
  'ExchConfrmtime': '20-Nov-2020 12:56:39',
  'Minqty': 0,
  'AlgoID': 'NA',
  'ordersource': 'NEST_REST',
  'sipindicator': 'N',
  'decprec': '2',
  'series': 'EQ',
  'PriceNumerator': '1',
  'Ordvaldate': 'NA',
  'Trsym': 'RELIANCE-EQ',
  'accountId': 'TS2499',
  'Dscqty': 0,
  'BrokerClient': '--',
  'reporttype': 'cancel',
  'remarks': '--',
  'Scripname': 'RELIANCE INDUSTRIES LTD',
  'Prc': '1950.00',
  'Exchange': 'NSE',
  'Sym': 'RELIANCE',
  'token': '2885',
  'Prctype': 'L',
  'RejReason': '--',
  'GeneralDenomenator': '1',
  'Validity': 'DAY',
  'GeneralNumerator': '1',
  'stat': 'Ok',
  'multiplier': '1',
  'bqty': '1',
  'noMktPro': '0',
  'discQtyPerc': '10',
  'Status': 'cancelled',
  'PriceDenomenator': '1',
  'SyomOrderId': '',
  'Trantype': 'S',
  'ExpDate': 'NA',
  'marketprotectionpercentage': '--',
  'Pcode': 'MIS',
  'Exseg': 'nse_cm',
  'user': 'TS2499'}]

tj.order_history(client_id,nestordernumber='201120000012092')

Output:

[{'Trsym': 'RELIANCE-EQ',
  'PriceNumerator': '1',
  'reporttype': 'NA',
  'customerfirm': 'C',
  'symbolname': 'RELIANCE',
  'Prc': '1950.00',
  'Qty': 1,
  'Action': 'S',
  'exchangetimestamp': '20-Nov-2020 12:56:39',
  'legorderindicator': '',
  'nestreqid': '2',
  'Ordtype': 'L',
  'exchange': 'NSE',
  'filldateandtime': '-- --',
  'GeneralDenomenator': '1',
  'ordergenerationtype': '--',
  'exchangeorderid': '1300000011749668',
  'productcode': 'MIS',
  'unfilledSize': 1,
  'triggerprice': '0.0',
  'GeneralNumerator': '1',
  'averageprice': '0.0',
  'ExchTimeStamp': '20/11/2020 12:56:39',
  'stat': 'Ok',
  'bqty': 1,
  'filledShares': 0,
  'Status': 'cancelled',
  'duration': 'DAY',
  'PriceDenomenator': '1',
  'rejectionreason': '--',
  'nestordernumber': '201120000012092',
  'scripname': 'RELIANCE INDUSTRIES LTD',
  'disclosedqty': '0',
  'ordersource': 'NEST_REST'}]