Skip to content

Instantly share code, notes, and snippets.

@blippy
Last active November 28, 2017 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blippy/2b1b405ea1f92a9af1b306978b95ac6b to your computer and use it in GitHub Desktop.
Save blippy/2b1b405ea1f92a9af1b306978b95ac6b to your computer and use it in GitHub Desktop.
plaintextaccounting
#!/usr/bin/env bash
function inputs {
echo "today `date --iso`" | cat - <<insert your input files here>>
}
function pending {
DSTAMP=`date --iso`
RE="s/^pending/ntran $DSTAMP/"
sed -e "$RE"
}
inputs | pending
#!/usr/bin/env bash
postify | cut -f3,4 | sumby | sed 's/^/bal\t/' | teet 0 ~/tmp/etb | cat - gaap.txt | awk '
BEGIN { FS= "\t" ; OFS = "\t" }
function pprint(acc, desc)
{
printf "%s\t% 12.2f\t%s\n", acc, bal[acc], desc
}
$1 == "bal" { bal[$2] = $3 }
$1 == "gaap" && $3 != "=" {
pprint($3, "")
#print $3, bal[$3]
bal[$2] += bal[$3]
}
$1 == "gaap" && $3 == "=" {
pprint($2, $4)
#print $2, bal[$2]
print ""
}
'
#!/usr/bin/env bash
# main driver routine
cd <<directory where files are>>
derive | etb
qtys.sh
echo "stocko: ~/tmp/stocko.csv"
stocko.sh > ~/tmp/stocko.csv
#!/usr/bin/env bash
function enpost {
awk '
BEGIN { FS = "\t"; OFS = "\t"; }
function post(dstamp, acc, amount, oacc, desc)
{
printf "post\t%s\t%s\t%.2f\t%s\t%s\n", dstamp, acc, amount, oacc, desc
#print desc
}
$1 == "ntran" {
post($2, $3, $5, $4, $6)
post($2, $4, -$5, $3, $6)
}
$1 == "etran-2" {
if ($7=="B")
amount = $6
else
amount = - $6
post($2, $3, -amount, "flow", $8)
post($2, "flow", amount, $3, $8)
}
'
}
enpost | grep 2017
#!/usr/bin/env bash
awk <accts2017v3.txt '
function wout(acc, ticker, qty)
{
#qty =sprintf("%.0f", qty)
#q1 = int(qty)
print acc ":" ticker, qty
print "all:" ticker, qty
}
$1 == "etran-2" {
val = $5 ;
if($7 == "S") val = -val;
wout($3, $4, val)
#qtys[$4] += val
}
$1 == "leak-2" {wout($3, $4, -$5)}
' | sumby
#!/usr/bin/env bash
awk '
BEGIN { IFS = "\t" ; OFS = "\t"; lnum = 0; }
function stdline(dstamp, line)
{
lnum += 1
printf "%s:%04d\t", dstamp, lnum
print line
}
$1 == "ntran" || $1 == "yahoo-1" || $1 == "etran-2" {
stdline($2, $0)
}
$1 == "today" || $1 == "year" || $1 == "period" {
stdline("1000-05-05", $0)
}
' | LC_ALL=C sort | sed 's/^\S\+\s//g'
#!/usr/bin/env awk
BEGIN {
IFS="\t" ; OFS = ",";
print "TICKER,DATE,TIME,TYPE,SHARES,FX,PRICE,CURRENCY,COMMISSION,TAX,TOTAL";
}
{
#print "";
#print $0;
dstamp = substr($1, 9, 2) "/" substr($1, 6, 2) "/" substr($1, 1, 4)
consid = $5
if($3=="B") {
sconsid = consid;
typa = "Buy"
typb = "Deposit"
} else {
sconsid = -consid
typa = "Sell";
typb = "Withdrawal";
}
qty = $4;
price = consid * 100 / qty;
print $2, dstamp, "10:10:10", typa, qty, 1, price, "GBX", 0 , "", sconsid
print "", dstamp, "10:10:10", typb, "", "", "" , "" , "", "", sconsid
}
#!/usr/bin/env bash
tsv < accts2017v3.txt | grep etran-2 | awk -F '\t' '
BEGIN { OFS="\t" }
{
ticker="LON:" substr($4, 1, length($4) -2)
print $2,ticker, $7, $5, $6
}
' | sort | awk -f stocko.awk
#include <iostream>
#include <map>
#include <regex>
int
main()
{
std::regex rgx("\\s+");
std::map<std::string, double> m;
std::string line;
std::sregex_token_iterator rend;
while(std::getline(std::cin, line)) {
std::sregex_token_iterator it(line.begin(), line.end(), rgx, -1);
//std::cout << (it.end() - it.begin()) << "\n";
if(it==rend) continue;
std::string key = *it++;
if(it==rend) continue;
//double value = stod(*it);
std::string vstr = *it;
double value = atof(vstr.c_str());
auto mit = m.find(key);
if(mit == m.end())
m[key] = value;
else
m[key] += value;
//std::cout << "sumby:" << key << "\t" << value << "\t" << m[key] << "\n";
//printf("sumby:%s\t%.3f\t%.3f\n", key.c_str(), value, m[key]);
}
for(auto mit=m.begin(); mit != m.end(); ++mit) {
printf("%s\t%.2f\n", mit->first.c_str(), mit->second);
//std::cout << mit->first << "\t" << mit->second << "\n";
}
return 0;
}
#!/usr/bin/env bash
if [ "x$1" != "x0" ]; then
tee $2
else
cat -
fi
#!/usr/bin/env bash
function print_help {
cat <<EOF
Prints out the accounts
-h this help
-n ACC name of account (default: rbs)
-w whole output, instead of tail
EOF
}
ACC=<<default account>>
WHOLE=0
while getopts "hn:w" opt
do
case $opt in
h) print_help ; exit 0 ;;
n) ACC=$OPTARG ;;
w) WHOLE=1 ;;
*) echo "Unrecognised argment: $opt" ; exit 1 ;;
esac
done
function defout {
tail
}
OUTPUTTER=defout
derive | sort-inputs | postify | awk -v acc=$ACC -F '\t' '
$3 == acc {
tot += $4
printf "%4s %s %10.2f %10.2f %4s %s\n", $3, $2, $4, tot, $5, $6
}
'
@blippy
Copy link
Author

blippy commented Nov 28, 2017

An explanation for all this is available here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment