Quickly implement a semi-automatic quantitative trading tool
In commodity futures trading, intertemporary arbitrage are a common trading method. This kind of arbitrage is not risk-free. When the unilateral direction of the spread continues to expand, the arbitrage position will be in a floating lose state. However, as long as the arbitrage position is properly controlled, it is still very operational and feasible.
In this article, we try to switch to another trading strategy, instead of constructing a fully automated trading strategy, we realized an interactive semi-automatic quantitative trading tool to make it easier to intertemporary arbitrage in commodity futures trading.
The development platform we will use the FMZ Quant platform. The focus of this article is on how to build semi-automatic strategies with interactive functions.
Intertemporal arbitrage is a very simple concept.
Intertemporal arbitrage concept
- Quote from Wikipedia
In economics and finance, arbitrage is the practice of taking advantage of a price difference between two or more markets: striking a combination of matching deals that capitalize upon the imbalance, the profit being the difference between the market prices at which the unit is traded. When used by academics, an arbitrage is a transaction that involves no negative cash flow at any probabilistic or temporal state and a positive cash flow in at least one state; in simple terms, it is the possibility of a risk-free profit after transaction costs. For example, an arbitrage opportunity is present when there is the opportunity to instantaneously buy something for a low price and sell it for a higher price.
Strategy Design
The strategy framework is as follows:
Function main(){ While(true){ If(exchange.IO("status")){ // Determine the connection status of the CTP protocol. LogStatus(_D(), "Already connected to CTP !") // Market Opening time, login connection is normal. } else { LogStatus(_D(), "CTP not connected!") // Not logged in to the trading front end. } } }
If the CTP protocol is connected properly, then we need to set up the trading contract and then get the market quote. After obtaining the quotes, we can use the FMZ Quant platform build-in "line drawing" library to draw the difference.
Function main(){ While(true){ If(exchange.IO("status")){ // Determine the connection status of the CTP protocol. exchange.SetContractType("rb2001") // Set the far month contract Var tickerA = exchange.GetTicker() // far-month contract quote data exchange.SetContractType("rb1910") // Set the near month contract Var tickerB = exchange.GetTicker() // near-month contract quote data Var diff = tickerA.Last - tickerB.Last $.PlotLine("diff", diff) LogStatus(_D(), "Already connected to CTP !") // Market Opening time, login connection is normal. } else { LogStatus(_D(), "CTP not connected!") // Not logged in to the trading front end. } } }
Get the market data, calculate the difference, and draw the graph to record. let it simply reflects the recent fluctuations in the price difference.
Use the function of "line drawing" library $.PlotLine
Interactive part
On the strategy editing page, you can add interactive controls directly to the strategy:
Use the function GetCommand
in the strategy code to capture the command that was sent to the robot after the above strategy control was triggered.
After the command is captured, different commands can be processed differently.
The trading part of the code can be packaged using the "Commodity Futures Trading Class Library" function. First, use var q = $.NewTaskQueue()
to generate the transaction control object q
(declared as a global variable).
var cmd = GetCommand() if (cmd) { if (cmd == "plusHedge") { q.pushTask(exchange, "rb2001", "sell", 1, function(task, ret) { Log(task.desc, ret) if (ret) { q.pushTask(exchange, "rb1910", "buy", 1, 123, function(task, ret) { Log("q", task.desc, ret, task.arg) }) } }) } else if (cmd == "minusHedge") { q.pushTask(exchange, "rb2001", "buy", 1, function(task, ret) { Log(task.desc, ret) if (ret) { q.pushTask(exchange, "rb1910", "sell", 1, 123, function(task, ret) { Log("q", task.desc, ret, task.arg) }) } }) } else if (cmd == "coverPlus") { q.pushTask(exchange, "rb2001", "closesell", 1, function(task, ret) { Log(task.desc, ret) if (ret) { q.pushTask(exchange, "rb1910", "closebuy", 1, 123, function(task, ret) { Log("q", task.desc, ret, task.arg) }) } }) } else if (cmd == "coverMinus") { q.pushTask(exchange, "rb2001", "closebuy", 1, function(task, ret) { Log(task.desc, ret) if (ret) { q.pushTask(exchange, "rb1910", "closesell", 1, 123, function(task, ret) { Log("q", task.desc, ret, task.arg) }) } }) } } q.poll()