In the optic of high-frequency trading, most of the standard trading algorithms work on the principle of mid-price prediction or mid-price movement prediction. However a big drawback of this technique can be seen on the following picture. Clearly in case 1, there is an increase in the mid-price of the product, but since we need to enter the market at the ask price and exit at the bid price, we are actually incurring a loss in this transaction. In case 2, the movement of the price is large enough to cover the bid-ask spread and thus is a profitable transaction.

We have however diverted from this traditional approach and instead focused on predicting only those price movements which are substantial enough to cross the bid-ask spread. It is important to understand that since we only consider change in price that cross the bid-ask spread as a ”movement”, which would certainly generate profit, predicting ”no movement” does not mean that there was absolutely no movement in the price of the product. It just means that the movement was not substantial enough to cross the spread and thus was as useless for our model as no actual movement in the price.

I understand well that principle, but it is way difference how to code it in python considering the best bid, the best ask and the mid-price.

How do I find a signal, that is strong enough to overcome the spread cost? Is there a way that principle to be coded as a function in python?


My actual labelling strategy is this one, but it represents the case 1 of the above picture.

def label(true_values):
    alpha = 0.00013
    walk_steps = 10
    labels = [0 for _ in range(walk_steps)]
    for k in range(walk_steps, len(true_values)):
        prev_vals = np.mean(true_values[k - walk_steps:k + 1])
        next_vals = np.mean(true_values[k:k + walk_steps + 1])
        pred_prev_vals = np.mean(true_values[k - walk_steps:k + 1])

        true_label = 0
        if prev_vals > next_vals * (1.0 + alpha):
            true_label = 1
        elif prev_vals < next_vals * (1.0 - alpha):
            true_label = -1


    return labels

It can be more accurate to get a labelling strategy that can tell "Ok, if the midprice cross the spread, then I'll place a label 1 or -1 depending if it is going up or down and placing a label 0 otherwise."

We can build the code in considering the bidprice and askprice as variables.

  • 4
    $\begingroup$ So far your "principle" only says you need large moves to cross the spread. You need a way to predict these large moves. $\endgroup$ – LazyCat Dec 19 '18 at 12:05
  • $\begingroup$ @LazyCat Thanks for your answer! Yes, you are right, but I need to create a signal for supervised learning first. This is one of the main purpose of my question. $\endgroup$ – davegaut Dec 19 '18 at 19:24
  • 3
    $\begingroup$ If that's the purpose of the question, then that's how you should ask it: "How do I find a signal, that is strong enough to overcome the spread cost". However, for all practical purposes, it's too close to "How do I make a lot of money" and is discouraged in the forum. $\endgroup$ – LazyCat Dec 19 '18 at 20:03

In practice you try to cross the half-spread when your signal is bigger than half-spread so your goal is to find a stable signal that is bigger than half-spread in expectation.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.