How to avoid common mistakes during trading - Loss Avoidance


  • Article Difficulty: ★☆☆☆☆
  • Using Loss Avoidance for Automated Trading Strategies
  • Studying the Effectiveness of Loss Avoidance using Listed Stock Price Data


“Loss avoidance” is a crucial topic in investing, whether for novice investors or experienced experts. As we pursue investment returns, the risk of losses is ever-present. Therefore, adopting effective loss avoidance strategies is vital to protect our capital and enhance the chances of investment success.
The effectiveness of loss avoidance goes beyond reducing potential losses; it can also improve the overall performance of investment portfolios. 

By identifying and avoiding investment opportunities that may lead to significant losses, we can prevent overexposure to high-risk assets or markets. Through asset diversification and risk management strategies, we can maintain relatively stable investment returns during market fluctuations and mitigate the impact of losses.

Effective loss avoidance strategies can also help us manage emotions and psychological factors in investments. Market volatility and uncertainty often trigger panic and impulsive decision-making, leading investors to act irrationally. However, with clear rules and strategies, we can remain calm and rational in response to market changes, thereby reducing losses stemming from emotionally driven investment decisions.

In this article, we will use Python and the tejapi to fetch stock price data to examine the differences between implementing loss avoidance and not taking any loss avoidance measures. By understanding and applying loss avoidance, we will be better equipped to protect our investments, reduce potential losses, and enhance long-term returns.

Editing Environment and Module Requirements

This article uses a Mac operating system and Visual studio code as the editor.

import tejapi
import matplotlib.pyplot as plt
tejapi.ApiConfig.api_key = "your api key"
tejapi.ApiConfig.ignoretz = True


Securities Trading Data Table (TWN/EWPRCD) – Closing Price Data

Data Import

The data period for the example will be from June 12, 2023, to July 12, 2023, using the stock price of Silks Hotel Management Consulting Co., Ltd. (2739). We will fetch the unadjusted closing prices.

# Select 2739 recent stock prices

company = '2739'
price_data = tejapi.get('TWN/EWPRCD', 
                coid = company,
                    # start date  
                    # end date
                opts={'columns': ['coid','mdate','close_d']}, 

After obtaining the closing price data, we can proceed with visualizing the data.

date = []
for d in price_data['mdate']:

plt.figure(figsize=(11, 6))
plt.plot(date, price_data['close_d'], label='stock price', color='blue') 
plt.title('stock price')  # set figure title
plt.xlabel('Days')  # set X-axis label
plt.ylabel('price')  # Set Y-axis label
Stock price trend

We can observe that the stock price experienced a small increase during this period, followed by a continuous decline. This situation is suitable for conducting a loss avoidance test.

Performance Calculation

get_day1_price = price_data['close_d'][0]
print('purchase price:',get_day1_price)
no_strategy = 0
if_strategy = 0

for i in range(0, len(price_data['close_d'])):
    no_strategy = no_strategy + price_data['close_d'][i]-get_day1_price
    # If the price decrease more than 10 percent of original price , it will be sold.
    if price_data['close_d'][i] < get_day1_price*0.9:
        if_strategy += 0
        if_strategy = if_strategy + price_data['close_d'][i]-get_day1_price

print('No-loss-avoidance operation settlement surplus:',no_strategy)
print('circumvention operation to settle the surplus:',if_strategy)
performance comparison

After calculation, it was found that implementing the loss avoidance strategy resulted in an additional profit of NT$30 per share compared to investors who continued to hold the stocks. Therefore, it is considered the best strategy to have discipline in selling when the stock price falls below the predetermined value.


This implementation demonstrated a trading strategy focused on loss avoidance. Using code, we observed that implementing sell orders under specific stock price conditions resulted in higher profits than holding onto the stocks.

However, it is essential to note that each investor’s situation and risk tolerance differ. This example represents one possible strategy and does not guarantee profits in all situations. In actual investing, we must carefully consider our goals, risk preferences, and timeframes and adopt an investment strategy that suits our needs.

Furthermore, this implementation serves as a reminder to be mindful of market risks and uncertainties. Various factors influence stock prices, including market sentiment, economic changes, company performance, etc. Regardless of our investment strategy, we cannot eliminate risk entirely. Therefore, exercising caution and establishing risk management mechanisms to navigate unforeseen market fluctuations is crucial.

While the code in this implementation provides faster and more accurate analysis results, we should also handle data carefully and ensure that the information and indicators used are reliable and effective. Proper data analysis and research form the foundation for making informed investment decisions.

In summary, loss avoidance is an essential aspect of investing, and leveraging automated trading can help us achieve this goal. However, we must select appropriate strategies based on our circumstances and market conditions while managing risk prudently. This approach will lead to more robust outcomes in our investments.

Source code

Extended reading

Related link