Skip to main content

What you’ll build

In this tutorial, you will build a simple crossover strategy that:
  1. collects MARK_PRICE updates,
  2. maintains one fast and one slow moving average,
  3. turns a crossover into a directional trade signal,
  4. submits one order through the signed and encrypted request path,
  5. tracks the resulting order updates.

Prerequisites

Step 1: Start from the existing bot skeleton

Reuse the same client, signing, encryption, and lifecycle-tracking setup from the first bot tutorial. This advanced lesson changes the signal logic, not the submission transport.

Step 2: Subscribe to one mark-price feed

Start with one symbol such as ETHP. Use the realtime client to subscribe to MARK_PRICE and append each new price to a rolling buffer. Keep the first version narrow:
  1. one symbol,
  2. one strategy identifier,
  3. one active position direction at a time.

Step 3: Define the two moving windows

Pick one fast window and one slow window, for example:
  • fast: 10 recent prices
  • slow: 30 recent prices
Do not emit any signal until the slow window is fully populated. Once both windows are warm:
  1. compute the fast average,
  2. compute the slow average,
  3. compare the current relationship with the previous relationship.

Step 4: Turn the crossover into one trade action

Use one simple rule:
  • if the fast average crosses above the slow average, prepare one long-biased action
  • if the fast average crosses below the slow average, prepare one short-biased action
Do not fire a new request on every update. Only react when the relationship changes.

Step 5: Keep the first execution path disciplined

Before sending the order:
  1. cancel or replace stale working orders for the same strategy,
  2. keep size aligned to minimum-order-size increments,
  3. keep the request inside the product price-band and notional constraints,
  4. confirm the new exposure still respects margin rules.
For the first version, use one small order size and one explicit price decision rather than trying to manage layered quotes.

Step 6: Submit through the normal request path

When a crossover appears:
  1. build the order request,
  2. sign it with EIP-712,
  3. encrypt it for the operator,
  4. submit it to the request endpoint,
  5. record the returned receipt metadata.
Do not duplicate the signing or encryption procedure inside this tutorial. Reuse the existing helpers from the earlier bot lesson.

Step 7: Track the order outcome

After submission:
  1. listen for ORDER_UPDATE,
  2. correlate updates by order identity,
  3. record whether the signal became a fill, cancel, or rejection,
  4. store the fast and slow averages that produced the signal.
That final record is the minimum useful audit trail for later tuning.

Step 8: Stop after one clean crossover cycle

This tutorial is complete when you have one full cycle:
  1. warm the windows,
  2. detect a crossover,
  3. submit one order,
  4. observe the resulting lifecycle updates,
  5. stop the strategy cleanly.

Next routes

Last modified on April 13, 2026