-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulator.py
68 lines (54 loc) · 2.14 KB
/
simulator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import json
from datetime import datetime
import pandas as pd
import numpy as np
import sys
from collections import namedtuple
from FinanceTools import *
class Simulator:
def __init__(self, tickers, value, start_date):
self.prcReader = PriceReader(tickers, start_date)
self.prcReader.load()
self.value = value
self.orders = []
self.restValue = {}
self.start_date = start_date
def ProcessTickers(self, stocks, date):
order = namedtuple("order", "Ticker Date Value Qty Type Category Fee")
for stock in stocks:
entryDate = stock["EntryDate"]
if pd.to_datetime(entryDate) > pd.to_datetime(date):
continue
part = float(stock["Participation"])
ticker = stock["Ticker"]
price = self.prcReader.getCurrentValue(ticker, date)
if price == np.nan:
continue
availableValue = (part * self.value) + self.restValue.get(ticker, 0)
qty = availableValue // price
if qty > 0:
self.orders.append(order(ticker, date, price, qty, "Compra", stock["Category"], (price * qty * 0.0003)))
rest = availableValue % price
if rest > 0:
self.restValue.update({ticker: rest})
def ProcessTimeline(self, stocks):
monthList = pd.date_range(start=self.start_date, end=datetime.today(), freq="MS").format(
formatter=lambda x: x.strftime(DataSchema.DATE_FORMAT)
)
for date in monthList:
self.ProcessTickers(stocks, date)
def Dataframe(self):
return pd.DataFrame(self.orders)
def GenerateWallet(inFile="simulator.json", outFile="orders.csv"):
with open(inFile) as file:
setup = json.load(file)
value = float(setup["BuyValue"])
tickers = []
for stock in setup["stocks"]:
tickers.append(stock["Ticker"])
sim = Simulator(tickers, value, setup["start_date"])
sim.ProcessTimeline(setup["stocks"])
sim.Dataframe().to_csv(outFile, index=False)
if __name__ == "__main__":
name = str(sys.argv[1])
GenerateWallet(name + ".json", name + ".csv")