In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "how to implement the entrustment strategy in python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Strategic thinking
Similarly, after entrusting an order, the mechanism of automatically generating stop-loss, stop-profit and backhand delegated tasks is used, except that the first entrusted order is triggered by the deviation between the 1-minute level average and the current price, and the regression strategy is used to close the position before the close. The strategy is not complicated.
Policy implementation details
Data preparation in Policy polling Logic
/ / get the market exchange.SetContractType (_ EntrustSymbol) / / set the contract, for example, set the parameter _ EntrustSymbol to rb2001var ticker = _ C (exchange.GetTicker) / / get the tick data var records = _ C (exchange.GetRecords) / / get the K-line data, set the K-line period to 1 minute, and here you get the 1-minute K-line data $.PlotRecords (records, "K") / / drawing. Draw the K line var nowTs = new Date (). GetTime () / / to get the current time and timestamp.
Get this data for later calculation.
You need to calculate the moving average after the opening of trading every morning. First of all, you need to determine the timestamp of the first K line at the opening of trading in daily terms:
For (var j = records.length-1; j >-1) Ts -) {var ts = records.time if (ts% (1000 * 60 * 24) = = 3600000) {/ / 60 * 60 * 1000 = 3600000 if (oneDayBeginTS! = ts) {oneDayBeginTS = ts Log ("A new day begins!") / / clear a global variable tag for the task queue IsEntrust = false / / setting Used to mark whether the delegate has been triggered. Here is to reset} break}} at the beginning of a new day.
This code traverses K-line data in reverse order to determine that the first K-line BAR,ts that meets ts% (1000 * 60 * 24) = = 3600000 is the timestamp of K-line, ts and 1000 * 60 * 60 * 24 to determine how many milliseconds have elapsed in a day. When this value equals 3600000, it is judged to be the start of the new opening time, as it opens at 9:00 in the morning, 60 * 60 * 1000 = 3600000, exactly 9:00 Beijing time when time has passed 3600000 milliseconds (1 hour). In order to get the time stamp of the first K-line Bar that opens every day: oneDayBeginTS = ts
Then calculate the moving average:
Var sum = 0var count = 0var avg = 0for (var n = 0; n
< records.length; n++) { if (records[n].Time >= oneDayBeginTS) {sum + = records [n] .close count++ if (n = = records.length-1) {avg = sum / count $.PlotLine ("avg", avg, records.time) # draw the moving average}
Calculate ATR
Using the 5-day ATR indicator as a reference for the degree of deviation, first calculate the ATR:
Var records_Day = _ C (exchange.GetRecords, PERIOD_D1) / / get daily K-line if (records_Day.length oneDayBeginTS + 1000 * 60 * 60 * 6) {p.CoverAll () _ TaskQueue = []}
The opening time of oneDayBeginTS + 1000 * 60 * 60 * 69:00 is delayed by 6 hours, that is, the opening time is one minute earlier. When the condition nowTs + 1000 * 60 > oneDayBeginTS + 1000 * 60 * 60 * 6 holds, that is, it is less than 1 minute before the closing time, the closing of the position begins. Proud of the powerful function of "Commodity Futures Trading Class Library", you can close all positions by directly calling the full leveling function p.CoverAll (). Commodity futures trading class library is not familiar with can look at this class library code, the code is open source.
Trigger entrustment condition
If (Math.abs (records.length-1] .close-avg) > _ Dis & &! IsEntrust) {var task = {taskType: ENTRUST, taskSymbol: _ EntrustSymbol, taskPrice: records [records.length-1] .close, taskAmount: _ EntrustAmount, taskDirection: records [records.length-1] .close-avg > 0? "sell": "buy", taskTrigger: records [records.length-1] .close-avg > 0? 1:-1, / / greater than trigger taskFinished: false} Log ("Please note, create delegated task", task, "# FF0000") _ TaskQueue.push (task) IsEntrust = true}
With the condition Math.abs (records [records.length-1] .close-avg) > _ Dis & &! IsEntrust as the trigger condition, a delegated task is created as long as the current closing price minus the absolute value of the current EMA is greater than the floating trigger distance of _ Dis.
If the delegated task is triggered, it will automatically create a series of stop-profit, stop-loss, and backhand tasks as in the previous article.
This is the end of the content of "how to implement the delegation strategy in python". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.