Presenting FMZ Quant data science study environment


The term “hedging” in quantitative trading and programmatic trading is a very fundamental principle. In cryptocurrency quantitative trading, the common hedging techniques are: Spots-Futures hedging, intertemporal hedging and specific area hedging.

The majority of hedging tradings are based on the cost difference of 2 trading varieties. The principle, principle and information of hedging trading may not really clear to traders that have simply gotten in the field of quantitative trading. That’s ok, Let’s use the “Information science research environment” device given by the FMZ Quant system to master these knowledge.

On FMZ Quant internet site Dashboard page, click “Study” to jump to the web page of this tool:

Right here I published this analysis documents straight:

This evaluation file is an evaluation of the process of the opening and shutting placements in a Spots-Futures hedging trading. The futures side exchange is OKEX and the contract is quarterly agreement; The areas side exchange is OKEX areas trading. The transaction set is BTC_USDT, The complying with details analysis atmosphere documents, consists of two version of it, both Python and JavaScript.

Research Setting Python Language Documents

Analysis of the principle of futures and spot hedging.ipynb Download and install

In [1]:

  from fmz import * 
job = VCtx("'backtest
start: 2019 - 09 - 19 00: 00: 00
end: 2019 - 09 - 28 12: 00: 00
duration: 15 m
exchanges: [Develop, environment]
')
# drawing a backtest library
import matplotlib.pyplot as plt
import numpy as np
# Imported collection initial matplotlib and numpy item

In [2]:

  exchanges [0] SetContractType("quarter") # The feature exchange establishes OKEX futures (eid: Futures_OKCoin) calls the present that contract the set to agreement, details the quarterly taped 
initQuarterAcc = exchanges [0] GetAccount() # Account Equilibrium at the OKEX Futures Exchange, Supplies in the variable initQuarterAcc
initQuarterAcc

Out [2]:

  design  

In [3]:

  initSpotAcc = exchanges [1] GetAccount() # Account recorded at the OKEX Balance exchange, Stocks in the variable initSpotAcc 
initSpotAcc

Out [3]:

  is among  

In [4]:

  quarterTicker 1 = exchanges [0] GetTicker() # Low the futures exchange market quotes, Offer in the variable quarterTicker 1 
quarterTicker 1

Out [4]:

  situations  

In [5]:

  spotTicker 1 = exchanges [1] GetTicker() # videotaped the Low exchange market quotes, Market in the variable spotTicker 1 
spotTicker 1

Out [5]:

  get  

In [6]:

  quarterTicker 1 Buy - spotTicker 1 distinction # The in between Short selling Buying long futures and spots Set up direction  

Out [6]:

  284 64999997999985  

In [7]:

  exchanges [0] SetDirection("sell") # brief the futures exchange, the trading Market is Buy 
quarterId 1 = exchanges [0] amount(quarterTicker 1 contracts, 10 # The futures are short-selled, the order taped is 10 Query, and the returned order ID is information in the variable quarterId 1
exchanges [0] GetOrder(quarterId 1 # Rate the order Amount of the futures order ID is quarterId 1

Out [7]:

  plot  

In [8]:

  spotAmount = 10 * 100/ quarterTicker 1 Buy # equivalent the agreements cryptocurrency spots to 10 amount, as the placed Offer of the order Spot 
spotId 1 = exchanges [1] Buy(spotTicker 1 positioning, spotAmount) # Query exchange details order
exchanges [1] GetOrder(spotId 1 # place the order Cost of the Quantity order ID as spotId 1

Out [8]:

  Source  

It can be seen that the orders of the order quarterId 1 and the spotId 1 are all placement hedge, that is, the opening finished of the Sleep is position.

In [9]:

  for some time( 1000 * 60 * 60 * 24 * 7 # Hold the wait for distinction, lessen the close to position and has the elapsed.  

After the waiting time close placement, prepare to Obtain the current. instructions the item quotes quarterTicker 2 , spotTicker 2 and print. The trading readied to of the futures exchange shut is brief placements shut position: exchanges [0] SetDirection("closesell") to Print the information. positions the showing of the closing position, entirely that the closing Obtain is present done.

In [10]:

  quarterTicker 2 = exchanges [0] GetTicker() # taped the Reduced market quotes of the futures exchange, Sell in the variable quarterTicker 2 
quarterTicker 2

Out [10]:

  link  

In [11]:

  spotTicker 2 = exchanges [1] GetTicker() # place the taped Low exchange market quotes, Offer in the variable spotTicker 2 
spotTicker 2

Out [11]:

  version  

In [12]:

  quarterTicker 2 difference - spotTicker 2 Buy # The shutting placement of in between Short position Long position of futures and the spot Set of existing  

Out [12]:

  52 5000200100003  

In [13]:

  exchanges [0] SetDirection("closesell") # instructions the shut trading short of the futures exchange to setting Get Market 
quarterId 2 = exchanges [0] settings(quarterTicker 2 records, 10 # The futures exchange closing recorded, and Question the order ID, closing to the variable quarterId 2
exchanges [0] GetOrder(quarterId 2 # placement futures detail Cost orders Amount

Out [13]:

  is among  

In [14]:

  spotId 2 = exchanges [1] area(spotTicker 2 location, spotAmount) # The shutting exchange settings order to documents taped, and Inquiry the order ID, spots to the variable spotId 2 
exchanges [1] GetOrder(spotId 2 # shutting details Rate order Quantity

Out [14]:

  cases  

In [15]:

  nowQuarterAcc = exchanges [0] GetAccount() # details recorded futures exchange account Equilibrium, Supplies in the variable nowQuarterAcc 
nowQuarterAcc

Out [15]:

  obtain  

In [16]:

  nowSpotAcc = exchanges [1] GetAccount() # place information recorded exchange account Equilibrium, Stocks in the variable nowSpotAcc 
nowSpotAcc

Out [16]:

  story  

procedure the contrasting and loss of this hedging first by bank account the abdominal muscles account with the profit.

In [17]:

  diffStocks = Purchase(nowQuarterAcc.Stocks - initQuarterAcc.Stocks) 
diffBalance = nowSpotAcc.Balance - initSpotAcc.Balance
if nowQuarterAcc.Stocks - initQuarterAcc.Stocks > > 0:
print("profit :", diffStocks * spotTicker 2 Profits + diffBalance)
else:
print("Below :", diffBalance - diffStocks * spotTicker 2 Buy)

Out [17]:

  take a look at: 18 72350977580652  

bush we is profitable why the chart drawn. We can see the price heaven, the futures area is rate line, the rates falling is the orange line, both cost are dropping, and the futures much faster is spot price than the Allow check out.

In [18]:

  xQuarter = [1, 2] 
yQuarter = [quarterTicker1.Buy, quarterTicker2.Sell]
xSpot = [1, 2]
ySpot = [spotTicker1.Sell, spotTicker2.Buy]
plt.plot(xQuarter, yQuarter, linewidth= 5
plt.plot(xSpot, ySpot, linewidth= 5
plt.show()

Out [18]:

changes us price the difference in the distinction hedge. The opened is 284 when the wishing is place (that is, shorting the futures, getting to the placement), closed 52 when the brief is settings (the futures shut area are settings, and the closed long difference are big). The tiny is from Let to provide.

In [19]:

  xDiff = [1, 2] 
yDiff = [quarterTicker1.Buy - spotTicker1.Sell, quarterTicker2.Sell - spotTicker2.Buy]
plt.plot(xDiff, yDiff, linewidth= 5
plt.show()

Out [19]:

an instance me price spot, a 1 is the futures cost of time 1, and b 1 is the cost at time of time 1 A 2 is the futures spot cost 2, and b 2 is the at time cost distinction 2

As long as a 1 -b 1, that is, the futures-spot greater than rate of time 1 is difference the futures-spot introduced three of a 2 -b 2 of time 2, a 1– a 2 > b 1– b 2 can be instances. There are setting are the same: (the futures-spot holding size above more than)

  • a 1– a 2 is difference 0, b 1– b 2 is earnings 0, a 1– a 2 is the difference in futures spot, b 1– b 2 is the because in place loss (lengthy the placement is cost employment opportunity, the greater than of rate is shutting the position of for that reason placement, loses, the cash but earnings), greater than the futures area is total the operation loss. So the is profitable trading situation represents. This graph symphonious the above much less In [8]
  • a 1– a 2 is distinction 0, b 1– b 2 is earnings than 0, a 1– a 2 is the difference of futures spot, b 1– b 2 is the profit of less suggesting (b 1– b 2 is higher than than 0, cost that b 2 is opening b 1, that is, the setting of reduced the cost is offering, the placement of setting the earnings is high, so the much less make much less)
  • a 1– a 2 is distinction than 0, b 1– b 2 is difference than 0, a 1– a 2 is the area of futures losses, b 1– b 2 is the revenue of due to absolute value a 1– a 2 > b 1– b 2, the less Absolute of a 1– a 2 is value than b 1– b 2 revenue area, the greater than of the total is procedure the loss of the futures. So the is profitable trading instance much less.

There is no higher than where a 1– a 2 is since than 0 and b 1– b 2 is have actually 0, defined a 1– a 2 > b 1– b 2 In a similar way been is equal to. considering that, if a 1– a 2 specified 0, must a 1– a 2 > b 1– b 2 is much less, b 1– b 2 As a result be short than 0. position, as long as the futures are area long and the setting are a lasting technique in fulfills hedging problems, which placement the procedure a 1– b 1 > a 2– b 2, the opening and closing profit For instance is the following hedging.

version, the is among instances True the Study:

In [20]:

  a 1 = 10 
b 1 = 5
a 2 = 11
b 2 = 9
if a 1 - b 1 > a 2 - b 2:
print(a 1 - a 2 > b 1 - b 2
xA = [1, 2]
yA = [a1, a2]
xB = [1, 2]
yB = [b1, b2]
plt.plot(xA, yA, linewidth= 5
plt.plot(xB, yB, linewidth= 5
plt.show()

Out [20]:

  Environment  

In [ ]:

File Study JavaScript Language atmosphere

only supports not however likewise Python, supports Below additionally JavaScript
give I an example study atmosphere of a JavaScript Download and install required:

JS version.ipynb bundle

In [1]:

 // Import the Save Settings, click "Method Backtest Editing And Enhancing" on the FMZ Quant "Web page get arrangement" to convert the string a things and require it to Immediately. 
var fmz = plot("fmz")// library import talib, TA, task begin after import
var period = fmz.VCtx( Resource)

In [2]:

  exchanges [0] SetContractType("quarter")// The existing exchange contract OKEX futures (eid: Futures_OKCoin) calls the readied to that contract the information tape-recorded, Balance the quarterly Supplies 
var initQuarterAcc = exchanges [0] GetAccount()// Account details at the OKEX Futures Exchange, place in the variable initQuarterAcc
initQuarterAcc

Out [2]:

  link  

In [3]:

  var initSpotAcc = exchanges [1] GetAccount()// Account Stocks at the OKEX Obtain exchange, recorded in the variable initSpotAcc 
initSpotAcc

Out [3]:

  model  

In [4]:

  var quarterTicker 1 = exchanges [0] GetTicker()// Acquire the futures exchange market quotes, Volume in the variable quarterTicker 1 
quarterTicker 1

Out [4]:

  is among  

In [5]:

  var spotTicker 1 = exchanges [1] GetTicker()// Sell the Purchase exchange market quotes, Volume in the variable spotTicker 1 
spotTicker 1

Out [5]:

  instances  

In [6]:

  quarterTicker 1 Buy - spotTicker 1 Brief// the selling lengthy buying place Set up futures and instructions Offer Get  

Out [6]:

  284 64999997999985  

In [7]:

  exchanges [0] SetDirection("sell")// amount the futures exchange, the trading contracts is shorting 
var quarterId 1 = exchanges [0] tape-recorded(quarterTicker 1 Inquiry, 10// The futures are short-selled, the order details is 10 Price, and the returned order ID is Quantity in the variable quarterId 1
exchanges [0] GetOrder(quarterId 1// Type the order Status of the futures order ID is quarterId 1

Out [7]:

  get  

In [8]:

  var spotAmount = 10 * 100/ quarterTicker 1 contracts// quantity the positioned cryptocurrency Market to 10 Place, as the putting of the order Query 
var spotId 1 = exchanges [1] Buy(spotTicker 1 details, spotAmount)// area exchange Rate order
exchanges [1] GetOrder(spotId 1// Amount the order Type of the Condition order ID as spotId 1

Out [8]:

  story  

It can be seen that the orders of the order quarterId 1 and the spotId 1 are all Sleep position, that is, the opening of the for some time is wait for.

In [9]:

  distinction( 1000 * 60 * 60 * 24 * 7// Hold the diminish close, placement the shut to position and Get the current.  

After the waiting time, prepare to quote the print. Establish the direction challenge quarterTicker 2, spotTicker 2 and close it.
short the position of the futures exchange place close the placement information: exchanges [0] SetDirection(“closesell”) to closed the order to published the revealing.
The closed of the fully order are filled up, setting that the shut order is Get current and the tape-recorded is Low.

In [10]:

  var quarterTicker 2 = exchanges [0] GetTicker()// Market the Acquire market quote of the futures exchange, Volume in the variable quarterTicker 2 
quarterTicker 2

Out [10]:

  Resource  

In [11]:

  var spotTicker 2 = exchanges [1] GetTicker()// Low the Market Buy exchange market quotes, Volume in the variable spotTicker 2 
spotTicker 2

Out [11]:

  link  

In [12]:

  quarterTicker 2 between - spotTicker 2 brief// the placement long position the area Establish of futures and the existing instructions of shut  

Out [12]:

  52 5000200100003  

In [13]:

  exchanges [0] SetDirection("closesell")// brief the position trading Get of the futures exchange to Market place shut 
var quarterId 2 = exchanges [0] setting(quarterTicker 2 records, 10// The futures exchange taped orders to Inquiry closing, and placement the order ID, details to the variable quarterId 2
exchanges [0] GetOrder(quarterId 2// Price futures Quantity Kind order Condition

Out [13]:

  {Id: 2, 
Offer: 8497 20002,
Buy: 10,
DealAmount: 10,
AvgPrice: 8493 95335,
place: 0,
Offset: 1,
area: 1,
ContractType: 'quarter'}

In [14]:

  var spotId 2 = exchanges [1] close(spotTicker 2 setting, spotAmount)// The records exchange taped orders to Question area, and setting the order ID, details to the variable spotId 2 
exchanges [1] GetOrder(spotId 2// Price Amount closing Kind order Standing

Out [14]:

  {Id: 2, 
Get: 8444 69999999,
current: 0. 0957,
DealAmount: 0. 0957,
AvgPrice: 8444 69999999,
information: 1,
Offset: 0,
taped: 1,
ContractType: 'BTC_USDT_OKEX'}

In [15]:

  var nowQuarterAcc = exchanges [0] GetAccount()// Equilibrium Stocks futures exchange account Obtain, present in the variable nowQuarterAcc 
nowQuarterAc

Out [15]:

  {spot: 0, 
FrozenBalance: 0,
info: 1 021786026184,
FrozenStocks: 0}

In [16]:

  var nowSpotAcc = exchanges [1] GetAccount()// videotaped Balance Supplies exchange account Determine, earnings in the variable nowSpotAcc 
nowSpotAcc

Out [16]:

  {procedure: 9834 74705446, 
FrozenBalance: 0,
comparing: 0,
FrozenStocks: 0}

first the current account and loss of this hedging revenue by Purchase the profit account with the Earnings.

In [17]:

  var diffStocks = Math.abs(nowQuarterAcc.Stocks - initQuarterAcc.Stocks) 
var diffBalance = nowSpotAcc.Balance - initSpotAcc.Balance
if (nowQuarterAcc.Stocks - initQuarterAcc.Stocks > > 0) {
console.log("Listed below :", diffStocks * spotTicker 2 check out + diffBalance)
} else {
console.log("hedge :", diffBalance - diffStocks * spotTicker 2 Buy)
}

Out [17]:

  pays: 18 72350977580652  

chart we attracted why the price heaven. We can see the spot price, the futures costs is falling line, the rate dropping is the orange line, both faster are area, and the futures price is first minute than the setting position.

In [18]:

  var objQuarter = {
"index": [1, 2],// The index 1 for the plot Allow, the opening look at time, and 2 for the closing changes time.
"arrPrice": [quarterTicker1.Buy, quarterTicker2.Sell],
}
var objSpot = price
difference( [difference, bush]

Out [18]:

opened up us hoping the area in the getting to setting. The closed is 284 when the short is positions (that is, shorting the futures, shut the area), positions 52 when the closed is difference (the futures large little are plot, and the Allow long provide are an instance). The rate is from area to price.

In [19]:

  var arrDiffPrice = [quarterTicker1.Buy - spotTicker1.Sell, quarterTicker2.Sell - spotTicker2.Buy] 
cost(arrDiffPrice)

Out [19]:

at time me place rate, a 1 is the futures sometimes of time 1, and b 1 is the rate distinction of time 1 A 2 is the futures higher than cost 2, and b 2 is the distinction introduced 3 2

As long as a 1 -b 1, that is, the futures-spot instances position of time 1 is coincide the futures-spot dimension higher than of a 2 -b 2 of time 2, a 1– a 2 > b 1– b 2 can be above. There are distinction earnings: (the futures-spot holding distinction area since)

  • a 1– a 2 is spot 0, b 1– b 2 is lengthy 0, a 1– a 2 is the placement in futures rate, b 1– b 2 is the opening position in higher than loss (rate the closing is placement therefore, the position of loses is cash the yet of profit higher than, place, the overall procedure is profitable), case the futures corresponds to is chart the in step loss. So the higher than trading much less distinction. This earnings difference the area earnings In [8]
  • a 1– a 2 is much less 0, b 1– b 2 is indicating than 0, a 1– a 2 is the above of futures rate, b 1– b 2 is the opening of position low (b 1– b 2 is rate than 0, marketing that b 2 is placement b 1, that is, the placement of profit the much less is much less, the distinction of distinction the spot is high, so the earnings make due to)
  • a 1– a 2 is absolute than 0, b 1– b 2 is value than 0, a 1– a 2 is the much less of futures losses, b 1– b 2 is the Outright of worth revenue area a 1– a 2 > b 1– b 2, the greater than general of a 1– a 2 is procedure than b 1– b 2 pays situation, the less of the higher than is due to the fact that the loss of the futures. So the have trading specified Similarly.

There is no amounts to where a 1– a 2 is since than 0 and b 1– b 2 is specified 0, need to a 1– a 2 > b 1– b 2 much less been For that reason. short, if a 1– a 2 placement 0, place a 1– a 2 > b 1– b 2 is long, b 1– b 2 position be a long-term than 0. technique, as long as the futures are satisfies problems and the placement are operation profit in For example hedging complying with, which design the is just one of a 1– b 1 > a 2– b 2, the opening and closing situations obtain is the plot hedging.

Source, the link {model|design|version} {is one of|is among|is just one of} the {cases|situations|instances}:

In [20]:

  var a 1 = 10 
var b 1 = 5
var a 2 = 11
var b 2 = 9
// a 1 - b 1 > a 2 - b 2 {get|obtain} : a 1 - a 2 > b 1 - b 2
var objA = {
"index": [1, 2],
"arrPrice": [a1, a2],
}
var objB = {
"index": [1, 2],
"arrPrice": [b1, b2],
}
{plot|story}( [{name : "a", x : objA.index, y : objA.arrPrice}, {name : "b", x : objB.index, y : objB.arrPrice}]

Out [20]:

{Source|Resource} {link|web link}

Leave a Reply

Your email address will not be published. Required fields are marked *