Skip to content

Commit

Permalink
✨ [alan-turing-institute#162]: implemented tracking transfers in and …
Browse files Browse the repository at this point in the history
…out into a new table
  • Loading branch information
Tdarnell committed Aug 13, 2023
1 parent 0caf526 commit c9b2cdc
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
18 changes: 18 additions & 0 deletions airsenal/framework/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,24 @@ class SessionBudget(Base):
id = Column(Integer, primary_key=True, autoincrement=True)
session_id = Column(String(100), nullable=False)
budget = Column(Integer, nullable=False)


class TransferPriceTracker(Base):
__tablename__ = "transfer_price_tracker"
id = Column(Integer, primary_key=True, autoincrement=True)
player_id = Column(Integer, ForeignKey("player.player_id"))
gameweek = Column(Integer, nullable=False)
season = Column(String(4), nullable=False)
timestamp = Column(String(100), nullable=False)
price = Column(Integer, nullable=False)
transfers_in = Column(Integer, nullable=False)
transfers_out = Column(Integer, nullable=False)

def __str__(self):
return (
f"{self.player_id} ({self.season} GW{self.gameweek}): "
f"£{self.price / 10}, {self.transfers_in}, {self.transfers_out}"
)


def get_connection_string():
Expand Down
40 changes: 40 additions & 0 deletions airsenal/scripts/price_change_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""
Classes for fetching and storing the transfers in/out for each player
The aim of this is to attempt to predict the price changes of players
and accordingly make transfers to maximise profit
"""
from sqlalchemy.orm.session import Session

from airsenal.framework.data_fetcher import FPLDataFetcher
from airsenal.framework.schema import session, TransferPriceTracker
from airsenal.framework.utils import NEXT_GAMEWEEK, CURRENT_SEASON

import datetime as dt
import pandas as pd


def fill_global_transfers_from_api(season: str, dbsession=Session):
"""
Get the global transfers in/out at the current time.
"""
fetcher = FPLDataFetcher()
summary: dict = fetcher.get_current_summary_data()
df: pd.DataFrame = (
pd.DataFrame(summary["elements"])
.rename(columns={"id": "player_id", "now_cost": "price"})[
["player_id", "price", "transfers_in", "transfers_out"]
]
.assign(
timestamp=dt.datetime.now().strftime("%Y-%m-%dT%H:%M:%SZ"),
gameweek=NEXT_GAMEWEEK,
season=season,
)
)
# add to database
for _, row in df.iterrows():
print(f"Adding transfer data for player {row['player_id']}")
dbsession.add(TransferPriceTracker(**row.to_dict()))
dbsession.commit()

if __name__ == "__main__":
fill_global_transfers_from_api(CURRENT_SEASON, dbsession=session)

0 comments on commit c9b2cdc

Please sign in to comment.