loading...
Cover image for I Wrote Demo Trading Bot

I Wrote Demo Trading Bot

eranelbaz profile image eranelbaz Updated on ・3 min read

The idea was to write a simple Trading bot in python which buy when the stock is low and sell when high on daily basis, but soon I understood this is bigger than what I thought

Class Diagram

The Idea was simple one, one class to represent my broker, multiple classes of strategies and one class which is the bot
I made another class I called StockValue that represent the value of a stock in a given time

StockValue

@dataclass
class StockValue:
    price: float
    name: str
    time: datetime = datetime.now()

Strategy

class Strategy:
    def decide_buy(self, sold_price: StockValue, price_history: [StockValue]) -> bool:
        raise NotImplemented()

    def decide_sell(self, bought_price: StockValue, price_history: [StockValue]) -> bool:
        raise NotImplemented()

I wrote only one implementation which is:

  • calculate the tangents
  • If the tangent changed from positive to negative return True to sell command
  • If the tangent changed from negative to positive return True to buy command

Also taking into consideration the current hour, because if near the closing of the trade day I want to sell and not to keep the stocks
And a small timeout because I don't want to buy and sell a lot in order to minimize commands and thus minimize commissions

Broker

class StockBroker:
    def get(self, stock_name: str) -> StockValue:
        raise NotImplemented()

    def buy(self, stock: StockValue, count: int) -> float:
        raise NotImplemented()

    def sell(self, stock: StockValue, count: int) -> float:
        raise NotImplemented()

Very simple

  • get will return you the current StockValue of the stock
  • buy will give command to buy and will return how much it cost
  • sell will give command to sell and will return how much I earn

when I initiate my mock broker I give it which stocks I want to trade and it runs using a csv file of the stock value throughout the day

The problem

I took a CSV from yahoo finance and run my simple algorithm using this data and I theoretically "made" a lot of money,
and than I wanted to tell myself why don't I connect it to a real broker and actually start to make money?
And than I read some more, there are a lot of selling and buying commands, such as do you want that your command will be valid only until the end of the day or until you send a cancel command?
And in real life you trade in front of real people, and maybe there is a small change in the trend before a big change, for example:

if you will sell in the first bar and than buy again, you will lose money to commissions in contrary to leave the money in and sell the stocks when the value is actually high

Also the problem is that this bot is a passive bot,

Good investor will sell stock because the market offer an higher price, and you will never buy stock because it rises
The Intelligent Investor, Benjamin Graham

Summery

So I won't say it was a complete failure, because I learnt a lot about trends and the stock market, read a bit from Benjamin Graham, and I think I will leave this job to the real people, who know how to trade :)

Like this post?
Support me via Patreon
Subscribe to my YouTube Channel

Posted on by:

eranelbaz profile

eranelbaz

@eranelbaz

Diver, Photographer, Indie Game Developer and Big Data Geo Analytics Team Leader

Discussion

markdown guide
 

Hey Hey, following the path of trial and error with writing own trading bot, I would suggest the following blog post blog.trality.com/trading-bots-in-t...
It helped me a bit to have better understanding of trading situation during the current fluctuations due to the covid.
BTW, the platform trality.com itself is nice, I've started to use it recently an so far I'm very pleased. I code my bots in python there and run backtests rightaway. The only thing is that they don't have to many exchanges connected for live-trading and they switch from free beta to paid subscriptions soon.

 

Hey
I'll check this blog post
thanks