In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to transform the Python single-variety strategy into a multi-variety strategy, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article.
First, hand-in-hand to teach you to transform Python single-variety strategy into multi-variety strategy.
Python version of the up-and-down strategy can operate an account on a trading pair for programmed trading, the principle is very simple, that is to chase up and down. Sometimes we want to use the same trading logic to operate different trading pairs. You can create multiple robots and set up different trading pairs to trade in various currencies. If the strategy is not very complex, given the strong flexibility of the inventor's quantitative trading platform. It is easy to transform a strategy into a multi-variety strategy, so that you can run multiple transactions by creating a single robot.
Modified strategy source code:
Backteststart: 2019-02-20 00:00:00end: 2020-01-10 00:00:00period: 1mexchanges: [{"eid": "OKEX", "currency": "BTC_USDT"}, {"eid": "OKEX", "currency": "ETH_USDT", "stocks": 30}, {"eid": "OKEX", "currency": "LTC_USDT", "stocks": 100}]''import timeimport jsonparams = {"arrBasePrice": [- 1,-1,-1] #-1 "arrRatio": [0.05,0.05,0.05], # 0.05 "arrAcc": [], # _ C (exchange.GetAccount) "arrLastCancelAll": [0,0,0], # 0 "arrMinStocks": [0.01,0.01,0.01], # 0.01 "arrPricePrecision": [2,2,2] # 2 "arrAmountPrecision": [3,2,2], # 2 "arrTick": []} def CancelAll (e): while True: orders = _ C (e.GetOrders) for i in range (len (orders)): e.CancelOrder (orders [I] ["Id"], orders [I]) if len (orders) = 0: break Sleep (1000) def process (e Index): global params ticker = _ C (e.GetTicker) params ["arrTick"] [index] = ticker if params ["arrBasePrice"] [index] =-1: params ["arrBasePrice"] [index] = ticker.Last if ticker.Last-params ["arrBasePrice"] [index] > 0 and (ticker.Last-params ["arrBasePrice"] [index]) / params ["arrBasePrice"] [index] > params ["arrRatio"] [index]: Params ["arrAcc"] [index] = _ C (e.GetAccount) if params ["arrAcc"] [index]. Balance * params ["arrRatio"] [index] / ticker.Last > params ["arrMinStocks"] [index]: e.Buy (ticker.Last) Params ["arrAcc"] [index]. Balance * params ["arrRatio"] [index] / ticker.Last) params ["arrBasePrice"] [index] = ticker.Last if ticker.Last-params ["arrBasePrice"] [index]
< 0 and (params["arrBasePrice"][index] - ticker.Last) / params["arrBasePrice"][index] >Params ["arrRatio"] [index]: params ["arrAcc"] [index] = _ C (e.GetAccount) if params ["arrAcc"] [index] .Stocks * params ["arrRatio"] [index] > params ["arrMinStocks"] [index]: e.Sell (ticker.Last Params ["arrAcc"] [index]. Stocks * params ["arrRatio"] [index]) params ["arrBasePrice"] [index] = ticker.Last ts = time.time () if ts-params ["arrLastCancelAll"] [index] > 60 * 5: CancelAll (e) params ["arrLastCancelAll"] [index] = ts def main (): global params for i in range (len (exchanges)): Params ["arrAcc"] .append (_ C (exchanges.GetAccount) params ["arrTick"] .append (_ C (exchanges[ I] .GetTicker)) exchanges.SetPrecision (params ["arrPricePrecision"] [I] Params ["arrAmountPrecision"] [I]) for key in params: if len (params [key]) < len (exchanges): raise "params error!" While True: tblAcc = {"type": "table", "title": "account", "cols": ["account information"], "rows": []} tblTick = {"type": "table", "title": "ticker" "cols": ["Market Information"], "rows": []} for i in range (len (exchanges)): process (Exchange [I]] I) for i in range (len (exchanges)): tblAcc ["rows"]. Append ([json.dumps (params ["arrAcc"] [I])) tblTick ["rows"] .append ([json.dumps (params ["arrTick"] [I])]) LogStatus (_ D (), "\ n`" + json.dumps ([tblAcc, tblTick]) + `) Sleep (500) II.
Comparing the code, do you find that it is very different from the code in the previous article? In fact, the transaction logic is exactly the same, without any changes, but if we modify the strategy into multiple varieties, we cannot use the previous form of "a single variable as a policy parameter". The more reasonable solution is to make the parameters into an array, and the index of each position in the array corresponds to the added transaction pair.
Then encapsulate this part of the transaction logic code into a function process, in the strategy main loop, iteratively call this function according to the added transaction pair, and let each transaction pair execute the transaction logic code once.
Iterative (traversal) call: for i in range (len (exchanges)): process (exchanges [I], I)
Policy parameters: params = {"arrBasePrice": [- 1,-1,-1], #-1 "arrRatio": [0.05,0.05,0.05], # 0.05 "arrAcc": [], # _ C (exchange.GetAccount) "arrLastCancelAll": [0,0,0] # 0 "arrMinStocks": [0.01,0.01,0.01], # 0.01 "arrPricePrecision": [2,2,2], # 2 "arrAmountPrecision": [3,2,2], # 2 "arrTick": []}
This design allows each trading pair to have its own parameters, because each trading pair may vary greatly in price, and may also vary in parameters, sometimes requiring differentiated settings.
CancelAll function
You can compare the changes in this function. This function just modifies the code a little bit, and then thinks about the intention of changing it like this.
Status bar chart data
Added a chart showing market data and account asset data in the status bar, which can display the corresponding assets and prices of each exchange object in real time.
Having mastered the above design ideas, isn't it easy to modify a Python strategy into a multi-variety strategy?
Third, back test
Thank you for reading this article carefully. I hope the article "how to transform Python single-variety strategy into multi-variety strategy" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.