eranelbaz
Posted on January 21, 2020
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 currentStockValue
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 January 21, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.