Package: overcome
Overcome and AnalysisWhile Overcome provides the operation that fits for every OHLCV row, in a history
input data set; Analysis provides the profits and number of overlapped opened
positions for a given data set, with fulfilled operations.Given a constant take profit and a stop loss, and a time base sorted data vector
with the "high", "low", and "close" values from a stock market product, Overcome
calculates the potential earnings in both, buying and selling for every step in
the timeline.Analysis by the other hand is a calculation after an operation has been already
set. It counts the overlapped buying and selling positions, separately, and the
profits for both as well.InstallInstall from Pypi repository from at least a Python 3.7.pipinstallovercomeOvercome usageInstantiate Overcome with the constant value for take profit and stop loss.
A value for the precision threshold is required as well. The precision threshold
applies on whether a "close" value is close to the take profit or stop loss
boundaries.outcome=Overcome(threshold=np.float32(0.00001),take_profit=np.float32(0.001),stop_loss=np.float32(0.001))Callapplymethod with the input and wait for the output. The required input
structure is a numpy array with a shape of (LENGTH, 3).The order of the 3 columns is very strict. The first one is for the "high"
values, the second one for the "low" values and the third one for "close" values.earn_buying,earn_selling=outcome.apply(high_low_close)It returns a tuple of two arrays with the same number of items than the input,
indeed matching the same timeline. The first array is for the earnings from
buying positions and the second one for the selling ones.Opened positions limitOpening all positions without limiting their number may be pretty risky.
Overcome Simulation supports to set a predefined positions number limit in each
direction, buying and selling. So, for example, if the positions limit is set to
10, when the first 10 positions will be opened in the simulated outcome, the
following ones won't open, and then they are not accountable for profit or loss.Creating an Overcome instance with opened positions limited at 10 for buying and
10 for selling is as follows.outcome=Overcome(threshold=np.float32(0.00001),take_profit=np.float32(0.001),stop_loss=np.float32(0.001),positions_limit=10)Counters and maximum delayBooleanhas_countersis an optional constructor argument to ask for a counter
for each, buying and selling positions, with the number of steps between the
position starting and closing steps. These optional information comes in the
returning tuple as two new items.outcome=Overcome(threshold=np.float32(0.00001),take_profit=np.float32(0.001),stop_loss=np.float32(0.001),positions_limit=10,has_counters=True)earn_buying,earn_selling,buying_lengths,selling_lengths=\outcome.apply(high_low_close)Having the number of steps of every operation lets us filter out the longer
operations in order to get only the fastest ones, apparently the most accurate
ones. The integer type optional argumentmax_delaymakes to ignore the earnings
from the operations longer than the maximum steps set in this value. In the
following example, the earnings from the positions with more than 10 steps will
be ignored and set to 0.Setting a value over 0 tomax_delayforceshas_countersto enable in order
to get the required counts. So, the returning tuple length is 4 as well.outcome=Overcome(threshold=np.float32(0.00001),take_profit=np.float32(0.001),stop_loss=np.float32(0.001),positions_limit=10,max_delay=10)earn_buying,earn_selling,buying_lengths,selling_lengths=\outcome.apply(high_low_close)Input from a dataframeStarting with a Dataframe asdffrom any product historical data, convert the
columns into the required input by the following expression.high_low_close=df[["high","low","close"]].to_numpy(dtype=np.float32)Then apply the calculation and merge the result into the original Dataframe as
follows.(df["earn_buying"],df["earn_selling"])=outcome.apply(high_low_close)ExampleThe following table is an example of dataframe with the new columns for
buying and selling earnings already calculated. This table is part of a test
suite, so the data is not real, it is just prepared to achieve a scenario with
a variety of positive and negative earnings in both operations, buying and
selling.The configuration for take profit for this example is 0.001 and the stop loss is
0.0007.timestampclosehighlowearn_buyingearn_selling2022-04-01 00:01:001.01.01.00.001-0.00072022-04-01 00:02:001.01.01.00.001-0.00072022-04-01 00:03:001.00051.00071.00.001-0.00072022-04-01 00:04:001.00051.0011.00010.001-0.00072022-04-01 00:05:001.00061.0011.0002-0.0007-0.00072022-04-01 00:06:001.00071.00111.0-0.0007-0.00072022-04-01 00:07:001.00081.00121.0004-0.0007-0.00072022-04-01 00:08:001.00091.00131.0005-0.00070.0012022-04-01 00:09:001.0011.00151.0006-0.00070.0012022-04-01 00:10:001.0011.00150.9995-0.00070.0012022-04-01 00:11:001.0011.00150.9993-0.00070.0012022-04-01 00:12:001.0011.00160.9990.00.0Analysis usageInstantiate Analysis by giving an integer type constant for every distinct
operation: relax, selling, buying.analysis=Analysis(threshold=np.float32(0.00001),take_profit=np.float32(0.001),stop_loss=np.float32(0.001),categories={"relax":0,"sell":1,"buy":2})results=analysis.apply(predictions,ohlcv_dataframe)Minimizing costsThere is a service that in a regular alternate way, sets off the given active
positions like buying or selling. This service is an optional dependency of
Analysis, so it allow to analyze the given operations, by minimizing them in
the way on.The following example, sets off 2 of 3 positions. Starting at the second row of
the given operations due to the RegularCostMinimizer's offset optional argument.RELAX_CATEGORY=0analysis=Analysis(threshold=np.float32(0.00001),take_profit=np.float32(0.001),stop_loss=np.float32(0.001),categories={"relax":RELAX_CATEGORY,"sell":1,"buy":2},minimizer=RegularCostMinimizer(RELAX_CATEGORY,2,offset=1))results=analysis.apply(predictions,ohlcv_dataframe)Test and developmentThis project is based on BDD development. Any change starts heretests/features.Moreover, you can find the most specific documentation about any module and
package of this project.Tests requirebehaveandpandas.
Execute the command below from the source code root directory to install the
extra requirements to start developing on this project.pip install -e ".[dev]"PYTHONPATH must contain the main source directory./srcto execute the
tests. Use the fantastic  command as
follows.PYTHONPATH=./src behave tests/featuresThere are a few integration tests as well. Execute as follows.PYTHONPATH=./src python tests/integration/test_overcome.pyAuthorJaume Mila Beajaume@westial.com