def __init__(self) -> None: db = Database() stocks: pd.DataFrame stocks_fs = db.load_data(table_name=TableName.DAY_FS, limit=1) db_mode = "append" if stocks_fs is not None and len(stocks_fs) > 0: stocks = db.load_data(table_name=TableName.DAY, time_from=stocks_fs.index[0]) else: stocks = db.load_data(table_name=TableName.DAY, time_from="-500d") db_mode = "replace" symbols = db.get_symbols() for sym in symbols: if len(stocks) > 0: stocks_sym = FinI.add_indicators(stocks[stocks.sym == sym]) # stocks.columns.drop(["symbol","sector","industry"]) # for idx, sym_s in stocks_sym.iterrows(): # sql_string = "".join(["select * from financials where symbol = '", # str(sym_s.sym), # "' and date::date <= date '", # str(idx.replace(hour=23, minute=59)), # "' order by date desc limit 1"]) # print(sql_string) # financials = db.sql_select_query_data(sql_string) # new_row = pd.concat([stocks_sym, financials], axis=1) # new_row.to_sql(name="p_day_fs", # if_exists="append", con=db.engine) if stocks_sym is not None: stocks_sym.to_sql(name="p_day_fs", if_exists=db_mode, con=db.engine) print(stocks_sym)
class Trading(): def __init__(self): self.db = Database() self.bs = BuySell( trading_strategy=TradingStrategies.LOGISTIC_REGRESSION, trading_provider=TradingProviders.ALPACA, fill_open_trades=OpenTradesSource.DB) self.bs.close_alpaca_postition = True self.spy_limit = 0 def lr_best_candidate(self): logging.warning("start") df_best_buy = pd.DataFrame() symbols = self.db.get_symbols() prob_perc = 0.9 spy_lr_raw = self.logistic_regression_raw(self.db, symbol="SPY") if spy_lr_raw.tail(1).iloc[0].prob_1 > self.spy_limit: for sym in symbols: try: logging.info(f"filling: {sym}") df_lr_raw = self.logistic_regression_raw(self.db, sym) df_best_buy = df_best_buy.append(df_lr_raw.tail(1)) logging.info(sym + str(df_lr_raw.tail(1).iloc[0].prob_1)) logging.info("SPY: " + str(spy_lr_raw.tail(1).iloc[0].prob_1)) # BUY if df_lr_raw.tail(1).iloc[0].prob_1 > prob_perc: self.bs.buy_stock_t( stock=df_lr_raw.tail(1).iloc[0], price=df_lr_raw.tail(1).iloc[0].close, qty=1, buy_already_buyed=True) logging.info("Buy" + sym) Utils.countdown(10) # SELL elif df_lr_raw.tail( 1 ).iloc[0].prob_1 < prob_perc and sym in self.bs.buy_sell_open[ "sym"]: self.bs.sell_stock_t( sym=sym, price=df_lr_raw.tail(1).iloc[0].close, qty=1) logging.info("Sell" + sym) except Exception as e: logging.info(e) if len(df_best_buy) > 0: logging.info(df_best_buy.sort_values(by="prob_1")) else: logging.error("no data") else: self.close_all_alpaca_postitions() self.close_all_db_postitions() logger.info("Buy prob: " + str(spy_lr_raw.tail(1).iloc[0].prob_1) + " Sell prob: " + str(spy_lr_raw.tail(1).iloc[0].prob_0)) def close_all_alpaca_postitions(self): self.bs.close_all_alpaca_postitions() def close_all_db_postitions(self): self.bs.clean_all_db_postitions() @staticmethod def logistic_regression_raw(db: Database, symbol="SPY"): df = db.load_data(table_name=TableName.DAY, time_from="-90D", symbols=[symbol]) # m_df_spy = self.db.load_data( # table_name=TableName.DAY, time_from=self.time_from, symbols=["SPY"]) df['open-close'] = df['close'] - df['open'].shift(1) df['close-close'] = df['close'].shift(-1) - df['close'] # wrong close close only for research df['close-close-prev'] = df['close'] - df['close'].shift(1) df['S_9'] = df['close'].rolling(window=9).mean() df['S_20'] = df['close'].rolling(window=20).mean() # df['S_50'] = df['close'].rolling(window=50).mean() # df['S_200'] = df['close'].rolling(window=200).mean() df['Corr_9'] = df['close'].rolling(window=9).corr(df['S_9']) df['Corr_20'] = df['close'].rolling(window=9).corr(df['S_20']) df['RSI'] = ta.momentum.rsi(close=df['close']) y = np.where(df['close'].shift(-1) > df['close'], 1, -1) df = df[[ "Corr_9", "open-close", "close-close-prev", "RSI", "S_9", "close" ]] # df = df[["Corr_9", "open-close", "close-close-prev", "RSI", "S_9"]] df = df.dropna() X = df.iloc[:, :30] # st.write(len(y)) # st.write(len(X)) split = int(0.7 * len(df)) X_train, X_test, y_train, y_test = X[:split], X[split:], y[:split], y[ split:] # We will instantiate the logistic regression in Python using ‘LogisticRegression’ # function and fit the model on the training dataset using ‘fit’ function. model = LogisticRegression() model = model.fit(X_train, y_train) # Examine coeficients # pd.DataFrame(zip(X.columns, np.transpose(model.coef_))) # st.write("Examine The Coefficients") # st.write(pd.DataFrame(zip(X.columns, np.transpose(model.coef_)))) #We will calculate the probabilities of the class for the test dataset using ‘predict_proba’ function. probability = model.predict_proba(X_test) df['Predicted_Signal'] = model.predict(X) df = df.tail(len(probability)) df["prob_0"] = probability[:, 0] df["prob_1"] = probability[:, 1] df["sym"] = symbol return df
class SlAs(): selected_stock = None timetick = None def __init__(self): self.db = Database() self.symbols = self.db.get_symbols(TableName.DAY) self.sectors = self.db.get_sectors(TableName.DAY) self.sm = StockMess() self.app = self.get_home_page() self.sw = StockWhisperer() self.submit = None def get_home_page(self): st.set_page_config(layout="wide") t1 = threading.Thread(target=SlAs.watch, args=(st, )) t1.start() # loop = asyncio.get_event_loop() print('after async') test = st.text_area("websocket", self.timetick) st.write("some text") test = st.text_area("websocket2", self.timetick) @staticmethod def watch(sti): i = 0 while True: print('thread') Utils.countdown(5) i += 1 st.write(str(i)) # test = st.empty() async def watch2(self, test): while True: print('async') Utils.countdown(5) test = str(datetime.now()) # test.markdown( # f""" # <p class="time"> # {str(datetime.now())} # </p> # """, unsafe_allow_html=True) try: await asyncio.sleep(1) except KeyboardInterrupt: print("ending async") exit() test = st.empty() @staticmethod async def time_tick(): timetick = 0 while True: timetick += 1 await asyncio.sleep(1)
class Trader(object): """ docstring """ def __init__(self): """ docstring """ self.bs = BuySell() self.db = Database() self.bt = BackTest() def load_data(self, table_name=None, symbols=None, sectors=None, limit=None): symbols = symbols if symbols is not None else self.symbols return sdf.retype( self.db.load_data(table_name, symbols=symbols, sectors=sectors, limit=limit)) def test_boll_rsi_macd(self, table_name=None, buy_now=False): self.bs.buyed_stocks = 0 self.bs.money = self.bs.credit["start_amount"] spy_stocks = self.load_data(table_name=table_name, symbols="SPY") symbols = self.db.get_symbols() print(str(symbols)) for symbol in symbols: print("symbol: " + str(symbol)) stck = self.load_data(table_name=table_name, symbols=symbol) if len(stck) < 1: break stck = self.bt.add_indicators(stck) print("calculating percent change:" + str(symbol)) # stck = self.stocks.loc[self.stocks.sym ==symbol[0]].sort_values(by='index') buy_now_process = buy_now self.symbols = symbol[0] cross_bollinger = 0 # self.prev_stock = stck.iloc[0] # self.first_stock = stck.iloc[0] # self.sell_marks = self.sell_marks.iloc[0:0] # self.buy_marks = self.buy_marks.iloc[0:0] self.transactions = 0 self.profit_perc = 0 for inx in range(50, len(stck)): # def check_boll(): """ docstring """ if stck.iloc[inx].boll >= stck.iloc[inx-1].close and \ stck.iloc[inx]['boll'] <= stck.iloc[inx].close: print("go up " + str(stck.iloc[inx].name) + " - boll:" + str(stck.iloc[inx - 1].boll) + " -prev: " + str(stck.iloc[inx - 1].close) + " - curr:" + str(stck.iloc[inx].close)) cross_bollinger = 1 elif stck.iloc[inx]['boll'] <= stck.iloc[inx-1]['close'] and \ stck.iloc[inx]['boll'] >= stck.iloc[inx]['close']: print("go down " + str(stck.iloc[inx].name) + " - boll:" + str(stck.iloc[inx - 1].boll) + " -prev: " + str(stck.iloc[inx - 1].close) + " - curr:" + str(stck.iloc[inx]['close'])) cross_bollinger = -1 else: cross_bollinger = 0 if self.bs.buyed_stocks == 0 and \ (cross_bollinger == 1) or \ buy_now_process: self.bs.buy(stck) buy_now_process = False #comment this block for selling at the end of the date if self.bs.buyed_stocks != 0 and \ (cross_bollinger == -1 or (stck['boll_mid_lb'] <= stck.iloc[inx-1]['close'] and stck.iloc[inx]['boll_mid_lb'] > stck.iloc[inx]['close'])): self.bs.sell(stck.iloc[inx]) # if self.buyed_stocks == 0 and \ # (cross_bollinger == 1 or \ # (stock['boll_mid_ub'] >= self.prev_stock['close'] and stock['boll_mid_ub'] < stock['close']) or \ # (stock['boll_mid_lb'] >= self.prev_stock['close'] and stock['boll_mid_lb'] < stock['close'])): # self.buy_stock(stock) # if self.buyed_stocks != 0 and \ # (cross_bollinger == -1 or \ # (stock['boll_mid_ub'] <= self.prev_stock['close'] and stock['boll_mid_ub'] > stock['close']) or \ # (stock['boll_mid_lb'] <= self.prev_stock['close'] and stock['boll_mid_lb'] > stock['close'])): # self.sell_stock(stock) # if self.buyed_stocks == 0 and \ # (cross_bollinger == 1 or \ # (stock['boll_mid_ub'] >= self.prev_stock['close'] and stock['boll_mid_ub'] < stock['close'])): # self.buy_stock(stock) # if self.buyed_stocks != 0 and \ # (cross_bollinger == -1 or \ # (stock['boll_mid_ub'] <= self.prev_stock['close'] and stock['boll_mid_ub'] > stock['close'])): # self.sell_stock(stock) # self.prev_stock = stck # check_boll() if self.transactions > 0: self.show_stats(symbol) # self.plot_stats(stck, spy_stocks) else: print("Theres no transactions please change BUY/SELL params")
class RunData(): selected_stock = None timetick = None time_from = None time_to = None selected = {} action_type = None types = ["sector-stats", "stock-detail", "stocks-stats"] def __init__(self): self.db = Database() self.symbols = self.db.get_symbols(TableName.DAY) self.sectors = self.db.get_sectors(TableName.DAY) self.sm = StockMess() self.app = self.get_home_page() self.sw = StockWhisperer() self.submit = None # self.fig = None # print(self.df) # def load_data(self, option, time_from = "-180d", time_to = None): # df = self.db.load_data( # "p_day", symbols=option, time_from=time_from, time_to = time_to) # df = FinI.add_indicators(sdf.retype(df)) # return df def get_home_page(self): st.set_page_config(layout="wide") asyncio.new_event_loop().run_until_complete(RunData.prepare_test_tasks()) @staticmethod async def prepare_test_tasks(): tasks = [] task = asyncio.ensure_future(RunData.async_test(0.05)) tasks.append(task) task = asyncio.ensure_future(RunData.async_test(0.1)) tasks.append(task) await asyncio.gather(*tasks, return_exceptions=True) @staticmethod async def async_test(sleep): progress_bar = st.sidebar.progress(0) status_text = st.sidebar.empty() last_rows = np.random.randn(1, 1) chart = st.line_chart(last_rows) for i in range(1, 101): new_rows = last_rows[-1, :] + np.random.randn(5, 1).cumsum(axis=0) status_text.text("%i%% Complete" % i) chart.add_rows(new_rows) progress_bar.progress(i) last_rows = new_rows await asyncio.sleep(sleep) progress_bar.empty()