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 realize the transaction Strategy based on the active flow of funds in Java

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to realize the trading strategy based on capital initiative flow in Java". The explanation in the article is simple and clear, easy to learn and understand. Please follow the editor's train of thought to study and learn "how to realize the trading strategy based on capital initiative flow in Java".

1. Summary

The price is either up or down. In the long run, the probability of price rise and fall should each be 50%. In order to correctly predict the future price, you need to obtain all the factors that affect the price in real time, and then give each factor a correct weight. finally, make an objective and rational analysis. To list all the factors that affect the price, it may fill the whole screen.

It can be summarized as follows: global economic environment, national macro policies, relevant industrial policies, supply and demand, international events, interest rates and exchange rates, inflation and tightening, market psychology, unknown factors and so on. Prediction has become a huge and impossible task. So early on, I knew that the market was unpredictable. So all the predictions in the market become hypotheses, and trading becomes a probability game, which is interesting.

Second, why to use the flow of funds

Since the market is unpredictable, is it really indifferent? No, all the macro and micro factors have been reflected in the price, that is to say, the price is the result of the interaction of all factors. We only need to analyze the price to make a complete trading strategy.

Think about it carefully first, why did the price go up?

You may say, because: the state policy support to the relevant industries, the origin of the rainstorm, the international trade war, MACD gold fork, others have bought and so on, of course, these may be right. In hindsight, there is always a reason for driving up prices.

In fact, the rise and fall of prices is similar to a rising tide lifts all boats. The rise in prices is inseparable from the promotion of capital. On the market, if there are more buyers than sellers, prices will rise. On the other hand, if more people sell than buy, the price will fall. With this concept, we can give reasonable expectations of the future price trend according to the supply and demand reflected by the net capital flow.

Third, the principle of capital flow

Different from the traditional analysis, the capital flow analysis analyzes which transactions are actively inflows and which are active outflows according to the transaction data of a period of time series. Then, we can know the net inflow of funds during that period by subtracting the active outflow from the active inflow of trading volume. If the net inflow of funds is positive, it means that the supply of the variety exceeds the demand; if the net outflow of funds, the supply of the variety exceeds the demand.

Read here, some people may wonder, in the actual transaction, some people buy and sell will be closed. The order of the transaction must be as much as there are purchases, and the amount of money in and out must be the same. How can there be capital inflows and outflows? In fact, strictly speaking, each purchase order must correspond to a corresponding sales order, capital inflow and capital outflow must be equal. If we want to figure out which orders are actively bought and which are actively sold, we can only use a compromise method, using bar data, according to volume and price.

IV. Calculation method of capital flow

The change of capital flow accurately corresponds to the real-time market behavior, and the net capital flow is calculated in real time by integrating bar data. There are two algorithms for calculating the proactive flow of funds:

First, if the transaction price of the current order is based on the counterparty price or excess price, the buying price > = selling price indicates that the buyer is more willing to complete the transaction at a higher price, that is, the active inflow of funds is included.

Second, if the current transaction price is higher than the last transaction price, then it can be understood that the current trading volume actively pushed up the price, that is, taking into account the active inflow of funds.

Take the above second algorithm as an example:

The closing price of a variety is 3450 at 10:00 and 3455 at 11:00, so we count the trading volume from 10:00 to 11:00 as the active inflow of funds. On the contrary, the active outflow of funds is included. On the basis of the second method, this paper adds the factor of price fluctuation, through the comparison of the closing price of bar before and after, the fluctuation of the trading volume of rising or falling bar is included in a series, and then the active inflow ratio of funds is further calculated according to this series.

V. transaction logic

This paper describes the capital flow of the futures market from the perspective of "quantity", and establishes a trading model to judge the short-term price trend through the real-time analysis of bar data. In general, capital flows and price trends can be divided into four basic situations:

Rising prices and net active inflows of funds per unit time: in this case, it is strong, and prices are more likely to continue to rise in the future.

The stock price rises, while the net outflow of capital initiative per unit time: in this case, it is medium-strong, and the rate at which prices will continue to rise in the future will be greatly reduced.

Falling stock prices and net active inflows of funds per unit time: in this case, it is weak, and prices are more likely to continue to fall in the future.

The stock price fell, while the net outflow of capital initiative per unit time: in this case, it is medium and weak, and the rate at which prices will continue to fall in the future will be greatly reduced.

The main variables are as follows:

Previous low (ll)

Previous high (hh)

Proactive Buy (barIn)

Proactive selling (barOut)

Ratio of active inflows to active outflows (barRatio)

Opening threshold (openValve)

Current position (myAmount)

Closing price of last K line (close)

Entry and exit conditions A good quantitative trading strategy not only requires stable returns, but also can control risks and avoid large losses when there is a small probability of time. Here we use the strategy of tracking the flow of active funds and analyze the direction of commodity futures with the help of short-term price forecasting, so as to achieve the effect of high return and low risk. The steps of the policy are shown below:

Long opening: if there is no current position and barRatio > openValve, buy and open

Short opening: if there is no current position, and barRatio

< 1 / openValve,卖出开仓; 多头平仓:如果当前持有多仓,并且close < ll,卖出平仓; 空头平仓:如果当前持有空仓,并且close >

Hh, buy and close the position.

VI. Write the source code of the strategy

Get and calculate the data

Function data () {var self = {}; var barVol = []; var bars = _ C (exchange.GetRecords); / / get bar data if (bars.length

< len * 2) { //控制bar数据数组的长度 return; } for (var i = len; i >

0; Close -) {var barSub_1 = bars [bars.length-(I + 1)] .Close-bars [bars.length-(I + 2)] .Close / / calculate the difference between the current closing price and the last bar closing price if (barSub_1 > 0) {/ / if the price goes up, add the positive number barVol.push (bars [bars.length-(I + 1)]. Volume * (bars [bars.length-(I + 1)]. High-bars [bars.length-(I + 1)] .Low);} else if (barSub_1)

< 0) { //如果价格跌了,就在数组里面添加负数 barVol.push(-bars[bars.length - (i + 1)].Volume * (bars[bars.length - (i + 1)].High - bars[bars.length - (i + 1)].Low)); } } if (barVol.length >

Len) {barVol.shift (); / / release excess data} self.barIn = 0; self.barOut = 0; for (var v = 0; v)

< barVol.length; v++) { if (barVol[v] >

0) {self.barIn + = barVol [v]; / merge all active inflows} else {self.barOut-= barVol [v]; / merge all active outflows}} self.barRatio = self.barIn / Math.abs (self.barOut); / / calculate the ratio of active inflows to active outflows bars.pop () / / Delete unfinished bar data self.close = bars [bars.length-1] .close; / / get the closing price of the last K line self.hh = TA.Highest (bars, hgLen, 'High'); / / get the front high self.ll = TA.Lowest (bars, hgLen,' Low'); / / get the front low return self;}

Through the inventor quantifying the GetRecords method in API, the bar data can be obtained directly. Including the highest price, the lowest price, the opening price, the closing price, the trading volume, the standard time stamp. If the latest transaction price is greater than the last transaction price, then the latest transaction volume * (highest price-lowest price) will be included in the active purchase; if the latest transaction price is less than the last transaction price, then the latest trading volume * (highest price-lowest price) will be included in the active sell.

Obtain position data

Function positions (name) {var self = {}; var mp = _ C (exchange.GetPosition); / / acquire position if (mp.length = = 0) {self.amount = 0;} for (var I = 0; I

< mp.length; i++) { //持仓数据处理 if (mp[i].ContractType == name) { if (mp[i].Type == PD_LONG || mp[i].Type == PD_LONG_YD) { self.amount = mp[i].Amount; } else if (mp[i].Type == PD_SHORT || mp[i].Type == PD_SHORT_YD) { self.amount = -mp[i].Amount; } self.profit = mp[i].Profit; } else { self.amount = 0; } } return self;} 通过发明者量化API中的GetPosition方法获取基础持仓数据,并对这些基础数据进一步处理,如果当前持有多单,那么就返回正持仓数量;如果当前持有空单,那么就返回负持仓数量。这样做的目的是方便计算开平仓逻辑。 下单交易 function trade() { var myData = data(); //执行data函数 if (!myData) { return; } var mp = positions(contractType); //获取持仓信息 var myAmount = mp.amount; //获取持仓数量 var myProfit = mp.profit; //获取持仓浮动盈亏 if (myAmount >

0 & & myData.close

< myData.ll) { p.Cover(contractType, unit); //多头平仓 } if (myAmount < 0 && myData.close >

MyData.hh) {p.Cover (contractType, unit); / / short closing} if (myAmount = = 0) {if (myData.barRatio > openValve) {p.OpenLong (contractType, unit); / / long opening} else if (myData.barRatio

< 1 / openValve) { p.OpenShort(contractType, unit); //空头开仓 } }}七、策略特点 特点: 核心参数少:模型设计思路清晰,核心参数只有3个。可优化空间很小,可以有效避免过度拟合。 较强的普适性:策略逻辑简单,具有高普适性,除农产品外适应大部分品种,可以进行多品种组合。 改进: 加入持仓量条件:单向(股票)市场资金流向可以根据价格涨跌、成交量等因素来界定资金的流入或流出。 但是,由于该策略并没有加入持仓量这个条件,使得统计主动性资金流向可能会失真。 加入标准差条件:仅仅依靠资金流向来作开仓条件,可能会出现频繁的虚假信号,造成频繁开平仓。通过统计指定时间内的资金净流出的平均值,上下加上标准差,来过滤虚假信号。 完整策略源码:/*backteststart: 2016-01-01 09:00:00end: 2019-12-31 15:00:00period: 1hexchanges: [{"eid":"Futures_CTP","currency":"FUTURES"}]*/var p = $.NewPositionManager(); //调用商品期货交易类库//持仓数据处理function positions(name) { var self = {}; var mp = _C(exchange.GetPosition); //获取持仓 if (mp.length == 0) { self.amount = 0; } for (var i = 0; i < mp.length; i++) { //持仓数据处理 if (mp[i].ContractType == name) { if (mp[i].Type == PD_LONG || mp[i].Type == PD_LONG_YD) { self.amount = mp[i].Amount; } else if (mp[i].Type == PD_SHORT || mp[i].Type == PD_SHORT_YD) { self.amount = -mp[i].Amount; } self.profit = mp[i].Profit; } else { self.amount = 0; } } return self;}//行情数据处理函数function data() { var self = {}; var barVol = []; var bars = _C(exchange.GetRecords); //获取bar数据 if (bars.length < len * 2) { //控制bar数据数组的长度 return; } for (var i = len; i >

0; Close -) {var barSub_1 = bars [bars.length-(I + 1)] .Close-bars [bars.length-(I + 2)] .Close / / calculate the difference between the current closing price and the last bar closing price if (barSub_1 > 0) {/ / if the price goes up, add the positive number barVol.push (bars [bars.length-(I + 1)]. Volume * (bars [bars.length-(I + 1)]. High-bars [bars.length-(I + 1)] .Low);} else if (barSub_1)

< 0) { //如果价格跌了,就在数组里面添加负数 barVol.push(-bars[bars.length - (i + 1)].Volume * (bars[bars.length - (i + 1)].High - bars[bars.length - (i + 1)].Low)); } } if (barVol.length >

Len) {barVol.shift (); / / release excess data} self.barIn = 0; self.barOut = 0; for (var v = 0; v)

< barVol.length; v++) { if (barVol[v] >

0) {self.barIn + = barVol [v]; / merge all active inflows} else {self.barOut-= barVol [v]; / merge all active outflows}} self.barRatio = self.barIn / Math.abs (self.barOut); / / calculate the ratio of active inflows to active outflows bars.pop () / / Delete the unfinished bar data self.close = bars [bars.length-1] .close; / / get the closing price of the last K line self.hh = TA.Highest (bars, hgLen, 'High'); / / get the front high self.ll = TA.Lowest (bars, hgLen,' Low'); / / get the front low return self;} / / transaction function function trade () {var myData = data () / / execute the data function if (! myData) {return;} var mp = positions (contractType); / / obtain position information var myAmount = mp.amount; / / obtain position quantity var myProfit = mp.profit; / / obtain position floating profit and loss if (myAmount > 0 & & myData.close

< myData.ll) { p.Cover(contractType, unit); //多头平仓 } if (myAmount < 0 && myData.close >

MyData.hh) {p.Cover (contractType, unit); / / short closing} if (myAmount = = 0) {if (myData.barRatio > openValve) {p.OpenLong (contractType, unit); / / long opening} else if (myData.barRatio < 1 / openValve) {p.OpenShort (contractType, unit) / / short opening} / / the main entry of the program, from here, launch function main () {while (true) {/ / enter the loop if (exchange.IO ("status")) {/ / if it is the opening time _ C (exchange.SetContractType, contractType); / / subscription contract trade () / / execute trade function}} 8. Policy backtest

Policy configuration: back-test performance:

Thank you for your reading, the above is the content of "how to achieve the trading strategy based on capital initiative flow in Java". After the study of this article, I believe you have a deeper understanding of how to realize the trading strategy based on capital initiative flow in Java, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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