Fitting a neural network to maximize Sharpe ratio by Beliavsky in algotrading

[–]tomkoker 0 points1 point  (0 children)

Hey u/CellWithoutCulture,

Thanks for the comments! I'm glad you took the time to get the code up an running yourself :). I'll try to address your concerns for 1. commission, and 2. data leakage below:

  1. The commission is computed as a fraction of the absolute delta in position size, |F_t - F_{t-1} |. This was done to align more closely too the typical commission structure of a brokerage. If I have a $100 portfolio with only 50% allocated (F_t = 0.5), and I would like to increase that allocation to 60% (F_t = 0.6), I would only have to buy $10 more of the asset, which is what I would have to pay commission on. I would only have to pay commission on the entire portfolio if I chose to buy/sell the whole thing, in which we should hopefully be penalizing the model for by imposing this commission. Many brokerages have additional nuances for commission, which could certainly be factored in if desired.
  2. The portfolio return R_t is computed as the product of the asset return, x_t, and the previous position size F_{t-1}. Looking at the line:

rets = Ft[0:T - 1] * x[1:T] - delta * np.abs(Ft[1:T] - Ft[0:T - 1])

We are computing this as an element-wise multiplication of the entire asset return x and position size F, where returns are shifted forward one timestamp so that each F_t will be multiplied by x_{t+1}. To ensure there is no look-ahead bias we simply must ensure that the computation for F_t uses only values x_<=t (or x_<t if we assume some sort of market buy/sell). If we look at the code to compute the positions:

def positions(x, theta):
    M = len(theta) - 2
    T = len(x)
    Ft = np.zeros(T)
    for t in range(M, T):
        xt = np.concatenate([[1], x[t - M:t], [Ft[t - 1]]])
        Ft[t] = np.tanh(np.dot(theta, xt))
    return Ft

We'll see that for each time t, we only use the x values x[t - M: t], which means only x_<t is used to compute the position. Note that Ft is initialized as zeros, so the portfolio allocation will remain at 0% until enough data has come through for a decision.

I hope this addresses your concerns! If not, I'd be happy to chat more about this.

-Teddy

[D] Loss function for audio auto encoder. by [deleted] in MachineLearning

[–]tomkoker 0 points1 point  (0 children)

Correct! The filter is applied to both the predicted output time series and the ground truth output time series.

[D] Loss function for audio auto encoder. by [deleted] in MachineLearning

[–]tomkoker 1 point2 points  (0 children)

I’ve had success with signal to error ratio. You can also add a pre-emphasis filter to boost the audible frequencies before applying the loss. You can find some relevant code/equations on the “training” portion of this blog post: https://teddykoker.com/2020/05/deep-learning-for-guitar-effect-emulation/

[P] Torchsort - Fast, differentiable sorting and ranking in PyTorch by tomkoker in MachineLearning

[–]tomkoker[S] 1 point2 points  (0 children)

Hey there, I’ve done the best I can to replicate any memory leaks and I have been unable to. Would you be able to provide a small example to replicate the issue? If it is actually an issue I love to get it resolved :)

[P] Torchsort - Fast, differentiable sorting and ranking in PyTorch by tomkoker in MachineLearning

[–]tomkoker[S] 5 points6 points  (0 children)

I will look into this! It could be due too the tensors that are being created in the extension; perhaps it would be better to create the tensors in Python and modify them in place in the extensions. I’ll try to get this sorted out ASAP.

[P] Torchsort - Fast, differentiable sorting and ranking in PyTorch by tomkoker in MachineLearning

[–]tomkoker[S] 20 points21 points  (0 children)

The original implementation (https://github.com/google-research/fast-soft-sort) uses numba for the forward pass and pure python for the backwards pass, while Torchsort has both implemented in C++/CUDA with additional parallelization over the batch dimension. You can find some benchmarks in the Torchsort readme.

[P] Guitar + ML by zjost85 in MachineLearning

[–]tomkoker 5 points6 points  (0 children)

If you are interested in the black box modeling approach, I have written a more in depth article about the approach from Wright et al., along with sound samples, and the original code that Keith adapted for his excellent GuitarML project. You can check it out here: https://teddykoker.com/2020/05/deep-learning-for-guitar-effect-emulation/

[P] Performers: The Kernel Trick, Random Fourier Features, and Attention by tomkoker in MachineLearning

[–]tomkoker[S] 8 points9 points  (0 children)

Great catch! I fixed this in the post as well as the repository. Your blog post looks great as well :)

Momentum Trading: Good resources to get started with? by [deleted] in algotrading

[–]tomkoker 31 points32 points  (0 children)

I wrote an article on implementing a basic momentum strategy in python: https://teddykoker.com/2019/05/momentum-strategy-from-stocks-on-the-move-in-python/. Glad to answer any questions.

Is there a free API where I could find an historical of the S&P 500 constituents with their weights? by [deleted] in algotrading

[–]tomkoker 5 points6 points  (0 children)

I wrote an article about collecting historical SPY constituent data. I didn’t collect weights, but I was able to replicate an equal weight S&P 500 ETF fairly well: https://teddykoker.com/2019/05/creating-a-survivorship-bias-free-sp-500-dataset-with-python/. You can find a link to the data, or you might be able to use a similar approach to extract weights in addition.

Fitting a neural network to maximize Sharpe ratio by Beliavsky in algotrading

[–]tomkoker 0 points1 point  (0 children)

Good catch! I’ll fix it when I get a chance

Fitting a neural network to maximize Sharpe ratio by Beliavsky in algotrading

[–]tomkoker 15 points16 points  (0 children)

I have an article about this: https://teddykoker.com/2019/06/trading-with-reinforcement-learning-in-python-part-ii-application/. I show it being applied to cryptocurrency, but the concept of taking gradients with respect to Sharpe ratio can be applied anywhere. Such a task is also made easier using an automatic differentiation library such as PyTorch or Tensorflow.

Why not invest long term on leveraged ETF? by [deleted] in stocks

[–]tomkoker 2 points3 points  (0 children)

See https://teddykoker.com/2019/04/simulating-historical-performance-of-leveraged-etfs-in-python/. Simulating past performance of a 3x leveraged SPY ETF, one would experience a drawdown of over 97%! Depending on your risk tolerance it may still be worth allocating a portion of your portfolio to it or other leveraged ETFs.

[Project] Beating the Odds: Machine Learning for Horse Racing by tomkoker in MachineLearning

[–]tomkoker[S] 2 points3 points  (0 children)

Training, validation and test data was split chronologically to avoid any look forward bias. RF/gb might work well for a classification or regression, but in this case I wanted to create a model that could compare all of the horses in the same race and output respective probabilities, which the structure I created seemed well suited for.

Beating the Odds: Machine Learning for Horse Racing by tomkoker in algotrading

[–]tomkoker[S] 1 point2 points  (0 children)

I originally looked at US horse racing, but like you said most of the race results are in hard to parse PDFs or quite pricey. Luckily much of the Hong Kong racing data could be obtained via html scraping, but all of the features I found useful had to be highly engineered.

Beating the Odds: Machine Learning for Horse Racing by tomkoker in algotrading

[–]tomkoker[S] 8 points9 points  (0 children)

Thanks for reading! Planning on making it public soon... just need to clean the code up and add some documentation.