Skip to content
All library documents

LSTM Trading Labels for Take-Profit and Stop-Loss Outcomes

Article Quant Q&A · Author: Jerem Lachkar

Summary

The document describes an LSTM trading model using RSI, MACD, a ratio of two exponential moving averages, and recent returns as inputs over a short history. It assigns separate long and short outputs based on whether a hypothetical trade reaches a take-profit or stop-loss threshold within a set time window, or reaches neither. The model uses linear output neurons and mean squared error, then opens a position when a predicted value exceeds a chosen threshold.

The author reports strong results during the optimization period but much less consistent performance afterward, suggesting overfitting despite regularization. They question whether the target design is suitable, given that many observations have neither target reached, and ask whether a reward-to-risk measure would be more appropriate. The document offers no specialist resolution or measured evidence comparing alternatives. It is a description of a modeling problem, not a validated strategy; transaction costs, leakage controls, and other backtest details are not discussed.

Key ideas

  • The described LSTM uses technical indicators and recent returns across a local sequence of observations.
  • Separate long and short targets encode take-profit, stop-loss, or no-trigger outcomes within a fixed horizon.
  • The author reports strong optimization-period results and weaker performance in a later test period.
  • The document raises concerns about target design and class imbalance but does not resolve them.
  • The reported backtest does not establish that the model will perform reliably in live trading.

Tags

Full text
# Technical Analysis Indicators as input of a LSTM Neural Network ? Need advices


# Technical Analysis Indicators as input of a LSTM Neural Network ? Need advices












I'm trying to make a trading strategy by training a LSTM neural network with input features being typical technical analysis metrics: RSI, MACD, ema ratio (EMA 50 divided by EMA 200, so that the NN can "see" when the 2 EMA cross : the ratio = 1), and the returns in %. I input a local history of 10 past periods (it's a random choice a made).

My question is about the way I compute the output neurons. I relatively new in Machine Learning so I'm not really sure about the choices I make.

At the moment, what I made is 2 output neurons, one for each order side : long and short. These 2 neurons have the labels:

- 1 if an order taken at the time of the input would lead to reach a take profit in the of a given percentage given before.

- -1 if it reaches the stop loss (also given).

- 0 if it does not reach TP or SL within a given timeframe, that I set to 6 hours (I'm basically telling the NN that an entry signal based on green light from T.A. indicators should be realized within 6 hours, otherwise, these indicators would have less/no meaning for what's going on after)

I created the model as follows :

```
model = Sequential()
model.add(LSTM(200, activation='relu', return_sequences=True, input_shape=(X.shape[1], X.shape[2],), kernel_regularizer=regularizers.l2(0.001)))
model.add(LSTM(100, input_shape=(X.shape[1], X.shape[2]), kernel_regularizer=regularizers.l2(0.001)))
model.add(Dense(2, activation='linear', kernel_regularizer=regularizers.l2(0.0001)))
model.compile(optimizer='adam', loss='mse')

dumb_train_loss, dumb_train_acc = model.evaluate(train_x, train_y)
dumb_test_loss, dumb_test_acc = model.evaluate(test_x, test_y)

Log.i(f'Before training: training period loss = {dumb_train_loss}, test period loss = {dumb_test_loss}')

model.fit(train_x, train_y, epochs=40, batch_size=10, verbose=1)
```

I backtest a strategy based on that: at all times, if the prediction of the model has one neuron (i.e., one order side) > 0.8 (close to 1, i.e., close to reaching a TP within the next 6 hours), then we take an order of that side of size 5% of the initial equity.

Despite the very good results in the optimisation period (on the left of the vertical dashed line, close to 100% of the trades are win), the results are much more random after (test period, on the right of the line). My model is therefore clearly overfitted despite the regularisation. I'm definitely not sure about the whole model structure as well and if it makes sense for my problem.

My question is mainly regarding the way I compute the output neurons, I feel that it could be much more precise. Would it be preferable to have a reward / risk ratio within 6 hours (like a Sharpe Ratio) instead of these "labels". My problem is not really a classification problem (because 4/5 of the times both outputs are 0/0, no SL or TP is reached within 6 hours) while I heard that a classification problem needed to have proper labels for all inputs, and all classes being quite equally distributed in the training set, which is not the case at all here.

If a specialist in Machine Learning could give some advices, that would be truly appreciated :)

Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.