trade API

A paradigm for financial apps.
Copyright (c) 2015-2018 Rafael da Silva Rocha

Version Documentation Live Demo
Build Windows Build Coverage Status Scrutinizer cii

trade

A framework for financial applications.

https://github.com/rochars/trade License: MIT

Copyright (c) 2015-2018 Rafael da Silva Rocha

Permission 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.

trade.context

class context.Context(occurrences, tasks)[source]

A class to group occurrences and contextualize them.

To contextualize a group of occurrences could mean: - To identify daytrades and separate them into different occurrences - Group occurrences with the same subject into one occurrence - Apply costs and other deductions to the value to the occurrences - Calculate taxes based on the occurrences in the context

Objects from this class are created with both a list of occurrences and a list of functions (called “context tasks”).

The context tasks are run by calling Context.apply(). Context tasks run in the order they are listed.

Every context task function receives one argument: the context itself. It is then free to manipulate the raw occurrence data stored in the Context.occurrences list.

The contextualized occurrence data is then stored in the Context.contextualized list.

The occurrences in Context.contextualized should be informed to a Holder as regular occurrences.

The original Occurrence list is not modified.

Attributes:
occurrences: A list of Occurrence objects. tasks: A list of functions. contextualized: A list of Occurrence objects.
apply()[source]

Apply the rules of the context to its occurrences.

This method executes all the functions defined in self.tasks in the order they are listed.

Every function that acts as a context task receives the Context object itself as its only argument.

The contextualized occurrences are then stored in Context.contextualized.

The original Occurrence instances are not modified.

trade.holder

class holder.Holder(state=None)[source]

A class to represent a owner of subjects.

A Holder object is created with a initial state. The state of Holder is then updated by Occurrence objects by calling Holder.trade(occurrence).

trade apps should define the Holder.state signature according to their needs.

Classes that inherit from Occurrence may add and manipulate new items on the Holder.state dictionary.

Attributes:
state: A dict representing the state of the Holder.
trade(occurrence)[source]

Update the state of the Holder with an occurrence.

trade.occurrence

class occurrence.Occurrence(subject, date, details)[source]

A class to represent occurrences with subjects.

Attributes:
subject: A Subject instance. date: A string to represent the moment of the occurrence. details: A dict with the occurrence details.
update_holder(holder)[source]

Udpate the Holder state according to the occurrence.

This implementation is a example of how a Occurrence object can update the Holder state; this method should be overriden by classes that inherit from the Occurrence class.

This sample implementation simply update the quantity and the average price of the Subject in the Holder’s possession every time objects from this class are passed to Holder.trade().

This sample implementation considers the following signature for the Holder.state dict:

{
    "SUBJECT SYMBOL": {
        "quantity": 0,
        "value": 0
    }
}

And the following signature for the Occurrance.details dict:

{
    "quantity": 0,
    "value": 0
}

trade.subject

class subject.Subject(symbol, details=None)[source]

A class to represent a tradable unit.

Attributes:
symbol: A string to identify the subject. details: A dict with additional data. Optional.

License

Copyright (c) 2015-2018 Rafael da Silva Rocha

Permission 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.