Package: trade
A paradigm for financial apps.Copyright (c) 2015-2018 Rafael da Silva Rochahttps://python-trade.appspot.comhttps://github.com/rochars/tradeInstallationpip install tradetradetradeimplements a paradigm for financial apps where the operations
themselves (calledoccurrences) are objects responsible for updating
the state of an asset holder every time they occur.The asset holder state at a given timestamp may be loaded from a source
(like a database) and then updated with a series of occurrences - raw
operation data loaded asOccurrenceobjects. This outputs a new state
for theHolder.tradeoffers four classes:Holder,Occurrence,SubjectandContextand defines the basic structure for a context-independent financial app. These
classes should be extended according to the needs of the app.It uses the concept ofholders,subjects,occurrencesandcontextsAsubjectis anything that can be traded.Aholderis someone who owns subjects.Aholderstate is updated byoccurrences.Occurrencesmay happen alone or incontexts.Asubjectis anything that can be tradedAsubjectmay be the share of a corporation, livestock and so on.Aholderis a entity that owns subjectsAholderowns subjects. Theholderstate is updated by occurrences.
A holder may hold none or many subjects.Occurrenceshappen with subjects and change the state of theholderAoccurrencemay be caused by theholder, like the purchase of units of somesubject. They can also represent other events, like a stock split.
In both cases they should change the state of theholder.Acontextmay have its own rulesContextsare groups of occurrences.Contextsare used to pre-process occurrences before informing them to aholder.Acontextmay be a situation where daytrades should be identified, for example.Acontextmay also involve taxes and other costs altering the details of the occurrences.Extending the frameworktradeshould be extended with new types ofoccurrences,subjectsandcontext rules.Theholderstate signature can be defined according to the needs of the application.Live sample appYou can try itlive.This is asample appbuilt withtrade. It runs as a service.The input is a JSON withoccurrencesand a initial state
for theholder.The sample app havecontext taskstoidentify daytradesand more.The service also calculates theprofitsandlossesfrom the
occurrences andgroups the resultsby asset.The output is theholderstate as a JSON.Use:fromtrade.holderimportHolderfromtrade.occurrenceimportOccurrencefromtrade.subjectimportSubject# create a holderholder=Holder()# define some subjectsome_asset=Subject('AST1')# create an occurrence with that subject.# In this example, a purchase of 100 units of the asset,# for the value of $20.some_occurrence=Occurrence(some_asset,'2018-01-02',{"quantity":100,"value":20})# pass it to the holderholder.trade(some_occurrence)# check the holder state:forsubject,stateinholder.state.items():print(subject)print(state)# AST1# {'value': 20.0, 'quantity': 100}Updating the holder state with a new occurrence:# create some other occurrence with that subject.# In this example, a sale of 20 units of the asset,# for the value of $30.holder.trade(Occurrence(some_asset,'2018-01-03',{"quantity":-20,"value":30}))# check the holder state. It should show a change in quantity# and some profit:forsubject,stateinholder.state.items():print(subject)print(state)# AST1# {'value': 20.0, 'quantity': 80}More occurrences:# create some other occurrence with that subject.# Now a purchase of 10 units of the asset, for the# value of $20.holder.trade(Occurrence(some_asset,'2018-01-04',{"quantity":10,"value":25}))# check the holder state. It should show a change in quantity# and in the value of the subject:forsubject,stateinholder.state.items():print(subject)print(state)# AST1# {'value': 20.555555555555557, 'quantity': 90}About meI work as a consultant in the development of financial applications.Talk to me:rocha.rafaelsilva@gmail.comLicenseCopyright (c) 2015-2018 Rafael da Silva RochaPermission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
“Software”), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.