top of page

Top 5 Ways to Make Money in Financial Markets Using Python


Author: Lorenzo Filotei


Over the last years, Python has become very useful in specific fields such as finance. Python is a programming language that is based on data. It aims to ease data-driven building strategies and make decisions. Python allows programmers to evaluate and confront data to reach a solution finalized for a variety of financial departments. In this article, some of its applications are discussed.

Among them are: ‘Algorithmic Trading’, ‘Quantitative Investing’, ‘Options Trading with Python’, ‘Portfolio Optimization’, ‘Crypto and DeFi Strategies’


Algorithmic Trading

This is a procedure that bases its functioning on instructions furnished to a programming software. Once the program receives these instructions, it will operate following them accurately. A quick example could be the following:


‘Buy 10 shares of this stock X if the 50-day MA goes over the 100-day MA.’


So if the first value overtakes the second one, the software will immediately place the buying order.


To create a reliable algorithmic trading strategy, developers can utilize Python libraries, like Pandas and NumPy. These libraries are well known for their efficiency and performance regards data analysis. Pandas is fundamental to importing historical price data, backtesting strategies, and calculating technical indicators. On the other hand, NumPy is more maths-based, strongly utilized for portfolio optimization and risk modelling. 


Python code snippet calculating moving averages and trading signals using pandas and numpy. Syntax highlighting shows comments and functions.

 In this example, the aforementioned libraries have been imported. The example represents a basic ‘Crossover Strategy’. Once the price data and time series were created, the command was used to calculate moving averages for identifying trends. Then, as we know, if the short moving average > long moving average (buy signal), Python will print 1; if it’s the opposite, it will print -1 (sell signal). 




Quantitative Investing

This method of investing has historically been chased by institutional investors, such as hedge funds. The usage of data-driven analysis allowed investors to be more aware of the systemic risk in the financial markets. Nowadays, with the integration of AI and machine learning, this strategy has become widely utilized, given its high efficiency and increasing reliability. There is a variety of quantitative investment strategies. Some of them include the following:

  • Statistical Arbitrage: through statistical modelling, aims to identify market inefficiencies to guarantee a certain profit. 

  • Machine Learning: utilizes algorithms for going through financial and technical data to create efficient and accurate financial models.

  • Artificial Intelligence: exploits machine learning for simulating human-like decisions, making them more complex and weighted. 


    Python code snippet on a black background for downloading stock data, loading Fama-French factors, converting percentages, and running regression.

This Python code builds a factor model of stock returns using historical data. It utilizes Apple’s price data, calculates daily returns, and compares them with three market factors from the Fama-French model



Options Trading with Python

An option is a contract that gives the holder the right to buy or sell a specific financial instrument at a pre-determined price for a certain period. This type of contract can be created for a variety of products, including stocks, bonds, and commodities. Overall, it can be defined as one of the asset classes in the financial markets. 

What’s an option and why do investors trade them?

The contract is based on three key points:

  • Strike price: is the price at which an option can be exercised.

  • Option premium: is the amount of money that the holder pays to the option seller for acquiring the right.

  • Expiration date: is the date at which the right to exercise the option expires and, from then the contract is worthless. 

Trading options is not for beginners. Indeed, it’s a contract often used for speculation, hedging, and getting leverage. The 4 basic operations that an investor can do with this type of contract are:


  1. Buy calls (long) = buying the right to buy

  2. Sell calls (short) = buying the right to sell

  3. Buy puts (long) = selling the right to buy

  4. Sell puts (short) = selling the right to sell


To make it clear, buying(selling) a stock means taking a long(short) position, while buying a call(put) option signifies a potential long(short) position in the underlying asset, giving the right to potentially take a long(short) position in it. 


Investors who buy options are named ‘holders’, and those who sell options are ‘writers’. Briefly, a holder is a buyer and is not obliged to buy or sell the underlying asset, while a writer, being a seller, is obligated to buy or sell if the option expires in the money. Therefore, writers are more risk-exposed because they can lose much more than the price of the options premium. 


A table compares call and put options for hedging against price changes. Text details uses by importers, exporters, ADRs, and short sellers.
Source: Investopedia - Usage samples of option trading

Portfolio Optimization

Portfolio optimization is simply finding the best way you can invest your money in different assets like stocks or ETFs to equate risk and return. Python makes it easy since it has great finance and data analysis libraries. With Pandas and NumPy, it is easy to load and manipulate financial data. Then, with the help of tools like PyPortfolioOpt, you can approximate expected returns, risk, and find the portfolio of investments that gives you the best result, like the best Sharpe ratio or minimum volatility. You can also specify rules like not investing too much in a particular stock or short-selling. Python lets you plot charts, like the efficient frontier, to observe how different portfolios would act. What is wonderful is that even more advanced models, like Black-Litterman or Hierarchical Risk Parity, are available. With Python, portfolio optimization is easy and within everyone's grasp for their professional purposes.


Python code for optimizing a stock portfolio using libraries like yfinance and PyPortfolioOpt. Comments guide each process step. Multicolored text.

This code shows an example of how to optimize a portfolio by using the Efficient Frontier model. First and foremost, traders will need to import libraries to have all the necessary tools. The next step will be downloading stock data (the stock’s prices over a specific period). Once data is obtained, it’s time for calculus to create the base for the Efficient Frontier model. The result is a set of optimal portfolio weights for each stock. By using Python, it will be possible to print the portfolio performance and the optimal allocation (weights for each stock).


Crypto and DeFi Strategies

DeFi, or decentralized finance, is reshaping how people interact with money by enabling peer-to-peer financial services built on smart-contract blockchains like Ethereum, Solana, and Cardano. Unlike traditional finance, DeFi removes intermediaries and lets anyone access services globally with just an internet connection.If you’re curious about investing in DeFi, NAGA highlights four main strategies, tailored to different risk profiles and goals:• HODL: hold and purchase cryptocurrencies such as ETH or DOT long-term.• Borrow and lend: lend and borrow assets and earn interest by offering liquidity or serving as collateral.• Staking and yield farming: lock up tokens to stake on networks or pools of liquidity and earn rewards.• DeFi stocks and indexes: invest indirectly through equities or ETFs against DeFi projectsBoth approaches have pros and cons—HODLing is straightforward but exposes you to price risk, while lending or staking can earn you passive income but is exposed to risks from smart contracts and the protocol. DeFi holds massive promise, but careful research and risk awareness are still top priorities.


Python code for a DeFi strategy simulation, showing ETH and USDC strategies. Includes matplotlib plots for HODL and LP value over 100 days.

In this code, a simple DeFi-inspired strategy is shown using Python. A ‘liquidity provision strategy’ was simulated and compared to simply ‘holding tokens’. This is not connected to a real blockchain, but it demonstrates how you might model returns.


Conclusions

The variety of tools and strategies that traders can use to make market operations and build a strategy has been heavily increased over the last years. From Python libraries to AI simulations, traders discovered a new way of creating opportunities and exploiting at the maximized profit/risk ratio. All these kinds of strategies are meant for expert traders, and any beginner in investing should master their skills first, and then try to start operating in the market. 



You can also read about:




References

Comments


  • White Facebook Icon
  • White Twitter Icon
  • White Instagram Icon
bottom of page