You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
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.
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.
|
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!
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.
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 #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.
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:
Calculate the price change for each candle. This is the difference between the closing price of the current candle and the previous candle.
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.
Smooth the gain and loss series by replacing negative values with zeros and positive values with their absolute values.
Calculate the average gain and average loss over the defined window size. This is usually done using the simple moving average (SMA) method.
Calculate the relative strength (RS) by dividing the average gain by the average loss.
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.
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.
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.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:
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.