top of page
My photo


  • Writer's pictureAdmin

BankNifty Option Buying strategy with Python and FyersAPI

Last weekend, I delved into the world of trading with Python, specifically exploring the capabilities of the FYERS API. Inspired by the potential for algorithmic trading, I crafted a simple yet insightful price reference strategy tailored for trading BankNIFTY. In this forthcoming blog post, I'm excited to share my discoveries with you. From navigating the intricacies of the FYERS API to mastering order placements and tracking, I'll walk you through each step of the process. However, a crucial disclaimer: while this strategy serves as a valuable learning tool, I urge caution and advise against its use in real-time trading scenarios. Let's embark on this educational journey together, empowering beginners to craft their own trading algorithms with Python.

Step One :

This code snippet automates the authentication process for accessing the FYERS API using Python. It initializes a web browser, navigates to the authorization URL, waits for the authorization code to be obtained, saves it to a text file, and then exchanges it for an access token. Finally, it initializes a client for interacting with the FYERS API. However, it could benefit from improved error handling and additional comments for clarity.

fyers api trading strategy

Step Two :

In the next phase of refining my trading strategy utilizing the FYERS API, I crafted a series of custom functions aimed at facilitating seamless execution and management of trades. Here's a snapshot of the functions I've developed:

1. close_all_positions: This function enables the systematic closure of all existing positions, providing a streamlined approach to managing portfolio exposure.

2. close_position: Designed to close a specific position, this function offers targeted control over individual trades within the portfolio.

3. get_openposition_id: With this function, traders can retrieve the unique identifier for each open position, facilitating precise tracking and management.

4. cancel_all_open_orders: Offering a swift solution to cancel all pending orders, this function ensures agility in adjusting trade plans as market conditions evolve.

5. place_SL_order, place_limit_order, place_market_order: These functions empower traders with versatile order placement options, including stop-loss, limit, and market orders, catering to diverse trading strategies.

6. check_price_level : This function compare the current price of bank nifty index with the reference price and return the value to buy/sell or wait.

7. get_ltp: By providing real-time access to price data, these functions enable traders to monitor market movements and make informed decisions.

8. check_total_PNL: This function calculates the total profit and close the positions if the max profit or loss achieved.

9. current_BN_ATM_CE, current_BN_ATM_PE: Tailored for options trading, these functions identify the current at-the-money call and put options for Bank NIFTY, aiding in strategy formulation.

10. get_openposition_side: This function retrieves if the open position is a call buy or a put buy.

These functions collectively serve to enhance the flexibility, efficiency, and effectiveness of the trading strategy, empowering traders to execute with precision and confidence.

Step Three:

In the third stage of refining our trading strategy, let's delve into the core methodology we've devised for navigating the Bank NIFTY market. This is a option plain option buying strategy according to the price change. Here's a breakdown of the key steps involved:

1. Reference Price Setup: To kickstart our strategy, we first fetch the current price of the Bank NIFTY index and designate it as our reference price.

2. ATM Calls and Puts: Next, we retrieve the current at-the-money (ATM) call and put options for Bank NIFTY, laying the groundwork for our trade decisions.

3. Continuous Price Tracking: We initiate a continuous monitoring process, checking the Bank NIFTY price at 30-second intervals to stay abreast of market movements.

4. Buy Order Placement: Should the Bank NIFTY price surge above our reference price by 10 points, we swiftly execute a buy order for the ATM call option. If any prior orders exist, we close them before initiating the new trade.

5. Sell Order Placement: Conversely, if the Bank NIFTY price dips below our reference price by 10 points, we promptly place a sell order for the ATM put option, again ensuring closure of any existing orders beforehand.

6. Wait-and-Watch Strategy: While the Bank NIFTY price remains within the range of +/- 10 points from our reference price, we adopt a patient stance, awaiting a decisive movement in either direction.

7. Profit and Loss Monitoring: At every 30-second interval, we diligently monitor our trade positions. If our profit reaches 5000, we opt to exit all active orders. Similarly, to mitigate losses, we've set a maximum loss threshold of 2000.

This comprehensive strategy leverages real-time market data and precise order execution to capitalize on favorable price movements while mitigating potential risks.

Reference :


Thanks for visiting 



  • Linkedin
  • Wix Facebook page
  • Wix Twitter page
Subscribe to get exclusive updates
bottom of page