github.com/vanclief/finmod@v0.12.10/market/orderbook_test.go (about)

     1  package market
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestNewOrderBook(t *testing.T) {
    11  	bids := []OrderBookRow{
    12  		{
    13  			Price:  5,
    14  			Volume: 1.3,
    15  		},
    16  		{
    17  			Price:  2,
    18  			Volume: 1,
    19  		},
    20  		{
    21  			Price:  3,
    22  			Volume: 1,
    23  		},
    24  		{
    25  			Price:  4,
    26  			Volume: 1,
    27  		},
    28  		{
    29  			Price:  7,
    30  			Volume: 1,
    31  		},
    32  		{
    33  			Price:  6,
    34  			Volume: 1,
    35  		},
    36  		{
    37  			Price:  1,
    38  			Volume: 1,
    39  		},
    40  		{
    41  			Price:  8,
    42  			Volume: 1,
    43  		},
    44  		{
    45  			Price:  9,
    46  			Volume: 1,
    47  		},
    48  	}
    49  
    50  	asks := []OrderBookRow{
    51  		{
    52  			Price:  20,
    53  			Volume: 1,
    54  		},
    55  		{
    56  			Price:  19,
    57  			Volume: 1,
    58  		},
    59  		{
    60  			Price:  18,
    61  			Volume: 1,
    62  		},
    63  		{
    64  			Price:  17,
    65  			Volume: 1,
    66  		},
    67  		{
    68  			Price:  16,
    69  			Volume: 1,
    70  		},
    71  		{
    72  			Price:  15,
    73  			Volume: 1,
    74  		},
    75  		{
    76  			Price:  14,
    77  			Volume: 1,
    78  		},
    79  		{
    80  			Price:  13,
    81  			Volume: 1,
    82  		},
    83  		{
    84  			Price:  12,
    85  			Volume: 1,
    86  		},
    87  		{
    88  			Price:  11,
    89  			Volume: 1,
    90  		},
    91  	}
    92  
    93  	ob := NewOrderBook(asks, bids, 5)
    94  
    95  	ob.Print()
    96  
    97  	ob = NewOrderBook([]OrderBookRow{}, []OrderBookRow{}, 5)
    98  }
    99  
   100  func TestApplyUpdate(t *testing.T) {
   101  	bids := []OrderBookRow{
   102  		{
   103  			Price:  1,
   104  			Volume: 1.3,
   105  		},
   106  		{
   107  			Price:  2,
   108  			Volume: 1,
   109  		},
   110  		{
   111  			Price:  3,
   112  			Volume: 1,
   113  		},
   114  		{
   115  			Price:  4,
   116  			Volume: 1,
   117  		},
   118  		{
   119  			Price:  5,
   120  			Volume: 1,
   121  		},
   122  	}
   123  
   124  	asks := []OrderBookRow{
   125  		{
   126  			Price:  12,
   127  			Volume: 1,
   128  		},
   129  		{
   130  			Price:  11,
   131  			Volume: 1,
   132  		},
   133  		{
   134  			Price:  10,
   135  			Volume: 1,
   136  		},
   137  		{
   138  			Price:  9,
   139  			Volume: 1,
   140  		},
   141  		{
   142  			Price:  8,
   143  			Volume: 1,
   144  		},
   145  		{
   146  			Price:  7,
   147  			Volume: 1,
   148  		},
   149  	}
   150  
   151  	ob := NewOrderBook(asks, bids, 5)
   152  
   153  	update := OrderBookUpdate{13, 1, "bid"}
   154  	err := ob.ApplyUpdate(update)
   155  	assert.Nil(t, err)
   156  
   157  	//update = OrderBookUpdate{3.5, 1.4, "bid"}
   158  	//err = ob.ApplyUpdate(update)
   159  	//assert.Nil(t, err)
   160  	//
   161  	//update = OrderBookUpdate{7.5, 2, "ask"}
   162  	//err = ob.ApplyUpdate(update)
   163  	//assert.Nil(t, err)
   164  	//
   165  	//update = OrderBookUpdate{8, 0, "ask"}
   166  	//err = ob.ApplyUpdate(update)
   167  	//assert.Nil(t, err)
   168  	//
   169  	//update = OrderBookUpdate{7, 3, "ask"}
   170  	//err = ob.ApplyUpdate(update)
   171  	//assert.Nil(t, err)
   172  
   173  	ob.Print()
   174  }
   175  
   176  func BenchmarkApplyUpdate(t *testing.B) {
   177  	bids := []OrderBookRow{
   178  		{
   179  			Price:  1,
   180  			Volume: 1.3,
   181  		},
   182  		{
   183  			Price:  2,
   184  			Volume: 1,
   185  		},
   186  		{
   187  			Price:  3,
   188  			Volume: 1,
   189  		},
   190  		{
   191  			Price:  4,
   192  			Volume: 1,
   193  		},
   194  		{
   195  			Price:  5,
   196  			Volume: 1,
   197  		},
   198  	}
   199  
   200  	asks := []OrderBookRow{
   201  		{
   202  			Price:  12,
   203  			Volume: 1,
   204  		},
   205  		{
   206  			Price:  11,
   207  			Volume: 1,
   208  		},
   209  		{
   210  			Price:  10,
   211  			Volume: 1,
   212  		},
   213  		{
   214  			Price:  9,
   215  			Volume: 1,
   216  		},
   217  		{
   218  			Price:  8,
   219  			Volume: 1,
   220  		},
   221  		{
   222  			Price:  7,
   223  			Volume: 1,
   224  		},
   225  	}
   226  
   227  	ob := NewOrderBook(asks, bids, 5)
   228  
   229  	update := OrderBookUpdate{1.5, 1, "bid"}
   230  	err := ob.ApplyUpdate(update)
   231  	assert.Nil(t, err)
   232  
   233  	update = OrderBookUpdate{3.5, 1.4, "bid"}
   234  	err = ob.ApplyUpdate(update)
   235  	assert.Nil(t, err)
   236  
   237  	update = OrderBookUpdate{7.5, 2, "ask"}
   238  	err = ob.ApplyUpdate(update)
   239  	assert.Nil(t, err)
   240  
   241  	update = OrderBookUpdate{8, 0, "ask"}
   242  	err = ob.ApplyUpdate(update)
   243  	assert.Nil(t, err)
   244  
   245  	update = OrderBookUpdate{7, 3, "ask"}
   246  	err = ob.ApplyUpdate(update)
   247  	assert.Nil(t, err)
   248  
   249  	//ob.Print()
   250  }
   251  
   252  func TestFillOrderBook(t *testing.T) {
   253  
   254  	ob := NewOrderBook([]OrderBookRow{}, []OrderBookRow{}, 5)
   255  
   256  	update := OrderBookUpdate{7.5, 2, "ask"}
   257  	err := ob.ApplyUpdate(update)
   258  	assert.Nil(t, err)
   259  
   260  	update = OrderBookUpdate{6, 2, "bid"}
   261  	err = ob.ApplyUpdate(update)
   262  	assert.Nil(t, err)
   263  
   264  	ob.Print()
   265  }
   266  
   267  func TestOrderBookGetDepth(t *testing.T) {
   268  	var firstOB, secondOB OrderBook
   269  	firstOB = firstOrderBook()
   270  	secondOB = secondOrderBook()
   271  	result, err := CalculateOverlap(firstOB, secondOB)
   272  	assert.Nil(t, err)
   273  	assert.True(t, result-4.9 < 0.01)
   274  
   275  	secondOB = firstOrderBook()
   276  	firstOB = secondOrderBook()
   277  	result, err = CalculateOverlap(firstOB, secondOB)
   278  	assert.Nil(t, err)
   279  	assert.True(t, result-4.9 < 0.01)
   280  
   281  	thirdOB := thirdOrderBook()
   282  	fourthOB := fourthOrderBook()
   283  	result, err = CalculateOverlap(thirdOB, fourthOB)
   284  	assert.Nil(t, err)
   285  	assert.True(t, result-4.1 < 0.01)
   286  
   287  	fourthOB = thirdOrderBook()
   288  	thirdOB = fourthOrderBook()
   289  	result, err = CalculateOverlap(fourthOB, thirdOB)
   290  	assert.Nil(t, err)
   291  	assert.True(t, result-4.1 < 0.01)
   292  }
   293  
   294  func getTestOrderBook() OrderBook {
   295  	return OrderBook{
   296  		Time: time.Now().Unix(),
   297  		Asks: []OrderBookRow{
   298  			{
   299  				Price:       0,
   300  				Volume:      1,
   301  				AccumVolume: 1,
   302  			},
   303  			{
   304  				Price:       1,
   305  				Volume:      1,
   306  				AccumVolume: 2,
   307  			},
   308  			{
   309  				Price:       2,
   310  				Volume:      1,
   311  				AccumVolume: 3,
   312  			},
   313  			{
   314  				Price:       3,
   315  				Volume:      1,
   316  				AccumVolume: 4,
   317  			},
   318  			{
   319  				Price:       4,
   320  				Volume:      1,
   321  				AccumVolume: 5,
   322  			},
   323  			{
   324  				Price:       5,
   325  				Volume:      1,
   326  				AccumVolume: 6,
   327  			},
   328  			{
   329  				Price:       6,
   330  				Volume:      1,
   331  				AccumVolume: 7,
   332  			},
   333  			{
   334  				Price:       7,
   335  				Volume:      1,
   336  				AccumVolume: 8,
   337  			},
   338  			{
   339  				Price:       8,
   340  				Volume:      1,
   341  				AccumVolume: 9,
   342  			},
   343  			{
   344  				Price:       9,
   345  				Volume:      1,
   346  				AccumVolume: 10,
   347  			},
   348  		},
   349  		Bids: []OrderBookRow{
   350  			{
   351  				Price:       20,
   352  				Volume:      1,
   353  				AccumVolume: 1,
   354  			},
   355  			{
   356  				Price:       19,
   357  				Volume:      1,
   358  				AccumVolume: 2,
   359  			},
   360  			{
   361  				Price:       18,
   362  				Volume:      1,
   363  				AccumVolume: 3,
   364  			},
   365  			{
   366  				Price:       17,
   367  				Volume:      1,
   368  				AccumVolume: 4,
   369  			},
   370  			{
   371  				Price:       16,
   372  				Volume:      1,
   373  				AccumVolume: 5,
   374  			},
   375  			{
   376  				Price:       15,
   377  				Volume:      1,
   378  				AccumVolume: 6,
   379  			},
   380  			{
   381  				Price:       14,
   382  				Volume:      1,
   383  				AccumVolume: 7,
   384  			},
   385  			{
   386  				Price:       13,
   387  				Volume:      1,
   388  				AccumVolume: 8,
   389  			},
   390  			{
   391  				Price:       12,
   392  				Volume:      1,
   393  				AccumVolume: 9,
   394  			},
   395  			{
   396  				Price:       11,
   397  				Volume:      1,
   398  				AccumVolume: 10,
   399  			},
   400  		},
   401  	}
   402  }
   403  
   404  func firstOrderBook() OrderBook {
   405  	asks := []OrderBookRow{
   406  		{
   407  			Price:  11,
   408  			Volume: 1,
   409  		},
   410  		{
   411  			Price:  12,
   412  			Volume: 1.5,
   413  		},
   414  		{
   415  			Price:  13,
   416  			Volume: 2.5,
   417  		},
   418  		{
   419  			Price:  14,
   420  			Volume: 3,
   421  		},
   422  		{
   423  			Price:  15,
   424  			Volume: 4.5,
   425  		},
   426  		{
   427  			Price:  16,
   428  			Volume: 5.5,
   429  		},
   430  	}
   431  
   432  	bids := []OrderBookRow{
   433  		{
   434  			Price:  9,
   435  			Volume: 0.5,
   436  		},
   437  		{
   438  			Price:  8,
   439  			Volume: 1.5,
   440  		},
   441  		{
   442  			Price:  7,
   443  			Volume: 2,
   444  		},
   445  		{
   446  			Price:  6,
   447  			Volume: 2.5,
   448  		},
   449  		{
   450  			Price:  5,
   451  			Volume: 3.5,
   452  		},
   453  		{
   454  			Price:  4,
   455  			Volume: 5,
   456  		},
   457  	}
   458  	return OrderBook{
   459  		Time:     time.Now().Unix(),
   460  		Asks:     asks,
   461  		Bids:     bids,
   462  		MaxDepth: 6,
   463  	}
   464  }
   465  
   466  func secondOrderBook() OrderBook {
   467  	asks := []OrderBookRow{
   468  		{
   469  			Price:  15,
   470  			Volume: 1,
   471  		},
   472  		{
   473  			Price:  16,
   474  			Volume: 1,
   475  		},
   476  		{
   477  			Price:  17,
   478  			Volume: 1,
   479  		},
   480  
   481  		{
   482  			Price:  18,
   483  			Volume: 1,
   484  		},
   485  		{
   486  			Price:  19,
   487  			Volume: 1,
   488  		},
   489  		{
   490  			Price:  20,
   491  			Volume: 1,
   492  		},
   493  	}
   494  
   495  	bids := []OrderBookRow{
   496  		{
   497  			Price:  13,
   498  			Volume: 0.7,
   499  		},
   500  		{
   501  			Price:  12,
   502  			Volume: 2.1,
   503  		},
   504  		{
   505  			Price:  11,
   506  			Volume: 3.4,
   507  		},
   508  		{
   509  			Price:  10,
   510  			Volume: 4.2,
   511  		},
   512  		{
   513  			Price:  9,
   514  			Volume: 5.1,
   515  		},
   516  		{
   517  			Price:  8,
   518  			Volume: 6,
   519  		},
   520  	}
   521  
   522  	return OrderBook{
   523  		Time:     time.Now().Unix(),
   524  		Asks:     asks,
   525  		Bids:     bids,
   526  		MaxDepth: 6,
   527  	}
   528  }
   529  
   530  func thirdOrderBook() OrderBook {
   531  	asks := []OrderBookRow{
   532  		{
   533  			Price:  11,
   534  			Volume: 2,
   535  		},
   536  		{
   537  			Price:  12,
   538  			Volume: 3,
   539  		},
   540  		{
   541  			Price:  13,
   542  			Volume: 3.5,
   543  		},
   544  		{
   545  			Price:  14,
   546  			Volume: 5,
   547  		},
   548  		{
   549  			Price:  15,
   550  			Volume: 6.2,
   551  		},
   552  		{
   553  			Price:  16,
   554  			Volume: 7.1,
   555  		},
   556  	}
   557  
   558  	bids := []OrderBookRow{
   559  		{
   560  			Price:  9,
   561  			Volume: 2,
   562  		},
   563  		{
   564  			Price:  8,
   565  			Volume: 3,
   566  		},
   567  		{
   568  			Price:  7,
   569  			Volume: 3.2,
   570  		},
   571  		{
   572  			Price:  6,
   573  			Volume: 3.6,
   574  		},
   575  		{
   576  			Price:  5,
   577  			Volume: 4.1,
   578  		},
   579  		{
   580  			Price:  4,
   581  			Volume: 5.2,
   582  		},
   583  	}
   584  	return OrderBook{
   585  		Time:     time.Now().Unix(),
   586  		Asks:     asks,
   587  		Bids:     bids,
   588  		MaxDepth: 6,
   589  	}
   590  }
   591  
   592  func fourthOrderBook() OrderBook {
   593  	asks := []OrderBookRow{
   594  		{
   595  			Price:  7,
   596  			Volume: 1,
   597  		},
   598  		{
   599  			Price:  8,
   600  			Volume: 1.3,
   601  		},
   602  		{
   603  			Price:  9,
   604  			Volume: 1.9,
   605  		},
   606  
   607  		{
   608  			Price:  10,
   609  			Volume: 2.4,
   610  		},
   611  		{
   612  			Price:  11,
   613  			Volume: 3.2,
   614  		},
   615  		{
   616  			Price:  12,
   617  			Volume: 4.3,
   618  		},
   619  	}
   620  
   621  	bids := []OrderBookRow{
   622  		{
   623  			Price:  5,
   624  			Volume: 0.7,
   625  		},
   626  		{
   627  			Price:  4,
   628  			Volume: 2.1,
   629  		},
   630  		{
   631  			Price:  3,
   632  			Volume: 3.4,
   633  		},
   634  		{
   635  			Price:  2,
   636  			Volume: 4.2,
   637  		},
   638  		{
   639  			Price:  1,
   640  			Volume: 5.1,
   641  		},
   642  		{
   643  			Price:  0.5,
   644  			Volume: 6,
   645  		},
   646  	}
   647  
   648  	return OrderBook{
   649  		Time:     time.Now().Unix(),
   650  		Asks:     asks,
   651  		Bids:     bids,
   652  		MaxDepth: 6,
   653  	}
   654  }