cbind(t(to_buy_from), t(to_sell_to))
[,1] [,2] [,3] [,4]
[1,] 61.7050 190 63.170 2500
[2,] 61.7500 29 63.150 799
[3,] 61.8050 166 63.110 500
[4,] 61.8950 166 63.060 10000
[5,] 61.9450 166 63.020 7840
[6,] 61.9805 6150 62.995 2000
[7,] 62.0000 3069 62.930 2000
[8,] 62.0600 166 62.860 10811
[9,] 62.1100 166 62.780 18054
[10,] 62.1450 166 62.755 9000
[11,] 62.1750 166 62.690 10960
[12,] 62.2250 166 62.635 100
[13,] 62.2450 166 62.585 2380
[14,] 62.2720 100 62.550 2119
[15,] 62.2830 4000 62.525 108091
[16,] 62.2875 100 62.505 2000
[17,] 62.2955 100 62.485 816
[18,] 62.3250 307 62.435 600
[19,] 62.3800 2906 62.400 300
[20,] 62.3940 1969 62.375 4611
[21,] 62.4250 166 62.355 5111
[22,] 62.4505 2000 62.335 1969
[23,] 62.4700 259 62.315 500
[24,] 62.4755 50 62.250 5142
[25,] 62.4800 166 62.165 660
[26,] 62.4935 305 62.115 2428
[27,] 62.4975 7786 62.085 779
[28,] 62.4995 50049 62.050 12811
[29,] 62.5045 914 62.015 192
[30,] 62.5150 1110 61.975 1200
[31,] 62.5285 400 61.895 40000
[32,] 62.5500 6352 61.835 100
[33,] 62.5750 9 61.775 133
[34,] 62.6000 394 61.750 7723
For the simpliest case, if the King had commanded that the minimum profit
should be 2.3742%, which is equal to 63.170/61.7050 (look at the first row),
then you can easily project that 190 quantity of goods will be dealt (the
minmum of [1,2] and [1,4]), and that the actual profit is 2.3742%.
If the king, however, has commanded that a deal should only be carried out if
the profit is higher than 2%, the calculation will be more complicated. I
don't know the right method, but I can demonstrate the wrong method and
explain why it is wrong.
The wrong approach is the following:
The idea is to write a function that asks how much volume (total quantity)
you want to deal, and returns the profit. This generates a relationship
between volume and profit, and with interpolation you can get the volumen for
any given minimum-profit requirement.
revenues <- function(open_orders, volumes) {
# calculate revenue using a list of open orders and desirable "volumes" of
goods
# expecting volumnes as a vector, to test the revenue (total amont of money)
# for each volume (total amount of goods to deal) in the 'volumes'
volume <- sapply(1:length(open_orders[2,]),
function(x) { sum(open_orders[2,1:x])})
revenue <- sapply(1:length(open_orders[2,]),
function(x) { sum(open_orders[1, 1:x] * open_orders[2,1:x])})
i <- findInterval(volumes, c(0, volume))
c(0, revenue)[i] + c(open_orders[1,], 0)[i]*(
volumes - c(0, volume)[i])
}
data.frame(volume = volumes, profit = revenues(to_sell_to, volumes) /
revenues(to_buy_from, volumes) - 1)
With the above routine, let us test the profit with the following volumes:
volumes = c(10, 100, 500, 1000, 5000, 10000, 30000, 50000, 70000, 90000)
And the result:
data.frame(volume = volumes, profit = revenues(to_sell_to, volumes) /
+ revenues(to_buy_from, volumes) - 1)
volume profit
1 10 0.023741938
2 100 0.023741938
3 500 0.022424508
4 1000 0.020974612
5 5000 0.018972785
6 10000 0.018087976
7 30000 0.012223652
8 50000 0.009288480
9 70000 0.007729286
10 90000 0.006204251