In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to use fastcache, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!
VnTrader 2.0 version has a lot of speed-up measures, of which lru_cache is a sharp tool to improve the back test speed, let me use 1.92 mainly I envy. It seems that this is the function provided by python 3.5.2, so I don't think much about it.
It has only recently been discovered that there are third parties that also support python 2.7s, such as functools32. There is also a C language implementation, faster, compatible with both Python2 and Python3 third-party module fastcache can achieve the same function, here using fastcache.
Installation
Very simple, pip can be installed directly.
Pip install fastcache-upgrade tests whether import fastcachefastcache.test () is installed correctly and is easy to use
When not using cache, the runtime is 0.7994769 seconds
From fastcache import clru_cachedef fib (n): if n < 2: return n return fib (n-2) + fib (n-1) import timedt0 = time.clock () for i in range (30): fib (I) spreadtime = (time.clock ()-dt0) print ("run time% s seconds"% spreadtime)
After use, the running time is 0.000185200000004 seconds
@ clru_cache (maxsize=999) def fib_cache (n): if n < 2: return n return fib_cache (n-2) + fib_cache (n-1) import timedt1 = time.clock () for i in range (30): fib_cache (I) spreadtime = (time.clock ()-dt1) print ("run time% s seconds"% spreadtime) use clru_cache to update trader/app/ctaStrategy/ctaBacktesting.py to cache back test historical data to memory You don't have to read it repeatedly.
Lru_cache is usually used only in static methods, which makes sense, although it can also be used in class methods, but there are different instances that generally cannot be reused and make expired instances unable to garbage collect. Here, create a new static method load_data, which is responsible for reading database data.
Clru_cache (maxsize=9999) def load_data (dbName, symbol, dataStartDate, strategyStartDate, dataEndDate, dataClass): dbClient = pymongo.MongoClient (globalSetting ['mongoHost'], globalSetting [' mongoPort']) collection = dbClient [dbName] [symbol] # data needed to load initialization flt = {'datetime': {' $gte': dataStartDate '$lt': strategyStartDate}} initCursor = collection.find (flt) .sort (' datetime') initData = [] # clear initData list for d in initCursor: data = dataClass () data.__dict__ = d initData.append (data) # load test data if not dataEndDate: flt = {'datetime': {' $gte' : strategyStartDate}} # data filter condition else: flt = {'datetime': {' $gte': strategyStartDate '$lte': dataEndDate}} BackData = [] dbCursor = collection.find (flt) .sort (' datetime') for dc in dbCursor: data = dataClass () data.__dict__ = dc BackData.append (data) count = len (initData) + len (BackData) return initData, BackData, count
Modify the existing method BacktestingEngine.loadHistoryData; to use the static method you just created
Def loadHistoryData (self): "" load historical data "self.output (u' start loading data') # first according to the backtest mode Confirm the data class to be used # load_data (dbName, symbol, dataStartDate, strategyStartDate, dataEndDate, dataClass) if self.mode = = self.BAR_MODE: dataClass = VtBarData func = self.newBar self.initData,self.BackTestData, count = load_data (self.dbName,self.symbol, self.dataStartDate, self.strategyStartDate, self.dataEndDate DataClass) else: dataClass = VtTickData func = self.newTick self.initData, self.BackTestData, count = load_data (self.dbName, self.symbol, self.dataStartDate, self.strategyStartDate, self.dataEndDate, dataClass) # load the data needed for initialization if self.hdsClient: initCursor = self.hdsClient.loadHistoryData (self.dbName Self.symbol, self.dataStartDate, self.strategyStartDate) # read the data from the query pointer And generate list self.initData = [] # clear initData list for d in initCursor: data = dataClass () data.__dict__ = d self.initData.append (data) # load back test data self.dbCursor = self.hdsClient.loadHistoryData (self.dbName Self.symbol, self.strategyStartDate Self.dataEndDate) for dc in self.dbCursor: data = dataClass () data.__dict__ = dc self.BackTestData.append (data) self.output (u' load complete Amount of data:% s'% count)
Modify the BacktestingEngine.runBacktesting; to use the replaced BackTestData queue instead of the database pointer.
Def runBacktesting (self): "run the backtest"# load the historical data self.loadHistoryData () # first according to the retest mode Confirm the data class to be used if self.mode = = self.BAR_MODE: dataClass = VtBarData func = self.newBar else: dataClass = VtTickData func = self.newTick self.output (u 'start backtest') self.strategy.onInit () self.strategy.inited = True self.output (u 'policy initialization complete') self.strategy.trading = True self.strategy.onStart () self.output (u' policy startup completed') self.output (u' start playback data') for d in self.BackTestData: func (d) self.output (u' data playback end')
Before use, it takes about 323.0888239 seconds to optimize 100 sets of parameters, and 190.762839 seconds after using cache optimization.
The above is all the contents of the article "how to use fastcache". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.