Browse Source

je sais plus

pull/14/head
thibaudlabat 8 months ago
parent
commit
eda640865e
Signed by: number42 GPG Key ID: 2CD7D563712B3A50
  1. 2
      .gitignore
  2. 15
      auto_trading/main.py
  3. 5
      auto_trading/strat/all_in.py
  4. 19
      main.py

2
.gitignore

@ -225,3 +225,5 @@ fabric.properties
# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser
# JetBrains Tools
.idea/

15
auto_trading/main.py

@ -4,7 +4,6 @@
"""
import pandas as pd
from .interfaces import Portfolio, Strategy, Broker, Predictor
@ -17,6 +16,8 @@ class Bot:
self.strategy = strategy
self.broker = broker
self.predictor = predictor
self._current_conversion_rate = None
self._balance = 0 # USD$
def run(self):
"""run the bot"""
@ -26,10 +27,16 @@ class Bot:
def run_once(self):
"""run the bot once"""
data = self.broker.next()
current_conversion_rate = data.iloc[-1].fillna(0).to_dict()
self.strategy.run(self.ptf, self.predictor.predict(data), current_conversion_rate)
self._current_conversion_rate = data.iloc[-1].fillna(0).to_dict()
self.strategy.run(self.ptf, self.predictor.predict(data), self._current_conversion_rate)
def print_results(self):
"""print the results"""
for k,v in self.ptf.content().items():
for k, v in self.ptf.content().items():
print(f"{k}: {v:e}")
def balance(self):
money = 0
for k, v in self.ptf.content().items():
money += self._current_conversion_rate[k] * v
return money

5
auto_trading/strat/all_in.py

@ -1,4 +1,5 @@
from ..interfaces import Strategy, Portfolio
import numpy as np
class AllIn(Strategy):
"""Just all in the greatest result"""
@ -17,7 +18,9 @@ class AllIn(Strategy):
ptf.widraw(amount, stock)
# after get the greatest result
greatest = max(result, key=lambda k: result[k])
result = {k:-1 if np.isnan(v) else v for k,v in result.items() }
epsilon = 1e-6
greatest = max(result, key=lambda k: result[k]-(np.inf if current_conversion_rate[k]<epsilon else 0))
# then buy all the greatest result
to_add = money/current_conversion_rate[greatest]

19
main.py

@ -5,13 +5,14 @@ from auto_trading.predictor.mean_agg import MeanAggregator
from auto_trading.predictor.normalized import NormalizedPredictor
from auto_trading.predictor.selector import SelectorPredictor
from auto_trading.main import Bot
import matplotlib.pyplot as plt
if __name__ == '__main__':
csv = "data/price_history.csv"
with open(csv, 'r') as f:
head = f.readline().replace("\n", "").split(",")[1:]
pred = MeanAggregator([NormalizedPredictor(), SelectorPredictor({"Tether": 0.7})])
pred = MeanAggregator([NormalizedPredictor(),SelectorPredictor({"Tether": 0.7})])
bot = Bot(
ptf=InMemoryPortfolio({k:1 for k in head}),
@ -22,8 +23,18 @@ if __name__ == '__main__':
bot.print_results()
print("\n")
for _ in range(3):
TIME = range(400)
B = []
for _ in TIME:
bot.run_once()
bot.print_results()
#bot.print_results()
assets = bot.ptf.content()
spread=1/100
for k in assets.keys():
assets[k]=(1-spread)*assets[k]
B.append( bot.balance() )
print(B[-1])
print("\n")
plt.plot(TIME,B)
plt.show()

Loading…
Cancel
Save