Ejemplo n.º 1
0
 def select_best_to_sell(self,
                         tester=None,
                         fund_list=None,
                         date=datetime.datetime(2019, 1, 2)):
     print("sell select started:", datetime.datetime.now())
     if tester is None:
         tester = self.tester
     if fund_list is None:
         fund_in_db = db_operator.sql_query(
             sql="SELECT distinct fund_code from Fund").fund_code.values
         fund_list = data_operator.get_funds_list()
         fund_list = fund_list[fund_list.ID.isin(fund_in_db)]
     best_fund = ["", 0]
     # test_all_funds_data = data_operator.load_funds(fund_list.ID.values,duration=[date-datetime.timedelta(days = 365*3), date])
     my_strategy = Strategy(tester)
     t1 = datetime.datetime.now()
     for fund_code in fund_list.ID.values:
         # all_fund_for_today = test_all_funds_data[test_all_funds_data.fund_code == fund_code]
         best_prob = my_strategy.if_sell(fund_code,
                                         date,
                                         True,
                                         all_data_fund=None)
         if best_prob > best_fund[1]:
             best_fund[0] = fund_code
             best_fund[1] = best_prob
     print(datetime.datetime.now() - t1, "used.")
     print("-", best_fund, date, datetime.datetime.now())
     return best_fund
Ejemplo n.º 2
0
 def verify_funds(self,
                  duration=[
                      datetime.datetime(2015, 1, 31),
                      (datetime.datetime(2012, 1, 31) +
                       datetime.timedelta(365 * 8))
                  ]):
     fund_in_db = db_operator.sql_query(
         sql="SELECT distinct fund_code from Fund").fund_code.values
     fund_list = data_operator.get_funds_list()
     fund_list = fund_list[fund_list.ID.isin(fund_in_db)]
     principal_fund = 1000
     self.tester = User(username="******",
                        password="******",
                        my_cash=principal_fund)
     predict_results = pd.DataFrame({
         "fund_code": [],
         "fund_name": [],
         "principal": [],
         "init_asset": [],
         "result": [],
         "gain_rate": [],
         "yearly_gain_rate": []
     })
     for i in range(len(fund_list)):
         res = self.verify_one_fund(fund_code=fund_list.ID.iloc[i],
                                    duration=duration)
         predict_results.append([[
             fund_list.ID.iloc[i], principal_fund, res[0], res[1], res[2],
             res[3]
         ]])
         print("fund_code:" + str(fund_list.ID.iloc[i]),
               "fund_name:" + fund_list.Name.iloc[i], "init:" + str(res[0]),
               "result:" + str(res[1]), "gain_rate:" + str(res[2]),
               "yearly_gain_rate:" + str(res[3]))
     predict_results.to_csv(config["output_path"])
Ejemplo n.º 3
0
 def save_latest_price(self, today=datetime.datetime.today(), timedelta=10):
     today_date = datetime.datetime(today.year, today.month, today.day)
     fund_list = data_operator.get_funds_list().ID.values
     today_fund = data_operator.get_funds_multi_thread(
         fund_list=fund_list,
         duration=[today_date - datetime.timedelta(timedelta), today_date],
         entry_per_page=20)
     data_operator.update_fund(today_fund)
Ejemplo n.º 4
0
 def verify_funds_multithread(self,
                              duration=[
                                  datetime.datetime(2015, 1, 31),
                                  (datetime.datetime(2012, 1, 31) +
                                   datetime.timedelta(365 * 8))
                              ],
                              principal=3000):
     print("job started: ", datetime.datetime.now())
     fund_in_db = db_operator.sql_query(
         sql="SELECT distinct fund_code from Fund").fund_code.values
     fund_list = data_operator.get_funds_list()
     fund_list = fund_list[fund_list.ID.isin(fund_in_db)]
     predict_result = {}
     predict_results = pd.DataFrame({
         "fund_code": [],
         "fund_name": [],
         "principal": [],
         "init_asset": [],
         "result": [],
         "gain_rate": [],
         "yearly_gain_rate": []
     })
     with ThreadPoolExecutor(config["threads"]) as executor:
         for i in range(len(fund_list)):
             # tester = User(username="******"+fund_list.ID.iloc[i], password="******", my_cash=3000)
             print(fund_list.ID.iloc[i], "started @",
                   datetime.datetime.now())
             r1 = executor.submit(self.verify_one_fund, None,
                                  fund_list.ID.iloc[i], duration, principal)
             predict_result[fund_list.ID.iloc[i]] = r1
             print(fund_list.ID.iloc[i], "ended @", datetime.datetime.now())
     for (x, y) in predict_result.items():
         res = y.result()
         new_record = {}
         new_record["fund_code"] = x
         new_record["fund_name"] = fund_list[fund_list.ID == x].Name.iloc[0]
         new_record["principal"] = principal
         new_record["init_asset"] = res[0]
         new_record["result"] = res[1]
         new_record["gain_rate"] = res[2]
         new_record["yearly_gain_rate"] = res[3]
         predict_results.append(new_record, ignore_index=True)
         print("fund_code:" + new_record["fund_code"],
               "fund_name:" + new_record["fund_name"],
               "init:" + str(res[0]), "result:" + str(res[1]),
               "gain_rate:" + str(res[2]),
               "yearly_gain_rate:" + str(res[3]))
     if os.path.exists(config["output_path"]):
         os.remove(config["output_path"])
     predict_results.to_csv(config["output_path"])
Ejemplo n.º 5
0
 def verify_funds_multiprocesses(self,
                                 duration=[
                                     datetime.datetime(2015, 1, 31),
                                     datetime.datetime(2020, 1, 28)
                                 ],
                                 principal=3000):
     p = Pool(config["ps_num"])
     print("job started: ", datetime.datetime.now())
     fund_in_db = db_operator.sql_query(
         sql="SELECT distinct fund_code from Fund").fund_code.values
     fund_list = data_operator.get_funds_list()
     fund_list = fund_list[fund_list.ID.isin(fund_in_db)]
     predict_result = {}
     predict_results = pd.DataFrame({
         "fund_code": [],
         "fund_name": [],
         "principal": [],
         "init_asset": [],
         "result": [],
         "gain_rate": [],
         "yearly_gain_rate": []
     })
     for i in range(len(fund_list)):
         a = p.apply_async(func=self.verify_one_fund,
                           args=(None, fund_list.ID.iloc[i], duration,
                                 principal))
         predict_result[fund_list.ID.iloc[i]] = a
     p.close()
     p.join()
     print("***** All jobs were thrown *****")
     for (x, y) in predict_result.items():
         res = y.get()
         new_record = {}
         new_record["fund_code"] = x
         new_record["fund_name"] = fund_list[fund_list.ID == x].Name.iloc[0]
         new_record["principal"] = principal
         new_record["init_asset"] = res[0]
         new_record["result"] = res[1]
         new_record["gain_rate"] = res[2]
         new_record["yearly_gain_rate"] = res[3]
         predict_results.append(new_record, ignore_index=True)
         print("fund_code:" + new_record["fund_code"],
               "fund_name:" + new_record["fund_name"],
               "init:" + str(res[0]), "result:" + str(res[1]),
               "gain_rate:" + str(res[2]),
               "yearly_gain_rate:" + str(res[3]))
     if os.path.exists(config["output_path"]):
         os.remove(config["output_path"])
     predict_results.to_csv(config["output_path"])
Ejemplo n.º 6
0
 def verify_comp(self,
                 duration=[
                     datetime.datetime(2015, 1, 31),
                     datetime.datetime(2020, 1, 28)
                 ],
                 principal=3000):
     fund_in_db = db_operator.sql_query(
         sql="SELECT distinct fund_code from Fund").fund_code.values
     fund_list = data_operator.get_funds_list()
     fund_list = fund_list[fund_list.ID.isin(fund_in_db)]
     tester = User(username="******" +
                   str(datetime.datetime.now().microsecond),
                   password="******",
                   my_cash=principal)
     tester_operator = Operator(user=tester)
     asset_before = tester_operator.get_asset_value(date=duration[0],
                                                    contains_cash=True,
                                                    realtimeprice=False)
     for i in range((duration[1] - duration[0]).days + 1):
         today = duration[0] + datetime.timedelta(days=i)
         fund_sell_prob = self.select_best_to_sell(tester=tester,
                                                   fund_list=fund_list,
                                                   date=today)
         fund_buy_prob = self.select_best_to_buy(tester=tester,
                                                 fund_list=fund_list,
                                                 date=today)
         if fund_sell_prob[1] > 0.7:
             tester_operator.sell_at_date(fund_code=fund_sell_prob[0],
                                          date=today,
                                          amount=100,
                                          if_value=True)
         if fund_buy_prob[1] > 0.7:
             tester_operator.buy_at_date(fund_code=fund_buy_prob[0],
                                         date=today,
                                         amount=100,
                                         if_value=True)
             print(tester_operator.user.asset)
     asset_after = tester_operator.get_asset_value(duration[1],
                                                   contains_cash=True,
                                                   realtimeprice=False)
     print(tester.asset, tester.my_cash)
     gain = asset_after - asset_before
     gain_rate = (asset_after - asset_before) / asset_before
     yearly_gain_rate = gain_rate / ((duration[1] - duration[0]).days / 365)
     return [asset_before, asset_after, gain, gain_rate, yearly_gain_rate]
Ejemplo n.º 7
0
 def get_funds_to_buy(self,
                      fund_list=data_operator.get_funds_list().ID.values):
     # real_time_price = data_operator.get_realtime_price(fund_list)
     today = datetime.datetime.today()
     self.save_latest_price()
     my_strategy = Strategy(user=self.user)
     prob_to_buy = my_strategy.if_buy_accelerated(fund_list=fund_list,
                                                  today_date=today,
                                                  if_verify=False,
                                                  all_data_fund=None)
     # prob_to_sell = my_strategy.if_sell_accelerated_2(fund_list=self.user.asset.my_fund.values, today_date=today, if_verify=True, all_data_fund=None)
     if len(prob_to_buy) > 0:
         for code in prob_to_buy.index:
             print("buy: ", code, "prob: ", prob_to_buy[code])
             # tester_operator.buy_at_date(fund_code=code, date=today, amount=30, if_value=True)
     # if len(prob_to_sell)>0:
     #     for code in prob_to_sell.index:
     #         print("sell: ", code, "prob: ", prob_to_sell[code])
     #         # tester_operator.sell_fund_all(fund_code=code,date=today)
     return prob_to_buy
Ejemplo n.º 8
0
import datetime
sys.setrecursionlimit(100000)
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
from utils.getfund import data_operator
# from utils.readconfig import config
# from models.models import db_operator
from utils.operatefund import *
# import datetime
# from bs4 import BeautifulSoup
import pandas as pd
from multiprocessing import Process, Queue, Pool
from models.models import db_operator
from concurrent.futures import ThreadPoolExecutor
# funds = ["110013", "000011","040001","020022"]
# count = 0
fds_list = data_operator.get_funds_list("data/fund_data/all_fund.csv")
res = data_operator.get_realtime_price(fund_codes=fds_list.ID.values)
print(res)
# fund_in_db = db_operator.sql_query(sql = "SELECT distinct fund_code from Fund").fund_code.values
# ff = db_operator.sql_query("SELECT * from Fund where fund_code = '110013' Order by date DESC")
# print(ff)
# ff = db_operator.sql_query("SELECT * from Fund where fund_code = '110013' Order by date")
# ff = data_operator.load_funds(fund_codes=["110013","110031"],duration=[datetime.datetime(2018,1,2),datetime.datetime(2018,2,1)],orderby="date", asc = "DESC")
# ff = data_operator.load_fund(fund_code="110013", duration=[datetime.datetime(2018,1,1), datetime.datetime(2018,1,10)],orderby="price", asc="DESC")
# print(ff.dtypes, ff)
# print(fund_in_db)
# print("111",datetime.datetime.now())
# fund_data_3y = data_operator.load_funds(fund_codes=fund_in_db,duration=[datetime.datetime(2015,1,27), datetime.datetime(2018,1,25)])
# print("112",datetime.datetime.now())
# fund_data_2y = fund_data_3y[fund_data_3y.date>datetime.datetime(2015,1,27)+datetime.timedelta(days = 364)]
# print("113",datetime.datetime.now())
Ejemplo n.º 9
0
import datetime
sys.setrecursionlimit(100000)
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
from utils.getfund import data_operator
# from utils.readconfig import config
# from models.models import db_operator
from utils.operatefund import *
# import datetime
# from bs4 import BeautifulSoup
import pandas as pd
from multiprocessing import Process, Queue, Pool
from models.models import db_operator
from concurrent.futures import ThreadPoolExecutor
# funds = ["110013", "000011","040001","020022"]
# count = 0
fds_list = data_operator.get_funds_list()
res = data_operator.get_realtime_price(fund_codes=fds_list.ID.values)
print(res)
# fund_in_db = db_operator.sql_query(sql = "SELECT distinct fund_code from Fund").fund_code.values
# ff = db_operator.sql_query("SELECT * from Fund where fund_code = '110013' Order by date DESC")
# print(ff)
# ff = db_operator.sql_query("SELECT * from Fund where fund_code = '110013' Order by date")
# ff = data_operator.load_funds(fund_codes=["110013","110031"],duration=[datetime.datetime(2018,1,2),datetime.datetime(2018,2,1)],orderby="date", asc = "DESC")
# ff = data_operator.load_fund(fund_code="110013", duration=[datetime.datetime(2018,1,1), datetime.datetime(2018,1,10)],orderby="price", asc="DESC")
# print(ff.dtypes, ff)
# print(fund_in_db)
# print("111",datetime.datetime.now())
# fund_data_3y = data_operator.load_funds(fund_codes=fund_in_db,duration=[datetime.datetime(2015,1,27), datetime.datetime(2018,1,25)])
# print("112",datetime.datetime.now())
# fund_data_2y = fund_data_3y[fund_data_3y.date>datetime.datetime(2015,1,27)+datetime.timedelta(days = 364)]
# print("113",datetime.datetime.now())