This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def remove_similar_sentences(sentences, model, similarity_threshold): | |
| """ | |
| model: the nlp model to use from the sentence transformer library | |
| sentences: a list of sentences from a website | |
| Returns a list of new sentences which are not too similar to each other | |
| """ | |
| new_sentences = sentences.copy() | |
| # Compute embeddings | |
| embeddings = model.encode(new_sentences, device='cpu', show_progress_bar=False) | |
| # Compute cosine-similarities for each sentence with each other sentence |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def get_documents(urls): | |
| """ | |
| Takes a list of urls, gets unique sentences from each website, | |
| and returns all of those sentences in a single list | |
| """ | |
| documents = [] | |
| for url in tqdm(urls): | |
| sentences = get_sentences(url) | |
| new_sentences = remove_similar_sentences(sentences, model, similarity_threshold) | |
| documents.append(new_sentences) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from backtesting import Strategy | |
| from backtesting.lib import crossover | |
| from backtesting import Backtest | |
| from yfinance import Ticker | |
| import pandas as pd | |
| from talib import EMA, SMA, WMA |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class EmaCross(Strategy): | |
| # Define the two EMA lags as *class variables* | |
| # for later optimization | |
| n1 = 5 | |
| n2 = 10 | |
| def init(self): | |
| # Precompute two moving averages | |
| self.ema1 = self.I(EMA, self.data.Close, self.n1) | |
| self.ema2 = self.I(EMA, self.data.Close, self.n2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def get_data(pair): | |
| ticker = Ticker(pair) | |
| df = ticker.history(period='3y', rounding=True)[['Open','High','Low','Close']] | |
| return df |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| df = get_data('ETH-USD') | |
| df = df/1000 | |
| strategies = [SmaCross, EmaCross, WmaCross] | |
| all_runs = [] | |
| for strategy in strategies: | |
| bt = Backtest(df, strategy, cash=100, commission=.002) | |
| stats = bt.optimize(n1=range(5, 90, 5), | |
| n2=range(10, 120, 5), | |
| maximize='Return (Ann.) [%]', | |
| constraint=lambda param: param.n1 < param.n2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from ccxt import coinbasepro | |
| def get_client(): | |
| client = coinbasepro({ | |
| 'apiKey': '', | |
| 'secret': '', | |
| 'password': '', | |
| 'enableRateLimit': True, | |
| }) | |
| return client |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def buy_max(pair, client=client): | |
| # Use max possible usd size | |
| price = client.fetchTicker(pair)['last'] | |
| size = client.fetchBalance()['USD']['free'] / price | |
| client.createMarketBuyOrder(pair, amount=size) | |
| print(f"bought ${size} of {pair}") | |
| def sell_max(pair, client=client): | |
| symbol = pair.split("-")[0] | |
| size = client.fetchBalance()[symbol]['free'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def run(request): | |
| alert = request.get_json() | |
| # # If alert request is a buy, we buy | |
| symbol = alert['ticker'] | |
| pair = symbol + "-USD" | |
| side = alert['side'] | |
| usd_price = client.fetch_ticker(pair)['last'] | |
| position = client.fetch_balance()[symbol]['free'] | |
| size = usd_price * position | |
| if side == 'buy' and size < 1: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def get_ohlc(ticker, period='60d', interval='5m'): | |
| cols = ['Open','High','Low','Close'] | |
| ohlc = Ticker(ticker).history(period=period, interval=interval)[cols][:-1] | |
| return ohlc | |
| df = get_ohlc('ETH-USD') |