04. Add Targets and Stops

As an investor, to maximize your profits, you need to get your pricing right – both when it comes to buying and selling. However, sometimes, prices fluctuate more than expected and it can become a little difficult to gauge whether to trade now or wait a little more. This is where price targets and stop-loss measures help, which let you know how long you should hold a stock.

The target price indicates the price at which you want to settle / square off your position, buy or sell. So, once the share price touches the target, you may look to sell it and pocket your profits. The calculation of the best target price involves a deeper analysis of the instrument’s historical costs as well as possible price influencers expected to act on the price in the future.

A stop-loss price acts as a target on the lower end. It lets you know when to sell before the stock falls further and worsens your loss. It is designed to minimize the investor's loss. For example, when entering into a trade, you can direct your broker to sell the position if the price moves 15% below your entry price. Doing so ensures that you can only lose 15% of your investments.

Let's look at the code example on how to add targets :

   def process_candle(self):
        if <entry_criteria>:
            self.last_trade_price = self.current['close']
            # capture last traded price
            
            self.buy(info="Enter Buy", quantity=1)

        if self.get_position()>0:
            percent = 100 * (self.current['close'] - self.last_trade_price) / self.last_trade_price
            #calculate a percentage exit based on entry
            
            #if <target_percentage_criteria>:
            # exit on a 5% target profit
            if percent > 5:
                self.close(info="Exit Buy", quantity=1)

Last updated