{"id":212190,"date":"2024-09-24T13:59:46","date_gmt":"2024-09-24T17:59:46","guid":{"rendered":"https:\/\/ibkrcampus.com\/campus\/?p=212190"},"modified":"2024-09-24T14:01:53","modified_gmt":"2024-09-24T18:01:53","slug":"basic-trading-algorithms-in-python","status":"publish","type":"post","link":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/basic-trading-algorithms-in-python\/","title":{"rendered":"Basic Trading Algorithms in Python"},"content":{"rendered":"\n<p><em>The article &#8220;Basic Trading Algorithms in Python&#8221; was originally posted on <a href=\"https:\/\/www.pyquantnews.com\/free-python-resources\/basic-trading-algorithms-in-python\">PyQuant News<\/a>.<\/em><\/p>\n\n\n\n<p>The rise of algorithmic trading has transformed financial markets, allowing traders to make precise, data-driven decisions. Python, with its powerful libraries like pandas and NumPy, is a top choice for developing trading algorithms. This article will guide you through creating basic trading algorithms in Python, making use of these libraries.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-introduction-to-algorithmic-trading\">Introduction to Algorithmic Trading<\/h3>\n\n\n\n<p>Algorithmic trading uses computer programs to perform trades at speeds and frequencies beyond human capability. These algorithms analyze market data, spot trading opportunities, and execute orders based on predefined criteria. The key benefits include:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Speed<\/strong>: Orders are executed in milliseconds.<\/li>\n\n\n\n<li><strong>Accuracy<\/strong>: Reduces human error.<\/li>\n\n\n\n<li><strong>Consistency<\/strong>: Operates continuously without fatigue.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Why Use Python for Trading Algorithms?<\/h3>\n\n\n\n<p>Python&#8217;s simplicity and versatility make it ideal for trading algorithms. This article focuses on two primary libraries:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>pandas<\/strong>: For data manipulation and analysis, providing structures and functions to work with structured data effortlessly.<\/li>\n\n\n\n<li><strong>NumPy<\/strong>: For numerical computing, handling large, multi-dimensional arrays and matrices along with a suite of mathematical functions.<\/li>\n<\/ul>\n\n\n\n<p>Other useful Python libraries include:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>scikit-learn<\/strong>: For machine learning.<\/li>\n\n\n\n<li><strong>matplotlib<\/strong>: For data visualization.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Setting Up the Environment<\/h3>\n\n\n\n<p>Before we begin coding, ensure you have Python along with the necessary libraries installed. Install pandas and NumPy using pip:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">pip install pandas\npip install numpy<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Data Acquisition and Preparation<\/h3>\n\n\n\n<p>The first step in developing trading algorithms is acquiring and preparing the data. We&#8217;ll use historical stock price data from Yahoo Finance, fetched using the&nbsp;<code>pandas_datareader<\/code>&nbsp;library.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import pandas_datareader.data as web\nimport datetime\n\n# Define the start and end date\nstart = datetime.datetime(2020, 1, 1)\nend = datetime.datetime(2022, 1, 1)\n\n# Fetch data for a particular stock\nstock_data = web.DataReader('AAPL', 'yahoo', start, end)\nprint(stock_data.head())<\/pre>\n\n\n\n<p>This data includes columns like &#8216;High&#8217;, &#8216;Low&#8217;, &#8216;Open&#8217;, &#8216;Close&#8217;, &#8216;Volume&#8217;, and &#8216;Adj Close&#8217;. We will focus on &#8216;Adj Close&#8217; prices.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Calculating Technical Indicators<\/h3>\n\n\n\n<p>Technical indicators are mathematical calculations based on historical price, volume, or open interest information, and they help predict future price movements. We will calculate two simple indicators: Moving Average (MA) and Relative Strength Index (RSI).<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Moving Average (MA)<\/h4>\n\n\n\n<p>A moving average smooths out price data by creating a constantly updated average price. The most common types are the Simple Moving Average (SMA) and the Exponential Moving Average (EMA).<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Simple Moving Average (SMA)\nstock_data['SMA_20'] = stock_data['Adj Close'].rolling(window=20).mean()\n\n# Exponential Moving Average (EMA)\nstock_data['EMA_20'] = stock_data['Adj Close'].ewm(span=20, adjust=False).mean()<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Relative Strength Index (RSI)<\/h4>\n\n\n\n<p>The RSI measures the speed and change of price movements, oscillating between 0 and 100, typically identifying overbought or oversold conditions.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">def calculate_rsi(data, window):\n   delta = data['Adj Close'].diff(1)\n   gain = (delta.where(delta &gt; 0, 0)).rolling(window=window).mean()\n   loss = (-delta.where(delta &lt; 0, 0)).rolling(window=window).mean()\n   rs = gain \/ loss\n   rsi = 100 - (100 \/ (1 + rs))\n   return rsi\n\nstock_data['RSI_14'] = calculate_rsi(stock_data, 14)<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Developing a Basic Trading Algorithm<\/h3>\n\n\n\n<p>With our indicators in place, we can develop a simple trading algorithm. Let&#8217;s create a basic strategy that buys a stock when its 20-day SMA is above the 50-day SMA and sells when it&#8217;s below.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-define-the-strategy\">Define the Strategy<br><\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Calculate longer-term SMA\nstock_data['SMA_50'] = stock_data['Adj Close'].rolling(window=50).mean()\n\n# Create buy\/sell signals\nstock_data['Signal'] = 0\nstock_data['Signal'][20:] = np.where(stock_data['SMA_20'][20:] &gt; stock_data['SMA_50'][20:], 1, 0)\nstock_data['Position'] = stock_data['Signal'].diff()<\/pre>\n\n\n\n<p>In this code:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We calculate the 50-day SMA.<\/li>\n\n\n\n<li>We generate buy\/sell signals where a &#8216;1&#8217; signifies a buy signal and &#8216;0&#8217; signifies a sell signal.<\/li>\n\n\n\n<li>The&nbsp;<code>Position<\/code>&nbsp;column indicates changes in position based on the difference between consecutive signals.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Backtesting the Strategy<\/h3>\n\n\n\n<p>Backtesting involves testing the trading strategy on historical data to evaluate its performance.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">initial_capital = 100000.0\nstock_data['Holdings'] = stock_data['Adj Close'] * stock_data['Position'].cumsum()\nstock_data['Cash'] = initial_capital - (stock_data['Adj Close'] * stock_data['Position']).cumsum()\nstock_data['Total'] = stock_data['Cash'] + stock_data['Holdings']\n\n# Calculate returns\nstock_data['Returns'] = stock_data['Total'].pct_change()\n\n# Print final portfolio value\nprint(\"Final Portfolio Value: ${}\".format(stock_data['Total'].iloc[-1]))<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Analyzing the Results<\/h3>\n\n\n\n<p>Analyzing the performance involves evaluating metrics such as cumulative returns, average returns, and volatility.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">cumulative_returns = (stock_data['Total'].iloc[-1] - initial_capital) \/ initial_capital\naverage_daily_returns = stock_data['Returns'].mean()\nvolatility = stock_data['Returns'].std()\n\nprint(\"Cumulative Returns: {:.2f}%\".format(cumulative_returns * 100))\nprint(\"Average Daily Returns: {:.4f}\".format(average_daily_returns))\nprint(\"Volatility: {:.4f}\".format(volatility))<\/pre>\n\n\n\n<p>These metrics provide a snapshot of the strategy&#8217;s effectiveness and risk.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Further Learning<\/h3>\n\n\n\n<p>Developing trading algorithms is a continuous learning process. Here are some resources to deepen your understanding:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>&#8220;Python for Finance&#8221; by Yves Hilpisch<\/strong>: A comprehensive introduction to financial analytics and algorithmic trading using Python.<\/li>\n\n\n\n<li><strong>QuantConnect<\/strong>: An algorithmic trading platform offering resources, tutorials, and a community of quants. It supports Python and provides historical data for backtesting.<\/li>\n\n\n\n<li><strong>Kaggle<\/strong>: A platform for data science competitions offering datasets and notebooks related to financial markets and trading algorithms.<\/li>\n\n\n\n<li><strong>Investopedia<\/strong>: A valuable resource for understanding financial concepts, including technical indicators and trading strategies.<\/li>\n\n\n\n<li><strong>Coursera<\/strong>: Offers online courses on financial engineering, algorithmic trading, and Python programming for finance.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion<\/h3>\n\n\n\n<p>Developing basic trading algorithms using Python&#8217;s pandas and NumPy libraries is a powerful way to harness data for informed trading decisions. This article provided a foundational guide to acquiring data, calculating technical indicators, implementing a simple trading strategy, and evaluating its performance. As you continue to explore and refine your algorithms, remember that financial markets are complex and ever-changing. Continuous learning and adaptation are key to success in algorithmic trading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article will guide you through creating basic trading algorithms in Python, making use of these libraries.<\/p>\n","protected":false},"author":1518,"featured_media":161387,"comment_status":"open","ping_status":"closed","sticky":true,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[339,343,349,338,341],"tags":[851,4873,4659,17814,1225,1224,595,16878,4412,1545,2308],"contributors-categories":[17813],"class_list":{"0":"post-212190","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-science","8":"category-programing-languages","9":"category-python-development","10":"category-ibkr-quant-news","11":"category-quant-development","12":"tag-algo-trading","13":"tag-backtesting","14":"tag-matplotlib","15":"tag-moving-average-ma","16":"tag-numpy","17":"tag-pandas","18":"tag-python","19":"tag-relative-strength-index-rsi","20":"tag-scikit-learn","21":"tag-technical-indicators","22":"tag-trading-algorithms","23":"contributors-categories-pyquantnews"},"pp_statuses_selecting_workflow":false,"pp_workflow_action":"current","pp_status_selection":"publish","acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.9 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Basic Trading Algorithms in Python | IBKR Quant<\/title>\n<meta name=\"description\" content=\"This article will guide you through creating basic trading algorithms in Python, making use of these libraries.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.interactivebrokers.com\/campus\/wp-json\/wp\/v2\/posts\/212190\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Basic Trading Algorithms in Python\" \/>\n<meta property=\"og:description\" content=\"This article will guide you through creating basic trading algorithms in Python, making use of these libraries.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/basic-trading-algorithms-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"IBKR Campus US\" \/>\n<meta property=\"article:published_time\" content=\"2024-09-24T17:59:46+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-09-24T18:01:53+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2022\/10\/python-hand-blue-background.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1000\" \/>\n\t<meta property=\"og:image:height\" content=\"563\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Jason\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jason\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\n\t    \"@context\": \"https:\\\/\\\/schema.org\",\n\t    \"@graph\": [\n\t        {\n\t            \"@type\": \"NewsArticle\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/basic-trading-algorithms-in-python\\\/#article\",\n\t            \"isPartOf\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/basic-trading-algorithms-in-python\\\/\"\n\t            },\n\t            \"author\": {\n\t                \"name\": \"Jason\",\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/person\\\/41e9bacc875edb13ed6288f4ffb2afec\"\n\t            },\n\t            \"headline\": \"Basic Trading Algorithms in Python\",\n\t            \"datePublished\": \"2024-09-24T17:59:46+00:00\",\n\t            \"dateModified\": \"2024-09-24T18:01:53+00:00\",\n\t            \"mainEntityOfPage\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/basic-trading-algorithms-in-python\\\/\"\n\t            },\n\t            \"wordCount\": 666,\n\t            \"commentCount\": 1,\n\t            \"publisher\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/basic-trading-algorithms-in-python\\\/#primaryimage\"\n\t            },\n\t            \"thumbnailUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2022\\\/10\\\/python-hand-blue-background.jpg\",\n\t            \"keywords\": [\n\t                \"Algo Trading\",\n\t                \"backtesting\",\n\t                \"Matplotlib\",\n\t                \"Moving Average (MA)\",\n\t                \"NumPy\",\n\t                \"Pandas\",\n\t                \"Python\",\n\t                \"Relative Strength Index (RSI)\",\n\t                \"Scikit-learn\",\n\t                \"technical indicators\",\n\t                \"Trading Algorithms\"\n\t            ],\n\t            \"articleSection\": [\n\t                \"Data Science\",\n\t                \"Programming Languages\",\n\t                \"Python Development\",\n\t                \"Quant\",\n\t                \"Quant Development\"\n\t            ],\n\t            \"inLanguage\": \"en-US\",\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"CommentAction\",\n\t                    \"name\": \"Comment\",\n\t                    \"target\": [\n\t                        \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/basic-trading-algorithms-in-python\\\/#respond\"\n\t                    ]\n\t                }\n\t            ]\n\t        },\n\t        {\n\t            \"@type\": \"WebPage\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/basic-trading-algorithms-in-python\\\/\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/basic-trading-algorithms-in-python\\\/\",\n\t            \"name\": \"Basic Trading Algorithms in Python | IBKR Campus US\",\n\t            \"isPartOf\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#website\"\n\t            },\n\t            \"primaryImageOfPage\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/basic-trading-algorithms-in-python\\\/#primaryimage\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/basic-trading-algorithms-in-python\\\/#primaryimage\"\n\t            },\n\t            \"thumbnailUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2022\\\/10\\\/python-hand-blue-background.jpg\",\n\t            \"datePublished\": \"2024-09-24T17:59:46+00:00\",\n\t            \"dateModified\": \"2024-09-24T18:01:53+00:00\",\n\t            \"description\": \"This article will guide you through creating basic trading algorithms in Python, making use of these libraries.\",\n\t            \"inLanguage\": \"en-US\",\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"ReadAction\",\n\t                    \"target\": [\n\t                        \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/basic-trading-algorithms-in-python\\\/\"\n\t                    ]\n\t                }\n\t            ]\n\t        },\n\t        {\n\t            \"@type\": \"ImageObject\",\n\t            \"inLanguage\": \"en-US\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/basic-trading-algorithms-in-python\\\/#primaryimage\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2022\\\/10\\\/python-hand-blue-background.jpg\",\n\t            \"contentUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2022\\\/10\\\/python-hand-blue-background.jpg\",\n\t            \"width\": 1000,\n\t            \"height\": 563,\n\t            \"caption\": \"Quant Python\"\n\t        },\n\t        {\n\t            \"@type\": \"WebSite\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#website\",\n\t            \"url\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/\",\n\t            \"name\": \"IBKR Campus US\",\n\t            \"description\": \"Financial Education from Interactive Brokers\",\n\t            \"publisher\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\"\n\t            },\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"SearchAction\",\n\t                    \"target\": {\n\t                        \"@type\": \"EntryPoint\",\n\t                        \"urlTemplate\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/?s={search_term_string}\"\n\t                    },\n\t                    \"query-input\": {\n\t                        \"@type\": \"PropertyValueSpecification\",\n\t                        \"valueRequired\": true,\n\t                        \"valueName\": \"search_term_string\"\n\t                    }\n\t                }\n\t            ],\n\t            \"inLanguage\": \"en-US\"\n\t        },\n\t        {\n\t            \"@type\": \"Organization\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\",\n\t            \"name\": \"Interactive Brokers\",\n\t            \"alternateName\": \"IBKR\",\n\t            \"url\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/\",\n\t            \"logo\": {\n\t                \"@type\": \"ImageObject\",\n\t                \"inLanguage\": \"en-US\",\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/logo\\\/image\\\/\",\n\t                \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2024\\\/05\\\/ibkr-campus-logo.jpg\",\n\t                \"contentUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2024\\\/05\\\/ibkr-campus-logo.jpg\",\n\t                \"width\": 669,\n\t                \"height\": 669,\n\t                \"caption\": \"Interactive Brokers\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/logo\\\/image\\\/\"\n\t            },\n\t            \"publishingPrinciples\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/about-ibkr-campus\\\/\",\n\t            \"ethicsPolicy\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/cyber-security-notice\\\/\"\n\t        },\n\t        {\n\t            \"@type\": \"Person\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/person\\\/41e9bacc875edb13ed6288f4ffb2afec\",\n\t            \"name\": \"Jason\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/author\\\/jasonpyquantnews\\\/\"\n\t        }\n\t    ]\n\t}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Basic Trading Algorithms in Python | IBKR Quant","description":"This article will guide you through creating basic trading algorithms in Python, making use of these libraries.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.interactivebrokers.com\/campus\/wp-json\/wp\/v2\/posts\/212190\/","og_locale":"en_US","og_type":"article","og_title":"Basic Trading Algorithms in Python","og_description":"This article will guide you through creating basic trading algorithms in Python, making use of these libraries.","og_url":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/basic-trading-algorithms-in-python\/","og_site_name":"IBKR Campus US","article_published_time":"2024-09-24T17:59:46+00:00","article_modified_time":"2024-09-24T18:01:53+00:00","og_image":[{"width":1000,"height":563,"url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2022\/10\/python-hand-blue-background.jpg","type":"image\/jpeg"}],"author":"Jason","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Jason","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"NewsArticle","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/basic-trading-algorithms-in-python\/#article","isPartOf":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/basic-trading-algorithms-in-python\/"},"author":{"name":"Jason","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/person\/41e9bacc875edb13ed6288f4ffb2afec"},"headline":"Basic Trading Algorithms in Python","datePublished":"2024-09-24T17:59:46+00:00","dateModified":"2024-09-24T18:01:53+00:00","mainEntityOfPage":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/basic-trading-algorithms-in-python\/"},"wordCount":666,"commentCount":1,"publisher":{"@id":"https:\/\/ibkrcampus.com\/campus\/#organization"},"image":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/basic-trading-algorithms-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2022\/10\/python-hand-blue-background.jpg","keywords":["Algo Trading","backtesting","Matplotlib","Moving Average (MA)","NumPy","Pandas","Python","Relative Strength Index (RSI)","Scikit-learn","technical indicators","Trading Algorithms"],"articleSection":["Data Science","Programming Languages","Python Development","Quant","Quant Development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/basic-trading-algorithms-in-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/basic-trading-algorithms-in-python\/","url":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/basic-trading-algorithms-in-python\/","name":"Basic Trading Algorithms in Python | IBKR Campus US","isPartOf":{"@id":"https:\/\/ibkrcampus.com\/campus\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/basic-trading-algorithms-in-python\/#primaryimage"},"image":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/basic-trading-algorithms-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2022\/10\/python-hand-blue-background.jpg","datePublished":"2024-09-24T17:59:46+00:00","dateModified":"2024-09-24T18:01:53+00:00","description":"This article will guide you through creating basic trading algorithms in Python, making use of these libraries.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/basic-trading-algorithms-in-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/basic-trading-algorithms-in-python\/#primaryimage","url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2022\/10\/python-hand-blue-background.jpg","contentUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2022\/10\/python-hand-blue-background.jpg","width":1000,"height":563,"caption":"Quant Python"},{"@type":"WebSite","@id":"https:\/\/ibkrcampus.com\/campus\/#website","url":"https:\/\/ibkrcampus.com\/campus\/","name":"IBKR Campus US","description":"Financial Education from Interactive Brokers","publisher":{"@id":"https:\/\/ibkrcampus.com\/campus\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ibkrcampus.com\/campus\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/ibkrcampus.com\/campus\/#organization","name":"Interactive Brokers","alternateName":"IBKR","url":"https:\/\/ibkrcampus.com\/campus\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/logo\/image\/","url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/05\/ibkr-campus-logo.jpg","contentUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/05\/ibkr-campus-logo.jpg","width":669,"height":669,"caption":"Interactive Brokers"},"image":{"@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/logo\/image\/"},"publishingPrinciples":"https:\/\/www.interactivebrokers.com\/campus\/about-ibkr-campus\/","ethicsPolicy":"https:\/\/www.interactivebrokers.com\/campus\/cyber-security-notice\/"},{"@type":"Person","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/person\/41e9bacc875edb13ed6288f4ffb2afec","name":"Jason","url":"https:\/\/www.interactivebrokers.com\/campus\/author\/jasonpyquantnews\/"}]}},"jetpack_featured_media_url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2022\/10\/python-hand-blue-background.jpg","_links":{"self":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts\/212190","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/users\/1518"}],"replies":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/comments?post=212190"}],"version-history":[{"count":0,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts\/212190\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/media\/161387"}],"wp:attachment":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/media?parent=212190"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/categories?post=212190"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/tags?post=212190"},{"taxonomy":"contributors-categories","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/contributors-categories?post=212190"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}