def saveInstruments(instruments=[]): serverConfig = getServerConfig() instrumentsFilepath = os.path.join(serverConfig['deployDir'], 'instruments.json') with open(instrumentsFilepath, 'w') as isdFile: json.dump(instruments, isdFile, indent=2, default=str) logging.info('Instruments: Saved %d instruments to file %s', len(instruments), instrumentsFilepath) # Update last save timestamp Instruments.updateLastSavedTimestamp()
def run(): if Utils.isTodayHoliday(): logging.info("Cannot start TradeManager as Today is Trading Holiday.") return if Utils.isMarketClosedForTheDay(): logging.info("Cannot start TradeManager as Market is closed for the day.") return Utils.waitTillMarketOpens("TradeManager") # check and create trades directory for today`s date serverConfig = getServerConfig() tradesDir = os.path.join(serverConfig['deployDir'], 'trades') TradeManager.intradayTradesDir = os.path.join(tradesDir, Utils.getTodayDateStr()) if os.path.exists(TradeManager.intradayTradesDir) == False: logging.info('TradeManager: Intraday Trades Directory %s does not exist. Hence going to create.', TradeManager.intradayTradesDir) os.mkdirs(TradeManager.intradayTradesDir) # start ticker service brokerName = Controller.getBrokerName() if brokerName == "zerodha": TradeManager.ticker = ZerodhaTicker() #elif brokerName == "fyers" # not implemented # ticker = FyersTicker() TradeManager.ticker.startTicker() TradeManager.ticker.registerListener(TradeManager.tickerListener) # sleep for 2 seconds for ticker connection establishment time.sleep(2) # Load all trades from json files to app memory TradeManager.loadAllTradesFromFile() # track and update trades in a loop while True: if Utils.isMarketClosedForTheDay(): logging.info('TradeManager: Stopping TradeManager as market closed.') break try: # Fetch all order details from broker and update orders in each trade TradeManager.fetchAndUpdateAllTradeOrders() # track each trade and take necessary action TradeManager.trackAndUpdateAllTrades() except Exception as e: logging.exception("Exception in TradeManager Main thread") # save updated data to json file TradeManager.saveAllTradesToFile() # sleep for 30 seconds and then continue time.sleep(30) logging.info('TradeManager: Main thread woke up..')
def loadInstruments(): serverConfig = getServerConfig() instrumentsFilepath = os.path.join(serverConfig['deployDir'], 'instruments.json') if os.path.exists(instrumentsFilepath) == False: logging.warn('Instruments: instrumentsFilepath %s does not exist', instrumentsFilepath) return [] # returns empty list isdFile = open(instrumentsFilepath, 'r') instruments = json.loads(isdFile.read()) logging.info('Instruments: loaded %d instruments from file %s', len(instruments), instrumentsFilepath) return instruments
app = Flask(__name__) app.config['DEBUG'] = True app.add_url_rule("/", view_func=HomeAPI.as_view("home_api")) app.add_url_rule("/apis/broker/login/zerodha", view_func=BrokerLoginAPI.as_view("broker_login_api")) app.add_url_rule("/apis/algo/start", view_func=StartAlgoAPI.as_view("start_algo_api")) app.add_url_rule("/positions", view_func=PositionsAPI.as_view("positions_api")) app.add_url_rule("/holdings", view_func=HoldingsAPI.as_view("holdings_api")) def initLoggingConfg(filepath): format = "%(asctime)s: %(message)s" logging.basicConfig(filename=filepath, format=format, level=logging.INFO, datefmt="%Y-%m-%d %H:%M:%S") # Execution starts here serverConfig = getServerConfig() logFileDir = serverConfig['logFileDir'] if os.path.exists(logFileDir) == False: print("LogFile Directory " + logFileDir + " does not exist. Exiting the app.") exit(-1) tradesDir = serverConfig['tradesDir'] if os.path.exists(tradesDir) == False: print("Trades Directory " + tradesDir + " does not exist. Exiting the app.") exit(-1) print("LogFile Directory = " + logFileDir) print("Trades Directory = " + tradesDir) initLoggingConfg(logFileDir + "/app.log")
app = Flask(__name__) app.config['DEBUG'] = True app.add_url_rule("/", view_func=HomeAPI.as_view("home_api")) app.add_url_rule("/apis/broker/login/zerodha", view_func=BrokerLoginAPI.as_view("broker_login_api")) app.add_url_rule("/apis/algo/start", view_func=StartAlgoAPI.as_view("start_algo_api")) app.add_url_rule("/positions", view_func=PositionsAPI.as_view("positions_api")) app.add_url_rule("/holdings", view_func=HoldingsAPI.as_view("holdings_api")) def initLoggingConfg(filepath): format = "%(asctime)s: %(message)s" logging.basicConfig(filename=filepath, format=format, level=logging.INFO, datefmt="%Y-%m-%d %H:%M:%S") # Execution starts here serverConfig = getServerConfig(Path(__file__).parent.parent) deployDir = serverConfig['deployDir'] if os.path.exists(deployDir) == False: print("Deploy Directory " + deployDir + " does not exist. Exiting the app.") exit(-1) logFileDir = serverConfig['logFileDir'] if os.path.exists(logFileDir) == False: print("LogFile Directory " + logFileDir + " does not exist. Exiting the app.") exit(-1) print("Deploy Directory = " + deployDir) print("LogFile Directory = " + logFileDir) initLoggingConfg(logFileDir + "/app.log")