Skip to main content
Skip to main content
Version: 1.0 (Current)

Create Algorithm

Create a new trading algorithm in draft status.

Endpoint

POST /api/trading/algorithms

Authentication

Requires authentication via Bearer token.

Authorization: Bearer <access_token>

Request

Headers

Content-Type: application/json
Authorization: Bearer <access_token>

Body

{
"name": "My Trading Strategy",
"strategyType": "momentum",
"timeframe": "15m",
"symbols": ["NSE:RELIANCE", "NSE:TCS"],
"positionSizing": {
"method": "percentage",
"value": 2
},
"entryConditions": {
"positionType": "long",
"logicalOperator": "AND",
"conditions": [
{
"type": "indicator_value",
"indicator": "RSI",
"period": 14,
"comparison": "below",
"value": 30
}
]
},
"exitConditions": {
"stopLoss": {
"type": "percentage",
"value": 2
},
"takeProfit": {
"type": "percentage",
"value": 4
}
},
"riskParameters": {
"maxPositionSize": 10,
"stopLoss": 2,
"takeProfit": 4,
"maxDailyLoss": 5,
"maxOpenPositions": 3,
"riskRewardRatio": 2
},
"executionSettings": {
"mode": "paper",
"exchange": "NSE",
"orderType": "market"
}
}

Parameters

FieldTypeRequiredDescription
namestringNoAlgorithm name (auto-generated if not provided)
strategyTypestringNoStrategy type: scalping, swing, position, arbitrage, market_making, momentum, mean_reversion
timeframestringNoTimeframe: 1m, 5m, 15m, 30m, 1h, 4h, 1d, 1w
symbolsarrayNoTrading symbols in EXCHANGE:SYMBOL format
positionSizingobjectNoPosition sizing configuration
entryConditionsobjectNoEntry conditions configuration
exitConditionsobjectNoExit conditions configuration
riskParametersobjectNoRisk management parameters
executionSettingsobjectNoExecution settings

Note: All fields are optional for draft algorithms. Use the Complete Algorithm endpoint (coming soon) endpoint to validate and activate.

Response

Success (201 Created)

{
"id": "507f1f77bcf86cd799439011",
"userId": "507f191e810c19729de860ea",
"name": "My Trading Strategy",
"status": "draft",
"active": false,
"strategyType": "momentum",
"timeframe": "15m",
"symbols": ["NSE:RELIANCE", "NSE:TCS"],
"positionSizing": {
"method": "percentage",
"value": 2
},
"entryConditions": {
"positionType": "long",
"logicalOperator": "AND",
"conditions": [
{
"type": "indicator_value",
"indicator": "RSI",
"period": 14,
"comparison": "below",
"value": 30
}
]
},
"exitConditions": {
"stopLoss": {
"type": "percentage",
"value": 2
},
"takeProfit": {
"type": "percentage",
"value": 4
}
},
"riskParameters": {
"maxPositionSize": 10,
"stopLoss": 2,
"takeProfit": 4,
"maxDailyLoss": 5,
"maxOpenPositions": 3,
"riskRewardRatio": 2
},
"executionSettings": {
"mode": "paper",
"exchange": "NSE",
"orderType": "market"
},
"performance": {
"totalTrades": 0,
"winningTrades": 0,
"losingTrades": 0,
"totalProfit": 0,
"totalLoss": 0,
"winRate": 0,
"profitFactor": 0,
"sharpeRatio": 0,
"maxDrawdown": 0,
"averageWin": 0,
"averageLoss": 0
},
"createdAt": "2024-01-15T10:30:00Z",
"updatedAt": "2024-01-15T10:30:00Z"
}

Location Header

Location: /api/trading/algorithms/507f1f77bcf86cd799439011

Errors

StatusCodeMessage
400VALIDATION_ERRORInvalid input data
401UNAUTHORIZEDAuthentication required
422UNPROCESSABLE_ENTITYInvalid configuration
429TOO_MANY_REQUESTSRate limit exceeded

Error Response Example

{
"error": {
"message": "Invalid timeframe",
"code": "VALIDATION_ERROR",
"status": 400,
"details": {
"field": "timeframe",
"value": "invalid",
"allowedValues": ["1m", "5m", "15m", "30m", "1h", "4h", "1d", "1w"]
}
}
}

Examples

Minimal Draft Algorithm

Create a minimal draft algorithm with just a name:

Request:

curl -X POST https://api.x3algo.com/api/trading/algorithms \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"name": "My First Algorithm"
}'

Response:

{
"id": "507f1f77bcf86cd799439011",
"name": "My First Algorithm",
"status": "draft",
"active": false,
"createdAt": "2024-01-15T10:30:00Z"
}

Complete Algorithm Configuration

Create a fully configured algorithm:

JavaScript:

const algorithm = {
name: "RSI Momentum Strategy",
strategyType: "momentum",
timeframe: "15m",
symbols: ["NSE:RELIANCE", "NSE:TCS", "NSE:INFY"],

positionSizing: {
method: "risk_based",
riskPercentage: 1,
stopLossPercentage: 2
},

entryConditions: {
positionType: "long",
logicalOperator: "AND",
confirmationCandles: 1,
conditions: [
{
type: "indicator_value",
indicator: "RSI",
period: 14,
comparison: "below",
value: 30
},
{
type: "indicator_indicator",
indicator1: "EMA",
period1: 9,
indicator2: "EMA",
period2: 21,
comparison: "crosses_above"
}
]
},

exitConditions: {
stopLoss: {
type: "percentage",
value: 2
},
takeProfit: {
type: "risk_reward",
ratio: 3
},
trailingStop: {
enabled: true,
type: "percentage",
value: 1,
activationThreshold: 2
}
},

riskParameters: {
maxPositionSize: 10,
stopLoss: 2,
takeProfit: 6,
maxDailyLoss: 5,
maxOpenPositions: 3,
riskRewardRatio: 3
},

executionSettings: {
mode: "paper",
exchange: "NSE",
orderType: "limit",
slippage: 0.1
}
}

const response = await fetch('https://api.x3algo.com/api/trading/algorithms', {
method: 'POST',
headers: {
'Authorization': `Bearer ${accessToken}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(algorithm)
})

const created = await response.json()
console.log('Algorithm ID:', created.id)

Python:

import requests

algorithm = {
'name': 'RSI Momentum Strategy',
'strategyType': 'momentum',
'timeframe': '15m',
'symbols': ['NSE:RELIANCE', 'NSE:TCS', 'NSE:INFY'],

'positionSizing': {
'method': 'risk_based',
'riskPercentage': 1,
'stopLossPercentage': 2
},

'entryConditions': {
'positionType': 'long',
'logicalOperator': 'AND',
'confirmationCandles': 1,
'conditions': [
{
'type': 'indicator_value',
'indicator': 'RSI',
'period': 14,
'comparison': 'below',
'value': 30
}
]
},

'exitConditions': {
'stopLoss': {
'type': 'percentage',
'value': 2
},
'takeProfit': {
'type': 'risk_reward',
'ratio': 3
}
},

'riskParameters': {
'maxPositionSize': 10,
'stopLoss': 2,
'takeProfit': 6,
'maxDailyLoss': 5,
'maxOpenPositions': 3,
'riskRewardRatio': 3
},

'executionSettings': {
'mode': 'paper',
'exchange': 'NSE',
'orderType': 'limit',
'slippage': 0.1
}
}

response = requests.post(
'https://api.x3algo.com/api/trading/algorithms',
headers={'Authorization': f'Bearer {access_token}'},
json=algorithm
)

created = response.json()
print('Algorithm ID:', created['id'])

Next Steps

After creating an algorithm:

  1. Update Configuration: Use Update Algorithm to modify settings
  2. Complete Algorithm: Use Complete Algorithm endpoint (coming soon) to validate and transition from draft
  3. Start Algorithm: Use Start Algorithm to begin trading
  4. Run Backtest: Test the strategy with historical data