Backtesting is an important part of any strategy. An essential part of most strategies is having a Stop Loss and a Take Profit. In Python, there are a myriad of backtesting options available to you that allow for this but sometimes simple is better. Today, we’re going to look at how one can implement a Stop Loss and Take Profit using pandas before then using bt to run the full backtest.

Contents

  1. Overview of bt
  2. Using pandas to implement Stop Loss and Take Profits
  3. Setting weights for bt
  4. Running the backtest

1. Overview of bt


Happy New Year to everyone! Given the start of the New Year (and the beginning of my preparation for my Finals), I thought it would be apt to start a mini-series on… Demography! With all the confusion that’s been going on in the world in the last year, maybe it would be good to try to understand people better. Which, Demography (defined as “the study of human populations in relation to the changes brought about by the interplay of births, deaths, and migration” (Pressat, 1985)) prompt fulfills. This will likely be a 4 part series, looking at 2 demographic measures per part. Each part will look at the theoretical concepts behind the measure and then explore using Python to easily calculate it. …


especially useful when the names don’t match!

A few days ago, I was asked to help someone collate results from an online survey. The problem was, the online survey had been done in two parts, each with its own Google Form. The request was to perform a simple linear regression model with the response variable recorded in one of the Google Forms, and the explanatory variables recorded in the other Google Form. However, there was no unique identifier for each survey participant and the only thing that they had been told to input were their names. This was going to be fun! …


One of the most popular ways to value a company is using a model known as the Discounted Cash Flow Model. This model attempts to find the intrinsic value of a company by projecting its future cash flows and then discounting it. This is typically done using an excel spreadsheet and is relatively simple (just requiring a little bit of math and some manual work to find the financial information of a company). In this article, we’re going to explore how we can easily automate this in Python (both the calculation as well as the pulling of financial data!), …


In a R-approved tidy way

I learnt about tidytext awhile ago and thought it was such a neat framework. The essential idea behind it was that you could perform text mining/sentiment analysis using dplyr and "tidy" dataframes. So, I just had to try it out! I looked around for awhile to find things to perform this exploration on and finally settled on Singapore Prime Minister Lee's Covid-19 related speeches - thought it might be fun to see how that changed over time. As always, a tl;dr to get things going.

( a side note here: this was a rather basic and easy introduction to tidytext but it really is a much more powerful package and a lot more (which I plan to write on when I have more time) can be done with…


An example of how you can use the “Reply Keyboard” function in Python-Telegram-Bot to create a simple thematic “photo album”

Ever wanted to create a Telegram bot that would allow you to share your photos with the rest of the world? Not really? Well, after you’re done with this article, you’ll probably think differently. And if not… well, you’re just wrong.

In this article, we will look at using the Reply Keyboard in Telegram (pictured below) to create our very own bot that sends photos to users based on categories. …


What does a group of guys chat about?

A couple of days ago, one of my friends mentioned that it would be interesting to do a visualization of our group chat — to see things like who spoke the most, who spoke after who the most, and what were the words most commonly used. In this article, I show you how I did that using pandas, json, and plotly.

Post Outline

  1. Gathering Data
  2. Cleaning and Preparing Data
  3. Visualizing the Data

1. Gathering Data

Image for post
Image for post

The group that we were in is hosted on telegram and the data was easy to pull. As shown, you just had to use Telegram Desktop to export the files. You have the option of either exporting as a .html or .json but I would 100% recommend .json …


Many articles have been written on the ‘true’ cost of owning a car in Singapore (Here, here, and here). And these articles have done well in laying out roughly how much one should expect to pay for a car. But one aspect that these articles have not covered is the opportunity costs of owning a car.

As financially responsible people, we all know that we should (a) Save and (b) Invest the savings. So when we buy a car, this ability to save and invest seemingly goes down the drain… or does it? In this article, I want to show you (with the help of this nifty tool) that this does not always have to be the case. …


Not guaranteed to work all the time but hey it worked for me so I’m just sharing

A while ago, I was intrigued by the IBD 50 - a list of the top 50 stocks that have been screened by the CAN SLIM method and distributed by Investors’ Business Daily (IBD).

You would typically have to subscribe to the IBD in order to get access to the list but in 2015, innovator ETFs released an ETF (ticker: FFTY) that tracked the IBD50! And since the holdings of the ETF are available publicly, we can just extract the holdings of FFTY in order to know each week what holdings are recommended by the CAN SLIM method.

However, as I tried to extract the holdings data from zacks.com, I faced an issue in Selenium. And in this article, I describe the quick fix! …


What happens if you always invest just before a market drop?

I read an article recently about why a regular investment plan far outweighs trying to time the market. While that definitely made sense, I also wondered what happened if one were to constantly invest just before the market collapse. What if one was the worst investor in the world? There have been a few articles on this before but they gave rather skimpy details and I was curious to see it on my own and visualize it. Furthermore, I also wanted to explore if dollar-cost-averaging (DCA) (monthly) or DCA (yearly) would be more profitable. …

About

Zachary Lim

Hi! I’m learning to explore data and think about personal finance (not always in that order)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store