Basics of Algorithmic Trading: Concepts, Examples, and AlgoWay Opportunities

How the cTrader Connector Logic Works

The cTrader handler in our project implements two primary modes of operation:

  • Reverse Mode
  • Hedge Mode

Below is a detailed explanation of their logic and differences.

Reverse Mode
Objective:

To completely close existing positions for a specified instrument and, if required, open a new position.

Logic:
  • The handler does not automatically open an opposite position after closing. It merely closes the current positions to free up assets.
  • If there are no existing positions, the handler performs no operations.
Key Steps:
  1. Check Existing Positions:
    • If there are open positions for the symbol (LONG or SHORT), they will be closed using the close_order function.
    • The handler follows a sequential closing mechanism until all positions are liquidated.
  2. Open New Position:
    • If a signal is received to open a new position after closing old ones, the system creates the new position via place_order.
When to Use:

This mode is ideal for strategies requiring all existing positions to be closed to prevent overlapping assets. For example, a trader wants to ensure no old positions are held before opening a new order.

Hedge Mode
Objective:

To open a new position without closing existing ones.

Logic:
  • When a signal to open a position is received, the handler does not close existing positions. It simply adds the new position to the current ones.
  • For instance, if a LONG position is already open, and a SHORT signal is received, the handler will open a new SHORT position while keeping the existing LONG.
Key Steps:
  1. Check Existing Positions:
    • The system analyzes current open positions but performs no closure operations.
  2. Open New Position:
    • A new position is created using the place_order function regardless of the currently open positions.
When to Use:

This mode is suitable for strategies requiring simultaneous positions in both directions (LONG and SHORT). For example, hedging risk where the trader wants to safeguard open positions.

Differences Between Reverse and Hedge Modes
Characteristic Reverse Mode Hedge Mode
Closing existing positions Yes No
Opening new positions Only after closing Always
Suitable for position reversal Yes No
Suitable for hedging No Yes
Examples
Example 1: Reverse Mode

Scenario:

Current Position: LONG 1 lot on EURUSD.
Signal: SHORT 1 lot.

Result:

The handler closes the existing LONG position. A new SHORT position will not be opened automatically (unless specified separately).

Example 2: Hedge Mode

Scenario:

Current Position: LONG 1 lot on EURUSD.
Signal: SHORT 1 lot.

Result:

The handler opens a new SHORT position without closing the LONG position.

Additional Notes on Stop-Loss and Take-Profit

Our connector exclusively uses market orders to ensure fast and reliable operation. However, the cTrader API does not support setting Stop-Loss (SL) or Take-Profit (TP) values directly when creating market orders.

Recommendation:
  • To use Stop-Loss and Take-Profit, define them in your TradingView strategy. The strategy should send signals to open and close positions according to the specified parameters.
Summary
  • Reverse Mode: Used for position reversal (closing before opening). This logic ensures all assets are freed before initiating a new trade.
  • Hedge Mode: Allows simultaneous holding of opposing positions, offering flexibility for complex strategies.

This logic makes our connector a more reliable and versatile tool for integrating TradingView and cTrader. Users can select the appropriate mode based on their strategy.