Python in algorithmic trading - page 16

 

IBridgePy’s Latest Backtesting Features By Dr. Hui Liu - August 9, 2019


IBridgePy’s Latest Backtesting Features By Dr. Hui Liu - August 9, 2019

Dr. Hui Liu, the creator of iBridgePy, provides a detailed overview of the latest backtesting features and functionalities offered by iBridgePy. He begins by discussing the cornerstone functions of iBridgePy and explaining the fundamental steps involved in building algorithmic trading strategies.

Dr. Liu focuses on the backtesting system of iBridgePy and presents two sample codes to demonstrate its usage. The first sample code utilizes historical data from Interactive Brokers, while the second sample code illustrates how users can improve their backtesting experience by supplying their own historical data. He emphasizes the importance of analyzing backtesting performance to identify areas for future improvements.

During the presentation, Dr. Liu introduces the Hybrid Pie feature, which allows users to trade various securities or commodities offered by advisor brokers and manage multiple accounts simultaneously. He highlights the benefits for investment advisers, such as executing multiple trading strategies and running consulting algorithms during backtesting.

To connect with brokers and retrieve real-time data, Dr. Liu demonstrates how to switch between different files within iBridgePy, such as "Example Show Positions," "Example Real-Time Prices," and "Get Historical Data." Users can customize account codes, file names, and access account balances and pending orders. The initialization function in iBridgePy declares global variables, and users can utilize code boxes and seek assistance from the iBridgePy team by reporting hybrid pack versions. Dr. Liu also provides guidance on closing IBGateway and opening TWS Trade Workstation.

The speaker covers the key functions in iBridgePy, starting with the initialization function that runs at the beginning of the code. The handle data function, which makes trading decisions, can be scheduled to run every minute or at a configurable interval. Users can utilize the schedule function for event scheduling instead of handle data. Real-time prices for specific securities can be accessed using the show_real_time_price function, while historical data can be retrieved with the request_historical_data function. To buy or sell securities, the place_order function is utilized. Dr. Liu includes a live demo of the stock screener function, showcasing its functionality.

Dr. Liu discusses the stock screener feature in iBridgePy, which enables users to search for stocks based on social media sentiment and price. Users can define their search parameters, such as instrument type, location code, and scan code. An example is given using social sentiment net as the scan code for a search of US major stocks with prices above $100. The speaker demonstrates the stock screener in action, presenting a list of the most active stocks and emphasizing how social media sentiment can provide insights into stock popularity. He concludes by highlighting the importance of selecting contracts and setting risk management guidelines when building algorithmic trading strategies.

The basic steps to building algorithmic trading strategies using iBridgePy are explained by Dr. Liu. He discusses the selection of a stock, determining the time interval for making trading decisions, calculating technical indicators through historical data, selecting order types, and handling exceptions. As an example, he presents a simple reversion strategy based on manually inputting the SPY contract and making trading decisions using daily closing prices. The sample code includes scheduled functions and a daily function to make trading decisions.

Dr. Liu delves into the backtesting features of iBridgePy, emphasizing the ability to retrieve historical data and utilize a pandas dataframe for analyzing the closing prices of securities. He demonstrates a sample code for a trend strategy employing a moving average crossover and explains the fundamentals of backtesting. Backtesting involves implementing a practice strategy based on historical data to inform trading decisions and testing its performance against historical data. He also highlights the alternative use of the handle data function for controlling the timing of function execution, providing an option to the scheduled function based on US market open and close times.

Dr. Liu provides insights into the iBridgePy backtesting system and the process involved. He explains that in backtesting mode using iBridgePy, the time capsule or candlesticks used are very similar to live trading. Each candlestick contains information such as the starting time, open price, high price, low price, close price, and volume. However, the time capsule does not include the ask or bid price, as the spread cannot be accurately modeled in backtesting.

Dr. Liu mentions that the backtesting mode supports market orders, limit orders, and stop orders, but trailing orders are not currently supported as they may not provide meaningful results. Transaction details during backtesting are stored in the output folder, with the file name containing the time when the backtesting was started.

Moving on, Dr. Liu explains the basic process of setting up a backtesting period in iBridgePy. Users can set the time frame to retrieve historical data for specific stocks and specify the time spot frequency as one minute, one hour, or one day. Historical data from older contracts can be retrieved by using tickers like SPY or AAPL. The code should be run specifically at the latest spot time, ensuring that the data within the desired time frame is available. By default, the cash starts from $10,000. Dr. Liu proceeds to demonstrate how to display account balance and position using a demo.

In his presentation, Dr. Liu demonstrates how to use iBridgePy's backtesting features to simulate a demo code for close price reversion. He shows how to use real-time prices based on historical data and how to run the target to observe its performance. He also showcases how the interval can be changed to run the code every hour instead of every minute. Overall, the video provides a useful demonstration of how to utilize iBridgePy's backtesting features for investment strategies.

Dr. Liu goes on to discuss the importance of making improvements to the backtesting process using iBridgePy. He suggests avoiding running the same code repeatedly to fetch the exact same data, as it wastes resources and violates IB spacing rules. Instead, he recommends fetching historical data using a defined plan with the same file name, reducing the amount of code that needs to be executed. Another suggestion is to use historical data provided by the user instead of fetching data from Interactive Brokers, which can make the backtesting process more efficient.

Furthermore, Dr. Liu introduces the feature of supplying local data for backtesting in iBridgePy. Users can provide a CSV file with all the necessary columns, such as opening price, high price, closing price, volume, etc. iBridgePy will ingest the data from the local file and simulate the backtesting accordingly. If the volume is unavailable, users can input '-1' as the value. This feature ensures accurate results for users' personal strategies and enhances the efficiency of simulations.

Dr. Liu also highlights the additional backtesting features in iBridgePy, such as custom time generators and the ability to use random numbers for testing code. He demonstrates how to create a time series using a Python package and a custom list and how to test code using random numbers instead of real-time prices. He further discusses the importance of performance analysis and introduces the usage of a log file to track and analyze the performance of backtesting. These new features provide faster and more efficient ways to test and analyze code in iBridgePy.

In addition, Dr. Liu discusses other features of iBridgePy's backtesting, including the balance log, which provides more details on portfolio value and cash, as well as the transaction log. He demonstrates how to visualize and analyze backtesting results using metrics such as the Sharpe ratio and the matplotlib package to create charts. Dr. Liu notes that once users are satisfied with their results on a paper account, they can switch to a live account for real trading.

During the Q&A session of the webinar, Dr. Liu addresses various questions from the attendees regarding the latest features of iBridgePy. One question pertains to managing other people's investment accounts with iBridgePy. Dr. Liu explains that individuals can become a portfolio advisor and set up a management account with Interactive Brokers to manage other people's investment accounts using iBridgePy.

Another question raised is about downloading sample code for a particular strategy. Dr. Liu suggests that attendees can obtain the necessary sample code from the iBridgePy website or by reaching out to the iBridgePy community forum. Additionally, he advises attendees on how to obtain historical option prices and Greek data for backtesting. Dr. Liu mentions that individuals can obtain historical option prices from Interactive Brokers and write Python code to utilize Greek data if they have access to it.

One attendee asks if iBridgePy supports an API to retrieve a watch list. Dr. Liu clarifies that currently, iBridgePy does not provide a specific API to get a watch list. However, he suggests that users can manually create a watch list within Interactive Brokers' platform.

In conclusion, Dr. Liu concludes the webinar by encouraging attendees to post any further questions they have on the iBridgePy community forum or to send him an email. He assures attendees that a recording of the presentation will be available upon request. Dr. Liu also reminds users to visit the iBridgePy website to download the software and access the API documentation. He expresses his gratitude to the participants for their attendance and attention throughout the webinar.

  • 00:00:00 Dr. Hui Liu discusses iBridgePy's latest backtesting features and provides a quick overview of iBridgePy. He reviews the cornerstone functions of iBridgePy and explains the basic steps to build an algorithm for trading strategies. He then focuses on the iBridgePy backtesting system and provides two sample codes for backtesting. The first sample uses historical data from Interactive Brokers, and the second sample discusses how to improve the backtesting experience by using historical data supplied by users. Dr. Liu lastly talks about the importance of analyzing backtesting performance for future improvements.

  • 00:05:00 Dr. Hui Liu discusses the latest backtesting features of IBridgePy, including the ability to trade any securities or commodities offered by advisor brokers and manage multiple accounts simultaneously with the Hybrid Pie feature. Investment advisers can also benefit from IBridgePy's ability to execute multiple trading strategies and run consulting algorithms during backtesting. Instructions for preparing the Hybrid Pie feature are straightforward, including downloading IBridgePy, creating a paper or live account with a broker like Interactive Brokers, and installing the IB software, either the lightweight IB gateway or the GUI-based trader workstation, and configuring it with IBridgePy.

  • 00:10:00 Dr. Hui Liu demonstrates how to use IBridgePy to connect to brokers and get real-time data by switching between different files such as “Example Show Positions”, “Example Real-Time Prices”, and “Get Historical Data”. Users can change account codes and file names, and can check account balances and pending orders. The initialize function declares global variables in IBridgePy, and users can use code boxes and ask for help from the IBridgePy team by reporting hybrid pack versions. Finally, Dr. Hui Liu shows how to close IBGateway and open TWS Trade Workstation.

  • 00:15:00 The speaker quickly goes over how to get connected to TWS and then explains the key functions in iBridgePy. The initialization function is declared to run at the beginning of the code, while the handle data function is where trading decisions are made and runs every minute or at a configurable interval. To schedule events, the user can use the schedule function instead of handle data. Real-time price for a specific security can be accessed using the show_real_time_price function and historical data can be retrieved using the request_historical_data function. The place_order function is used to buy or sell securities, and the speaker also provides a live demo of the stock screener function.

  • 00:20:00 Dr. Hui Liu, the creator of iBridgePy, demonstrates how to use the stock screener feature to search for stocks based on social media sentiment and price. He explains that users can define their search parameters for the scanner by specifying the instrument type, location code, and scan code, with an example of using social sentiment net as the scan code for a search of US major stocks with prices above $100. Dr. Liu then demonstrates the stock screener on the live market, showing a list of the most active stocks and how social media sentiment can provide insight into the popularity of certain stocks. Finally, he outlines some key considerations for building algorithmic trading strategies, including choosing which contracts to trade and how to set risk management guidelines.

  • 00:25:00 Dr. Hui Liu discusses the basic steps to building algorithmic trading strategies using iBridgePy, followed by a simple reversion strategy example. The steps to building a strategy include selecting a stock, determining the time interval for making trading decisions, calculating technical indicators through historical data, selecting order types, and handling exceptions. The simple reversion strategy involves manually inputting the SPY contract and making trading decisions based on daily closing prices. The sample code for the strategy includes scheduled functions and a daily function to make trading decisions.

  • 00:30:00 Dr. Hui Liu discusses iBridgePy's latest backtesting features, including the ability to retrieve historical data and use a pandas dataframe to analyze the close prices of securities. He demonstrates a sample code for a trend strategy using moving average crossover and explains the fundamentals of backtesting, which involve implementing a practice strategy by considering past data to inform trading decisions and then testing it against historical data to see if it produces successful outcomes. Liu also notes that the traditional handle data function can be used to control when a function is run, providing an alternative to the scheduled function which relies on US market open and close times.

  • 00:35:00 Dr. Hui Liu explains that in backtesting mode using Ibridgepy, the time capsule or candlesticks that are used are very similar to live trading. Each candlestick contains information such as the starting time, open price, high price, low price, close price, and volume. However, the time capsule does not include the ask or bid price as the spread cannot be well modeled in backtesting. The data can be supplied by IB or a user can supply their own data sources in any format as long as Python can read it and transform it into a pandas data frame. The backtesting mode currently supports market order, limit order, and stop order, but not trailing order as it is not accurate and doesn't provide meaningful results. Finally, transaction details are stored in the output folder with a file name containing the time when the backtesting was started.

  • 00:40:00 Dr. Hui Liu explains the basics of the iBridgePy backtesting system. When a simulated trading day begins, the system calculates the portfolio value and records it in the balance log. Then, he demonstrates using historical data from Interactive Brokers and backtesting a strategy called "close price reversion." To do this, he selects the file name and modifies some parameters, such as data provider name and run mode. He also explains the concept of a historical data ingestion plan, where users tell iBridgePy what data they want to receive from Interactive Brokers to use in their strategy. To run backtests, users require daily bar historical data and one minute data to retrieve market closed price info one minute before the market closes.

  • 00:45:00 Dr. Hui Liu explains the process of setting up a backtesting period in iBridgePy. The time frame can be set to retrieve historical data for specific stocks, and the time spot frequency can be set for one minute, one hour, or one day. Historical data from old contracts can be retrieved by using the SPY or AAPL ticker. The code should run specific in the latest spot time, and data within the proper time frame should be available. The default cash starts from $10,000, and Hui proceeds to show how to display account balance and position using a demo.

  • 00:50:00 Dr. Hui Liu demonstrates how to use IBridgePy's backtesting features to simulate a demo code for close price reversion. He shows how to use real-time prices based on historical data and how to run the target to see how it performs. He also shows how to change the interval to run the code every hour instead of every minute. Overall, the video provides a useful demonstration of how to use IBridgePy's backtesting features for investment strategies.

  • 00:55:00 Dr. Hui Liu discusses making improvements to the backtesting process using iBridgePy. The first suggestion is to avoid running the same code repeatedly to fetch the exact same data, which wastes resources and violates IB spacing rules. Instead, historical data is fetched using a defined plan with the same file name, therefore reducing the amount of code that needs to be run. The second suggestion is to use historical data provided by the user instead of fetching data from IB, making the backtesting process more efficient.

  • 01:00:00 Dr. Hui Liu discusses the feature of supplying local data for backtesting in IBridgePy. Instead of fetching data from the ID server, users can provide a CSV file with all the necessary columns, such as opening price, high price, closing price, volume, etc. IBridgePy will ingest the data from the local file and simulate the backtesting accordingly. However, if the volume is unavailable, users can input '-1' as the value. Additionally, this feature provides accurate results for a user's personal strategies and can result in efficient simulations.

  • 01:05:00 Dr. Hui Liu discusses the latest backtesting features of IBridgePy, such as custom time generators and the ability to use random numbers for testing code. He demonstrates how to create a time series using a Python package and a custom list, and how to test code using random numbers rather than real-time prices. He also discusses performance analysis using a log file to track and analyze the performance of the backtesting. Overall, these new features provide faster and more efficient ways to test and analyze code in IBridgePy.

  • 01:10:00 Dr. Hui Liu discusses additional features of iBridgePy's backtesting, including the balance log which shows more details on portfolio value and cash, as well as the transaction log. He also demonstrates how to visualize and analyze backtesting results using the Sharpe ratio and matplotlab package to create charts. Dr. Liu notes that once users are satisfied with their results on a paper account, they can then switch to a live one.

  • 01:15:00 Dr. Hui Liu responds to various questions from webinar attendees about the latest features of iBridgePy. He mentions that individuals can manage other people's investment accounts with iBridgePy after becoming a portfolio advisor and setting up a management account with IB. Attendees also ask about downloading sample code for the strategy and getting historical option prices and Greek data for backtesting. Liu suggests that individuals can obtain historical option prices from IB and write Python code to use the Greek data if they have it. Finally, he mentions that iBridgePy does not support an API to get a watch list, but individuals can manually create one in IB.

  • 01:20:00 Dr. Hui Liu ends the webinar by encouraging attendees to post any further questions they have on the iBridgePy community forum or send him an email. He also mentions that a recording of the presentation will be available upon request, and that he will be posting an iBridgePy tutorial soon where attendees can obtain the necessary code. Dr. Liu also reminds users that they can visit the iBridgePy website to download the software and view the API documentation. He closes by thanking the participants for their attendance and attention.
IBridgePy’s Latest Backtesting Features By Dr. Hui Liu - August 9, 2019
IBridgePy’s Latest Backtesting Features By Dr. Hui Liu - August 9, 2019
  • 2019.09.11
  • www.youtube.com
Session outline:- IBridgePy's cornerstone functions.- Basic steps to build a trading strategy.- Backtest strategies using IB's historical data.- Backtest str...
 

Programmatic Trading in Indian Markets with Python | Kite Connect API | Algo Trading Zerodha


Programmatic Trading in Indian Markets with Python | Kite Connect API | Algo Trading Zerodha

The speaker introduces Kite Connect, a platform that allows users to access their trading accounts programmatically using Python. They explain that the motivation behind Kite Connect was to address the challenge of increasing market participation in India. They introduced the concept of brokerage as a service, enabling startups to use Kite Connect's APIs to build customized trading experiences for their clients. This, in turn, aims to attract the next 10-20 million Indians to invest in the markets. The speaker also highlights Zerodha as a leading brokerage firm in India, with over 175,000 clients and a significant contribution to the Indian stock market turnover.

The speaker provides insights into the origins of Kite Connect, which was developed to provide a simple and affordable online trading platform for thousands of clients in India. Recognizing the large pool of software engineers in India who often lack opportunities to utilize their skills, Kite Connect made its APIs available for software engineers to backtest strategies and manage positions. While the platform charges a fee for API usage, they also offer a discount code for new developers who sign up. The speaker then introduces a colleague who proceeds to demonstrate how easy it is to build a custom trading platform using Kite Connect. The demonstration focuses on placing orders, retrieving position holdings, and eventually developing a trading strategy.

The speaker offers a step-by-step guide on obtaining an API key for the Kite Connect API, which is crucial for programmatic trading in the Indian market using Python. They also explain the concept of a postback URL, which is used to notify users about the status of their orders and trade confirmations. Additionally, the speaker emphasizes the significance of installing the Kite Connect Python library, which simplifies the usage of the API with just a few lines of code. They discuss the essential functionalities of a trading app, the importance of request responses and handling exceptions during the app's development process, and the significance of secure authentication for any trading app.

Next, the speaker explains how Python can be used for programmatic trading in Indian markets using Zerodha's Kite Connect API. The process begins with using the API key to redirect to the Kite platform and login to the user's account. Once logged in, a request token is sent back, which is then used to authenticate the user and obtain an access token. The speaker provides a hands-on demonstration of using the Kite Connect API, covering steps such as importing the library, storing the API key, and obtaining a request token. Finally, the request token is hashed with the API secret to obtain an access token.

The video continues by discussing how to use Python and the Kite Connect API for programmatic trading on Indian markets. It walks through the process of setting up access and public tokens, which are then used to establish a connection with the Kite Connect API for streaming market information. The video explains how to place an order, including specifying the trading symbol, quantity, transaction type, order type, and product. It also covers how to retrieve order details, holdings, and positions. The speaker highlights that there are many additional API calls beyond what's covered in the video, but they are all well-documented on Kite Connect's website.

The speaker delves into the importance of instruments when using the Kite Connect API for programmatic trading in Indian markets. Instruments are used to handle various stock symbols and contracts, and they require a unique instrument ID to subscribe or unsubscribe to streaming data. The speaker explains that the instrument ID can be obtained from a comprehensive CSV file and is also used to access historical data. WebSocket streaming is discussed, including different modes of data reception such as LTP mode, code mode, and full text mode. The speaker provides an example of how streaming data works through WebSocket and how callbacks can be used to receive data. Additionally, the speaker explains the use of postback and the requirement of a public URL for its implementation.

The speaker covers the implementation of a trading strategy using historical data and emphasizes the ease of placing an order using the Kite Connect API. They discuss how a startup can have an endpoint to receive postbacks from the Kite server, enabling notifications for successful or failed orders, among other things. The process of obtaining historical data using the API is demonstrated, with an emphasis on subscribing to Kite's historical data feature before requesting the data. Finally, a basic moving average strategy is presented to illustrate the simplicity of placing orders with the Kite API.

The speaker shares their own strategy for programmatic trading in Indian markets using Python and the Kite Connect API. The strategy involves calculating the moving average and placing a trade whenever there is a crossover. They showcase the function for placing orders, highlighting that just one line of code is needed for actual trading. While acknowledging that their strategy may not be the best, they emphasize its ease of implementation and modification for different trading symbols and transaction types. The speaker mentions that the Kite Connect API is open source, allowing for customization and use in creating trading platforms. They also mention the extensive documentation and active forum available on the Kite Connect website to support users with any questions or issues.

The CEO of Quant Institute (QuantInsti) introduces their company, which has been actively involved in algorithmic and quantitative trading since 2010. They provide training programs and events for thousands of students globally. The CEO expresses alignment with the programmatic usage of Python in trading strategies discussed in the webinar and apologizes for not being able to answer all the questions but assures the audience that their team is diligently surveying and addressing all the inquiries.

The speaker discusses the impact of QuantInsti's EPAT program, a comprehensive six-month online course focused on quantitative and algorithmic trading. The program covers a wide range of topics, including statistics, econometrics, financial computing, Python, MATLAB, backtesting tools, and various trading strategy paradigms. They highlight that the course is taught by a diverse group of experienced practitioners from around the world. Graduates of the program are well-prepared for entering the workforce, and QuantInsti offers career services and guidance for those interested in setting up their own trading desks. The program's global reach is emphasized, with alumni in over 30 countries across six continents.

The speaker invites viewers to join an upcoming informative session on algorithmic trading scheduled for November 3, 2016, at 6:30 pm IST. They encourage the audience to follow the company on social media platforms such as Facebook, Twitter, LinkedIn, and YouTube, where they can find educational resources and participate in discussions. They offer to send links to interested participants and invite them to submit any queries through the provided form on borninstorms. Finally, the speaker concludes by stating that they will now begin addressing questions from the audience.

  • 00:00:00 The speaker introduces Kite Connect and explains how it enables users to access their trading accounts programmatically using Python. The motivation behind Kite Connect was to solve the challenge of growing the Indian market participation, and the concept of brokerage as a service was introduced. Startups can use Kite Connect's APIs to build niche user experiences for their clients, which will help to attract the next 10-20 million Indians to invest in the markets. The speaker also mentions that Zerodha offers the best platforms in the country and has over 175,000 clients, contributing to a significant portion of the Indian stock market turnover.
    |
  • 00:05:00 The speaker explains the origins of Kite Connect, which was developed to provide a simple and affordable online trading platform to thousands of clients in India. Recognizing India's vast pool of software engineers who often do not have the opportunity to use their skillset, Kite Connect made its APIs available for software engineers to backtest strategies and manage positions. The platform charges a fee for using its APIs, but also offers a discount code to new developers who sign up. The speaker then hands over to another colleague to demonstrate how easy it is to use Kite Connect to build a custom trading platform, with the aim of placing orders, getting position holdings, and eventually developing a strategy.

  • 00:10:00 The speaker provides a step-by-step guide on how to obtain an API key for the Kite Connect API, which is essential for programmatic trading in the Indian market using Python. The speaker also explains the postback URL, which is used to notify users about the status of their orders and trade confirmations. Additionally, the speaker emphasizes the importance of installing the Kite Connect Python library, which is a wrapper around the Kite Connect API built to simplify the usage of the API within a few lines of code. The speaker also sheds light on the breakdown of a trading app's basic functionalities and the significance of request responses and exceptions in the trading app's development process. The login flow is also explained here, which emphasizes the importance of secure authentication for any trading app.

  • 00:15:00 The speaker explains how to use Python to programmatic trade in Indian markets using Zerodha's Kite Connect API. The first step is to use the API key to redirect to the Kite platform and login to the user's account. Once logged in, a request token is sent back which is then used to authenticate the user and obtain an access token. The speaker goes on to provide a hands-on demonstration of using the Kite Connect API, including importing the library, storing the API key, and obtaining a request token. Finally, the request token is used to obtain an access token by hashing it with the API secret.

  • 00:20:00 The video discusses how to use Python and the Kite Connect API for programmatic trading on Indian markets. The video walks through the process of setting up an access token and public token, and then using those tokens to set up a connection with the Kite Connect API for streaming information. The video also explains how to place an order, including the necessary components like trading symbol, quantity, transaction type, order type, and product. Finally, the video covers how to retrieve order details and holdings and positions. The video emphasizes that there are many additional API calls beyond those covered in the video, but they are all well-documented on Kite Connect's website.

  • 00:25:00 The speaker discusses the importance of instruments when using Kite Connect API for programmatic trading in Indian markets with Python. Instruments are used to deal with various stock symbols and contracts, and they require a unique instrument ID to subscribe or unsubscribe to streaming data. The speaker explains that the instrument ID can be obtained from a huge CSV file and is also used to access historical data. The speaker also discusses WebSocket streaming and the different modes in which data can be received, such as LTP mode, code mode, and full text mode. The speaker provides an example of how streaming data works through WebSocket and how to use callbacks to receive data. Finally, the speaker discusses the use of postback and the need for a public URL to implement it.

  • 00:30:00 The speaker discusses implementing a strategy using historical data and the ease of placing an order using the Kite Connect API. He explains how a startup can have an endpoint to receive postbacks from the Kite server, which can be used to trigger notifications for successful or failed orders, among other things. He also demonstrates how to get historical data using the API and emphasizes the need to subscribe to Kite's historical data feature before requesting historical data. Finally, he presents a basic moving average strategy to illustrate how simple it is to place orders with the Kite API.

  • 00:35:00 The speaker explains their strategy for programmatic trading in Indian markets using Python and the Kite Connect API. The strategy involves calculating the moving average and placing a trade whenever there is a crossover. The function for placing orders is shown, with one line of code for actual trading. The speaker emphasizes that while their strategy is not the best out there, it is easy to implement and modify for different trading symbols and transaction types. The Kite Connect API is open source, allowing for modification and use in creating a trading platform. The speaker also mentions the extensive documentation and active forum available on the Kite Connect website to assist with any questions or issues.

  • 00:40:00 The CEO of Quantity introduces their company, which has been involved in algorithmic and quantitative trading since 2010 and provides training programs and events for thousands of students globally. They mention their alignment with the programmatic usage of Python in trading strategies discussed in the webinar and apologize for not being able to answer all the questions, but assure the audience that their team is working on surveying and addressing all of the inquiries.

  • 00:45:00 The speaker discusses the impact of QuantInsti's EPAT program, which is a six-month long online course focused on quantitative and algorithmic trading. The program covers a broad range of topics, including statistics, econometrics, financial computing, Python, MATLAB, backtesting tools, and various trading strategy paradigms. The course is taught by an international pool of more than a dozen practitioners with a wealth of experience in the industry. Graduates of the program are well-equipped to enter the workforce, and QuantInsti offers a variety of career services, as well as complimentary guidance for those who wish to set up their own trading desks. The program has achieved global reach, with alumni in over 30 countries on six continents.

  • 00:50:00 The speaker invites viewers to join their upcoming informative session on algorithmic trading that will be conducted on November 3, 2016, at 6:30 pm IST. He also encourages them to follow the company on social media platforms like Facebook, Twitter, LinkedIn, and YouTube, where they can find a plethora of educational resources and participate in discussions. The speaker also offers to send links to interested participants and invites them to fill out any query form on borninstorms. Finally, he concludes by saying that they will now begin taking questions from the audience.
Programmatic Trading in Indian Markets with Python | Kite Connect API | Algo Trading Zerodha
Programmatic Trading in Indian Markets with Python | Kite Connect API | Algo Trading Zerodha
  • 2016.10.18
  • www.youtube.com
Discover how to harness the power of Algo Trading with Zerodha's Kite Connect APIs to implement Programmatic Trading in Indian Markets using Python. As it pr...
 

Trading with Interactive Brokers using Python | By Dr. Hui Liu



Trading with Interactive Brokers using Python | By Dr. Hui Liu

Good evening, everyone! Welcome to the webinar on implementing algorithmic trading strategies in the online market, hosted by Kuantan Ste. My name is Slow Me, and I'm a Senior Program Manager here. We are delighted to have participants from all around the world joining us today, including those from Hawaii who have managed to join us bright and early in the morning. Your enthusiasm is truly inspiring!

Our esteemed speaker for today's webinar is Dr. Hoyle Yu, a renowned author and expert in trading. Dr. Yu has an impressive background with extensive experience in US equities and the forex market. He has also developed a popular Python trading platform that enables traders to easily implement their strategies. With a PhD from the University of Virginia and an alumni of Shanghai University, Dr. Yu brings a wealth of knowledge to this webinar. We are honored to have him here with us.

Before we begin, let me provide a brief overview of what we will cover in today's webinar. Dr. Yu will first discuss the advantages of using Interactive Brokers as a trading platform. He will then delve into the benefits of using Python for trading and introduce his own tool, IBPY, which simplifies trading with Interactive Brokers. The webinar will focus on key aspects of real-time trading, including accessing real-time price data, retrieving historical data, and placing orders. Dr. Yu will also introduce the Moving Average Crossover trading strategy.

We are excited to have such an esteemed speaker and a diverse audience for this webinar. Without further delay, I invite Dr. Hoyle Yu to take the floor and share his expertise on trading with Interactive Brokers using Python and IBPY.

Today, I will be discussing trading with Interactive Brokers using Python, specifically utilizing IBPY, a tool I have developed. Before we dive into the details, let's begin by exploring the advantages of Interactive Brokers as a trading platform.

Interactive Brokers is a well-established brokerage firm based in the United States. It offers several key advantages that make it a popular choice among traders. Firstly, Interactive Brokers provides a robust API for automated trading, which will be our main focus today. This API empowers traders to execute their strategies efficiently and effectively. Secondly, Interactive Brokers offers competitive trading costs, making it a cost-effective option for traders. You can easily compare their trading costs on their website. Based on my personal experience, they provide low-cost trading options. Thirdly, Interactive Brokers has an extensive global presence, offering access to over a hundred market centers in 24 countries. This global reach allows traders to tap into various international markets, enabling diversification and expanded trading opportunities. Lastly, Interactive Brokers supports a wide range of products, including stocks, options, futures, and forex, catering to the diverse needs of traders. These advantages make Interactive Brokers a preferred choice for many traders.

Now, let's explore the benefits of using Python for trading. Python offers numerous advantages, particularly for beginners. Compared to other programming languages like Java or C++, Python is relatively easy to learn. With some introductory knowledge, one can quickly grasp the fundamentals of Python and start implementing trading strategies. Moreover, Python is an open-source language, providing access to a vast collection of modules and libraries. These resources can be easily downloaded and integrated into your trading systems, offering powerful tools for data analysis, visualization, and algorithmic trading. Python's expansive ecosystem makes it a flexible and versatile language for traders.

Now that we understand the advantages of Interactive Brokers and Python, let's explore the capabilities of IBPY. IBPY simplifies trading with Interactive Brokers by providing a user-friendly interface for Python developers. It streamlines tasks such as accessing real-time price data, retrieving historical data, and placing orders. IBPY bridges the gap between the Interactive Brokers API and Python, making it easier for traders to implement their strategies without dealing with complex code.

One of the key features of IBPY is its ability to access real-time price data. Traders can retrieve live market data, including stock quotes, option chains, and futures prices, directly into their Python environment. This real-time data enables traders to make informed decisions based on the latest market information, improving their chances of success.

IBPY also allows traders to retrieve historical data, which is essential for backtesting and analyzing trading strategies. By accessing historical price data, traders can evaluate the performance of their strategies over different market conditions and make data-driven adjustments. IBPY provides a convenient interface to fetch historical data for a wide range of instruments, timeframes, and data types, enabling thorough analysis and strategy refinement.

Placing orders is another crucial aspect of algorithmic trading, and IBPY simplifies this process. Traders can use the IBPY API to submit various types of orders, including market orders, limit orders, and stop orders. They can specify parameters such as quantity, price, and order type, and IBPY handles the execution seamlessly. This functionality allows traders to automate their trading strategies, saving time and reducing the risk of manual errors.

In addition to these features, IBPY supports a range of advanced trading operations. Traders can retrieve account information, manage positions and portfolios, monitor account activity, and receive real-time trade notifications. This comprehensive set of functionalities empowers traders to have full control over their trading activities and make timely decisions.

Now, let's briefly discuss the Moving Average Crossover trading strategy, which Dr. Yu will cover in detail later in the webinar. This strategy is based on the concept of moving averages, which are widely used in technical analysis. The Moving Average Crossover strategy involves monitoring two moving averages of different time periods, typically a shorter-term moving average and a longer-term moving average. When the shorter-term moving average crosses above the longer-term moving average, it generates a buy signal, indicating a potential upward trend. Conversely, when the shorter-term moving average crosses below the longer-term moving average, it generates a sell signal, indicating a potential downward trend. This strategy aims to capture trends and take advantage of price movements.

In conclusion, today's webinar will provide valuable insights into trading with Interactive Brokers using Python and IBPY. Dr. Hoyle Yu, with his extensive knowledge and expertise, will guide us through the advantages of Interactive Brokers, the benefits of using Python for trading, the capabilities of IBPY, and the Moving Average Crossover trading strategy. We hope you find this webinar informative and engaging. Without further ado, I hand it over to Dr. Yu to begin his presentation. Thank you all for joining us today, and enjoy the webinar!

Trading with Interactive Brokers using Python | By Dr. Hui Liu
Trading with Interactive Brokers using Python | By Dr. Hui Liu
  • 2016.09.29
  • www.youtube.com
Dr. Hui Liu speaks about implementing algorithmic trading strategies in live markets with Interactive Brokers using Python. This webinar is perfect for begin...
 

Automated Trading with Python | Webinar by Dr. Yves J. Hilpisch



Automated Trading with Python | Webinar by Dr. Yves J. Hilpisch

Dr. Yves J. Hilpisch is introduced as the founder and managing partner of Python Course Group, a company focused on Python and open-source technologies in the financial industry, particularly in the areas of computational finance and financial data science. He has authored several books on Python and finance and has developed a platform called DataPark, which enables data scientists to work with structured data directly in the browser. Dr. Hilpisch mentions that he will be releasing a new book later this year and also introduces TPQ, a global network of Python quants and companies.

The webinar begins with Dr. Hilpisch acknowledging the increasing number of inquiries he has received about using Python in finance, particularly from start-ups aiming to attract Python developers for finance-related projects. He announces a new feature that allows users to download historical data from the Comm Trading Platform and outlines the three main topics that will be covered in the webinar: downloading historical data, coding and backtesting a trading strategy, and working with streaming data for algorithmic trading. He emphasizes the importance of data and backtesting in developing sensible trading strategies.

Dr. Hilpisch proceeds to demonstrate how to retrieve historical data using Python, focusing on the German DAX index as an example. He explains that users can select from over 100 different instruments, including currency pairs and commodities like gold or silver. By retrieving data in chunks and appending it to a DataFrame object, users can store the data on disk for efficient retrieval without the need for repeated downloads. He emphasizes the role of data in backtesting and its significance in evaluating trading strategies.

Next, Dr. Hilpisch shows an example of a simple trading strategy based on two moving averages: a shorter one (5 minutes) and a longer one (15 minutes). He explains that when the shorter trend crosses above the longer trend, it generates a buy signal, and when the shorter trend crosses below the longer trend, it generates a sell signal. He clarifies that this is not investment advice but demonstrates how to implement the strategy in Python using a vectorized approach, making the code efficient and concise.

Dr. Hilpisch moves on to calculating the performance of a trading strategy using previously calculated log returns. He explains how traders can determine the performance of their strategy by multiplying their position by the realized return. He uses an example of investing long in the market and waiting for one day to illustrate the calculation. He highlights the importance of testing the trading strategy to avoid in-sample bias and cautions that positive performance in one period does not guarantee success in other periods.

The discussion then shifts to comparing the standard deviation of returns between a market investment strategy and a trend-based investment signaling strategy based on minute blocks. Dr. Hilpisch demonstrates that the trend-based strategy exhibits lower standard deviations of returns. He emphasizes the need for working with streaming data when implementing automated trading strategies and introduces the Plotly API for generating real-time plots.

Dr. Hilpisch demonstrates the use of a fake API that generates data in real-time format. Although the current demonstration is not truly streaming, he mentions that it can be made to stream with some additional effort by instantiating additional objects and embedding them in the Jupyter Notebook. He provides an example of a for loop that collects data from the API and shows it in an interactive visualization area in real-time, point by point.

Automated trading using real-time or streaming data is then discussed. Dr. Hilpisch introduces the "buy" and "sell" functions, which place market orders and require the number of units to be traded. He presents the "TrendTrader" class, which is based on trend-following strategies and inherits from the streamer class of the Wonder API. He explains the custom attributes of the class, such as an empty data frame and the number of ticks set to 0 initially. The class allows for defining the first and second trends as parameters.

Dr. Hilpisch explains that the "onsuccess" method is called when new data is received from the streaming API, even if it arrives sporadically. The data is appended to the previously instantiated data frame and converted to the European time zone. The short-term and long-term trends are calculated based on the observations, and the appropriate trading actions are taken accordingly. He clarifies that while the strategy presented may not be sensible, his focus is on illustrating the process. He also discusses the "onerror" method, which handles any errors that may occur during the streaming process and disconnects from the API.

In conclusion, Dr. Hilpisch showcases the implementation of an automated trading strategy using Python. He demonstrates how a Python program can generate buy and sell orders automatically based on the comparison of moving averages. He emphasizes that successful algorithmic trading requires a combination of finance and technology skills, a deep understanding of the markets, proficiency in big data statistics, and the ability to automate trading processes. The webinar serves as an educational resource for individuals interested in acquiring the necessary knowledge and skills for a career in algorithmic trading.

  • 00:00:00 Dr. Yves J. Hilpisch introduces himself and provides some background information about his work. He is the founder and managing partner of Python Course Group, which focuses on Python and other open-source technologies used in the financial industry, specifically in the areas of computational finance and financial data science. Dr. Hilpisch has authored several books about Python and finance and has developed a platform called DataPark, which allows data scientists to work with structured data in the browser. He briefly mentioned that his new book would be released later this year, and also mentioned the beginning of a global network of Python quants and companies called TPQ.

  • 00:05:00 Dr. Yves J. Hilpisch begins the webinar with a discussion of how he has received an increased number of queries asking how to best use Python in finance, and highlighting that many start-ups are a specific audience looking to attract people to build Python for finance. Hilpisch then introduces a new ability to download historical data from the Comm Trading Platform, and shares the three main topics which the webinar will cover such as 1) how to download historical data, 2) how to code and back test a trading strategy, and 3) how to work with streaming data — a critical requirement for algorithmic trading. Hilpisch keeps the topic simple and attempts to convey the underlying ideas and explanation of steps needed to implement these single steps, and shares how without data and subsequently, backtesting, you cannot have any sensible trading strategies.

  • 00:10:00 Dr. Yves Hilpisch discusses data retrieval by demonstrating how to retrieve historic data for an instrument, specifically the German DAX index. Users can select an instrument from more than 100 different options, including currency pairs and commodities like gold or silver. The command results in a JSON object containing two observations: the open, close, and bid. Hilpisch notes that because two daily observations are not sufficient for backtesting, he will show how to retrieve data for a more extended period. By generating a data set that is longer than two daily observations and storing it on disk, users can quickly retrieve this information without having to spend the time re-retrieving it.

  • 00:15:00 Dr. Yves J. Hilpisch demonstrates how to retrieve and store financial market data using Python. The data is retrieved in chunks and appended to a DataFrame object, which is then stored in a binary format. He emphasizes the importance of data in backtesting and how it allows for the judgment of trading strategies. Additionally, he shows an example of a simple trading strategy, based on two different trends, and how to calculate the log returns as a basis for later performance judgment. The trading strategy is just one example, and his presentation aims to demonstrate the Python tools and techniques available for automated trading.

  • 00:20:00 The speaker explains a simple trading strategy using two moving averages, one shorter (5 minutes) and one longer (15 minutes), to generate signals for when to buy or sell the asset. If the shorter trend crosses the longer trend from below, it is a signal to buy, and if the shorter trend crosses the longer trend from above, it is a signal to sell. The speaker warns that this is not investment advice and emphasizes the use of a backtest to see how the strategy has performed in the past. The speaker also shows how to implement the strategy in Python using a vectorized approach, making it efficient and concise.

  • 00:25:00 Dr. Yves J. Hilpisch discusses how to calculate the performance of a trading strategy using the returns calculated previously. By taking the position entered previously and multiplying it by the realized return, traders can determine their trading strategy's performance. Using the example of investing long in the market and waiting for one day, traders can multiply their position by the daily return of the market. By combining the previous position and the current return, traders can evaluate their trading strategy's performance over a specific time period, cautioning that a positive performance is not a guarantee for every period. He also gives a word of caution about testing the trading strategy to avoid in-sample bias and the possibility that the trading strategy might not be a good choice to implement.

  • 00:30:00 Dr. Yves J. Hilpisch discusses the standard deviation of returns for a market investment strategy versus a trend-based investment signaling strategy, both based on minute blocks. The trend-based strategy demonstrated lower standard deviations of returns, and Dr. Hilpisch emphasizes the importance of working with streaming data when implementing an automated trading strategy. He demonstrates the use of the Plotly API for generating real-time plots and discusses the need for a system that can process and make trading decisions based on real-time data.

  • 00:35:00 Dr. Yves J. Hilpisch demonstrates the use of a fake API that generates data in the same format as an actual API in real-time. The data is collected and shown in a dataframe object, which can be visualized using an interactive visualization area. While the current demonstration is not truly streaming, Dr. Hilpisch notes that it can be made to stream with a bit more effort by instantiating additional objects and embedding them in the Jupiter Notebook. He provides an example of a for loop that collects data from an API and shows it in the interactive visualization area in real-time, point by point.

  • 00:40:00 Dr. Yves Hilpisch discusses automated trading using real-time or streaming data. He introduces two functions, "buy" and "sell," which are used to place market orders and require the number of units being traded. The class used for trading is the "TrendTrader" which is based on trend-following strategies. This class inherits from the streamer class of the Wonder API, and includes custom attributes such as an empty data frame object and the number of ticks set to 0 in the beginning. The first and second trends can also be defined as parameters.

  • 00:45:00 Dr. Yves J. Hilpisch discusses how the method onsuccess gets called when new data is received from the streaming API, which may come in sporadically rather than at regular intervals. The data is then appended to a data frame instantiated previously and converted to the European time zone. Two trends, t1 and t2, are calculated based on short-term and long-term observations. If the short-term trend is above the long-term trend and the trader is not invested, they buy the specified number of units and set the not invested flag to false. Conversely, if the short-term trend is below or equal to the long-term trend and the trader is invested, they will sell. Dr. Hilpisch mentions that while this strategy may not be sensible, his focus is on illustrating how the process works. The on error method is also discussed, which catches any errors that may occur and disconnects from the API streaming.

  • 00:50:00 Yves Hilpisch showcases how to implement an automatic trading strategy using Python. He demonstrates how a Python program can automatically generate buy and sell orders based on the comparison of averages of five and 15 tick data. He emphasizes that algorithmic trading requires merging skills in finance and technology, understanding the markets, big data statistics, and automation. The video provides an educational resource for people interested in the gamut of operations and skills required for a successful algorithmic trading career.
Automated Trading with Python | Webinar by Dr. Yves J. Hilpisch
Automated Trading with Python | Webinar by Dr. Yves J. Hilpisch
  • 2016.02.15
  • www.youtube.com
The video is a recording of the webinar "Automated Trading with Python" which was conducted on 10th February 2016. The webinar aimed at introducing the audie...
 

Algorithmic Trading Using Python - Introduction



Algorithmic Trading Using Python - Introduction

Hello and welcome back to the first video in the brand new video series on trading. In this video, we will explore the purpose and content of this century's video stream. The main goal of this video series is to provide you with a comprehensive understanding of algorithmic trading. We will cover everything from the basics to advanced implementation, testing, and optimization of trading algorithms.

The series is designed to help you learn and utilize Python programming in the context of algorithmic trading. We will explore various topics such as generating trading signals, implementing strategies, backtesting, and deploying algorithms in live trading environments. To facilitate this, we will introduce you to a powerful algorithmic trading platform called the Contact Platform.

The Contact Platform is an integrated development environment that enables you to create, test, and deploy your trading algorithms. It offers a user-friendly interface and supports various programming languages, including Python, which we will be using extensively in this series. You can access the platform online via the Contact website or download the software for Windows or Mac.

To accompany the video series, we have created a dedicated YouTube page where you can find additional resources and code examples. These examples cover a range of topics, including clustering techniques, script creation for stocks, bonds, forex, CFDs, options, and more. Whether you are a beginner or an experienced trader, this series is the perfect starting point to dive into algorithmic trading.

It is crucial to understand the importance of developing and testing trading algorithms before deploying them in real trading scenarios. This is one of the most time-consuming aspects of algorithmic trading. However, we have made it easier for you by providing access to free historical market data with high-resolution minute-level data for European stock options and other major international markets. This will be a valuable resource for algorithm developers and researchers.

Throughout the series, we will leverage powerful libraries and tools for machine learning, sentiment analysis, and backtesting. These libraries, such as Pandas and NumPy, are widely used in the trading community and will be essential for our analysis. By utilizing these libraries and the Contact Platform, you will have the flexibility to create and test various trading strategies.

In the upcoming videos, we will take a step-by-step approach to coding trading algorithms. You will follow along with me as we discuss and implement different strategies. Additionally, I will share supplementary materials, such as code snippets and videos, on my YouTube channel and in accompanying articles.

In the next video, we will dive deeper into the development process and explore how trading algorithms work. Stay tuned and join us on this exciting journey into the world of algorithmic trading.

Algorithmic Trading Using Python - Introduction
Algorithmic Trading Using Python - Introduction
  • 2021.03.12
  • www.youtube.com
This is the first video of my algorithmic trading tutorial series in which you will learn everything you need to know to start writing your own trading bots ...
 

Algorithmic Trading Using Python #2



Algorithmic Trading Using Python #2

Welcome to the second video of our algorithmic trading course. In this video, we will delve into the process of algorithm development, focusing on the conceptual level. We will walk you through each step, from idea generation to live deployment, covering essential aspects such as research, implementation, backtesting, and optimization.

The algorithm development process begins with idea generation. We will explore various sources of ideas, including market observations, fundamental analysis, technical indicators, and quantitative models. Understanding how to generate and refine trading ideas is crucial for developing successful algorithms.

Once we have a promising idea, we move on to the research phase. This involves gathering relevant data, performing statistical analysis, and conducting thorough backtesting. We will discuss the importance of data quality and how it impacts the reliability of our algorithms. Additionally, we will explore different performance metrics and risk measures to evaluate the effectiveness of our strategies.

After conducting thorough research, we proceed to the implementation stage. Here, we translate our trading ideas into code. We will use programming languages such as Python to write algorithms that can analyze data, generate trading signals, and execute trades automatically. You will learn about coding best practices and how to structure your code for maintainability and scalability.

Once the algorithm is implemented, we move on to the backtesting phase. Backtesting involves simulating the algorithm's performance using historical data. We will discuss different backtesting methodologies, including in-sample and out-of-sample testing, to validate the algorithm's performance and assess its robustness.

Optimization is another critical step in algorithm development. We will explore various techniques to fine-tune our algorithms and optimize their performance. This includes parameter optimization, sensitivity analysis, and risk management strategies. By optimizing our algorithms, we aim to improve their profitability and reduce risk.

Finally, we will touch upon the live deployment of our algorithms. We will discuss considerations such as connectivity to market data, order execution, and risk management in a live trading environment. Understanding the challenges and considerations of live deployment is essential for successful algorithmic trading.

Throughout this video, we will provide insights and practical tips to guide you through the algorithm development process. By the end, you will have a comprehensive understanding of how to conceptualize, research, implement, backtest, optimize, and deploy trading algorithms effectively.

Now, let's dive into the fascinating world of algorithmic trading and explore the process of algorithm development in detail.

Algorithmic Trading Using Python #2
Algorithmic Trading Using Python #2
  • 2021.03.19
  • www.youtube.com
In this 2nd video of this algorithmic trading course, you will learn all about the algorithm development process on a conceptual level. We will cover everyth...
 

How to Use Support Resistance Levels and RSI for Profitable Currency Trading




How to Use Support Resistance Levels and RSI for Profitable Currency Trading

In the creation of the RSI trading strategy, the conventional method of identifying overbought and oversold areas is found to be ineffective. Instead, a more effective approach is proposed, which combines support and resistance levels with the RSI to improve entry points. This alternative method contradicts what is commonly taught in classic trading tutorials. However, when tested using Python, the results were more promising.

To implement this strategy, the first step is to identify support and resistance levels using fractals or candles that exhibit extreme high or low values compared to neighboring candles. The number of candles to compare with is a variable in the code, allowing flexibility in the comparison range. Additionally, a condition can be applied to consider candles with significant rejection movements, indicated by a wick length exceeding a certain threshold.

Once the support and resistance levels are detected, they may need to be merged if they are very close to each other. This can be done by computing the difference or distance between the levels. If the distance is below a specified threshold, the levels can be merged by replacing them with the mean value or eliminating duplicate levels.

After obtaining the key levels, reversal signals can be computed based on three assumptions. First, a candle should have a wick close to a support or resistance level, with its body contained by the level. If it's a support, the body should be above the level, and if it's a resistance, the body should be below the level. The previous candles should also be contained by the same support or resistance level. This ensures that the current candle's behavior aligns with the trend indicated by the surrounding candles.

When testing historical data, the generated signals can be plotted on a chart. A bullish reversal signal is represented by a purple signal point below the candle, while a bearish reversal signal is represented by a purple signal point above the candle. However, not all signals on the chart can be traded, as some may be more effective than others. To filter the signals, the RSI is used. If the RSI is above a threshold value, indicating an uptrend momentum, only bullish signals are considered. Conversely, if the RSI is below a lower threshold, indicating a downtrend momentum, only bearish signals are considered. This way, signals are aligned with the trend, and signals that go against the trend are filtered out.

To automate the indicators in Python and evaluate the strategy, a Jupyter Notebook file can be used. The Euro/US dollar candlestick data for the one-hour timeframe between 2003 and 2023 is loaded using the pandas library. The data is filtered to remove zero-volume candles and weekends, and the RSI is calculated using the pandas technical analysis library. The support and resistance levels are detected using functions that compare the current candle with neighboring candles, and the close proximity to the levels is checked using additional functions. These functions consider the body and wick of the candle in relation to the levels, as well as the preceding candles.

Once all the necessary functions are defined, a function called "check_candle_signal" is created to compute the signals. This function takes the index of the current candle, the number of candles on the left and right to consider, the number of back candles to check, and the data frame as inputs. It uses the previously defined functions to compute the support and resistance levels, merge them if necessary, and generate signals based on the defined conditions and RSI thresholds.

The signals are then counted and printed to evaluate the strategy's performance. To visualize the signals on the chart, the corresponding candles are selected and plotted, with the signals represented by purple points. The plot allows for a visual assessment of the signals and their alignment with the identified support and resistance levels.

After loading the candlestick data and performing necessary data cleaning steps such as removing zero-volume candles and weekends, the next step is to calculate the Relative Strength Index (RSI).

RSI is a popular momentum oscillator used to identify overbought and oversold conditions in a market. It measures the magnitude of recent price changes to determine whether an asset is overbought or oversold. RSI values range from 0 to 100, where values above 70 are typically considered overbought, and values below 30 are considered oversold.

To calculate RSI, you need to define a window size, which represents the number of previous candles to consider when calculating the indicator. The most common window size is 14, but you can adjust it based on your requirements and the characteristics of the asset you're analyzing.

The RSI calculation involves the following steps:

  1. Calculate the price change for each candle. This is the difference between the closing price of the current candle and the previous candle.

  2. Split the price changes into two separate series: gains and losses. Gains represent positive price changes, while losses represent negative price changes. If a price change is positive, it is considered a gain. If it is negative, it is considered a loss.

  3. Smooth the gain and loss series by replacing negative values with zeros and positive values with their absolute values.

  4. Calculate the average gain and average loss over the defined window size. This is usually done using the simple moving average (SMA) method.

  5. Calculate the relative strength (RS) by dividing the average gain by the average loss.

  6. Calculate the RSI by applying the formula: RSI = 100 - (100 / (1 + RS))

By plotting the RSI values over time, you can visually analyze the overbought and oversold conditions of the asset. Traders often use RSI as a signal for potential trend reversals or as confirmation for entry and exit points.

It's important to note that RSI is just one of many technical indicators used in financial analysis. Its effectiveness depends on various factors such as market conditions, the asset being analyzed, and the timeframe being considered.

How to Use Support Resistance Levels and RSI for Profitable Currency Trading
How to Use Support Resistance Levels and RSI for Profitable Currency Trading
  • 2023.05.11
  • www.youtube.com
In this Python FOREX and currency trading tutorial, we will show you how to use the Relative Strength Index (RSI) and Support/Resistance Levels for profitabl...
 

Moving Average And VWAP Trend Strategies Backtest In Python


Moving Average And VWAP Trend Strategies Backtest In Python

Hello, today we will compare the V-WAP (Volume-Weighted Average Price) and the Moving Average indicators for trading and algorithmic trading. We will use both indicators to build a trading bot and backtest it using Bitcoin data.

The profit returns of the strategies we will use in this video can vary from around 200 percent up to an outstanding 473 percent over three years of data. If you are interested in the coding part, the backtest is done using the Python language, and you can download the Jupyter Notebook file from the link provided in the description.

Apart from being my favorite indicator in trading, V-WAP is important because it helps traders determine whether a security is trading at a fair value or not. This information is crucial because if a price is trading above the V-WAP curve or far above the V-WAP curve, it could indicate that it's overvalued. Conversely, if the current price is trading below the V-WAP curve, it could indicate that the security is undervalued. In a nutshell, we always expect the price to converge back to the V-WAP level at some point. This can be observed in an example where the difference between the price candles and the yellow V-WAP curve increased before the price converged back to the V-WAP level and bounced off the yellow curve again. There is a clear difference between the moving average curve and the V-WAP yellow curve.

To calculate the V-WAP, we can use the following expression: V-WAP = Cumulative mean price * Volume / Cumulative volume. The mean price is the average price between the high, low, and closing prices. The cumulative part means the total sum since the trading session opened. The trading session can be reset daily, weekly, or monthly, depending on the timeframe we are aiming for. The V-WAP computation is different from a simple moving average as it incorporates more information on the trading volume.

We can also use V-WAP to determine optimal entry and exit points for trades, as it acts as a dynamic support and resistance level that moves along with the price. This is what we will be using V-WAP for in the strategy shown in this video.

The strategy we will be using is as follows: First, we will look at a series of candles that are either below or above the V-WAP or moving average curve, depending on which indicator we are using for the comparison. If the candles are below the curve, we are looking for a short signal, and the entry point will be determined when a candle is close enough to the curve. If the candles are above the curve, we are looking for a long signal, and the entry point will be determined when a candle is close enough to the curve. We will be applying the same strategy using both the moving average curve and the V-WAP curve for comparison.

In the Jupiter Notebook file, we load the Bitcoin US dollar candlestick data with a 15-minute timeframe between 2019 and 2022. We clean the data formats and use the pandas technical analysis module to compute the V-WAP and the EMA (Exponential Moving Average). We then compute the signal for both the EMA and V-WAP using a certain number of back candles. The signal values are stored in new columns in the data frame. We visualize the signal points for better understanding.

For backtesting, we can use different trade management approaches. In this case, we use the ATR (Average True Range) to set the stop-loss and take-profit levels. The take-profit level is set based on the stop-loss distance, with a take-profit to stop-loss ratio of 2.5. The stop-loss is calculated as 0.8 times the ATR.

In the backtest, we iterate through each candle in the data frame and check for entry and exit conditions based on the signal generated by the V-WAP and moving average indicators. If a trade is initiated, we calculate the stop-loss and take-profit levels based on the ATR. If the price reaches either the stop-loss or take-profit level, we exit the trade and record the profit or loss.

Once the backtest is complete, we calculate the cumulative profit and loss and generate performance metrics such as total return, annualized return, maximum drawdown, and Sharpe ratio.

The results of the backtest show the performance of the trading strategy using both the V-WAP and moving average indicators. By comparing the results, we can evaluate which indicator performs better in terms of profitability and risk management.

Please note that the backtest results will depend on various factors such as the specific parameters used, the selected time period, and market conditions. It's important to thoroughly analyze the strategy and conduct robust testing before applying it to real trading.

Moving Average And VWAP Trend Strategies Backtest In Python
Moving Average And VWAP Trend Strategies Backtest In Python
  • 2023.04.20
  • www.youtube.com
In this video, we compare VWAP and Moving Average indicators for trading and Algorithmic trading. We use both indicators to build a trading bot, backtesting ...
 

Master Price Action Trading with Automated Doji and Engulfing Patterns using Python


Master Price Action Trading with Automated Doji and Engulfing Patterns using Python

Today, we will compare two candle patterns: the Doji and the Engulfing candles. We will use a simple strategy combined with the Bollinger Bands and backtest these patterns using historical data from the past year on the one-hour timeframe. Our goal is to demonstrate how combining price action patterns with other indicators can be a powerful tool for pure technical trading.

To implement this strategy, we provide the Python code, which is available for download from the link in the video description. If you find the content helpful, please support our channel by liking and following it. Additionally, if you have any ideas you'd like to explore, we can discuss them in the comments section.

For the purpose of this demonstration, we will focus on two sets of patterns: a Doji candle preceded by a bearish candle and followed by an uptrend or bullish candle, indicating a future uptrend. The second pattern is the Engulfing pattern, where the open and close prices of a candle are higher and lower, respectively, than those of the previous candle. Essentially, the current candle engulfs the previous one, determining the forecasted trend for future prices. We will specifically look at examples of bullish setups for these two patterns. It's also worth considering the bearish setups, where a Doji is followed by a bearish candle, or in the case of the Engulfing pattern, the engulfing candle has a bearish direction. These setups would indicate a future downtrend.

Now, let's delve into how we use these patterns in our strategy. First, we wait for a price candle to close above or below the Bollinger Band lines. If a bullish pattern forms below the lower Bollinger Band line, we set a buying signal. On the other hand, if a bearish candle setup occurs above the upper Bollinger Band line, we set a short or selling signal. In simpler terms, if we observe a bullish pattern below the Bollinger Bands, we expect the price to rise. Conversely, if we spot a bearish pattern above the Bollinger Bands, we anticipate the price to decline. This is the aspect we will explore using Python.

The provided Jupyter notebook demonstrates the backtesting of this indicator. The code employs the Y Finance module and pandas for data retrieval and analysis, respectively. The Euro to US dollar exchange rate data from April 1, 2021, to March 19, 2023, is downloaded on an hourly timeframe.

To compute the Bollinger Bands, we utilize the pandas technical analysis module, setting a length of 30 and a standard deviation of 1.5. These parameters can be adjusted for further experimentation.

Next, we define the function "Bollinger Doji Signal," which takes the dataframe with open, close, high, and low prices, as well as the Bollinger Bands data. We check for the first condition, which entails a bullish signal. In this case, we look for a closing price of the current candle below the lower Bollinger Band line. Simultaneously, the current closing price should be greater than the opening price, indicating a green or uptrend candle preceded by a Doji candle. Additionally, the preceding candle should be bearish, with the closing price lower than the opening price. This combination represents a reversal pattern. If these conditions are met and the pattern occurs below the lower Bollinger Band line, we set a buying signal. Conversely, for a bearish signal, the closing price should be above the upper Bollinger Band line, and the current candle should be bearish, with the closing price below the opening price. Preceding this, there should be an uptrend or green candle with the closing price higher than the opening price.

In this case, the combination represents a bearish reversal pattern, and if these conditions are met and the pattern occurs above the upper Bollinger Band line, we set a selling signal.

The function "Bollinger Doji Signal" calculates the signals based on the conditions mentioned above and returns a dataframe with the original data along with the signals column indicating whether to buy (1), sell (-1), or hold (0) at each candle.

We then apply this function to our historical data and store the results in a new dataframe called "signals_df".

To evaluate the performance of our strategy, we calculate the returns by multiplying the signals with the percentage change in the closing price and summing them cumulatively. We also calculate the cumulative returns of a buy-and-hold strategy, where we simply hold the asset for the entire period.

Finally, we plot the cumulative returns of both strategies on a graph to visualize the performance.

By analyzing the backtest results, we can assess the effectiveness of the Doji and Engulfing candle patterns combined with the Bollinger Bands indicator in generating trading signals. It's important to note that this is a simplified example and should not be considered as financial advice. Additionally, the strategy and its parameters can be further optimized and customized based on individual preferences and risk tolerance.

To fully understand and replicate the code, it's recommended to download and run the provided Jupyter notebook, which contains the complete code and explanations.

Remember that trading strategies should be thoroughly tested and validated before implementing them in real trading scenarios. It's always recommended to consult with a financial advisor or conduct extensive research before making any investment decisions.

We hope this explanation helps you better understand the implementation of the Doji and Engulfing candle patterns combined with the Bollinger Bands indicator in a trading strategy.
Master Price Action Trading with Automated Doji and Engulfing Patterns using Python
Master Price Action Trading with Automated Doji and Engulfing Patterns using Python
  • 2023.03.25
  • www.youtube.com
In this informative video, we will explore the power of price action patterns in combination with Bollinger Bands for trading strategy backtesting. Specifica...
 

Strategy BackTest Of Price Channel Break Out In Python



Strategy BackTest Of Price Channel Break Out In Python

Hello and welcome back to my channel. In today's video, we'll be focusing on developing and backtesting a strategy based on price channel breakouts. If you're new here, I recommend watching our previous episode where I introduced and explained the code for our custom Python-based price channel indicator. I'll leave a link in the description below. Now, let's dive into today's topic and carry out the backtesting part.

In the previous video, we presented the code to detect price channels. To recap, we detect the fractals, which are the high and low points of the candles. A fractal is a point on the candle that is higher or lower than all the neighboring candles at the same time. We detect different fractals, both highs and lows, and fit them into slopes using regression. These slopes represent the channel that defines the price channel. We then try to detect breakouts when the price goes out of this channel, assuming that the price will continue in the same direction.

Visually, the indicator seems to be working fine, but we're curious to see how it performs on a long-term strategy. That's why we're conducting the full backtesting today. We'll use Python for the backtest, and you can download the code from the link in the description.

To start, we load our data, taking 10 to 20 years' worth of data from 2003 to 2023, which is what we used in the last video as well. We set the number of back candles to 45, which is the window over which we'll detect the fractals (highs and lows) and define our channel.

We have a function called is_pivot that detects if a candle is a pivot point or a fractal point. It takes two parameters: the candle index and the window size. It checks if the candle is higher or lower than all the highs or lows of the neighboring candles within the specified window. It returns a value based on the type of pivot: 1 for a pivot high, 2 for a pivot low, 3 for both, and 0 for other cases. We apply this function to each candle in our data frame and save the results in a new column called "is_pivot."

Next, we need to collect the channels or detect the channels. We look at the 45 candles before the current candle and check for high and low fractals. If we have at least three highs and three lows, we fit them using linear regression and return the parameters of the slopes. Otherwise, we return zeros. We apply this function to the data frame using the specified window size, and the results are saved in a new column called "channel."

We also have a function called is_breakout that detects breakouts outside of the price channel. For each candle, we check if the previous candle's high is within the channel and if the previous candle's closing price is below the channel's low. If both conditions are met, and the current candle's open and closing prices are also outside the channel, we return a signal: 1 for a downtrend signal, 2 for an uptrend signal, and 0 for other cases. We run this function over all the candles and save the results in a new column called "is_breakout."

Now that we have the breakout signals, we can proceed with the backtesting. We use the Backtesting Library as usual, with an initial lot size of 10% of the equity. We define a take profit to stop loss ratio and set the stop loss and take profit levels based on the signal. For example, if the signal is 2 (uptrend), and no trades are open, we set the stop loss at the low of the previous candle and the take profit at a multiple of the average true range (ATR).

We iterate over each candle in the data frame and check if there is a breakout signal. If there is a signal and no trades are currently open, we enter a new trade based on the signal. We calculate the trade size based on the lot size and the available equity.

Once a trade is open, we track its progress. We update the stop loss and take profit levels as the price moves in our favor. If the price hits the stop loss or take profit level, we close the trade and record the result.

Finally, we calculate and print the overall performance metrics, including the total number of trades, the percentage of winning trades, the average return per trade, and the overall return on equity.

Here is a step-by-step explanation of the price channel breakout strategy without the code:

  1. Load the historical price data.
  2. Define the parameters for the strategy, such as the period for calculating the average true range (ATR) and the multiple for setting the take profit level.
  3. Iterate over each candle in the data frame.
  4. Check if there is a breakout signal. A breakout occurs when the price exceeds the upper or lower channel boundary.
  5. If there is a breakout signal and no trades are currently open, enter a new trade based on the signal. For a bullish breakout, buy the asset, and for a bearish breakout, sell the asset.
  6. Calculate the trade size based on the available equity. For example, you may set the trade size as a percentage of the equity, such as 10%.
  7. Set the stop loss level based on the previous channel boundary. For a bullish breakout, the stop loss is set below the lower channel boundary, and for a bearish breakout, the stop loss is set above the upper channel boundary.
  8. Set the take profit level based on the current price and the ATR. Multiply the ATR by a multiple, such as 2, and add it to the current price for a bullish breakout or subtract it from the current price for a bearish breakout.
  9. Track the progress of the trade. Update the stop loss and take profit levels as the price moves in your favor. This may involve adjusting the stop loss to lock in profits or trailing the stop loss behind the price.
  10. If the price hits the stop loss or take profit level, close the trade and record the result (profit or loss).
  11. Repeat steps 5 to 10 for each breakout signal and update the trade size, stop loss, and take profit levels accordingly.
  12. Calculate and print the overall performance metrics, such as the total number of trades, the percentage of winning trades, the average return per trade, and the overall return on equity.

Implementing this strategy in code will allow you to backtest it on historical data and analyze its performance. The code provided earlier demonstrates the implementation of the strategy using the Backtesting library in Python.

Strategy BackTest Of Price Channel Break Out In Python
Strategy BackTest Of Price Channel Break Out In Python
  • 2023.03.10
  • www.youtube.com
This is a continuation of the previously released video (https://youtu.be/Bnv7euL-FxM) on price channels break out. This is a full strategy backtest sample s...