Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to encapsulate a Python policy into a local file

2025-01-18 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 package a Python strategy into a local file". In daily operation, I believe many people have doubts about how to package a Python strategy into a local file. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the question of "how to package a Python strategy into a local file". Next, please follow the editor to study!

Encapsulate a policy

Let's find a simple Python strategy to demonstrate, using the classic Dual Thrust strategy, policy address: https://www.fmz.com/strategy/21856

We strive to encapsulate the policy into a file that can be called by the policy code on the FMZ platform without changing any part of the policy code, and the execution result is exactly the same as running the policy directly. The biggest problem with encapsulation is that the global objects, global functions, and constant values called by the policy code on the FMZ platform cannot be accessed in our encapsulated files, so we must find a way to transfer these objects, functions, variables, and constants to the encapsulated file. Then we will deal with it step by step.

Copy the python version of Dual Thrust OKCoin futures strategy code, paste into the local Python file, the local Python file named testA.

Paste into the file testA opened by the local editor.

Add some code to keep the part of the policy code copied and pasted intact

# function, object exchanges = Noneexchange = NoneLog = NoneSleep = NoneTA = NoneChart = NoneLogProfitReset = NoneLogStatus = None_C = NoneLogProfit = None # Policy parameters ContractTypeIdx = NoneMarginLevelIdx = NoneNPeriod = NoneKs = NoneAmountOP = NoneInterval = NoneLoopInterval = NonePeriodShow # constant ORDER_STATE_PENDING = 0ORDER_STATE_CLOSED = 1ORDER_STATE_CANCELED = 3ORDER_TYPE_BUY = 0ORDER_ TYPE_SELL = 1PD_LONG = 0PD_SHORT = 1 def SetExchanges (es): global exchanges Exchangeexchanges = esexchange = es [0] def SetFunc (pLog, pSleep, pTA, pChart, pLogStatus, pLogProfitReset, pendant N, pendant C, pLogProfit): global Log, Sleep, TA, Chart, LogStatus, LogProfitReset, _ N, _ C, LogProfitLog = pLogSleep = pSleepTA = pTAChart = pChartLogStatus = pLogStatusLogProfitReset = pLogProfitReset_N = p_N_C = p_CLogProfit = pLogProfit def SetParams (pContractTypeIdx, pMarginLevelIdx, pNPeriod, pKs, pKx, pAmountOP, pInterval, pLoopInterval, pPeriodShow): pPeriodShow, pPeriodShow, global ContractTypeIdx, MarginLevelIdx, NPeriod, MarginLevelIdx PeriodShowContractTypeIdx = pContractTypeIdxMarginLevelIdx = pMarginLevelIdxNPeriod = pNPeriodKs = pKsKx = pKxAmountOP = pAmountOPInterval = pIntervalLoopInterval = pLoopIntervalPeriodShow = pPeriodShow

The main function of the above code is to declare global functions and variables used in the current file. Then reserve the interface SetExchanges,SetParams,SetFunc for importing these functions. Policies on the FMZ platform call these functions and pass in some of the functions, objects, and so on.

Startup Strategy on FMZ platform

The startup strategy is simple, as follows:

There are only a few lines of code written on the FMZ platform. It should be noted that the parameters of this startup strategy are exactly the same as the python version of the strategy we encapsulated. In fact, you can directly copy the "python version of Dual Thrust OKCoin Futures" strategy, and then clear the policy code and paste it.

Import sys# here I wrote the path where I placed the testA file, specifically I replaced it with xxx To put it simply, you can set your own testA file path sys.path.append ("/ Users/xxx/Desktop/pythonPlayground/") import testAdef main (): # transfer exchange object testA.SetExchanges (exchanges) # transfer global function SetFunc (pLog, pSleep, pTA, pChart, pLogStatus, pLogProfitReset, pumped N, pumped C, pLogProfit) testA.SetFunc (Log, Sleep, TA, Chart, LogStatus, LogProfitReset, _ N, _ C, LogProfit) # delivery policy parameters SetParams (pContractTypeIdx, pMarginLevelIdx, pNPeriod) PKs, pKx, pAmountOP, pInterval, pLoopInterval, pPeriodShow) testA.SetParams (ContractTypeIdx, MarginLevelIdx, NPeriod, Ks, Kx, AmountOP, Interval, LoopInterval, PeriodShow) # executes the policy main function testA.main () in the encapsulated testA file

In this way, we encapsulate the policy logic body in the testA file and put it on the local device of the custodian. On the FMZ platform, we only need to save a startup policy, and the robot that creates this startup policy can directly load our local file and run it locally in the custodian.

Back test and comparison

Local loading testA file back test

Original policy, back-tested on public servers

Another, simpler way

Load the file directly into execution.

This time we prepare a testB file to place the code for the "python version of Dual Thrust OKCoin Futures" strategy.

Import timeclass Error_noSupport (BaseException): def _ init__ (self): Log ("only support OKCoin futures! # FF0000") class Error_AtBeginHasPosition (BaseException): def _ init__ (self): Log ("Futures position at startup! # FF0000") ChartCfg = {'_ isStock': True,'title': {'text':' Dual Thrust up and down chart'}, 'yAxis': {.

If the policy is too long, it will be omitted, and the policy code will not be changed at all.

Then prepare the "python version of Dual Thrust OKCoin futures (start strategy, directly execute the testB file)", which is our strategy on the FMZ platform, create a robot, load the testB file directly, and execute it directly. It should be noted that the startup strategy must also have the same policy parameter settings (policy interface parameters) as the original "python Dual Thrust OKCoin Futures".

If _ _ name__ = ='_ main__':Log ("run...") try:# file path is processed, and you can write to the actual path where your testB file is placed f = open ("/ Users/xxx/Desktop/pythonPlayground/testB.py", "r") code = f.read () exec (code) except Exception as e:Log (e)

Perform a backtest:

At this point, the study on "how to encapsulate a Python strategy into a local file" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report