# Equation solving problem

Asked by Matgoo on 8 Jun 2012
Latest activity Commented on by Sargondjani on 12 Jun 2012

HI everybody.

for my job i need to set up and solve an equation. I need to allocate trades to multiple account with the same average buy price. For example.

I bought 100 shares: 2 at 2.50 37 at 2.54 41 at 2.56 and 30 at 2.60. Then i need to allocate, out of the 100 shares, 40 shares to account1, 33 shares to account2, 7 shares to account3 and 20 shares to account4. - all of the account must have the same average buy price (2 to 3 digits must me equal, after it can vary.) - you can not divide a share, so only plain numbers. - all the shares must be allocated. - the algorithm should work with 10+ accounts and 50+ different shares prices.

it's 2 weeks I work on this, have approximate solutions, but the the correct equation and integration in matlab, I don't! see some help!

Thank you

Matgoo

## Products

No products are associated with this question.

Answer by Sargondjani on 8 Jun 2012

what do you have so far? or which part is difficult for you?

anyway, we have to know what your rule is to determine the allocation of shares. for example, if you have 10 people who should all get 0.6 share, but you have only 6 shares, what are you gonna do? (you have to give some 0 and some 1 share)

Matgoo on 10 Jun 2012

Hi Sargondjani,

Actually, you can't have less shares than accounts. The shares must be plain numbers.

I have for now an equation like this now:
syms a1 b1 c1 d1 f1 g1 h1 j1 k1 l1 m1 n1 o1 p1 q1 r1 s1 t1 u1 v1 w1 x1 y1 a2 b2 c2 d2 f2 g2 h2 j2 k2 l2 m2 n2 o2 p2 q2 r2 s2 t2 u2 v2 w2 x2 y2 a3 b3 c3 d3 f3 g3 h3 j3 k3 l3 m3 n3 o3 p3 q3 r3 s3 t3 u3 v3 w3 x3 y3 a4 b4 c4 d4 f4 g4 h4 j4 k4 l4 m4 n4 o4 p4 q4 r4 s4 t4 u4 v4 w4 x4 y4 a5 b5 c5 d5 f5 g5 h5 j5 k5 l5 m5 n5 o5 p5 q5 r5 s5 t5 u5 v5 w5 x5 y5 a6 b6 c6 d6 f6 g6 h6 j6 k6 l6 m6 n6 o6 p6 q6 r6 s6 t6 u6 v6 w6 x6 y6 a7 b7 c7 d7 f7 g7 h7 j7 k7 l7 m7 n7 o7 p7 q7 r7 s7 t7 u7 v7 w7 x7 y7 a8 b8 c8 d8 f8 g8 h8 j8 k8 l8 m8 n8 o8 p8 q8 r8 s8 t8 u8 v8 w8 x8 y8
solve((a2*5.1475+b2*5.15+c2*5.1525+d2*5.155+f2*5.1575+g2*5.16+h2*5.1625+j2*5.165+k2*5.1675+l2*5.17+m2*5.1725+n2*5.18+o2*5.1825+p2*5.185+q2*5.215+r2*5.2175+s2*5.22+t2*5.2225+u2*5.225+v2*5.2325+w2*5.2375+x2*5.24+y2*5.2475) / (a2+b2+c2+d2+f2+g2+h2+j2+k2+l2+m2+n2+o2+p2+q2+r2+s2+t2+u2+v2+w2+x2+y2)==(a1*5.1475+b1*5.15+c1*5.1525+d1*5.155+f1*5.1575+g1*5.16+h1*5.1625+j1*5.165+k1*5.1675+l1*5.17+m1*5.1725+n1*5.18+o1*5.1825+p1*5.185+q1*5.215+r1*5.2175+s1*5.22+t1*5.2225+u1*5.225+v1*5.2325+w1*5.2375+x1*5.24+y1*5.2475) / (a1+b1+c1+d1+f1+g1+h1+j1+k1+l1+m1+n1+o1+p1+q1+r1+s1+t1+u1+v1+w1+x1+y1),(a3*5.1475+b3*5.15+c3*5.1525+d3*5.155+f3*5.1575+g3*5.16+h3*5.1625+j3*5.165+k3*5.1675+l3*5.17+m3*5.1725+n3*5.18+o3*5.1825+p3*5.185+q3*5.215+r3*5.2175+s3*5.22+t3*5.2225+u3*5.225+v3*5.2325+w3*5.2375+x3*5.24+y3*5.2475) / (a3+b3+c3+d3+f3+g3+h3+j3+k3+l3+m3+n3+o3+p3+q3+r3+s3+t3+u3+v3+w3+x3+y3)==(a1*5.1475+b1*5.15+c1*5.1525+d1*5.155+f1*5.1575+g1*5.16+h1*5.1625+j1*5.165+k1*5.1675+l1*5.17+m1*5.1725+n1*5.18+o1*5.1825+p1*5.185+q1*5.215+r1*5.2175+s1*5.22+t1*5.2225+u1*5.225+v1*5.2325+w1*5.2375+x1*5.24+y1*5.2475) / (a1+b1+c1+d1+f1+g1+h1+j1+k1+l1+m1+n1+o1+p1+q1+r1+s1+t1+u1+v1+w1+x1+y1),(a4*5.1475+b4*5.15+c4*5.1525+d4*5.155+f4*5.1575+g4*5.16+h4*5.1625+j4*5.165+k4*5.1675+l4*5.17+m4*5.1725+n4*5.18+o4*5.1825+p4*5.185+q4*5.215+r4*5.2175+s4*5.22+t4*5.2225+u4*5.225+v4*5.2325+w4*5.2375+x4*5.24+y4*5.2475) / (a4+b4+c4+d4+f4+g4+h4+j4+k4+l4+m4+n4+o4+p4+q4+r4+s4+t4+u4+v4+w4+x4+y4)==(a1*5.1475+b1*5.15+c1*5.1525+d1*5.155+f1*5.1575+g1*5.16+h1*5.1625+j1*5.165+k1*5.1675+l1*5.17+m1*5.1725+n1*5.18+o1*5.1825+p1*5.185+q1*5.215+r1*5.2175+s1*5.22+t1*5.2225+u1*5.225+v1*5.2325+w1*5.2375+x1*5.24+y1*5.2475) / (a1+b1+c1+d1+f1+g1+h1+j1+k1+l1+m1+n1+o1+p1+q1+r1+s1+t1+u1+v1+w1+x1+y1),(a5*5.1475+b5*5.15+c5*5.1525+d5*5.155+f5*5.1575+g5*5.16+h5*5.1625+j5*5.165+k5*5.1675+l5*5.17+m5*5.1725+n5*5.18+o5*5.1825+p5*5.185+q5*5.215+r5*5.2175+s5*5.22+t5*5.2225+u5*5.225+v5*5.2325+w5*5.2375+x5*5.24+y5*5.2475) / (a5+b5+c5+d5+f5+g5+h5+j5+k5+l5+m5+n5+o5+p5+q5+r5+s5+t5+u5+v5+w5+x5+y5)==(a1*5.1475+b1*5.15+c1*5.1525+d1*5.155+f1*5.1575+g1*5.16+h1*5.1625+j1*5.165+k1*5.1675+l1*5.17+m1*5.1725+n1*5.18+o1*5.1825+p1*5.185+q1*5.215+r1*5.2175+s1*5.22+t1*5.2225+u1*5.225+v1*5.2325+w1*5.2375+x1*5.24+y1*5.2475) / (a1+b1+c1+d1+f1+g1+h1+j1+k1+l1+m1+n1+o1+p1+q1+r1+s1+t1+u1+v1+w1+x1+y1),(a6*5.1475+b6*5.15+c6*5.1525+d6*5.155+f6*5.1575+g6*5.16+h6*5.1625+j6*5.165+k6*5.1675+l6*5.17+m6*5.1725+n6*5.18+o6*5.1825+p6*5.185+q6*5.215+r6*5.2175+s6*5.22+t6*5.2225+u6*5.225+v6*5.2325+w6*5.2375+x6*5.24+y6*5.2475) / (a6+b6+c6+d6+f6+g6+h6+j6+k6+l6+m6+n6+o6+p6+q6+r6+s6+t6+u6+v6+w6+x6+y6)==(a1*5.1475+b1*5.15+c1*5.1525+d1*5.155+f1*5.1575+g1*5.16+h1*5.1625+j1*5.165+k1*5.1675+l1*5.17+m1*5.1725+n1*5.18+o1*5.1825+p1*5.185+q1*5.215+r1*5.2175+s1*5.22+t1*5.2225+u1*5.225+v1*5.2325+w1*5.2375+x1*5.24+y1*5.2475) / (a1+b1+c1+d1+f1+g1+h1+j1+k1+l1+m1+n1+o1+p1+q1+r1+s1+t1+u1+v1+w1+x1+y1),(a7*5.1475+b7*5.15+c7*5.1525+d7*5.155+f7*5.1575+g7*5.16+h7*5.1625+j7*5.165+k7*5.1675+l7*5.17+m7*5.1725+n7*5.18+o7*5.1825+p7*5.185+q7*5.215+r7*5.2175+s7*5.22+t7*5.2225+u7*5.225+v7*5.2325+w7*5.2375+x7*5.24+y7*5.2475) / (a7+b7+c7+d7+f7+g7+h7+j7+k7+l7+m7+n7+o7+p7+q7+r7+s7+t7+u7+v7+w7+x7+y7)==(a1*5.1475+b1*5.15+c1*5.1525+d1*5.155+f1*5.1575+g1*5.16+h1*5.1625+j1*5.165+k1*5.1675+l1*5.17+m1*5.1725+n1*5.18+o1*5.1825+p1*5.185+q1*5.215+r1*5.2175+s1*5.22+t1*5.2225+u1*5.225+v1*5.2325+w1*5.2375+x1*5.24+y1*5.2475) / (a1+b1+c1+d1+f1+g1+h1+j1+k1+l1+m1+n1+o1+p1+q1+r1+s1+t1+u1+v1+w1+x1+y1),(a8*5.1475+b8*5.15+c8*5.1525+d8*5.155+f8*5.1575+g8*5.16+h8*5.1625+j8*5.165+k8*5.1675+l8*5.17+m8*5.1725+n8*5.18+o8*5.1825+p8*5.185+q8*5.215+r8*5.2175+s8*5.22+t8*5.2225+u8*5.225+v8*5.2325+w8*5.2375+x8*5.24+y8*5.2475) / (a8+b8+c8+d8+f8+g8+h8+j8+k8+l8+m8+n8+o8+p8+q8+r8+s8+t8+u8+v8+w8+x8+y8)==(a1*5.1475+b1*5.15+c1*5.1525+d1*5.155+f1*5.1575+g1*5.16+h1*5.1625+j1*5.165+k1*5.1675+l1*5.17+m1*5.1725+n1*5.18+o1*5.1825+p1*5.185+q1*5.215+r1*5.2175+s1*5.22+t1*5.2225+u1*5.225+v1*5.2325+w1*5.2375+x1*5.24+y1*5.2475) / (a1+b1+c1+d1+f1+g1+h1+j1+k1+l1+m1+n1+o1+p1+q1+r1+s1+t1+u1+v1+w1+x1+y1))

etc etc.. this is an example with 8 account, and the 5.1475, 5.1525 etch are the prices of the shares that have been bought.
not sure if thats helps you!

So the difficult part for me is: to find the right equation (maybe this one is, but i'm not sure) and then to find a way to implement it is Matlab or another solver to get the results...

to follow on your answer, it might happen that 4 shares have been filled at 5.1234 and we have 8 account, so not all account will have some shares filled at that prices. that's another tricky part. The bottom line must be that all account must have the "as close as possible" average buy price for their shares.

Not sure i am clear enough, if not, tell me!

Thanks for your help, i really appreciate.

Matgoo

Sargondjani on 12 Jun 2012

can you explain what you are doing?

as for the rule to determine who gets what. i would do it like this:
-first round down (floor) all the shares each account should get, and give them those shares
-second, order the fractions that every account would get and then allocate shares starting at highest fraction... until all 'remaining' shares are allocated. this would discriminate between equal fractions by being higher in the list. the algorithm would be something like:
X= vector with the number of shares each account should get
N = number of shares you have.
-count number of remaining shares: Rem_N=N - sum(floor(X)):
-order X by their fractions (number after .)
-then round up (ceil) the first Rem_N and round down (floor) the remaining fractions