In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about how to achieve a Bollinger strategy in JavaScript. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
Brief introduction of strategy
Bollinger Belt is also called Bollinger Channel, which is referred to as BOLL in English. It is one of the most commonly used technical indicators, invented by John John Bollinger in the 1980s. In theory, the price always fluctuates around the value within a certain range. Bollinger Belt introduces the concept of "price channel" on the basis of this theory.
The method of calculation is to use the principle of statistics to calculate the "standard deviation" of the price for a period of time, and then to calculate the "trust interval" of the price by adding / subtracting 2 times the standard deviation of the moving average. Its basic type is a ribbon channel composed of three track lines (middle rail, upper rail and lower rail). The middle rail is the average cost of the price, and the upper rail and the lower rail represent the pressure line and support line of the price respectively. As shown in the following figure:
Due to the use of the concept of standard deviation, the width of the Bollinger channel will be dynamically adjusted according to the recent price fluctuations. If the fluctuation is small, the Bollinger channel will be narrowed; if the fluctuation is large, the Bollinger channel will become wider. When the BOLL channel changes from wide to narrow, it shows that the price gradually returns to the mean. When the BOLL channel changes from narrow to wide, it means that the market begins to change. If the price is on the track, it indicates an increase in buying power, and if the price goes down the track, it indicates an increase in effort.
Calculation method of Bollinger Belt Index
Among all the technical indicators, the calculation method of Bollinger belt is one of the most complex, in which the concept of standard deviation in statistics is introduced, which involves the calculation of middle trajectory (MB), upper trajectory (UP) and lower trajectory (DN). The calculation method is as follows:
Simple moving average for the middle rail = N time period
Standard deviation of upper rail = middle rail + K × N time period
Standard deviation of lower rail = middle rail − K × N time period
Function main () {/ / main function, where the program executes while (true) {/ / enter the loop exchange.SetContractType ('MA888'); / / sets the contract var records = exchange.GetRecords (); / / gets the K-line array var boll = TA.BOLL (records, 50); / / gets the 50-cycle BOLL indicator array top = boll [0] / / get the BOLL metrics track array ma = boll [1]; / / get the BOLL metrics track array bottom = boll [2]; / / get the BOLL metrics orbit array Log (top); / / print the BOLL metrics orbit array to the log Log (ma) / / print the track array of BOLL metrics to the log Log (bottom); / / print the track array of BOLL metrics to the log}}
Policy logic
Bollinger wires can be used in many ways, either alone or in combination with other indicators. In this tutorial, we will use one of the easiest ways to use Bollinger wires. That is, when the price breaks through the upper track from the bottom up, that is, when it breaks through the upper pressure line, we think that various forces are strengthening, a wave of rising market has been formed, and the buy open signal is generated; when the price falls below the bottom track, that is, when it falls below the support line, we believe that the short side is strengthening, a wave of downtrend has been formed, and the sell open signal is generated.
If after buying and opening, the price falls back to the middle track of the Bollinger line, we think that the various forces are weakening, or the short side is strengthening, and the sell closing signal is generated; if the price rises back to the middle rail of the Bollinger line after the sell is opened, we believe that the short force is weakening, or the multi-party force is strengthening, and the buy closing signal is generated.
Terms of sale and purchase
Long opening: if there is no position, and the closing price is greater than the track, and the time is not 14:45
Short opening: if there is no position, and the closing price is less than the lower track, and the time is not 14:45
Long closing position: if you hold a long order and the closing price is less than the middle track, or if the time is 14:45
Short closing: if you hold a short order and the closing price is greater than the middle track, or if the time is 14:45
Policy code implementation
In order to implement the strategy, we first need to consider what data we need. What API is used to get it? Then how to calculate the transaction logic? Finally, what are the ways to place an order to trade? Next, let's do it step by step:
Step 1: use the CTA policy framework
The so-called CTA strategy framework is a set of standard framework officially launched by the inventor. Using this framework, we can focus on programming transaction logic without considering the trivial problems of developing quantitative trading strategy. For example, if the framework is not used, when placing an order, you need to consider changing the position, issuing the purchase and purchase price, withdrawing the order or chasing the order when the order is not closed.
Function main () {$.CTA ("rb000/rb888", function (st) {/ / Writing Policy})}
The figure above is the CTA strategy framework using the inventor's quantification tool. This is a fixed code format, and all the transaction logic code is written from line 3. In use, except for the need to modify the variety code (light yellow), there is no need to modify anything else.
It should be noted that the variety code in the above figure is "rb000/rb888", which means that the signal data uses "rb000", the transaction data uses "rb888", and the position changes automatically. Of course, you can also specify a specific variety code, such as the variety code "rb1910", that is, both signal data and transaction data use "rb1910".
Step 2: get all kinds of data
Think about it carefully, what kind of data do you need? From our strategic trading logic, we find that: first, we need to obtain the current position, then compare the relationship between the closing price and the middle and lower tracks of the Bollinger belt index, and finally judge whether the market is about to close. So let's get these data.
Get K-line data
The first thing is to get the K-line array and the closing price of the last K-line, because with the K-line array, the Bollinger belt index can be calculated. Written in code, it goes like this:
Function main () {$.CTA ("rb000/rb888", function (st) {var r = st.records; / / get K-line array if (r.length)
< 20) return; // 过滤K线长度 var close = r[r.length - 2].close; // 获取上根K线收盘价 })} 如以上代码所示: 第4行:获取K线数组,这是一个固定的格式。 第5行:过滤K线的长度,因为我们计算布林带指标用的参数是20,当K线小于20根的时候,是无法计算布林带指标的。所以这里要过滤下K线的长度,如果K线少于20根,就直接返回,继续等待下一根K线。 第6行:从获取的K线数组中,先获取上根K线的对象,再从该对象中获取收盘价。获取一个数组的倒数第二个元素,就是这个数组的长度减2(r[r.length - 2]);K线数组里面的元素都是一个个对象,对象包含了开盘价、最高价、最低价、收盘价、成交量、时间,要获取收盘价就直接在后面加上"."和属性名就可以了(r[r.length - 2].Close)。 获取K线时间数据 因为我们是日内策略,需要在收盘前平掉仓位,所以要判断当前K线是不是临近收盘,如果是临近收盘的K线就平掉仓位,如果不是临近收盘的K线就可以开仓,用代码写出来是这样的: function main(){ $.CTA("rb000/rb888", function(st){ var r = st.records; // 获取K线数组 if (r.length < 20) return; // 过滤K线长度 var close = r[r.length - 2].close; // 获取上根K线收盘价 var time = new Date(r[r.length - 1].Time); // 根据当根K线时间戳,创建一个时间对象 var isClose = time.getHours() == 14 && time.getMinutes() == 45; // 判断时间是否14:45 })} 如以上代码所示: 第8行:获取当根K线的时间戳属性,然后创建一个时间对象(new Date(时间戳))。 第9行:根据时间对象,分别计算小时和分钟数,并判断当根K线的时间是不是14:45。 获取持仓数据 持仓信息是量化交易策略中一个很重要的条件,当交易条件成立时,还需要通过持仓状态和持仓数,来判断是否下单。比如:当买入开仓交易条件成立时,如果有持仓,就不必在重复下单了;如果无持仓,就可以下单。用代码写出来是这样的: function main(){ $.CTA("rb000/rb888", function(st){ var r = st.records; // 获取K线数组 if (r.length < 20) return; // 过滤K线长度 var close = r[r.length - 2].close; // 获取上根K线收盘价 var time = new Date(r[r.length - 1].Time); // 根据当根K线时间戳,创建一个时间对象 var isClose = time.getHours() == 14 && time.getMinutes() == 45; // 判断时间是否14:45 var mp = st.position.amount; // 获取持仓信息 })} 如以上代码所示: 第11行:获取当前的持仓状态。如果有多单,则值是1;如果有空单,则值是-1;如果无持仓,则值是0。 获取布林带数据 接着就需要计算布林带指标上轨、中轨、下轨的数值了。那就要先获取布林带数组,在从数组中获取上中下轨的数值。在发明者量化工具中,获取布林带数组还很简单,直接调用布林带的API就可以了,难的是获取上中下轨的数值,因为布林带数组是一个二维数组。 二维数组其实很好理解,它就是数组中的数组,那么获取的顺序就是:先获取数组中指定的数组,然后在从指定的数组中获取指定的元素,如以下代码所示: var arr = [[100, 200, 300], [10, 20, 30], [1, 2, 3]]; // 这是一个二维数组var test = arr[0]; // 先获取这个二维数组里面的第一个数组,并把结果设置给testvar demo1 = test[0]; // 再从test数组中,获取第一个数值demo1; // 结果是:100var demo2 = arr[0][0] // 我们也可以这样写demo2; // 结果同样为100 如以下代码中,第13行~19行就是用代码获取布林带上轨、中轨、下轨的数值。其中第13行是直接使用发明者量化工具的API,直接获取布林带数组;第14行~16行是先分别获取二维数组中的上轨数组、中轨数组、下轨数组;第17行~19行是分别从上轨数组、中轨数组、下轨数组中获取上根K线的布林带上轨、中轨、下轨数值。 function main(){ $.CTA("rb000/rb888", function(st){ var r = st.records; // 获取K线数组 if (r.length < 20) return; // 过滤K线长度 var close = r[r.length - 2].close; // 获取上根K线收盘价 var time = new Date(r[r.length - 1].Time); // 根据当根K线时间戳,创建一个时间对象 var isClose = time.getHours() == 14 && time.getMinutes() == 45; // 判断时间是否14:45 var mp = st.position.amount; // 获取持仓信息 var boll = TA.BOLL(r, 20, 2); // 计算布林带指标 var upLine = boll[0]; //获取上轨数组 var midLine = boll[1]; //获取中轨数组 var downLine = boll[2]; //获取下轨数组 var upPrice = upLine[upLine.length - 2]; //获取上根K线的上轨数组 var midPrice = midLine[midLine.length - 2]; //获取上根K线的中轨数组 var downPrice = downLine[downLine.length -2]; //获取上根K线的下轨数组 })} 第三步:下单交易 有了以上数据,就可以编写交易逻辑以及下单交易的代码了。格式也非常简单,最常用到的是"if语句",用文字可以描述为:如果条件1和条件2成立,下单;如果条件3或条件4成立,下单。如以下代码所示: function main(){ $.CTA("rb000/rb888", function(st){ var r = st.records; // 获取K线数组 if (r.length < 20) return; // 过滤K线长度 var close = r[r.length - 2].close; // 获取上根K线收盘价 var time = new Date(r[r.length - 1].Time); // 根据当根K线时间戳,创建一个时间对象 var isClose = time.getHours() == 14 && time.getMinutes() == 45; // 判断时间是否14:45 var mp = st.position.amount; // 获取持仓信息 var boll = TA.BOLL(r, 20, 2); // 计算布林带指标 var upLine = boll[0]; //获取上轨数组 var midLine = boll[1]; //获取中轨数组 var downLine = boll[2]; //获取下轨数组 var upPrice = upLine[upLine.length - 2]; //获取上根K线的上轨数组 var midPrice = midLine[midLine.length - 2]; //获取上根K线的中轨数组 var downPrice = downLine[downLine.length -2]; //获取上根K线的下轨数组 if (mp == 1 && (close < midPrice || isClose)) return -1; //如果持多单,并且收盘价小于中轨,或者时间是14:45,平多 if (mp == -1 && (close >MidPrice | | isClose) return 1; / / if the order is short and the closing price is greater than the medium rail, or if the time is 14:45, the if (mp = = 0 & close > upPrice & &! isClose) return 1; / / if there is no position and the closing price is greater than the track, and the time is 14:45, open more than if (mp = = 0 & close)
< downPrice && !isClose) return -1; //如果无持仓,并且收盘价小于下轨,并且时间是14:45,开空 })} 以上代码中,第21行~24行就是交易逻辑以及下单交易的代码。从上往下分别是:平多、平空、开多、开空。 以开多单(第23行)为例,这是一个"if语句",在该语句中如果只执行一行代码,花括号"{}"是可以省略的。该语句翻译成文字是:如果当前持仓是0,并且收盘价大于上轨,并且K线时间不是14:45,就"return 1" 细心的你可能会发现,这几行有"return 1"和"return -1",这是一个固定的格式,意思就是:如果是买入的就写"return 1";如果是卖出的就写"return -1"。开多和平空都是买入,所以写"return 1";开空和平多都是卖出,所以写"return -1"。 完整的策略代码 至此一个完整的策略代码就写完了,如果把交易框架、交易数据、交易逻辑、下单买卖等分开来写是不是很简单呢,以下就是这个策略的整个代码: function main(){ $.CTA("rb000/rb888", function(st){ var r = st.records; // 获取K线数组 if (r.length < 20) return; // 过滤K线长度 var close = r[r.length - 2].close; // 获取上根K线收盘价 var time = new Date(r[r.length - 1].Time); // 根据当根K线时间戳,创建一个时间对象 var isClose = time.getHours() == 14 && time.getMinutes() == 45; // 判断时间是否14:45 var mp = st.position.amount; // 获取持仓信息 var boll = TA.BOLL(r, 20, 2); // 计算布林带指标 var upLine = boll[0]; //获取上轨数组 var midLine = boll[1]; //获取中轨数组 var downLine = boll[2]; //获取下轨数组 var upPrice = upLine[upLine.length - 2]; //获取上根K线的上轨数组 var midPrice = midLine[midLine.length - 2]; //获取上根K线的中轨数组 var downPrice = downLine[downLine.length -2]; //获取上根K线的下轨数组 if (mp == 1 && (close < midPrice || isClose)) return -1; //如果持多单,并且收盘价小于中轨,或者时间是14:45,平多 if (mp == -1 && (close >MidPrice | | isClose) return 1; / / if you hold a short order and the closing price is greater than the medium rail, or if the time is 14:45, flat if (mp = = 0 & close > upPrice & &! isClose) return 1; / / if there is no position and the closing price is higher than the track, and the time is 14:45, open multiple if (mp = = 0 & close < downPrice & &! isClose) return-1 / / if there is no position, and the closing price is less than the lower track, and the time is 14:45, open})}
There are two areas to pay attention to:
1. Try (but not necessarily) to write the strategy logic as when the root K-line condition is established, the next K-line condition is established, or the upper K-line condition is true, and when the root K-line is issued, the result of the back test is not much different from that of the firm offer. It's okay not to write this way, but pay attention to whether the policy logic is correct.
2. in general, write the closing logic before the opening logic in order to make the strategy logic as much as possible in line with your expectations. For example, if the strategy logic happens to catch up with the backhand, the rule of the backhand is to close the position before opening a new position. Instead of opening a new position and then closing it. If we write the closing logic directly in front of the opening logic, this problem will not occur.
The above is how to achieve a Bollinger track strategy in the JavaScript shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are 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.