TradingBase.py
2.21 KB
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
import pandas as pd
class TradingBase:
Name = ''
TotalInvestmentAmount = 0
Method = "Split evenly"
BuyingMethod = 1 # 1 : at_once
BuyingDateTime = [] # datetime
SellDateTime = [] # datetime
NumberOfStocks = 10
PriceDiv = "Open"
StockBuyingSearchTarget = []
StockSellSearchTarget = []
df_all = None
df_stock = None
db = None
def __init__(self):
self.Name = 'Base'
def __repr__(self):
return self.Name
def setTrading(self):
print("setTrading")
def doTrading(self):
self.df_all['BuyingPrice'] = 0
self.df_all['SellPrice'] = 0
self.df_all['BuyingAmount'] = 0
if self.Method == 1: # "Split evenly"
if self.BuyingMethod == 1:
investmentAmount = self.TotalInvestmentAmount / self.NumberOfStocks
for index, row in self.df_all.iterrows():
self.df_stock = self.db.findSPbyID(self.StockBuyingSearchTarget, self.df_all.loc[index, 'StockCode'], self.BuyingDateTime[0].strftime("%Y-%m-%d"))
if self.df_stock is None :
print('db query error')
else:
if self.df_stock.size < 1 :
continue
buyingPrice = self.df_stock[self.StockBuyingSearchTarget[0]][0]
self.df_stock = self.db.findSPbyID(self.StockSellSearchTarget, self.df_all.loc[index, 'StockCode'], self.SellDateTime[0].strftime("%Y-%m-%d"))
if self.df_stock is None :
print('db query error')
else:
if self.df_stock.size < 1 :
continue
sellPrice = self.df_stock[self.StockSellSearchTarget[0]][0]
self.df_all.loc[index, 'BuyingPrice'] = buyingPrice
self.df_all.loc[index, 'SellPrice'] = sellPrice
self.df_all.loc[index, 'BuyingAmount'] = int(investmentAmount / buyingPrice)
elif self.BuyingMethod == 2:
print('...')
else:
print('...')
else:
print('...')