ledger3.texi 341 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295
  1. \input texinfo @c -*-texinfo-*-
  2. @setfilename ledger3.info
  3. @include version.texi
  4. @set FIXME:UNDOCUMENTED @sc{undocumented}! Please help by contributing documentation for this feature.
  5. @set InternalUseOnly For internal use only.
  6. @settitle Ledger: Command-Line Accounting
  7. @c Before release, run C-u C-c C-u C-a (texinfo-all-menus-update with
  8. @c a prefix arg). This updates the node pointers, which texinfmt.el
  9. @c needs.
  10. @c | Formating | Indexing | |
  11. @c | | @cindex | concept |
  12. @c | @command | @findex | Ledger CLI Command (like balance) |
  13. @c | @option | @findex | Ledger CLI Option (like --market) |
  14. @c | @var | | Ledger CLI option Variable (like -f FILE) |
  15. @c | | | Ledger file Syntax |
  16. @c | @samp | | Valued example or single char |
  17. @c | @file | | File, Buffer |
  18. @c | @file | | Program (like ledger, report, acprep) |
  19. @c Restructuring manual ideas
  20. @c http://beyondgrep.com/documentation/ack-2.04-man.html
  21. @c How to make documented ledger examples validate automatically.
  22. @c
  23. @c The test/DocTests.py script will be run along with the other tests
  24. @c when using ctest or acprep check.
  25. @c The script parses the texinfo file and looks for three kinds of
  26. @c specially marked @smallexamples, then it will run the ledger
  27. @c command from the example, and compare the results with the output
  28. @c from the documentation.
  29. @c
  30. @c To specially mark a @smallexample append @c command:UUID, where
  31. @c UUID is the first 7 digits from the commands sha1sum, e.g.:
  32. @c
  33. @c @smallexample @c command:CDE330A
  34. @c $ ledger -f sample.dat reg expenses
  35. @c @end smallexample
  36. @c
  37. @c Then DocTests.py will look for corresponding documented output,
  38. @c which may appear anywhere in the file, and is marked with
  39. @c @smallexample @c output:UUID where UUID is the UUID from the
  40. @c corresponding ledger command example, e.g.:
  41. @c
  42. @c @smallexample @c output:CDE330A
  43. @c 04-May-27 Book Store Expenses:Books $20.00 $20.00
  44. @c Expenses:Cards $40.00 $60.00
  45. @c Expenses:Docs $30.00 $90.0
  46. @c @end smallexample
  47. @c
  48. @c Now where does this data in sample.dat come from?
  49. @c DocTests.py is a bit smart about ledger's file argument, since
  50. @c it will check if the given filename exists in the test/input/
  51. @c directory.
  52. @c
  53. @c Sometimes the journal data for an example is specified within
  54. @c the documentation itself, in that case the journal example data
  55. @c needs to be specially marked as well using @smallexample @c input:UUID,
  56. @c again with the UUID being the UUID of the corresponding ledger example
  57. @c command. If multiple inputs with the same UUID are found they will be
  58. @c concatenated together and given as one set of data to the example command.
  59. @c
  60. @c @smallexample @c input:35CB2A3
  61. @c 2014/02/09 The Italian Place
  62. @c Expenses:Food:Dining $ 36.84
  63. @c Assets:Cash
  64. @c @end smallexample
  65. @c
  66. @c @smallexample @c command:35CB2A3
  67. @c $ ledger -f inline.dat accounts
  68. @c @end smallexample
  69. @c
  70. @c @smallexample @c output:35CB2A3
  71. @c Assets:Cash
  72. @c Expenses:Food:Dining
  73. @c @end smallexample
  74. @c
  75. @c To use different example commands with the same input from the documentation
  76. @c add with_input:UUID to the example command, where UUID is the UUID of the input,
  77. @c e.g.:
  78. @c
  79. @c @smallexample @c command:94FD2B6,with_input:35CB2A3
  80. @c $ ledger -f inline.dat bal expenses
  81. @c @end smallexample
  82. @c
  83. @c @smallexample @c output:94FD2B6
  84. @c $ 36.84 Expenses:Food:Dining
  85. @c @end smallexample
  86. @c
  87. @c To pass additional input to ledger for certain commands, e.g. convert add
  88. @c with_file:filename to the example command and add a file:UUID to an example
  89. @c that holds the additional input, where UUID is the UUID of the command,
  90. @c e.g.:
  91. @c
  92. @c @smallexample @c file:download.csv
  93. @c 767718,12/13/2011,"Withdrawal","ACE HARDWARE 16335 S HOUGHTON RD",-8.80,,00001640.04,,
  94. @c @end smallexample
  95. @c
  96. @c @smallexample @c command:94FD2B6,with_file:download.csv
  97. @c $ ledger -f sample.dat convert download.csv
  98. @c @end smallexample
  99. @c
  100. @c Additionally DocTests.py will pass --args-only and --columns 80 to ledger
  101. @c to ignore any default arguments from the environment or .ledgerrc.
  102. @c
  103. @c To manually run the tests in this file run:
  104. @c $ ./test/DocTests.py -vv --ledger ./ledger --file ./doc/ledger3.texi
  105. @copying
  106. Copyright @copyright{} 2003–2015, John Wiegley. All rights reserved.
  107. Redistribution and use in source and binary forms, with or without
  108. modification, are permitted provided that the following conditions are
  109. met:
  110. @itemize
  111. @item
  112. Redistributions of source code must retain the above copyright
  113. notice, this list of conditions and the following disclaimer.
  114. @item
  115. Redistributions in binary form must reproduce the above copyright
  116. notice, this list of conditions and the following disclaimer in the
  117. documentation and/or other materials provided with the distribution.
  118. @item
  119. Neither the name of New Artisans LLC nor the names of its
  120. contributors may be used to endorse or promote products derived from
  121. this software without specific prior written permission.
  122. @end itemize
  123. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  124. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  125. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  126. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  127. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  128. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  129. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  130. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  131. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  132. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  133. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  134. @end copying
  135. @dircategory User Applications
  136. @direntry
  137. * Ledger3: (ledger3). Command-Line Accounting
  138. @end direntry
  139. @documentencoding UTF-8
  140. @iftex
  141. @finalout
  142. @end iftex
  143. @titlepage
  144. @title Ledger: Command-Line Accounting
  145. @subtitle For Version @value{VERSION} of Ledger
  146. @author John Wiegley
  147. @page
  148. @vskip 0pt plus 1filll
  149. @insertcopying
  150. @end titlepage
  151. @contents
  152. @ifnottex
  153. @node Top, Introduction to Ledger, (dir), (dir)
  154. @top Overview
  155. Ledger is a command-line accounting tool that provides double-entry
  156. accounting based on a text journal. It provides no bells or whistles,
  157. and returns the user to the days before user interfaces were even a
  158. twinkling in their father's CRT.
  159. @end ifnottex
  160. @menu
  161. * Introduction to Ledger::
  162. * Ledger Tutorial::
  163. * Principles of Accounting with Ledger::
  164. * Keeping a Journal::
  165. * Transactions::
  166. * Building Reports::
  167. * Reporting Commands::
  168. * Command-Line Syntax::
  169. * Budgeting and Forecasting::
  170. * Time Keeping::
  171. * Value Expressions::
  172. * Format Strings::
  173. * Extending with Python::
  174. * Ledger for Developers::
  175. * Major Changes from version 2.6::
  176. * Example Journal File::
  177. * Miscellaneous Notes::
  178. * Concepts Index::
  179. * Commands & Options Index::
  180. @end menu
  181. @node Introduction to Ledger, Ledger Tutorial, Top, Top
  182. @chapter Introduction to Ledger
  183. @menu
  184. * Fat-free Accounting::
  185. * Building the program::
  186. * Getting help::
  187. @end menu
  188. @node Fat-free Accounting, Building the program, Introduction to Ledger, Introduction to Ledger
  189. @section Fat-free Accounting
  190. Ledger is an accounting tool with the moxie to exist. It provides no
  191. bells or whistles, and returns the user to the days before user
  192. interfaces were even a twinkling in their father's CRT.
  193. What it does offer is a double-entry accounting journal with all the
  194. flexibility and muscle of its modern day cousins, without any of the
  195. fat. Think of it as the Bran Muffin of accounting tools.
  196. To use it, you need to start keeping a journal. This is the basis of
  197. all accounting, and if you haven't started yet, now is the time to
  198. learn. The little booklet that comes with your checkbook is a journal,
  199. so we'll describe double-entry accounting in terms of that.
  200. @c If you use another GUI accounting program like GnuCash, the vast
  201. @c majority of its functionality is geared towards helping you keep
  202. @c a journal.
  203. A checkbook journal records debits (subtractions, or withdrawals) and
  204. credits (additions, or deposits) with reference to a single account:
  205. the checking account. Where the money comes from, and where it goes
  206. to, are described in the payee field, where you write the person or
  207. company's name. The ultimate aim of keeping a checkbook journal is to
  208. know how much money is available to spend. That's really the aim of
  209. all journals.
  210. @cindex postings
  211. What computers add is the ability to walk through these postings,
  212. and tell you things about your spending habits; to let you devise
  213. budgets and get control over your spending; to squirrel away money
  214. into virtual savings account without having to physically move money
  215. around; etc. As you keep your journal, you are recording information
  216. about your life and habits, and sometimes that information can start
  217. telling you things you aren't aware of. Such is the aim of all good
  218. accounting tools.
  219. The next step up from a checkbook journal, is a journal that keeps
  220. track of all your accounts, not just checking. In such a journal, you
  221. record not only who gets paid---in the case of a debit---but where the
  222. money came from. In a checkbook journal, it's assumed that all the
  223. money comes from your checking account. But in a general journal, you
  224. write postings in two lines: the source account and target account.
  225. @emph{There must always be a debit from at least one account for every
  226. credit made to another account}. This is what is meant by
  227. ``double-entry'' accounting: the journal must always balance to zero,
  228. with an equal number of debits and credits.
  229. For example, let's say you have a checking account and a brokerage
  230. account, and you can write checks from both of them. Rather than keep
  231. two checkbooks, you decide to use one journal for both. In this
  232. general journal you need to record a payment to Pacific Bell for your
  233. monthly phone bill, and a transfer (via check) from your brokerage
  234. account to your checking account. The Pacific Bell bill is $23.00,
  235. let's say, and you want to pay it from your checking account. In the
  236. general journal you need to say where the money came from, in addition
  237. to where it's going to. These transactions might look like this:
  238. @smallexample
  239. 9/29 Pacific Bell $23.00 $23.00
  240. Checking $-23.00 0
  241. 9/30 Checking $100.00 $100.00
  242. (123) Brokerage $-100.00 0
  243. @end smallexample
  244. The posting must balance to $0: $23 went to Pacific Bell, $23 came
  245. from Checking. The next entry shows check number 123 written against
  246. your brokerage account, transferring money to your checking account.
  247. There is nothing left over to be accounted for, since the money has
  248. simply moved from one account to another in both cases. This is the
  249. basis of double-entry accounting: money never pops in or out of
  250. existence; it is always a posting from one account to another.
  251. Keeping a general journal is the same as keeping two separate
  252. journals: One for Pacific Bell and one for Checking. In that case,
  253. each time a payment is written into one, you write a corresponding
  254. withdrawal into the other. This makes it easier to write in
  255. a ``running balance'', since you don't have to look back at the last
  256. time the account was referenced---but it also means having a lot of
  257. journal books, if you deal with multiple accounts.
  258. @cindex account, meaning of
  259. @cindex meaning of account
  260. Here is a good place for an aside on the use of the word ``account''.
  261. Most private people consider an account to be something that holds
  262. money at an institution for them. Ledger uses a more general
  263. definition of the word. An account is anywhere money can go. Other
  264. finance programs use ``categories'', Ledger uses accounts. So, for
  265. example, if you buy some groceries at Trader Joe's, then more groceries
  266. at Whole Food Market, you might assign the transactions like this
  267. @smallexample @c input:validate
  268. 2011/03/15 Trader Joe's
  269. Expenses:Groceries $100.00
  270. Assets:Checking
  271. 2011/03/15 Whole Food Market
  272. Expenses:Groceries $75.00
  273. Assets:Checking
  274. @end smallexample
  275. In both cases the money goes to the @samp{Groceries} account, even
  276. though the payees were different. You can set up your accounts in any
  277. way you choose.
  278. Enter the beauty of computerized accounting. The purpose of the
  279. Ledger program is to make general journal accounting simple, by
  280. keeping track of the balances for you. Your only job is to enter the
  281. postings. If an individual posting does not balance, Ledger displays
  282. an error and indicates the incorrect posting.@footnote{In some special
  283. cases, it automatically balances this transaction for you.}
  284. In summary, there are two aspects of Ledger use: updating the journal
  285. data file, and using the Ledger tool to view the summarized result of
  286. your transactions.
  287. And just for the sake of example---as a starting point for those who
  288. want to dive in head-first---here are the journal transactions from
  289. above, formatted as the Ledger program wishes to see them:
  290. @smallexample @c input:48DDF26
  291. 2004/09/29 Pacific Bell
  292. Expenses:Pacific Bell $23.00
  293. Assets:Checking
  294. @end smallexample
  295. The account balances and registers in this file, if saved as
  296. @file{ledger.dat}, could be reported using:
  297. @smallexample @c command:48DDF26
  298. $ ledger -f ledger.dat balance
  299. @end smallexample
  300. @smallexample @c output:48DDF26
  301. $-23.00 Assets:Checking
  302. $23.00 Expenses:Pacific Bell
  303. --------------------
  304. 0
  305. @end smallexample
  306. Or
  307. @smallexample @c command:8C7295F,with_input:48DDF26
  308. $ ledger -f ledger.dat register checking
  309. @end smallexample
  310. @smallexample @c output:8C7295F
  311. 04-Sep-29 Pacific Bell Assets:Checking $-23.00 $-23.00
  312. @end smallexample
  313. And even:
  314. @smallexample @c command:BB32EF2,with_input:48DDF26
  315. $ ledger -f ledger.dat register Bell
  316. @end smallexample
  317. @smallexample @c output:BB32EF2
  318. 04-Sep-29 Pacific Bell Expenses:Pacific Bell $23.00 $23.00
  319. @end smallexample
  320. An important difference between Ledger and other finance packages is
  321. that Ledger will never alter your input file. You can create and edit
  322. that file in any way you prefer, but Ledger is only for analyzing the
  323. data, not for altering it.
  324. @node Building the program, Getting help, Fat-free Accounting, Introduction to Ledger
  325. @section Building the program
  326. Ledger is written in ANSI C++, and should compile on any unix platform.
  327. The easiest way to build and install ledger is to use the prepared
  328. acprep script, that does a lot of the footwork:
  329. @smallexample
  330. # to install missing dependencies
  331. ./acprep dependencies
  332. # building ledger
  333. ./acprep update
  334. # to run the actual installation
  335. make install
  336. @end smallexample
  337. See the `help` subcommand to `acprep`, which explains some of its many
  338. options. You can run `make check` to confirm the result, and `make
  339. install` to install. If these intructions do not work for you can check the
  340. `INSTALL.md` in the source directory for more up do date build instructions.
  341. @node Getting help, , Building the program, Introduction to Ledger
  342. @section Getting help
  343. @findex help
  344. Ledger has a complete online help system based on GNU Info. This manual
  345. can be searched directly from the command-line using @code{info ledger},
  346. which will bring up this entire manual in your TTY. Alternatively, the
  347. shorter man page can be accessed from the command-line either via
  348. @code{man ledger} or @code{ledger --help}
  349. If you need help on how to use Ledger, or run into problems, you can
  350. join the Ledger mailing list at
  351. @url{http://groups.google.com/group/ledger-cli}.
  352. You can also find help in the @code{#ledger} channel on the IRC server
  353. @code{irc.freenode.net}.
  354. @node Ledger Tutorial, Principles of Accounting with Ledger, Introduction to Ledger, Top
  355. @chapter Ledger Tutorial
  356. @cindex tutorial
  357. @menu
  358. * Start a Journal File::
  359. * Run a Few Reports::
  360. @end menu
  361. @node Start a Journal File, Run a Few Reports, Ledger Tutorial, Ledger Tutorial
  362. @section Start a Journal File
  363. @cindex journals
  364. A journal is a record of your financial transactions and will be central
  365. to using Ledger. For now we just want to get a taste of what Ledger can
  366. do. An example journal is included with the source code distribution,
  367. called @file{drewr3.dat} (@pxref{Example Journal File}). Copy it
  368. someplace convenient and open up a terminal window in that directory.
  369. If you would rather start with your own journal right away please
  370. @pxref{Keeping a Journal}.
  371. @node Run a Few Reports, , Start a Journal File, Ledger Tutorial
  372. @section Run a Few Reports
  373. @menu
  374. * Balance Report::
  375. * Register Report::
  376. * Cleared Report::
  377. * Using the Windows Command-Line::
  378. @end menu
  379. Please note that as a command-line program, Ledger is controlled from
  380. your shell. There are several different command shells that all
  381. behave slightly differently with respect to some special characters.
  382. In particular, the ``bash'' shell will interpret @samp{$} signs
  383. differently than ledger and they must be escaped to reach the actual
  384. program. Another example is ``zsh'', which will interpret @samp{^}
  385. differently than ledger expects. In all cases that follow you should
  386. take that into account when entering the command-line arguments as given.
  387. There are too many variations between shells to give concrete examples
  388. for each.
  389. @node Balance Report, Register Report, Run a Few Reports, Run a Few Reports
  390. @subsection Balance Report
  391. @cindex balance report
  392. @findex balance
  393. To find the balances of all of your accounts, run this command:
  394. @smallexample @c command:1071890
  395. $ ledger -f drewr3.dat balance
  396. @end smallexample
  397. Ledger will generate:
  398. @smallexample @c output:1071890
  399. $ -3,804.00 Assets
  400. $ 1,396.00 Checking
  401. $ 30.00 Business
  402. $ -5,200.00 Savings
  403. $ -1,000.00 Equity:Opening Balances
  404. $ 6,654.00 Expenses
  405. $ 5,500.00 Auto
  406. $ 20.00 Books
  407. $ 300.00 Escrow
  408. $ 334.00 Food:Groceries
  409. $ 500.00 Interest:Mortgage
  410. $ -2,030.00 Income
  411. $ -2,000.00 Salary
  412. $ -30.00 Sales
  413. $ -63.60 Liabilities
  414. $ -20.00 MasterCard
  415. $ 200.00 Mortgage:Principal
  416. $ -243.60 Tithe
  417. --------------------
  418. $ -243.60
  419. @end smallexample
  420. @noindent
  421. Showing you the balance of all accounts. Options and search terms can
  422. pare this down to show only the accounts you want.
  423. A more useful report is to show only your Assets and Liabilities:
  424. @smallexample @c command:5BF4D8E
  425. $ ledger -f drewr3.dat balance Assets Liabilities
  426. @end smallexample
  427. @smallexample @c output:5BF4D8E
  428. $ -3,804.00 Assets
  429. $ 1,396.00 Checking
  430. $ 30.00 Business
  431. $ -5,200.00 Savings
  432. $ -63.60 Liabilities
  433. $ -20.00 MasterCard
  434. $ 200.00 Mortgage:Principal
  435. $ -243.60 Tithe
  436. --------------------
  437. $ -3,867.60
  438. @end smallexample
  439. @node Register Report, Cleared Report, Balance Report, Run a Few Reports
  440. @subsection Register Report
  441. @cindex register report
  442. @findex register
  443. To show all transactions and a running total:
  444. @smallexample @c command:66E3A2C
  445. $ ledger -f drewr3.dat register
  446. @end smallexample
  447. @noindent
  448. Ledger will generate:
  449. @smallexample @c output:66E3A2C
  450. 10-Dec-01 Checking balance Assets:Checking $ 1,000.00 $ 1,000.00
  451. Equit:Opening Balances $ -1,000.00 0
  452. 10-Dec-20 Organic Co-op Expense:Food:Groceries $ 37.50 $ 37.50
  453. Expense:Food:Groceries $ 37.50 $ 75.00
  454. Expense:Food:Groceries $ 37.50 $ 112.50
  455. Expense:Food:Groceries $ 37.50 $ 150.00
  456. Expense:Food:Groceries $ 37.50 $ 187.50
  457. Expense:Food:Groceries $ 37.50 $ 225.00
  458. Assets:Checking $ -225.00 0
  459. 10-Dec-28 Acme Mortgage Lia:Mortgage:Principal $ 200.00 $ 200.00
  460. Expe:Interest:Mortgage $ 500.00 $ 700.00
  461. Expenses:Escrow $ 300.00 $ 1,000.00
  462. Assets:Checking $ -1,000.00 0
  463. 11-Jan-02 Grocery Store Expense:Food:Groceries $ 65.00 $ 65.00
  464. Assets:Checking $ -65.00 0
  465. 11-Jan-05 Employer Assets:Checking $ 2,000.00 $ 2,000.00
  466. Income:Salary $ -2,000.00 0
  467. (Liabilities:Tithe) $ -240.00 $ -240.00
  468. 11-Jan-14 Bank Assets:Savings $ 300.00 $ 60.00
  469. Assets:Checking $ -300.00 $ -240.00
  470. 11-Jan-19 Grocery Store Expense:Food:Groceries $ 44.00 $ -196.00
  471. Assets:Checking $ -44.00 $ -240.00
  472. 11-Jan-25 Bank Assets:Checking $ 5,500.00 $ 5,260.00
  473. Assets:Savings $ -5,500.00 $ -240.00
  474. 11-Jan-25 Tom's Used Cars Expenses:Auto $ 5,500.00 $ 5,260.00
  475. Assets:Checking $ -5,500.00 $ -240.00
  476. 11-Jan-27 Book Store Expenses:Books $ 20.00 $ -220.00
  477. Liabilities:MasterCard $ -20.00 $ -240.00
  478. 11-Dec-01 Sale Asse:Checking:Business $ 30.00 $ -210.00
  479. Income:Sales $ -30.00 $ -240.00
  480. (Liabilities:Tithe) $ -3.60 $ -243.60
  481. @end smallexample
  482. @noindent
  483. To limit this to a more useful subset, simply add the accounts you are
  484. interested in seeing transactions for:
  485. @cindex accounts, limiting by
  486. @cindex limiting by accounts
  487. @smallexample @c command:96B0EB3
  488. $ ledger -f drewr3.dat register Groceries
  489. @end smallexample
  490. @smallexample @c output:96B0EB3
  491. 10-Dec-20 Organic Co-op Expense:Food:Groceries $ 37.50 $ 37.50
  492. Expense:Food:Groceries $ 37.50 $ 75.00
  493. Expense:Food:Groceries $ 37.50 $ 112.50
  494. Expense:Food:Groceries $ 37.50 $ 150.00
  495. Expense:Food:Groceries $ 37.50 $ 187.50
  496. Expense:Food:Groceries $ 37.50 $ 225.00
  497. 11-Jan-02 Grocery Store Expense:Food:Groceries $ 65.00 $ 290.00
  498. 11-Jan-19 Grocery Store Expense:Food:Groceries $ 44.00 $ 334.00
  499. @end smallexample
  500. @noindent
  501. Which matches the balance reported for the @samp{Groceries} account:
  502. @smallexample @c command:AECD64E
  503. $ ledger -f drewr3.dat balance Groceries
  504. @end smallexample
  505. @smallexample @c output:AECD64E
  506. $ 334.00 Expenses:Food:Groceries
  507. @end smallexample
  508. @noindent
  509. If you would like to find transaction to only a certain payee use
  510. @samp{payee} or @samp{@@}:
  511. @smallexample @c command:C6BC57E
  512. $ ledger -f drewr3.dat register payee "Organic"
  513. @end smallexample
  514. @smallexample @c output:C6BC57E
  515. 10-Dec-20 Organic Co-op Expense:Food:Groceries $ 37.50 $ 37.50
  516. Expense:Food:Groceries $ 37.50 $ 75.00
  517. Expense:Food:Groceries $ 37.50 $ 112.50
  518. Expense:Food:Groceries $ 37.50 $ 150.00
  519. Expense:Food:Groceries $ 37.50 $ 187.50
  520. Expense:Food:Groceries $ 37.50 $ 225.00
  521. Assets:Checking $ -225.00 0
  522. @end smallexample
  523. @node Cleared Report, Using the Windows Command-Line, Register Report, Run a Few Reports
  524. @subsection Cleared Report
  525. @cindex cleared report
  526. @findex cleared
  527. A very useful report is to show what your obligations are versus what
  528. expenditures have actually been recorded. It can take several days for
  529. a check to clear, but you should treat it as money spent. The
  530. @command{cleared} report shows just that (note that the
  531. @command{cleared} report will not format correctly for accounts that
  532. contain multiple commodities):
  533. @smallexample @c command:B86F6A6
  534. $ ledger -f drewr3.dat cleared
  535. @end smallexample
  536. @smallexample @c output:B86F6A6
  537. $ -3,804.00 $ 775.00 Assets
  538. $ 1,396.00 $ 775.00 10-Dec-20 Checking
  539. $ 30.00 0 Business
  540. $ -5,200.00 0 Savings
  541. $ -1,000.00 $ -1,000.00 10-Dec-01 Equity:Opening Balances
  542. $ 6,654.00 $ 225.00 Expenses
  543. $ 5,500.00 0 Auto
  544. $ 20.00 0 Books
  545. $ 300.00 0 Escrow
  546. $ 334.00 $ 225.00 10-Dec-20 Food:Groceries
  547. $ 500.00 0 Interest:Mortgage
  548. $ -2,030.00 0 Income
  549. $ -2,000.00 0 Salary
  550. $ -30.00 0 Sales
  551. $ -63.60 0 Liabilities
  552. $ -20.00 0 MasterCard
  553. $ 200.00 0 Mortgage:Principal
  554. $ -243.60 0 Tithe
  555. ---------------- ---------------- ---------
  556. $ -243.60 0
  557. @end smallexample
  558. @noindent
  559. The first column shows the outstanding balance, the second column
  560. shows the ``cleared'' balance.
  561. @node Using the Windows Command-Line, , Cleared Report, Run a Few Reports
  562. @subsection Using the Windows Command-Line
  563. @cindex windows cmd.exe
  564. @cindex currency symbol display on windows
  565. Using ledger under the windows command shell has one significant
  566. limitation. CMD.EXE is limited to standard ASCII characters and as
  567. such cannot display any currency symbols other than dollar signs
  568. @samp{$}.
  569. @node Principles of Accounting with Ledger, Keeping a Journal, Ledger Tutorial, Top
  570. @chapter Principles of Accounting with Ledger
  571. @menu
  572. * Accounting with Ledger::
  573. * Stating where money goes::
  574. * Assets and Liabilities::
  575. * Commodities and Currencies::
  576. * Accounts and Inventories::
  577. * Understanding Equity::
  578. * Dealing with Petty Cash::
  579. * Working with multiple funds and accounts::
  580. @end menu
  581. @node Accounting with Ledger, Stating where money goes, Principles of Accounting with Ledger, Principles of Accounting with Ledger
  582. @section Accounting with Ledger
  583. @cindex double-entry accounting
  584. Accounting is simply tracking your money. It can range from nothing,
  585. and just waiting for automatic overdraft protection to kick in, or
  586. not, to a full-blown double-entry accounting system. Ledger
  587. accomplishes the latter. With ledger you can handle your personal
  588. finances or your business's. Double-entry accounting scales.
  589. @node Stating where money goes, Assets and Liabilities, Accounting with Ledger, Principles of Accounting with Ledger
  590. @section Stating where money goes
  591. @cindex credits and debits
  592. Accountants will talk of ``credits'' and ``debits'', but the meaning
  593. is often different from the layman's understanding. To avoid
  594. confusion, Ledger uses only subtractions and additions, although the
  595. underlying intent is the same as standard accounting principles.
  596. Recall that every posting will involve two or more accounts.
  597. Money is transferred from one or more accounts to one or more other
  598. accounts. To record the posting, an amount is @emph{subtracted}
  599. from the source accounts, and @emph{added} to the target accounts.
  600. In order to write a Ledger transaction correctly, you must determine
  601. where the money comes from and where it goes to. For example, when
  602. you are paid a salary, you must add money to your bank account and
  603. also subtract it from an income account:
  604. @smallexample @c input:validate
  605. 9/29 My Employer
  606. Assets:Checking $500.00
  607. Income:Salary $-500.00
  608. @end smallexample
  609. @cindex income is negative
  610. @cindex why is income negative
  611. Why is the Income a negative figure? When you look at the balance
  612. totals for your ledger, you may be surprised to see that Expenses are
  613. a positive figure, and Income is a negative figure. It may take some
  614. getting used to, but to properly use a general ledger you must think
  615. in terms of how money moves. Rather than Ledger ``fixing'' the minus
  616. signs, let's understand why they are there.
  617. When you earn money, the money has to come from somewhere. Let's call
  618. that somewhere ``society''. In order for society to give you an
  619. income, you must take money away (withdraw) from society in order to
  620. put it into (make a payment to) your bank. When you then spend that
  621. money, it leaves your bank account (a withdrawal) and goes back to
  622. society (a payment). This is why Income will appear negative---it
  623. reflects the money you have drawn from society---and why Expenses will
  624. be positive---it is the amount you've given back. These additions and
  625. subtractions will always cancel each other out in the end, because you
  626. don't have the ability to create new money: it must always come from
  627. somewhere, and in the end must always leave. This is the beginning of
  628. economy, after which the explanation gets terribly difficult.
  629. Based on that explanation, here's another way to look at your balance
  630. report: every negative figure means that that account or person or
  631. place has less money now than when you started your ledger; and every
  632. positive figure means that that account or person or place has more
  633. money now than when you started your ledger. Make sense?
  634. @node Assets and Liabilities, Commodities and Currencies, Stating where money goes, Principles of Accounting with Ledger
  635. @section Assets and Liabilities
  636. @cindex assets and liabilities
  637. @cindex debts are liabilities
  638. Assets are money that you have, and Liabilities are money that you
  639. owe. ``Liabilities'' is just a more inclusive name for Debts.
  640. An Asset is typically increased by transferring money from an Income
  641. account, such as when you get paid. Here is a typical transaction:
  642. @smallexample @c input:6B43DD4
  643. 2004/09/29 My Employer
  644. Assets:Checking $500.00
  645. Income:Salary
  646. @end smallexample
  647. Money, here, comes from an Income account belonging to @samp{My
  648. Employer}, and is transferred to your checking account. The money is
  649. now yours, which makes it an Asset.
  650. Liabilities track money owed to others. This can happen when you
  651. borrow money to buy something, or if you owe someone money. Here is
  652. an example of increasing a MasterCard liability by spending money with
  653. it:
  654. @smallexample @c input:6B43DD4
  655. 2004/09/30 Restaurant
  656. Expenses:Dining $25.00
  657. Liabilities:MasterCard
  658. @end smallexample
  659. The Dining account balance now shows $25 spent on Dining, and
  660. a corresponding $25 owed on the MasterCard---and therefore shown as
  661. $-25.00. The MasterCard liability shows up as negative because it
  662. offsets the value of your assets.
  663. The combined total of your Assets and Liabilities is your net worth.
  664. So to see your current net worth, use this command:
  665. @smallexample @c command:6B43DD4
  666. $ ledger balance ^assets ^liabilities
  667. @end smallexample
  668. @smallexample @c output:6B43DD4
  669. $500.00 Assets:Checking
  670. $-25.00 Liabilities:MasterCard
  671. --------------------
  672. $475.00
  673. @end smallexample
  674. In a similar vein, your Income accounts show up negative, because they
  675. transfer money @emph{from} an account in order to increase your
  676. assets. Your Expenses show up positive because that is where the
  677. money went to. The combined total of Income and Expenses is your cash
  678. flow. A positive cash flow means you are spending more than you make,
  679. since income is always a negative figure. To see your current cash
  680. flow, use this command:
  681. @smallexample @c command:DB128F3,with_input:6B43DD4
  682. $ ledger balance ^income ^expenses
  683. @end smallexample
  684. @smallexample @c output:DB128F3
  685. $25.00 Expenses:Dining
  686. $-500.00 Income:Salary
  687. --------------------
  688. $-475.00
  689. @end smallexample
  690. Another common question to ask of your expenses is: How much do I
  691. spend each month on X? Ledger provides a simple way of displaying
  692. monthly totals for any account. Here is an example that summarizes
  693. your monthly automobile expenses:
  694. @smallexample @c command:DB524E4
  695. $ ledger -M register -f drewr3.dat expenses:auto
  696. @end smallexample
  697. @smallexample @c output:DB524E4
  698. 11-Jan-01 - 11-Jan-31 Expenses:Auto $ 5,500.00 $ 5,500.00
  699. @end smallexample
  700. This assumes, of course, that you use account names like
  701. @samp{Expenses:Auto:Gas} and @samp{Expenses:Auto:Repair}.
  702. @menu
  703. * Tracking reimbursable expenses::
  704. @end menu
  705. @node Tracking reimbursable expenses, , Assets and Liabilities, Assets and Liabilities
  706. @subsection Tracking reimbursable expenses
  707. @cindex reimbursable expense tracking
  708. Sometimes you will want to spend money on behalf of someone else, which
  709. will eventually get repaid. Since the money is still @emph{yours}, it
  710. is really an asset. And since the expenditure was for someone else, you
  711. don't want it contaminating your Expenses reports. You will need to
  712. keep an account for tracking reimbursements.
  713. This is fairly easy to do in ledger. When spending the money, spend
  714. it @emph{to} your Assets:Reimbursements, using a different account for
  715. each person or business that you spend money for. For example:
  716. @smallexample @c input:validate
  717. 2004/09/29 Circuit City
  718. Assets:Reimbursements:Company XYZ $100.00
  719. Liabilities:MasterCard
  720. @end smallexample
  721. This shows $100.00 spent on a MasterCard at Circuit City, with the
  722. expense was made on behalf of Company XYZ. Later, when Company XYZ
  723. pays the amount back, the money will transfer from that reimbursement
  724. account back to a regular asset account:
  725. @smallexample @c input:validate
  726. 2004/09/29 Company XYZ
  727. Assets:Checking $100.00
  728. Assets:Reimbursements:Company XYZ
  729. @end smallexample
  730. This deposits the money owed from Company XYZ into a checking account,
  731. presumably because they paid the amount back with a check.
  732. But what to do if you run your own business, and you want to keep
  733. track of expenses made on your own behalf, while still tracking
  734. everything in a single ledger file? This is more complex, because you
  735. need to track two separate things: 1) The fact that the money should
  736. be reimbursed to you, and 2) What the expense account was, so that you
  737. can later determine where your company is spending its money.
  738. This kind of posting is best handled with mirrored postings in
  739. two different files, one for your personal accounts, and one for your
  740. company accounts. But keeping them in one file involves the same
  741. kinds of postings, so those are what is shown here. First, the
  742. personal transaction, which shows the need for reimbursement:
  743. @smallexample @c input:validate
  744. 2004/09/29 Circuit City
  745. Assets:Reimbursements:Company XYZ $100.00
  746. Liabilities:MasterCard
  747. @end smallexample
  748. This is the same as above, except that you own Company XYZ, and are
  749. keeping track of its expenses in the same ledger file. This
  750. transaction should be immediately followed by an equivalent
  751. transaction, which shows the kind of expense, and also notes the fact
  752. that $100.00 is now payable to you:
  753. @smallexample @c input:validate
  754. 2004/09/29 Circuit City
  755. Company XYZ:Expenses:Computer:Software $100.00
  756. Company XYZ:Accounts Payable:Your Name
  757. @end smallexample
  758. This second transaction shows that Company XYZ has just spent $100.00
  759. on software, and that this $100.00 came from Your Name, which must be
  760. paid back.
  761. These two transactions can also be merged, to make things a little
  762. clearer. Note that all amounts must be specified now:
  763. @smallexample @c input:validate
  764. 2004/09/29 Circuit City
  765. Assets:Reimbursements:Company XYZ $100.00
  766. Liabilities:MasterCard $-100.00
  767. Company XYZ:Expenses:Computer:Software $100.00
  768. Company XYZ:Accounts Payable:Your Name $-100.00
  769. @end smallexample
  770. To ``pay back'' the reimbursement, just reverse the order of
  771. everything, except this time drawing the money from a company asset,
  772. paying it to accounts payable, and then drawing it again from the
  773. reimbursement account, and paying it to your personal asset account.
  774. It's easier shown than said:
  775. @smallexample @c input:validate
  776. 2004/10/15 Company XYZ
  777. Assets:Checking $100.00
  778. Assets:Reimbursements:Company XYZ $-100.00
  779. Company XYZ:Accounts Payable:Your Name $100.00
  780. Company XYZ:Assets:Checking $-100.00
  781. @end smallexample
  782. And now the reimbursements account is paid off, accounts payable is paid
  783. off, and $100.00 has been effectively transferred from the company's
  784. checking account to your personal checking account. The money simply
  785. ``waited''---in both @samp{Assets:Reimbursements:Company XYZ}, and
  786. @samp{Company XYZ:Accounts Payable:Your Name}---until such time as it
  787. could be paid off.
  788. The value of tracking expenses from both sides like that is that you
  789. do not contaminate your personal expense report with expenses made on
  790. behalf of others, while at the same time making it possible to
  791. generate accurate reports of your company's expenditures. It is more
  792. verbose than just paying for things with your personal assets, but it
  793. gives you a very accurate information trail.
  794. The advantage to keep these doubled transactions together is that they
  795. always stay in sync. The advantage to keeping them apart is that it
  796. clarifies the transfer's point of view. To keep the postings in
  797. separate files, just separate the two transactions that were joined
  798. above. For example, for both the expense and the pay-back shown
  799. above, the following four transactions would be created. Two in your
  800. personal ledger file:
  801. @smallexample @c input:990E0D1
  802. 2004/09/29 Circuit City
  803. Assets:Reimbursements:Company XYZ $100.00
  804. Liabilities:MasterCard $-100.00
  805. 2004/10/15 Company XYZ
  806. Assets:Checking $100.00
  807. Assets:Reimbursements:Company XYZ $-100.00
  808. @end smallexample
  809. And two in your company ledger file:
  810. @smallexample @c input:990E0D1
  811. apply account Company XYZ
  812. 2004/09/29 Circuit City
  813. Expenses:Computer:Software $100.00
  814. Accounts Payable:Your Name $-100.00
  815. 2004/10/15 Company XYZ
  816. Accounts Payable:Your Name $100.00
  817. Assets:Checking $-100.00
  818. end apply account
  819. @end smallexample
  820. (Note: The @code{apply account} above means that all accounts
  821. mentioned in the file are children of that account. In this case it
  822. means that all activity in the file relates to Company XYZ).
  823. After creating these transactions, you will always know that $100.00
  824. was spent using your MasterCard on behalf of Company XYZ, and that
  825. Company XYZ spent the money on computer software and paid it back
  826. about two weeks later.
  827. @smallexample @c command:990E0D1
  828. $ ledger balance --no-total
  829. @end smallexample
  830. @smallexample @c output:990E0D1
  831. $100.00 Assets:Checking
  832. 0 Company XYZ
  833. $-100.00 Assets:Checking
  834. $100.00 Expenses:Computer:Software
  835. $-100.00 Liabilities:MasterCard
  836. @end smallexample
  837. @node Commodities and Currencies, Accounts and Inventories, Assets and Liabilities, Principles of Accounting with Ledger
  838. @section Commodities and Currencies
  839. Ledger makes no assumptions about the commodities you use; it only
  840. requires that you specify a commodity. The commodity may be any
  841. non-numeric string that does not contain a period, comma, forward
  842. slash or at-sign. It may appear before or after the amount, although
  843. it is assumed that symbols appearing before the amount refer to
  844. currencies, while non-joined symbols appearing after the amount refer
  845. to commodities. Here are some valid currency and commodity
  846. specifiers:
  847. @smallexample
  848. $20.00 ; currency: twenty US dollars
  849. 40 AAPL ; commodity: 40 shares of Apple stock
  850. 60 DM ; currency: 60 Deutsch Mark
  851. £50 ; currency: 50 British pounds
  852. 50 EUR ; currency: 50 Euros (or use appropriate symbol)
  853. @end smallexample
  854. Ledger will examine the first use of any commodity to determine how
  855. that commodity should be printed on reports. It pays attention to
  856. whether the name of commodity was separated from the amount, whether
  857. it came before or after, the precision used in specifying the amount,
  858. whether thousand marks were used, etc. This is done so that printing
  859. the commodity looks the same as the way you use it.
  860. An account may contain multiple commodities, in which case it will
  861. have separate totals for each. For example, if your brokerage account
  862. contains both cash, gold, and several stock quantities, the balance
  863. might look like:
  864. @smallexample
  865. $200.00
  866. 100.00 AU
  867. AAPL 40
  868. BORL 100
  869. FEQTX 50 Assets:Brokerage
  870. @end smallexample
  871. This balance report shows how much of each commodity is in your
  872. brokerage account.
  873. Sometimes, you will want to know the current street value of your
  874. balance, and not the commodity totals. For this to happen, you must
  875. specify what the current price is for each commodity. The price can
  876. be any commodity, in which case the balance will be computed in terms
  877. of that commodity. The usual way to specify prices is with a price
  878. history file, which might look like this:
  879. @smallexample @c input:validate
  880. P 2004/06/21 02:18:01 FEQTX $22.49
  881. P 2004/06/21 02:18:01 BORL $6.20
  882. P 2004/06/21 02:18:02 AAPL $32.91
  883. P 2004/06/21 02:18:02 AU $400.00
  884. @end smallexample
  885. @findex --price-db @var{FILE}
  886. @findex --market
  887. Specify the price history to use with the @option{--price-db @var{FILE}}
  888. option, with the @option{--market (-V)} option to report in terms of
  889. current market value:
  890. @smallexample
  891. $ ledger --price-db prices.db -V balance brokerage
  892. @end smallexample
  893. The balance for your brokerage account will be reported in US dollars,
  894. since the prices database uses that currency.
  895. @smallexample
  896. $40880.00 Assets:Brokerage
  897. @end smallexample
  898. You can convert from any commodity to any other commodity. Let's say
  899. you had $5000 in your checking account, and for whatever reason you
  900. wanted to know many ounces of gold that would buy, in terms of the
  901. current price of gold:
  902. @smallexample
  903. $ ledger -T "@{1 AU@}*(O/P@{1 AU@})" balance checking
  904. @end smallexample
  905. Although the total expression appears complex, it is simply saying
  906. that the reported total should be in multiples of AU units, where the
  907. quantity is the account total divided by the price of one AU. Without
  908. the initial multiplication, the reported total would still use the
  909. dollars commodity, since multiplying or dividing amounts always keeps
  910. the left value's commodity. The result of this command might be:
  911. @smallexample
  912. 14.01 AU Assets:Checking
  913. @end smallexample
  914. @menu
  915. * Commodity price histories::
  916. * Commodity equivalences::
  917. @end menu
  918. @node Commodity price histories, Commodity equivalences, Commodities and Currencies, Commodities and Currencies
  919. @subsection Commodity price histories
  920. Whenever a commodity is purchased using a different commodity (such as
  921. a share of common stock using dollars), it establishes a price for
  922. that commodity on that day. It is also possible, by recording price
  923. details in a ledger file, to specify other prices for commodities at
  924. any given time. Such price transactions might look like those below:
  925. @smallexample @c input:validate
  926. P 2004/06/21 02:17:58 TWCUX $27.76
  927. P 2004/06/21 02:17:59 AGTHX $25.41
  928. P 2004/06/21 02:18:00 OPTFX $39.31
  929. P 2004/06/21 02:18:01 FEQTX $22.49
  930. P 2004/06/21 02:18:02 AAPL $32.91
  931. @end smallexample
  932. By default, ledger will not consider commodity prices when generating
  933. its various reports. It will always report balances in terms of the
  934. commodity total, rather than the current value of those commodities.
  935. To enable pricing reports, use one of the commodity reporting options.
  936. @node Commodity equivalences, , Commodity price histories, Commodities and Currencies
  937. @subsection Commodity equivalences
  938. Sometimes a commodity has several forms which are all equivalent. An
  939. example of this is time. Whether tracked in terms of minutes, hours
  940. or days, it should be possible to convert between the various forms.
  941. Doing this requires the use of commodity equivalences.
  942. For example, you might have the following two postings, one which
  943. transfers an hour of time into a @samp{Billable} account, and another
  944. which decreases the same account by ten minutes. The resulting report
  945. will indicate that fifty minutes remain:
  946. @smallexample @c input:DF3FEBE
  947. 2005/10/01 Work done for company
  948. Billable:Client 1h
  949. Project:XYZ
  950. 2005/10/02 Return ten minutes to the project
  951. Project:XYZ 10m
  952. Billable:Client
  953. @end smallexample
  954. Reporting the balance for this ledger file produces:
  955. @smallexample @c command:DF3FEBE
  956. $ ledger --no-total balance Billable Project
  957. @end smallexample
  958. @smallexample @c output:DF3FEBE
  959. 50.0m Billable:Client
  960. -50.0m Project:XYZ
  961. @end smallexample
  962. @findex C
  963. This example works because ledger already knows how to handle seconds,
  964. minutes and hours, as part of its time tracking support. Defining
  965. other equivalences is simple. The following is an example that
  966. creates data equivalences, helpful for tracking bytes, kilobytes,
  967. megabytes, and more:
  968. @smallexample @c input:validate
  969. C 1.00 Kb = 1024 b
  970. C 1.00 Mb = 1024 Kb
  971. C 1.00 Gb = 1024 Mb
  972. C 1.00 Tb = 1024 Gb
  973. @end smallexample
  974. Each of these definitions correlates a commodity (such as @samp{Kb})
  975. and a default precision, with a certain quantity of another commodity.
  976. In the above example, kilobytes are reported with two decimal places
  977. of precision and each kilobyte is equal to 1024 bytes.
  978. Equivalence chains can be as long as desired. Whenever a commodity
  979. would report as a decimal amount (less than @samp{1.00}), the next
  980. smallest commodity is used. If a commodity could be reported in terms
  981. of a higher commodity without resulting to a partial fraction, then
  982. the larger commodity is used.
  983. @node Accounts and Inventories, Understanding Equity, Commodities and Currencies, Principles of Accounting with Ledger
  984. @section Accounts and Inventories
  985. Since Ledger's accounts and commodity system is so flexible, you can
  986. have accounts that don't really exist, and use commodities that no one
  987. else recognizes. For example, let's say you are buying and selling
  988. various items in EverQuest, and want to keep track of them using a
  989. ledger. Just add items of whatever quantity you wish into your
  990. EverQuest account:
  991. @smallexample @c input:48F4E47
  992. 9/29 Get some stuff at the Inn
  993. Places:Black's Tavern -3 Apples
  994. Places:Black's Tavern -5 Steaks
  995. EverQuest:Inventory
  996. @end smallexample
  997. Now your EverQuest:Inventory has 3 apples and 5 steaks in it. The
  998. amounts are negative, because you are taking @emph{from} Black's
  999. Tavern in order to add to your Inventory account. Note that you don't
  1000. have to use @samp{Places:Black's Tavern} as the source account. You
  1001. could use @samp{EverQuest:System} to represent the fact that you
  1002. acquired them online. The only purpose for choosing one kind of
  1003. source account over another is to generate more informative reports
  1004. later on. The more you know, the better the analysis you can perform.
  1005. If you later sell some of these items to another player, the
  1006. transaction would look like:
  1007. @smallexample @c input:48F4E47
  1008. 10/2 Sturm Brightblade
  1009. EverQuest:Inventory -2 Steaks
  1010. EverQuest:Inventory 15 Gold
  1011. @end smallexample
  1012. Now you've turned 2 steaks into 15 gold, courtesy of your customer,
  1013. Sturm Brightblade.
  1014. @smallexample @c command:48F4E47
  1015. $ ledger balance EverQuest
  1016. @end smallexample
  1017. @smallexample @c output:48F4E47
  1018. 3 Apples
  1019. 15 Gold
  1020. 3 Steaks EverQuest:Inventory
  1021. @end smallexample
  1022. @node Understanding Equity, Dealing with Petty Cash, Accounts and Inventories, Principles of Accounting with Ledger
  1023. @section Understanding Equity
  1024. The most confusing transaction in any ledger will be your equity
  1025. account---because starting balances can't come out of nowhere.
  1026. When you first start your ledger, you will likely already have money
  1027. in some of your accounts. Let's say there's $100 in your checking
  1028. account; then add a transaction to your ledger to reflect this amount.
  1029. Where will the money come from? The answer: your equity.
  1030. @smallexample @c input:validate
  1031. 10/2 Opening Balance
  1032. Assets:Checking $100.00
  1033. Equity:Opening Balances
  1034. @end smallexample
  1035. But what is equity? You may have heard of equity when people talked
  1036. about house mortgages, as ``the part of the house that you own''.
  1037. Basically, equity is like the value of something. If you own a car
  1038. worth $5000, then you have $5000 in equity in that car. In order to
  1039. turn that car (a commodity) into a cash flow, or a credit to your bank
  1040. account, you will have to debit the equity by selling it.
  1041. When you start a ledger, you are probably already worth something.
  1042. Your net worth is your current equity. By transferring the money in
  1043. the ledger from your equity to your bank accounts, you are crediting
  1044. the ledger account based on your prior equity. That is why, when you
  1045. look at the balance report, you will see a large negative number for
  1046. Equity that never changes: Because that is what you were worth (what
  1047. you debited from yourself in order to start the ledger) before the
  1048. money started moving around. If the total positive value of your
  1049. assets is greater than the absolute value of your starting equity, it
  1050. means you are making money.
  1051. Clear as mud? Keep thinking about it. Until you figure it out, put
  1052. @code{not Equity} at the end of your balance command, to remove the
  1053. confusing figure from the total.
  1054. @node Dealing with Petty Cash, Working with multiple funds and accounts, Understanding Equity, Principles of Accounting with Ledger
  1055. @section Dealing with Petty Cash
  1056. Something that stops many people from keeping a ledger at all is the
  1057. insanity of tracking small cash expenses. They rarely generate a
  1058. receipt, and there are often a lot of small postings, rather than
  1059. a few large ones, as with checks.
  1060. One solution is: don't bother. Move your spending to a debit card,
  1061. but in general ignore cash. Once you withdraw it from the ATM, mark
  1062. it as already spent to an @samp{Expenses:Cash} category:
  1063. @smallexample @c input:validate
  1064. 2004/03/15 ATM
  1065. Expenses:Cash $100.00
  1066. Assets:Checking
  1067. @end smallexample
  1068. If at some point you make a large cash expense that you want to track,
  1069. just @emph{move} the amount of the expense from @samp{Expenses:Cash}
  1070. into the target account:
  1071. @smallexample @c input:validate
  1072. 2004/03/20 Somebody
  1073. Expenses:Food $65.00
  1074. Expenses:Cash
  1075. @end smallexample
  1076. This way, you can still track large cash expenses, while ignoring all
  1077. of the smaller ones.
  1078. @node Working with multiple funds and accounts, , Dealing with Petty Cash, Principles of Accounting with Ledger
  1079. @section Working with multiple funds and accounts
  1080. There are situations when the accounts you're tracking are different
  1081. between your clients and the financial institutions where money is
  1082. kept. An example of this is working as the treasurer for a religious
  1083. institution. From the secular point of view, you might be working
  1084. with three different accounts:
  1085. @itemize
  1086. @item Checking
  1087. @item Savings
  1088. @item Credit Card
  1089. @end itemize
  1090. From a religious point of view, the community expects to divide its
  1091. resources into multiple ``funds'', from which it makes purchases or
  1092. reserves resources for later:
  1093. @itemize
  1094. @item School fund
  1095. @item Building fund
  1096. @item Community fund
  1097. @end itemize
  1098. The problem with this kind of setup is that, when you spend money, it
  1099. comes from two or more places at once: the account and the fund. And
  1100. yet, the correlation of amounts between funds and accounts is rarely
  1101. one-to-one. What if the school fund has @samp{$500.00}, but
  1102. @samp{$400.00} of that comes from Checking, and @samp{$100.00} from
  1103. Savings?
  1104. Traditional finance packages require that the money reside in only one
  1105. place. But there are really two ``views'' of the data: from the
  1106. account point of view and from the fund point of view---yet both sets
  1107. should reflect the same overall expenses and cash flow. It's simply
  1108. where the money resides that differs.
  1109. This situation can be handled one of two ways. The first is using
  1110. virtual postings to represent the fact that money is moving to and
  1111. from two kind of accounts at the same time:
  1112. @smallexample @c input:396F24E
  1113. 2004/03/20 Contributions
  1114. Assets:Checking $500.00
  1115. Income:Donations
  1116. 2004/03/25 Distribution of donations
  1117. [Funds:School] $300.00
  1118. [Funds:Building] $200.00
  1119. [Assets:Checking] $-500.00
  1120. @end smallexample
  1121. The use of square brackets in the second transaction ensures that the
  1122. virtual postings balance to zero. Now money can be spent directly
  1123. from a fund at the same time as money is drawn from a physical
  1124. account:
  1125. @smallexample @c input:396F24E
  1126. 2004/03/25 Payment for books (paid from Checking)
  1127. Expenses:Books $100.00
  1128. Assets:Checking $-100.00
  1129. (Funds:School) $-100.00
  1130. @end smallexample
  1131. When reports are generated, by default they'll appear in terms of the
  1132. funds. In this case, you will likely want to mask out your
  1133. @samp{Assets} account, because otherwise the balance won't make much
  1134. sense:
  1135. @smallexample @c command:396F24E
  1136. $ ledger --no-total bal not ^Assets
  1137. @end smallexample
  1138. @smallexample @c output:396F24E
  1139. $100.00 Expenses:Books
  1140. $400.00 Funds
  1141. $200.00 Building
  1142. $200.00 School
  1143. $-500.00 Income:Donations
  1144. @end smallexample
  1145. @findex --real
  1146. If the @option{--real} option is used, the report will be in terms of
  1147. the real accounts:
  1148. @smallexample @c command:2F1CB75,with_input:396F24E
  1149. $ ledger --real --no-total bal
  1150. @end smallexample
  1151. @smallexample @c output:2F1CB75
  1152. $400.00 Assets:Checking
  1153. $100.00 Expenses:Books
  1154. $-500.00 Income:Donations
  1155. @end smallexample
  1156. If more asset accounts are needed as the source of a posting, just
  1157. list them as you would normally, for example:
  1158. @smallexample @c input:validate
  1159. 2004/03/25 Payment for books (paid from Checking)
  1160. Expenses:Books $100.00
  1161. Assets:Checking $-50.00
  1162. Liabilities:Credit Card $-50.00
  1163. (Funds:School) $-100.00
  1164. @end smallexample
  1165. The second way of tracking funds is to use transaction codes. In this
  1166. respect the codes become like virtual accounts that embrace the entire
  1167. set of postings. Basically, we are associating a transaction with a
  1168. fund by setting its code. Here are two transactions that deposit money
  1169. into, and spend money from, the @samp{Funds:School} fund:
  1170. @smallexample @c input:AD068BA
  1171. 2004/03/25 (Funds:School) Donations
  1172. Assets:Checking $100.00
  1173. Income:Donations
  1174. 2004/03/25 (Funds:Building) Donations
  1175. Assets:Checking $20.00
  1176. Income:Donations
  1177. 2004/04/25 (Funds:School) Payment for books
  1178. Expenses:Books $50.00
  1179. Assets:Checking
  1180. @end smallexample
  1181. Note how the accounts now relate only to the real accounts, and any
  1182. balance or register reports will reflect this. That the transactions
  1183. relate to a particular fund is kept only in the code.
  1184. @findex --payee=code
  1185. @findex --by-payee
  1186. How does this become a fund report? By using the
  1187. @option{--payee=code} option, you can generate a register report
  1188. where the payee for each posting shows the code. Alone, this is not
  1189. terribly interesting; but when combined with the @option{--by-payee
  1190. (-P)} option, you will now see account subtotals for any postings
  1191. related to a specific fund. So, to see the current monetary balances of
  1192. all funds, the command would be:
  1193. @smallexample @c command:AD068BA
  1194. $ ledger --payee=code -P reg ^Assets
  1195. @end smallexample
  1196. @smallexample @c output:AD068BA
  1197. 04-Mar-25 Funds:Building Assets:Checking $20.00 $20.00
  1198. 04-Mar-25 Funds:School Assets:Checking $50.00 $70.00
  1199. @end smallexample
  1200. Or to see a particular fund's expenses, the @samp{School} fund in this
  1201. case:
  1202. @smallexample @c command:E30B2FC,with_input:AD068BA
  1203. $ ledger --payee=code -P reg ^Expenses and code School
  1204. @end smallexample
  1205. @smallexample @c output:E30B2FC
  1206. 04-Apr-25 Funds:School Expenses:Books $50.00 $50.00
  1207. @end smallexample
  1208. Both approaches yield different kinds of flexibility, depending on how
  1209. you prefer to think of your funds: as virtual accounts, or as tags
  1210. associated with particular transactions. Your own tastes will decide
  1211. which is best for your situation.
  1212. @node Keeping a Journal, Transactions, Principles of Accounting with Ledger, Top
  1213. @chapter Keeping a Journal
  1214. The most important part of accounting is keeping a good journal. If
  1215. you have a good journal, tools can be written to work whatever
  1216. mathematical tricks you need to better understand your spending
  1217. patterns. Without a good journal, no tool, however smart, can help
  1218. you.
  1219. The Ledger program aims at making journal transactions as simple as
  1220. possible. Since it is a command-line tool, it does not provide a user
  1221. interface for keeping a journal. If you require an user interface to
  1222. maintain journal transactions GnuCash is a good alternative.
  1223. If you are not using GnuCash, but a text editor to maintain your
  1224. journal, read on. Ledger has been designed to make data transactions
  1225. as simple as possible, by keeping the journal format easy, and also by
  1226. automagically determining as much information as possible based on the
  1227. nature of your transactions.
  1228. For example, you do not need to tell Ledger about the accounts you
  1229. use. Any time Ledger sees a posting involving an account it knows
  1230. nothing about, it will create it@footnote{This also means if you
  1231. misspell an account it will end up getting counted separately from what
  1232. you intended. The provided Emacs major mode provides for automatically
  1233. filling in account names.}. If you use a commodity that is new to
  1234. Ledger, it will create that commodity, and determine its display
  1235. characteristics (placement of the symbol before or after the amount,
  1236. display precision, etc.) based on how you used the commodity in the
  1237. posting.
  1238. @menu
  1239. * The Most Basic Entry::
  1240. * Starting up::
  1241. * Structuring your Accounts::
  1242. * Commenting on your Journal::
  1243. * Currency and Commodities::
  1244. * Keeping it Consistent::
  1245. * Journal Format::
  1246. * Converting from other formats::
  1247. * Archiving Previous Years::
  1248. @end menu
  1249. @node The Most Basic Entry, Starting up, Keeping a Journal, Keeping a Journal
  1250. @section The Most Basic Entry
  1251. Here is the Pacific Bell example from above, given as a Ledger
  1252. posting, with the addition of a check number:
  1253. @smallexample @c input:validate
  1254. 9/29 (1023) Pacific Bell
  1255. Expenses:Utilities:Phone $23.00
  1256. Assets:Checking $-23.00
  1257. @end smallexample
  1258. As you can see, it is very similar to what would be written on paper,
  1259. minus the computed balance totals, and adding in account names that
  1260. work better with Ledger's scheme of things. In fact, since Ledger is
  1261. smart about many things, you don't need to specify the balanced
  1262. amount, if it is the same as the first line:
  1263. @smallexample @c input:validate
  1264. 9/29 (1023) Pacific Bell
  1265. Expenses:Utilities:Phone $23.00
  1266. Assets:Checking
  1267. @end smallexample
  1268. For this transaction, Ledger will figure out that $-23.00 must come
  1269. from @samp{Assets:Checking} in order to balance the transaction.
  1270. Also note the structure of the account entries. There is an implied
  1271. hierarchy established by separating with colons (@pxref{Structuring your
  1272. Accounts}).
  1273. @cindex spaces in postings
  1274. @cindex posting format details
  1275. @strong{The format is very flexible and it isn't necessary that you
  1276. indent and space out things exactly as shown. The only requirements
  1277. are that the start of the transaction (the date typically) is at the
  1278. beginning of the first line of the transaction, and the accounts are
  1279. indented by at least one space. If you omit the leading spaces in the
  1280. account lines Ledger will generate an error. There must be at least
  1281. two spaces, or a tab, between the amount and the account. If you do
  1282. not have adequate separation between the amount and the account Ledger
  1283. will give an error and stop calculating.}
  1284. @node Starting up, Structuring your Accounts, The Most Basic Entry, Keeping a Journal
  1285. @section Starting up
  1286. @cindex initial equity
  1287. @cindex beginning ledger
  1288. @cindex opening balance
  1289. Unless you have recently arrived from another planet, you already have
  1290. a financial state. You need to capture that financial state so that
  1291. Ledger has a starting point.
  1292. At some convenient point in time you knew the balances and outstanding
  1293. obligation of every financial account you have. Those amounts form the
  1294. basis of the opening entry for ledger. For example if you chose the
  1295. beginning of 2011 as the date to start tracking finances with ledger,
  1296. your opening balance entry could look like this:
  1297. @smallexample @c input:validate
  1298. 2011/01/01 * Opening Balance
  1299. Assets:Joint Checking $800.14
  1300. Assets:Other Checking $63.44
  1301. Assets:Savings $2805.54
  1302. Assets:Investments:401K:Deferred 100.0000 VIFSX @@ $80.5227
  1303. Assets:Investments:401K:Matching 50.0000 VIFSX @@ $83.7015
  1304. Assets:Investments:IRA 250.0000 VTHRX @@ $20.5324
  1305. Liabilities:Mortgage $-175634.88
  1306. Liabilities:Car Loan $-3494.26
  1307. Liabilities:Visa -$1762.44
  1308. Equity:Opening Balances
  1309. @end smallexample
  1310. There is nothing special about the name ``Opening Balances'' as the
  1311. payee of the account name, anything convenient that you understand will
  1312. work.
  1313. @node Structuring your Accounts, Commenting on your Journal, Starting up, Keeping a Journal
  1314. @section Structuring your Accounts
  1315. @cindex accounts, naming
  1316. @cindex naming accounts
  1317. There really are no requirements for how you do this, but to preserve
  1318. your sanity we suggest some very basic structure to your accounting
  1319. system.
  1320. At the highest level you have five sorts of accounts:
  1321. @enumerate
  1322. @item Expenses: where money goes,
  1323. @item Assets: where money sits,
  1324. @item Income: where money comes from,
  1325. @item Liabilities: money you owe,
  1326. @item Equity: the real value of your property.
  1327. @end enumerate
  1328. Starting the structure off this way will make it simpler for you to get
  1329. answers to the questions you really need to ask about your finances.
  1330. Beneath these top level accounts you can have any level of detail you
  1331. desire. For example, if you want to keep specific track of how much
  1332. you spend on burgers and fries, you could have the following:
  1333. @smallexample @c input:validate
  1334. Expenses:Food:Hamburgers and Fries
  1335. @end smallexample
  1336. @node Commenting on your Journal, Currency and Commodities, Structuring your Accounts, Keeping a Journal
  1337. @section Commenting on your Journal
  1338. @cindex comments, characters
  1339. @cindex block comments
  1340. @cindex comments, block
  1341. Comments are generally started using a @samp{;}. However, in order to
  1342. increase compatibility with other text manipulation programs and
  1343. methods, four additional comment characters are valid if used at the
  1344. beginning of a line: @samp{#}, @samp{|}, and @samp{*} and @samp{%}.
  1345. Block comments can be made by use @code{comment} ... @code{end
  1346. comment}.
  1347. @smallexample @c input:validate
  1348. ; This is a single line comment,
  1349. # and this,
  1350. % and this,
  1351. | and this,
  1352. * and this.
  1353. comment
  1354. This is a block comment with
  1355. multiple lines
  1356. end comment
  1357. @end smallexample
  1358. There are several forms of comments within a transaction, for example:
  1359. @smallexample @c input:validate
  1360. ; this is a global comment that is not applied to a specific transaction
  1361. ; it can start with any of the five characters but is not included in the
  1362. ; output from 'print' or 'output'
  1363. 2011/12/11 Something Sweet
  1364. ; German Chocolate Cake
  1365. ; :Broke Diet:
  1366. Expenses:Food $10.00 ; Friends: The gang
  1367. Assets:Credit Union:Checking
  1368. @end smallexample
  1369. @noindent
  1370. The first comment is global and Ledger will not attach it to any
  1371. specific transactions. The comments within the transaction must all
  1372. start with @samp{;} and are preserved as part of the transaction. The
  1373. @samp{:} indicates meta-data and tags (@pxref{Metadata}).
  1374. @node Currency and Commodities, Keeping it Consistent, Commenting on your Journal, Keeping a Journal
  1375. @section Currency and Commodities
  1376. @cindex currency
  1377. @cindex commodity
  1378. Ledger is agnostic when it comes to how you value your accounts.
  1379. Dollars, Euros, Pounds, Francs, Shares etc. are all just ``commodities''.
  1380. Holdings in stocks, bonds, mutual funds and other financial
  1381. instruments can be labeled using whatever is convenient for you (stock
  1382. ticker symbols are suggested for publicly traded assets).@footnote{You
  1383. can track @emph{anything}, even time or distance traveled. As long as
  1384. it cannot be created or destroyed inside your accounting system.}
  1385. For the rest of this manual, we will only use the word ``commodities''
  1386. when referring to the units on a transaction value.
  1387. This is fundamentally different than many common accounting packages,
  1388. which assume the same currency throughout all of your accounts. This
  1389. means if you typically operate in Euros, but travel to the US and have
  1390. some expenses, you would have to do the currency conversion
  1391. @emph{before} you made the entry into your financial system. With
  1392. ledger this is not required. In the same journal you can have entries
  1393. in any or all commodities you actually hold. You can use the
  1394. reporting capabilities to convert all commodities to a single
  1395. commodity for reporting purposes without ever changing the underlying
  1396. entry.
  1397. For example, the following entries reflect transactions made for a
  1398. business trip to Europe from the US:
  1399. @smallexample @c input:82150D9
  1400. 2011/09/23 Cash in Munich
  1401. Assets:Cash €50.00
  1402. Assets:Checking $-66.00
  1403. 2011/09/24 Dinner in Munich
  1404. Expenses:Business:Travel €35.00
  1405. Assets:Cash
  1406. @end smallexample
  1407. This says that $66.00 came out of checking and turned into 50
  1408. Euros. The implied exchange rate was $1.32. Then 35.00 Euros were
  1409. spent on Dinner in Munich.
  1410. Running a ledger balance report shows:
  1411. @smallexample @c command:82150D9
  1412. $ ledger -f example.dat bal
  1413. @end smallexample
  1414. @smallexample @c output:82150D9
  1415. $-66.00
  1416. €15.00 Assets
  1417. €15.00 Cash
  1418. $-66.00 Checking
  1419. €35.00 Expenses:Business:Travel
  1420. --------------------
  1421. $-66.00
  1422. €50.00
  1423. @end smallexample
  1424. The top two lines show my current assets as $-66.00 in checking (in
  1425. this very short example I didn't establish opening an opening balance
  1426. for the checking account) and €15.00. After spending on dinner I have
  1427. €15.00 in my wallet. The bottom line balances to zero, but is shown
  1428. in two lines since we haven't told ledger to convert commodities.
  1429. @menu
  1430. * Naming Commodities::
  1431. * Buying and Selling Stock::
  1432. * Fixing Lot Prices::
  1433. * Complete control over commodity pricing::
  1434. @end menu
  1435. @node Naming Commodities, Buying and Selling Stock, Currency and Commodities, Currency and Commodities
  1436. @subsection Naming Commodities
  1437. Commodity names can have any character, including white-space.
  1438. However, if you include white-space or numeric characters, the
  1439. commodity name must be enclosed in double quotes @samp{"}:
  1440. @smallexample @c input:validate
  1441. 1999/06/09 ! Achat
  1442. Actif:SG PEE STK 49.957 "Arcancia Équilibre 454"
  1443. Actif:SG PEE STK $-234.90
  1444. 2000/12/08 ! Achat
  1445. Actif:SG PEE STK 215.796 "Arcancia Équilibre 455"
  1446. Actif:SG PEE STK $-10742.54
  1447. @end smallexample
  1448. @node Buying and Selling Stock, Fixing Lot Prices, Naming Commodities, Currency and Commodities
  1449. @subsection Buying and Selling Stock
  1450. @cindex buying stock
  1451. Buying stock is a typical example that many will use that involves
  1452. multiple commodities in the same transaction. The type of the share
  1453. (AAPL for Apple Inc.) and the share purchase price in the currency
  1454. unit you made the purchase in ($ for AAPL). Yes, the typical
  1455. convention is as follows:
  1456. @smallexample @c input:validate
  1457. 2004/05/01 Stock purchase
  1458. Assets:Broker 50 AAPL @@ $30.00
  1459. Expenses:Broker:Commissions $19.95
  1460. Assets:Broker $-1,519.95
  1461. @end smallexample
  1462. This assumes you have a brokerage account that is capable of managing
  1463. both liquid and commodity assets. Now, on the day of the sale:
  1464. @smallexample @c input:validate
  1465. 2005/08/01 Stock sale
  1466. Assets:Broker -50 AAPL @{$30.00@} @@ $50.00
  1467. Expenses:Broker:Commissions $19.95
  1468. Income:Capital Gains $-1,000.00
  1469. Assets:Broker $2,480.05
  1470. @end smallexample
  1471. @noindent
  1472. You can, of course, elide the amount of the last posting. It is there
  1473. for clarity's sake.
  1474. The @samp{@{$30.00@}} is a lot price. You can also use a lot date,
  1475. @samp{[2004/05/01]}, or both, in case you have several lots of the
  1476. same price/date and your taxation model is based on
  1477. longest-held-first.
  1478. @node Fixing Lot Prices, Complete control over commodity pricing, Buying and Selling Stock, Currency and Commodities
  1479. @subsection Fixing Lot Prices
  1480. @cindex fixing lot prices
  1481. @cindex consumable commodity pricing
  1482. Commodities that you keep in order to sell at a later time have
  1483. a variable value that fluctuates with the market prices. Commodities
  1484. that you consume should not fluctuate in value, but stay at the lot
  1485. price they were purchased at. As an extension of ``lot pricing'', you
  1486. can fix the per-unit price of a commodity.
  1487. For example, say you buy 10 gallons of gas at $1.20. In future
  1488. ``value'' reports, you don't want these gallons reported in terms of
  1489. today's price, but rather the price when you bought it. At the same
  1490. time, you also want other kinds of commodities---like stocks---
  1491. reported in terms of today's price.
  1492. This is supported as follows:
  1493. @smallexample @c input:validate
  1494. 2009/01/01 Shell
  1495. Expenses:Gasoline 11 GAL @{=$2.299@}
  1496. Assets:Checking
  1497. @end smallexample
  1498. This transaction actually introduces a new commodity, @samp{GAL
  1499. @{=$2.29@}}, whose market value disregards any future changes in the
  1500. price of gasoline.
  1501. If you do not want price fixing, you can specify this same transaction
  1502. in one of two ways, both equivalent (note the lack of the equal sign
  1503. compared to the transaction above):
  1504. @smallexample @c input:validate
  1505. 2009/01/01 Shell
  1506. Expenses:Gasoline 11 GAL @{$2.299@}
  1507. Assets:Checking
  1508. 2009/01/01 Shell
  1509. Expenses:Gasoline 11 GAL @@ $2.299
  1510. Assets:Checking
  1511. @end smallexample
  1512. There is no difference in meaning between these two forms. Why do
  1513. both exist, you ask? To support things like this:
  1514. @smallexample @c input:validate
  1515. 2009/01/01 Shell
  1516. Expenses:Gasoline 11 GAL @{=$2.299@} @@ $2.30
  1517. Assets:Checking
  1518. @end smallexample
  1519. This transaction says that you bought 11 gallons priced at $2.299 per
  1520. gallon at a @emph{cost to you} of $2.30 per gallon. Ledger
  1521. auto-generates a balance posting in this case to Equity:Capital Losses
  1522. to reflect the 1.1 cent difference, which is then balanced by
  1523. Assets:Checking because its amount is null.
  1524. @node Complete control over commodity pricing, , Fixing Lot Prices, Currency and Commodities
  1525. @subsection Complete control over commodity pricing
  1526. @findex --market
  1527. @findex --exchange @var{COMMODITY}
  1528. Ledger allows you to have very detailed control over how your
  1529. commodities are valued. You can fine tune the results given using the
  1530. @option{--market} or @option{--exchange @var{COMMODITY}} options. There
  1531. are now several points of interception; you can specify the valuation
  1532. method:
  1533. @enumerate
  1534. @item on a commodity itself,
  1535. @item on a posting, via metadata (effect is largely the same as #1),
  1536. @item on an xact, which then applies to all postings in that xact,
  1537. @item on any posting via an automated transaction,
  1538. @item on a per-account basis,
  1539. @item on a per-commodity basis,
  1540. @item by changing the journal default of @code{market}.
  1541. @end enumerate
  1542. Fixated pricing (such as @samp{@{=$20@}}) still plays a role in this
  1543. scheme. As far as valuation goes, it's shorthand for writing
  1544. @samp{((s,d,t -> market($20,d,t)))}.
  1545. A valuation function receives three arguments:
  1546. @table @code
  1547. @item source
  1548. A string identifying the commodity whose price is being asked for
  1549. (example: @samp{EUR}).
  1550. @item date
  1551. The reference date the price should be relative.
  1552. @item target
  1553. A string identifying the ``target'' commodity, or the commodity the
  1554. returned price should be in. This argument is null if @option{--market}
  1555. was used instead of @option{--exchange @var{COMMODITY}}.
  1556. @end table
  1557. The valuation function should return an amount. If you've written
  1558. your function in Python, you can return something like
  1559. @samp{Amount("$100")}. If the function returns an explicit value,
  1560. that value is always used, regardless of the commodity, the date, or
  1561. the desired target commodity. For example,
  1562. @smallexample @c input:validate
  1563. define myfunc_seven(s, d, t) = 7 EUR
  1564. @end smallexample
  1565. In order to specify a fixed price, but still valuate that price into
  1566. the target commodity, use something like this:
  1567. @smallexample @c input:validate
  1568. define myfunc_five(s, d, t) = market(5 EUR, d, t)
  1569. @end smallexample
  1570. The @code{value} directive sets the valuation used for all commodities
  1571. used in the rest of the data stream. This is the fallback, if nothing
  1572. more specific is found.
  1573. @smallexample @c input:validate
  1574. value myfunc_seven
  1575. @end smallexample
  1576. You can set a specific valuation function on a per-commodity basis.
  1577. Instead of defining a function, you can also pass a lambda.
  1578. @smallexample @c input:validate
  1579. commodity $
  1580. value s, d, t -> 6 EUR
  1581. @end smallexample
  1582. Each account can also provide a default valuation function for any
  1583. commodities transferred to that account.
  1584. @smallexample @c input:validate
  1585. account Expenses:Food5
  1586. value myfunc_five
  1587. @end smallexample
  1588. The metadata field @samp{Value}, if found, overrides the valuation
  1589. function on a transaction-wide or per-posting basis.
  1590. @smallexample @c input:validate
  1591. = @@XACT and Food
  1592. ; Value:: 8 EUR
  1593. (Equity) $1
  1594. = @@POST and Dining
  1595. (Expenses:Food9) $1
  1596. ; Value:: 9 EUR
  1597. @end smallexample
  1598. Lastly, you can specify the valuation function/value for any specific
  1599. amount using the @samp{(( ))} commodity annotation.
  1600. @smallexample @c input:814A366
  1601. 2012-03-02 KFC
  1602. Expenses:Food2 $1 ((2 EUR))
  1603. Assets:Cash2
  1604. 2012-03-03 KFC
  1605. Expenses:Food3 $1
  1606. ; Value:: 3 EUR
  1607. Assets:Cash3
  1608. 2012-03-04 KFC
  1609. ; Value:: 4 EUR
  1610. Expenses:Food4 $1
  1611. Assets:Cash4
  1612. 2012-03-05 KFC
  1613. Expenses:Food5 $1
  1614. Assets:Cash5
  1615. 2012-03-06 KFC
  1616. Expenses:Food6 $1
  1617. Assets:Cash6
  1618. 2012-03-07 KFC
  1619. Expenses:Food7 1 CAD
  1620. Assets:Cas7
  1621. 2012-03-08 XACT
  1622. Expenses:Food8 $1
  1623. Assets:Cash8
  1624. 2012-03-09 POST
  1625. Expenses:Dining9 $1
  1626. Assets:Cash9
  1627. @end smallexample
  1628. @smallexample @c command:814A366
  1629. $ ledger reg -V food
  1630. @end smallexample
  1631. @smallexample @c output:814A366
  1632. 12-Mar-02 KFC Expenses:Food2 2 EUR 2 EUR
  1633. 12-Mar-03 KFC Expenses:Food3 3 EUR 5 EUR
  1634. 12-Mar-04 KFC Expenses:Food4 4 EUR 9 EUR
  1635. 12-Mar-05 KFC Expenses:Food5 $1 $1
  1636. 9 EUR
  1637. 12-Mar-06 KFC Expenses:Food6 $1 $2
  1638. 9 EUR
  1639. 12-Mar-07 KFC Expenses:Food7 1 CAD $2
  1640. 1 CAD
  1641. 9 EUR
  1642. 12-Mar-08 XACT Expenses:Food8 $1 $3
  1643. 1 CAD
  1644. 9 EUR
  1645. @end smallexample
  1646. @node Keeping it Consistent, Journal Format, Currency and Commodities, Keeping a Journal
  1647. @section Keeping it Consistent
  1648. @findex --strict
  1649. @findex accounts
  1650. Sometimes Ledger's flexibility can lead to difficulties. Using a
  1651. freeform text editor to enter transactions makes it easy to keep the
  1652. data, but also easy to enter accounts or payees inconsistently or with
  1653. spelling errors.
  1654. In order to combat inconsistency you can define allowable accounts and
  1655. payees. For simplicity, create a separate text file and define accounts
  1656. and payees like
  1657. @smallexample @c input:validate
  1658. account Expenses
  1659. account Expenses:Utilities
  1660. @end smallexample
  1661. Using the @option{--strict} option will cause Ledger to complain if any
  1662. accounts are not previously defined:
  1663. @smallexample
  1664. $ ledger bal --strict
  1665. Warning: "FinanceData/Master.dat", line 6: Unknown account 'Liabilities:Tithe Owed'
  1666. Warning: "FinanceData/Master.dat", line 8: Unknown account 'Liabilities:Tithe Owed'
  1667. Warning: "FinanceData/Master.dat", line 15: Unknown account 'Allocation:Equities:Domestic'
  1668. @end smallexample
  1669. If you have a large Ledger register already created use the
  1670. @command{accounts} command to get started:
  1671. @smallexample @c command:validate
  1672. $ ledger accounts >> Accounts.dat
  1673. @end smallexample
  1674. @noindent
  1675. You will have to edit this file to add the @code{account} directive in
  1676. front of every line.
  1677. @node Journal Format, Converting from other formats, Keeping it Consistent, Keeping a Journal
  1678. @section Journal Format
  1679. The ledger file format is quite simple, but also very flexible. It
  1680. supports many options, though typically the user can ignore most of
  1681. them. They are summarized below.
  1682. @menu
  1683. * Transactions and Comments::
  1684. * Command Directives::
  1685. @end menu
  1686. @node Transactions and Comments, Command Directives, Journal Format, Journal Format
  1687. @subsection Transactions and Comments
  1688. The initial character of each line determines what the line means, and
  1689. how it should be interpreted. Allowable initial characters are:
  1690. @table @code
  1691. @item NUMBER
  1692. A line beginning with a number denotes a transaction. It may be
  1693. followed by any number of lines, each beginning with white-space, to
  1694. denote the transaction's account postings. The format of the first
  1695. line is:
  1696. @smallexample
  1697. DATE[=EDATE] [*|!] [(CODE)] DESC
  1698. @end smallexample
  1699. If @samp{*} appears after the date (with optional effective date), it
  1700. indicates the transaction is ``cleared'', which can mean whatever the
  1701. user wants it to mean. If @samp{!} appears after the date, it
  1702. indicates the transaction is ``pending''; i.e., tentatively cleared
  1703. from the user's point of view, but not yet actually cleared. If
  1704. a @code{CODE} appears in parentheses, it may be used to indicate
  1705. a check number, or the type of the posting. Following these is the
  1706. payee, or a description of the posting.
  1707. The format of each following posting is:
  1708. @smallexample
  1709. ACCOUNT AMOUNT [; NOTE]
  1710. @end smallexample
  1711. The @code{ACCOUNT} may be surrounded by parentheses if it is a virtual
  1712. posting, or square brackets if it is a virtual posting that
  1713. must balance. The @code{AMOUNT} can be followed by a per-unit
  1714. posting cost, by specifying @code{@@ AMOUNT}, or a complete
  1715. posting cost with @code{@@@@ AMOUNT}. Lastly, the @code{NOTE} may
  1716. specify an actual and/or effective date for the posting by using
  1717. the syntax @code{[ACTUAL_DATE]} or @code{[=EFFECTIVE_DATE]} or
  1718. @code{[ACTUAL_DATE=EFFECTIVE_DATE]} (@pxref{Virtual postings}).
  1719. @item P
  1720. @findex --download
  1721. @findex P
  1722. @cindex historical prices
  1723. Specifies a historical price for a commodity. These are usually found
  1724. in a pricing history file (see the @option{--download (-Q)} option).
  1725. The syntax is:
  1726. @smallexample
  1727. P DATE SYMBOL PRICE
  1728. @end smallexample
  1729. @item =
  1730. @findex =
  1731. @cindex automated transaction
  1732. @cindex transaction, automated
  1733. An automated transaction. A value expression must appear after the
  1734. equal sign.
  1735. After this initial line there should be a set of one or more postings,
  1736. just as if it were a normal transaction. If the amounts of the postings
  1737. have no commodity, they will be applied as multipliers to whichever real
  1738. posting is matched by the value expression (@pxref{Automated
  1739. Transactions}).
  1740. @item ~
  1741. @findex ~
  1742. @cindex periodic transaction
  1743. @cindex transaction, periodic
  1744. A periodic transaction. A period expression must appear after the tilde.
  1745. After this initial line there should be a set of one or more
  1746. postings, just as if it were a normal transaction.
  1747. @item ; # % | *
  1748. @findex comment
  1749. @cindex comments
  1750. A line beginning with a semicolon, pound, percent, bar or asterisk
  1751. indicates a comment, and is ignored. Comments will not be returned in
  1752. a ``print'' response.
  1753. @item indented ;
  1754. @cindex tags
  1755. If the semicolon is indented and occurs inside a transaction, it is
  1756. parsed as a persistent note for its preceding category. These notes or
  1757. tags can be used to augment the reporting and filtering capabilities of
  1758. Ledger.
  1759. @end table
  1760. @node Command Directives, , Transactions and Comments, Journal Format
  1761. @subsection Command Directives
  1762. @findex --strict
  1763. @findex --pedantic
  1764. @table @code
  1765. @item beginning of line
  1766. Command directives must occur at the beginning of a line. Use of
  1767. @samp{!} and @samp{@@} is deprecated.
  1768. @item account
  1769. @findex account
  1770. @cindex pre-declare account
  1771. Pre-declare valid account names. This only has an effect if
  1772. @option{--strict} or @option{--pedantic} is used (see below). The
  1773. @code{account} directive supports several optional sub-directives, if
  1774. they immediately follow the account directive and if they begin with
  1775. whitespace:
  1776. @smallexample @c input:validate
  1777. account Expenses:Food
  1778. note This account is all about the chicken!
  1779. alias food
  1780. payee ^(KFC|Popeyes)$
  1781. check commodity == "$"
  1782. assert commodity == "$"
  1783. eval print("Hello!")
  1784. default
  1785. @end smallexample
  1786. The @code{note} sub-directive associates a textual note with the
  1787. account. This can be accessed later using the @code{note} value
  1788. expression function in any account context.
  1789. The @code{alias} sub-directive, which can occur multiple times, allows
  1790. the alias to be used in place of the full account name anywhere that
  1791. account names are allowed.
  1792. The @code{payee} sub-directive, which can occur multiple times,
  1793. provides regexes that identify the account if that payee is
  1794. encountered and an account within its transaction ends in the name
  1795. "Unknown". Example:
  1796. @smallexample @c input:validate
  1797. 2012-02-27 KFC
  1798. Expenses:Unknown $10.00 ; Read now as "Expenses:Food"
  1799. Assets:Cash
  1800. @end smallexample
  1801. The @code{check} and @code{assert} directives warn or raise an error
  1802. (respectively) if the given value expression evaluates to false within
  1803. the context of any posting.
  1804. The @code{eval} directive evaluates the value expression in the
  1805. context of the account at the time of definition. At the moment this
  1806. has little value.
  1807. The @code{default} directive specifies that this account should be
  1808. used as the ``balancing account'' for any future transactions that
  1809. contain only a single posting.
  1810. @item apply account
  1811. @findex apply account
  1812. @c instance_t::master_account_directive
  1813. Sets the root for all accounts following this directive. Ledger
  1814. supports a hierarchical tree of accounts. It may be convenient to
  1815. keep two ``root accounts''. For example you may be tracking your
  1816. personal finances and your business finances. In order to keep them
  1817. separate you could preface all personal accounts with @samp{personal:}
  1818. and all business accounts with @samp{business:}. You can easily split
  1819. out large groups of transactions without manually editing them using
  1820. the account directive. For example:
  1821. @smallexample @c input:validate
  1822. apply account Personal
  1823. 2011/11/15 Supermarket
  1824. Expenses:Groceries $ 50.00
  1825. Assets:Checking
  1826. @end smallexample
  1827. Would result in all postings going into
  1828. @samp{Personal:Expenses:Groceries} and @samp{Personal:Assets:Checking}
  1829. until an @samp{end apply account} directive was found.
  1830. @item alias
  1831. @findex alias
  1832. @cindex account, alias
  1833. @c instance_t::alias_directive
  1834. Define an alias for an account name. If you have a deeply nested tree
  1835. of accounts, it may be convenient to define an alias, for example:
  1836. @smallexample @c input:94A99E8
  1837. alias Dining=Expenses:Entertainment:Dining
  1838. alias Checking=Assets:Credit Union:Joint Checking Account
  1839. 2011/11/28 YummyPalace
  1840. Dining $10.00
  1841. Checking
  1842. @end smallexample
  1843. The aliases are only in effect for transactions read in after the alias
  1844. is defined and are affected by @code{account} directives that precede
  1845. them.
  1846. @smallexample @c command:94A99E8
  1847. $ ledger bal --no-total ^Exp
  1848. @end smallexample
  1849. @smallexample @c output:94A99E8
  1850. $10.00 Expenses:Entertainment:Dining
  1851. @end smallexample
  1852. With the option @option{--recursive-aliases}, aliases can refer to other
  1853. aliases, the following example produces exactly the same transactions
  1854. and account names as the preceding one:
  1855. @smallexample @c input:83E1FB3
  1856. alias Entertainment=Expenses:Entertainment
  1857. alias Dining=Entertainment:Dining
  1858. alias Checking=Assets:Credit Union:Joint Checking Account
  1859. 2011/11/30 ChopChop
  1860. Dining $10.00
  1861. Checking
  1862. @end smallexample
  1863. @smallexample @c command:83E1FB3
  1864. $ ledger balance --no-total --recursive-aliases ^Exp
  1865. @end smallexample
  1866. @smallexample @c output:83E1FB3
  1867. $10.00 Expenses:Entertainment:Dining
  1868. @end smallexample
  1869. The option @option{--no-aliases} completely disables alias expansion.
  1870. All accounts are read verbatim as they are in the ledger file.
  1871. @item assert
  1872. @findex assert
  1873. @cindex assertions
  1874. @c instance_t::assert_directive
  1875. An assertion can throw an error if a condition is not met during
  1876. Ledger's run.
  1877. @smallexample
  1878. assert <VALUE EXPRESSION BOOLEAN RESULT>
  1879. @end smallexample
  1880. @item bucket
  1881. @anchor{bucket}
  1882. @findex bucket
  1883. @cindex bucket
  1884. @c instance_t::default_account_directive
  1885. Defines the default account to use for balancing transactions.
  1886. Normally, each transaction has at least two postings, which must
  1887. balance to zero. Ledger allows you to leave one posting with no
  1888. amount and automatically balance the transaction in the
  1889. posting. The @code{bucket} allows you to fill in all postings and
  1890. automatically generate an additional posting to the bucket account
  1891. balancing the transaction. If any transaction is unbalanced, it
  1892. will automatically be balanced against the @code{bucket} account.
  1893. The following example sets @samp{Assets:Checking} as the bucket:
  1894. @smallexample @c input:validate
  1895. bucket Assets:Checking
  1896. 2011/01/25 Tom's Used Cars
  1897. Expenses:Auto $ 5,500.00
  1898. 2011/01/27 Book Store
  1899. Expenses:Books $20.00
  1900. 2011/12/01 Sale
  1901. Assets:Checking:Business $ 30.00
  1902. @end smallexample
  1903. @item capture
  1904. @c instance_t::account_mapping_directive
  1905. @findex capture
  1906. @findex print
  1907. @findex register
  1908. Directs Ledger to replace any account matching a regex with the given
  1909. account. For example:
  1910. @smallexample @c input:validate
  1911. capture Expenses:Deductible:Medical Medical
  1912. @end smallexample
  1913. Would cause any posting with @samp{Medical} in its name to be replaced
  1914. with @samp{Expenses:Deductible:Medical}.
  1915. Ledger will display the mapped payees in @command{print} and
  1916. @command{register} reports.
  1917. @item check
  1918. @findex check
  1919. @cindex assertions
  1920. @c instance_t::check_directive in textual.cc
  1921. A check issues a warning if a condition is not met during Ledger's
  1922. run.
  1923. @smallexample
  1924. check <VALUE EXPRESSION BOOLEAN RESULT>
  1925. @end smallexample
  1926. @item comment
  1927. @findex comment
  1928. @cindex comments
  1929. @c instance_t::comment_directive in textual.cc
  1930. Start a block comment, closed by @code{end comment}.
  1931. @item commodity
  1932. @findex commodity
  1933. @cindex pre-declare commodity
  1934. Pre-declare commodity names. This only has an effect if
  1935. @option{--strict} or @option{--pedantic} is used (see below).
  1936. @smallexample @c input:validate
  1937. commodity $
  1938. commodity CAD
  1939. @end smallexample
  1940. The @code{commodity} directive supports several optional
  1941. sub-directives, if they immediately follow the commodity directive
  1942. and---if they are on successive lines---begin with whitespace:
  1943. @smallexample @c input:validate
  1944. commodity $
  1945. note American Dollars
  1946. format $1,000.00
  1947. nomarket
  1948. default
  1949. @end smallexample
  1950. The @code{note} sub-directive associates a textual note with the
  1951. commodity. At present this has no value other than documentation.
  1952. The @code{format} sub-directive gives you a way to tell Ledger how to
  1953. format this commodity. In the future, using this directive will disable
  1954. Ledger's observation of other ways that commodity is used, and will
  1955. provide the ``canonical'' representation.
  1956. The @code{nomarket} sub-directive states that the commodity's price
  1957. should never be auto-downloaded.
  1958. The @code{default} sub-directive marks this as the ``default'' commodity.
  1959. @item define
  1960. @findex define
  1961. @c instance_t::define_directive in textual.cc
  1962. Allows you to define value expressions for future use. For example:
  1963. @smallexample @c input:validate
  1964. define var_name=$100
  1965. 2011/12/01 Test
  1966. Expenses (var_name*4)
  1967. Assets
  1968. @end smallexample
  1969. The posting will have a cost of $400.
  1970. @item end
  1971. @findex end
  1972. @c instance_t::end_directive in textual.cc
  1973. Closes block commands like @code{tag} or @code{comment}.
  1974. @item expr
  1975. @findex expr
  1976. @c instance_t::expr_directive in textual.cc
  1977. @item fixed
  1978. @findex fixed
  1979. @cindex fixated prices
  1980. @c instance_t::fixed_directive in textual.cc
  1981. A fixed block is used to set fixated prices (@pxref{Fixated prices and
  1982. costs}) for a series of transactions. It's purely a typing saver, for
  1983. use when entering many transactions with fixated prices.
  1984. Thus, the following:
  1985. @smallexample @c input:validate
  1986. fixed CAD $0.90
  1987. 2012-04-10 Lunch in Canada
  1988. Assets:Wallet -15.50 CAD
  1989. Expenses:Food 15.50 CAD
  1990. 2012-04-11 Second day Dinner in Canada
  1991. Assets:Wallet -25.75 CAD
  1992. Expenses:Food 25.75 CAD
  1993. endfixed CAD
  1994. @end smallexample
  1995. is equivalent to this:
  1996. @smallexample @c input:validate
  1997. 2012-04-10 Lunch in Canada
  1998. Assets:Wallet -15.50 CAD @{=$0.90@}
  1999. Expenses:Food 15.50 CAD @{=$0.90@}
  2000. 2012-04-11 Second day Dinner in Canada
  2001. Assets:Wallet -25.75 CAD @{=$0.90@}
  2002. Expenses:Food 25.75 CAD @{=$0.90@}
  2003. @end smallexample
  2004. Note that ending a @code{fixed} is done differently than other
  2005. directives, as @code{fixed} is closed with an @code{endfixed} (i.e.,
  2006. there is @emph{no space} between @code{end} and @code{fixed}).
  2007. For the moment, users may wish to study
  2008. @uref{http://bugs.ledger-cli.org/show_bug.cgi?id=789, Bug Report 789}
  2009. before using the @code{fixed} directive in production.
  2010. @item include
  2011. @findex include
  2012. @c instance_t::include_directive in textual.cc
  2013. Include the stated file as if it were part of the current file.
  2014. @item payee
  2015. @findex payee
  2016. @c instance_t::payee_alias_mapping_directive in textual.cc
  2017. @c instance_t::payee_uuid_mapping_directive in textual.cc
  2018. @findex print
  2019. @findex register
  2020. The @code{payee} directive supports two optional sub-directives, if they
  2021. immediately follow the payee directive and---if it is on a successive
  2022. line---begins with whitespace:
  2023. @smallexample @c input:validate
  2024. payee KFC
  2025. alias KENTUCKY FRIED CHICKEN
  2026. uuid 2a2e21d434356f886c84371eebac6e44f1337fda
  2027. @end smallexample
  2028. The @code{alias} sub-directive provides a regex which, if it matches
  2029. a parsed payee, the declared payee name is substituted:
  2030. @smallexample @c input:validate
  2031. 2012-02-27 KENTUCKY FRIED CHICKEN ; will be read as being 'KFC'
  2032. @end smallexample
  2033. The @code{uuid} sub-directive specifies that a transaction with exactly
  2034. the uuid given should have the declared payee name substituted:
  2035. @smallexample @c input:validate
  2036. 2014-05-13 UNHELPFUL PAYEE ; will be read as being 'KFC'
  2037. ; UUID: 2a2e21d434356f886c84371eebac6e44f1337fda
  2038. @end smallexample
  2039. Ledger will display the mapped payees in @command{print} and
  2040. @command{register} reports.
  2041. @item apply tag
  2042. @findex apply tag
  2043. @c instance_t::tag_directive in textual.cc
  2044. Allows you to designate a block of transactions and assign the same
  2045. tag to all. Tags can have values and may be nested.
  2046. @smallexample @c input:validate
  2047. apply tag hastag
  2048. apply tag nestedtag: true
  2049. 2011/01/25 Tom's Used Cars
  2050. Expenses:Auto $ 5,500.00
  2051. ; :nobudget:
  2052. Assets:Checking
  2053. 2011/01/27 Book Store
  2054. Expenses:Books $20.00
  2055. Liabilities:MasterCard
  2056. end apply tag
  2057. 2011/12/01 Sale
  2058. Assets:Checking:Business $ 30.00
  2059. Income:Sales
  2060. end apply tag
  2061. @end smallexample
  2062. @noindent
  2063. is the equivalent of:
  2064. @smallexample @c input:validate
  2065. 2011/01/25 Tom's Used Cars
  2066. ; :hastag:
  2067. ; nestedtag: true
  2068. Expenses:Auto $ 5,500.00
  2069. ; :nobudget:
  2070. Assets:Checking
  2071. 2011/01/27 Book Store
  2072. ; :hastag:
  2073. ; nestedtag: true
  2074. Expenses:Books $20.00
  2075. Liabilities:MasterCard
  2076. 2011/12/01 Sale
  2077. ; :hastag:
  2078. Assets:Checking:Business $ 30.00
  2079. Income:Sales
  2080. @end smallexample
  2081. @c TODO: the following paragraph seems to be false, the automated tests
  2082. @c fail, if anything appears after end apply tag.
  2083. @c Note that anything following @code{end apply tag} is ignored. Placing
  2084. @c the name of the tag that is being closed is a simple way to keep
  2085. @c track.
  2086. @item tag
  2087. @findex tag
  2088. @cindex pre-declare tag
  2089. Pre-declares tag names. This only has an effect if @option{--strict} or
  2090. @option{--pedantic} is used (see below).
  2091. @smallexample @c input:validate
  2092. tag Receipt
  2093. tag CSV
  2094. @end smallexample
  2095. The @code{tag} directive supports two optional sub-directives, if they
  2096. immediately follow the tag directive and---if on a successive
  2097. line---begin with whitespace:
  2098. @smallexample @c input:validate
  2099. tag Receipt
  2100. check value =~ /pattern/
  2101. assert value != "foobar"
  2102. @end smallexample
  2103. The @code{check} and @code{assert} sub-directives warn or error
  2104. (respectively) if the given value expression evaluates to false within
  2105. the context of any use of the related tag. In such a context,
  2106. ``value'' is bound to the value of the tag (which may be something else
  2107. but a string if typed metadata is used!). Such checks or assertions are
  2108. not called if no value is given.
  2109. @item test
  2110. @findex test
  2111. @cindex comments
  2112. @c instance_t::comment_directive in textual.cc
  2113. This is a synonym for @code{comment} and must be closed by an
  2114. @code{end} tag.
  2115. @item year
  2116. @findex year
  2117. @anchor{year}
  2118. @c instance_t::year_directive in textual.cc
  2119. Denotes the year used for all subsequent transactions that give a date
  2120. without a year. The year should appear immediately after the
  2121. directive, for example: @code{year 2004}. This is useful at the
  2122. beginning of a file, to specify the year for that file. If all
  2123. transactions specify a year, however, this command has no effect.
  2124. @end table
  2125. The following single letter commands may be at the beginning of a line
  2126. alone, for backwards compatibility with older Ledger versions.
  2127. @table @code
  2128. @item A
  2129. @findex A
  2130. @findex bucket
  2131. @xref{bucket}.
  2132. @item Y
  2133. @findex Y
  2134. @findex year
  2135. @xref{year}.
  2136. @item N SYMBOL
  2137. @findex N
  2138. Indicates that pricing information is to be ignored for a given
  2139. symbol, nor will quotes ever be downloaded for that symbol. Useful
  2140. with a home currency, such as the dollar @samp{$}. It is recommended
  2141. that these pricing options be set in the price database file, which
  2142. defaults to @file{~/.pricedb}. The syntax for this command is:
  2143. @smallexample @c input:validate
  2144. N SYMBOL
  2145. @end smallexample
  2146. @item D AMOUNT
  2147. @findex xact
  2148. @findex D
  2149. Specifies the default commodity to use, by specifying an amount in the
  2150. expected format. The @command{xact} command will use this commodity as
  2151. the default when none other can be determined. This command may be used
  2152. multiple times, to set the default flags for different commodities;
  2153. whichever is seen last is used as the default commodity. For example,
  2154. to set US dollars as the default commodity, while also setting the
  2155. thousands flag and decimal flag for that commodity, use:
  2156. @smallexample @c input:validate
  2157. D $1,000.00
  2158. @end smallexample
  2159. @item C AMOUNT1 = AMOUNT2
  2160. @findex C
  2161. Specifies a commodity conversion, where the first amount is given to
  2162. be equivalent to the second amount. The first amount should use the
  2163. decimal precision desired during reporting:
  2164. @smallexample @c input:validate
  2165. C 1.00 Kb = 1024 bytes
  2166. @end smallexample
  2167. @item I, i, O, o, b, h
  2168. @findex I
  2169. @findex i
  2170. @findex O
  2171. @findex o
  2172. @findex b
  2173. @findex h
  2174. These four relate to timeclock support, which permits Ledger to read
  2175. timelog files. See timeclock's documentation for more info on the
  2176. syntax of its timelog files.
  2177. @end table
  2178. @node Converting from other formats, Archiving Previous Years, Journal Format, Keeping a Journal
  2179. @section Converting from other formats
  2180. @cindex csv importing
  2181. There are numerous tools to help convert various formats to a Ledger
  2182. file. Most banks will generate a comma separated values file that can
  2183. easily be parsed into Ledger format using one of those tools. Some of
  2184. the most popular tools are:
  2185. @itemize
  2186. @item @code{ledger convert download.csv}
  2187. @item @code{hledger -f checking.csv print}
  2188. @item @uref{https://github.com/quentinsf/icsv2ledger, @code{icsv2ledger}}
  2189. @item @uref{https://github.com/tazzben/csvToLedger, @code{csvToLedger}}
  2190. @item @uref{https://launchpad.net/csv2ledger, @code{CSV2Ledger}}
  2191. @end itemize
  2192. @noindent
  2193. Directly pulling information from banks is outside the scope of
  2194. Ledger's function.
  2195. @node Archiving Previous Years, , Converting from other formats, Keeping a Journal
  2196. @section Archiving Previous Years
  2197. @findex equity
  2198. @findex print
  2199. After a while, your journal can get to be pretty large. While this
  2200. will not slow down Ledger---it's designed to process journals very
  2201. quickly---things can start to feel ``messy''; and it's a universal
  2202. complaint that when finances feel messy, people avoid them.
  2203. Thus, archiving the data from previous years into their own files can
  2204. offer a sense of completion, and freedom from the past. But how to
  2205. best accomplish this with the ledger program? There are two commands
  2206. that make it very simple: @command{print}, and @command{equity}.
  2207. Let's take an example file, with data ranging from year 2000 until
  2208. 2004. We want to archive years 2000 and 2001 to their own file,
  2209. leaving just 2003 and 2004 in the current file. So, use
  2210. @command{print} to output all the earlier transactions to a file
  2211. called @file{ledger-old.dat}:
  2212. @smallexample
  2213. $ ledger -f ledger.dat -b 2000 -e 2001 print > ledger-old.dat
  2214. @end smallexample
  2215. To delete older data from the current ledger file, use @command{print}
  2216. again, this time specifying year 2002 as the starting date:
  2217. @smallexample
  2218. $ ledger -f ledger.dat -b 2002 print > x
  2219. $ mv x ledger.dat
  2220. @end smallexample
  2221. However, now the current file contains @emph{only} postings from 2002
  2222. onward, which will not yield accurate present-day balances, because the
  2223. net income from previous years is no longer being tallied. To
  2224. compensate for this, we must append an equity report for the old ledger
  2225. at the beginning of the new one:
  2226. @smallexample
  2227. $ ledger -f ledger-old.dat equity > equity.dat
  2228. $ cat equity.dat ledger.dat > x
  2229. $ mv x ledger.dat
  2230. $ rm equity.dat
  2231. @end smallexample
  2232. Now the balances reported from @file{ledger.dat} are identical to what
  2233. they were before the data was split.
  2234. How often should you split your ledger? You never need to, if you
  2235. don't want to. Even eighty years of data will not slow down ledger
  2236. much, and that's just using present day hardware! Or, you can keep
  2237. the previous and current year in one file, and each year before that
  2238. in its own file. It's really up to you, and how you want to organize
  2239. your finances. For those who also keep an accurate paper trail, it
  2240. might be useful to archive the older years to their own files, then
  2241. burn those files to a CD to keep with the paper records---along with
  2242. any electronic statements received during the year. In the arena of
  2243. organization, just keep in mind this maxim: Do whatever keeps you
  2244. doing it.
  2245. @node Transactions, Building Reports, Keeping a Journal, Top
  2246. @chapter Transactions
  2247. @menu
  2248. * Basic format::
  2249. * Eliding amounts::
  2250. * Auxiliary dates::
  2251. * Codes::
  2252. * Transaction state::
  2253. * Transaction notes::
  2254. * Metadata::
  2255. * Virtual postings::
  2256. * Expression amounts::
  2257. * Balance verification::
  2258. * Posting cost::
  2259. * Explicit posting costs::
  2260. * Posting cost expressions::
  2261. * Total posting costs::
  2262. * Virtual posting costs::
  2263. * Commodity prices::
  2264. * Prices versus costs::
  2265. * Fixated prices and costs::
  2266. * Lot dates::
  2267. * Lot notes::
  2268. * Lot value expressions::
  2269. * Automated Transactions::
  2270. @end menu
  2271. @node Basic format, Eliding amounts, Transactions, Transactions
  2272. @section Basic format
  2273. The most basic form of transaction is:
  2274. @smallexample @c input:validate
  2275. 2012-03-10 KFC
  2276. Expenses:Food $20.00
  2277. Assets:Cash $-20.00
  2278. @end smallexample
  2279. This transaction has a date, a payee or description, a target account
  2280. (the first posting), and a source account (the second posting). Each
  2281. posting specifies what action is taken related to that account.
  2282. A transaction can have any number of postings:
  2283. @smallexample @c input:validate
  2284. 2012-03-10 KFC
  2285. Expenses:Food $20.00
  2286. Assets:Cash $-10.00
  2287. Liabilities:Credit $-10.00
  2288. @end smallexample
  2289. @node Eliding amounts, Auxiliary dates, Basic format, Transactions
  2290. @section Eliding amounts
  2291. The first thing you can do to make things easier is elide amounts.
  2292. That is, if exactly one posting has no amount specified, Ledger will
  2293. infer the inverse of the other postings' amounts:
  2294. @smallexample @c input:validate
  2295. 2012-03-10 KFC
  2296. Expenses:Food $20.00
  2297. Assets:Cash $-10.00
  2298. Liabilities:Credit ; same as specifying $-10
  2299. @end smallexample
  2300. @noindent
  2301. If the other postings use multiple commodities, Ledger will copy the
  2302. empty posting N times and fill in the negated values of the various
  2303. commodities:
  2304. @smallexample @c input:validate
  2305. 2012-03-10 KFC
  2306. Expenses:Food $20.00
  2307. Expenses:Tips $2.00
  2308. Assets:Cash EUR -10.00
  2309. Assets:Cash GBP -10.00
  2310. Liabilities:Credit
  2311. @end smallexample
  2312. @noindent
  2313. This transaction is identical to writing:
  2314. @smallexample @c input:validate
  2315. 2012-03-10 KFC
  2316. Expenses:Food $20.00
  2317. Expenses:Tips $2.00
  2318. Assets:Cash EUR -10.00
  2319. Assets:Cash GBP -10.00
  2320. Liabilities:Credit $-22.00
  2321. Liabilities:Credit EUR 10.00
  2322. Liabilities:Credit GBP 10.00
  2323. @end smallexample
  2324. @node Auxiliary dates, Codes, Eliding amounts, Transactions
  2325. @section Auxiliary dates
  2326. @findex --aux-date
  2327. You can associate a second date with a transaction by following the
  2328. primary date with an equals sign:
  2329. @smallexample @c input:validate
  2330. 2012-03-10=2012-03-08 KFC
  2331. Expenses:Food $20.00
  2332. Assets:Cash $-20.00
  2333. @end smallexample
  2334. What this auxiliary date means is entirely up to you. The only use
  2335. Ledger has for it is that if you specify @option{--aux-date}, then all
  2336. reports and calculations (including pricing) will use the auxiliary
  2337. date as if it were the primary date.
  2338. @node Codes, Transaction state, Auxiliary dates, Transactions
  2339. @section Codes
  2340. A transaction can have a textual ``code''. This has no meaning and is
  2341. only displayed by the print command. Checking accounts often use
  2342. codes like DEP, XFER, etc., as well as check numbers. This is to give
  2343. you a place to put those codes:
  2344. @smallexample @c input:validate
  2345. 2012-03-10 (#100) KFC
  2346. Expenses:Food $20.00
  2347. Assets:Checking
  2348. @end smallexample
  2349. @node Transaction state, Transaction notes, Codes, Transactions
  2350. @section Transaction state
  2351. @findex --cleared
  2352. @findex --uncleared
  2353. @findex --pending
  2354. A transaction can have a ``state'': cleared, pending, or uncleared. The
  2355. default is uncleared. To mark a transaction cleared, put an asterisk
  2356. @samp{*} before the payee, after the date or code:
  2357. @smallexample @c input:validate
  2358. 2012-03-10 * KFC
  2359. Expenses:Food $20.00
  2360. Assets:Cash
  2361. @end smallexample
  2362. @noindent
  2363. To mark it pending, use a @samp{!}:
  2364. @smallexample @c input:validate
  2365. 2012-03-10 ! KFC
  2366. Expenses:Food $20.00
  2367. Assets:Cash
  2368. @end smallexample
  2369. What these mean is entirely up to you. The @option{--cleared} option
  2370. limits reports to only cleared items, while @option{--uncleared}
  2371. shows both uncleared and pending items, and @option{--pending} shows
  2372. only pending items.
  2373. I use cleared to mean that I've reconciled the transaction with my
  2374. bank statement, and pending to mean that I'm in the middle of
  2375. a reconciliation.
  2376. When you clear a transaction, that's really just shorthand for
  2377. clearing all of its postings. That is:
  2378. @smallexample @c input:validate
  2379. 2012-03-10 * KFC
  2380. Expenses:Food $20.00
  2381. Assets:Cash
  2382. @end smallexample
  2383. @noindent
  2384. Is the same as writing:
  2385. @smallexample @c input:validate
  2386. 2012-03-10 KFC
  2387. * Expenses:Food $20.00
  2388. * Assets:Cash
  2389. @end smallexample
  2390. @noindent
  2391. You can mark individual postings as cleared or pending, in case one
  2392. ``side'' of the transaction has cleared, but the other hasn't yet:
  2393. @smallexample @c input:validate
  2394. 2012-03-10 KFC
  2395. Liabilities:Credit $100.00
  2396. * Assets:Checking
  2397. @end smallexample
  2398. @node Transaction notes, Metadata, Transaction state, Transactions
  2399. @section Transaction notes
  2400. After the payee, and after at least one tab or two spaces (or a space
  2401. and a tab, which Ledger calls a ``hard separator''), you may
  2402. introduce a note about the transaction using the @samp{;} character:
  2403. @smallexample @c input:validate
  2404. 2012-03-10 * KFC ; yum, chicken...
  2405. Expenses:Food $20.00
  2406. Assets:Cash
  2407. @end smallexample
  2408. @noindent
  2409. Notes can also appear on the next line, so long as that line begins
  2410. with whitespace:
  2411. @smallexample @c input:validate
  2412. 2012-03-10 * KFC ; yum, chicken...
  2413. ; and more notes...
  2414. Expenses:Food $20.00
  2415. Assets:Cash
  2416. 2012-03-10 * KFC
  2417. ; just these notes...
  2418. Expenses:Food $20.00
  2419. Assets:Cash
  2420. @end smallexample
  2421. A transaction's note is shared by all its postings. This becomes
  2422. significant when querying for metadata (see below). To specify that
  2423. a note belongs only to one posting, place it after a hard separator
  2424. after the amount, or on its own line preceded by whitespace:
  2425. @smallexample @c input:validate
  2426. 2012-03-10 * KFC
  2427. Expenses:Food $20.00 ; posting #1 note
  2428. Assets:Cash
  2429. ; posting #2 note, extra indentation is optional
  2430. @end smallexample
  2431. @node Metadata, Virtual postings, Transaction notes, Transactions
  2432. @section Metadata
  2433. @c TODO add cindex
  2434. @c TODO https://groups.google.com/d/msg/ledger-cli/2csLPcHJ3ak/a17jOClzLTUJ
  2435. @c > Is there a way to produce a register report that lists all the transaction
  2436. @c > that contain a certain tag, and sort them based on the value of the tag?
  2437. @c ledger reg --sort "tag('foo')" %foo
  2438. @c ledger reg --group-by "tag('Employer)" Remboursement:Employer and tag Employer
  2439. @c > Is it possible to get subtotals for each tag value?
  2440. @c ledger --group-by "tag('foo')" bal
  2441. @c TODO https://groups.google.com/d/msg/ledger-cli/K9NBhNlVnYc/TDYDAWhOA5EJ
  2442. One of Ledger's more powerful features is the ability to associate
  2443. typed metadata with postings and transactions (by which I mean all of
  2444. a transaction's postings). This metadata can be queried, displayed,
  2445. and used in calculations.
  2446. The are two forms of metadata: plain tags, and tag/value pairs.
  2447. @menu
  2448. * Metadata tags::
  2449. * Metadata values::
  2450. * Typed metadata::
  2451. @end menu
  2452. @node Metadata tags, Metadata values, Metadata, Metadata
  2453. @subsection Metadata tags
  2454. To tag an item, put any word not containing whitespace between two
  2455. colons inside a comment:
  2456. @smallexample @c input:validate
  2457. 2012-03-10 * KFC
  2458. Expenses:Food $20.00
  2459. Assets:Cash
  2460. ; :TAG:
  2461. @end smallexample
  2462. You can gang up multiple tags by sharing colons:
  2463. @smallexample @c input:validate
  2464. 2012-03-10 * KFC
  2465. Expenses:Food $20.00
  2466. Assets:Cash
  2467. ; :TAG1:TAG2:TAG3:
  2468. @end smallexample
  2469. @menu
  2470. * Payee metadata tag::
  2471. @end menu
  2472. @node Payee metadata tag, , Metadata tags, Metadata tags
  2473. @subsubsection Payee metadata tag
  2474. @cindex Payee metadata tag
  2475. @findex register
  2476. @findex payees
  2477. @findex --by-payee
  2478. ``Payee'' is a special metadata field. If set on a posting, it will be
  2479. used as the payee name for that posting. This affects the
  2480. @command{register} report, the @command{payees} report, and the
  2481. @option{--by-payee} option.
  2482. This is useful when for example you deposit 4 checks at a time to the
  2483. bank. On the bank statement, there is just one amount @samp{$400}, but
  2484. you can specify from whom each check came from, as shown by example
  2485. below:
  2486. @smallexample @c input:9B43E57
  2487. 2010-06-17 Sample
  2488. Assets:Bank $400.00
  2489. Income:Check1 $-100.00 ; Payee: Person One
  2490. Income:Check2 $-100.00 ; Payee: Person Two
  2491. Income:Check3 $-100.00 ; Payee: Person Three
  2492. Income:Check4 $-100.00 ; Payee: Person Four
  2493. @end smallexample
  2494. When reporting with
  2495. @smallexample @c command:9B43E57
  2496. $ ledger reg
  2497. @end smallexample
  2498. it appears as:
  2499. @smallexample @c output:9B43E57
  2500. 10-Jun-17 Sample Assets:Bank $400.00 $400.00
  2501. Person One Income:Check1 $-100.00 $300.00
  2502. Person Two Income:Check2 $-100.00 $200.00
  2503. Person Three Income:Check3 $-100.00 $100.00
  2504. Person Four Income:Check4 $-100.00 0
  2505. @end smallexample
  2506. This shows that they are all in the same transaction (which is why the
  2507. date is not repeated), but they have different payees now.
  2508. @node Metadata values, Typed metadata, Metadata tags, Metadata
  2509. @subsection Metadata values
  2510. To associate a value with a tag, use the syntax ``Key: Value'', where
  2511. the value can be any string of characters. Whitespace is needed after
  2512. the colon, and cannot appear in the Key:
  2513. @smallexample @c input:validate
  2514. 2012-03-10 * KFC
  2515. Expenses:Food $20.00
  2516. Assets:Cash
  2517. ; MyTag: This is just a bogus value for MyTag
  2518. @end smallexample
  2519. @node Typed metadata, , Metadata values, Metadata
  2520. @subsection Typed metadata
  2521. If a metadata tag ends in ::, its value will be parsed as a value
  2522. expression and stored internally as a value rather than as a string.
  2523. For example, although I can specify a date textually like so:
  2524. @smallexample @c input:validate
  2525. 2012-03-10 * KFC
  2526. Expenses:Food $20.00
  2527. Assets:Cash
  2528. ; AuxDate: 2012/02/30
  2529. @end smallexample
  2530. @noindent
  2531. This date is just a string, and won't be parsed as a date unless its
  2532. value is used in a date-context (at which time the string is parsed
  2533. into a date automatically every time it is needed as a date). If on
  2534. the other hand I write this:
  2535. @smallexample
  2536. 2012-03-10 * KFC
  2537. Expenses:Food $20.00
  2538. Assets:Cash
  2539. ; AuxDate:: [2012/02/30]
  2540. @end smallexample
  2541. @noindent
  2542. Then it is parsed as a date only once, and during parsing of the
  2543. journal file, which would let me know right away that it is an invalid
  2544. date.
  2545. @node Virtual postings, Expression amounts, Metadata, Transactions
  2546. @section Virtual postings
  2547. @findex --real
  2548. Ordinarily, the amounts of all postings in a transaction must balance
  2549. to zero. This is non-negotiable. It's what double-entry accounting
  2550. is all about! But there are some tricks up Ledger's sleeve...
  2551. You can use virtual accounts to transfer amounts to an account on the
  2552. sly, bypassing the balancing requirement. The trick is that these
  2553. postings are not considered ``real'', and can be removed from all
  2554. reports using @option{--real}.
  2555. To specify a virtual account, surround the account name with
  2556. parentheses:
  2557. @smallexample @c input:validate
  2558. 2012-03-10 * KFC
  2559. Expenses:Food $20.00
  2560. Assets:Cash
  2561. (Budget:Food) $-20.00
  2562. @end smallexample
  2563. If you want, you can state that virtual postings @emph{should} balance
  2564. against one or more other virtual postings by using brackets (which
  2565. look ``harder'') rather than parentheses:
  2566. @smallexample @c input:validate
  2567. 2012-03-10 * KFC
  2568. Expenses:Food $20.00
  2569. Assets:Cash
  2570. [Budget:Food] $-20.00
  2571. [Equity:Budgets] $20.00
  2572. @end smallexample
  2573. @node Expression amounts, Balance verification, Virtual postings, Transactions
  2574. @section Expression amounts
  2575. An amount is usually a numerical figure with an (optional) commodity,
  2576. but it can also be any value expression. To indicate this, surround
  2577. the amount expression with parentheses:
  2578. @smallexample @c input:validate
  2579. 2012-03-10 * KFC
  2580. Expenses:Food ($10.00 + $20.00) ; Ledger adds it up for you
  2581. Assets:Cash
  2582. @end smallexample
  2583. @node Balance verification, Posting cost, Expression amounts, Transactions
  2584. @section Balance verification
  2585. @findex --permissive
  2586. @menu
  2587. * Balance assertions::
  2588. * Balance assignments::
  2589. * Resetting a balance::
  2590. * Balancing transactions::
  2591. @end menu
  2592. If at the end of a posting's amount (and after the cost too, if there
  2593. is one) there is an equals sign, then Ledger will verify that the
  2594. total value for that account as of that posting matches the amount
  2595. specified. See @option{--permissive} option to relax the balance assertions checks.
  2596. There are two forms of this features: balance assertions, and balance
  2597. assignments.
  2598. @node Balance assertions, Balance assignments, Balance verification, Balance verification
  2599. @subsection Balance assertions
  2600. A balance assertion has this general form:
  2601. @smallexample
  2602. 2012-03-10 KFC
  2603. Expenses:Food $20.00
  2604. Assets:Cash $-20.00 = $500.00
  2605. @end smallexample
  2606. This simply asserts that after subtracting $20.00 from Assets:Cash,
  2607. that the resulting total matches $500.00. If not, it is an error.
  2608. @node Balance assignments, Resetting a balance, Balance assertions, Balance verification
  2609. @subsection Balance assignments
  2610. A balance assignment has this form:
  2611. @smallexample
  2612. 2012-03-10 KFC
  2613. Expenses:Food $20.00
  2614. Assets:Cash = $500.00
  2615. @end smallexample
  2616. This sets the amount of the second posting to whatever it would need to
  2617. be for the total in @samp{Assets:Cash} to be $500.00 after the posting.
  2618. If the resulting amount is not $-20.00 in this case, it is an error.
  2619. @node Resetting a balance, Balancing transactions, Balance assignments, Balance verification
  2620. @subsection Resetting a balance
  2621. Say your book-keeping has gotten a bit out of date, and your Ledger
  2622. balance no longer matches your bank balance. You can create an
  2623. adjustment transaction using balance assignments:
  2624. @smallexample @c input:validate
  2625. 2012-03-10 Adjustment
  2626. Assets:Cash = $500.00
  2627. Equity:Adjustments
  2628. @end smallexample
  2629. Since the second posting is also null, it's value will become the
  2630. inverse of whatever amount is generated for the first posting.
  2631. This is the only time in ledger when more than one posting's amount
  2632. may be empty---and then only because it's not truly empty, it is
  2633. indirectly provided by the balance assignment's value.
  2634. @node Balancing transactions, , Resetting a balance, Balance verification
  2635. @subsection Balancing transactions
  2636. @findex --empty
  2637. As a consequence of all the above, consider the following transaction:
  2638. @smallexample
  2639. 2012-03-10 My Broker
  2640. [Assets:Brokerage] = 10 AAPL
  2641. @end smallexample
  2642. What this says is: set the amount of the posting to whatever value is
  2643. needed so that @samp{Assets:Brokerage} contains 10 AAPL. Then, because
  2644. this posting must balance, ensure that its value is zero. This can only
  2645. be true if Assets:Brokerage does indeed contain 10 AAPL at that point in
  2646. the input file.
  2647. A balanced virtual transaction is used simply to indicate to Ledger that
  2648. this is not a ``real'' transaction. It won't appear in any reports
  2649. anyway (unless you use a register report with @option{--empty}).
  2650. @node Posting cost, Explicit posting costs, Balance verification, Transactions
  2651. @section Posting cost
  2652. When you transfer a commodity from one account to another, sometimes
  2653. it gets transformed during the transaction. This happens when you
  2654. spend money on gas, for example, which transforms dollars into gallons
  2655. of gasoline, or dollars into stocks in a company.
  2656. In those cases, Ledger will remember the ``cost'' of that transaction
  2657. for you, and can use it during reporting in various ways. Here's an
  2658. example of a stock purchase:
  2659. @smallexample @c input:validate
  2660. 2012-03-10 My Broker
  2661. Assets:Brokerage 10 AAPL
  2662. Assets:Brokerage:Cash $-500.00
  2663. @end smallexample
  2664. This is different from transferring 10 AAPL shares from one account to
  2665. another, in this case you are @emph{exchanging} one commodity for
  2666. another. The resulting posting's cost is $50.00 per share.
  2667. @node Explicit posting costs, Posting cost expressions, Posting cost, Transactions
  2668. @section Explicit posting costs
  2669. You can make any posting's cost explicit using the @samp{@@} symbol
  2670. after the amount or amount expression:
  2671. @smallexample @c input:validate
  2672. 2012-03-10 My Broker
  2673. Assets:Brokerage 10 AAPL @@ $50.00
  2674. Assets:Brokerage:Cash $-500.00
  2675. @end smallexample
  2676. When you do this, since Ledger can now figure out the balancing amount
  2677. from the first posting's cost, you can elide the other amount:
  2678. @smallexample @c input:validate
  2679. 2012-03-10 My Broker
  2680. Assets:Brokerage 10 AAPL @@ $50.00
  2681. Assets:Brokerage:Cash
  2682. @end smallexample
  2683. @menu
  2684. * Primary and secondary commodities::
  2685. @end menu
  2686. @node Primary and secondary commodities, , Explicit posting costs, Explicit posting costs
  2687. @subsection Primary and secondary commodities
  2688. @findex --market
  2689. @findex --exchange @var{COMMODITY}
  2690. It is a general convention within Ledger that the ``top'' postings in
  2691. a transaction contain the target accounts, while the final posting
  2692. contains the source account. Whenever a commodity is exchanged like
  2693. this, the commodity moved to the target account is considered
  2694. ``secondary'', while the commodity used for purchasing and tracked in
  2695. the cost is ``primary''.
  2696. Said another way, whenever Ledger sees a posting cost of the form
  2697. "AMOUNT @@ AMOUNT", the commodity used in the second amount is marked
  2698. ``primary''.
  2699. The only meaning a primary commodity has is that the @option{--market
  2700. (-V)} flag will never convert a primary commodity into any other
  2701. commodity. @option{--exchange @var{COMMODITY} (-X)} still will,
  2702. however.
  2703. @node Posting cost expressions, Total posting costs, Explicit posting costs, Transactions
  2704. @section Posting cost expressions
  2705. Just as you can have amount expressions, you can have posting
  2706. expressions:
  2707. @smallexample @c input:validate
  2708. 2012-03-10 My Broker
  2709. Assets:Brokerage 10 AAPL @@ ($500.00 / 10)
  2710. Assets:Brokerage:Cash
  2711. @end smallexample
  2712. You can even have both:
  2713. @smallexample @c input:validate
  2714. 2012-03-10 My Broker
  2715. Assets:Brokerage (5 AAPL * 2) @@ ($500.00 / 10)
  2716. Assets:Brokerage:Cash
  2717. @end smallexample
  2718. @node Total posting costs, Virtual posting costs, Posting cost expressions, Transactions
  2719. @section Total posting costs
  2720. The cost figure following the @samp{@@} character specifies the
  2721. @emph{per-unit} price for the commodity being transferred. If you'd
  2722. like to specify the total cost instead, use @samp{@@@@}:
  2723. @smallexample @c input:validate
  2724. 2012-03-10 My Broker
  2725. Assets:Brokerage 10 AAPL @@@@ $500.00
  2726. Assets:Brokerage:Cash
  2727. @end smallexample
  2728. Ledger reads this as if you had written:
  2729. @smallexample @c input:validate
  2730. 2012-03-10 My Broker
  2731. Assets:Brokerage 10 AAPL @@ ($500.00 / 10)
  2732. Assets:Brokerage:Cash
  2733. @end smallexample
  2734. @node Virtual posting costs, Commodity prices, Total posting costs, Transactions
  2735. @section Virtual posting costs
  2736. Normally whenever a commodity exchange like this happens, the price of
  2737. the exchange (such as $50 per share of AAPL, above) is recorded in
  2738. Ledger's internal price history database. To prevent this from
  2739. happening in the case of an exceptional transaction, surround the
  2740. @samp{@@} or @samp{@@@@} with parentheses:
  2741. @smallexample @c input:validate
  2742. 2012-03-10 My Brother
  2743. Assets:Brokerage 1000 AAPL (@@) $1
  2744. Income:Gifts Received
  2745. @end smallexample
  2746. @node Commodity prices, Prices versus costs, Virtual posting costs, Transactions
  2747. @section Commodity prices
  2748. @findex --lot-prices
  2749. When a transaction occurs that exchanges one commodity for another,
  2750. Ledger records that commodity price not only within its internal price
  2751. database, but also attached to the commodity itself. Usually this fact
  2752. remains invisible to the user, unless you turn on @option{--lot-prices}
  2753. to show these hidden price figures.
  2754. For example, consider the stock sale given above:
  2755. @smallexample @c input:validate
  2756. 2012-03-10 My Broker
  2757. Assets:Brokerage 10 AAPL @@ $50.00
  2758. Assets:Brokerage:Cash
  2759. @end smallexample
  2760. The commodity transferred into @samp{Assets:Brokerage} is not actually 10
  2761. AAPL, but rather 10 AAPL @{$50.00@}. The figure in braces after the
  2762. amount is called the ``lot price''. It's Ledger's way of remembering
  2763. that this commodity was transferred through an exchange, and that
  2764. $50.00 was the price of that exchange.
  2765. This becomes significant if you later sell that commodity again. For
  2766. example, you might write this:
  2767. @smallexample @c input:validate
  2768. 2012-04-10 My Broker
  2769. Assets:Brokerage:Cash
  2770. Assets:Brokerage -10 AAPL @@ $75.00
  2771. @end smallexample
  2772. And that would be perfectly fine, but how do you track the capital
  2773. gains on the sale? It could be done with a virtual posting:
  2774. @smallexample @c input:validate
  2775. 2012-04-10 My Broker
  2776. Assets:Brokerage:Cash
  2777. Assets:Brokerage -10 AAPL @@ $75.00
  2778. (Income:Capital Gains) $-250.00
  2779. @end smallexample
  2780. But this gets messy since capital gains income is very real, and not
  2781. quite appropriate for a virtual posting.
  2782. Instead, if you reference that same hidden price annotation, Ledger
  2783. will figure out that the price of the shares you're selling, and the
  2784. cost you're selling them at, don't balance:
  2785. @smallexample
  2786. 2012-04-10 My Broker
  2787. Assets:Brokerage:Cash $750.00
  2788. Assets:Brokerage -10 AAPL @{$50.00@} @@ $75.00
  2789. @end smallexample
  2790. This transaction will fail because the $250.00 price difference
  2791. between the price you bought those shares at, and the cost you're
  2792. selling them for, does not match. The lot price also identifies which
  2793. shares you purchased on that prior date.
  2794. @menu
  2795. * Total commodity prices::
  2796. @end menu
  2797. @node Total commodity prices, , Commodity prices, Commodity prices
  2798. @subsection Total commodity prices
  2799. As a shorthand, you can specify the total price instead of the
  2800. per-share price in doubled braces. This goes well with total costs,
  2801. but is not required to be used with them:
  2802. @smallexample @c input:validate
  2803. 2012-04-10 My Broker
  2804. Assets:Brokerage:Cash $750.00
  2805. Assets:Brokerage -10 AAPL @{@{$500.00@}@} @@@@ $750.00
  2806. Income:Capital Gains $-250.00
  2807. @end smallexample
  2808. It should be noted that this is a convenience only for cases where you
  2809. buy and sell whole lots. The @{@{$500.00@}@} is @emph{not} an attribute
  2810. of the commodity, whereas @{$50.00@} is. In fact, when you write
  2811. @{@{$500.00@}@}, Ledger just divides that value by 10 and sees
  2812. @{$50.00@}. So if you use the print command to look at this
  2813. transaction, you'll see the single braces form in the output. The
  2814. double braces price form is a shorthand only.
  2815. Plus, it comes with dangers. This works fine:
  2816. @smallexample @c input:validate
  2817. 2012-04-10 My Broker
  2818. Assets:Brokerage 10 AAPL @@ $50.00
  2819. Assets:Brokerage:Cash $-500.00
  2820. 2012-04-10 My Broker
  2821. Assets:Brokerage:Cash $375.00
  2822. Assets:Brokerage -5 AAPL @{$50.00@} @@ $375.00
  2823. Income:Capital Gains $-125.00
  2824. 2012-04-10 My Broker
  2825. Assets:Brokerage:Cash $375.00
  2826. Assets:Brokerage -5 AAPL @{$50.00@} @@ $375.00
  2827. Income:Capital Gains $-125.00
  2828. @end smallexample
  2829. @noindent
  2830. But this does not do what you might expect:
  2831. @smallexample
  2832. 2012-04-10 My Broker
  2833. Assets:Brokerage 10 AAPL @@ $50.00
  2834. Assets:Brokerage:Cash $750.00
  2835. 2012-04-10 My Broker
  2836. Assets:Brokerage:Cash $375.00
  2837. Assets:Brokerage -5 AAPL @{@{$500.00@}@} @@ $375.00
  2838. Income:Capital Gains $-125.00
  2839. 2012-04-10 My Broker
  2840. Assets:Brokerage:Cash $375.00
  2841. Assets:Brokerage -5 AAPL @{@{$500.00@}@} @@ $375.00
  2842. Income:Capital Gains $-125.00
  2843. @end smallexample
  2844. And in cases where the amounts do not divide into whole figures and
  2845. must be rounded, the capital gains figure could be off by a cent. Use
  2846. with caution.
  2847. @node Prices versus costs, Fixated prices and costs, Commodity prices, Transactions
  2848. @section Prices versus costs
  2849. Because lot pricing provides enough information to infer the cost, the
  2850. following two transactions are equivalent:
  2851. @smallexample
  2852. 2012-04-10 My Broker
  2853. Assets:Brokerage 10 AAPL @@ $50.00
  2854. Assets:Brokerage:Cash $750.00
  2855. 2012-04-10 My Broker
  2856. Assets:Brokerage 10 AAPL @{$50.00@}
  2857. Assets:Brokerage:Cash $750.00
  2858. @end smallexample
  2859. However, note that what you see in some reports may differ, for
  2860. example in the print report. Functionally, however, there is no
  2861. difference, and neither the register nor the balance report are
  2862. sensitive to this difference.
  2863. @node Fixated prices and costs, Lot dates, Prices versus costs, Transactions
  2864. @section Fixated prices and costs
  2865. If you buy a stock last year, and ask for its value today, Ledger will
  2866. consult its price database to see what the most recent price for that
  2867. stock is. You can short-circuit this lookup by ``fixing'' the price
  2868. at the time of a transaction. This is done using @samp{@{=AMOUNT@}}:
  2869. @smallexample
  2870. 2012-04-10 My Broker
  2871. Assets:Brokerage 10 AAPL @{=$50.00@}
  2872. Assets:Brokerage:Cash $750.00
  2873. @end smallexample
  2874. These 10 AAPL will now always be reported as being worth $50, no
  2875. matter what else happens to the stock in the meantime.
  2876. Fixated prices are a special case of using lot valuation expressions
  2877. (see below) to fix the value of a commodity lot.
  2878. Since price annotations and costs are largely interchangeable and
  2879. a matter of preference, there is an equivalent syntax for specified
  2880. fixated prices by way of the cost:
  2881. @smallexample
  2882. 2012-04-10 My Broker
  2883. Assets:Brokerage 10 AAPL @@ =$50.00
  2884. Assets:Brokerage:Cash $750.00
  2885. @end smallexample
  2886. This is the same as the previous transaction, with the same caveats
  2887. found in @ref{Prices versus costs}.
  2888. @node Lot dates, Lot notes, Fixated prices and costs, Transactions
  2889. @section Lot dates
  2890. @findex --lot-dates
  2891. In addition to lot prices, you can specify lot dates and reveal them
  2892. with @option{--lot-dates}. Other than that, however, they have no
  2893. special meaning to Ledger. They are specified after the amount in
  2894. square brackets (the same way that dates are parsed in value
  2895. expressions):
  2896. @smallexample
  2897. 2012-04-10 My Broker
  2898. Assets:Brokerage:Cash $375.00
  2899. Assets:Brokerage -5 AAPL @{$50.00@} [2012-04-10] @@ $375.00
  2900. Income:Capital Gains $-125.00
  2901. @end smallexample
  2902. @node Lot notes, Lot value expressions, Lot dates, Transactions
  2903. @section Lot notes
  2904. @findex --lot-notes
  2905. @findex --lots
  2906. You can also associate arbitrary notes for your own record keeping in
  2907. parentheses, and reveal them with @option{--lot-notes}. One caveat is
  2908. that the note cannot begin with an @samp{@@} character, as that would
  2909. indicate a virtual cost:
  2910. @smallexample
  2911. 2012-04-10 My Broker
  2912. Assets:Brokerage:Cash $375.00
  2913. Assets:Brokerage -5 AAPL @{$50.00@} [2012-04-10] (Oh my!) @@ $375.00
  2914. Income:Capital Gains $-125.00
  2915. @end smallexample
  2916. You can specify any combination of lot prices, dates or notes, in any
  2917. order. They are all optional.
  2918. To show all lot information in a report, use @option{--lots}.
  2919. @node Lot value expressions, Automated Transactions, Lot notes, Transactions
  2920. @section Lot value expressions
  2921. Normally when you ask Ledger to display the values of commodities held,
  2922. it uses a value expression called ``market'' to determine the most
  2923. recent value from its price database---even downloading prices from the
  2924. Internet, if @option{--download (-Q)} was specified and a suitable
  2925. @file{getquote} script is found on your system.
  2926. However, you can override this valuation logic by providing
  2927. a commodity valuation expression in doubled parentheses. This
  2928. expression must result in one of two values: either an amount to
  2929. always be used as the per-share price for that commodity; or
  2930. a function taking three arguments, which is called to determine that
  2931. price.
  2932. If you use the functional form, you can either specify a function
  2933. name, or a lambda expression. Here's a function that yields the price
  2934. as $10 in whatever commodity is being requested:
  2935. @smallexample @c input:validate
  2936. define ten_dollars(s, date, t) = market($10, date, t)
  2937. @end smallexample
  2938. I can now use that in a lot value expression as follows:
  2939. @smallexample @c input:validate
  2940. 2012-04-10 My Broker
  2941. Assets:Brokerage:Cash $375.00
  2942. Assets:Brokerage -5 AAPL @{$50.00@} ((ten_dollars)) @@@@ $375.00
  2943. Income:Capital Gains $-125.00
  2944. @end smallexample
  2945. Alternatively, I could do the same thing without pre-defining
  2946. a function by using a lambda expression taking three arguments:
  2947. @smallexample
  2948. 2012-04-10 My Broker
  2949. A:B:Cash $375.00
  2950. A:B -5 AAPL @{$50.00@} ((s, d, t -> market($10, date, t))) @@@@ $375.00
  2951. Income:Capital Gains $-125.00
  2952. @end smallexample
  2953. The arguments passed to these functions have the following meaning:
  2954. @itemize
  2955. @item source
  2956. The source commodity string, or an amount object. If it is a string,
  2957. the return value must be an amount representing the price of the
  2958. commodity identified by that string (example: @samp{$}). If it is an
  2959. amount, return the value of that amount as a new amount (usually
  2960. calculated as commodity price times source amount).
  2961. @item date
  2962. The date to use for determining the value. If null, it means no date
  2963. was specified, which can mean whatever you want it to mean.
  2964. @item target
  2965. If not null, a string representing the desired target commodity that the
  2966. commodity price, or repriced amount, should be valued in. Note that
  2967. this string can be a comma-separated list, and that some or all of the
  2968. commodities in that list may be suffixed with an exclamation mark, to
  2969. indicate what is being desired.
  2970. @end itemize
  2971. In most cases, it is simplest to either use explicit amounts in your
  2972. valuation expressions, or just pass the arguments down to @samp{market}
  2973. after modifying them to suit your needs.
  2974. @node Automated Transactions, , Lot value expressions, Transactions
  2975. @section Automated Transactions
  2976. An automated transaction is a special kind of transaction which adds
  2977. its postings to other transactions any time one of that other
  2978. transactions' postings matches its predicate. The predicate uses the
  2979. same query syntax as the Ledger command-line.
  2980. Consider this posting:
  2981. @smallexample @c input:validate
  2982. 2012-03-10 KFC
  2983. Expenses:Food $20.00
  2984. Assets:Cash
  2985. @end smallexample
  2986. If I write this automated transaction before it in the file:
  2987. @smallexample @c input:validate
  2988. = expr true
  2989. Foo $50.00
  2990. Bar $-50.00
  2991. @end smallexample
  2992. Then the first transaction will be modified during parsing as if I'd
  2993. written this:
  2994. @smallexample @c input:validate
  2995. 2012-03-10 KFC
  2996. Expenses:Food $20.00
  2997. Foo $50.00
  2998. Bar $-50.00
  2999. Assets:Cash $-20.00
  3000. Foo $50.00
  3001. Bar $-50.00
  3002. @end smallexample
  3003. Despite this fancy logic, automated transactions themselves follow
  3004. most of the same rules as regular transactions: their postings must
  3005. balance (unless you use a virtual posting), you can have metadata,
  3006. etc.
  3007. One thing you cannot do, however, is elide amounts in an automated
  3008. transaction.
  3009. @menu
  3010. * Amount multipliers::
  3011. * Accessing the matching posting's amount::
  3012. * Referring to the matching posting's account::
  3013. * Applying metadata to every matched posting::
  3014. * Applying metadata to the generated posting::
  3015. * State flags::
  3016. * Effective Dates::
  3017. * Periodic Transactions::
  3018. * Concrete Example of Automated Transactions::
  3019. @end menu
  3020. @node Amount multipliers, Accessing the matching posting's amount, Automated Transactions, Automated Transactions
  3021. @subsection Amount multipliers
  3022. As a special case, if an automated transaction's posting's amount
  3023. (phew) has no commodity, it is taken as a multiplier upon the matching
  3024. posting's cost. For example:
  3025. @smallexample @c input:validate
  3026. = expr true
  3027. Foo 50.00
  3028. Bar -50.00
  3029. 2012-03-10 KFC
  3030. Expenses:Food $20.00
  3031. Assets:Cash
  3032. @end smallexample
  3033. Then the latter transaction turns into this during parsing:
  3034. @smallexample @c input:validate
  3035. 2012-03-10 KFC
  3036. Expenses:Food $20.00
  3037. Foo $1000.00
  3038. Bar $-1000.00
  3039. Assets:Cash $-20.00
  3040. Foo $1000.00
  3041. Bar $-1000.00
  3042. @end smallexample
  3043. @node Accessing the matching posting's amount, Referring to the matching posting's account, Amount multipliers, Automated Transactions
  3044. @subsection Accessing the matching posting's amount
  3045. If you use an amount expression for an automated transaction's
  3046. posting, that expression has access to all the details of the matched
  3047. posting. For example, you can refer to that posting's amount using
  3048. the ``amount'' value expression variable:
  3049. @smallexample @c input:validate
  3050. = expr true
  3051. (Foo) (amount * 2) ; same as just "2" in this case
  3052. 2012-03-10 KFC
  3053. Expenses:Food $20.00
  3054. Assets:Cash
  3055. @end smallexample
  3056. This becomes:
  3057. @smallexample @c input:validate
  3058. 2012-03-10 KFC
  3059. Expenses:Food $20.00
  3060. (Foo) $40.00
  3061. Assets:Cash $-20.00
  3062. (Foo) $-40.00
  3063. @end smallexample
  3064. @node Referring to the matching posting's account, Applying metadata to every matched posting, Accessing the matching posting's amount, Automated Transactions
  3065. @subsection Referring to the matching posting's account
  3066. Sometimes you want to refer to the account that was matched
  3067. in some way within the automated transaction itself. This is
  3068. done by using the string @samp{$account}, anywhere within the
  3069. account part of the automated posting:
  3070. @smallexample @c input:validate
  3071. = food
  3072. (Budget:$account) 10
  3073. 2012-03-10 KFC
  3074. Expenses:Food $20.00
  3075. Assets:Cash
  3076. @end smallexample
  3077. Becomes:
  3078. @smallexample @c input:validate
  3079. 2012-03-10 KFC
  3080. Expenses:Food $20.00
  3081. (Budget:Expenses:Food) $200.00
  3082. Assets:Cash $-20.00
  3083. @end smallexample
  3084. @node Applying metadata to every matched posting, Applying metadata to the generated posting, Referring to the matching posting's account, Automated Transactions
  3085. @subsection Applying metadata to every matched posting
  3086. If the automated transaction has a transaction note, that note is
  3087. copied (along with any metadata) to every posting that matches the
  3088. predicate:
  3089. @smallexample @c input:validate
  3090. = food
  3091. ; Foo: Bar
  3092. (Budget:$account) 10
  3093. 2012-03-10 KFC
  3094. Expenses:Food $20.00
  3095. Assets:Cash
  3096. @end smallexample
  3097. Becomes:
  3098. @smallexample @c input:validate
  3099. 2012-03-10 KFC
  3100. Expenses:Food $20.00
  3101. ; Foo: Bar
  3102. (Budget:Expenses:Food) $200.00
  3103. Assets:Cash $-20.00
  3104. @end smallexample
  3105. @node Applying metadata to the generated posting, State flags, Applying metadata to every matched posting, Automated Transactions
  3106. @subsection Applying metadata to the generated posting
  3107. If the automated transaction's posting has a note, that note is
  3108. carried to the generated posting within the matched transaction:
  3109. @smallexample @c input:validate
  3110. = food
  3111. (Budget:$account) 10
  3112. ; Foo: Bar
  3113. 2012-03-10 KFC
  3114. Expenses:Food $20.00
  3115. Assets:Cash
  3116. @end smallexample
  3117. Becomes:
  3118. @smallexample @c input:validate
  3119. 2012-03-10 KFC
  3120. Expenses:Food $20.00
  3121. (Budget:Expenses:Food) $200.00
  3122. ; Foo: Bar
  3123. Assets:Cash $-20.00
  3124. @end smallexample
  3125. This is slightly different from the rules for regular transaction
  3126. notes, in that an automated transaction's note does not apply to every
  3127. posting within the automated transaction itself, but rather to every
  3128. posting it matches.
  3129. @node State flags, Effective Dates, Applying metadata to the generated posting, Automated Transactions
  3130. @subsection State flags
  3131. Although you cannot mark an automated transaction as a whole as
  3132. cleared or pending, you can mark its postings with a @samp{*} or
  3133. @samp{!} before the account name, and that state flag gets carried to
  3134. the generated posting.
  3135. @node Effective Dates, Periodic Transactions, State flags, Automated Transactions
  3136. @subsection Effective Dates
  3137. @cindex effective dates
  3138. @findex --effective
  3139. In the real world, transactions do not take place instantaneously.
  3140. Purchases can take several days to post to a bank account. And you may
  3141. pay ahead for something for which you want to distribute costs. With
  3142. Ledger you can control every aspect of the timing of a transaction.
  3143. Say you're in business. If you bill a customer, you can enter
  3144. something like
  3145. @cindex effective date of invoice
  3146. @smallexample @c input:validate
  3147. 2008/01/01=2008/01/14 Client invoice ; estimated date you'll be paid
  3148. Assets:Accounts Receivable $100.00
  3149. Income: Client name
  3150. @end smallexample
  3151. Then, when you receive the payment, you change it to
  3152. @smallexample @c input:validate
  3153. 2008/01/01=2008/01/15 Client invoice ; actual date money received
  3154. Assets:Accounts Receivable $100.00
  3155. Income: Client name
  3156. @end smallexample
  3157. @noindent
  3158. and add something like
  3159. @smallexample @c input:validate
  3160. 2008/01/15 Client payment
  3161. Assets:Checking $100.00
  3162. Assets:Accounts Receivable
  3163. @end smallexample
  3164. Now
  3165. @smallexample @c command:validate
  3166. $ ledger --begin 2008/01/01 --end 2008/01/14 bal Income
  3167. @end smallexample
  3168. @noindent
  3169. gives you your accrued income in the first two weeks of the year, and
  3170. @smallexample @c command:validate
  3171. $ ledger --effective --begin 2008/01/01 --end 2008/01/14 bal Income
  3172. @end smallexample
  3173. @noindent
  3174. gives you your cash basis income in the same two weeks.
  3175. Another use is distributing costs out in time. As an example, suppose
  3176. you just prepaid into a local vegetable co-op that sustains you
  3177. through the winter. It costs $225 to join the program, so you write
  3178. a check. You don't want your October grocery budget to be blown
  3179. because you bought food ahead, however. What you really want is for
  3180. the money to be evenly distributed over the next six months so that
  3181. your monthly budgets gradually take a hit for the vegetables you'll
  3182. pick up from the co-op, even though you've already paid for them.
  3183. @smallexample @c input:6453542
  3184. 2008/10/16 * (2090) Bountiful Blessings Farm
  3185. Expenses:Food:Groceries $ 37.50 ; [=2008/10/01]
  3186. Expenses:Food:Groceries $ 37.50 ; [=2008/11/01]
  3187. Expenses:Food:Groceries $ 37.50 ; [=2008/12/01]
  3188. Expenses:Food:Groceries $ 37.50 ; [=2009/01/01]
  3189. Expenses:Food:Groceries $ 37.50 ; [=2009/02/01]
  3190. Expenses:Food:Groceries $ 37.50 ; [=2009/03/01]
  3191. Assets:Checking
  3192. @end smallexample
  3193. This entry accomplishes this. Every month you'll see an
  3194. automatic $37.50 deficit like you should, while your checking account
  3195. really knows that it debited $225 this month.
  3196. And using the @option{--effective} option, the initial date will be
  3197. overridden by the effective dates.
  3198. @smallexample @c command:6453542
  3199. $ ledger --effective register Groceries
  3200. @end smallexample
  3201. @smallexample @c output:6453542
  3202. 08-Oct-01 Bountiful Blessings.. Expense:Food:Groceries $ 37.50 $ 37.50
  3203. 08-Nov-01 Bountiful Blessings.. Expense:Food:Groceries $ 37.50 $ 75.00
  3204. 08-Dec-01 Bountiful Blessings.. Expense:Food:Groceries $ 37.50 $ 112.50
  3205. 09-Jan-01 Bountiful Blessings.. Expense:Food:Groceries $ 37.50 $ 150.00
  3206. 09-Feb-01 Bountiful Blessings.. Expense:Food:Groceries $ 37.50 $ 187.50
  3207. 09-Mar-01 Bountiful Blessings.. Expense:Food:Groceries $ 37.50 $ 225.00
  3208. @end smallexample
  3209. @node Periodic Transactions, Concrete Example of Automated Transactions, Effective Dates, Automated Transactions
  3210. @subsection Periodic Transactions
  3211. @findex --budget
  3212. A periodic transaction starts with a tilde @samp{~} followed by a period
  3213. expression (see @ref{Period Expressions}). Periodic transactions are used for budgeting and
  3214. forecasting only, they have no effect without the @option{--budget}
  3215. option specified. For examples and details, @pxref{Budgeting and
  3216. Forecasting}.
  3217. @node Concrete Example of Automated Transactions, , Periodic Transactions, Automated Transactions
  3218. @subsection Concrete Example of Automated Transactions
  3219. As a Bahá'í, I need to compute Huqúqu'lláh whenever I acquire assets.
  3220. It is similar to tithing for Jews and Christians, or to Zakát for
  3221. Muslims. The exact details of computing Huqúqu'lláh are somewhat
  3222. complex, but if you have further interest, please consult the Web.
  3223. Ledger makes this otherwise difficult law very easy. Just set up an
  3224. automated posting at the top of your ledger file:
  3225. @smallexample @c input:C371854
  3226. ; This automated transaction will compute Huqúqu'lláh based on this
  3227. ; journal's postings. Any accounts that match will affect the
  3228. ; Liabilities:Huququ'llah account by 19% of the value of that posting.
  3229. = /^(?:Income:|Expenses:(?:Business|Rent$|Furnishings|Taxes|Insurance))/
  3230. (Liabilities:Huququ'llah) 0.19
  3231. @end smallexample
  3232. This automated posting works by looking at each posting in the
  3233. ledger file. If any match the given value expression, 19% of the
  3234. posting's value is applied to the @samp{Liabilities:Huququ'llah}
  3235. account. So, if $1000 is earned from @samp{Income:Salary}, $190 is
  3236. added to @samp{Liabilities:Huqúqu'lláh}; if $1000 is spent on Rent,
  3237. $190 is subtracted.
  3238. @smallexample @c input:C371854
  3239. 2003/01/01 (99) Salary
  3240. Income:Salary -$1000
  3241. Assets:Checking
  3242. 2003/01/01 (100) Rent
  3243. Expenses:Rent $500
  3244. Assets:Checking
  3245. @end smallexample
  3246. The ultimate balance of Huqúqu'lláh reflects how
  3247. much is owed in order to fulfill one's obligation to Huqúqu'lláh.
  3248. When ready to pay, just write a check to cover the amount shown in
  3249. @samp{Liabilities:Huququ'llah}. That transaction would look like:
  3250. @smallexample @c input:validate
  3251. 2003/01/01 (101) Baha'i Huqúqu'lláh Trust
  3252. Liabilities:Huququ'llah $1,000.00
  3253. Assets:Checking
  3254. @end smallexample
  3255. That's it. To see how much Huqúq is currently owed based on your
  3256. ledger transactions, use:
  3257. @smallexample @c command:C371854
  3258. $ ledger balance Liabilities:Huquq
  3259. @end smallexample
  3260. @smallexample @c output:C371854
  3261. $-95 Liabilities:Huququ'llah
  3262. @end smallexample
  3263. This works fine, but omits one aspect of the law: that Huqúq is only
  3264. due once the liability exceeds the value of 19 mithqáls of gold (which
  3265. is roughly 2.22 ounces). So what we want is for the liability to
  3266. appear in the balance report only when it exceeds the present day
  3267. value of 2.22 ounces of gold. This can be accomplished using the
  3268. command:
  3269. @c TODO: fix this, it doesn't work any longer
  3270. @smallexample
  3271. $ ledger -Q -t "/Liab.*Huquq/?(a/P@{2.22 AU@}<=@{-1.0@}&a):a" bal liab
  3272. @end smallexample
  3273. With this command, the current price for gold is downloaded, and the
  3274. Huqúqu'lláh is reported only if its value exceeds that of 2.22 ounces
  3275. of gold. If you wish the liability to be reflected in the parent
  3276. subtotal either way, use this instead:
  3277. @c TODO: fix this, it doesn't work any longer
  3278. @smallexample
  3279. $ ledger -Q -T "/Liab.*Huquq/?(O/P@{2.22 AU@}<=@{-1.0@}&O):O" bal liab
  3280. @end smallexample
  3281. In some cases, you may wish to refer to the account of whichever posting
  3282. matched your automated transaction's value expression. To do this, use
  3283. the special account name @samp{$account}:
  3284. @smallexample @c input:validate
  3285. = /^Some:Long:Account:Name/
  3286. [$account] -0.10
  3287. [Savings] 0.10
  3288. @end smallexample
  3289. This example causes 10% of the matching account's total to be deferred
  3290. to the @samp{Savings} account---as a balanced virtual posting, which
  3291. may be excluded from reports by using @option{--real}.
  3292. @node Building Reports, Reporting Commands, Transactions, Top
  3293. @chapter Building Reports
  3294. @menu
  3295. * Introduction::
  3296. * Balance Reports::
  3297. * Typical queries::
  3298. * Advanced Reports::
  3299. @end menu
  3300. @node Introduction, Balance Reports, Building Reports, Building Reports
  3301. @section Introduction
  3302. The power of Ledger comes from the incredible flexibility in its
  3303. reporting commands, combined with formatting commands. Some options
  3304. control what is included in the calculations, and formatting controls
  3305. how it is displayed. The combinations are infinite. This chapter will
  3306. show you the basics of combining various options and commands. In the
  3307. next chapters you will find details about the specific commands and
  3308. options.
  3309. @node Balance Reports, Typical queries, Introduction, Building Reports
  3310. @section Balance Reports
  3311. @menu
  3312. * Controlling the Accounts and Payees::
  3313. * Controlling Formatting::
  3314. @end menu
  3315. @node Controlling the Accounts and Payees, Controlling Formatting, Balance Reports, Balance Reports
  3316. @subsection Controlling the Accounts and Payees
  3317. The balance report is the most commonly used report. The simplest
  3318. invocation is:
  3319. @smallexample @c command:1D00D56
  3320. $ ledger balance -f drewr3.dat
  3321. @end smallexample
  3322. @noindent
  3323. which will print the balances of every account in your journal.
  3324. @smallexample @c output:1D00D56
  3325. $ -3,804.00 Assets
  3326. $ 1,396.00 Checking
  3327. $ 30.00 Business
  3328. $ -5,200.00 Savings
  3329. $ -1,000.00 Equity:Opening Balances
  3330. $ 6,654.00 Expenses
  3331. $ 5,500.00 Auto
  3332. $ 20.00 Books
  3333. $ 300.00 Escrow
  3334. $ 334.00 Food:Groceries
  3335. $ 500.00 Interest:Mortgage
  3336. $ -2,030.00 Income
  3337. $ -2,000.00 Salary
  3338. $ -30.00 Sales
  3339. $ -63.60 Liabilities
  3340. $ -20.00 MasterCard
  3341. $ 200.00 Mortgage:Principal
  3342. $ -243.60 Tithe
  3343. --------------------
  3344. $ -243.60
  3345. @end smallexample
  3346. Most times, this is more than you want. Limiting the results to
  3347. specific accounts is as easy as entering the names of the accounts
  3348. after the command:
  3349. @smallexample @c command:06B2AD4
  3350. $ ledger balance -f drewr3.dat Auto MasterCard
  3351. @end smallexample
  3352. @smallexample @c output:06B2AD4
  3353. $ 5,500.00 Expenses:Auto
  3354. $ -20.00 Liabilities:MasterCard
  3355. --------------------
  3356. $ 5,480.00
  3357. @end smallexample
  3358. @noindent
  3359. Note the implicit logical or between @samp{Auto} and
  3360. @samp{Mastercard}.
  3361. If you want the entire contents of a branch of your account tree, use
  3362. the highest common name in the branch:
  3363. @smallexample @c command:B0468E1
  3364. $ ledger balance -f drewr3.dat Income
  3365. @end smallexample
  3366. @smallexample @c output:B0468E1
  3367. $ -2,030.00 Income
  3368. $ -2,000.00 Salary
  3369. $ -30.00 Sales
  3370. --------------------
  3371. $ -2,030.00
  3372. @end smallexample
  3373. You can use general regular expressions in nearly any place Ledger
  3374. needs a string:
  3375. @smallexample @c command:EAE389F
  3376. $ ledger balance -f drewr3.dat ^Bo
  3377. @end smallexample
  3378. @smallexample @c output:EAE389F
  3379. @end smallexample
  3380. This first example looks for any account starting with @samp{Bo}, of
  3381. which there are none.
  3382. @smallexample @c command:E2AF6AD
  3383. $ ledger balance -f drewr3.dat Bo
  3384. @end smallexample
  3385. @smallexample @c output:E2AF6AD
  3386. $ 20.00 Expenses:Books
  3387. @end smallexample
  3388. This second example looks for any account containing @samp{Bo}, which is
  3389. @samp{Expenses:Books}.
  3390. @cindex limit by payees
  3391. @findex --limit @var{EXPR}
  3392. If you want to know exactly how much you have spent in a particular
  3393. account on a particular payee, the following are equivalent:
  3394. @smallexample @c command:validate
  3395. $ ledger balance Auto:Fuel and Chevron
  3396. @end smallexample
  3397. @smallexample @c command:validate
  3398. $ ledger balance --limit 'account=~/Fuel/' and 'payee=~/Chev/'
  3399. @end smallexample
  3400. @noindent
  3401. will show you the amount expended on gasoline at Chevron. The second
  3402. example is the first example of the very powerful expression language
  3403. available to shape reports. The first example may be easier to
  3404. remember, but learning to use the second will open up far more
  3405. possibilities.
  3406. If you want to exclude specific accounts from the report, you can
  3407. exclude multiple accounts with parentheses:
  3408. @smallexample @c command:validate
  3409. $ ledger bal Expenses and not (Expenses:Drinks or Expenses:Candy or Expenses:Gifts)
  3410. @end smallexample
  3411. @node Controlling Formatting, , Controlling the Accounts and Payees, Balance Reports
  3412. @subsection Controlling Formatting
  3413. These examples all use the default formatting for the balance
  3414. report. Customizing the formatting can easily allowing to see only what
  3415. you want, or interface Ledger with other programs.
  3416. @node Typical queries, Advanced Reports, Balance Reports, Building Reports
  3417. @section Typical queries
  3418. A query such as the following shows all expenses since last
  3419. October, sorted by total:
  3420. @smallexample @c command:validate
  3421. $ ledger -b "last oct" -S T bal ^expenses
  3422. @end smallexample
  3423. From left to right the options mean: Show transactions since last
  3424. October; sort by the absolute value of the total; and report the balance
  3425. for all accounts that begin with @samp{expenses}.
  3426. @menu
  3427. * Reporting monthly expenses::
  3428. @end menu
  3429. @node Reporting monthly expenses, , Typical queries, Typical queries
  3430. @subsection Reporting monthly expenses
  3431. @findex --monthly
  3432. @findex --display @var{EXPR}
  3433. @findex --period-sort @var{VEXPR}
  3434. @findex --related
  3435. @findex --subtotal
  3436. The following query makes it easy to see monthly expenses, with each
  3437. month's expenses sorted by the amount:
  3438. @smallexample @c command:validate
  3439. $ ledger -M --period-sort "(amount)" reg ^expenses
  3440. @end smallexample
  3441. Now, you might wonder where the money came from to pay for these things.
  3442. To see that report, add @option{--related (-r)}, which shows the
  3443. ``related account'' postings:
  3444. @smallexample @c command:validate
  3445. $ ledger -M --period-sort "(amount)" -r reg ^expenses
  3446. @end smallexample
  3447. But maybe this prints too much information. You might just want to
  3448. see how much you're spending with your MasterCard. That kind of query
  3449. requires the use of a display predicate, since the postings
  3450. calculated must match @samp{^expenses}, while the postings
  3451. displayed must match @samp{mastercard}. The command would be:
  3452. @smallexample @c command:validate
  3453. $ ledger -M -r --display 'account=~/mastercard/' reg ^expenses
  3454. @end smallexample
  3455. This query says: Report monthly subtotals; report the ``related
  3456. account'' postings; display only related postings whose
  3457. account matches @samp{mastercard}, and base the calculation on
  3458. postings matching @samp{^expenses}.
  3459. This works just as well for reporting the overall total, too:
  3460. @smallexample @c command:validate
  3461. $ ledger -s -r --display "account=~/mastercard/" reg ^expenses
  3462. @end smallexample
  3463. The @option{--subtotal (-s)} option subtotals all postings, just as
  3464. @option{--monthly (-M)} subtotaled by the month. The running total in
  3465. both cases is off, however, since a display expression is being used.
  3466. @node Advanced Reports, , Typical queries, Building Reports
  3467. @section Advanced Reports
  3468. @menu
  3469. * Asset Allocation::
  3470. * Visualizing with Gnuplot::
  3471. @end menu
  3472. @node Asset Allocation, Visualizing with Gnuplot, Advanced Reports, Advanced Reports
  3473. @subsection Asset Allocation
  3474. A very popular method of managing portfolios is to control the
  3475. percent allocation of assets by certain categories. The mix of
  3476. categories and the weights applied to them vary by investing
  3477. philosophy, but most follow a similar pattern. Tracking asset
  3478. allocation in ledger is not difficult but does require some additional
  3479. effort to describe how the various assets you own contribute to the
  3480. asset classes you want to track.
  3481. In our simple example we assume you want to apportion your assets into
  3482. the general categories of domestic and international equities (stocks)
  3483. and a combined category of bonds and cash. For illustrative purposes,
  3484. we will use several publicly available mutual funds from Vanguard.
  3485. The three funds we will track are the Vanguard 500 IDX FD Signal
  3486. (VIFSX), the Vanguard Target Retirement 2030 (VTHRX), and the Vanguard
  3487. Short Term Federal Fund (VSGBX). Each of these funds allocates assets
  3488. to different categories of the investment universe and in different
  3489. proportions. When you buy a share of VTHRX, that share is partially
  3490. invested in equities, and partially invested in bonds and cash. Below
  3491. is the asset allocation for each of the instruments listed above:
  3492. @multitable @columnfractions .2 .2 .3 .3
  3493. @item @tab Domestic @tab Global @tab
  3494. @item Symbol @tab Equity @tab Equity @tab bonds/cash
  3495. @item VIFSX @tab 100% @tab @tab
  3496. @item VTHRX @tab 24.0% @tab 56.3% @tab 19.7%
  3497. @item VSGBX @tab @tab @tab 100%
  3498. @end multitable
  3499. These numbers are available from the prospectus of any publicly
  3500. available mutual fund. Of course a single stock issue is 100% equity
  3501. and a single bond issue is 100% bonds.
  3502. We track purchases of specific investments using the symbol of that
  3503. investment as its commodity. How do we tell Ledger that a share of
  3504. VTHRX is 24% Domestic equity? Enter automatic transactions and
  3505. virtual accounts.
  3506. At the top of our ledger we enter automatic transactions that describe
  3507. these proportions to Ledger. In the same entries we set up virtual
  3508. accounts that let us separate these abstract calculations from our
  3509. actual balances.
  3510. For the three instruments listed above, those automatic transactions
  3511. would look like:
  3512. @smallexample @c input:582C8C2
  3513. = expr ( commodity == 'VIFSX' )
  3514. (Allocation:Equities:Domestic) 1.000
  3515. = expr ( commodity == 'VTHRX' )
  3516. (Allocation:Equities:Global) 0.240
  3517. (Allocation:Equities:Domestic) 0.563
  3518. (Allocation:Bonds/Cash) 0.197
  3519. = expr ( commodity == 'VBMFX')
  3520. (Allocation:Bonds/Cash) 1.000
  3521. 2015-01-01 Buy VIFSX
  3522. Assets:Broker 100 VIFSX
  3523. Assets:Cash $-10000
  3524. 2015-01-01 Buy VTHRX
  3525. Assets:Broker 10 VTHRX
  3526. Assets:Cash $-10000
  3527. 2015-01-01 Buy VBMFX
  3528. Assets:Broker 1 VBMFX
  3529. Assets:Cash $-10000
  3530. @end smallexample
  3531. How do these work? First the @samp{=} sign at the beginning of the
  3532. line tells ledger this is an automatic transaction to be applied when
  3533. the condition following the @samp{=} is true. After the @samp{=} sign
  3534. is a value expression (@pxref{Value Expressions}) that returns true
  3535. any time a posting contains the commodity of interest.
  3536. The following line gives the proportions (not percentages) of each unit
  3537. of commodity that belongs to each asset class. Whenever Ledger sees a
  3538. buy or sell of a particular commodity it will credit or debit these
  3539. virtual accounts with that proportion of the number of shares moved.
  3540. Now that Ledger understands how to distribute the commodities amongst
  3541. the various asset classes how do we get a report that tells us our
  3542. current allocation? Using the balance command and some tricky
  3543. formatting!
  3544. @smallexample @c command:582C8C2
  3545. ledger bal Allocation --current --format "\
  3546. %-17((depth_spacer)+(partial_account))\
  3547. %10(percent(market(display_total), market(parent.total)))\
  3548. %16(market(display_total))\n%/"
  3549. @end smallexample
  3550. Which yields:
  3551. @smallexample @c output:582C8C2
  3552. Allocation 100.00% $30000
  3553. Bonds/Cash 39.90% $11970
  3554. Equities 60.10% $18030
  3555. Domestic 86.69% $15630
  3556. Global 13.31% $2400
  3557. @end smallexample
  3558. Let's look at the Ledger invocation a bit closer. The command above is
  3559. split into lines for clarity. The first line is very vanilla Ledger
  3560. asking for the current balances of the account in the ``Allocation''
  3561. tree, using a special formatter.
  3562. @cindex depth_spacer
  3563. @cindex display_total
  3564. @cindex parent.total
  3565. The magic is in the formatter. The second line simply tells Ledger to
  3566. print the partial account name indented by its depth in the tree. The
  3567. third line is where we calculate and display the percentages. The
  3568. @code{display_total} command gives the values of the total calculated
  3569. for the account in this line. The @code{parent.total} command gives
  3570. the total for the next level up in the tree. @code{percent} formats
  3571. their ratio as a percentage. The fourth line tells ledger to display
  3572. the current market value of the line. The last two characters
  3573. @samp{%/} tell Ledger what to do for the last line, in this case,
  3574. nothing.
  3575. @node Visualizing with Gnuplot, , Asset Allocation, Advanced Reports
  3576. @subsection Visualizing with Gnuplot
  3577. @cindex plotting
  3578. @cindex Gnuplot
  3579. @findex --amount-data
  3580. @findex --total-data
  3581. @findex --limit @var{EXPR}
  3582. @findex --display @var{EXPR}
  3583. If you have the ``Gnuplot'' program installed, you can graph any of the
  3584. above register reports. The script to do this is included in the ledger
  3585. distribution, and is named @file{contrib/report}. Install @file{report}
  3586. anywhere along your @env{PATH}, and then use @file{report} instead of
  3587. @file{ledger} when doing a register report. The only thing to keep in
  3588. mind is that you must specify @option{--amount-data (-j)} or
  3589. @option{--total-data (-J)} to indicate whether ``Gnuplot'' should plot
  3590. the amount, or the running total. For example, this command plots total
  3591. monthly expenses made on your MasterCard.
  3592. @smallexample
  3593. $ report -j -M -r --display "account =~ /mastercard/" reg ^expenses
  3594. @end smallexample
  3595. The @file{report} script is a very simple Bourne shell script, that
  3596. passes a set of scripted commands to ``Gnuplot''. Feel free to modify
  3597. the script to your liking, since you may prefer histograms to line
  3598. plots, for example.
  3599. Here are some useful plots:
  3600. @smallexample
  3601. report -j -M reg ^expenses # monthly expenses
  3602. report -J reg checking # checking account balance
  3603. report -J reg ^income ^expenses # cash flow report
  3604. # net worth report, ignoring non-$ postings
  3605. report -J -l "Ua>=@{\$0.01@}" reg ^assets ^liab
  3606. # net worth report starting last February. the use of a display
  3607. # predicate (-d) is needed, otherwise the balance will start at
  3608. # zero, and thus the y-axis will not reflect the true balance
  3609. report -J -l "Ua>=@{\$0.01@}" -d "d>=[last feb]" reg ^assets ^liab
  3610. @end smallexample
  3611. The last report uses both a calculation predicate @option{--limit
  3612. @var{EXPR} (-l)} and a display predicate @option{--display @var{EXPR}
  3613. (-d)}. The calculation predicate limits the report to postings whose
  3614. amount is greater than or equal to $1 (which can only happen if the
  3615. posting amount is in dollars). The display predicate limits the
  3616. transactions @emph{displayed} to just those since last February, even
  3617. though those transactions from before will be computed as part of the
  3618. balance.
  3619. @node Reporting Commands, Command-Line Syntax, Building Reports, Top
  3620. @chapter Reporting Commands
  3621. @menu
  3622. * Primary Financial Reports:: Reports in other formats:: Reports about
  3623. * Reports in other Formats::
  3624. * Reports about your Journals::
  3625. @end menu
  3626. @node Primary Financial Reports, Reports in other Formats, Reporting Commands, Reporting Commands
  3627. @section Primary Financial Reports
  3628. @menu
  3629. * The @command{balance} command::
  3630. * The @command{equity} command::
  3631. * The @command{register} command::
  3632. * The @command{print} command::
  3633. @end menu
  3634. @node The @command{balance} command, The @command{equity} command, Primary Financial Reports, Primary Financial Reports
  3635. @subsection The @command{balance} command
  3636. @findex balance
  3637. The @command{balance} command reports the current balance of all
  3638. accounts. It accepts a list of optional regexes, which confine the
  3639. balance report to the matching accounts. If an account contains
  3640. multiple types of commodities, each commodity's total is reported
  3641. separately.
  3642. @node The @command{equity} command, The @command{register} command, The @command{balance} command, Primary Financial Reports
  3643. @subsection The @command{equity} command
  3644. @findex equity
  3645. The @command{equity} command prints out account balances as if they
  3646. were transactions. This makes it easy to establish the starting
  3647. balances for an account, such as when @ref{Archiving Previous Years}.
  3648. @node The @command{register} command, The @command{print} command, The @command{equity} command, Primary Financial Reports
  3649. @subsection The @command{register} command
  3650. @findex register
  3651. @findex --amount-data
  3652. @findex --total-data
  3653. The @command{register} command displays all the postings occurring in
  3654. a single account, line by line. The account regex must be specified as
  3655. the only argument to this command. If any regexes occur after the
  3656. required account name, the register will contain only those postings
  3657. that match, which makes it very useful for hunting down a particular
  3658. posting.
  3659. The output from @command{register} is very close to what a typical
  3660. checkbook, or single-account ledger, would look like. It also shows a
  3661. running balance. The final running balance of any register should
  3662. always be the same as the current balance of that account.
  3663. If you have ``Gnuplot'' installed, you may plot the amount or running
  3664. total of any register by using the script @file{report}, which is
  3665. included in the Ledger distribution. The only requirement is that you
  3666. add either @option{--amount-data (-j)} or @option{--total-data (-J)} to
  3667. your @command{register} command, in order to plot either the amount or
  3668. total column, respectively.
  3669. @node The @command{print} command, , The @command{register} command, Primary Financial Reports
  3670. @subsection The @command{print} command
  3671. @findex print
  3672. The @command{print} command prints out ledger transactions in a textual
  3673. format that can be parsed by Ledger. They will be properly formatted,
  3674. and output in the most economic form possible. The @command{print}
  3675. command also takes a list of optional regexes, which will cause only
  3676. those postings which match in some way to be printed.
  3677. The @command{print} command can be a handy way to clean up a ledger
  3678. file whose formatting has gotten out of hand.
  3679. @node Reports in other Formats, Reports about your Journals, Primary Financial Reports, Reporting Commands
  3680. @section Reports in other Formats
  3681. @menu
  3682. * Comma Separated Values files::
  3683. * The @command{lisp} command::
  3684. * Emacs @command{org} Mode::
  3685. * Org mode with Babel::
  3686. * The @command{pricemap} command::
  3687. * The @command{xml} command::
  3688. * @command{prices} and @command{pricedb} commands::
  3689. @end menu
  3690. @node Comma Separated Values files, The @command{lisp} command, Reports in other Formats, Reports in other Formats
  3691. @subsection Comma Separated Values files
  3692. @menu
  3693. * The @command{csv} command::
  3694. * The @command{convert} command::
  3695. @end menu
  3696. @node The @command{csv} command, The @command{convert} command, Comma Separated Values files, Comma Separated Values files
  3697. @subsubsection The @command{csv} command
  3698. @findex csv
  3699. The @command{csv} command prints the desired ledger
  3700. transactions in a csv format suitable for importing into other programs.
  3701. You can specify the transactions to print using all the normal
  3702. limiting and searching functions.
  3703. @node The @command{convert} command, , The @command{csv} command, Comma Separated Values files
  3704. @subsubsection The @command{convert} command
  3705. @cindex csv importing
  3706. @cindex comma separated variable file reading
  3707. @findex convert
  3708. @findex --input-date-format @var{DATE_FORMAT}
  3709. The @command{convert} command parses a comma separated value (csv) file
  3710. and prints Ledger transactions. Many banks offer csv file downloads.
  3711. Unfortunately, the file formats, aside from the commas, are all
  3712. different. The ledger @command{convert} command tries to help as much
  3713. as it can.
  3714. Your bank's csv files will have fields in different orders from other
  3715. banks, so there must be a way to tell Ledger what to expect. Insert
  3716. a line at the beginning of the csv file that describes the fields to
  3717. Ledger.
  3718. For example, this is a portion of a csv file downloaded from a credit
  3719. union in the United States:
  3720. @smallexample
  3721. Account Name: VALUFIRST CHECKING
  3722. Account Number: 71
  3723. Date Range: 11/13/2011 - 12/13/2011
  3724. Transaction Number,Date,Description,Memo,Amount Debit,Amount Credit,Balance,Check Number,Fees
  3725. 767718,12/13/2011,"Withdrawal","ACE HARDWARE 16335 S HOUGHTON RD",-8.80,,00001640.04,,
  3726. 767406,12/13/2011,"Withdrawal","ACE HARDWARE 16335 S HOUGHTON RD",-1.03,,00001648.84,,
  3727. 683342,12/13/2011,"Visa Checking","NetFlix Date 12/12/11 000326585896 5968",-21.85,,00001649.87,,
  3728. 639668,12/13/2011,"Withdrawal","ID: 1741472662 CO: XXAA.COM PAYMNT",-236.65,,00001671.72,,
  3729. 1113648,12/12/2011,"Withdrawal","Tuscan IT #00037657",-29.73,,00001908.37,,
  3730. @end smallexample
  3731. Unfortunately, as it stands Ledger cannot read it, but you can. Ledger
  3732. expects the first line to contain a description of the fields on each
  3733. line of the file. The fields ledger can recognize contain these
  3734. case-insensitive strings @code{date}, @code{posted}, @code{code},
  3735. @code{payee} or @code{desc} or @code{description}, @code{amount},
  3736. @code{cost}, @code{total}, and @code{note}.
  3737. Delete the account description lines at the top, and replace the first
  3738. line in the data above with:
  3739. @smallexample
  3740. ,date,payee,note,amount,,,code,
  3741. @end smallexample
  3742. Then execute ledger like this:
  3743. @smallexample
  3744. $ ledger convert download.csv --input-date-format "%m/%d/%Y"
  3745. @end smallexample
  3746. Where the @option{--input-date-format @var{DATE_FORMAT}} option tells
  3747. ledger how to interpret the dates.
  3748. Importing csv files is a lot of work, but is very amenable to
  3749. scripting.
  3750. If there are columns in the bank data you would like to keep in your
  3751. ledger data, besides the primary fields described above, you can name
  3752. them in the field descriptor list and Ledger will include them in the
  3753. transaction as meta data if it doesn't recognize the field name. For
  3754. example, if you want to capture the bank transaction number and it
  3755. occurs in the first column of the data use:
  3756. @smallexample
  3757. transid,date,payee,note,amount,,,code,
  3758. @end smallexample
  3759. Ledger will include @samp{; transid: 767718} in the first transaction
  3760. from the file above.
  3761. @findex --invert
  3762. @findex --auto-match
  3763. @findex --account @var{STR}
  3764. @findex --rich-data
  3765. The @command{convert} command accepts four options. They are
  3766. @option{--invert} which inverts the amount field, @option{--auto-match}
  3767. which automatically matches an account from the Ledger journal for every
  3768. CSV line, @option{--account @var{STR}} which you can use to specify the
  3769. account to balance against, and @option{--rich-data} which stores
  3770. additional tag/value pairs.
  3771. Using the two first lines of the above csv file,
  3772. @smallexample @c file:01B0350
  3773. ,date,payee,note,amount,,,code,
  3774. 767718,12/13/2011,"Withdrawal","ACE HARDWARE 16335 S HOUGHTON RD",-8.80,,00001640.04,,
  3775. 767406,12/13/2011,"Withdrawal","ACE HARDWARE 16335 S HOUGHTON RD",-1.03,,00001648.84,,
  3776. @end smallexample
  3777. and launching the below command,
  3778. @smallexample @c command:01B0350,with_file:download.csv
  3779. $ ledger convert download.csv --input-date-format "%m/%d/%Y" \
  3780. --invert --account Assets:MyBank --rich-data \
  3781. --file sample.dat --now=2012/01/13
  3782. @end smallexample
  3783. you will get the result:
  3784. @smallexample @c output:01B0350
  3785. 2011/12/13 * Withdrawal ;ACE HARDWARE 16335 S HOUGHTON RD
  3786. ; CSV: 767718,12/13/2011,"Withdrawal","ACE HARDWARE 16335 S HOUGHTON RD",-8.80,,00001640.04,,
  3787. ; Imported: 2012/01/13
  3788. ; UUID: dfdc3c3d5c54c6967dd39d5b4e4fd1ea76e87233
  3789. Expenses:Unknown 8.8
  3790. Assets:MyBank
  3791. 2011/12/13 * Withdrawal ;ACE HARDWARE 16335 S HOUGHTON RD
  3792. ; CSV: 767406,12/13/2011,"Withdrawal","ACE HARDWARE 16335 S HOUGHTON RD",-1.03,,00001648.84,,
  3793. ; Imported: 2012/01/13
  3794. ; UUID: 63086448b1f29f7fd6efb11ea40660185a213f9d
  3795. Expenses:Unknown 1.03
  3796. Assets:MyBank
  3797. @end smallexample
  3798. The three added metadata are: @samp{CSV} as the original line from csv
  3799. file, @samp{Imported} as the date when the csv file was imported into
  3800. Ledger, and @samp{UUID} as a checksum of original csv line.
  3801. If an entry with the same @samp{UUID} tag is already included in the
  3802. normal ledger file (specified via @option{--file @var{FILE} (-f)} or via
  3803. the environment variable @env{LEDGER_FILE}) this entry will not be
  3804. printed again.
  3805. In the output above, the account is @samp{Expenses:Unknown} for CSV
  3806. lines. You can use the @option{--auto-match} option to automatically
  3807. match an account from your Ledger journal.
  3808. You can also use @command{convert} with @code{payee} and @code{account}
  3809. directives. First, you can use the @code{payee} and @code{alias}
  3810. directive to rewrite the @code{payee} field based on some rules. Then
  3811. you can use the account and its @code{payee} directive to specify the
  3812. account. I use it like this, for example:
  3813. @smallexample @c input:validate
  3814. payee Aldi
  3815. alias ^ALDI SUED SAGT DANKE
  3816. account Aufwand:Einkauf:Lebensmittel
  3817. payee ^(Aldi|Alnatura|Kaufland|REWE)$
  3818. @end smallexample
  3819. Note that it may be necessary for the output of @samp{ledger convert}
  3820. to be passed through @code{ledger print} a second time if you want to
  3821. match on the new payee field. During the @code{ledger convert} run,
  3822. only the original payee name as specified in the csv data seems to be
  3823. used.
  3824. @node The @command{lisp} command, Emacs @command{org} Mode, Comma Separated Values files, Reports in other Formats
  3825. @subsection The @command{lisp} command
  3826. @findex lisp
  3827. @findex emacs
  3828. The @command{lisp} command prints results in a form that can be read
  3829. directly by Emacs Lisp. The format of the @code{sexp} is:
  3830. @smallexample
  3831. ((BEG-POS CLEARED DATE CODE PAYEE
  3832. (ACCOUNT AMOUNT)...) ; list of postings
  3833. ...) ; list of transactions
  3834. @end smallexample
  3835. @noindent
  3836. @command{emacs} can also be used as a synonym for @command{lisp}.
  3837. @node Emacs @command{org} Mode, Org mode with Babel, The @command{lisp} command, Reports in other Formats
  3838. @subsection Emacs @command{org} Mode
  3839. @findex org
  3840. The @command{org} command produces a journal file suitable for use in
  3841. the Emacs Org mode. More details on using Org mode can be found at
  3842. @url{http://www.orgmode.org}.
  3843. Org mode has a sub-system known as Babel which allows for literate
  3844. programming. This allows you to mix text and code within the same
  3845. document and automatically execute code which may generate results
  3846. which will then appear in the text.
  3847. One of the languages supported by Babel is Ledger, so that you can have
  3848. ledger commands embedded in a text file and have the output of ledger
  3849. commands also appear in the text file. The output can be updated
  3850. whenever any new ledger entries are added.
  3851. For instance, the following Org mode text document snippet illustrates
  3852. a very naive but still useful application of the Babel system:
  3853. @smallexample
  3854. * A simple test of ledger in an org file
  3855. The following are some entries and I have requested that ledger be run
  3856. to generate a balance on the accounts. I could have asked for
  3857. a register or, in fact, anything at all the ledger can do through
  3858. command-line options.
  3859. #+begin_src ledger :cmdline bal :results value
  3860. 2010/01/01 * Starting balance
  3861. assets:bank:savings £1300.00
  3862. income:starting balances
  3863. 2010/07/22 * Got paid
  3864. assets:bank:chequing £1000.00
  3865. income:salary
  3866. 2010/07/23 Rent
  3867. expenses:rent £500.00
  3868. assets:bank:chequing
  3869. #+end_src
  3870. #+results:
  3871. : £1800.00 assets:bank
  3872. : £500.00 chequing
  3873. : £1300.00 savings
  3874. : £500.00 expenses:rent
  3875. : £-2300.00 income
  3876. : £-1000.00 salary
  3877. : £-1300.00 starting balances
  3878. @end smallexample
  3879. Typing @kbd{C-c C-c} anywhere in the ``ledger source code block'' will
  3880. invoke ledger on the contents of that block and generate a ``results''
  3881. block. The results block can appear anywhere in the file but, by
  3882. default, will appear immediately below the source code block.
  3883. You can combine multiple source code blocks before executing ledger and
  3884. do all kinds of other wonderful things with Babel (and Org mode).
  3885. @node Org mode with Babel, The @command{pricemap} command, Emacs @command{org} Mode, Reports in other Formats
  3886. @subsection Org mode with Babel
  3887. Using Babel, it is possible to record financial transactions
  3888. conveniently in an org file and subsequently generate the financial
  3889. reports required.
  3890. As of Org mode 7.01, Ledger support is provided. Check the Babel
  3891. documentation on Worg for instructions on how to achieve this but
  3892. I currently do this directly as follows:
  3893. @smallexample
  3894. (org-babel-do-load-languages
  3895. 'org-babel-load-languages
  3896. '((ledger . t) ;this is the important one for this tutorial
  3897. ))
  3898. @end smallexample
  3899. Once Ledger support in Babel has been enabled, we can proceed to
  3900. include Ledger entries within an org file. There are three ways (at
  3901. least) in which these can be included:
  3902. @enumerate
  3903. @item
  3904. place all Ledger entries within one single source block and execute this
  3905. block with different arguments to generate the appropriate reports,
  3906. @item
  3907. place Ledger entries in more than one source block and use the
  3908. @code{noweb} literary programming approach, supported by Babel, to
  3909. combine these into one block elsewhere in the file for processing by
  3910. Ledger,
  3911. @item
  3912. place Ledger entries in different source blocks and use @code{tangle} to
  3913. generate a Ledger file which you can subsequently process using Ledger
  3914. directly.
  3915. @end enumerate
  3916. The first two are described in more detail in this short tutorial.
  3917. @menu
  3918. * Embedded Ledger example with single source block::
  3919. * Multiple Ledger source blocks with @code{noweb}::
  3920. * Income Entries::
  3921. * Expenses::
  3922. * Financial Summaries::
  3923. * An overall balance summary::
  3924. * Generating a monthly register::
  3925. * Summary::
  3926. @end menu
  3927. @node Embedded Ledger example with single source block, Multiple Ledger source blocks with @code{noweb}, Org mode with Babel, Org mode with Babel
  3928. @subsubsection Embedded Ledger example with single source block
  3929. The easiest, albeit possibly least useful, way in which to use Ledger
  3930. within an org file is to use a single source block to record all Ledger
  3931. entries. The following is an example source block:
  3932. @smallexample
  3933. #+name: allinone
  3934. #+begin_src ledger
  3935. 2010/01/01 * Starting balance
  3936. assets:bank:savings £1300.00
  3937. income:starting balances
  3938. 2010/07/22 * Got paid
  3939. assets:bank:chequing £1000.00
  3940. income:salary
  3941. 2010/07/23 Rent
  3942. expenses:rent £500.00
  3943. assets:bank:chequing
  3944. 2010/07/24 Food
  3945. expenses:food £150.00
  3946. assets:bank:chequing
  3947. 2010/07/31 * Interest on bank savings
  3948. assets:bank:savings £3.53
  3949. income:interest
  3950. 2010/07/31 * Transfer savings
  3951. assets:bank:savings £250.00
  3952. assets:bank:chequing
  3953. 2010/08/01 got paid again
  3954. assets:bank:chequing £1000.00
  3955. income:salary
  3956. #+end_src
  3957. @end smallexample
  3958. In this example, we have combined both expenses and income into one set
  3959. of Ledger entries. We can now generate register and balance reports (as
  3960. well as many other types of reports) using Babel to invoke Ledger with
  3961. specific arguments. The arguments are passed to Ledger using the
  3962. @code{:cmdline} header argument. In the code block above, there is no
  3963. such argument so the system takes the default. For Ledger code blocks,
  3964. the default @code{:cmdline} argument is @code{bal} and the result of
  3965. evaluating this code block (@kbd{C-c C-c}) would be:
  3966. @smallexample
  3967. #+results: allinone()
  3968. : £2653.53 assets:bank
  3969. : £1100.00 chequing
  3970. : £1553.53 savings
  3971. : £650.00 expenses
  3972. : £150.00 food
  3973. : £500.00 rent
  3974. : £-3303.53 income
  3975. : £-3.53 interest
  3976. : £-2000.00 salary
  3977. : £-1300.00 starting balances
  3978. @end smallexample
  3979. If, instead, you wished to generate a register of all the transactions,
  3980. you would change the @code{#+begin_src} line for the code block to
  3981. include the required command-line option:
  3982. @smallexample
  3983. #+begin_src ledger :cmdline reg
  3984. @end smallexample
  3985. Evaluating the code block again would generate a different report.
  3986. Having to change the actual directive on the code block and re-evaluate
  3987. makes it difficult to have more than one view of your transactions and
  3988. financial state. Eventually, Babel will support passing arguments to
  3989. @code{#+call} evaluations of code blocks but this support is missing
  3990. currently. Instead, we can use the concepts of literary programming, as
  3991. implemented by the @code{noweb} features of Babel, to help us.
  3992. @node Multiple Ledger source blocks with @code{noweb}, Income Entries, Embedded Ledger example with single source block, Org mode with Babel
  3993. @subsubsection Multiple Ledger source blocks with @code{noweb}
  3994. The @code{noweb} feature of Babel allows us to expand references to
  3995. other code blocks within a code block. For Ledger, this can be used to
  3996. group transactions according to type, say, and then bring various sets
  3997. of transactions together to generate reports.
  3998. Using the same transactions used above, we could consider splitting
  3999. these into expenses and income, as follows:
  4000. @node Income Entries, Expenses, Multiple Ledger source blocks with @code{noweb}, Org mode with Babel
  4001. @subsubsection Income Entries
  4002. The first set of entries relates to income, either monthly pay or
  4003. interest, all typically going into one of my bank accounts. Here, I have
  4004. placed several entries, but we could have had each entry in a separate
  4005. @code{src} block. Note that all code blocks you wish to refer to later
  4006. must have the @code{:noweb yes} header argument specified.
  4007. @smallexample
  4008. #+name: income
  4009. #+begin_src ledger :noweb yes
  4010. 2010/01/01 * Starting balance
  4011. assets:bank:savings £1300.00
  4012. income:starting balances
  4013. 2010/07/22 * Got paid
  4014. assets:bank:chequing £1000.00
  4015. income:salary
  4016. 2010/07/31 * Interest on bank savings
  4017. assets:bank:savings £3.53
  4018. income:interest
  4019. 2010/07/31 * Transfer savings
  4020. assets:bank:savings £250.00
  4021. assets:bank:chequing
  4022. 2010/08/01 got paid again
  4023. assets:bank:chequing £1000.00
  4024. income:salary
  4025. #+end_src
  4026. @end smallexample
  4027. @node Expenses, Financial Summaries, Income Entries, Org mode with Babel
  4028. @subsubsection Expenses
  4029. The following entries relate to personal expenses, such as rent and
  4030. food. Again, these have all been placed in a single @code{src} block but
  4031. could have been done individually.
  4032. @smallexample
  4033. #+name: expenses
  4034. #+begin_src ledger :noweb yes
  4035. 2010/07/23 Rent
  4036. expenses:rent £500.00
  4037. assets:bank:chequing
  4038. 2010/07/24 Food
  4039. expenses:food £150.00
  4040. assets:bank:chequing
  4041. #+end_src
  4042. @end smallexample
  4043. @node Financial Summaries, An overall balance summary, Expenses, Org mode with Babel
  4044. @subsubsection Financial Summaries
  4045. Given the ledger entries defined above in the income and expenses code
  4046. blocks, we can now refer to these using the noweb expansion directives,
  4047. @code{<<name>>}. We can now define different code blocks to generate
  4048. specific reports for those transactions. Below are two examples, one to
  4049. generate a balance report and one to generate a register report of all
  4050. transactions.
  4051. @node An overall balance summary, Generating a monthly register, Financial Summaries, Org mode with Babel
  4052. @subsubsection An overall balance summary
  4053. @findex --subtotal
  4054. The overall balance of your account and expenditure with a breakdown
  4055. according to category is specified by passing the @code{:cmdline bal}
  4056. argument to Ledger. This code block can now be evaluated (@kbd{C-c C-c})
  4057. and the results generated by incorporating the transactions referred to
  4058. by the @code{<<income>>} and @code{<<expenses>>} lines.
  4059. @smallexample
  4060. #+name: balance
  4061. #+begin_src ledger :cmdline bal :noweb yes
  4062. <<income>>
  4063. <<expenses>>
  4064. #+end_src
  4065. #+results: balance
  4066. : £2653.53 assets:bank
  4067. : £1100.00 chequing
  4068. : £1553.53 savings
  4069. : £650.00 expenses
  4070. : £150.00 food
  4071. : £500.00 rent
  4072. : £-3303.53 income
  4073. : £-3.53 interest
  4074. : £-2000.00 salary
  4075. : £-1300.00 starting balances
  4076. @end smallexample
  4077. If you want a less detailed breakdown of where your money is, you can
  4078. specify the @option{--collapse (-n)} flag (i.e. @samp{:cmdline -n bal})
  4079. to tell Ledger to exclude sub-accounts in the report.
  4080. @smallexample
  4081. #+begin_src ledger :cmdline -n bal :noweb yes
  4082. <<income>>
  4083. <<expenses>>
  4084. #+end_src
  4085. #+results:
  4086. : £2653.53 assets
  4087. : £650.00 expenses
  4088. : £-3303.53 income
  4089. @end smallexample
  4090. @node Generating a monthly register, Summary, An overall balance summary, Org mode with Babel
  4091. @subsubsection Generating a monthly register
  4092. @findex register
  4093. @findex --monthly
  4094. You can also generate a monthly register (the @command{reg} command) by
  4095. executing the following @code{src} block. This presents a summary of
  4096. transactions for each monthly period (the @option{--monthly (-M)}
  4097. argument) with a running total in the final column (which should be 0 at
  4098. the end if all the entries are correct).
  4099. @smallexample
  4100. #+name: monthlyregister
  4101. #+begin_src ledger :cmdline -M reg :noweb yes
  4102. <<income>>
  4103. <<expenses>>
  4104. #+end_src
  4105. #+results: monthlyregister
  4106. :2010/01/01 - 2010/01/31 assets:bank:savings £1300.00 £1300.00
  4107. : in:starting balances £-1300.00 0
  4108. :2010/07/01 - 2010/07/31 assets:bank:chequing £100.00 £100.00
  4109. : assets:bank:savings £253.53 £353.53
  4110. : expenses:food £150.00 £503.53
  4111. : expenses:rent £500.00 £1003.53
  4112. : income:interest £-3.53 £1000.00
  4113. : income:salary £-1000.00 0
  4114. :2010/08/01 - 2010/08/01 assets:bank:chequing £1000.00 £1000.00
  4115. : income:salary £-1000.00 0
  4116. @end smallexample
  4117. We could also generate a monthly report on our assets showing how these
  4118. are increasing (or decreasing!). In this case, the final column will be
  4119. the running total of the assets in our ledger.
  4120. @smallexample
  4121. #+name: monthlyassetsregister
  4122. #+begin_src ledger :cmdline -M reg assets :noweb yes
  4123. <<income>>
  4124. <<expenses>>
  4125. #+end_src
  4126. #+results: monthlyassetsregister
  4127. : 2010/01/01 - 2010/01/31 assets:bank:savings £1300.00 £1300.00
  4128. : 2010/07/01 - 2010/07/31 assets:bank:chequing £100.00 £1400.00
  4129. : assets:bank:savings £253.53 £1653.53
  4130. : 2010/08/01 - 2010/08/01 assets:bank:chequing £1000.00 £2653.53
  4131. @end smallexample
  4132. @node Summary, , Generating a monthly register, Org mode with Babel
  4133. @subsubsection Summary
  4134. This short tutorial shows how Ledger entries can be embedded in an org
  4135. file and manipulated using Babel. However, only simple Ledger features
  4136. have been illustrated; please refer to the Ledger documentation for
  4137. examples of more complex operations on a ledger.
  4138. @node The @command{pricemap} command, The @command{xml} command, Org mode with Babel, Reports in other Formats
  4139. @subsection The @command{pricemap} command
  4140. @findex pricemap
  4141. If you have the @file{graphviz} graph visualization package installed,
  4142. ledger can generate a graph of the relationship between your various
  4143. commodities. The output file is in the ``dot'' format.
  4144. This is probably not very interesting, unless you have many different
  4145. commodities valued in terms of each other. For example, multiple
  4146. currencies and multiple investments valued in those currencies.
  4147. @node The @command{xml} command, @command{prices} and @command{pricedb} commands, The @command{pricemap} command, Reports in other Formats
  4148. @subsection The @command{xml} command
  4149. @findex xml
  4150. By default, Ledger uses a human-readable data format, and displays its
  4151. reports in a manner meant to be read on screen. For the purpose of
  4152. writing tools which use Ledger, however, it is possible to read and
  4153. display data using XML. This section documents that format.
  4154. The general format used for Ledger data is:
  4155. @smallexample
  4156. <?xml version="1.0"?>
  4157. <ledger>
  4158. <xact>...</xact>
  4159. <xact>...</xact>
  4160. <xact>...</xact>...
  4161. </ledger>
  4162. @end smallexample
  4163. The data stream is enclosed in a @code{ledger} tag, which contains a
  4164. series of one or more transactions. Each @code{xact} describes one
  4165. transaction and contains a series of one or more postings:
  4166. @smallexample
  4167. <xact>
  4168. <en:date>2004/03/01</en:date>
  4169. <en:cleared/>
  4170. <en:code>100</en:code>
  4171. <en:payee>John Wiegley</en:payee>
  4172. <en:postings>
  4173. <posting>...</posting>
  4174. <posting>...</posting>
  4175. <posting>...</posting>...
  4176. </en:postings>
  4177. </xact>
  4178. @end smallexample
  4179. The date format for @code{en:date} is always @code{YYYY/MM/DD}. The
  4180. @code{en:cleared} tag is optional, and indicates whether the posting has
  4181. been cleared or not. There is also an @code{en:pending} tag, for
  4182. marking pending postings. The @code{en:code} and @code{en:payee} tags
  4183. both contain whatever text the user wishes.
  4184. After the initial transaction data, there must follow a set of postings
  4185. marked with @code{en:postings}. Typically these postings will all
  4186. balance each other, but if not they will be automatically balanced into
  4187. an account named @samp{Unknown}.
  4188. Within the @code{en:postings} tag is a series of one or more
  4189. @code{posting}'s, which have the following form:
  4190. @smallexample
  4191. <posting>
  4192. <tr:account>Expenses:Computer:Hardware</tr:account>
  4193. <tr:amount>
  4194. <value type="amount">
  4195. <amount>
  4196. <commodity flags="PT">$</commodity>
  4197. <quantity>90.00</quantity>
  4198. </amount>
  4199. </value>
  4200. </tr:amount>
  4201. </posting>
  4202. @end smallexample
  4203. This is a basic posting. It may also begin with
  4204. @code{tr:virtual} and/or @code{tr:generated} tags, to indicate virtual
  4205. and auto-generated postings. Then follows the @code{tr:account}
  4206. tag, which contains the full name of the account the posting is
  4207. related to. Colons separate parent from child in an account name.
  4208. Lastly follows the amount of the posting, indicated by
  4209. @code{tr:amount}. Within this tag is a @code{value} tag, of which
  4210. there are four different kinds, each with its own format:
  4211. @enumerate
  4212. @item Boolean,
  4213. @item integer,
  4214. @item amount,
  4215. @item balance.
  4216. @end enumerate
  4217. The format of a Boolean value is @code{true} or @code{false}
  4218. surrounded by a @code{boolean} tag, for example:
  4219. @smallexample
  4220. <boolean>true</boolean>
  4221. @end smallexample
  4222. The format of an integer value is the numerical value surrounded by an
  4223. @code{integer} tag, for example:
  4224. @smallexample
  4225. <integer>12036</integer>
  4226. @end smallexample
  4227. The format of an amount contains two members, the commodity and the
  4228. quantity. The commodity can have a set of flags that indicate how to
  4229. display it. The meaning of the flags (all of which are optional) are:
  4230. @table @code
  4231. @item P
  4232. The commodity is prefixed to the value.
  4233. @item S
  4234. The commodity is separated from the value by a space.
  4235. @item T
  4236. Thousands markers are used to display the amount.
  4237. @item E
  4238. The format of the amount is European, with period used as a thousands
  4239. marker, and comma used as the decimal point.
  4240. @end table
  4241. The actual quantity for an amount is an integer of arbitrary size.
  4242. Ledger uses the GNU multiple precision arithmetic library to handle
  4243. such values. The XML format assumes the reader to be equally capable.
  4244. Here is an example amount:
  4245. @smallexample
  4246. <value type="amount">
  4247. <amount>
  4248. <commodity flags="PT">$</commodity>
  4249. <quantity>90.00</quantity>
  4250. </amount>
  4251. </value>
  4252. @end smallexample
  4253. Lastly, a balance value contains a series of amounts, each with a
  4254. different commodity. Unlike the name, such a value does need to
  4255. balance. It is called a balance because it sums several amounts. For
  4256. example:
  4257. @smallexample
  4258. <value type="balance">
  4259. <balance>
  4260. <amount>
  4261. <commodity flags="PT">$</commodity>
  4262. <quantity>90.00</quantity>
  4263. </amount>
  4264. <amount>
  4265. <commodity flags="TE">DM</commodity>
  4266. <quantity>200.00</quantity>
  4267. </amount>
  4268. </balance>
  4269. </value>
  4270. @end smallexample
  4271. That is the extent of the XML data format used by Ledger. It will
  4272. output such data if the @command{xml} command is used, and can read
  4273. the same data.
  4274. @node @command{prices} and @command{pricedb} commands, , The @command{xml} command, Reports in other Formats
  4275. @subsection @command{prices} and @command{pricedb} commands
  4276. @findex prices
  4277. @findex pricedb
  4278. @findex --average
  4279. The @command{prices} command displays the price history for matching
  4280. commodities. The @option{--average (-A)} option is useful with this
  4281. report, to display the running average price, or @option{--deviation
  4282. (-D)} to show each price's deviation from that average.
  4283. There is also a @command{pricedb} command which outputs the same
  4284. information as @command{prices}, but does so in a format that can be
  4285. parsed by Ledger. This is useful for generating and tidying up
  4286. pricedb database files.
  4287. @node Reports about your Journals, , Reports in other Formats, Reporting Commands
  4288. @section Reports about your Journals
  4289. @findex --count
  4290. @menu
  4291. * @command{accounts}::
  4292. * @command{payees}::
  4293. * @command{commodities}::
  4294. * @command{tags}::
  4295. * @command{xact}::
  4296. * @command{stats}::
  4297. * @command{select}::
  4298. @end menu
  4299. @node @command{accounts}, @command{payees}, Reports about your Journals, Reports about your Journals
  4300. @subsection @command{accounts}
  4301. @findex accounts
  4302. The @command{accounts} command reports all of the accounts in the
  4303. journal. Following the command with a regular expression will limit the
  4304. output to accounts matching the regex. The output is sorted by name.
  4305. Using the @option{--count} option will tell you how many entries use
  4306. each account.
  4307. @node @command{payees}, @command{commodities}, @command{accounts}, Reports about your Journals
  4308. @subsection @command{payees}
  4309. @findex payees
  4310. The @command{payees} command reports all of the unique payees in the
  4311. journal. Using the @option{--count} option will tell you how many
  4312. entries use each payee. To filter the payees displayed you must use the
  4313. prefix @@:
  4314. @smallexample @c command:validate
  4315. $ ledger payees @@Nic
  4316. @end smallexample
  4317. @smallexample
  4318. Nicolas
  4319. Nicolas BOILABUS
  4320. Oudtshoorn Municipality
  4321. Vaca Veronica
  4322. @end smallexample
  4323. @node @command{commodities}, @command{tags}, @command{payees}, Reports about your Journals
  4324. @subsection @command{commodities}
  4325. @findex commodities
  4326. Report all commodities present in the journals under consideration. The
  4327. output is sorted by name. Using the @option{--count} option will tell
  4328. you how many entries use each commodity.
  4329. @node @command{tags}, @command{xact}, @command{commodities}, Reports about your Journals
  4330. @subsection @command{tags}
  4331. @findex tags
  4332. @findex --values
  4333. The @command{tags} command reports all of the tags in the journal. The
  4334. output is sorted by name. Using the @option{--count} option will tell
  4335. you how many entries use each tag. Using the @option{--values} option
  4336. will report the values used by each tag.
  4337. @node @command{xact}, @command{stats}, @command{tags}, Reports about your Journals
  4338. @subsection @command{xact}
  4339. @findex draft
  4340. @findex entry
  4341. @findex xact
  4342. The @command{xact} command simplifies the creation of new transactions.
  4343. It works on the principle that 80% of all postings are variants of
  4344. earlier postings. Here's how it works:
  4345. Say you currently have this posting in your ledger file:
  4346. @smallexample @c input:03ACB97
  4347. 2004/03/15 * Viva Italiano
  4348. Expenses:Food $12.45
  4349. Expenses:Tips $2.55
  4350. Liabilities:MasterCard $-15.00
  4351. @end smallexample
  4352. Now it's @samp{2004/4/9}, and you've just eaten at @samp{Viva Italiano}
  4353. again. The exact amounts are different, but the overall form is the
  4354. same. With the @command{xact} command you can type:
  4355. @smallexample @c command:03ACB97
  4356. $ ledger xact 2004/4/9 viva food 11 tips 2.50
  4357. @end smallexample
  4358. This produces the following output:
  4359. @smallexample @c output:03ACB97
  4360. 2004/04/09 Viva Italiano
  4361. Expenses:Food $11.00
  4362. Expenses:Tips $2.50
  4363. Liabilities:MasterCard
  4364. @end smallexample
  4365. It works by finding a past posting matching the regular expression
  4366. @samp{viva}, and assuming that any accounts or amounts specified will be
  4367. similar to that earlier posting. If Ledger does not succeed in
  4368. generating a new transaction, an error is printed and the exit code is
  4369. set to @samp{1}.
  4370. Here are a few more examples of the @command{xact} command, assuming
  4371. the above journal transaction:
  4372. @smallexample
  4373. $ ledger xact 4/9 viva 11.50
  4374. $ ledger xact 4/9 viva 11.50 checking # (from `checking')
  4375. $ ledger xact 4/9 viva food 11.50 tips 8
  4376. $ ledger xact 4/9 viva food 11.50 tips 8 cash
  4377. $ ledger xact 4/9 viva food $11.50 tips $8 cash
  4378. $ ledger xact 4/9 viva dining "DM 11.50"
  4379. @end smallexample
  4380. @command{draft} and @command{entry} are both synonyms of
  4381. @command{xact}. @command{entry} is provided for backwards compatibility
  4382. with Ledger 2.X.
  4383. @node @command{stats}, @command{select}, @command{xact}, Reports about your Journals
  4384. @subsection @command{stats}
  4385. @findex stats
  4386. @findex stat
  4387. @value{FIXME:UNDOCUMENTED}
  4388. @node @command{select}, , @command{stats}, Reports about your Journals
  4389. @subsection @command{select}
  4390. @findex select
  4391. @value{FIXME:UNDOCUMENTED}
  4392. @node Command-Line Syntax, Budgeting and Forecasting, Reporting Commands, Top
  4393. @chapter Command-Line Syntax
  4394. @menu
  4395. * Basic Usage::
  4396. * Command-Line Quick Reference::
  4397. * Detailed Option Description::
  4398. * Period Expressions::
  4399. @end menu
  4400. @node Basic Usage, Command-Line Quick Reference, Command-Line Syntax, Command-Line Syntax
  4401. @section Basic Usage
  4402. This chapter describes Ledger's features and options. You may wish to
  4403. survey this to get an overview before diving into the @ref{Ledger
  4404. Tutorial} and more detailed examples that follow.
  4405. Ledger has a very simple command-line interface, named---enticingly
  4406. enough---@file{ledger}. It supports a few reporting commands, and
  4407. a large number of options for refining the output from those commands.
  4408. The basic syntax of any ledger command is:
  4409. @smallexample
  4410. $ ledger [OPTIONS...] COMMAND [ARGS...]
  4411. @end smallexample
  4412. After the command word there may appear any number of arguments. For
  4413. most commands, these arguments are regular expressions that cause the
  4414. output to relate only to postings matching those regular expressions.
  4415. For the @command{xact} command, the arguments have a special meaning,
  4416. described below.
  4417. The regular expressions arguments always match the account name that
  4418. a posting refers to. To match on the payee of the transaction
  4419. instead, precede the regular expression with @samp{payee} or
  4420. @samp{@@}. For example, the following balance command reports account
  4421. totals for rent, food and movies, but only those whose payee matches
  4422. Freddie:
  4423. @smallexample @c command:validate
  4424. $ ledger bal rent food movies payee freddie
  4425. @end smallexample
  4426. @noindent
  4427. or
  4428. @smallexample @c command:validate
  4429. $ ledger bal rent food movies @@freddie
  4430. @end smallexample
  4431. There are many, many command options available with the @file{ledger}
  4432. program, and it takes a while to master them. However, none of them are
  4433. required to use the basic reporting commands.
  4434. @node Command-Line Quick Reference, Detailed Option Description, Basic Usage, Command-Line Syntax
  4435. @section Command-Line Quick Reference
  4436. @menu
  4437. * Basic Reporting Commands::
  4438. * Basic Options::
  4439. * Report Filtering::
  4440. * Error Checking and Calculation Options::
  4441. * Output Customization::
  4442. * Grouping Options::
  4443. * Commodity Reporting::
  4444. @end menu
  4445. @node Basic Reporting Commands, Basic Options, Command-Line Quick Reference, Command-Line Quick Reference
  4446. @subsection Basic Reporting Commands
  4447. @ftable @command
  4448. @item balance
  4449. @itemx bal
  4450. Show account balances.
  4451. @item register
  4452. @itemx reg
  4453. Show all transactions with running total.
  4454. @item csv
  4455. @cindex csv exporting
  4456. Show transactions in csv format, for exporting to other programs.
  4457. @item print
  4458. Print transactions in a format readable by ledger.
  4459. @item xml
  4460. Produce XML output of the register command.
  4461. @item lisp
  4462. @itemx emacs
  4463. Produce s-expression output, suitable for Emacs.
  4464. @item equity
  4465. Print account balances as transactions.
  4466. @item prices
  4467. Print price history for matching commodities.
  4468. @item pricedb
  4469. Print price history for matching commodities in a format readable by
  4470. ledger.
  4471. @item xact
  4472. Generate transactions based on previous postings.
  4473. @end ftable
  4474. @node Basic Options, Report Filtering, Basic Reporting Commands, Command-Line Quick Reference
  4475. @subsection Basic Options
  4476. @ftable @option
  4477. @item --help
  4478. @itemx -h
  4479. Display the man page for @file{ledger}.
  4480. @item --version
  4481. Print version information and exit.
  4482. @item --file @var{FILE}
  4483. @itemx -f @var{FILE}
  4484. Read @file{FILE} as a ledger file.
  4485. @item --output @var{FILE}
  4486. @itemx -o @var{FILE}
  4487. Redirect output to @file{FILE}.
  4488. @item --init-file @var{FILE}
  4489. @itemx -i @var{FILE}
  4490. Specify an options file.
  4491. @item --import @var{FILE}
  4492. Import @var{FILE} as Python module.
  4493. @item --account @var{STR}
  4494. @itemx -a @var{STR}
  4495. Specify default account @var{STR} for QIF file postings.
  4496. @end ftable
  4497. @node Report Filtering, Error Checking and Calculation Options, Basic Options, Command-Line Quick Reference
  4498. @subsection Report Filtering
  4499. @ftable @option
  4500. @item --current
  4501. @itemx -c
  4502. Display only transactions on or before the current date.
  4503. @item --begin @var{DATE}
  4504. @itemx -b @var{DATE}
  4505. Limit the processing to transactions on or after @var{DATE}.
  4506. @item --end @var{DATE}
  4507. @itemx -e @var{DATE}
  4508. Limit the processing to transactions before @var{DATE}.
  4509. @item --period @var{PERIOD_EXPRESSION}
  4510. @itemx -p @var{PERIOD_EXPRESSION}
  4511. Limit the processing to transactions in @var{PERIOD_EXPRESSION}.
  4512. @item --period-sort @var{VEXPR}
  4513. Sort postings within each period according to @var{VEXPR}.
  4514. @item --cleared
  4515. @itemx -C
  4516. Display only cleared postings.
  4517. @item --dc
  4518. Display register or balance in debit/credit format.
  4519. @item --uncleared
  4520. @itemx -U
  4521. Display only uncleared postings.
  4522. @item --real
  4523. @itemx -R
  4524. Display only real postings.
  4525. @item --actual
  4526. @itemx -L
  4527. Display only actual postings, not automated ones.
  4528. @item --related
  4529. @itemx -r
  4530. Display related postings.
  4531. @item --budget
  4532. Display how close your postings meet your budget.
  4533. @item --add-budget
  4534. Show unbudgeted postings.
  4535. @item --unbudgeted
  4536. Show only unbudgeted postings.
  4537. @item --forecast-while @var{VEXPR}
  4538. @itemx --forecast @var{VEXPR}
  4539. Project balances into the future.
  4540. @item --limit @var{EXPR}
  4541. @itemx -l @var{EXPR}
  4542. Limit which postings are used in calculations by @var{EXPR}.
  4543. @item --amount @var{EXPR}
  4544. @itemx -t @var{EXPR}
  4545. Change value expression reported in @command{register} report.
  4546. @item --total @var{VEXPR}
  4547. @itemx -T @var{VEXPR}
  4548. Change the value expression used for ``totals'' column in
  4549. @command{register} and @command{balance} reports.
  4550. @end ftable
  4551. @node Error Checking and Calculation Options, Output Customization, Report Filtering, Command-Line Quick Reference
  4552. @subsection Error Checking and Calculation Options
  4553. @ftable @option
  4554. @item --strict
  4555. Accounts, tags or commodities not previously declared will cause
  4556. warnings.
  4557. @item --pedantic
  4558. Accounts, tags or commodities not previously declared will cause errors.
  4559. @item --check-payees
  4560. Enable strict and pedantic checking for payees as well as accounts,
  4561. commodities and tags. This only works in conjunction with
  4562. @option{--strict} or @option{--pedantic}.
  4563. @item --immediate
  4564. Instruct ledger to evaluate calculations immediately rather than lazily.
  4565. @end ftable
  4566. @node Output Customization, Grouping Options, Error Checking and Calculation Options, Command-Line Quick Reference
  4567. @subsection Output Customization
  4568. @ftable @option
  4569. @item --collapse
  4570. @itemx -n
  4571. Collapse transactions with multiple postings.
  4572. @item --subtotal
  4573. @itemx -s
  4574. Report register as a single subtotal.
  4575. @item --by-payee
  4576. @itemx -P
  4577. Report subtotals by payee.
  4578. @item --empty
  4579. @itemx -E
  4580. Include empty accounts in the report.
  4581. @item --weekly
  4582. @itemx -W
  4583. Report posting totals by week.
  4584. @item --quarterly
  4585. Report posting totals by quarter.
  4586. @item --yearly
  4587. @itemx -Y
  4588. Report posting totals by year.
  4589. @item --dow
  4590. Report posting totals by day of week.
  4591. @item --sort @var{VEXPR}
  4592. @itemx -S @var{VEXPR}
  4593. Sort a report using @var{VEXPR}.
  4594. @item --wide
  4595. @itemx -w
  4596. Assume 132 columns instead of 80.
  4597. @item --head @var{INT}
  4598. Report the first @var{INT} postings.
  4599. @item --tail @var{INT}
  4600. Report the last @var{INT} postings.
  4601. @item --pager @var{FILE}
  4602. Direct output to @var{FILE} pager program.
  4603. @item --no-pager
  4604. Direct output to stdout, avoiding pager program.
  4605. @item --average
  4606. @itemx -A
  4607. Report the average posting value.
  4608. @item --deviation
  4609. @itemx -D
  4610. Report each posting's deviation from the average.
  4611. @item --percent
  4612. @itemx -%
  4613. Show subtotals in the balance report as percentages.
  4614. @c @item --totals
  4615. @c Include running total in the @command{xml} report
  4616. @item --pivot @var{TAG}
  4617. Produce a pivot table of the @var{TAG} type specified.
  4618. @item --amount-data
  4619. @itemx -j
  4620. Show only the date and value columns to format the output for plots.
  4621. @item --plot-amount-format @var{FORMAT_STRING}
  4622. Specify the format for the plot output.
  4623. @item --total-data
  4624. @itemx -J
  4625. Show only the date and total columns to format the output for plots.
  4626. @item --plot-total-format @var{FORMAT_STRING}
  4627. Specify the format for the plot output.
  4628. @item --display @var{EXPR}
  4629. @itemx -d @var{EXPR}
  4630. Display only postings that meet the criteria in the @var{EXPR}.
  4631. @item --date-format @var{DATE_FORMAT}
  4632. @itemx -y @var{DATE_FORMAT}
  4633. Change the basic date format used in reports.
  4634. @item --format @var{FORMAT_STRING}
  4635. @itemx --balance-format @var{FORMAT_STRING}
  4636. @itemx --register-format @var{FORMAT_STRING}
  4637. @itemx --prices-format @var{FORMAT_STRING}
  4638. @itemx -F @var{FORMAT_STRING}
  4639. Set the reporting format for various reports.
  4640. @item --anon
  4641. Print the ledger register with anonymized accounts and payees, useful
  4642. for filing bug reports.
  4643. @end ftable
  4644. @node Grouping Options, Commodity Reporting, Output Customization, Command-Line Quick Reference
  4645. @subsection Grouping Options
  4646. @ftable @option
  4647. @item --by-payee
  4648. @itemx -P
  4649. Group postings by common payee names.
  4650. @item --daily
  4651. @itemx -D
  4652. Group postings by day.
  4653. @item --weekly
  4654. @itemx -W
  4655. Group postings by week.
  4656. @item --monthly
  4657. @itemx -M
  4658. Group postings by month.
  4659. @item --quarterly
  4660. Group postings by quarter.
  4661. @item --yearly
  4662. @itemx -Y
  4663. Group postings by year.
  4664. @item --dow
  4665. Group by day of weeks.
  4666. @item --subtotal
  4667. @itemx -s
  4668. Group postings together, similar to the balance report.
  4669. @end ftable
  4670. @node Commodity Reporting, , Grouping Options, Command-Line Quick Reference
  4671. @subsection Commodity Reporting
  4672. @ftable @option
  4673. @item --price-db @var{FILE}
  4674. Use @file{FILE} for retrieving stored commodity prices.
  4675. @item --price-exp @var{INT}
  4676. @itemx --leeway @var{INT}
  4677. @itemx -Z @var{INT}
  4678. Set expected freshness of prices in @var{INT} minutes.
  4679. @item --download
  4680. @itemx -Q
  4681. Download quotes using the script named @file{getquote}.
  4682. @c FIXME: The option doesn't exist currently.
  4683. @c @item --getquote @var{FILE}
  4684. @c Sets the path to a user-defined script to download commodity prices.
  4685. @item --quantity
  4686. @itemx -O
  4687. Report commodity totals without conversion.
  4688. @item --basis
  4689. @itemx -B
  4690. Report cost basis.
  4691. @item --market
  4692. @itemx -V
  4693. Report last known market value.
  4694. @item --gain
  4695. @itemx -G
  4696. Report net gain or loss for commodities that have a price history.
  4697. @end ftable
  4698. @node Detailed Option Description, Period Expressions, Command-Line Quick Reference, Command-Line Syntax
  4699. @section Detailed Option Description
  4700. @menu
  4701. * Global Options::
  4702. * Session Options::
  4703. * Report Options::
  4704. * Basic options::
  4705. * Report filtering::
  4706. * Output customization::
  4707. * Commodity reporting::
  4708. * Environment variables::
  4709. @end menu
  4710. @node Global Options, Session Options, Detailed Option Description, Detailed Option Description
  4711. @subsection Global Options
  4712. Options for Ledger reports affect three separate scopes of operation:
  4713. Global, Session, and Report. In practice there is very little
  4714. difference between these scopes. Ledger 3.0 contains provisions for
  4715. GUIs, which would make use of the different scopes by keeping an
  4716. instance of Ledger running in the background and running multiple
  4717. sessions with multiple reports per session.
  4718. @ftable @option
  4719. @item --args-only
  4720. Ignore all environment and init-file settings and
  4721. use only command-line arguments to control Ledger. Useful for debugging
  4722. or testing small journal files not associated with your main financial
  4723. database.
  4724. @item --debug @var{CODE}
  4725. @value{FIXME:UNDOCUMENTED}
  4726. If ledger has been built with debug options this will provide extra data during
  4727. the run.
  4728. @item --help
  4729. @itemx -h
  4730. Display the man page for @file{ledger}.
  4731. @item --init-file @var{FILE}
  4732. Specify the location of the init file. The default is home directory
  4733. @file{~/.ledgerrc}, or current directory @file{./.ledgerrc} if not found
  4734. in home directory.
  4735. @item --options
  4736. Display the options in effect for this Ledger invocation, along with
  4737. their values and the source of those values, for example:
  4738. @smallexample @c command:A9349E4,with_input:03ACB97
  4739. $ ledger --options bal --cleared
  4740. @end smallexample
  4741. @smallexample @c output:A9349E4
  4742. ===============================================================================
  4743. [Global scope options]
  4744. --args-only --args-only
  4745. [Session scope options]
  4746. --file = A9349E4.dat --file
  4747. [Report scope options]
  4748. --cleared --cleared
  4749. --columns = 80 --columns
  4750. --limit = cleared --cleared
  4751. ===============================================================================
  4752. $15.00 Expenses
  4753. $12.45 Food
  4754. $2.55 Tips
  4755. $-15.00 Liabilities:MasterCard
  4756. --------------------
  4757. 0
  4758. @end smallexample
  4759. @noindent
  4760. For the source column, a value starting with a @samp{-} or @samp{--}
  4761. indicated the source was a command-line argument. If the entry starts
  4762. with a @samp{$}, the source was an environment variable. If the source
  4763. is @code{?normalize} the value was set internally by ledger, in
  4764. a function called @code{normalize_options}.
  4765. @item --script @var{FILE}
  4766. Execute a ledger script.
  4767. @item --trace @var{INT}
  4768. Enable tracing. The @var{INT} specifies the level of trace desired.
  4769. @item --verbose
  4770. @itemx -v
  4771. Print detailed information on the execution of Ledger.
  4772. @item --verify
  4773. Enable additional assertions during run-time. This causes a significant
  4774. slowdown. When combined with @option{--debug @var{CODE}} ledger will
  4775. produce memory trace information.
  4776. @item --verify-memory
  4777. Verify that every constructed object is properly destructed. This is for
  4778. debugging purposes only.
  4779. @item --version
  4780. Print version information and exit.
  4781. @end ftable
  4782. @node Session Options, Report Options, Global Options, Detailed Option Description
  4783. @subsection Session Options
  4784. Options for Ledger reports affect three separate scopes of operation:
  4785. Global, Session, and Report. In practice there is very little
  4786. difference between these scopes. Ledger 3.0 contains provisions for
  4787. GUIs, which would make use of the different scopes by keeping an
  4788. instance of Ledger running in the background and running multiple
  4789. sessions with multiple reports per session.
  4790. @ftable @option
  4791. @item --check-payees
  4792. Enable strict and pedantic checking for payees as well as accounts,
  4793. commodities and tags. This only works in conjunction with
  4794. @option{--strict} or @option{--pedantic}.
  4795. @item --day-break
  4796. Break up @command{register} report of @ref{timelog} entries that span multiple
  4797. days by day.
  4798. @c see test/baseline/opt-day-break.dat
  4799. @c @smallexample @c input:
  4800. @c i 2015/
  4801. @c @end smallexample
  4802. @c @smallexample @c command:
  4803. @c $ ledger reg --day-break
  4804. @c @end smallexample
  4805. @c @smallexample @c output:
  4806. @c @end smallexample
  4807. @item --decimal-comma
  4808. Direct Ledger to parse journals using the European standard comma as
  4809. a decimal separator, not the usual period.
  4810. @item --download
  4811. @itemx -Q
  4812. Direct Ledger to download prices.
  4813. @c using the script defined via the option
  4814. @c @option{--getquote @var{FILE}}.
  4815. @item --explicit
  4816. Direct Ledger to require pre-declarations for entities (such as accounts,
  4817. commodities and tags) rather than taking entities from cleared
  4818. transactions as defined. This option is useful in combination with
  4819. @option{--strict} or @option{--pedantic}.
  4820. @item --file @var{FILE}
  4821. @itemx -f @var{FILE}
  4822. Specify the input @file{FILE} for this invocation.
  4823. @c FIXME: The option doesn't exist currently.
  4824. @c @item --getquote @var{FILE}
  4825. @c @cindex getquote
  4826. @c @cindex download prices
  4827. @c Tell ledger where to find the user defined script to download prices
  4828. @c information.
  4829. @item --input-date-format @var{DATE_FORMAT}
  4830. Specify the input date format for journal entries. For example,
  4831. @smallexample
  4832. $ ledger convert Export.csv --input-date-format "%m/%d/%Y"
  4833. @end smallexample
  4834. Would convert the @file{Export.csv} file to ledger format, assuming
  4835. the dates in the CSV file are like 12/23/2009 (@pxref{Date and Time
  4836. Format Codes}).
  4837. @item --master-account @var{STR}
  4838. Prepend all account names with the argument.
  4839. @smallexample @c command:A76BB56
  4840. $ ledger -f drewr3.dat bal --no-total --master-account HUMBUG
  4841. @end smallexample
  4842. @smallexample @c output:A76BB56
  4843. 0 HUMBUG
  4844. $ -3,804.00 Assets
  4845. $ 1,396.00 Checking
  4846. $ 30.00 Business
  4847. $ -5,200.00 Savings
  4848. $ -1,000.00 Equity:Opening Balances
  4849. $ 6,654.00 Expenses
  4850. $ 5,500.00 Auto
  4851. $ 20.00 Books
  4852. $ 300.00 Escrow
  4853. $ 334.00 Food:Groceries
  4854. $ 500.00 Interest:Mortgage
  4855. $ -2,030.00 Income
  4856. $ -2,000.00 Salary
  4857. $ -30.00 Sales
  4858. $ 180.00 Liabilities
  4859. $ -20.00 MasterCard
  4860. $ 200.00 Mortgage:Principal
  4861. @end smallexample
  4862. @item --no-aliases
  4863. Ledger does not expand any aliases if this option is specified.
  4864. @item --pedantic
  4865. Accounts, tags or commodities not previously declared will cause errors.
  4866. @item --permissive
  4867. Quiet balance assertions.
  4868. @item --price-db @var{FILE}
  4869. Specify the location of the price entry data file.
  4870. @item --price-exp @var{INT}
  4871. @itemx --leeway @var{INT}
  4872. @itemx -Z @var{INT}
  4873. Set the expected freshness of price quotes, in @var{INT} minutes. That
  4874. is, if the last known quote for any commodity is older than this value,
  4875. and if @option{--download} is being used, then the Internet will be
  4876. consulted again for a newer price. Otherwise, the old price is still
  4877. considered to be fresh enough.
  4878. @item --strict
  4879. Ledger normally silently accepts any account or commodity in a posting,
  4880. even if you have misspelled a commonly used one. The option
  4881. @option{--strict} changes that behavior. While running with
  4882. @option{--strict}, Ledger interprets all cleared transactions as
  4883. correct, and if it encounters a new account or commodity (same as
  4884. a misspelled commodity or account) it will issue a warning giving you
  4885. the file and line number of the problem.
  4886. @item --recursive-aliases
  4887. Normally, ledger only expands aliases once. With this option, ledger
  4888. tries to expand the result of alias expansion recursively, until no more
  4889. expansions apply.
  4890. @item --time-colon
  4891. The @option{--time-colon} option will display the value for a seconds
  4892. based commodity as real hours and minutes.
  4893. For example 8100 seconds by default will be displayed as 2.25 whereas
  4894. with the @option{--time-colon} option they will be displayed as 2:15.
  4895. @item --value-expr @var{VEXPR}
  4896. Set a global value expression annotation.
  4897. @c needs example
  4898. @end ftable
  4899. @node Report Options, Basic options, Session Options, Detailed Option Description
  4900. @subsection Report Options
  4901. Options for Ledger reports affect three separate scopes of operation:
  4902. Global, Session, and Report. In practice there is very little
  4903. difference between these scopes. Ledger 3.0 contains provisions for
  4904. GUIs, which would make use of the different scopes by keeping an
  4905. instance of Ledger running in the background and running multiple
  4906. sessions with multiple reports per session.
  4907. @ftable @option
  4908. @item --abbrev-len @var{INT}
  4909. Set the minimum length an account can be abbreviated to if it doesn't
  4910. fit inside the @code{account-width}. If @var{INT} is zero, then the
  4911. account name will be truncated on the right. If @var{INT} is greater
  4912. than @code{account-width} then the account will be truncated on the
  4913. left, with no shortening of the account names in order to fit into the
  4914. desired width.
  4915. @item --account @var{STR}
  4916. Prepend @var{STR} to all accounts reported. That is, the option
  4917. @samp{--account Personal} would tack @samp{Personal:} to the beginning
  4918. of every account reported in a balance report or register report.
  4919. @item --account-width @var{INT}
  4920. Set the width of the account column in the @command{register} report
  4921. to @var{INT} characters.
  4922. @item --actual
  4923. @itemx -L
  4924. Report only real transactions, ignoring all automated or virtual
  4925. transactions.
  4926. @item --add-budget
  4927. Show only unbudgeted postings.
  4928. @item --amount @var{EXPR}
  4929. @itemx -t @var{EXPR}
  4930. Apply the given value expression to the posting amount (@pxref{Value
  4931. Expressions}). Using @option{--amount @var{EXPR}} you can apply an
  4932. arbitrary transformation to the postings.
  4933. @item --amount-data
  4934. @itemx -j
  4935. On a register report print only the date and amount of postings.
  4936. Useful for graphing and spreadsheet applications.
  4937. @item --amount-width @var{INT}
  4938. Set the width in characters of the amount column in the
  4939. @command{register} report.
  4940. @item --anon
  4941. Anonymize registry output, mostly for sending in bug reports.
  4942. @item --auto-match
  4943. When generating a ledger transaction from a CSV file using the
  4944. @command{convert} command, automatically match an account from the
  4945. Ledger journal.
  4946. @item --aux-date
  4947. @itemx --effective
  4948. Show auxiliary dates for all calculations (@pxref{Effective Dates}).
  4949. @item --average
  4950. @itemx -A
  4951. Print average values over the number of transactions instead of
  4952. running totals.
  4953. @item --balance-format @var{FORMAT_STRING}
  4954. Specify the format to use for the @command{balance} report (@pxref{Format
  4955. Strings}). The default is:
  4956. @smallexample
  4957. "%(justify(scrub(display_total), 20, -1, true, color))"
  4958. " %(!options.flat ? depth_spacer : \"\")"
  4959. "%-(ansify_if(partial_account(options.flat), blue if color))\n%/"
  4960. "%$1\n%/"
  4961. "--------------------\n"
  4962. @end smallexample
  4963. @item --base
  4964. Reduce convertible commodities down the bottom of the conversion, e.g.
  4965. display time in seconds. This also applies to custom commodity
  4966. conversions (@pxref{Commodity equivalences}).
  4967. @item --basis
  4968. @itemx -B
  4969. @itemx --cost
  4970. Report the cost basis on all posting.
  4971. @item --begin @var{DATE}
  4972. Specify the start @var{DATE} of all calculations. Transactions before
  4973. that date will be ignored.
  4974. @item --bold-if @var{VEXPR}
  4975. Print the entire line in bold if the given value expression is true
  4976. (@pxref{Value Expressions}).
  4977. @smallexample @c command:validate
  4978. $ ledger reg Expenses --begin Dec --bold-if "amount>100"
  4979. @end smallexample
  4980. @noindent
  4981. list all transactions since the beginning of December and print in
  4982. bold any posting greater than $100.
  4983. @item --budget
  4984. Only display budgeted items. In a register report this
  4985. displays transactions in the budget, in a balance report this displays
  4986. accounts in the budget (@pxref{Budgeting and Forecasting}).
  4987. @item --budget-format @var{FORMAT_STRING}
  4988. Specify the format to use for the @command{budget} report (@pxref{Format
  4989. Strings}). The default is:
  4990. @smallexample
  4991. "%(justify(scrub(display_total), 20, -1, true, color))"
  4992. " %(!options.flat ? depth_spacer : \"\")"
  4993. "%-(ansify_if(partial_account(options.flat), blue if color))\n%/"
  4994. "%$1\n%/"
  4995. "--------------------\n"
  4996. @end smallexample
  4997. @item --by-payee
  4998. @itemx -P
  4999. Group the register report by payee.
  5000. @item --cleared
  5001. @itemx -C
  5002. Consider only transactions that have been cleared for display and
  5003. calculation.
  5004. @item --cleared-format @var{FORMAT_STRING}
  5005. @c FIXME thdox: to keep?
  5006. Specify the format to use for the @command{cleared} report (@pxref{Format
  5007. Strings}). The default is:
  5008. @smallexample
  5009. "%(justify(scrub(get_at(total_expr, 0)), 16, 16 + prepend_width, "
  5010. " true, color)) %(justify(scrub(get_at(total_expr, 1)), 18, "
  5011. " 36 + prepend_width, true, color))"
  5012. " %(latest_cleared ? format_date(latest_cleared) : \" \")"
  5013. " %(!options.flat ? depth_spacer : \"\")"
  5014. "%-(ansify_if(partial_account(options.flat), blue if color))\n%/"
  5015. "%$1 %$2 %$3\n%/"
  5016. "%(prepend_width ? \" \" * prepend_width : \"\")"
  5017. "---------------- ---------------- ---------\n"
  5018. @end smallexample
  5019. @item --collapse
  5020. @itemx -n
  5021. By default ledger prints all accounts in an account tree. With
  5022. @option{--collapse} it prints only the top level account specified.
  5023. @item --collapse-if-zero
  5024. Collapse the account display only if it has a zero balance.
  5025. @item --color
  5026. @itemx --ansi
  5027. Use color if the terminal supports it.
  5028. @item --columns @var{INT}
  5029. Specify the width of the @command{register} report in characters.
  5030. @item --count
  5031. Direct ledger to report the number of items when appended to the
  5032. @command{commodities}, @command{accounts} or @command{payees} command.
  5033. @item --csv-format @var{FORMAT_STRING}
  5034. Specify the format to use for the @command{csv} report (@pxref{Format
  5035. Strings}). The default is:
  5036. @smallexample
  5037. "%(quoted(date)),"
  5038. "%(quoted(code)),"
  5039. "%(quoted(payee)),"
  5040. "%(quoted(display_account)),"
  5041. "%(quoted(commodity(scrub(display_amount)))),"
  5042. "%(quoted(quantity(scrub(display_amount)))),"
  5043. "%(quoted(cleared ? \"*\" : (pending ? \"!\" : \"\"))),"
  5044. "%(quoted(join(note | xact.note)))\n"
  5045. @end smallexample
  5046. @item --current
  5047. Shorthand for @samp{--limit "date <= today"}.
  5048. @item --daily
  5049. @itemx -D
  5050. Shorthand for @samp{--period "daily"}.
  5051. @item --date @var{EXPR}
  5052. Transform the date of the transaction using @var{EXPR}.
  5053. @item --date-format @var{DATE_FORMAT}
  5054. @itemx -y @var{DATE_FORMAT}
  5055. Specify the format ledger should use to read and print dates
  5056. (@pxref{Date and Time Format Codes}).
  5057. @item --date-width @var{INT}
  5058. Specify the width, in characters, of the date column in the
  5059. @command{register} report.
  5060. @item --datetime-format @var{DATETIME_FORMAT}
  5061. Specify the format ledger should use to print datetimes.
  5062. @item --dc
  5063. Display register or balance in debit/credit format If you use
  5064. @option{--dc} with either the @command{register} (reg) or
  5065. @command{balance} (bal) commands, you will now get extra columns.
  5066. The register goes from this:
  5067. @smallexample
  5068. 12-Mar-10 Employer Assets:Cash $100 $100
  5069. Income:Employer $-100 0
  5070. 12-Mar-10 KFC Expenses:Food $20 $20
  5071. Assets:Cash $-20 0
  5072. 12-Mar-10 KFC - Rebate Assets:Cash $5 $5
  5073. Expenses:Food $-5 0
  5074. 12-Mar-10 KFC - Food & Reb.. Expenses:Food $20 $20
  5075. Expenses:Food $-5 $15
  5076. Assets:Cash $-15 0
  5077. @end smallexample
  5078. @noindent
  5079. To this:
  5080. @smallexample
  5081. 12-Mar-10 Employer Assets:Cash $100 0 $100
  5082. In:Employer 0 $100 0
  5083. 12-Mar-10 KFC Expens:Food $20 0 $20
  5084. Assets:Cash 0 $20 0
  5085. 12-Mar-10 KFC - Rebate Assets:Cash $5 0 $5
  5086. Expens:Food 0 $5 0
  5087. 12-Mar-10 KFC - Food &.. Expens:Food $20 0 $20
  5088. Expens:Food 0 $5 $15
  5089. Assets:Cash 0 $15 0
  5090. @end smallexample
  5091. @noindent
  5092. Where the first column is debits, the second is credits, and the third
  5093. is the running total. Only the running total may contain negative
  5094. values.
  5095. For the balance report without @option{--dc}:
  5096. @smallexample
  5097. $70 Assets:Cash
  5098. $30 Expenses:Food
  5099. $-100 Income:Employer
  5100. --------------------
  5101. 0
  5102. @end smallexample
  5103. @noindent
  5104. And with @option{--dc} it becomes this:
  5105. @smallexample
  5106. $105 $35 $70 Assets:Cash
  5107. $40 $10 $30 Expenses:Food
  5108. 0 $100 $-100 Income:Employer
  5109. --------------------------------------------
  5110. $145 $145 0
  5111. @end smallexample
  5112. @item --depth @var{INT}
  5113. Limit the depth of the account tree. In a balance report, for example,
  5114. a @samp{--depth 2} statement will print balances only for accounts with
  5115. two levels, i.e. @samp{Expenses:Entertainment} but not
  5116. @samp{Expenses:Entertainment:Dining}. This is a display predicate, which
  5117. means it only affects display, not the total calculations.
  5118. @item --deviation
  5119. Report each posting’s deviation from the average. It is only meaningful
  5120. in the register and prices reports.
  5121. @item --display @var{EXPR}
  5122. Display only lines that satisfy the expression @var{EXPR}.
  5123. @item --display-amount @var{EXPR}
  5124. Apply a transformation to the @emph{displayed} amount. This happens
  5125. after calculations occur.
  5126. @item --display-total @var{EXPR}
  5127. Apply a transformation to the @emph{displayed} total. This happens after
  5128. calculations occur.
  5129. @item --dow
  5130. @itemx --days-of-week
  5131. Group transactions by the day of the week.
  5132. @smallexample @c command:validate
  5133. $ ledger reg Expenses --dow --collapse
  5134. @end smallexample
  5135. @noindent
  5136. Will print all Expenses totaled for each day of the week.
  5137. @item --empty
  5138. @itemx -E
  5139. Include empty accounts in the report and in average calculations.
  5140. @item --end @var{DATE}
  5141. Specify the end @var{DATE} for a transaction to be considered in the
  5142. report. All transactions on or after this date are ignored.
  5143. @item --equity
  5144. Related to the @command{equity} command (@pxref{The @command{equity}
  5145. command}). Gives current account balances in the form of a register
  5146. report.
  5147. @item --exact
  5148. Report beginning and ending of periods by the date of the first and last
  5149. posting occurring in that period.
  5150. @item --exchange @var{COMMODITY}
  5151. @itemx -X @var{COMMODITY}
  5152. Display values in terms of the given @var{COMMODITY}. The latest
  5153. available price is used. The syntax
  5154. @option{-X @var{COMMODITY1}:@var{COMMODITY2}} displays values in @var{COMMODITY1}
  5155. in terms of @var{COMMODITY2} using the latest available price, but
  5156. will not automatically covert any other commodities to
  5157. @var{COMMODITY2}. Multiple @option{-X} arguments may be used on a
  5158. single command-line (as in
  5159. @option{-X COMMODITY1:COMMODITY2 -X COMMODITY3:COMMODITY2}),
  5160. which is particularly useful for situations where many prices are
  5161. available for reporting in terms of @var{COMMODITY2}, but only a few
  5162. should be displayed that way.
  5163. @item --flat
  5164. Force the full names of accounts to be used in the balance report. The
  5165. balance report will not use an indented tree.
  5166. @item --force-color
  5167. Output TTY color codes even if the TTY doesn't support them. Useful
  5168. for TTYs that don't advertise their capabilities correctly.
  5169. @item --force-pager
  5170. Force Ledger to paginate its output.
  5171. @item --forecast-while @var{VEXPR}
  5172. @itemx --forecast @var{VEXPR}
  5173. Continue forecasting while @var{VEXPR} is true.
  5174. @item --forecast-years @var{INT}
  5175. Forecast at most @var{INT} years into the future.
  5176. @item --format @var{FORMAT_STRING}
  5177. @itemx -F @var{FORMAT_STRING}
  5178. Use the given format string to print output.
  5179. @item --gain
  5180. @itemx -G
  5181. @itemx --change
  5182. Report on gains using the latest available prices.
  5183. @item --generated
  5184. Include auto-generated postings (such as those from automated
  5185. transactions) in the report, in cases where you normally wouldn't want
  5186. them.
  5187. @item --group-by @var{EXPR}
  5188. Group transactions together in the @command{register} report.
  5189. @var{EXPR} can be anything, although most common would be @code{payee}
  5190. or @code{commodity}. The @code{tags()} function is also useful here.
  5191. @item --group-title-format @var{FORMAT_STRING}
  5192. Set the format for the headers that separates the report sections of
  5193. a grouped report. Only has an effect with a @option{--group-by
  5194. @var{EXPR}} register report.
  5195. @smallexample @c command:validate
  5196. $ ledger reg Expenses --group-by "payee" --group-title-format "------------------------ %-20(value) ---------------------\n"
  5197. @end smallexample
  5198. @smallexample
  5199. ------------------------ 7-Eleven ---------------------
  5200. 2011/08/13 7-Eleven Expenses:Auto:Misc $ 5.80 $ 5.80
  5201. ------------------------ AAA Dues ---------------------
  5202. 2011/06/02 AAA Dues Expenses:Auto:Misc $ 215.00 $ 215.00
  5203. ------------------------ ABC Towing and Wrecking ---------------------
  5204. 2011/03/17 ABC Towing and Wrec.. Expenses:Auto:Hobbies $ 48.20 $ 48.20
  5205. ...
  5206. @end smallexample
  5207. @item --head @var{INT}
  5208. @itemx --first @var{INT}
  5209. Print the first @var{INT} entries. Opposite of @option{--tail
  5210. @var{INT}}.
  5211. @item --historical
  5212. @itemx -H
  5213. Value commodities at the time of their acquisition.
  5214. @item --immediate
  5215. Evaluate calculations immediately rather than lazily.
  5216. @item --inject
  5217. Use @code{Expected} amounts in calculations. In case you know
  5218. what amount a transaction should be, but the actual transaction has the
  5219. wrong value you can use metadata to specify the expected amount:
  5220. @smallexample @c input:validate
  5221. 2012-03-12 Paycheck
  5222. Income $-990; Expected:: $-1000.00
  5223. Checking
  5224. @end smallexample
  5225. Then using the command @code{ledger reg --inject=Expected Income} would
  5226. treat the transaction as if the ``Expected Value'' was actual.
  5227. @item --invert
  5228. Change the sign of all reported values.
  5229. @item --limit @var{EXPR}
  5230. @itemx -l @var{EXPR}
  5231. Only transactions that satisfy @var{EXPR} are considered in
  5232. calculations and for display.
  5233. @item --lot-dates
  5234. Report the date on which each commodity in a balance report was
  5235. purchased.
  5236. @item --lot-notes
  5237. @itemx --lot-tags
  5238. Report the tag attached to each commodity in a balance report.
  5239. @item --lot-prices
  5240. Report the price at which each commodity in a balance report was
  5241. purchased.
  5242. @item --lots
  5243. Report the date and price at which each commodity was purchased in
  5244. a balance report.
  5245. @item --lots-actual
  5246. Preserve the uniqueness of commodities so they aren't merged during
  5247. reporting without printing the lot annotations.
  5248. @item --market
  5249. @itemx -V
  5250. Use the latest market value for all commodities.
  5251. @item --meta @var{TAG}
  5252. In the register report, prepend the transaction with the value of the
  5253. given @var{TAG}.
  5254. @item --meta-width @var{INT}
  5255. Specify the width of the Meta column used for the @option{--meta
  5256. @var{TAG}} options.
  5257. @item --monthly
  5258. @itemx -M
  5259. Synonym for @samp{--period "monthly"}.
  5260. @item --no-aliases
  5261. Aliases are completely ignored.
  5262. @item --no-color
  5263. Suppress any color TTY output.
  5264. @item --no-pager
  5265. Direct output to stdout, avoiding pager program.
  5266. @item --no-revalued
  5267. Stop Ledger from showing @code{<Revalued>} postings. This option is useful
  5268. in combination with the @option{--exchange} or @option{--market} option.
  5269. @item --no-rounding
  5270. Don't output @samp{<Adjustment>} postings. Note that this will cause the
  5271. running total to often not add up! Its main use is for
  5272. @option{--amount-data (-j)} and @option{--total-data (-J)} reports.
  5273. @item --no-titles
  5274. Suppress the output of group titles.
  5275. @item --no-total
  5276. Suppress printing the final total line in a balance report.
  5277. @item --now @var{DATE}
  5278. Define the current date in case you want to calculate in the past or
  5279. future using @option{--current}.
  5280. @item --only @var{FIXME}
  5281. This is a postings predicate that applies after certain transforms have
  5282. been executed, such as periodic gathering.
  5283. @item --output @var{FILE}
  5284. Redirect the output of ledger to the file defined in @file{FILE}.
  5285. @item --pager @var{FILE}
  5286. Direct output to @var{FILE} pager program.
  5287. @item --payee @var{VEXPR}
  5288. Sets a value expression for formatting the payee. In the
  5289. @command{register} report this prevents the second entry from having
  5290. a date and payee for each transaction.
  5291. @item --payee-width @var{INT}
  5292. Set the number of columns dedicated to the payee in the register
  5293. report to @var{INT}.
  5294. @item --pending
  5295. Use only postings that are marked pending.
  5296. @item --percent
  5297. @itemx -%
  5298. Calculate the percentage value of each account in balance reports.
  5299. Only works for accounts that have a single commodity.
  5300. @item --period @var{PERIOD_EXPRESSION}
  5301. Define a period expression that sets the time period during which
  5302. transactions are to be accounted. For a @command{register} report only
  5303. the transactions that satisfy the period expression with be displayed.
  5304. For a @command{balance} report only those transactions will be accounted
  5305. in the final balances.
  5306. @item --pivot @var{TAG}
  5307. Produce a balance pivot report @emph{around} the given @var{TAG}. For
  5308. example, if you have multiple cars and track each fuel purchase in
  5309. @samp{Expenses:Auto:Fuel} and tag each fuel purchase with a tag
  5310. identifying which car the purchase was for @samp{; Car: Prius}, then the
  5311. command:
  5312. @smallexample @c command:validate
  5313. $ ledger bal Fuel --pivot "Car" --period "this year"
  5314. @end smallexample
  5315. @smallexample
  5316. $ 3491.26 Car
  5317. $ 1084.22 M3:Expenses:Auto:Fuel
  5318. $ 149.65 MG V11:Expenses:Auto:Fuel
  5319. $ 621.89 Prius:Expenses:Auto:Fuel
  5320. $ 1635.50 Sienna:Expenses:Auto:Fuel
  5321. $ 42.69 Expenses:Auto:Fuel
  5322. --------------------
  5323. $ 3533.95
  5324. @end smallexample
  5325. @xref{Metadata values}.
  5326. @item --plot-amount-format @var{FORMAT_STRING}
  5327. Define the output format for an amount data plot. @xref{Visualizing
  5328. with Gnuplot}.
  5329. @item --plot-total-format @var{FORMAT_STRING}
  5330. Define the output format for a total data plot. @xref{Visualizing with
  5331. Gnuplot}.
  5332. @item --prepend-format @var{FORMAT_STRING}
  5333. Prepend @var{STR} to every line of the output.
  5334. @item --prepend-width @var{INT}
  5335. Reserve @var{INT} spaces at the beginning of each line of the output.
  5336. @item --price
  5337. @itemx -I
  5338. Use the price of the commodity purchase for performing calculations.
  5339. @item --pricedb-format @var{FORMAT_STRING}
  5340. Set the format expected for the historical price file.
  5341. @item --prices-format @var{FORMAT_STRING}
  5342. Set the format for the @command{prices} report.
  5343. @item --primary-date
  5344. @itemx --actual-dates
  5345. Show primary dates for all calculations (@pxref{Effective Dates}).
  5346. @item --quantity
  5347. @itemx -O
  5348. Report commodity totals (this is the default).
  5349. @item --quarterly
  5350. Synonym for @samp{--period "quarterly"}.
  5351. @item --raw
  5352. In the @command{print} report, show transactions using the exact same
  5353. syntax as specified by the user in their data file. Don't do any
  5354. massaging or interpreting. This can be useful for minor cleanups, like
  5355. just aligning amounts.
  5356. @item --real
  5357. @itemx -R
  5358. Account using only real transactions ignoring virtual and automatic
  5359. transactions.
  5360. @item --register-format @var{FORMAT_STRING}
  5361. Define the output format for the @command{register} report.
  5362. @item --related
  5363. In a @command{register} report show the related account. This is the
  5364. other @emph{side} of the transaction.
  5365. @item --related-all
  5366. Show all postings in a transaction, similar to @option{--related} but
  5367. show both @emph{sides} of each transaction.
  5368. @item --revalued
  5369. Report discrepancy in values for manual reports by inserting @code{<Revalued>}
  5370. postings. This is implied when using the @option{--exchange} or
  5371. @option{--market} option.
  5372. @item --revalued-only
  5373. Show only @code{<Revalued>} postings.
  5374. @item --revalued-total @var{FIXME}
  5375. Display the sum of the revalued postings as the running total, which serves
  5376. to show unrealized capital in a gain/losses report.
  5377. @item --rich-data
  5378. @itemx --detail
  5379. When generating a ledger transaction from a CSV file using the
  5380. @command{convert} command, add CSV, Imported, and UUID metadata.
  5381. @item --seed @var{INT}
  5382. Set the random seed to @var{INT} for the @code{generate} command.
  5383. Used as part of development testing.
  5384. @item --sort @var{VEXPR}
  5385. @itemx -S @var{VEXPR}
  5386. Sort the @command{register} report based on the value expression given
  5387. to sort.
  5388. @item --sort-all @var{FIXME}
  5389. @value{FIXME:UNDOCUMENTED}
  5390. @item --sort-xacts @var{VEXPR}
  5391. @itemx --period-sort @var{VEXPR}
  5392. Sort the postings within transactions using the given value expression.
  5393. @item --start-of-week @var{INT}
  5394. Tell ledger to use a particular day of the week to start its ``weekly''
  5395. summary. @samp{--start-of-week=1} specifies Monday as the start of the
  5396. week.
  5397. @item --subtotal
  5398. @itemx -s
  5399. Cause all transactions in a @command{register} report to be collapsed
  5400. into a single, subtotaled transaction.
  5401. @item --tail @var{INT}
  5402. @itemx --last @var{INT}
  5403. Report only the last @var{INT} entries. Only useful in
  5404. a @command{register} report.
  5405. @item --time-report
  5406. Add two columns to the balance report to show the earliest checkin and
  5407. checkout times for timelog entries.
  5408. @item --total @var{VEXPR}
  5409. @itemx -T @var{VEXPR}
  5410. Define a value expression used to calculate the total in reports.
  5411. @item --total-data
  5412. @itemx -J
  5413. Show only dates and totals to format the output for plots.
  5414. @item --total-width @var{INT}
  5415. Set the width of the total field in the register report.
  5416. @item --truncate @var{CODE}
  5417. Indicates how truncation should happen when the contents of columns
  5418. exceed their width. Valid arguments are @samp{leading}, @samp{middle},
  5419. and @samp{trailing}. The default is smarter than any of these three,
  5420. as it considers sub-names within the account name (that style is
  5421. called ``abbreviate'').
  5422. @item --unbudgeted
  5423. Show only unbudgeted postings.
  5424. @item --uncleared
  5425. @itemx -U
  5426. Use only uncleared transactions in calculations and reports.
  5427. @item --unrealized
  5428. Show generated unrealized gain and loss accounts in the balance
  5429. report.
  5430. @item --unrealized-gains @var{STR}
  5431. Allow the user to specify what account name should be used for
  5432. unrealized gains. Defaults to @samp{"Equity:Unrealized Gains"}.
  5433. Often set in one's @file{~/.ledgerrc} file to change the default.
  5434. @item --unrealized-losses @var{STR}
  5435. Allow the user to specify what account name should be used for
  5436. unrealized losses. Defaults to @samp{"Equity:Unrealized Losses"}.
  5437. Often set in one's @file{~/.ledgerrc} file to change the default.
  5438. @item --unround
  5439. Perform all calculations without rounding and display results to full
  5440. precision.
  5441. @item --values
  5442. Shows the values used by each tag when used in combination with the
  5443. @command{tags} command.
  5444. @item --weekly
  5445. @itemx -W
  5446. Synonym for @samp{--period "weekly"}.
  5447. @item --wide
  5448. Let the register report use 132 columns instead of 80 (the default).
  5449. Identical to @samp{--columns "132"}.
  5450. @item --yearly
  5451. @itemx -Y
  5452. Synonym for @samp{--period "yearly"}.
  5453. @end ftable
  5454. @node Basic options, Report filtering, Report Options, Detailed Option Description
  5455. @subsection Basic options
  5456. These are the most basic command options. Most likely, the user will
  5457. want to set them using environment variables (see @ref{Environment
  5458. variables}), instead of using actual command-line options:
  5459. @ftable @option
  5460. @item --help
  5461. @itemx -h
  5462. Display the man page for @file{ledger}.
  5463. @item --version
  5464. Print the current version of ledger and exits. This is useful for
  5465. sending bug reports, to let the author know which version of ledger you
  5466. are using.
  5467. @item --file @var{FILE}
  5468. @itemx -f @var{FILE}
  5469. Read @file{FILE} as a ledger file. @var{FILE} can be @samp{-} which is
  5470. a synonym for @samp{/dev/stdin}. This command may be used multiple
  5471. times. Typically, the environment variable @env{LEDGER_FILE} is set,
  5472. rather than using this command-line option.
  5473. @item --output @var{FILE}
  5474. @itemx -o @var{FILE}
  5475. Redirect output from any command to @file{FILE}. By default, all output
  5476. goes to standard output.
  5477. @item --init-file @var{FILE}
  5478. @itemx -i @var{FILE}
  5479. Causes @file{FILE} to be read by ledger before any other ledger file.
  5480. This file may not contain any postings, but it may contain option
  5481. settings. To specify options in the init file, use the same syntax as
  5482. on the command-line, but put each option on its own line. Here is an
  5483. example init file:
  5484. @smallexample @c input:validate
  5485. --price-db ~/finance/.pricedb
  5486. --wide
  5487. ; ~/.ledgerrc ends here
  5488. @end smallexample
  5489. Option settings on the command-line or in the environment always take
  5490. precedence over settings in the init file.
  5491. @item --account @var{STR}
  5492. @itemx -a @var{STR}
  5493. Specify the default account which QIF file postings are assumed to
  5494. relate to.
  5495. @end ftable
  5496. @node Report filtering, Output customization, Basic options, Detailed Option Description
  5497. @subsection Report filtering
  5498. These options change which postings affect the outcome of a
  5499. report, in ways other than just using regular expressions:
  5500. @ftable @option
  5501. @item --current
  5502. @itemx -c
  5503. Display only transactions occurring on or before the current date.
  5504. @item --begin @var{DATE}
  5505. @itemx -b @var{DATE}
  5506. Constrain the report to transactions on or after @var{DATE}. Only
  5507. transactions after that date will be calculated, which means that the
  5508. running total in the balance report will always start at zero with the
  5509. first matching transaction. (Note: This is different from using
  5510. @option{--display @var{EXPR}} to constrain what is displayed).
  5511. @item --end @var{DATE}
  5512. @itemx -e @var{DATE}
  5513. Constrain the report so that transactions on or after @var{DATE} are
  5514. not considered.
  5515. @item --period @var{PERIOD_EXPRESSION}
  5516. @itemx -p @var{PERIOD_EXPRESSION}
  5517. Set the reporting period to @var{STR}. This will subtotal all matching
  5518. transactions within each period separately, making it easy to see
  5519. weekly, monthly, quarterly, etc., posting totals. A period string can
  5520. even specify the beginning and end of the report range, using simple
  5521. terms like @samp{last June} or @samp{next month}. For more details on
  5522. period expressions, see @ref{Period Expressions}.
  5523. @item --period-sort @var{VEXPR}
  5524. Sort the postings within each reporting period using the value
  5525. expression @var{EXPR}. This is most often useful when reporting
  5526. monthly expenses, in order to view the highest expense categories at
  5527. the top of each month:
  5528. @c TODO: the parameter to --period-sort was -At, which doesn't seem to work any longer
  5529. @smallexample @c command:validate
  5530. $ ledger -M --period-sort total reg ^Expenses
  5531. @end smallexample
  5532. @item --cleared
  5533. @itemx -C
  5534. Display only postings whose transaction has been marked ``cleared''
  5535. (by placing an asterisk to the right of the date).
  5536. @item --uncleared
  5537. @itemx -U
  5538. Display only postings whose transaction has not been marked ``cleared''
  5539. (i.e., if there is no asterisk to the right of the date).
  5540. @item --real
  5541. @itemx -R
  5542. Display only real postings, not virtual. (A virtual posting is
  5543. indicated by surrounding the account name with parentheses or brackets;
  5544. see @ref{Virtual postings} for more information).
  5545. @item --actual
  5546. @itemx -L
  5547. Display only actual postings, and not those created by automated
  5548. transactions.
  5549. @item --related
  5550. @itemx -r
  5551. Display postings that are related to whichever postings would
  5552. otherwise have matched the filtering criteria. In the register
  5553. report, this shows where money went to, or the account it came from.
  5554. In the balance report, it shows all the accounts affected by
  5555. transactions having a related posting. For example, if a file had
  5556. this transaction:
  5557. @smallexample @c input:94C5675
  5558. 2004/03/20 Safeway
  5559. Expenses:Food $65.00
  5560. Expenses:Cash $20.00
  5561. Assets:Checking $-85.00
  5562. @end smallexample
  5563. And the register command was:
  5564. @smallexample @c command:94C5675
  5565. $ ledger -f example.dat -r register food
  5566. @end smallexample
  5567. The following would be printed, showing the postings related to the
  5568. posting that matched:
  5569. @smallexample @c output:94C5675
  5570. 04-Mar-20 Safeway Expenses:Cash $20.00 $20.00
  5571. Assets:Checking $-85.00 $-65.00
  5572. @end smallexample
  5573. @item --budget
  5574. Useful for displaying how close your postings meet your budget.
  5575. @option{--add-budget} also shows unbudgeted postings, while
  5576. @option{--unbudgeted} shows only those. @option{--forecast @var{VEXPR}}
  5577. is a related option that projects your budget into the future, showing
  5578. how it will affect future balances. @xref{Budgeting and Forecasting}.
  5579. @item --limit @var{EXPR}
  5580. @itemx -l @var{EXPR}
  5581. Limit which postings take part in the calculations of a report.
  5582. @item --amount @var{EXPR}
  5583. @itemx -t @var{EXPR}
  5584. Change the value expression used to calculate the ``value'' column in
  5585. the @command{register} report, the amount used to calculate account
  5586. totals in the @command{balance} report, and the values printed in the
  5587. @command{equity} report. @xref{Value Expressions}.
  5588. @item --total @var{VEXPR}
  5589. @itemx -T @var{VEXPR}
  5590. Set the value expression used for the ``totals'' column in the
  5591. @command{register} and @command{balance} reports.
  5592. @end ftable
  5593. @c @node Search Terms, Output Customization, Report Filtering, Detailed Options Description
  5594. @c @subsection Search Terms
  5595. @c Valid Ledger invocations look like:
  5596. @c @smallexample
  5597. @c ledger [OPTIONS] <COMMAND> <SEARCH-TERMS>
  5598. @c @end smallexample
  5599. @c Where @code{COMMAND} is any command verb (@pxref{Reporting
  5600. @c Commands}), @code{OPTIONS} can occur anywhere, and
  5601. @c @code{SEARCH-TERM} is one or more of the following:
  5602. @c @smallexample
  5603. @c word search for any account containing 'word'
  5604. @c TERM and TERM boolean AND between terms
  5605. @c TERM or TERM boolean OR between terms
  5606. @c not TERM invert the meaning of the term
  5607. @c payee word search for any payee containing 'word'
  5608. @c @@word shorthand for 'payee word'
  5609. @c desc word alternate for 'payee word'
  5610. @c note word search for any note containing 'word'
  5611. @c &word shorthand for 'note word'
  5612. @c tag word search for any metadata tag containing 'word'
  5613. @c tag word=value search for any metadata tag containing 'word'
  5614. @c whose value contains 'value'
  5615. @c %word shorthand for 'tag word'
  5616. @c %word=value shorthand for 'tag word=value'
  5617. @c meta word alternate for 'tag word'
  5618. @c meta word=value alternate for 'tag word=value'
  5619. @c expr 'EXPR' apply the given value expression as a predicate
  5620. @c '=EXPR' shorthand for 'expr EXPR'
  5621. @c \( TERMS \) group terms; useful if using and/or/not
  5622. @c @end smallexample
  5623. @c So, to list all transaction that charged to ``food'' but not
  5624. @c ``dining'' for any payee other than ``chang'' the following three
  5625. @c commands would be equivalent:
  5626. @c @smallexample
  5627. @c ledger reg food not dining @@chang
  5628. @c ledger reg food and not dining and not payee chang
  5629. @c ledger reg food not dining expr 'payee =~ /chang/'
  5630. @c @end smallexample
  5631. @node Output customization, Commodity reporting, Report filtering, Detailed Option Description
  5632. @subsection Output customization
  5633. These options affect only the output, but not which postings are
  5634. used to create it:
  5635. @ftable @option
  5636. @item --collapse
  5637. @itemx -n
  5638. Cause transactions in a @command{register} report with multiple
  5639. postings to be collapsed into a single, subtotaled transaction.
  5640. @item --subtotal
  5641. @itemx -s
  5642. Cause all transactions in a @command{register} report to be collapsed
  5643. into a single, subtotaled transaction.
  5644. @item --by-payee
  5645. @itemx -P
  5646. Report subtotals by payee.
  5647. @item --empty
  5648. @itemx -E
  5649. Include even empty accounts in the @command{balance} report.
  5650. @item --weekly
  5651. @itemx -W
  5652. Report posting totals by the week. The week begins on whichever day of
  5653. the week begins the month containing that posting. To set a specific
  5654. begin date, use a period string, such as @samp{weekly from DATE}.
  5655. @item --monthly
  5656. @itemx -M
  5657. Report posting totals by month.
  5658. @item --yearly
  5659. @itemx -Y
  5660. Report posting totals by year. For more complex periods, use
  5661. @option{--period}.
  5662. @c TODO end this sentence
  5663. @item --period @var{PERIOD_EXPRESSION}
  5664. Option described above.
  5665. @item --dow
  5666. Report posting totals for each day of the week. This is an easy way
  5667. to see if weekend spending is more than on weekdays.
  5668. @item --sort @var{VEXPR}
  5669. @itemx -S @var{VEXPR}
  5670. Sort a report by comparing the values determined using the value
  5671. expression @var{VEXPR}. For example, using @samp{-S "-abs(total)"} in
  5672. the @command{balance} report will sort account balances from greatest to
  5673. least, using the absolute value of the total. For more on how to use
  5674. value expressions, see @ref{Value Expressions}.
  5675. @item --pivot @var{TAG}
  5676. Produce a pivot table around the @var{TAG} provided. This requires
  5677. meta data using valued tags.
  5678. @item --wide
  5679. @itemx -w
  5680. Cause the default @command{register} report to assume 132 columns
  5681. instead of 80.
  5682. @item --head @var{INT}
  5683. Cause only the first @var{INT} transactions to be printed. This is
  5684. different from using the command-line utility @file{head}, which would
  5685. limit to the first @var{INT} postings. @option{--tail @var{INT}} outputs
  5686. only the last @var{INT} transactions. Both options may be used
  5687. simultaneously. If a negative amount is given, it will invert the
  5688. meaning of the flag (instead of the first five transactions being
  5689. printed, for example, it would print all but the first five).
  5690. @item --pager @var{FILE}
  5691. Tell Ledger to pass its output to the given @var{FILE} pager program;
  5692. very useful when the output is especially long. This behavior can be
  5693. made the default by setting the @env{LEDGER_PAGER} environment variable.
  5694. @item --no-pager
  5695. Tell Ledger to @emph{not} pass its output to a pager program; useful
  5696. when a pager is set by default.
  5697. @item --average
  5698. @itemx -A
  5699. Report the average posting value.
  5700. @item --deviation
  5701. @itemx -D
  5702. Report each posting's deviation from the average. It is only meaningful
  5703. in the @command{register} and @command{prices} reports.
  5704. @item --percent
  5705. @itemx -%
  5706. Show account subtotals in the @command{balance} report as percentages of
  5707. the parent account.
  5708. @c @option{--totals} include running total information in the
  5709. @c @command{xml} report.
  5710. @item --amount-data
  5711. @itemx -j
  5712. Change the @command{register} report so that it prints nothing but the
  5713. date and the value column, and the latter without commodities. This is
  5714. only meaningful if the report uses a single commodity. This data can
  5715. then be fed to other programs, which could plot the date, analyze it,
  5716. etc.
  5717. @item --total-data
  5718. @itemx -J
  5719. Change the @command{register} report so that it prints nothing but the
  5720. date and total columns, without commodities.
  5721. @item --display @var{EXPR}
  5722. @itemx -d @var{EXPR}
  5723. Limit which postings or accounts are actually displayed in a report.
  5724. They might still be calculated, and be part of the running total of a
  5725. register report, for example, but they will not be displayed. This is
  5726. useful for seeing last month's checking postings, against a running
  5727. balance which includes all posting values:
  5728. @smallexample @c command:validate
  5729. $ ledger -d "d>=[last month]" reg checking
  5730. @end smallexample
  5731. The output from this command is very different from the following,
  5732. whose running total includes only postings from the last month
  5733. onward:
  5734. @smallexample @c command:validate
  5735. $ ledger -p "last month" reg checking
  5736. @end smallexample
  5737. Which is more useful depends on what you're looking to know: the total
  5738. amount for the reporting range (using @option{--period
  5739. @var{PERIOD_EXPRESSION} (-p)}), or simply a display restricted to the
  5740. reporting range (using @option{--display @var{EXPR} (-d)}).
  5741. @item --date-format @var{DATE_FORMAT}
  5742. @itemx -y @var{DATE_FORMAT}
  5743. Change the basic date format used by reports. The default uses a date
  5744. like @samp{2004/08/01}, which represents the default date format of
  5745. @code{%Y/%m/%d}. To change the way dates are printed in general, the
  5746. easiest way is to put @option{--date-format @var{DATE_FORMAT}} in the
  5747. Ledger initialization file @file{~/.ledgerrc} (or the file referred to
  5748. by @env{LEDGER_INIT}).
  5749. @item --format @var{FORMAT_STRING}
  5750. @itemx -F @var{FORMAT_STRING}
  5751. Set the reporting format for whatever report ledger is about to make.
  5752. @xref{Format Strings}. There are also specific format commands for
  5753. each report type:
  5754. @item --balance-format @var{FORMAT_STRING}
  5755. Define the output format for the @command{balance} report. The default
  5756. (defined in @file{report.h} is:
  5757. @smallexample
  5758. "%(ansify_if(
  5759. justify(scrub(display_total), 20,
  5760. 20 + int(prepend_width), true, color),
  5761. bold if should_bold))
  5762. %(!options.flat ? depth_spacer : \"\")
  5763. %-(ansify_if(
  5764. ansify_if(partial_account(options.flat), blue if color),
  5765. bold if should_bold))\n%/
  5766. %$1\n%/
  5767. %(prepend_width ? \" \" * int(prepend_width) : \"\")
  5768. --------------------\n"
  5769. @end smallexample
  5770. @item --cleared-format @var{FORMAT_STRING}
  5771. Define the format for the cleared report. The default is:
  5772. @smallexample
  5773. "%(justify(scrub(get_at(display_total, 0)), 16, 16 + int(prepend_width),
  5774. true, color)) %(justify(scrub(get_at(display_total, 1)), 18,
  5775. 36 + int(prepend_width), true, color))
  5776. %(latest_cleared ? format_date(latest_cleared) : \" \")
  5777. %(!options.flat ? depth_spacer : \"\")
  5778. %-(ansify_if(partial_account(options.flat), blue if color))\n%/
  5779. %$1 %$2 %$3\n%/
  5780. %(prepend_width ? \" \" * int(prepend_width) : \"\")
  5781. ---------------- ---------------- ---------\n"
  5782. @end smallexample
  5783. @item --register-format @var{FORMAT_STRING}
  5784. Define the output format for the @command{register} report. The default
  5785. (defined in @file{report.h} is:
  5786. @smallexample
  5787. "%(ansify_if(
  5788. ansify_if(justify(format_date(date), int(date_width)),
  5789. green if color and date > today),
  5790. bold if should_bold))
  5791. %(ansify_if(
  5792. ansify_if(justify(truncated(payee, int(payee_width)), int(payee_width)),
  5793. bold if color and !cleared and actual),
  5794. bold if should_bold))
  5795. %(ansify_if(
  5796. ansify_if(justify(truncated(display_account, int(account_width),
  5797. int(abbrev_len)), int(account_width)),
  5798. blue if color),
  5799. bold if should_bold))
  5800. %(ansify_if(
  5801. justify(scrub(display_amount), int(amount_width),
  5802. 3 + int(meta_width) + int(date_width) + int(payee_width)
  5803. + int(account_width) + int(amount_width) + int(prepend_width),
  5804. true, color),
  5805. bold if should_bold))
  5806. %(ansify_if(
  5807. justify(scrub(display_total), int(total_width),
  5808. 4 + int(meta_width) + int(date_width) + int(payee_width)
  5809. + int(account_width) + int(amount_width) + int(total_width)
  5810. + int(prepend_width), true, color),
  5811. bold if should_bold))\n%/
  5812. %(justify(\" \", int(date_width)))
  5813. %(ansify_if(
  5814. justify(truncated(has_tag(\"Payee\") ? payee : \" \",
  5815. int(payee_width)), int(payee_width)),
  5816. bold if should_bold))
  5817. %$3 %$4 %$5\n"
  5818. @end smallexample
  5819. @item --csv-format @var{FORMAT_STRING}
  5820. Set the format for @command{csv} reports. The default is:
  5821. @smallexample
  5822. "%(quoted(date)),
  5823. %(quoted(code)),
  5824. %(quoted(payee)),
  5825. %(quoted(display_account)),
  5826. %(quoted(commodity(scrub(display_amount)))),
  5827. %(quoted(quantity(scrub(display_amount)))),
  5828. %(quoted(cleared ? \"*\" : (pending ? \"!\" : \"\"))),
  5829. %(quoted(join(note | xact.note)))\n"
  5830. @end smallexample
  5831. @item --plot-amount-format @var{FORMAT_STRING}
  5832. Set the format for amount plots, using the @option{--amount-data (-j)}
  5833. option. The default is:
  5834. @smallexample
  5835. "%(format_date(date, \"%Y-%m-%d\")) %(quantity(scrub(display_amount)))\n"
  5836. @end smallexample
  5837. @item --plot-total-format @var{FORMAT_STRING}
  5838. Set the format for total plots, using the @option{--total-data (-J)}
  5839. option. The default is:
  5840. @smallexample
  5841. "%(format_date(date, \"%Y-%m-%d\")) %(quantity(scrub(display_total)))\n"
  5842. @end smallexample
  5843. @item --pricedb-format @var{FORMAT_STRING}
  5844. Set the format expected for the historical price file. The default is:
  5845. @smallexample
  5846. "P %(datetime) %(display_account) %(scrub(display_amount))\n"
  5847. @end smallexample
  5848. @item --prices-format @var{FORMAT_STRING}
  5849. Set the format for the @command{prices} report. The default is:
  5850. @smallexample
  5851. "%(date) %-8(display_account) %(justify(scrub(display_amount), 12,
  5852. 2 + 9 + 8 + 12, true, color))\n"
  5853. @end smallexample
  5854. @end ftable
  5855. @node Commodity reporting, Environment variables, Output customization, Detailed Option Description
  5856. @subsection Commodity reporting
  5857. These options affect how commodity values are displayed:
  5858. @ftable @option
  5859. @item --price-db @var{FILE}
  5860. Set the file that is used for recording downloaded commodity prices.
  5861. It is always read on startup, to determine historical prices. Other
  5862. settings can be placed in this file manually, to prevent downloading
  5863. quotes for a specific commodity, for example. This is done by adding a
  5864. line like the following:
  5865. @smallexample @c input:validate
  5866. ; Don't download quotes for the dollar, or timelog values
  5867. N $
  5868. N h
  5869. @end smallexample
  5870. @noindent
  5871. Note: Ledger NEVER writes output to files. You are responsible for
  5872. updating the price-db file. The best way is to have your price
  5873. download script maintain this file.
  5874. The format of the file can be changed by telling ledger to use the
  5875. @option{--pricedb-format @var{FORMAT_STRING}} you define.
  5876. @item --price-exp @var{INT}
  5877. @itemx --leeway @var{INT}
  5878. @itemx -Z @var{INT}
  5879. Set the expected freshness of price quotes, in @var{INT} minutes. That
  5880. is, if the last known quote for any commodity is older than this value,
  5881. and if @option{--download} is being used, then the Internet will be
  5882. consulted again for a newer price. Otherwise, the old price is still
  5883. considered to be fresh enough.
  5884. @item --download
  5885. @itemx -Q
  5886. Cause quotes to be automagically downloaded, as needed, by running
  5887. a script named @file{getquote} and expecting that script to return
  5888. a value understood by ledger. A sample implementation of
  5889. a @file{getquote} script, implemented in Perl, is provided in the
  5890. distribution. Downloaded quote price are then appended to the price
  5891. database, usually specified using the environment variable
  5892. @env{LEDGER_PRICE_DB}.
  5893. @end ftable
  5894. There are several different ways that ledger can report the totals it
  5895. displays. The most flexible way to adjust them is by using value
  5896. expressions, and the @option{--amount @var{EXPR} (-t)} and
  5897. @option{--total @var{VEXPR} (-T)} options. However, there are also
  5898. several ``default'' reports, which will satisfy most users' basic
  5899. reporting needs:
  5900. @ftable @option
  5901. @item --quantity
  5902. @itemx -O
  5903. Report commodity totals (this is the default).
  5904. @item --basis
  5905. @itemx -B
  5906. Report the cost basis for all postings.
  5907. @item --market
  5908. @itemx -V
  5909. Use the last known value for commodities to calculate final values.
  5910. @item --gain
  5911. @itemx -G
  5912. Report the net gain/loss for all commodities in the report that have
  5913. a price history.
  5914. @end ftable
  5915. Often you will be more interested in the value of your entire holdings,
  5916. in your preferred currency. It might be nice to know you hold 10,000
  5917. shares of PENNY, but you are more interested in whether or not that is
  5918. worth $1000.00 or $10,000.00. However, the current day value of a
  5919. commodity can mean different things to different people, depending on
  5920. the accounts involved, the commodities, the nature of the transactions,
  5921. etc.
  5922. @findex --now @var{DATE}
  5923. @findex --market
  5924. @findex --exchange @var{COMMODITY}
  5925. When you specify @option{--market (-V)}, or @option{--exchange
  5926. @var{COMMODITY} (-X)}, you are requesting that some or all of the
  5927. commodities be valuated as of today (or whatever @option{--now
  5928. @var{DATE}} is set to). But what does such a valuation mean? This
  5929. meaning is governed by the presence of a @var{VALUE} meta-data property,
  5930. whose content is an expression used to compute that value.
  5931. If no @var{VALUE} property is specified, each posting is assumed to have
  5932. a default, as if you'd specified a global, automated transaction as
  5933. follows:
  5934. @smallexample @c input:validate
  5935. = expr true
  5936. ; VALUE:: market(amount, date, exchange)
  5937. @end smallexample
  5938. This definition emulates the present day behavior of @option{--market
  5939. (-V)} and @option{--exchange @var{COMMODITY} (-X)} (in the case of
  5940. @samp{-X}, the requested commodity is passed via the string
  5941. @samp{exchange} above).
  5942. @cindex Euro conversion
  5943. One thing many people have wanted to do is to fixate the valuation of
  5944. old European currencies in terms of the Euro after a certain date:
  5945. @smallexample @c input:validate
  5946. = expr commodity == "DM"
  5947. ; VALUE:: date < [Jun 2008] ? market(amount, date, exchange) : 1.44 EUR
  5948. @end smallexample
  5949. This says: If @option{--now @var{DATE}} is some old date, use market
  5950. prices as they were at that time; but if @option{--now @var{DATE}} is
  5951. past June 2008, use a fixed price for converting Deutsch Mark to Euro.
  5952. Or how about never re-valuating commodities used in Expenses, since
  5953. they cannot have a different future value:
  5954. @smallexample @c input:validate
  5955. = /^Expenses:/
  5956. ; VALUE:: market(amount, post.date, exchange)
  5957. @end smallexample
  5958. This says the future valuation is the same as the valuation at the time
  5959. of posting. @code{post.date} equals the posting's date, while just 'date' is
  5960. the value of @option{--now @var{DATE}} (defaults to today).
  5961. Or how about valuating miles based on a reimbursement rate during a
  5962. specific time period:
  5963. @smallexample @c input:validate
  5964. = expr commodity == "miles" and date >= [2007] and date < [2008]
  5965. ; VALUE:: market($1.05, date, exchange)
  5966. @end smallexample
  5967. In this case, miles driven in 2007 will always be valuated at $1.05
  5968. each. If you use @samp{-X EUR} to expressly request all amounts in
  5969. Euro, Ledger shall convert $1.05 to Euro by whatever means are
  5970. appropriate for dollars.
  5971. Note that you can have a valuation expression specific to a particular
  5972. posting or transaction, by overriding these general defaults using
  5973. specific meta-data:
  5974. @smallexample @c input:validate
  5975. 2010-12-26 Example
  5976. Expenses:Food $20
  5977. ; Just to be silly, always valuate *these* $20 as 30 DM, no matter what
  5978. ; the user asks for with -V or -X
  5979. ; VALUE:: 30 DM
  5980. Assets:Cash
  5981. @end smallexample
  5982. This example demonstrates that your value expression should be as
  5983. symbolic as possible, using terms like 'amount' and 'date', rather than
  5984. specific amounts and dates. Also, you should pass the amount along to
  5985. the function 'market' so it can be further revalued if the user has
  5986. asked for a specific currency.
  5987. Or, if it better suits your accounting, you can be less symbolic,
  5988. which allows you to report most everything in EUR if you use @samp{-X
  5989. EUR}, except for certain accounts or postings which should always be
  5990. valuated in another currency. For example:
  5991. @c TODO is this example missing the actual line to get the effect?
  5992. @c it looks like it only contains a match, but no effect
  5993. @smallexample @c input:validate
  5994. = /^Assets:Brokerage:CAD$/
  5995. ; Always report the value of commodities in this account in
  5996. ; terms of present day dollars, despite what was asked for
  5997. ; on the command-line VALUE:: market(amount, date, @samp{$})
  5998. @end smallexample
  5999. @cindex FIFO/LIFO
  6000. @cindex LIFO/FIFO
  6001. @findex --lots
  6002. @findex --lot-prices
  6003. @findex --exchange @var{COMMODITY}
  6004. @findex --historical
  6005. @findex --basis
  6006. @findex --price
  6007. Ledger presently has no way of handling such things as FIFO and LIFO.
  6008. If you specify an unadorned commodity name, like AAPL, it will balance
  6009. against itself. If @option{--lots} are not being displayed, then it
  6010. will appear to balance against any lot of AAPL.
  6011. @cindex adorned commodity
  6012. @findex --lot-prices
  6013. If you specify an adorned commodity, like AAPL @{$10.00@}, it will also
  6014. balance against itself, and against any AAPL if @option{--lots} is not
  6015. specified. But if you do specify @option{--lot-prices}, for example,
  6016. then it will balance against that specific price for AAPL.
  6017. Normally when you use @option{--exchange @var{COMMODITY} (-X)} to
  6018. request that amounts be reported in a specific commodity, Ledger uses
  6019. these values:
  6020. @itemize
  6021. @item Register Report
  6022. For the @command{register} report, use the value of that commodity on
  6023. the date of the posting being reported, with a @samp{<Revalued>} posting
  6024. added at the end if today's value is different from the value of the
  6025. last posting.
  6026. @item Balance Report
  6027. For the @command{balance} report, use the value of that commodity as of
  6028. today.
  6029. @end itemize
  6030. You can now specify @option{--historical (-H)} to ask that all
  6031. valuations for any amount be done relative to the date that amount was
  6032. encountered.
  6033. You can also now use @option{--exchange @var{COMMODITY} (-X)} (and
  6034. @option{--historical (-H)}) in conjunction with @option{--basis (-B)}
  6035. and @option{--price (-I)}, to see valuation reports of just your basis
  6036. costs or lot prices.
  6037. Finally, sometimes, you may seek to only report one (or some subset) of
  6038. the commodities in terms of another commodity. In this situation, you
  6039. can use the syntax @option{--exchange @var{COMMODITY1}:@var{COMMODITY2}}
  6040. to request that ledger always display @var{COMMODITY1} in terms of
  6041. @var{COMMODITY2}, but you want no other commodities to be automatically
  6042. displayed in terms of @var{COMMODITY2} without additional
  6043. @option{--exchange} options. For example, if you wanted to report EUR
  6044. and BTC in terms of USD, but report all other commodities without
  6045. conversion to USD, you could use: @option{--exchange EUR:USD --exchange
  6046. BTC:USD}.
  6047. @node Environment variables, , Commodity reporting, Detailed Option Description
  6048. @subsection Environment variables
  6049. Every option to ledger may be set using an environment variable if the
  6050. option has a long name. For example setting the environment variable
  6051. @samp{@env{LEDGER_DATE_FORMAT}="%d.%m.%Y"} will have the same effect as specifying
  6052. @samp{@option{--date-format} '%d.%m.%Y'} on the command-line. Options on the
  6053. command-line always take precedence over environment variable settings, however.
  6054. Note that you may also permanently specify option values by placing
  6055. option settings in the file @file{~/.ledgerrc} one option per line, for example:
  6056. @smallexample @c input:validate
  6057. --pager /bin/cat
  6058. @end smallexample
  6059. @node Period Expressions, , Detailed Option Description, Command-Line Syntax
  6060. @section Period Expressions
  6061. @c TODO use @var below
  6062. A period expression indicates a span of time, or a reporting interval,
  6063. or both. The full syntax is:
  6064. @smallexample
  6065. [INTERVAL] [BEGIN] [END]
  6066. @end smallexample
  6067. The optional @var{INTERVAL} part may be any one of:
  6068. @smallexample
  6069. every day
  6070. every week
  6071. every month
  6072. every quarter
  6073. every year
  6074. every N days # N is any integer
  6075. every N weeks
  6076. every N months
  6077. every N quarters
  6078. every N years
  6079. daily
  6080. weekly
  6081. biweekly
  6082. monthly
  6083. bimonthly
  6084. quarterly
  6085. yearly
  6086. @end smallexample
  6087. After the interval, a begin time, end time, both or neither may be
  6088. specified. As for the begin time, it can be either of:
  6089. @smallexample
  6090. from <SPEC>
  6091. since <SPEC>
  6092. @end smallexample
  6093. The end time can be either of:
  6094. @smallexample
  6095. to <SPEC>
  6096. until <SPEC>
  6097. @end smallexample
  6098. Where @var{SPEC} can be any of:
  6099. @smallexample
  6100. 2004
  6101. 2004/10
  6102. 2004/10/1
  6103. 10/1
  6104. october
  6105. oct
  6106. this week # or day, month, quarter, year
  6107. next week
  6108. last week
  6109. @end smallexample
  6110. The beginning and ending can be given at the same time, if it spans a
  6111. single period. In that case, just use @var{SPEC} by itself. In that
  6112. case, the period @samp{oct}, for example, will cover all the days in
  6113. October. The possible forms are:
  6114. @smallexample
  6115. <SPEC>
  6116. in <SPEC>
  6117. @end smallexample
  6118. Here are a few examples of period expressions:
  6119. @smallexample
  6120. monthly
  6121. monthly in 2004
  6122. weekly from oct
  6123. weekly from last month
  6124. from sep to oct
  6125. from 10/1 to 10/5
  6126. monthly until 2005
  6127. from apr
  6128. until nov
  6129. last oct
  6130. weekly last august
  6131. @end smallexample
  6132. @node Budgeting and Forecasting, Time Keeping, Command-Line Syntax, Top
  6133. @chapter Budgeting and Forecasting
  6134. @menu
  6135. * Budgeting::
  6136. * Forecasting::
  6137. @end menu
  6138. @node Budgeting, Forecasting, Budgeting and Forecasting, Budgeting and Forecasting
  6139. @section Budgeting
  6140. @findex --budget
  6141. @findex --add-budget
  6142. @findex --unbudgeted
  6143. @findex --monthly
  6144. Keeping a budget allows you to pay closer attention to your income and
  6145. expenses, by reporting how far your actual financial activity is from
  6146. your expectations.
  6147. To start keeping a budget, put some periodic transactions
  6148. (@pxref{Periodic Transactions}) at the top of your ledger file. A
  6149. periodic transaction is almost identical to a regular transaction, except
  6150. that it begins with a tilde and has a period expression in place of a
  6151. payee. For example:
  6152. @smallexample @c input:validate
  6153. ~ Monthly
  6154. Expenses:Rent $500.00
  6155. Expenses:Food $450.00
  6156. Expenses:Auto:Gas $120.00
  6157. Expenses:Insurance $150.00
  6158. Expenses:Phone $125.00
  6159. Expenses:Utilities $100.00
  6160. Expenses:Movies $50.00
  6161. Expenses $200.00 ; all other expenses
  6162. Assets
  6163. ~ Yearly
  6164. Expenses:Auto:Repair $500.00
  6165. Assets
  6166. @end smallexample
  6167. These two periodic transactions give the usual monthly expenses, as well
  6168. as one typical yearly expense. For help on finding out what your
  6169. average monthly expenses are for any category, use a command like:
  6170. @smallexample @c command:validate
  6171. $ ledger -p "this year" --monthly --average balance ^expenses
  6172. @end smallexample
  6173. The reported totals are the current year's average for each account.
  6174. Once these periodic transactions are defined, creating a budget report is
  6175. as easy as adding @option{--budget} to the command-line. For example,
  6176. a typical monthly expense report would be:
  6177. @smallexample @c command:validate
  6178. $ ledger --monthly register ^expenses
  6179. @end smallexample
  6180. To see the same report balanced against your budget, use:
  6181. @smallexample @c command:validate
  6182. $ ledger --budget --monthly register ^expenses
  6183. @end smallexample
  6184. A budget report includes only those accounts that appear in the budget.
  6185. To see all expenses balanced against the budget, use
  6186. @option{--add-budget}. You can even see only the unbudgeted expenses
  6187. using @option{--unbudgeted}:
  6188. @smallexample @c command:validate
  6189. $ ledger --unbudgeted --monthly register ^expenses
  6190. @end smallexample
  6191. You can also use these flags with the @command{balance} command.
  6192. @node Forecasting, , Budgeting, Budgeting and Forecasting
  6193. @section Forecasting
  6194. @findex --forecast @var{VEXPR}
  6195. Sometimes it's useful to know what your finances will look like in the
  6196. future, such as determining when an account will reach zero. Ledger
  6197. makes this easy to do, using the same periodic transactions as are used
  6198. for budgeting. An example forecast report can be generated with:
  6199. @smallexample @c command:validate
  6200. $ ledger --file drewr3.dat --forecast "T>@{\$-500.00@}" register ^assets ^liabilities
  6201. @end smallexample
  6202. This report continues outputting postings until the running total
  6203. is greater than $-500.00. A final posting is always shown, to
  6204. inform you what the total afterwards would be.
  6205. Forecasting can also be used with the @command{balance} report,
  6206. but by date only, and not against the running total:
  6207. @smallexample @c command:validate
  6208. $ ledger --forecast "d<[2010]" bal ^assets ^liabilities
  6209. @end smallexample
  6210. @node Time Keeping, Value Expressions, Budgeting and Forecasting, Top
  6211. @chapter Time Keeping
  6212. @findex --day-break
  6213. @anchor{timelog}
  6214. Ledger directly supports ``timelog'' entries, which have this form:
  6215. @smallexample @c input:validate
  6216. i 2013/03/28 22:13:00 ACCOUNT[ PAYEE]
  6217. o 2013/03/29 03:39:00
  6218. @end smallexample
  6219. This records a check-in to the given ACCOUNT, and a check-out. You can
  6220. be checked-in to multiple accounts at a time, if you wish, and they can
  6221. span multiple days (use @option{--day-break} to break them up in the
  6222. report). The number of seconds between check-in and check-out is
  6223. accumulated as time to that ACCOUNT. If the checkout uses a capital
  6224. @samp{O}, the transaction is marked ``cleared''. You can use an
  6225. optional PAYEE for whatever meaning you like.
  6226. Now, there are a few ways to generate this information. You can use
  6227. the @file{timeclock.el} package, which is part of Emacs. Or you can
  6228. write a simple script in whichever language you prefer to emit similar
  6229. information. Or you can use Org mode's time-clocking abilities and
  6230. the @file{org2tc} script developed by John Wiegley.
  6231. These timelog entries can appear in a separate file, or directly in
  6232. your main ledger file. The initial @samp{i} and @samp{o} characters
  6233. count as Ledger ``directives'', and are accepted anywhere that
  6234. ordinary transactions are valid.
  6235. @node Value Expressions, Format Strings, Time Keeping, Top
  6236. @chapter Value Expressions
  6237. @findex --limit @var{EXPR}
  6238. @findex --display @var{EXPR}
  6239. Ledger uses value expressions to make calculations for many different
  6240. purposes:
  6241. @enumerate
  6242. @item
  6243. The values displayed in reports.
  6244. @item
  6245. For predicates (where truth is anything non-zero), to determine which
  6246. postings are calculated (option @option{--limit @var{EXPR} (-l)}) or
  6247. displayed (option @option{--display @var{EXPR} (-d)}).
  6248. @item
  6249. For sorting criteria, to yield the sort key.
  6250. @item
  6251. In the matching criteria used by automated postings.
  6252. @end enumerate
  6253. Value expressions support most simple math and logic operators, in
  6254. addition to a set of functions and variables.
  6255. @c A function's argument is whatever follows it. The following is
  6256. @c a display predicate that I use with the @command{balance} command:
  6257. @c @smallexample
  6258. @c ledger -d '/^Liabilities/?T<0:UT>100' balance
  6259. @c @end smallexample
  6260. @c The effect is that account totals are displayed only if: 1) A
  6261. @c Liabilities account has a total less than zero; or 2) the absolute
  6262. @c value of the account's total exceeds 100 units of whatever commodity
  6263. @c contains. If it contains multiple commodities, only one of them must
  6264. @c exceed 100 units.
  6265. Display predicates are also very handy with register reports, to
  6266. constrain which transactions are printed. For example, the following
  6267. command shows only transactions from the beginning of the current month,
  6268. while still calculating the running balance based on all transactions:
  6269. @smallexample @c command:validate
  6270. $ ledger -d "d>[this month]" register checking
  6271. @end smallexample
  6272. The advantage of this command's complexity is that it prints the
  6273. running total in terms of all transactions in the register. The
  6274. following, simpler command is similar, but totals only the displayed
  6275. postings:
  6276. @smallexample @c command:validate
  6277. $ ledger -b "this month" register checking
  6278. @end smallexample
  6279. @menu
  6280. * Variables::
  6281. * Functions::
  6282. * Operators::
  6283. * Complex expressions::
  6284. @end menu
  6285. @node Variables, Functions, Value Expressions, Value Expressions
  6286. @section Variables
  6287. @findex --amount @var{EXPR}
  6288. @findex --total @var{VEXPR}
  6289. Below are the one letter variables available in any value expression.
  6290. For the @command{register} and @command{print} commands, these variables
  6291. relate to individual postings, and sometimes the account affected by a
  6292. posting. For the @command{balance} command, these variables relate to
  6293. accounts, often with a subtle difference in meaning. The use of each
  6294. variable for both is specified.
  6295. @table @code
  6296. @item t
  6297. This maps to whatever the user specified with @option{--amount
  6298. @var{EXPR} (-t)}. In a @command{register} report, @option{--amount
  6299. @var{EXPR} (-t)} changes the value column; in a @command{balance}
  6300. report, it has no meaning by default. If @option{--amount @var{EXPR}
  6301. (-t)} was not specified, the current report style's value expression is
  6302. used.
  6303. @item T
  6304. This maps to whatever the user specified with @option{--total
  6305. @var{VEXPR} (-T)}. In a register report, @option{--total @var{VEXPR}
  6306. (-T)} changes the totals column; in a balance report, this is the value
  6307. given for each account. If @option{--total @var{VEXPR} (-T)} was not
  6308. specified, the current report style's value expression is used.
  6309. @item m
  6310. This is always the present moment/date.
  6311. @end table
  6312. @menu
  6313. * Posting/account details::
  6314. * Calculated totals::
  6315. @end menu
  6316. @node Posting/account details, Calculated totals, Variables, Variables
  6317. @subsection Posting/account details
  6318. @table @code
  6319. @item d
  6320. A posting's date, as the number of seconds past the epoch. This
  6321. is always ``today'' for an account.
  6322. @item a
  6323. The posting's amount; the balance of an account, without
  6324. considering children.
  6325. @item b
  6326. The cost of a posting; the cost of an account, without its
  6327. children.
  6328. @item v
  6329. The market value of a posting or an account, without its children.
  6330. @item g
  6331. The net gain (market value minus cost basis), for a posting or an
  6332. account, without its children. It is the same as @samp{v-b}.
  6333. @item l
  6334. The depth (``level'') of an account. If an account has one parent,
  6335. its depth is one.
  6336. @item n
  6337. The index of a posting, or the count of postings affecting an
  6338. account.
  6339. @item X
  6340. @samp{1} if a posting's transaction has been cleared, @samp{0} otherwise.
  6341. @item R
  6342. @samp{1} if a posting is not virtual, @samp{0} otherwise.
  6343. @item Z
  6344. @samp{1} if a posting is not automated, @samp{0} otherwise.
  6345. @end table
  6346. @node Calculated totals, , Posting/account details, Variables
  6347. @subsection Calculated totals
  6348. @table @code
  6349. @item O
  6350. The total of all postings seen so far, or the total of an account
  6351. and all its children.
  6352. @item N
  6353. The total count of postings affecting an account and all its
  6354. children.
  6355. @end table
  6356. @node Functions, Operators, Variables, Value Expressions
  6357. @section Functions
  6358. The available one letter functions are:
  6359. @table @code
  6360. @item -
  6361. Negates the argument.
  6362. @item U
  6363. The absolute (unsigned) value of the argument.
  6364. @item S
  6365. Strips the commodity from the argument.
  6366. @item P
  6367. The present market value of the argument. The syntax @samp{P(x,d)} is
  6368. supported, which yields the market value at time @samp{d}. If no date
  6369. is given, then the current moment is used.
  6370. @end table
  6371. @node Operators, Complex expressions, Functions, Value Expressions
  6372. @section Operators
  6373. The binary and ternary operators, in order of precedence, are:
  6374. @enumerate
  6375. @item @code{* /}
  6376. @item @code{+ -}
  6377. @item @code{! < > =}
  6378. @item @code{& | ?:}
  6379. @end enumerate
  6380. @menu
  6381. * Unary Operators::
  6382. * Binary Operators::
  6383. @end menu
  6384. @node Unary Operators, Binary Operators, Operators, Operators
  6385. @subsection Unary Operators
  6386. @code{not}
  6387. @code{neg}
  6388. @node Binary Operators, , Unary Operators, Operators
  6389. @subsection Binary Operators
  6390. @code{==}
  6391. @code{<}
  6392. @code{<=}
  6393. @code{>}
  6394. @code{>=}
  6395. @code{and}
  6396. @code{or}
  6397. @code{+}
  6398. @code{-}
  6399. @code{*}
  6400. @code{/}
  6401. @code{QUERY}
  6402. @code{COLON}
  6403. @code{CONS}
  6404. @code{SEQ}
  6405. @code{DEFINE}
  6406. @code{LOOKUP}
  6407. @code{LAMBDA}
  6408. @code{CALL}
  6409. @code{MATCH}
  6410. @node Complex expressions, , Operators, Value Expressions
  6411. @section Complex expressions
  6412. More complicated expressions are possible using:
  6413. @table @code
  6414. @item "amount == COMMODITY AMOUNT"
  6415. The amount can be any kind of amount supported by ledger,
  6416. with or without a commodity. Use this for decimal values.
  6417. @item /REGEX/
  6418. @itemx account =~ /REGEX/
  6419. A regular expression that matches against an account's full name. If
  6420. a posting, this will match against the account affected by the
  6421. posting.
  6422. @item @@/REGEX/
  6423. @itemx expr payee =~ /REGEX/
  6424. A regular expression that matches against a transaction's payee name.
  6425. @item %/REGEX/
  6426. @itemx tag(REGEX)
  6427. A regular expression that matches against a transaction's tags.
  6428. @item expr date =~ /REGEX/
  6429. Useful for specifying a date in plain terms. For example, you could say
  6430. @samp{expr date =~ /2014/}.
  6431. @item expr comment =~ /REGEX/
  6432. A regular expression that matches against a posting's comment
  6433. field. This searches only a posting's field, not the transaction's note
  6434. or comment field. For example, @code{ledger reg "expr" "comment =~
  6435. /landline/"} will match:
  6436. @smallexample @c input:validate
  6437. 2014/1/29 Phone bill
  6438. Assets:Checking $50.00
  6439. Expenses:Phone $-50.00 ; landline bill
  6440. @end smallexample
  6441. but will not match:
  6442. @smallexample @c input:validate
  6443. 2014/1/29 Phone bill ; landline bill
  6444. ; landline bill
  6445. Assets:Checking $50.00
  6446. Expenses:Phone $-50.00
  6447. @end smallexample
  6448. To match the latter, use @samp{ledger reg "expr" "note =~ /landline/"}
  6449. instead.
  6450. @item expr note =~ /REGEX/
  6451. A regular expression that matches against a transaction's note field.
  6452. This searches all comments in the transaction, including comments on
  6453. individual postings. Thus, @samp{ledger reg "expr" "note =~ /landline/"}
  6454. will match both all the three examples below:
  6455. @smallexample @c input:validate
  6456. 2014/1/29 Phone bill
  6457. Assets:Checking $50.00
  6458. Expenses:Phone $-50.00 ; landline bill
  6459. @end smallexample
  6460. @smallexample @c input:validate
  6461. 2014/1/29 Phone bill ; landline bill
  6462. Assets:Checking $50.00
  6463. Expenses:Phone $-50.00
  6464. @end smallexample
  6465. @smallexample @c input:validate
  6466. 2014/1/29 Phone bill
  6467. ; landline bill
  6468. Assets:Checking $50.00
  6469. Expenses:Phone $-50.00
  6470. @end smallexample
  6471. @item (EXPR)
  6472. A sub-expression is nested in parenthesis. This can be useful passing
  6473. more complicated arguments to functions, or for overriding the natural
  6474. precedence order of operators.
  6475. @item expr base =~ /REGEX/
  6476. A regular expression that matches against an account's base name. If
  6477. a posting, this will match against the account affected by the
  6478. posting.
  6479. @item expr code =~ /REGEX/
  6480. A regular expression that matches against the transaction code (the
  6481. text that occurs between parentheses before the payee).
  6482. @end table
  6483. The @command{query} command can be used to see how Ledger interprets
  6484. your query. This can be useful if you are not getting the results you
  6485. expect (@pxref{Pre-Commands}).
  6486. @menu
  6487. * Miscellaneous::
  6488. @end menu
  6489. @node Miscellaneous, , Complex expressions, Complex expressions
  6490. @subsection Miscellaneous
  6491. The following Ledger journal data (saved as @file{expr.dat}) is used to explain the behaviour of the
  6492. functions and variables below:
  6493. @anchor{expr.dat}
  6494. @smallexample @c input:3406FC1
  6495. 2015/01/16 * (C0D3) Payee
  6496. Assets:Cash ¤ -123,45
  6497. ; Payee: PiggyBank
  6498. Expenses:Office Supplies
  6499. @end smallexample
  6500. @defun abs value
  6501. @defunx U value
  6502. Return the absolute value of the given @var{value}, e.g. @var{amount}.
  6503. @smallexample @c command:3406FC1
  6504. $ ledger -f expr.dat --format "%(account) %(abs(amount))\n" reg assets
  6505. @end smallexample
  6506. @smallexample @c output:3406FC1
  6507. Assets:Cash ¤ 123,45
  6508. @end smallexample
  6509. @end defun
  6510. @defun amount_expr
  6511. Return the calculated amount of the posting according to the @option{--amount}
  6512. option.
  6513. @end defun
  6514. @defun ansify_if value color bool
  6515. Render the given @var{expression} as a string, applying the proper ANSI escape
  6516. codes to display it in the given @var{color} if @var{bool} is true. It
  6517. typically checks the value of the option @option{--color}. Since ANSI escape
  6518. codes include non-printable character sequences, such as escape @kbd{^[}
  6519. the following example may not appear as the final result on the command-line.
  6520. @smallexample @c command:4D836EE,with_input:3406FC1
  6521. $ ledger -f expr.dat --format "%(ansify_if(account, blue, options.color))\n" reg
  6522. @end smallexample
  6523. @smallexample @c output:4D836EE
  6524. Assets:Cash
  6525. Expenses:Office Supplies
  6526. @end smallexample
  6527. @end defun
  6528. @defun ceiling value
  6529. Return the next integer of @var{value} toward @math{+}infinity.
  6530. @smallexample @c command:FF9C18C,with_input:3406FC1
  6531. $ ledger -f expr.dat --format "%(account) %(ceiling(amount))\n" reg
  6532. @end smallexample
  6533. @smallexample @c output:FF9C18C
  6534. Assets:Cash ¤ -123,00
  6535. Expenses:Office Supplies ¤ 124,00
  6536. @end smallexample
  6537. @end defun
  6538. @defvar code
  6539. Return the transaction code, the string between the parenthesis after the date.
  6540. @smallexample @c command:46FCFD3,with_input:3406FC1
  6541. $ ledger -f expr.dat --format "%(account) %(code)\n" reg assets
  6542. @end smallexample
  6543. @smallexample @c output:46FCFD3
  6544. Assets:Cash C0D3
  6545. @end smallexample
  6546. @end defvar
  6547. @defvar commodity
  6548. Return the commodity of the posting amount.
  6549. @end defvar
  6550. @smallexample @c command:2CD27D7,with_input:3406FC1
  6551. $ ledger -f expr.dat --format "%(account) %(commodity)\n" reg
  6552. @end smallexample
  6553. @smallexample @c output:2CD27D7
  6554. Assets:Cash ¤
  6555. Expenses:Office Supplies ¤
  6556. @end smallexample
  6557. @defvar date
  6558. Return the date of the posting.
  6559. @end defvar
  6560. @smallexample @c command:67EBA45,with_input:3406FC1
  6561. $ ledger -f expr.dat --format "%(date) %(account)\n" reg assets
  6562. @end smallexample
  6563. @smallexample @c output:67EBA45
  6564. 2015/01/16 Assets:Cash
  6565. @end smallexample
  6566. @defvar display_amount
  6567. @defvarx t
  6568. @value{FIXME:UNDOCUMENTED}
  6569. @end defvar
  6570. @c FIXME
  6571. @defvar display_total
  6572. @defvarx T
  6573. @value{FIXME:UNDOCUMENTED}
  6574. @end defvar
  6575. @defun floor value
  6576. Return the next integer of @var{value} toward @math{-}infinity.
  6577. @smallexample @c command:4FDC7C5,with_input:3406FC1
  6578. $ ledger -f expr.dat --format "%(account) %(floor(amount))\n" reg
  6579. @end smallexample
  6580. @smallexample @c output:4FDC7C5
  6581. Assets:Cash ¤ -124,00
  6582. Expenses:Office Supplies ¤ 123,00
  6583. @end smallexample
  6584. @end defun
  6585. @defun format string
  6586. Evaluate @var{string} as format just like the @option{--format} option.
  6587. @end defun
  6588. @defun format_date date format
  6589. Return the @var{date} as a string using @var{format}. See
  6590. @code{strftime (3)} for format string details.
  6591. @smallexample @c command:9605B13,with_input:3406FC1
  6592. $ ledger -f expr.dat --format "%(format_date(date, '%A, %B %d. %Y'))\n" reg assets
  6593. @end smallexample
  6594. @smallexample @c output:9605B13
  6595. Friday, January 16. 2015
  6596. @end smallexample
  6597. @end defun
  6598. @defun format_datetime datetime format
  6599. Return the @var{datetime} as a string using @var{format}. Refer to
  6600. @code{strftime (3)} for format string details.
  6601. @end defun
  6602. @defun get_at sequence index
  6603. Return the value in @var{sequence} at @var{index}. The first element is @var{index} 0.
  6604. @value{InternalUseOnly}
  6605. @end defun
  6606. @defun is_seq value
  6607. Return true if @var{value} is a sequence. @value{InternalUseOnly}
  6608. @end defun
  6609. @defun join value
  6610. Replace all newlines in @var{value} with @code{\n}.
  6611. @end defun
  6612. @defun justify value first_width latter_width right_justify colorize
  6613. Right or left justify the string representing @var{value}. The width
  6614. of the field in the first line is given by @var{first_width}. For
  6615. subsequent lines the width is given by @var{latter_width}. If
  6616. @var{latter_width=-1}, then @var{first_width} is used for all lines.
  6617. If @var{right_justify=true} then the field is right justified within
  6618. the width of the field. If it is @var{false}, then the field is left
  6619. justified and padded to the full width of the field. If
  6620. @var{colorize} is true, then ledger will honor color settings.
  6621. @smallexample @c command:082FB27,with_input:3406FC1
  6622. $ ledger -f expr.dat --format "»%(justify(account, 30, 30, true))«\n" reg
  6623. @end smallexample
  6624. @smallexample @c output:082FB27
  6625. » Assets:Cash«
  6626. » Expenses:Office Supplies«
  6627. @end smallexample
  6628. @end defun
  6629. @defun market value datetime
  6630. @defunx P
  6631. Return the price of @var{value} at @var{datetime}. Note that @var{datetime}
  6632. must be surrounded by brackets in order to be parsed correctly,
  6633. e.g. @code{[2012/03/23]}.
  6634. @end defun
  6635. @defun nail_down
  6636. @value{FIXME:UNDOCUMENTED}
  6637. @end defun
  6638. @defvar now
  6639. @defvarx d
  6640. @defvarx m
  6641. Return the current datetime.
  6642. @end defvar
  6643. @defvar options
  6644. A variable that allows access to the values of the given command-line options
  6645. using the long option names, e.g. to see whether @option{--daily} or @option{-D}
  6646. was given use @code{option.daily}.
  6647. @smallexample @c command:C1FC7A7,with_input:3406FC1
  6648. $ ledger -f expr.dat -X $ -D --format "%(options.daily) %(options.exchange)\n" reg assets
  6649. @end smallexample
  6650. @smallexample @c output:C1FC7A7
  6651. true $
  6652. @end smallexample
  6653. @end defvar
  6654. @defun percent value_a value_b
  6655. Return the percentage of @var{value_a} in relation to @var{value_b} (used as 100%)
  6656. @smallexample @c command:04959BF,with_input:3406FC1
  6657. $ ledger -f expr.dat --format "%(percent(amount, 200))\n" reg
  6658. @end smallexample
  6659. @smallexample @c output:04959BF
  6660. -61.73%
  6661. 61.73%
  6662. @end smallexample
  6663. @end defun
  6664. @defun print value
  6665. Print @var{value} to stdout. @value{InternalUseOnly}
  6666. @end defun
  6667. @defun quantity value
  6668. Return the quantity of @var{value} for values that have a per-unit cost.
  6669. @end defun
  6670. @defun quoted expression
  6671. Surround @var{expression} with double-quotes.
  6672. @smallexample @c command:EAD8AA7,with_input:3406FC1
  6673. $ ledger -f expr.dat --format "%(quoted(account)) %(quoted(amount))\n" reg
  6674. @end smallexample
  6675. @smallexample @c output:EAD8AA7
  6676. "Assets:Cash" "¤ -123,45"
  6677. "Expenses:Office Supplies" "¤ 123,45"
  6678. @end smallexample
  6679. @end defun
  6680. @defun round
  6681. @value{FIXME:UNDOCUMENTED}
  6682. @end defun
  6683. @defun rounded
  6684. @value{FIXME:UNDOCUMENTED}
  6685. @end defun
  6686. @defun roundto value n
  6687. Return @var{value} rounded to @var{n} digits. Does not affect formatting.
  6688. @smallexample @c command:B4DFB9F,with_input:3406FC1
  6689. $ ledger -f expr.dat --format "%(account) %(roundto(amount, 1))\n" reg
  6690. @end smallexample
  6691. @smallexample @c output:B4DFB9F
  6692. Assets:Cash ¤ -123,40
  6693. Expenses:Office Supplies ¤ 123,50
  6694. @end smallexample
  6695. @end defun
  6696. @defun scrub value
  6697. Clean @var{value} using various transformations such as @code{round}, stripping
  6698. value annotations, and more.
  6699. @end defun
  6700. @defun should_bold
  6701. Return true if expression given to @option{--bold-if} evaluates to true. @value{InternalUseOnly}
  6702. @end defun
  6703. @defun strip value
  6704. @defunx S
  6705. Strip value annotation from @var{value}.
  6706. @end defun
  6707. @defun to_amount value
  6708. Convert @var{value} to an amount. @value{InternalUseOnly}
  6709. @end defun
  6710. @defun to_balance value
  6711. Convert @var{value} to a balance. @value{InternalUseOnly}
  6712. @end defun
  6713. @defun to_boolean value
  6714. Convert @var{value} to a boolean. @value{InternalUseOnly}
  6715. @end defun
  6716. @defun to_date value
  6717. Convert @var{value} to a date. @value{InternalUseOnly}
  6718. @end defun
  6719. @defun to_datetime value
  6720. Convert @var{value} to a datetime. @value{InternalUseOnly}
  6721. @end defun
  6722. @defun to_int value
  6723. @defunx int value
  6724. Return the integer value for @var{value}.
  6725. @smallexample @c command:0B0CBA1,with_input:3406FC1
  6726. $ ledger -f expr.dat --format "%(1 + to_int('1'))\n%(2,5 + int(2,5))\n" reg assets
  6727. @end smallexample
  6728. @smallexample @c output:0B0CBA1
  6729. 2
  6730. 4.5
  6731. @end smallexample
  6732. @end defun
  6733. @defun to_mask value
  6734. Convert @var{value} to a mask. @value{InternalUseOnly}
  6735. @end defun
  6736. @defun to_sequence value
  6737. Convert @var{value} to a sequence. @value{InternalUseOnly}
  6738. @end defun
  6739. @defun to_string value
  6740. @defunx str value
  6741. Convert @var{value} to a character string.
  6742. @end defun
  6743. @defvar today
  6744. Return today's date.
  6745. @end defvar
  6746. @smallexample @c command:F2FDF4B,with_input:3406FC1
  6747. $ ledger -f expr.dat --now 2015/01/01 --format "%(today)\n" reg assets
  6748. @end smallexample
  6749. @smallexample @c output:F2FDF4B
  6750. 2015/01/01
  6751. @end smallexample
  6752. @defun top_amount
  6753. @value{FIXME:UNDOCUMENTED}
  6754. @end defun
  6755. @defun total_expr
  6756. Return the calculated total of the posting according to the @option{--total}
  6757. option.
  6758. @end defun
  6759. @defun trim value
  6760. Trim leading and trailing whitespace from @var{value}.
  6761. @smallexample @c command:377BBAB,with_input:3406FC1
  6762. $ ledger -f expr.dat --format "»%(trim(' Trimmed '))«\n" reg assets
  6763. @end smallexample
  6764. @smallexample @c output:377BBAB
  6765. »Trimmed«
  6766. @end smallexample
  6767. @end defun
  6768. @defun truncatedstring total_len account_len
  6769. Truncate @var{string} to @var{total_len} ensuring that each account is at least
  6770. @var{account_len} long.
  6771. @end defun
  6772. @defun unround
  6773. @value{FIXME:UNDOCUMENTED}
  6774. @end defun
  6775. @defun unrounded
  6776. @value{FIXME:UNDOCUMENTED}
  6777. @end defun
  6778. @defun value_date
  6779. @value{FIXME:UNDOCUMENTED}
  6780. @end defun
  6781. @node Format Strings, Extending with Python, Value Expressions, Top
  6782. @chapter Format Strings
  6783. @menu
  6784. * Format String Basics::
  6785. * Format String Structure::
  6786. * Format Expressions::
  6787. * Balance format::
  6788. * Formatting Functions and Codes::
  6789. @end menu
  6790. @node Format String Basics, Format String Structure, Format Strings, Format Strings
  6791. @section Format String Basics
  6792. @findex --format @var{FORMAT_STRING}
  6793. @findex --balance-format @var{FORMAT_STRING}
  6794. @findex --budget-format @var{FORMAT_STRING}
  6795. @findex --cleared-format @var{FORMAT_STRING}
  6796. @findex --csv-format @var{FORMAT_STRING}
  6797. @findex --plot-amount-format @var{FORMAT_STRING}
  6798. @findex --plot-total-format @var{FORMAT_STRING}
  6799. @findex --pricedb-format @var{FORMAT_STRING}
  6800. @findex --prices-format @var{FORMAT_STRING}
  6801. @findex --register-format @var{FORMAT_STRING}
  6802. Format strings may be used to change the output format of reports. They
  6803. are specified by passing a formatting string to the @option{--format
  6804. @var{FORMAT_STRING} (-F)} option. Within that string, constructs are
  6805. allowed which make it possible to display the various parts of an
  6806. account or posting in custom ways.
  6807. There are several additional flags that allow you to define formats
  6808. for specific reports. These are useful to define in your configuration
  6809. file and will allow you to run ledger reports from the command-line
  6810. without having to enter a new format for each command.
  6811. @itemize
  6812. @item @option{--balance-format @var{FORMAT_STRING}}
  6813. @item @option{--budget-format @var{FORMAT_STRING}}
  6814. @item @option{--cleared-format @var{FORMAT_STRING}}
  6815. @item @option{--csv-format @var{FORMAT_STRING}}
  6816. @item @option{--plot-amount-format @var{FORMAT_STRING}}
  6817. @item @option{--plot-total-format @var{FORMAT_STRING}}
  6818. @item @option{--pricedb-format @var{FORMAT_STRING}}
  6819. @item @option{--prices-format @var{FORMAT_STRING}}
  6820. @item @option{--register-format @var{FORMAT_STRING}}
  6821. @end itemize
  6822. @node Format String Structure, Format Expressions, Format String Basics, Format Strings
  6823. @section Format String Structure
  6824. Within a format string, a substitution is specified using a percent
  6825. @samp{%} character. The basic format of all substitutions is:
  6826. @smallexample
  6827. %[-][MIN WIDTH][.MAX WIDTH](VALEXPR)
  6828. @end smallexample
  6829. If the optional minus sign @samp{-} follows the percent character
  6830. @samp{%}, whatever is substituted will be left justified. The default
  6831. is right justified. If a minimum width is given next, the substituted
  6832. text will be at least that wide, perhaps wider. If a period and
  6833. a maximum width is given, the substituted text will never be wider
  6834. than this, and will be truncated to fit. Here are some examples:
  6835. @table @code
  6836. @item %-20P
  6837. A transaction's payee, left justified and padded to 20 characters wide.
  6838. @item %20P
  6839. The same, right justified, at least 20 chars wide.
  6840. @item %.20P
  6841. The same, no more than 20 chars wide.
  6842. @end table
  6843. The expression following the format constraints can be a single letter,
  6844. or an expression enclosed in parentheses or brackets.
  6845. @node Format Expressions, Balance format, Format String Structure, Format Strings
  6846. @section Format Expressions
  6847. @findex --amount @var{EXPR}
  6848. @findex --total @var{VEXPR}
  6849. For demonstration purposes the journal data from @ref{expr.dat} is used.
  6850. The allowable expressions are:
  6851. @table @code
  6852. @item %
  6853. Inserts a percent sign.
  6854. @smallexample @c command:6F90EFC,with_input:3406FC1
  6855. $ ledger -f expr.dat --format "%%\n" reg assets
  6856. @end smallexample
  6857. @smallexample @c output:6F90EFC
  6858. %
  6859. @end smallexample
  6860. @item t
  6861. Inserts the results of the value expression specified by
  6862. @option{--amount @var{EXPR} (-t)}. If @option{--amount @var{EXPR} (-t)}
  6863. was not specified, the current report style's value expression is used.
  6864. @item T
  6865. Inserts the results of the value expression specified by @option{--total
  6866. @var{VEXPR} (-T)}. If @option{--total @var{VEXPR} (-T)} was not
  6867. specified, the current report style's value expression is used.
  6868. @item (EXPR)
  6869. Inserts the amount resulting from the value expression given in
  6870. parentheses. To insert five times the total value of an account, for
  6871. example, one could say @samp{%12(5*O)}. Note: It's important to put the
  6872. five first in that expression, so that the commodity doesn't get
  6873. stripped from the total.
  6874. @smallexample @c command:494256E,with_input:3406FC1
  6875. $ ledger -f expr.dat --format "%12(5*O)\n" reg assets
  6876. @end smallexample
  6877. @smallexample @c output:494256E
  6878. ¤ -617,25
  6879. @end smallexample
  6880. @item [DATEFMT]
  6881. Inserts the result of formatting a posting's date with a date format
  6882. string, exactly like those supported by @code{strftime (3)}. For
  6883. example: @samp{%[%Y/%m/%d %H:%M:%S]}.
  6884. @item S
  6885. Insert the path name of the file from which the transaction's data was
  6886. read. Only sensible in a @command{register} report.
  6887. @c Note: Unable to test this properly since the output depends on
  6888. @c where the ledger source tree resides in the filesystem.
  6889. @smallexample
  6890. $ ledger -f ~/journal.dat --format "%S\n" reg assets
  6891. @end smallexample
  6892. @smallexample
  6893. /home/jwiegley/journal.dat
  6894. @end smallexample
  6895. @item B
  6896. Inserts the beginning character position of that transaction within the
  6897. file.
  6898. @smallexample @c command:2B669C9,with_input:3406FC1
  6899. $ ledger -f expr.dat --format "%B\n" reg assets
  6900. @end smallexample
  6901. @smallexample @c output:2B669C9
  6902. 26
  6903. @end smallexample
  6904. @item b
  6905. Inserts the beginning line of that transaction within the file.
  6906. @smallexample @c command:F6E356F,with_input:3406FC1
  6907. $ ledger -f expr.dat --format "%b\n" reg assets
  6908. @end smallexample
  6909. @smallexample @c output:F6E356F
  6910. 2
  6911. @end smallexample
  6912. @item E
  6913. Inserts the ending character position of that transaction within the
  6914. file.
  6915. @smallexample @c command:0E55246,with_input:3406FC1
  6916. $ ledger -f expr.dat --format "%E\n" reg assets
  6917. @end smallexample
  6918. @smallexample @c output:0E55246
  6919. 90
  6920. @end smallexample
  6921. @item e
  6922. Inserts the ending line of that transaction within the file.
  6923. @smallexample @c command:A26F4C0,with_input:3406FC1
  6924. $ ledger -f expr.dat --format "%e\n" reg assets
  6925. @end smallexample
  6926. @smallexample @c output:A26F4C0
  6927. 3
  6928. @end smallexample
  6929. @item D
  6930. Returns the date according to the default format.
  6931. @item d
  6932. Returns the date according to the default format. If the transaction
  6933. has an effective date, it prints @code{ACTUAL_DATE=EFFECTIVE_DATE}.
  6934. @item X
  6935. If a posting has been cleared, this returns a 1, otherwise returns 0.
  6936. @item Y
  6937. This is the same as @samp{%X}, except that it only displays a state
  6938. character if all of the member postings have the same state.
  6939. @item C
  6940. Inserts the transaction code. This is the value specified between
  6941. parentheses on the first line of the transaction.
  6942. @smallexample @c command:C1CAAF3,with_input:3406FC1
  6943. $ ledger -f expr.dat --format "%C\n" reg assets
  6944. @end smallexample
  6945. @c Note: The output needs a space character at the end
  6946. @c for this test to pass
  6947. @smallexample @c output:C1CAAF3
  6948. (C0D3)
  6949. @end smallexample
  6950. @item P
  6951. Inserts the payee related to a posting.
  6952. @smallexample @c command:F41A9BB,with_input:3406FC1
  6953. $ ledger -f expr.dat --format "%P\n" reg assets
  6954. @end smallexample
  6955. @smallexample @c output:F41A9BB
  6956. PiggyBank
  6957. @end smallexample
  6958. @c @item a
  6959. @c Inserts the optimal short name for an account. This is normally
  6960. @c used in balance reports. It prints a parent account's name if that
  6961. @c name has not been printed yet, otherwise it just prints the
  6962. @c account's name.
  6963. @item A
  6964. Inserts the full name of an account.
  6965. @smallexample @c command:29A70DD,with_input:3406FC1
  6966. $ ledger -f expr.dat --format "%A\n" reg
  6967. @end smallexample
  6968. @smallexample @c output:29A70DD
  6969. Assets:Cash
  6970. Expenses:Office Supplies
  6971. @end smallexample
  6972. @c @item W
  6973. @c This is the same as @code{%A}, except that it first displays the
  6974. @c posting's state @emph{if the transaction's posting states are not
  6975. @c all the same}, followed by the full account name. This is offered
  6976. @c as a printing optimization, so that combined with @code{%Y}, only
  6977. @c the minimum amount of state detail is printed.
  6978. @c @item o
  6979. @c Inserts the ``optimized'' form of a posting's amount. This is used
  6980. @c by the print report. In some cases, this inserts nothing; in
  6981. @c others, it inserts the posting amount and its cost. It's use is
  6982. @c not recommended unless you are modifying the print report.
  6983. @item N
  6984. Inserts the note associated with a posting, if one exists.
  6985. @smallexample @c command:E6DC93A,with_input:3406FC1
  6986. $ ledger -f expr.dat --format "%N\n" reg assets
  6987. @end smallexample
  6988. @smallexample @c output:E6DC93A
  6989. Payee: PiggyBank
  6990. @end smallexample
  6991. @item /
  6992. The @samp{%/} construct is special. It separates a format string
  6993. between what is printed for the first posting of a transaction, and
  6994. what is printed for all subsequent postings. If not used, the
  6995. same format string is used for all postings.
  6996. @smallexample @c command:E80897D,with_input:3406FC1
  6997. $ ledger -f expr.dat --format "%P\n%/%A\n" reg
  6998. @end smallexample
  6999. @smallexample @c output:E80897D
  7000. PiggyBank
  7001. Expenses:Office Supplies
  7002. @end smallexample
  7003. @end table
  7004. @node Balance format, Formatting Functions and Codes, Format Expressions, Format Strings
  7005. @section Balance format
  7006. @findex --balance-format @var{FORMAT_STRING}
  7007. @findex --format @var{FORMAT_STRING}
  7008. As an example of how flexible the @option{--format @var{FORMAT_STRING}}
  7009. strings can be, the default balance format looks like this (the various
  7010. functions are described later):
  7011. @smallexample
  7012. "%(justify(scrub(display_total), 20, -1, true, color))"
  7013. " %(!options.flat ? depth_spacer : \"\")"
  7014. "%-(ansify_if(partial_account(options.flat), blue if color))\n%/"
  7015. "%$1\n%/"
  7016. "--------------------\n"
  7017. @end smallexample
  7018. @node Formatting Functions and Codes, , Balance format, Format Strings
  7019. @section Formatting Functions and Codes
  7020. @menu
  7021. * Field Widths::
  7022. * Colors::
  7023. * Quantities and Calculations::
  7024. * Date Functions::
  7025. * Date and Time Format Codes::
  7026. * Text Formatting::
  7027. * Data File Parsing Information::
  7028. @end menu
  7029. @node Field Widths, Colors, Formatting Functions and Codes, Formatting Functions and Codes
  7030. @subsection Field Widths
  7031. The following codes return the width allocated for the specific fields.
  7032. The defaults can be changed using the corresponding command-line
  7033. options:
  7034. @itemize
  7035. @item @code{date_width}
  7036. @item @code{payee_width}
  7037. @item @code{account_width}
  7038. @item @code{amount_width}
  7039. @item @code{total_width}
  7040. @end itemize
  7041. @node Colors, Quantities and Calculations, Field Widths, Formatting Functions and Codes
  7042. @subsection Colors
  7043. The character-based formatting ledger can do is limited to the ANSI
  7044. terminal character colors and font highlights in a normal TTY session.
  7045. @multitable @columnfractions .3 .3 .3
  7046. @item @code{red} @tab @code{magenta} @tab @code{bold}
  7047. @item @code{green} @tab @code{cyan} @tab @code{underline}
  7048. @item @code{yellow} @tab @code{white} @tab @code{blink}
  7049. @item @code{blue} @tab @code{black}
  7050. @end multitable
  7051. @node Quantities and Calculations, Date Functions, Colors, Formatting Functions and Codes
  7052. @subsection Quantities and Calculations
  7053. @table @code
  7054. @item amount_expr
  7055. @item abs
  7056. @item commodity
  7057. @item display_amount
  7058. @item display_total
  7059. @item floor
  7060. @item get_at
  7061. @item is_seq
  7062. @item market
  7063. @item percent
  7064. @item price
  7065. @item quantity
  7066. @item rounded
  7067. @item truncated
  7068. @item total_expr
  7069. @item top_amount
  7070. @item to_boolean
  7071. @item to_int
  7072. @item to_amount
  7073. @item to_balance
  7074. @item unrounded
  7075. @end table
  7076. @node Date Functions, Date and Time Format Codes, Quantities and Calculations, Formatting Functions and Codes
  7077. @subsection Date Functions
  7078. @findex --now @var{DATE}
  7079. The following functions allow you to manipulate and format dates.
  7080. @table @code
  7081. @item date
  7082. Return the date of the current transaction.
  7083. @item format_date(date, "FORMAT_STRING")
  7084. Format the date using the given format string.
  7085. @item now
  7086. Return the current date and time. If the @option{--now @var{DATE}}
  7087. option is defined it will return that value.
  7088. @item today
  7089. Return the current date. If the @option{--now @var{DATE}} option is
  7090. defined it will return that value.
  7091. @item to_datetime
  7092. Convert a string to a date-time value.
  7093. @item to_date
  7094. Convert a string to date value.
  7095. @item value_date
  7096. @end table
  7097. @menu
  7098. * Date and Time Format Codes::
  7099. @end menu
  7100. @node Date and Time Format Codes, Text Formatting, Date Functions, Formatting Functions and Codes
  7101. @subsection Date and Time Format Codes
  7102. Date and time format are specified as strings of single letter codes
  7103. preceded by percent signs. Any separator, or no separator can be
  7104. specified.
  7105. @menu
  7106. * Days::
  7107. * Weekdays::
  7108. * Month::
  7109. * Miscellaneous Date Codes::
  7110. @end menu
  7111. @node Days, Weekdays, Date and Time Format Codes, Date and Time Format Codes
  7112. @subsubsection Days
  7113. Dates are formed from a combination of day, month and year codes, in
  7114. whatever order you prefer:
  7115. @table @code
  7116. @item %Y
  7117. Four digit year.
  7118. @item %y
  7119. Two digit year.
  7120. @item %m
  7121. Two digit month.
  7122. @item %d
  7123. Two digit date.
  7124. @end table
  7125. @noindent
  7126. So @code{"%Y%m%d"} yields @samp{20111214} which provides a date that
  7127. is simple to sort on.
  7128. @node Weekdays, Month, Days, Date and Time Format Codes
  7129. @subsubsection Weekdays
  7130. You can have additional weekday information in your date with @samp{%A}
  7131. as
  7132. @table @code
  7133. @item %m-%d-%Y %A
  7134. yields @samp{02-10-2010 Wednesday}.
  7135. @item %A %m-%d-%Y
  7136. yields @samp{Wednesday 02-10-2010}.
  7137. @end table
  7138. @noindent
  7139. These are options you can select for weekday
  7140. @table @code
  7141. @item %a
  7142. weekday, abbreviated Wed.
  7143. @item %A
  7144. weekday, full Wednesday.
  7145. @item %d
  7146. day of the month (dd), zero padded up to 10.
  7147. @item %e
  7148. day of the month (dd), no leading zero up to 10.
  7149. @item %j
  7150. day of year, zero padded 000–366.
  7151. @item %u
  7152. day of week starting with Monday (1), i.e. @code{mtwtfss} 3.
  7153. @item %w
  7154. day of week starting with Sunday (0), i.e. @code{smtwtfs} 3.
  7155. @end table
  7156. @node Month, Miscellaneous Date Codes, Weekdays, Date and Time Format Codes
  7157. @subsubsection Month
  7158. You can have additional month information in your date with @samp{%B}
  7159. as
  7160. @table @code
  7161. @item %m-%d-%Y %B
  7162. yields @samp{02-10-2010 February}.
  7163. @item %B %m-%d-%Y
  7164. yields @samp{February 02-10-2010}.
  7165. @end table
  7166. @noindent
  7167. These are options you can select for month
  7168. @table @code
  7169. @item %m
  7170. @samp{mm} month as two digits.
  7171. @item %b
  7172. Locale’s abbreviated month, for example @samp{02} might be abbreviated
  7173. as @samp{Feb}.
  7174. @item %B
  7175. Locale’s full month, variable length, e.g. February.
  7176. @end table
  7177. @node Miscellaneous Date Codes, , Month, Date and Time Format Codes
  7178. @subsubsection Miscellaneous Date Codes
  7179. Additional date format parameters which can be used:
  7180. @table @code
  7181. @item %U
  7182. week number Sunday as first day of week, ranging 01–53.
  7183. @item %W
  7184. week number Monday as first day of week, ranging 01–53.
  7185. @item %V
  7186. week of the year, ranging 01–53.
  7187. @item %C
  7188. century, ranging 00–99.
  7189. @item %D
  7190. yields @code{%m/%d/%y} as in @samp{02/10/10}.
  7191. @item %x
  7192. locale’s date representation, as @samp{02/10/2010} for the U.S.
  7193. @item %F
  7194. yields @code{%Y-%m-%d} as in @samp{2010-02-10}.
  7195. @end table
  7196. @node Text Formatting, Data File Parsing Information, Date and Time Format Codes, Formatting Functions and Codes
  7197. @subsection Text Formatting
  7198. The following format functions allow you limited formatting of text:
  7199. @table @code
  7200. @item ansify_if(value, color)
  7201. Surrounds the string representing value with ANSI codes to give it
  7202. @code{color} on an TTY display. Has no effect if directed to a file.
  7203. @item justify(value, first_width, latter_width, right_justify, colorize)
  7204. Right or left justify the string representing @code{value}. The width
  7205. of the field in the first line is given by @code{first_width}. For
  7206. subsequent lines the width is given by @code{latter_width}. If
  7207. @code{latter_width=-1}, then @code{first_width} is use for all lines.
  7208. If @code{right_justify=true} then the field is right justify within
  7209. the width of the field. If it is @code{false}, then the field is left
  7210. justified and padded to the full width of the field. If
  7211. @code{colorize} is true, then ledger will honor color settings.
  7212. @item join(STR)
  7213. Replaces line feeds in @code{STR} with @samp{\n}.
  7214. @item quoted(STR)
  7215. Return @code{STR} surrounded by double quotes, @samp{"STR"}.
  7216. @item strip(value)
  7217. Values can have numerous annotations, such as effective dates and lot
  7218. prices. @code{strip} removes these annotations.
  7219. @end table
  7220. @node Data File Parsing Information, , Text Formatting, Formatting Functions and Codes
  7221. @subsection Data File Parsing Information
  7222. The following format strings provide locational metadata
  7223. regarding the coordinates of entries in the source data file(s) that
  7224. generated the posting.
  7225. @table @code
  7226. @item filename
  7227. the name of the ledger data file from whence the posting came,
  7228. abbreviated @samp{S}.
  7229. @item beg_pos
  7230. character position in @code{filename} where entry for posting begins,
  7231. abbreviated @samp{B}.
  7232. @item end_pos
  7233. character position in @code{filename} where entry for posting ends,
  7234. abbreviated @samp{E}.
  7235. @item beg_line
  7236. line number in @code{filename} where entry for posting begins,
  7237. abbreviated @samp{b}.
  7238. @item end_line
  7239. line number in @code{filename} where entry for posting ends,
  7240. abbreviated @samp{e}.
  7241. @end table
  7242. @node Extending with Python, Ledger for Developers, Format Strings, Top
  7243. @chapter Extending with Python
  7244. Python can be used to extend your Ledger experience. But first,
  7245. a word must be said about Ledger's data model, so that other things
  7246. make sense later.
  7247. @menu
  7248. * Basic data traversal::
  7249. * Raw versus Cooked::
  7250. * Queries::
  7251. * Embedded Python::
  7252. * Amounts::
  7253. @end menu
  7254. @node Basic data traversal, Raw versus Cooked, Extending with Python, Extending with Python
  7255. @section Basic data traversal
  7256. Every interaction with Ledger happens in the context of a Session.
  7257. Even if you don't create a session manually, one is created for you by
  7258. the top-level interface functions. The Session is where objects live
  7259. like the Commodities that Amounts refer to.
  7260. The make a Session useful, you must read a Journal into it, using the
  7261. function `@code{read_journal}`. This reads Ledger data from the given
  7262. file, populates a Journal object within the current Session, and
  7263. returns a reference to that Journal object.
  7264. Within the Journal live all the Transactions, Postings, and other
  7265. objects related to your data. There are also AutomatedTransactions
  7266. and PeriodicTransactions, etc.
  7267. Here is how you would traverse all the postings in your data file:
  7268. @smallexample
  7269. import ledger
  7270. for xact in ledger.read_journal("sample.dat").xacts():
  7271. for post in xact.posts():
  7272. print "Transferring %s to/from %s" % (post.amount, post.account)
  7273. @end smallexample
  7274. @node Raw versus Cooked, Queries, Basic data traversal, Extending with Python
  7275. @section Raw versus Cooked
  7276. Ledger data exists in one of two forms: raw and cooked. Raw objects are
  7277. what you get from a traversal like the above, and represent exactly what
  7278. was seen in the data file. Consider this journal:
  7279. @smallexample @c input:validate
  7280. = true
  7281. (Assets:Cash) $100
  7282. 2012-03-01 KFC
  7283. Expenses:Food $100
  7284. Assets:Credit
  7285. @end smallexample
  7286. In this case, the @emph{raw} regular transaction in this file is:
  7287. @smallexample @c input:validate
  7288. 2012-03-01 KFC
  7289. Expenses:Food $100
  7290. Assets:Credit
  7291. @end smallexample
  7292. While the @emph{cooked} form is:
  7293. @smallexample @c input:validate
  7294. 2012-03-01 KFC
  7295. Expenses:Food $100
  7296. Assets:Credit $-100
  7297. (Assets:Cash) $100
  7298. @end smallexample
  7299. So the easy way to think about raw vs. cooked is that raw is the
  7300. unprocessed data, and cooked has had all considerations applied.
  7301. When you traverse a Journal by iterating over its transactions, you are
  7302. generally looking at raw data. In order to look at cooked data, you
  7303. must generate a report of some kind by querying the journal:
  7304. @smallexample
  7305. for post in ledger.read_journal("sample.dat").query("food"):
  7306. print "Transferring %s to/from %s" % (post.amount, post.account)
  7307. @end smallexample
  7308. The reason why queries iterate over postings instead of transactions is
  7309. that queries often return only a ``slice'' of the transactions they
  7310. apply to. You can always get at a matching posting's transaction by
  7311. looking at its @code{xact} member:
  7312. @smallexample
  7313. last_xact = None
  7314. for post in ledger.read_journal("sample.dat").query(""):
  7315. if post.xact != last_xact:
  7316. for post in post.xact.posts:
  7317. print "Transferring %s to/from %s" % (post.amount,
  7318. post.account)
  7319. last_xact = post.xact
  7320. @end smallexample
  7321. This query ends up reporting every cooked posting in the Journal, but
  7322. does it transaction-wise. It relies on the fact that an unsorted report
  7323. returns postings in the exact order they were parsed from the journal
  7324. file.
  7325. @node Queries, Embedded Python, Raw versus Cooked, Extending with Python
  7326. @section Queries
  7327. The Journal.query() method accepts every argument you can specify on the
  7328. command-line, including @option{--options}.
  7329. Since a query ``cooks'' the journal it applies to, only one query may be
  7330. active for that journal at a given time. Once the query object is gone
  7331. (after the for loop), then the data reverts back to its raw state.
  7332. @node Embedded Python, Amounts, Queries, Extending with Python
  7333. @section Embedded Python
  7334. You can embed Python into your data files using the 'python' directive:
  7335. @smallexample
  7336. python
  7337. import os
  7338. def check_path(path_value):
  7339. print "%s => %s" % (str(path_value), os.path.isfile(str(path_value)))
  7340. return os.path.isfile(str(path_value))
  7341. tag PATH
  7342. assert check_path(value)
  7343. 2012-02-29 KFC
  7344. ; PATH: somebogusfile.dat
  7345. Expenses:Food $20
  7346. Assets:Cash
  7347. @end smallexample
  7348. Any Python functions you define this way become immediately available as
  7349. valexpr functions.
  7350. @node Amounts, , Embedded Python, Extending with Python
  7351. @section Amounts
  7352. When numbers come from Ledger, like post.amount, the type of the value
  7353. is Amount. It can be used just like an ordinary number, except that
  7354. addition and subtraction are restricted to amounts with the same
  7355. commodity. If you need to create sums of multiple commodities, use
  7356. a Balance. For example:
  7357. @smallexample
  7358. total = Balance()
  7359. for post in ledger.read_journal("sample.dat").query(""):
  7360. total += post.amount
  7361. print total
  7362. @end smallexample
  7363. @node Ledger for Developers, Major Changes from version 2.6, Extending with Python, Top
  7364. @chapter Ledger for Developers
  7365. @menu
  7366. * Internal Design::
  7367. * Journal File Format for Developers::
  7368. * Developer Commands::
  7369. * Ledger Development Environment::
  7370. @end menu
  7371. @node Internal Design, Journal File Format for Developers, Ledger for Developers, Ledger for Developers
  7372. @section Internal Design
  7373. Ledger is developed as a tiered set of functionality, where lower tiers
  7374. know nothing about the higher tiers. In fact, multiple libraries are
  7375. built during the development the process, and link unit tests to these
  7376. libraries, so that it is a link error for a lower tier to violate this
  7377. modularity.
  7378. Those tiers are:
  7379. @itemize
  7380. @item Utility code
  7381. There's lots of general utility in Ledger for doing time parsing, using
  7382. Boost.Regex, error handling, etc. It's all done in a way that can be
  7383. reused in other projects as needed.
  7384. @item Commoditized Amounts (amount_t, commodity_t and friends)
  7385. A numerical abstraction combining multi-precision rational numbers (via
  7386. GMP) with commodities. These structures can be manipulated like regular
  7387. numbers in either C++ or Python (as Amount objects).
  7388. @item Commodity Pool
  7389. Commodities are all owned by a commodity pool, so that future parsing of
  7390. amounts can link to the same commodity and established a consistent
  7391. price history and record of formatting details.
  7392. @item Balances
  7393. Adds the concept of multiple amounts with varying commodities. Supports
  7394. simple arithmetic, and multiplication and division with non-commoditized
  7395. values.
  7396. @item Price history
  7397. Amounts have prices, and these are kept in a data graph which the amount
  7398. code itself is only dimly aware of (there's three points of access so an
  7399. amount can query its revalued price on a given date).
  7400. @item Values
  7401. Often the higher layers in Ledger don't care if something is an amount
  7402. or a balance, they just want to add stuff to it or print it. For this,
  7403. I created a type-erasure class, value_t/Value, into which many things
  7404. can be stuffed and then operated on. They can contain amounts,
  7405. balances, dates, strings, etc. If you try to apply an operation between
  7406. two values that makes no sense (like dividing an amount by a balance),
  7407. an error occurs at runtime, rather than at compile-time (as would happen
  7408. if you actually tried to divide an @code{amount_t} by
  7409. a @code{balance_t}).
  7410. This is the core data type for the value expression language.
  7411. @item Value expressions
  7412. The next layer up adds functions and operators around the Value concept.
  7413. This lets you apply transformations and tests to Values at runtime
  7414. without having to bake it into C++. The set of functions available is
  7415. defined by each object type in Ledger (posts, accounts, transactions,
  7416. etc.), though the core engine knows nothing about these. At its base,
  7417. it only knows how to apply operators to values, and how to pass them to
  7418. and receive them from functions.
  7419. @item Query expressions
  7420. Expressions can be onerous to type at the command-line, so there's
  7421. a shorthand for reporting called ``query expressions''. These add no
  7422. functionality of their own, but are purely translated from the input
  7423. string down to the corresponding value expression, for example the
  7424. input string @samp{cash} is translated to @samp{(account
  7425. =~ /cash/)}. This is a convenience layer.
  7426. @item Format strings
  7427. Format strings let you interpolate value expressions into strings, with
  7428. the requirement that any interpolated value have a string
  7429. representation. Really all this does is calculate the value expression
  7430. in the current report context, call the resulting value's
  7431. @code{to_string()} method, and stuffs the result into the output string.
  7432. It also provides printf-like behavior, such as min/max width, right/left
  7433. justification, etc.
  7434. @item Journal items
  7435. Next is a base type shared by anything that can appear in a journal: an
  7436. item_t. It contains details common to all such parsed entities, like
  7437. what file and line it was found on, etc.
  7438. @item Journal posts
  7439. The most numerous object found in a Journal, postings are a type of item
  7440. that contain an account, an amount, a cost, and metadata. There are
  7441. some other complications, like the account can be marked virtual, the
  7442. amount could be an expression, etc.
  7443. @item Journal transactions
  7444. Postings are owned by transactions, always. This subclass of @code{item_t}
  7445. knows about the date, the payee, etc. If a date or metadata tag is
  7446. requested from a posting and it doesn't have that information, the
  7447. transaction is queried to see if it can provide it.
  7448. @item Journal accounts
  7449. Postings are also shared by accounts, though the actual memory is
  7450. managed by the transaction. Each account knows all the postings within
  7451. it, but contains relatively little information of its own.
  7452. @item The Journal object
  7453. Finally, all transactions with their postings, and all accounts, are
  7454. owned by a @code{journal_t} object. This is the go-to object for
  7455. querying and reporting on your data.
  7456. @item Textual journal parser
  7457. There is a textual parser, wholly contained in @file{textual.cc}, which
  7458. knows how to parse text into journal objects, which then get
  7459. ``finalized'' and added to the journal. Finalization is the step that
  7460. enforces the double-entry guarantee.
  7461. @item Iterators
  7462. Every journal object is ``iterable'', and these iterators are defined in
  7463. @file{iterators.h} and @file{iterators.cc}. This iteration logic is kept out of the
  7464. basic journal objects themselves for the sake of modularity.
  7465. @item Comparators
  7466. Another abstraction isolated to its own layer, this class encapsulating
  7467. the comparison of journal objects, based on whatever value expression
  7468. the user passed to @option{--sort @var{VEXPR}}.
  7469. @item Temporaries
  7470. Many reports bring pseudo-journal objects into existence, like postings
  7471. which report totals in a @samp{Total} account. These objects are
  7472. created and managed by a @code{temporaries_t} object, which gets used in
  7473. many places by the reporting filters.
  7474. @item Option handling
  7475. There is an option handling subsystem used by many of the layers further
  7476. down. It makes it relatively easy for me to add new options, and to
  7477. have those option settings immediately accessible to value expressions.
  7478. @item Session objects
  7479. Every journal object is owned by a session, with the session providing
  7480. support for that object. In GUI terms, this is the Controller object
  7481. for the journal Data object, where every document window would be
  7482. a separate session. They are all owned by the global scope.
  7483. @item Report objects
  7484. Every time you create any report output, a report object is created to
  7485. determine what you want to see. In the Ledger REPL, a new report object
  7486. is created every time a command is executed. In CLI mode, only one
  7487. report object ever comes into being, as Ledger immediately exits after
  7488. displaying the results.
  7489. @item Reporting filters
  7490. The way Ledger generates data is this: it asks the session for the
  7491. current journal, and then creates an iterator applied to that journal.
  7492. The kind of iterator depends on the type of report.
  7493. This iterator is then walked, and every object yielded from the iterator
  7494. is passed to an ``item handler'', whose type is directly related to the
  7495. type of the iterator.
  7496. There are many, many item handlers, which can be chained together. Each
  7497. one receives an item (post, account, xact, etc.), performs some action
  7498. on it, and then passes it down to the next handler in the chain. There
  7499. are filters which compute the running totals; that queue and sort all
  7500. the input items before playing them back out in a new order; that filter
  7501. out items which fail to match a predicate, etc. Almost every reporting
  7502. feature in Ledger is related to one or more filters. Looking at
  7503. @file{filters.h}, there are over 25 of them defined currently.
  7504. @item The filter chain
  7505. How filters get wired up, and in what order, is a complex process based
  7506. on all the various options specified by the user. This is the job of
  7507. the chain logic, found entirely in @file{chain.cc}. It took a really
  7508. long time to get this logic exactly right, which is why I haven't
  7509. exposed this layer to the Python bridge yet.
  7510. @item Output modules
  7511. Although filters are great and all, in the end you want to see stuff.
  7512. This is the job of special ``leaf'' filters called output modules. They
  7513. are implemented just like a regular filter, but they don't have
  7514. a ``next'' filter to pass the data on down to. Instead, they are the
  7515. end of the line and must do something with the item that results in the
  7516. user seeing something on their screen or in a file.
  7517. @item Select queries
  7518. Select queries know a lot about everything, even though they implement
  7519. their logic by implementing the user's query in terms of all the other
  7520. features thus presented. Select queries have no functionality of their
  7521. own, they are simple a shorthand to provide access to much of Ledger's
  7522. functionality via a cleaner, more consistent syntax.
  7523. @item The Global Scope
  7524. There is a master object which owns every other objects, and this is
  7525. Ledger's global scope. It creates the other objects, provides REPL
  7526. behavior for the command-line utility, etc. In GUI terms, this is the
  7527. Application object.
  7528. @item The Main Driver
  7529. This creates the global scope object, performs error reporting, and
  7530. handles command-line options which must precede even the creation of the
  7531. global scope, such as @option{--debug @var{CODE}}.
  7532. @end itemize
  7533. And that's Ledger in a nutshell. All the rest are details, such as
  7534. which value expressions each journal item exposes, how many filters
  7535. currently exist, which options the report and session scopes define,
  7536. etc.
  7537. @node Journal File Format for Developers, Developer Commands, Internal Design, Ledger for Developers
  7538. @section Journal File Format for Developers
  7539. This chapter offers a complete description of the journal data format,
  7540. suitable for implementers in other languages to follow. For users, the
  7541. chapter on keeping a journal is less extensive, but more typical of
  7542. common usage (@pxref{Keeping a Journal}).
  7543. Data is collected in the form of @dfn{transactions} which occur in one
  7544. or more @dfn{journal files}. Each transaction, in turn, is made up of
  7545. one or more @dfn{postings}, which describe how @dfn{amounts} flow from
  7546. one @dfn{account} to another. Here is an example of the simplest of
  7547. journal files:
  7548. @smallexample @c input:validate
  7549. 2010/05/31 Just an example
  7550. Expenses:Some:Account $100.00
  7551. Income:Another:Account
  7552. @end smallexample
  7553. In this example, there is a transaction date, a payee, or description of
  7554. the transaction, and two postings. The postings show movement of one
  7555. hundred dollars from an account within the Income hierarchy, to the
  7556. specified expense account. The name and meaning of these accounts is
  7557. arbitrary, with no preferences implied, although you will find it useful
  7558. to follow standard accounting practices (@pxref{Principles of Accounting
  7559. with Ledger}).
  7560. Since an amount is missing from the second posting, it is assumed to be
  7561. the inverse of the first. This guarantees the cardinal rule of
  7562. double-entry accounting: the sum of every transaction must balance to
  7563. zero, or it is in error. Whenever Ledger encounters a @dfn{null
  7564. posting} in a transaction, it uses it to balance the remainder.
  7565. It is also typical, though not enforced, to think of the first posting
  7566. as the destination, and the final as the source. Thus, the amount of
  7567. the first posting is typically positive. Consider:
  7568. @smallexample @c input:validate
  7569. 2010/05/31 An income transaction
  7570. Assets:Checking $1,000.00
  7571. Income:Salary
  7572. 2010/05/31 An expense transaction
  7573. Expenses:Dining $100.00
  7574. Assets:Checking
  7575. @end smallexample
  7576. @menu
  7577. * Comments and meta-data::
  7578. * Specifying Amounts::
  7579. * Posting costs::
  7580. * Primary commodities::
  7581. @end menu
  7582. @node Comments and meta-data, Specifying Amounts, Journal File Format for Developers, Journal File Format for Developers
  7583. @subsection Comments and meta-data
  7584. Comments are generally started using a @samp{;}. However, in order to
  7585. increase compatibility with other text manipulation programs and methods
  7586. three additional comment characters are valid if used at the beginning
  7587. of a line: @samp{#}, @samp{|}, and @samp{*}.
  7588. @node Specifying Amounts, Posting costs, Comments and meta-data, Journal File Format for Developers
  7589. @subsection Specifying Amounts
  7590. @cindex amounts
  7591. The heart of a journal is the amounts it records, and this fact is
  7592. reflected in the diversity of amount expressions allowed. All of them
  7593. are covered here, though it must be said that sometimes, there are
  7594. multiple ways to achieve a desired result.
  7595. @emph{Note:} It is important to note that there must be at least two
  7596. spaces between the end of the account and the beginning of the amount
  7597. (including a commodity designator).
  7598. @menu
  7599. * Integer Amounts::
  7600. * Commoditized Amounts::
  7601. @end menu
  7602. @node Integer Amounts, Commoditized Amounts, Specifying Amounts, Specifying Amounts
  7603. @subsubsection Integer Amounts
  7604. In the simplest form, bare decimal numbers are accepted:
  7605. @smallexample @c input:validate
  7606. 2010/05/31 An income transaction
  7607. Assets:Checking 1000.00
  7608. Income:Salary
  7609. @end smallexample
  7610. @cindex uncommoditized amounts
  7611. Such amounts may only use an optional period for a decimal point. These
  7612. are referred to as @dfn{integer amounts} or @dfn{uncommoditized
  7613. amounts}. In most ways they are similar to @dfn{commoditized amounts},
  7614. but for one significant difference: They always display in reports with
  7615. @dfn{full precision}. More on this in a moment. For now, a word must
  7616. be said about how Ledger stores numbers.
  7617. Every number parsed by Ledger is stored internally as an
  7618. infinite-precision rational value. Floating-point math is never used,
  7619. as it cannot be trusted to maintain precision of values. So, in the
  7620. case of @samp{1000.00} above, the internal value is @samp{100000/100}.
  7621. While rational numbers are great at not losing precision, the question
  7622. arises: How should they be displayed? A number like @samp{100000/100}
  7623. is no problem, since it represents a clean decimal fraction. But what
  7624. about when the number @samp{1/1} is divided by three? How should one
  7625. print @samp{1/3}, an infinitely repeating decimal?
  7626. Ledger gets around this problem by rendering rationals into decimal at
  7627. the last possible moment, and only for display. As such, some
  7628. rounding must, at times, occur. If this rounding would affect the
  7629. calculation of a running total, special accommodation postings are
  7630. generated to make you aware it has happened. In practice, it happens
  7631. rarely, but even then it does not reflect adjustment of the
  7632. @emph{internal amount}, only the displayed amount.
  7633. What has still not been answered is how Ledger rounds values. Should
  7634. @samp{1/3} be printed as @samp{0.33} or @samp{0.33333}? For
  7635. commoditized amounts, the number of decimal places is decided by
  7636. observing how each commodity is used; but in the case of integer
  7637. amounts, an arbitrary factor must be chosen. Initially, this factor
  7638. is six. Thus, @samp{1/3} is printed back as @samp{0.333333}.
  7639. Further, this rounding factor becomes associated with each particular
  7640. value, and is carried through mathematical operations. For example,
  7641. if that particular number were multiplied by itself, the decimal
  7642. precision of the result would be twelve. Addition and subtraction do
  7643. not affect precision.
  7644. Since each integer amount retains its own display precision, this is
  7645. called @dfn{full precision}, as opposed to commoditized amounts, which
  7646. always look to their commodity to know what precision they should
  7647. round to, and so use @dfn{commodity precision}.
  7648. @node Commoditized Amounts, , Integer Amounts, Specifying Amounts
  7649. @subsubsection Commoditized Amounts
  7650. A @dfn{commoditized amount} is an integer amount which has an
  7651. associated commodity. This commodity can appear before or after the
  7652. amount, and may or may not be separated from it by a space. Most
  7653. characters are allowed in a commodity name, except for the following:
  7654. @itemize @bullet
  7655. @item Any kind of white-space
  7656. @item Numerical digits
  7657. @item Punctuation: @code{.,;:?!}
  7658. @item Mathematical and logical operators: @code{-+*/^&|=}
  7659. @item Bracketing characters: @code{<>[]()}@{@}
  7660. @item The at symbol: @code{@@}
  7661. @end itemize
  7662. And yet, any of these may appear in a commodity name if it is
  7663. surrounded by double quotes, for example:
  7664. @smallexample
  7665. 100 "EUN+133"
  7666. @end smallexample
  7667. If a @dfn{quoted commodity} is found, it is displayed in quotes as
  7668. well, to avoid any confusion as to which part is the amount, and which
  7669. part is the commodity.
  7670. Another feature of commoditized amounts is that they are reported back
  7671. in the same form as parsed. If you specify dollar amounts using
  7672. @samp{$100}, they will print the same; likewise with @samp{100 $} or
  7673. @samp{$100.000}. You may even use decimal commas, such as
  7674. @samp{$100,00}, or thousand-marks, as in @samp{$10,000.00}.
  7675. These display characteristics become associated with the commodity,
  7676. with the result being that all amounts of the same commodity are
  7677. reported consistently. Where this is most noticeable is the
  7678. @dfn{display precision}, which is determined by the most precise value
  7679. seen for a given commodity---in most cases.
  7680. Ledger makes a distinction between @dfn{observed amounts} and
  7681. unobserved amounts. An observed amount is critiqued by Ledger to
  7682. determine how amounts using that commodity should be displayed;
  7683. unobserved amounts are significant in their value only---no matter how
  7684. they are specified, it does not change how other amounts in that
  7685. commodity will be displayed.
  7686. An example of this is found in cost expressions, covered next.
  7687. @node Posting costs, Primary commodities, Specifying Amounts, Journal File Format for Developers
  7688. @subsection Posting costs
  7689. You have seen how to specify either a commoditized or an integer
  7690. amount for a posting. But what if the amount you paid for something
  7691. was in one commodity, and the amount received was another? There are
  7692. two main ways to express this:
  7693. @smallexample @c input:validate
  7694. 2010/05/31 Farmer's Market
  7695. Assets:My Larder 100 apples
  7696. Assets:Checking -$20.00
  7697. @end smallexample
  7698. In this example, you have paid twenty dollars for one hundred apples.
  7699. The cost to you is twenty cents per apple, and Ledger calculates this
  7700. implied cost for you. You can also make the cost explicit using a
  7701. @dfn{cost amount}:
  7702. @smallexample @c input:validate
  7703. 2010/05/31 Farmer's Market
  7704. Assets:My Larder 100 apples @@ $0.200000
  7705. Assets:Checking
  7706. @end smallexample
  7707. Here the @dfn{per-unit cost} is given explicitly in the form of a cost
  7708. amount; and since cost amounts are @emph{unobserved}, the use of six
  7709. decimal places has no effect on how dollar amounts are displayed in
  7710. the final report. You can also specify the @dfn{total cost}:
  7711. @smallexample @c input:validate
  7712. 2010/05/31 Farmer's Market
  7713. Assets:My Larder 100 apples @@@@ $20
  7714. Assets:Checking
  7715. @end smallexample
  7716. These three forms have identical meaning. In most cases the first is
  7717. preferred, but the second two are necessary when more than two
  7718. postings are involved:
  7719. @smallexample @c input:validate
  7720. 2010/05/31 Farmer's Market
  7721. Assets:My Larder 100 apples @@ $0.200000
  7722. Assets:My Larder 100 pineapples @@ $0.33
  7723. Assets:My Larder 100 "crab apples" @@ $0.04
  7724. Assets:Checking
  7725. @end smallexample
  7726. Here the implied cost is @samp{$57.00}, which is entered into the null
  7727. posting automatically so that the transaction balances.
  7728. @node Primary commodities, , Posting costs, Journal File Format for Developers
  7729. @subsection Primary commodities
  7730. @findex --market
  7731. @findex --basis
  7732. In every transaction involving more than one commodity, there is
  7733. always one which is the @dfn{primary commodity}. This commodity
  7734. should be thought of as the exchange commodity, or the commodity used
  7735. to buy and sell units of the other commodity. In the fruit examples
  7736. above, dollars are the primary commodity. This is decided by Ledger
  7737. based on the placement of the commodity in the transaction:
  7738. @smallexample @c input:validate
  7739. 2010/05/31 Sample Transaction
  7740. Expenses 100 secondary
  7741. Assets -50 primary
  7742. 2010/05/31 Sample Transaction
  7743. Expenses 100 secondary @@ 0.5 primary
  7744. Assets
  7745. 2010/05/31 Sample Transaction
  7746. Expenses 100 secondary @@@@ 50 primary
  7747. Assets
  7748. @end smallexample
  7749. The only case where knowledge of primary versus secondary comes into
  7750. play is in reports that use the @option{--market (-V)} or
  7751. @option{--basis (-B)} options. With these, only primary commodities are
  7752. shown.
  7753. If a transaction uses only one commodity, this commodity is also
  7754. considered a primary. In fact, when Ledger goes about ensuring that
  7755. all transactions balance to zero, it only ever asks this of primary
  7756. commodities.
  7757. @node Developer Commands, Ledger Development Environment, Journal File Format for Developers, Ledger for Developers
  7758. @section Developer Commands
  7759. @menu
  7760. * @command{echo}::
  7761. * @command{reload}::
  7762. * @command{source}::
  7763. * Debug Options::
  7764. * Pre-Commands::
  7765. @end menu
  7766. @node @command{echo}, @command{reload}, Developer Commands, Developer Commands
  7767. @subsection @command{echo}
  7768. @findex echo
  7769. This command simply echoes its argument back to the output.
  7770. @node @command{reload}, @command{source}, @command{echo}, Developer Commands
  7771. @subsection @command{reload}
  7772. @findex reload
  7773. Forces ledger to reload any journal files. This function exists to
  7774. support external programs controlling a running ledger process and does
  7775. nothing for a command-line user.
  7776. @node @command{source}, Debug Options, @command{reload}, Developer Commands
  7777. @subsection @command{source}
  7778. @findex source
  7779. The @command{source} command takes a journal file as an argument and
  7780. parses it checking for errors; no other reports are generated, and no
  7781. other arguments are necessary. Ledger will return success if no errors
  7782. are found.
  7783. @node Debug Options, Pre-Commands, @command{source}, Developer Commands
  7784. @subsection Debug Options
  7785. These options are primarily for Ledger developers, but may be of some
  7786. use to a user trying something new.
  7787. @ftable @option
  7788. @item --args-only
  7789. Ignore init files and environment variables for the ledger run.
  7790. @item --debug @var{CODE}
  7791. If Ledger has been built with debug options this will provide extra
  7792. data during the run. The following are the available @var{CODES} to
  7793. debug:
  7794. @multitable @columnfractions .32 .43 .27
  7795. @item @code{account.display} @tab @code{draft.xact} @tab @code{option.names}
  7796. @item @code{account.sorted} @tab @code{expr.calc} @tab @code{org.next_amount}
  7797. @item @code{amount.commodities} @tab @code{expr.compile} @tab @code{org.next_total}
  7798. @item @code{amount.convert} @tab @code{expr.merged.compile} @tab @code{parser.error}
  7799. @item @code{amount.is_zero} @tab @code{filters.changed_value} @tab @code{pool.commodities}
  7800. @item @code{amount.parse} @tab @code{filters.changed_value.rounding} @tab @code{post.assign}
  7801. @item @code{amount.price} @tab @code{filters.collapse} @tab @code{python.init}
  7802. @item @code{amount.refs} @tab @code{filters.forecast} @tab @code{python.interp}
  7803. @item @code{amount.roundto} @tab @code{filters.interval} @tab @code{query.mask}
  7804. @item @code{amount.truncate} @tab @code{filters.revalued} @tab @code{report.predicate}
  7805. @item @code{amount.unround} @tab @code{format.abbrev} @tab @code{scope.search}
  7806. @item @code{annotate.less} @tab @code{format.expr} @tab @code{scope.symbols}
  7807. @item @code{archive.journal} @tab @code{generate.post} @tab @code{select.parse}
  7808. @item @code{auto.columns} @tab @code{generate.post.string} @tab @code{textual.include}
  7809. @item @code{budget.generate} @tab @code{history.find} @tab @code{textual.parse}
  7810. @item @code{commodity.annotated.strip} @tab @code{history.map} @tab @code{timelog}
  7811. @item @code{commodity.annotations} @tab @code{item.meta} @tab @code{times.epoch}
  7812. @item @code{commodity.compare} @tab @code{ledger.read} @tab @code{times.interval}
  7813. @item @code{commodity.download} @tab @code{ledger.validate} @tab @code{times.parse}
  7814. @item @code{commodity.exchange} @tab @code{lookup} @tab @code{value.sort}
  7815. @item @code{commodity.price.find} @tab @code{lookup.account} @tab @code{value.storage.refcount}
  7816. @item @code{commodity.prices.add} @tab @code{mask.match} @tab @code{xact.extend}
  7817. @item @code{commodity.prices.find} @tab @code{memory.debug} @tab @code{xact.extend.cleared}
  7818. @item @code{csv.mappings} @tab @code{op.memory} @tab @code{xact.extend.fail}
  7819. @item @code{csv.parse} @tab @code{option.args} @tab @code{xact.finalize}
  7820. @end multitable
  7821. @
  7822. @item --trace @var{INT}
  7823. Enable tracing. The @var{INT} specifies the level of trace desired:
  7824. @multitable @columnfractions .3 .7
  7825. @item @code{LOG_OFF} @tab 0
  7826. @item @code{LOG_CRIT} @tab 1
  7827. @item @code{LOG_FATAL} @tab 2
  7828. @item @code{LOG_ASSERT} @tab 3
  7829. @item @code{LOG_ERROR} @tab 4
  7830. @item @code{LOG_VERIFY} @tab 5
  7831. @item @code{LOG_WARN} @tab 6
  7832. @item @code{LOG_INFO} @tab 7
  7833. @item @code{LOG_EXCEPT} @tab 8
  7834. @item @code{LOG_DEBUG} @tab 9
  7835. @item @code{LOG_TRACE} @tab 10
  7836. @item @code{LOG_ALL} @tab 11
  7837. @end multitable
  7838. @
  7839. @item --verbose
  7840. @itemx -v
  7841. Print detailed information on the execution of Ledger.
  7842. @item --verify
  7843. Enable additional assertions during run-time. This causes a significant
  7844. slowdown. When combined with @option{--debug @var{CODE}} ledger will
  7845. produce memory trace information.
  7846. @item --verify-memory
  7847. Verify that every constructed object is properly destructed. This is for
  7848. debugging purposes only.
  7849. @item --version
  7850. Print version information and exit.
  7851. @end ftable
  7852. @node Pre-Commands, , Debug Options, Developer Commands
  7853. @subsection Pre-Commands
  7854. @cindex pre-commands
  7855. Pre-commands are useful when you aren't sure how a command or option
  7856. will work. The difference between a pre-command and a regular command
  7857. is that pre-commands ignore the journal data file completely, nor is
  7858. the user's init file read.
  7859. @ftable @command
  7860. @item eval @var{VEXPR}
  7861. Evaluate the given value expression against the model transaction.
  7862. @item format @var{FORMAT_STRING}
  7863. Print details of how ledger uses the given formatting description and
  7864. apply it against a model transaction.
  7865. @item generate
  7866. Randomly generates syntactically valid Ledger data from a seed. Used
  7867. by the @samp{GenerateTests} harness for development testing.
  7868. @item parse @var{VEXPR}
  7869. @itemx expr @var{VEXPR}
  7870. Print details of how ledger uses the given value expression description
  7871. and apply it against a model transaction.
  7872. @item period @var{PERIOD_EXPRESSION}
  7873. Evaluate the given period and report how Ledger interprets it:
  7874. @smallexample @c command:51F6A2C
  7875. $ ledger period "this year" --now 2011-01-01
  7876. @end smallexample
  7877. @smallexample @c output:51F6A2C
  7878. --- Period expression tokens ---
  7879. TOK_THIS: this
  7880. TOK_YEAR: year
  7881. END_REACHED: <EOF>
  7882. --- Before stabilization ---
  7883. range: in year 2011
  7884. --- After stabilization ---
  7885. range: in year 2011
  7886. start: 11-Jan-01
  7887. finish: 12-Jan-01
  7888. --- Sample dates in range (max. 20) ---
  7889. 1: 11-Jan-01
  7890. @end smallexample
  7891. @item query
  7892. @itemx args
  7893. Evaluate the given arguments and report how Ledger interprets it against
  7894. the following model transaction:
  7895. @smallexample @c command:validate
  7896. $ ledger query "/Book/"
  7897. @end smallexample
  7898. @smallexample
  7899. --- Input arguments ---
  7900. ("/Book/")
  7901. --- Context is first posting of the following transaction ---
  7902. 2004/05/27 Book Store
  7903. ; This note applies to all postings. :SecondTag:
  7904. Expenses:Books 20 BOOK @@ $10
  7905. ; Metadata: Some Value
  7906. ; Typed:: $100 + $200
  7907. ; :ExampleTag:
  7908. ; Here follows a note describing the posting.
  7909. Liabilities:MasterCard $-200.00
  7910. --- Input expression ---
  7911. (account =~ /Book/)
  7912. --- Text as parsed ---
  7913. (account =~ /Book/)
  7914. --- Expression tree ---
  7915. 0x7fd639c0da40 O_MATCH (1)
  7916. 0x7fd639c10170 IDENT: account (1)
  7917. 0x7fd639c10780 VALUE: /Book/ (1)
  7918. --- Compiled tree ---
  7919. 0x7fd639c10520 O_MATCH (1)
  7920. 0x7fd639c0d6c0 IDENT: account (1)
  7921. 0x7fd639c0d680 FUNCTION (1)
  7922. 0x7fd639c10780 VALUE: /Book/ (1)
  7923. --- Calculated value ---
  7924. true
  7925. @end smallexample
  7926. @item script
  7927. @value{FIXME:UNDOCUMENTED}
  7928. @item template
  7929. Shows the insertion template that the @command{xact} sub-command
  7930. generates. This is a debugging command.
  7931. @end ftable
  7932. @node Ledger Development Environment, , Developer Commands, Ledger for Developers
  7933. @section Ledger Development Environment
  7934. @menu
  7935. * @file{acprep} build configuration tool::
  7936. * Testing Framework::
  7937. @end menu
  7938. @node @file{acprep} build configuration tool, Testing Framework, Ledger Development Environment, Ledger Development Environment
  7939. @subsection @file{acprep} build configuration tool
  7940. @node Testing Framework, , @file{acprep} build configuration tool, Ledger Development Environment
  7941. @subsection Testing Framework
  7942. Ledger source ships with a fairly complete set of tests to verify that
  7943. all is well, and no old errors have resurfaced. Tests are run
  7944. individually with @file{ctest}. All tests can be run using @code{make
  7945. check} or @code{ninja check} depending on which build tool you prefer.
  7946. Once built, the ledger executable resides under the @file{build}
  7947. subdirectory in the source tree. Tests are built and stored in the
  7948. @file{test} subdirectory for the build. For example,
  7949. @file{~/ledger/build/ledger/opt/test}.
  7950. @menu
  7951. * Running Tests::
  7952. * Writing Tests::
  7953. @end menu
  7954. @node Running Tests, Writing Tests, Testing Framework, Testing Framework
  7955. @subsubsection Running Tests
  7956. The complete test suite can be run from the build directory using the
  7957. check option for the build tool you use. For example, @code{make
  7958. check}. The entire test suit lasts around a minute for the optimized
  7959. built and many times longer for the debug version. While developing
  7960. and debugging, running individual tests can save a great deal of time.
  7961. Individual tests can be run from the @file{test} subdirectory of the
  7962. build location. To execute a single test use @code{ctest -V -R regex},
  7963. where the regex matches the name of the test you want to build.
  7964. There are nearly 300 tests stored under the @file{test} subdirectory
  7965. in the main source distribution. They are broken into two broad
  7966. categories, baseline and regression. To run the @file{5FBF2ED8} test,
  7967. for example, issue @code{ctest -V -R "5FB"}.
  7968. @node Writing Tests, , Running Tests, Testing Framework
  7969. @subsubsection Writing Tests
  7970. To write a new test first decide to which broad category the test belongs:
  7971. baseline or regression. Depending on the category tests are named differently
  7972. baseline tests are prefixed with their type, e.g. @samp{cmd}
  7973. (@pxref{Baseline Test Types} for valid types), whereas regressions are either
  7974. named after the bug id, e.g. @samp{1234.test} or uuid @samp{91416D62.test}.
  7975. In case several test files belong to the same bug number the files by appending
  7976. @code{_X} where @samp{X} is the number of the test, e.g. @samp{1234_1.test},
  7977. @samp{1234_2.test}.
  7978. Baseline Test Types:
  7979. @anchor{Baseline Test Types}
  7980. @table @code
  7981. @item cmd
  7982. Ledger commands like @command{register} or @command{balance}
  7983. @item dir
  7984. Ledger directives like @code{account} or @code{alias}
  7985. @item feat
  7986. Ledger features such as balance assertions in journal file
  7987. @item opt
  7988. Ledger options such as @option{--period} or @option{--format}
  7989. @end table
  7990. A ledger test file contains three sections:
  7991. @enumerate
  7992. @item the journal data used for the test, this can be empty in certain
  7993. scenarios
  7994. @item the ledger command-line options used for the test
  7995. @item the expected output
  7996. @end enumerate
  7997. Ledger has a special command directive for tests, everything between
  7998. @code{test} and @code{end test} is treated like a comment, so every
  7999. Ledger test is automatically a valid Ledger file.
  8000. The test scripts take the remainder of the @code{test} line and use
  8001. it as command-line arguments for ledger, the text enclosed in @code{test}
  8002. and @code{end test} is expected output, for example:
  8003. @smallexample @c input:validate
  8004. ; This is the journal data
  8005. year 2014
  8006. 12/24 (C0d3) Santa Claus
  8007. Assets:Bank ¤ -150,00
  8008. Expenses:Presents
  8009. ; The following line specifies the ledger command-line options for this test and
  8010. ; everything between the next line and `end test` specifies the expected output
  8011. test reg --payee=code
  8012. 14-Dec-24 C0d3 Assets:Bank ¤ -150,00 ¤ -150,00
  8013. 14-Dec-24 C0d3 Expenses:Presents ¤ 150,00 0
  8014. end test
  8015. @end smallexample
  8016. When it is necessary to test for errors printed to @code{stderr} redirect
  8017. the test output by adding @code{->} to the @code{test} line and match the
  8018. expected error text in an @code{__ERROR__} section:
  8019. @smallexample
  8020. 2014/01/01 * Acme Corporation
  8021. Assets:Bank:Checking ¤ 1.000,00
  8022. [Fund:Vacation] ¤ 300,00
  8023. [Fund:Studies] ¤ 600,00
  8024. Income:Salary ¤ -2.000,00
  8025. test reg ->
  8026. __ERROR__
  8027. While parsing file "$FILE", line 5:
  8028. While balancing transaction from "$FILE", lines 1-5:
  8029. > 2014/01/01 * Acme Corporation
  8030. > Assets:Bank:Checking ¤ 1.000,00
  8031. > [Fund:Vacation] ¤ 300,00
  8032. > [Fund:Studies] ¤ 600,00
  8033. > Income:Salary ¤ -2.000,00
  8034. Unbalanced remainder is:
  8035. ¤ -100,00
  8036. Amount to balance against:
  8037. ¤ 1.900,00
  8038. Error: Transaction does not balance
  8039. end test
  8040. @end smallexample
  8041. A special @code{$FILE} variable can be used to match the journal filename
  8042. used during the test.
  8043. To add new tests to the test suite use the rebuild_cache option for the
  8044. build tool you use, for example @code{make rebuild_cache}, now the
  8045. new tests can be run as documented in @ref{Running Tests}.
  8046. @node Major Changes from version 2.6, Example Journal File, Ledger for Developers, Top
  8047. @chapter Major Changes from version 2.6
  8048. The following have been removed from Ledger 3.0:
  8049. @itemize
  8050. @item
  8051. OFX support.
  8052. @item
  8053. GnuCash file import.
  8054. @item
  8055. The option @option{--performance (-g)}.
  8056. @item
  8057. The balance report now defaults to showing all relevant accounts. This
  8058. is the opposite of 2.x. That is, @command{bal} in 3.0 does what @samp{-s
  8059. bal} did in 2.x. To see 2.6 behavior, use @option{--collapse (-n)}
  8060. option in 3.0, like @samp{bal -n}. The @option{--subtotal (-s)} option
  8061. no longer has any effect on balance reports.
  8062. @end itemize
  8063. @noindent
  8064. The following are deprecated in Ledger 3.0:
  8065. @itemize
  8066. @item
  8067. Single character value expressions are deprecated and should be changed
  8068. to the new value expressions available in 3.0
  8069. @item
  8070. The following environment variables have been renamed in Ledger 3.0:
  8071. @table @env
  8072. @item LEDGER
  8073. is now @env{LEDGER_FILE},
  8074. @item LEDGER_INIT
  8075. is now @env{LEDGER_INIT_FILE},
  8076. @item PRICE_HIST
  8077. is now @env{LEDGER_PRICE_DB},
  8078. @item PRICE_EXP
  8079. is now @env{LEDGER_PRICE_EXP}.
  8080. @end table
  8081. @end itemize
  8082. @node Example Journal File, Miscellaneous Notes, Major Changes from version 2.6, Top
  8083. @appendix Example Journal File
  8084. The following journal file is included with the source distribution of
  8085. ledger. It is called @file{drewr3.dat} and exhibits many ledger
  8086. features, include automatic and virtual transactions,
  8087. @smallexample @c input:validate
  8088. ; -*- ledger -*-
  8089. = /^Income/
  8090. (Liabilities:Tithe) 0.12
  8091. ;~ Monthly
  8092. ; Assets:Checking $500.00
  8093. ; Income:Salary
  8094. ;~ Monthly
  8095. ; Expenses:Food $100
  8096. ; Assets
  8097. 2010/12/01 * Checking balance
  8098. Assets:Checking $1,000.00
  8099. Equity:Opening Balances
  8100. 2010/12/20 * Organic Co-op
  8101. Expenses:Food:Groceries $ 37.50 ; [=2011/01/01]
  8102. Expenses:Food:Groceries $ 37.50 ; [=2011/02/01]
  8103. Expenses:Food:Groceries $ 37.50 ; [=2011/03/01]
  8104. Expenses:Food:Groceries $ 37.50 ; [=2011/04/01]
  8105. Expenses:Food:Groceries $ 37.50 ; [=2011/05/01]
  8106. Expenses:Food:Groceries $ 37.50 ; [=2011/06/01]
  8107. Assets:Checking $ -225.00
  8108. 2010/12/28=2011/01/01 Acme Mortgage
  8109. Liabilities:Mortgage:Principal $ 200.00
  8110. Expenses:Interest:Mortgage $ 500.00
  8111. Expenses:Escrow $ 300.00
  8112. Assets:Checking $ -1000.00
  8113. 2011/01/02 Grocery Store
  8114. Expenses:Food:Groceries $ 65.00
  8115. Assets:Checking
  8116. 2011/01/05 Employer
  8117. Assets:Checking $ 2000.00
  8118. Income:Salary
  8119. 2011/01/14 Bank
  8120. ; Regular monthly savings transfer
  8121. Assets:Savings $ 300.00
  8122. Assets:Checking
  8123. 2011/01/19 Grocery Store
  8124. Expenses:Food:Groceries $ 44.00 ; hastag: not block
  8125. Assets:Checking
  8126. 2011/01/25 Bank
  8127. ; Transfer to cover car purchase
  8128. Assets:Checking $ 5,500.00
  8129. Assets:Savings
  8130. ; :nobudget:
  8131. apply tag hastag: true
  8132. apply tag nestedtag: true
  8133. 2011/01/25 Tom's Used Cars
  8134. Expenses:Auto $ 5,500.00
  8135. ; :nobudget:
  8136. Assets:Checking
  8137. 2011/01/27 Book Store
  8138. Expenses:Books $20.00
  8139. Liabilities:MasterCard
  8140. end tag
  8141. 2011/12/01 Sale
  8142. Assets:Checking:Business $ 30.00
  8143. Income:Sales
  8144. end tag
  8145. @end smallexample
  8146. @node Miscellaneous Notes, Concepts Index, Example Journal File, Top
  8147. @appendix Miscellaneous Notes
  8148. Various notes from the discussion list that I haven't incorporated in
  8149. to the main body of the documentation.
  8150. @menu
  8151. * Cookbook::
  8152. @end menu
  8153. @node Cookbook, , Miscellaneous Notes, Miscellaneous Notes
  8154. @section Cookbook
  8155. @menu
  8156. * Invoking Ledger::
  8157. * Ledger Files::
  8158. @end menu
  8159. @node Invoking Ledger, Ledger Files, Cookbook, Cookbook
  8160. @subsection Invoking Ledger
  8161. @smallexample @c command:validate
  8162. $ ledger --group-by "tag('trip')" bal
  8163. @end smallexample
  8164. @c FIXME: The following example fails to validate due to:
  8165. @c While applying is_realzero to :
  8166. @c Error: Cannot determine if an uninitialized value is really zero
  8167. @c @smallexample @c command:validate
  8168. @c $ ledger reg --sort "tag('foo')" %foo
  8169. @c @end smallexample
  8170. @smallexample @c command:validate
  8171. $ ledger cleared VWCU NFCU Tithe Misentry
  8172. @end smallexample
  8173. @smallexample @c command:validate
  8174. $ ledger register Joint --uncleared
  8175. @end smallexample
  8176. @smallexample @c command:validate
  8177. $ ledger register Checking --sort d -d 'd>[2011/04/01]' until 2011/05/25
  8178. @end smallexample
  8179. @node Ledger Files, , Invoking Ledger, Cookbook
  8180. @subsection Ledger Files
  8181. @smallexample @c input:validate
  8182. = /^Income:Taxable/
  8183. (Liabilities:Tithe Owed) -0.1
  8184. = /Noah/
  8185. (Liabilities:Tithe Owed) -0.1
  8186. = /Jonah/
  8187. (Liabilities:Tithe Owed) -0.1
  8188. = /Tithe/
  8189. (Liabilities:Tithe Owed) -1.0
  8190. @end smallexample
  8191. @node Concepts Index, Commands & Options Index, Miscellaneous Notes, Top
  8192. @unnumbered Concepts Index
  8193. @printindex cp
  8194. @node Commands & Options Index, , Concepts Index, Top
  8195. @unnumbered Commands & Options Index
  8196. @printindex fn
  8197. @bye