Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Python Algorithmic Trading Cookbook

You're reading from   Python Algorithmic Trading Cookbook All the recipes you need to implement your own algorithmic trading strategies in Python

Arrow left icon
Product type Paperback
Published in Aug 2020
Publisher Packt
ISBN-13 9781838989354
Length 542 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Pushpak Dagade Pushpak Dagade
Author Profile Icon Pushpak Dagade
Pushpak Dagade
Arrow right icon
View More author details
Toc

Table of Contents (16) Chapters Close

Preface 1. Handling and Manipulating Date, Time, and Time Series Data 2. Stock Markets - Primer on Trading FREE CHAPTER 3. Fetching Financial Data 4. Computing Candlesticks and Historical Data 5. Computing and Plotting Technical Indicators 6. Placing Regular Orders on the Exchange 7. Placing Bracket and Cover Orders on the Exchange 8. Algorithmic Trading Strategies - Coding Step by Step 9. Algorithmic Trading - Backtesting 10. Algorithmic Trading - Paper Trading 11. Algorithmic Trading - Real Trading 12. Other Books You May Enjoy Appendix I
1. Appendix II
2. Appendix III

Querying margins and funds

Before placing orders, it is important to ensure that you have enough margins and funds available in your broking account to place the orders successfully. A lack of sufficient funds would result in the rejection of any orders placed by the broker, which means the others would never get placed on the exchange. This recipe shows you how to find the available margins and funds in your broking account at any point in time.

Getting ready

Make sure the broker_connection object is available in your Python namespace. Refer to the first recipe of this chapter to learn how to set it up.

How to do it…

We execute the following steps to complete this recipe:

  1. Display the equity margins:
>>> equity_margins = broker_connection.get_margins('equity')
>>> equity_margins

We'll get the following output (your output may differ):

{'enabled': True,
'net': 1623.67,
'available': {'adhoc_margin': 0,
'cash': 1623.67,
'opening_balance': 1623.67,
'live_balance': 1623.67,
'collateral': 0,
'intraday_payin': 0},
'utilised': {'debits': 0,
'exposure': 0,
'm2m_realised': 0,
'm2m_unrealised': 0,
'option_premium': 0,
'payout': 0,
'span': 0,
'holding_sales': 0,
'turnover': 0,
'liquid_collateral': 0,
'stock_collateral': 0}}
  1. Display the equity funds:
>>> equity_funds = broker_connection.get_funds('equity')
>>> equity_funds

We'll get the following output (your output may differ):

1623.67
  1. Display the commodity margins:
>>> commodity_margins = get_margins(commodity')
>>> commodity_margins

We'll get the following output (your output may differ):

{'enabled': True,
'net': 16215.26,
'available': {'adhoc_margin': 0,
'cash': 16215.26,
'opening_balance': 16215.26,
'live_balance': 16215.26,
'collateral': 0,
'intraday_payin': 0},
'utilised': {'debits': 0,
'exposure': 0,
'm2m_realised': 0,
'm2m_unrealised': 0,
'option_premium': 0,
'payout': 0,
'span': 0,
'holding_sales': 0,
'turnover': 0,
'liquid_collateral': 0,
'stock_collateral': 0}}
  1. Display the commodity funds:
>>> commodity_funds = broker_connection.get_funds('commodity')
>>> commodity_funds

We'll get the following output (your output may differ):

0

How it works…

The broker_connection object provides methods for fetching the available margins and funds for your broking account:

  • get_margins()
  • get_funds()

The broker Zerodha keeps track of margins and funds separately for equity and commodity products. If you are using a different broker supported by pyalgotrading, it may or may not track the funds and margins separately for equity and commodity.

Step 1 shows how margins can be queried for the equity product using the get_margins() method of the broker_connection object, with equity as an argument. Step 2 shows how funds can be queried for the equity product using the get_funds() method of the broker_connection object, with the equity string as an argument.

Steps 3 and 4 show how margins and funds can be queried for the commodity product in a similar way with the commodity string as an argument.

You have been reading a chapter from
Python Algorithmic Trading Cookbook
Published in: Aug 2020
Publisher: Packt
ISBN-13: 9781838989354
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime