def indicator_add(): test = File(TEST_STOCK, class_type='Stock').load() test.add_indicator('SMA80') test.add_indicator('SMA150') compare(test, STOCK_WITH_INDICATORS, "Add indicators") test = File('SMA80', class_type='Indicator').load() compare(test, APPLIED_INDICATOR, "Applied indicator attributes")
def refresh(self, verbose = False, replace = False, to_date = None): # updates stock data with the latest data from quandl, computes indicators for the new values and computes orders for all strategies in Strategy if replace: self.__init__(self.code, self.indicators, self.strategies) if not self.uptodate(): df = self.get_data_from_api()[:to_date] self.data = pd.concat([self.data, df]).reset_index().drop_duplicates(subset='DATE', keep='first').set_index('DATE') self.last_date = self.data.iloc[-1].name for indicator_name in self.indicators: indicator = File(indicator_name, class_type = 'Indicator').load() indicator.apply_on_stock(self) """for strategy in self.strategies: strategy.apply_on_stock(self, verbose) self._set_last_refresh()""" self.save(self) if verbose: print(self.code + " successfully updated!")
def buy_awaiting(self): stock = File(self.stock, "Stock").load() position = stock.data.index.get_loc(self.opening_date) if position + 1 < len(stock.data): bid_price = stock.data.iloc[position + 1].OPEN self.set_bid_price(bid_price) self.set_opening_date(stock.data.iloc[position + 1].name) self.update_price(bid_price) self.change_status('pending')
def strategy_awaiting_sell(): stock = Stock(TEST_AWAITING_TRADE_STOCK) stock.refresh(to_date=TEST_REFRESH_DATE_AWAITING_SELL_1) stock.add_indicator('SMA80') stock.add_indicator('SMA150') test = File('SMA80_SMA150', class_type='Strategy').load() test.refresh([stock]) compare(test, STRATEGY_AWAITING_SELL_BEFORE, "Awaiting sell before") test.refresh([stock]) print("Reiteration: ok") stock.refresh(to_date=TEST_REFRESH_DATE_AWAITING_SELL_2) test.refresh([stock]) compare(test, STRATEGY_AWAITING_SELL_AFTER, "Awaiting sell after")
def drop(self, stocks): for stock_name in list(self.history.keys()): stock = File(stock_name).load() stock.drop_column(self.name) stock.save(stock) super().drop(self)
def process_request(self, str_request): if str_request == "help" and self._state != GDSProcessorState.COMPLETE: self._sender.send_text(self.help) if str_request == "abort": self._current_task.remove_source(self._current_source.source_title) self._state = GDSProcessorState.START self._sender.send_text("Настройка google drive source прервана") return True elif (self._state == GDSProcessorState.START and re.match( r"googleDrive", str_request, re.IGNORECASE) is not None): self._current_task.add_source(self._current_source) self._sender.send_text( "Настройка googleDrive source\n" "Введите название: ", end="") self._state = GDSProcessorState.NAMING elif self._state == GDSProcessorState.NAMING: self._current_source.source_title = str_request self._sender.send_text("Авторизация в google drive") self._google_drive_processor.process_request("start") self._state = GDSProcessorState.SETUP_GOOGLE_DRIVE elif self._state == GDSProcessorState.SETUP_GOOGLE_DRIVE: self._google_drive_processor.process_request(str_request) if self._google_drive_processor.is_finished(): self._state = GDSProcessorState.DESTINATION_SUB_PATH self._sender.send_text("Введите путь для сохранения: ", end="") elif self._state == GDSProcessorState.DESTINATION_SUB_PATH: self._current_source.set_destination_sub_path_to_restore( str_request) self._sender.send_text( "Введите путь к файлам на google drive" "(для завершения введите пустую строку): ", end="") self._state = GDSProcessorState.ADD_FILE_REMAINING elif self._state == GDSProcessorState.ADD_FILE_REMAINING: if str_request != "": self._current_task.add_element_to_restore( File(file_path=str_request), self._current_source) else: self._sender.send_text( "Настройка Google Drive source завершена") self._state = GDSProcessorState.COMPLETE elif self._state == GDSProcessorState.COMPLETE: return self._google_drive_processor.process_request(str_request) else: self._sender.send_text("Ошибка") return False
def add_indicator(self, indicator_name): indicator = File(indicator_name, class_type = "Indicator").load() indicator.apply_on_stock(self) if not (indicator_name in self.indicators): self.indicators.append(indicator.name) self.save(self)
def sell_awaiting(self): stock = File(self.stock, "Stock").load() position = stock.data.index.get_loc(self.closing_date) if position + 1 < len(stock.data): self.close(stock.data.iloc[position + 1].name, stock.data.iloc[position + 1].OPEN)
def get_indicator(indicator_name): return File(indicator_name, class_type='Indicator').load()
def get_function(function_name): return File(function_name, class_type='Function').load()
def get_stock(code): return File(code, class_type='Stock').load()
def get_strategy(strategy_name): return File(strategy_name, class_type='Strategy').load()
def load_stocks(): stocks = [] codes = Codes() for index, _ in codes.data[codes.data.INCLUDE_FOR_ANALYSIS].iterrows(): stocks.append(File(index, class_type='Stock').load()) return stocks
def stock_load(): File(TEST_STOCK, class_type='Stock').load() print("Loading stock: ok")
def strategy_apply(): stock = File(TEST_STOCK, class_type='Stock').load() test = File('SMA80_SMA150', class_type='Strategy').load() test.refresh([stock]) compare(test, APPLIED_STRATEGY, "Apply strategy") compare(stock, STOCK_WITH_APPLIED_STRATEGY, "Applied strategy")
def refresh_with_indicators(): test = File(TEST_STOCK, class_type='Stock').load() test.refresh(to_date=TEST_REFRESH_DATE_WITH_INDICATORS) compare(test, REFRESHED_STOCK_WITH_INDICATORS, "Refresh with indicators")
def stock_refresh(): test = File(TEST_STOCK, class_type='Stock').load() test.refresh(to_date=TEST_REFRESH_DATE_EMPTY) compare(test, REFRESHED_EMPTY_STOCK, "Empty stock refresh") test.refresh(to_date=TEST_REFRESH_DATE_NOT_EMPTY) compare(test, REFRESHED_NOT_EMPTY_STOCK, "Not empty stock refresh")