Skip to content
All library documents

How Signal Changes Translate into Trading Orders

Article Quant Q&A · Author: toy

Summary

The document explains how a binary strategy signal becomes a sequence of trading orders in a simple moving-average backtest. The signal represents the desired position: zero means holding no shares, while one means holding a share. Taking the signal's first difference converts changes in that target into order events.

A change from zero to one produces a positive order to enter the position; a change from one to zero produces a negative order to exit. When the signal remains unchanged, the difference is zero, so no new order is needed. The example clarifies the meaning of the positions series, but does not cover transaction costs, portfolio sizing, short positions, or how orders are filled. Its interpretation assumes that the binary signal maps directly to the intended holding in one share.

Key ideas

  • The binary signal represents the desired holding, with zero indicating no shares and one indicating a share.
  • Differencing the signal turns changes in desired holdings into order events.
  • A positive change indicates a buy, while a negative change indicates a sale.
  • A zero difference means the target position has not changed and generates no new order.

Tags

Full text
# Why the diff of signal is called positions and what does it mean in backtesting?


# Why the diff of signal is called positions and what does it mean in backtesting?












I'm trying to learn Backtesting 101. I found this example which is very simple but I do not quite understand some of the terms. I understand Moving Average algorithm which is to measure trends or to smooth the graph. But it the code, the author said `# Take the difference of the signals in order to generate actual trading orders` which I don't quiet understand what this means? Because from the code the `signals` is going to be between `0` and `1` why does it need o be diff again?

Here's the code in Python Pandas

```
def generate_signals(self):
  # Create DataFrame and initialise signal series to zero
  signals = pd.DataFrame(index=self.bars.index)
  signals['signal'] = 0

  # Create the short/long simple moving averages
  signals['short_mavg'] = pd.rolling_mean(bars['Adj Close'], self.short_window, min_periods=1)
  signals['long_mavg'] = pd.rolling_mean(bars['Adj Close'], self.long_window, min_periods=1)

  # When the short SMA exceeds the long SMA, set the ‘signals’ Series to 1 (else 0)
  signals['signal'][self.short_window:] = np.where(signals['short_mavg' [self.short_window:] > signals['long_mavg'][self.short_window:], 1, 0)

  # Take the difference of the signals in order to generate actual trading orders
  signals['positions'] = signals['signal'].diff()
  return signals
```

Becuase that `positions` is going to be used in the actual backtesting.

## Answer by Kiwiakos (score 5, accepted)

https://quant.stackexchange.com/a/22925

My understanding, in that context, is that signal indicates that you want to hold a share (signal is 1) or hold no shares (signal is zero). Therefore taking the diff will tell you if you want to buy (signal zero to 1, diff is 1), sell (signal 1 to zero, diff is -1) or do nothing (signal stays at zero or stays at 1, diff is zero).

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.