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
The Idea was simple one, one class to represent my
broker, multiple classes of
strategies and one class which is the
I made another class I called
StockValue that represent the value of a stock in a given time
@dataclass class StockValue: price: float name: str time: datetime = datetime.now()
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
Trueto sell command
- If the tangent changed from negative to positive return
Trueto 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
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()
getwill return you the current
StockValueof the stock
buywill give command to buy and will return how much it cost
sellwill 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
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
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
Top comments (3)
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.
I'll check this blog post