Cumulative & Annualized Returns

The cumulative return of an investment is the aggregate return that an investment has gained or lost over time (it can be both positive or negative). If you have Open-High-Low-Close stock data, you can compute cumulative returns on its adjusted price as dividends, and stock splits will lead to incorrect results. \[R_c = \frac{P_c}{P_i} - 1\] where \(R_c\) is the cumulative return, \(P_c\) is the current price, and \(P_i\) is the initial price. …

Posted on

Keeping Python Dictionaries in Order

If you want to control the order of items in a dictionary, you can use an OrderedDict. It preserves the original insertion order of data. It’s a useful construct for when you want to seriealize or encode in different formats. As a note, the structure of an OrderedDict (a doubly linked list), means that these dictionaries are at least twice as heavy as normal dictionaries, meaning they require more memory. …

Posted on

Mapping Keys to Multiple Dictionary Values in Python

If you want to create a dictionary where you map keys to more than one value (a “multi-dict”), you must store these values into another container like a list or set. Use lists if you want to preserve the order of insertions, use sets if you don’t want to keep duplicates. It all depends on your use case, use the container with the characteristics that fit your needs. d = { "a" : [1, 2, 3], "b" : [4, 5] } d ## {'a': [1, 2, 3], 'b': [4, 5]} e = { "a" : {1, 2, 3}, "b" : {4, 5} } e ## {'a': {1, 2, 3}, 'b': {4, 5}} You can also use defaultdict which allows you to write cleaner code. …

Posted on

Control Limits in Analytics

Control limits are visual references that help you detect if a statistic (or time series) is getting “out of control.” I first saw them referenced on Avinash Kaushik’s blog. You can use a metric’s standard deviation to plot a bounded region, (\(\pm 3 \sigma\)), within which the statistic is assumed to behave normally. It’s not wandering too far off from its mean. The reason why this is useful in analytics is that more often than not, people will latch onto meaningless fluctuations and believe them to be worthy of attention. …

Posted on

Notes on Trading #2

Stop-Losses and Take-Profits Having Stop-Loss Orders (S/L) and Take-Profit Orders (T/P) allows you to compute a Risk/Reward Ratio for you trades. If you’re trading at the weekly level, these types of orders might let you ride upward trends more reliably than if you did it intraday. Volatile assets might hit a T/P target way earlier than you’d hope, which might be a good thing if you want to dip in and out. …

Posted on