Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Python Algorithmic Trading Cookbook
  • Toc
  • feedback
Python Algorithmic Trading Cookbook

Python Algorithmic Trading Cookbook

By : Dagade
3.8 (10)
close
Python Algorithmic Trading Cookbook

Python Algorithmic Trading Cookbook

3.8 (10)
By: Dagade

Overview of this book

If you want to find out how you can build a solid foundation in algorithmic trading using Python, this cookbook is here to help. Starting by setting up the Python environment for trading and connectivity with brokers, you’ll then learn the important aspects of financial markets. As you progress, you’ll learn to fetch financial instruments, query and calculate various types of candles and historical data, and finally, compute and plot technical indicators. Next, you’ll learn how to place various types of orders, such as regular, bracket, and cover orders, and understand their state transitions. Later chapters will cover backtesting, paper trading, and finally real trading for the algorithmic strategies that you've created. You’ll even understand how to automate trading and find the right strategy for making effective decisions that would otherwise be impossible for human traders. By the end of this book, you’ll be able to use Python libraries to conduct key tasks in the algorithmic trading ecosystem. Note: For demonstration, we're using Zerodha, an Indian Stock Market broker. If you're not an Indian resident, you won't be able to use Zerodha and therefore will not be able to test the examples directly. However, you can take inspiration from the book and apply the concepts across your preferred stock market broker of choice.
Table of Contents (16 chapters)
close

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.

bookmark search playlist download font-size

Change the font size

margin-width

Change margin width

day-mode

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Delete Bookmark

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete