/
Transactions.cs
684 lines (610 loc) · 25.8 KB
/
Transactions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
using System;
using System.Diagnostics;
using System.Collections;
// Change history
// 20-Aug-2008: Initialize the matchfound variable to false in the for loop to take care of the situation where a
// transaction is partially matched and no further matching transaction is found. In that case a
// NoMatchFound event should be raised for the balance quantity in the partially matched transaction.
namespace Helpers
{
public interface IStockMatch
{
// Called once before any matching is done
void BeginOperation();
// Called once for each stock before any matching is done
void BeginStock(string stock);
// Called once for each transaction for which a match will be searched.
void BeginMatch(SingleTransaction s);
// Called each time no match is found for a transaction
void NoMatchFound(SingleTransaction s);
// Called each time a matching transaction is found
void MatchFound(SingleTransaction first, SingleTransaction matched, bool firstPartialMatch, bool secondPartialMatch);
// Called once for each stock after all transactions are processed.
void EndStock(string stock);
// Called once when the operation is about to end.
void EndOperation();
}
/// <summary>
/// Summary description for SingleTransaction.
/// </summary>
public class SingleTransaction: IComparable
{
public enum eTransactionType
{
Buy = 0,
Add,
Remove,
Sell,
None,
MaxTranType};
internal eTransactionType transactionType;
internal DateTime transactionDate;
internal string transactionStockCode;
internal long transactionQty;
internal decimal transactionPrice;
internal decimal transactionCharges;
internal bool transactionReconciledFlag;
internal string transactionLotIdentifier;
public string StockCode
{
get
{
return transactionStockCode;
}
}
public decimal UnitCharges
{
get
{
return this.transactionCharges / this.transactionQty;
}
}
public decimal TransactionPrice
{
get
{
return transactionPrice;
}
}
public DateTime TransactionDate
{
get
{
return transactionDate;
}
}
public long TransactionQty
{
get
{
return transactionQty;
}
}
public SingleTransaction()
{
transactionType = SingleTransaction.eTransactionType.None;
transactionQty = 0;
transactionPrice = 0.0M;
transactionCharges = 0.0M;
transactionReconciledFlag = false;
transactionLotIdentifier = "";
transactionStockCode = "";
transactionDate = DateTime.Now;
}
public SingleTransaction(SingleTransaction.eTransactionType tType, DateTime tDate, string tStockCode, long tQty, decimal tPrice, decimal tCharges,string tlotId)
{
transactionType = tType;
transactionStockCode = tStockCode;
transactionQty = tQty;
transactionPrice = tPrice;
transactionCharges = tCharges;
transactionDate = tDate;
transactionReconciledFlag = false;
transactionLotIdentifier = tlotId;
Debug.WriteLine (this, "Constructor of SingleTransaction called");
}
public bool IsAcquisition()
{
return (transactionType == eTransactionType.Buy || transactionType == eTransactionType.Add);
}
public bool IsAddition()
{
return (transactionType == eTransactionType.Add);
}
public bool IsDisposal()
{
return (transactionType == eTransactionType.Sell || transactionType == eTransactionType.Remove);
}
public bool IsRemoval()
{
return (transactionType == eTransactionType.Remove);
}
public bool IsOppositeType(eTransactionType e)
{
return (((e == eTransactionType.Buy || e == eTransactionType.Add) && this.IsDisposal())||
((e == eTransactionType.Sell || e == eTransactionType.Remove) && this.IsAcquisition()));
}
public void Dump()
{
Console.WriteLine("{0,6} {1,15:d} {2,-6} {3,6} {4,20:f} {5,20:f} {6}", transactionStockCode, transactionDate, transactionType.ToString(), transactionQty, transactionPrice, transactionQty * transactionPrice + transactionCharges, transactionLotIdentifier);
}
#region IComparable Members
public int CompareTo(object obj)
{
if (obj is SingleTransaction)
{
SingleTransaction thisTrans = (SingleTransaction) obj;
if (transactionStockCode != thisTrans.transactionStockCode)
return transactionStockCode.CompareTo(thisTrans.transactionStockCode);
int ret = transactionDate.CompareTo(thisTrans.transactionDate);
if (ret != 0)
return ret;
ret = (int)transactionType - (int)thisTrans.transactionType;
if (ret != 0)
return ret;
return transactionLotIdentifier.CompareTo(thisTrans.transactionLotIdentifier);
}
throw new ArgumentException("Object is not SingleTransaction");
}
#endregion
};
public class Account
{
SingleTransaction[] acTransactions;
public Account(int numTrans)
{
if (0 == numTrans)
numTrans = 256;
acTransactions = (SingleTransaction[])Array.CreateInstance(typeof(SingleTransaction),numTrans);
}
int lastTransactionUsed = 0;
public int AddTransaction(SingleTransaction.eTransactionType ttype, DateTime tDate, string tStockCode, long tQty, decimal tPrice, decimal tCharges, string tlotId)
{
// Validations
// Create a new transaction
SingleTransaction thisTrans = new SingleTransaction(ttype,tDate,tStockCode,tQty, tPrice, tCharges, tlotId);
acTransactions[lastTransactionUsed++] = thisTrans;
if (lastTransactionUsed >= acTransactions.GetLength(0))
return -1;
return 0;
} // AddTransaction
public void Dump()
{
foreach (SingleTransaction s in acTransactions)
if (s != null)
s.Dump();
} // Dump
public ArrayList GetStockList()
{
int i = 0;
int numtrans = lastTransactionUsed;
ArrayList stocklist = new ArrayList();
string thisStockName = "";
for (i = 0; i < numtrans; i++)
{
SingleTransaction thisTrans = acTransactions[i];
bool bStockHasBeenProcessed = false;
if (thisStockName == "")
{
//Moving to new stock code. Check if this has been processed already
foreach (String s in stocklist)
{
if (thisTrans.transactionStockCode == s)
{
bStockHasBeenProcessed = true;
break;
}
}
}
if (bStockHasBeenProcessed)
continue;
// this stock has not been processed.
thisStockName = thisTrans.transactionStockCode;
// Done processing all transactions.
stocklist.Add(thisStockName);
thisStockName = "";
}
return stocklist;
} //GetStockList
public void StockTaking(DateTime asofDate, bool bShowZeroBalance)
{
int i = 0;
int numtrans = lastTransactionUsed;
ArrayList stocklist = new ArrayList();
string thisStockName = "";
for (i=0;i<numtrans;i++)
{
SingleTransaction thisTrans = acTransactions[i];
bool bStockHasBeenProcessed = false;
if (thisStockName == "")
{
//Moving to new stock code. Check if this has been processed already
foreach (String s in stocklist)
{
if (thisTrans.transactionStockCode == s)
{
bStockHasBeenProcessed = true;
break;
}
}
}
if (bStockHasBeenProcessed)
continue;
// this stock has not been processed.
thisStockName = thisTrans.transactionStockCode;
//Console.WriteLine("Processing stock {0}", thisStockName);
long nStockQty = 0;
if (thisTrans.transactionType == SingleTransaction.eTransactionType.Buy ||
thisTrans.transactionType == SingleTransaction.eTransactionType.Add )
nStockQty += thisTrans.transactionQty;
else
nStockQty -= thisTrans.transactionQty;
// go through the rest of the transactions
for (int j = i+1; j<numtrans; j++)
{
SingleTransaction thisNextTrans = acTransactions[j];
if (thisNextTrans.transactionStockCode != thisStockName)
continue; //ignore other stock code transactions
// check if the transaction happened before the asofDate
if (thisNextTrans.transactionDate > asofDate)
{
//Console.WriteLine("Transaction happened after as of date. Ignoring");
//thisNextTrans.Dump();
continue; // ignore transactions that happened after asofDate
}
if (thisNextTrans.transactionType == SingleTransaction.eTransactionType.Buy ||
thisNextTrans.transactionType == SingleTransaction.eTransactionType.Add )
nStockQty += thisNextTrans.transactionQty;
else
nStockQty -= thisNextTrans.transactionQty;
}
// Done processing all transactions.
stocklist.Add(thisStockName);
if (nStockQty == 0 && !bShowZeroBalance)
{
thisStockName = "";
continue;
}
Console.WriteLine("Stock balance for {0,6} as of {1,15:d} is {2,15}", thisStockName, asofDate, nStockQty);
thisStockName = "";
}
}// StockTaking
public void CapitalGains(System.DateTime fromdate, System.DateTime todate, int longtermdays)
{
// Sort transactions by stock code.
// Data structure is a Hashtable of StockCode (Key) to ArrayList of SingleTransaction (value)
Hashtable mystocktable = new Hashtable();
if (!SortTransactionsByStockCode(out mystocktable))
return;
decimal totallongterm = 0.0M;
decimal totalshortterm = 0.0M;
// Now process each stock
bool header = true;
bool bAtLeastOneTransForThisStock = false;
foreach (String stock in mystocktable.Keys)
{
ArrayList thisStockTransactions = (ArrayList)mystocktable[stock];
header = true;
bAtLeastOneTransForThisStock = false;
thisStockTransactions.Sort();
bool bfirst = true;
long nStockQty = 0;
decimal longterm = 0.0M;
decimal shortterm = 0.0M;
decimal sUnitBrokerage = 0.0M;
for (int i=0;i< thisStockTransactions.Count;i++)
{
SingleTransaction s = (SingleTransaction)thisStockTransactions[i];
if (s.transactionReconciledFlag)
continue;
sUnitBrokerage = s.transactionCharges/s.transactionQty;
if (bfirst)
{
bfirst = false;
if (!s.IsAcquisition())
Console.WriteLine("First transaction for {0} is not a BUY order. ref {1}", s.transactionStockCode, s.transactionLotIdentifier);
if (s.IsAcquisition())
nStockQty += s.transactionQty;
else
nStockQty -= s.transactionQty;
}
if (s.transactionReconciledFlag)
continue;
if (nStockQty != 0)
{
// find matching transaction
SingleTransaction.eTransactionType thisttype = s.transactionType;
bool restartloop = false;
do
{
restartloop = false;
decimal tlongterm = 0.0M;
decimal tshortterm = 0.0M;
for (int j=i+1; j<thisStockTransactions.Count;j++)
{
SingleTransaction snext = (SingleTransaction)thisStockTransactions[j];
if (snext.transactionReconciledFlag)
continue;
if (!snext.IsOppositeType(thisttype) )
continue;
if (SingleTransaction.ReferenceEquals(s,snext))
continue;
decimal spunit = 0.0M; decimal cpunit = 0.0M;decimal gainlossunit = 0.0M;
if (s.transactionDate.CompareTo(snext.transactionDate) < 0)
{
spunit = snext.transactionPrice - snext.transactionCharges/snext.transactionQty;
cpunit = s.transactionPrice + sUnitBrokerage;
}
else
{
spunit = s.transactionPrice - sUnitBrokerage;
cpunit = snext.transactionPrice + snext.transactionCharges/snext.transactionQty;
}
gainlossunit = spunit - cpunit;
if (nStockQty == snext.transactionQty)
{
SingleTransaction first = null;
if (nStockQty == s.transactionQty)
first = s;
else
first = new SingleTransaction(s.transactionType,s.transactionDate,s.transactionStockCode,nStockQty,s.transactionPrice,s.transactionCharges/s.transactionQty*nStockQty,s.transactionLotIdentifier);
SingleTransaction selltrans = null;
if (first.IsDisposal())
selltrans = first;
else
selltrans = snext;
if (selltrans.transactionDate.CompareTo(fromdate) >= 0 && selltrans.transactionDate.CompareTo(todate) <= 0)
{
TimeSpan ts = new TimeSpan();
ts = snext.transactionDate-first.transactionDate;
if (ts.Days < longtermdays)
{
tshortterm = gainlossunit*nStockQty;
shortterm += tshortterm;
totalshortterm += tshortterm;
tlongterm = 0.0M;
}
else
{
tlongterm = gainlossunit*nStockQty;
longterm += tlongterm;
totallongterm += tlongterm;
tshortterm = 0.0M;
}
Helpers.OutputHelper.PrintGains(first,snext,tshortterm,tlongterm,header);
header = false;
bAtLeastOneTransForThisStock = true;
}
s.transactionReconciledFlag = true;
snext.transactionReconciledFlag = true;
nStockQty = 0;
bfirst = true;
break;
}
else if (nStockQty > snext.transactionQty)
{
long qty = snext.transactionQty;
SingleTransaction first = new SingleTransaction(s.transactionType,s.transactionDate,s.transactionStockCode,qty,s.transactionPrice,s.transactionCharges/s.transactionQty*qty,s.transactionLotIdentifier);
SingleTransaction selltrans = null;
if (first.IsDisposal())
selltrans = first;
else
selltrans = snext;
if (selltrans.transactionDate.CompareTo(fromdate) >= 0 && selltrans.transactionDate.CompareTo(todate) <= 0)
{
TimeSpan ts = new TimeSpan();
ts = snext.transactionDate-first.transactionDate;
if (ts.Days < longtermdays)
{
tshortterm = gainlossunit*qty;
shortterm += tshortterm;
totalshortterm += tshortterm;
tlongterm = 0.0M;
}
else
{
tlongterm = gainlossunit*qty;
longterm += tlongterm;
totallongterm += tlongterm;
tshortterm = 0.0M;
}
Helpers.OutputHelper.PrintGains(first,snext,tshortterm,tlongterm,header);
header = false;
bAtLeastOneTransForThisStock = true;
}
nStockQty -= qty;
snext.transactionReconciledFlag = true;
}
else if (nStockQty < snext.transactionQty)
{
SingleTransaction first = new SingleTransaction(s.transactionType,s.transactionDate,s.transactionStockCode,nStockQty,s.transactionPrice,s.transactionCharges/s.transactionQty*nStockQty,s.transactionLotIdentifier);
SingleTransaction second = new SingleTransaction(snext.transactionType,snext.transactionDate,snext.transactionStockCode,nStockQty,snext.transactionPrice,snext.transactionCharges/snext.transactionQty*nStockQty,snext.transactionLotIdentifier);
SingleTransaction selltrans = null;
if (first.IsDisposal())
selltrans = first;
else
selltrans = second;
int nFrom = selltrans.transactionDate.CompareTo(fromdate);
int nTo = selltrans.transactionDate.CompareTo(todate);
if ( nFrom >= 0 && nTo <= 0)
{
TimeSpan ts = new TimeSpan();
ts = second.transactionDate-first.transactionDate;
if (ts.Days < longtermdays)
{
tshortterm = gainlossunit*nStockQty;
shortterm += tshortterm;
totalshortterm += tshortterm;
tlongterm = 0.0M;
}
else
{
tlongterm = gainlossunit*nStockQty;
longterm += tlongterm;
totallongterm += tlongterm;
tshortterm = 0.0M;
}
Helpers.OutputHelper.PrintGains(first,second,tshortterm,tlongterm,header);
header = false;
bAtLeastOneTransForThisStock = true;
}
nStockQty = snext.transactionQty - nStockQty;
s.transactionReconciledFlag = true;
s = snext;
sUnitBrokerage = s.transactionCharges/s.transactionQty;
thisttype = s.transactionType;
restartloop = true;
break;
}
}
}while (restartloop);
}
else
bfirst = true;
}
if (bAtLeastOneTransForThisStock)
{
Console.WriteLine("{0,110} {1,12:F2} {2,12:F2}", "", shortterm, longterm);
Console.WriteLine("---------------------------------------------------------------------------------------------------------------------------------------------");
}
}
Console.WriteLine("{0,110} {1,12:F2} {2,12:F2}", "", totalshortterm, totallongterm);
Console.WriteLine("---------------------------------------------------------------------------------------------------------------------------------------------");
return;
}// CapitalGains
internal void ResetMatchedTransactions()
{
for (int n = 0; n<lastTransactionUsed; n++)
{
SingleTransaction s = acTransactions[n];
s.transactionReconciledFlag = false;
}
}
public void MatchTransactions(string matchthis, IStockMatch eventSink)
{
ResetMatchedTransactions();
eventSink.BeginOperation();
Hashtable mystocktable;
if (!SortTransactionsByStockCode(out mystocktable))
return;
long nStockQty = 0;
foreach (String stock in mystocktable.Keys)
{
if (matchthis != "" && matchthis != stock)
continue;
ArrayList thisStockTransactions = (ArrayList)mystocktable[stock];
bool bfirst = true;
eventSink.BeginStock(stock);
for (int i = 0; i < thisStockTransactions.Count; i++)
{
SingleTransaction s = (SingleTransaction)thisStockTransactions[i];
if (s.transactionReconciledFlag)
continue;
if (bfirst)
{
bfirst = false;
nStockQty = s.transactionQty;
eventSink.BeginMatch(s);
}
if (s.transactionReconciledFlag)
continue;
if (nStockQty == 0)
continue;
// find matching transaction
SingleTransaction.eTransactionType thisttype = s.transactionType;
bool restartloop = false;
do
{
restartloop = false;
bool matchfound = false;
for (int j = i + 1; j < thisStockTransactions.Count; j++, matchfound = false)
{
SingleTransaction snext = (SingleTransaction)thisStockTransactions[j];
if (snext.transactionReconciledFlag)
continue;
if (!snext.IsOppositeType(thisttype))
continue;
if (SingleTransaction.ReferenceEquals(s, snext))
continue;
matchfound = true;
if (nStockQty == snext.transactionQty)
{
SingleTransaction first = null;
if (nStockQty == s.transactionQty)
first = s;
else
first = new SingleTransaction(s.transactionType, s.transactionDate, s.transactionStockCode, nStockQty, s.transactionPrice, s.transactionCharges / s.transactionQty * nStockQty, s.transactionLotIdentifier);
eventSink.MatchFound(first, snext, false, false);
s.transactionReconciledFlag = true;
snext.transactionReconciledFlag = true;
nStockQty = 0;
bfirst = true;
// A BUY transaction has been completely reconciled with one or more SELL transactions.
// Now we need to start with the next BUY transaction. Hence break out!
break;
}
else if (nStockQty > snext.transactionQty)
{
long qty = snext.transactionQty;
SingleTransaction first = new SingleTransaction(s.transactionType, s.transactionDate, s.transactionStockCode, qty, s.transactionPrice, s.transactionCharges / s.transactionQty * qty, s.transactionLotIdentifier);
eventSink.MatchFound(first, snext, true, false);
nStockQty -= qty;
snext.transactionReconciledFlag = true;
}
else if (nStockQty < snext.transactionQty)
{
SingleTransaction first = new SingleTransaction(s.transactionType, s.transactionDate, s.transactionStockCode, nStockQty, s.transactionPrice, s.transactionCharges / s.transactionQty * nStockQty, s.transactionLotIdentifier);
SingleTransaction second = new SingleTransaction(snext.transactionType, snext.transactionDate, snext.transactionStockCode, nStockQty, snext.transactionPrice, snext.transactionCharges / snext.transactionQty * nStockQty, snext.transactionLotIdentifier);
eventSink.MatchFound(first, second, false, true);
nStockQty = snext.transactionQty - nStockQty;
s.transactionReconciledFlag = true;
s = snext;
// Now the SELL transaction which is at a later date is left with some qty that is not matched. We need to find
// a matching BUY transaction from an earlier date to completely reconcile this SELL transaction. Hence start
// from the beginning of the transaction list.
SingleTransaction begin = new SingleTransaction(s.transactionType, s.transactionDate, s.transactionStockCode, nStockQty, s.transactionPrice, s.transactionCharges / s.transactionQty * nStockQty, s.transactionLotIdentifier);
eventSink.BeginMatch(begin);
thisttype = s.transactionType;
restartloop = true;
break;
}
} // loop on all transactions following the current one we are looking for a match for.
// if there is no matching transaction, the next transaction should still be considered a new one.
if (!matchfound)
{
SingleTransaction sUnmatched = new SingleTransaction(s.transactionType, s.transactionDate, s.transactionStockCode, nStockQty, s.transactionPrice, s.transactionCharges / s.transactionQty * nStockQty, s.transactionLotIdentifier);
s.transactionReconciledFlag = true;
eventSink.NoMatchFound(sUnmatched);
bfirst = true;
}
} while (restartloop);
} // loop on all transactions for current stock
eventSink.EndStock(stock);
} // loop on all stocks
eventSink.EndOperation();
}
private bool SortTransactionsByStockCode(out Hashtable mystocktable)
{
// Sort transactions by stock code.
// Data structure is a Hashtable of StockCode (Key) to ArrayList of SingleTransaction (value)
mystocktable = new Hashtable();
int i = 0;
bool retval = false;
int numtrans = lastTransactionUsed;
for (i = 0; i < numtrans; i++)
{
SingleTransaction thisTrans = acTransactions[i];
ArrayList thisStockTransactions = null;
String thisStockCode = thisTrans.transactionStockCode;
// check if this stock already has a hashtable entry.
if (!mystocktable.ContainsKey(thisTrans.transactionStockCode))
mystocktable.Add(thisTrans.transactionStockCode, (thisStockTransactions = new ArrayList()));
else
thisStockTransactions = (ArrayList)mystocktable[thisStockCode];
thisStockTransactions.Add(thisTrans);
retval = true;
} //for
return retval;
}
}
}