Ejemplo n.º 1
0
class Stats(object):
    """
    docstring
    """
    def __init__(self):
        """
        docstring
        """
        self.db = Database()
        self.df = pd.DataFrame()

    def get_data(self):
        self.db.set_time_from("-120d")
        self.df = self.db.load_data(TableName.DAY, symbols="jks")
        return self.df

    def get_volume_realtions(self):
        df = self.get_data()
        print(df.corr())
        # print(df)
        sns.pairplot(df, kind="scatter")
        plt.show()

    def linear_relations(self):
        df = self.get_data()
        print(linregress(df['close'], df['volume']))
        plt.scatter(df['close'], df['volume'])
        plt.show()
Ejemplo n.º 2
0
 def __init__(self):
     """
     docstring
     """
     self.bs = BuySell()
     self.db = Database()
     self.bt = BackTest()
Ejemplo n.º 3
0
 def __init__(self):
     """
     docstring
     """
     self.db = Database()
     self.df = pd.DataFrame()
     self.bt = BackTest()
Ejemplo n.º 4
0
 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
Ejemplo n.º 5
0
 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
Ejemplo n.º 6
0
Archivo: app.py Proyecto: 4crash/4crash
 def __init__(self):
     self.db = Database()
     self.df = self.db.load_data("p_day",
                                 symbols=['SPY'],
                                 time_from="-120d")
     self.df = sdf.retype(self.df)
     self.df = FinI.add_indicators(self.df)
     self.app = self.get_home_page(self.df)
     self.fig = None
Ejemplo n.º 7
0
class MPred():

    data = pd.DataFrame()

    def __init__(self):
        """
        docstring
        """
        self.db = Database()

    def prepare_prop(self, sym):
        # self.db.set_time_from("365d")
        data = self.db.load_data("p_day", symbols=sym)
        data["ds"] = pd.to_datetime(data.index, utc=True).tz_localize(None)
        data["y"] = data.close
        print(data)
        m = Prophet(daily_seasonality=True)  # the Prophet class (model)
        m.fit(data)  # fit the model using all data
        self.plot_prediction(m, sym)

    def plot_prediction(self, m, sym):
        # we need to specify the number of days in future
        future = m.make_future_dataframe(periods=365)
        prediction = m.predict(future)
        m.plot(prediction)
        plt.title("Prediction using the Prophet: " + str(sym))
        plt.gca().fmt_xdata = mpl.dates.DateFormatter('%Y-%m-%d')
        plt.xlabel("Date")
        plt.ylabel("Close Stock Price")
        plt.show()
Ejemplo n.º 8
0
    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
Ejemplo n.º 9
0
Archivo: app.py Proyecto: 4crash/4crash
def update_figure(input_value):
    if len(input_value) > 1:
        input_value = "SPY"

    db = Database()
    df = db.load_data("p_day", symbols=input_value, time_from="-120d")
    df = sdf.retype(df)
    df = FinI.add_indicators(df)
    fig = go.Figure()
    fig.add_trace(
        go.Candlestick(x=df.index,
                       open=df['open'],
                       high=df['high'],
                       low=df['low'],
                       close=df['close']), )

    fig.add_trace(go.Scatter(x=df.index, y=df.sma9))

    fig.update_layout(transition_duration=500)

    return fig
Ejemplo n.º 10
0
Archivo: app.py Proyecto: 4crash/4crash
class RunDash():
    def __init__(self):
        self.db = Database()
        self.df = self.db.load_data("p_day",
                                    symbols=['SPY'],
                                    time_from="-120d")
        self.df = sdf.retype(self.df)
        self.df = FinI.add_indicators(self.df)
        self.app = self.get_home_page(self.df)
        self.fig = None
        # print(self.df)

    def get_home_page(self, df):
        # print(df.index)
        self.fig = go.Figure()
        symbols = ["NVDA", "PLUG", "AMD", "SPY"]

        self.fig.add_trace(
            go.Candlestick(x=df.index,
                           open=df['open'],
                           high=df['high'],
                           low=df['low'],
                           close=df['close']), )

        self.fig.add_trace(go.Scatter(x=df.index, y=df.sma9))

        app.layout = html.Div(children=[
            html.H1(children='Stock prices'),
            dcc.Dropdown(id='buyed_symbols',
                         options=[{
                             'label': i,
                             'value': i
                         } for i in symbols],
                         value='symbol'),
            html.Br(),
            html.Div(id="output"),
            dcc.Graph(id='price-graph', figure=self.fig)
        ])
        return app
Ejemplo n.º 11
0
    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)
Ejemplo n.º 12
0
utc = pytz.UTC
# from buy_sell import BuySell
# from sklearn import linear_model, preprocessing
from pytz import timezone

localtz = timezone('Europe/Prague')
# from plot_i import PlotI
# from fin_i import FinI
# from utils import Utils
# from check_indicators import CheckIndicators as chi
from alpaca_examples.market_db import Database, TableName
from buy_sell import BuySell
import logging
# import pytest

db = Database()
bs = BuySell()
data_intc = db.load_data(table_name=TableName.DAY,
                         time_from="-10d",
                         symbols=["INTC"])
data_amd = db.load_data(table_name=TableName.DAY,
                        time_from="-10d",
                        symbols=["AMD"])


def test_buy_sell_stock_t():
    bs.buy_sell_open = pd.DataFrame()
    bs.money = 10000

    bs.buy_stock_t(data_intc.iloc[0],
                   data_intc.iloc[0].close,
Ejemplo n.º 13
0
class PredDT(object):
    """
    docstring
    """
    def __init__(self):
        """
        docstring
        """
        self.db = Database()
        self.df = pd.DataFrame()
        self.bt = BackTest()
    
    def dt_prediction(self):
        self.db.set_time_from("730d")
        self.df = self.db.load_data(TableName.DAY,symbols='SPY')
        # print(self.df)
        self.add_sma(30)
        self.add_sma(100)
        print(self.df)
        pass
    def add_sma(self, days):
        self.df["sma"+str(days)] = self.df['close'].rolling(window=days).mean()
        
    def plot_data(self):

        self.df.plot(kind="line", use_index=True,
                y=["close","sma30","sma100"], legend=False, color=["b","orange","y"], linewidth=2, alpha=0.7)
        plt.show()

    def heatmap(self):
        self.db.set_time_from("600d")
        self.df = self.db.load_data(TableName.DAY, symbols='CHWY')
        # calculate the correlation matrix
        
        # self.df = ta.add_all_ta_features(self.df,open="open",close="close",high="high",low="low", volume="volume")
        stocks = sdf.retype(self.df)
        stocks = FinI.add_indicators(stocks)
        stocks.get("macd")
        stocks.get("kdjk")
        stocks.get("boll")
        stocks.get("sma")
        print(stocks)
        # stocks = FinI.add_day_types(stocks)
        
        stocks.drop(columns=["open", "high", "low",
                           "sym", "sector", ], inplace=True)
        print(stocks[['volume',"yearweek"]])
        corr = stocks.corr()
        corr = corr.round(1)
        # print(corr)

        # plot the heatmap
        sns.heatmap(corr, annot=True, cmap="coolwarm",
                    xticklabels=corr.columns,
                    yticklabels=corr.columns)
        plt.show()
    
    def prediction(self):
        df = self.df[['close']]
        future_days = 25
        # create the future date set X and convert it ti numpy array and remove last 'x' rows/days
        df['Prediction'] = df[['close']].shift(-future_days)
        X=np.array(df.drop(['Prediction'],1))[:-future_days]
        # create the target data cell and get all of the target data 
        y=np.array(df['Prediction'])[:-future_days]
        # print(y)
        # lets split the data into 75% training and 25 %tresting
        x_train,x_test, y_train, y_test = train_test_split(X, y, test_size = 0.25)
        
        # create decision tree regresor
        tree = DecisionTreeRegressor().fit(x_train, y_train)
        # create regresssion model
        lr = LinearRegression().fit(x_train, y_train)
        # get the last x rows of the future data set
        x_future = df.drop(['Prediction'],1)[:-future_days]
        x_future = x_future.tail(future_days)
        x_future = np.array(x_future)
        
        tree_prediction = tree.predict(x_future)
        print(tree_prediction)
        lr_prediction = lr.predict(x_future)
        print()
        print(lr_prediction)
        
        # visualise predicted data
        predictions = tree_prediction
        valid = df[X.shape[0]:]
        valid['Predictions'] = predictions
        plt.figure(figsize=(16,8))
        plt.title('days')
        plt.xlabel('days')
        plt.gca().fmt_xdata = mpl.dates.DateFormatter('%Y-%m-%d')
        plt.grid(axis="both")
        plt.ylabel("prediction")
        plt.plot(df['close'])
        plt.plot(valid[['close','Predictions']])
        plt.legend(['Orig','Val', 'Pred'])
        plt.show()
Ejemplo n.º 14
0
 def __init__(self):
     """
     docstring
     """
     self.db = Database()
Ejemplo n.º 15
0
import pytz
utc = pytz.UTC
# from alpaca_examples.buy_sell import BuySell
# from sklearn import linear_model, preprocessing
from pytz import timezone
localtz = timezone('Europe/Prague')
from alpaca_examples.plot_i import PlotI
from alpaca_examples.fin_i import FinI
from alpaca_examples.utils import Utils
from alpaca_examples.check_indicators import CheckIndicators as chi
from alpaca_examples.market_db import Database, TableName

import pytest

db = Database()
sym = "INTC"


def test_get_nearest_values():
    # price_levels = np.array([10,8,5,3,-1,-5,-10,-15])
    df = db.load_data(table_name=TableName.DAY, symbols=[sym])
    df = FinI.add_levels(df)
    price = df.iloc[-1].close

    low, high = FinI.get_nearest_values(
        price_levels=df.price_level.dropna().tolist(), price=price)
    print("low" + str(low))
    print("high" + str(high))

    assert low is not None and low[0] < price, "low  array failed"
Ejemplo n.º 16
0
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
Ejemplo n.º 17
0
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")
Ejemplo n.º 18
0
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()
Ejemplo n.º 19
0
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)