Build Your First Algorithm in 10 Minutes
In this tutorial, you'll create a simple moving average crossover strategy from scratch. This is perfect for beginners and takes about 10 minutes to complete.
What You'll Build
A momentum strategy that:
- Buys when a fast moving average (20-period) crosses above a slow moving average (50-period)
- Sells when the fast MA crosses below the slow MA
- Uses 2% of capital per trade
- Sets a 2% stop loss and 4% take profit (1:2 risk-reward)
- Trades NSE:RELIANCE on a 15-minute timeframe
Prerequisites
- An x3Algo account (sign up at x3algo.com)
- Basic understanding of moving averages (optional but helpful)
Step 1: Create New Algorithm
- Log in to your x3Algo dashboard
- Navigate to Algorithms in the sidebar
- Click the Create Algorithm button
- You'll see a 5-step wizard
Step 2: Configure Basic Information
Fill in the basic details:
Strategy Name
My First MA Crossover
Description
Simple moving average crossover strategy for learning purposes
Strategy Type
Select Momentum from the dropdown. Momentum strategies capitalize on trending markets.
Timeframe
Select 15m (15 minutes). This means:
- Your algorithm checks for signals every 15 minutes
- Each candle represents 15 minutes of price action
- Good balance between signal frequency and noise reduction
Trading Symbols
Enter:
NSE:RELIANCE
Always use the format EXCHANGE:SYMBOL:
- NSE: National Stock Exchange (Indian equities)
- BSE: Bombay Stock Exchange
- MCX: Multi Commodity Exchange
- NCDEX: National Commodity & Derivatives Exchange
Examples: NSE:TCS, MCX:GOLD, BSE:SENSEX
Click Next to proceed to Step 2.
Step 3: Configure Position Sizing
Position sizing determines how much capital to risk per trade.
Method
Select Percentage-based from the dropdown.
Percentage
Enter:
2
This means each trade will use 2% of your available capital.
- Conservative: Protects your capital from large losses
- Sustainable: Allows for 50 consecutive losses before account depletion
- Professional: Used by many institutional traders
- Flexible: Automatically adjusts as your account grows
For beginners, never risk more than 2-3% per trade.
Example Calculation
- Account balance: ₹100,000
- Position size: 2% = ₹2,000
- If RELIANCE is trading at ₹2,000/share
- Quantity: ₹2,000 / ₹2,000 = 1 share
Click Next to proceed to Step 3.
Step 4: Configure Entry Conditions
Entry conditions define when to open a position.
Position Type
Select Both (allows both long and short positions).
Logical Operator
Select AND (all conditions must be true).
Add First Condition
Click Add Condition and configure:
For Long Entry (Buy Signal):
- Type: Indicator vs Indicator
- Indicator 1: SMA (Simple Moving Average)
- Period 1: 20
- Operator: crosses above
- Indicator 2: SMA
- Period 2: 50
This creates a buy signal when the 20-period SMA crosses above the 50-period SMA (bullish crossover).
Confirmation Candles
Set to:
1
This waits for 1 candle to confirm the signal, reducing false signals.
- 0 candles: Enter immediately (more signals, more false positives)
- 1-2 candles: Balanced approach (recommended for beginners)
- 3+ candles: Very conservative (fewer signals, higher quality)
Time Filters (Optional)
For now, leave this empty to trade all day. You can add time filters later to trade only during specific hours.
Click Next to proceed to Step 4.
Step 5: Configure Exit Conditions
Exit conditions protect your profits and limit losses.
Stop Loss
Click Configure Stop Loss:
- Type: Percentage
- Percentage: 2
This exits the trade if price moves 2% against you.
Example:
- Entry price: ₹2,000
- Stop loss: ₹2,000 × (1 - 0.02) = ₹1,960
- Maximum loss: ₹40 per share
Take Profit
Click Configure Take Profit:
- Type: Risk-Reward Ratio
- Ratio: 2
This automatically calculates take profit at 2× your stop loss distance.
Example:
- Entry price: ₹2,000
- Stop loss distance: 2% = ₹40
- Take profit distance: 2 × ₹40 = ₹80
- Take profit price: ₹2,000 + ₹80 = ₹2,080
- Risk-reward: Risk ₹40 to make ₹80 (1:2 ratio)
- 1:1: Break even with 50% win rate
- 1:2: Profitable with 34% win rate (recommended minimum)
- 1:3: Profitable with 26% win rate (aggressive)
Always aim for at least 1:2 risk-reward ratio.
Exit on Opposite Signal
Enable this option:
- ☑️ Exit on opposite signal
- ☑️ Close immediately
This exits long positions when a short signal appears (and vice versa).
Click Next to proceed to Step 5.
Step 6: Configure Risk Parameters
Risk parameters provide portfolio-level protection.
Maximum Position Size
Enter:
10
This limits any single position to 10% of your capital, preventing over-concentration.
Maximum Daily Loss
Enter:
5
This is your circuit breaker. If you lose 5% of your account in a single day, the algorithm automatically pauses.
Example:
- Account: ₹100,000
- Daily loss limit: 5% = ₹5,000
- After losing ₹5,000 in one day, algorithm stops trading
- Prevents emotional revenge trading
- Protects capital during adverse market conditions
Maximum Open Positions
Enter:
3
This limits how many trades can run simultaneously, controlling overall exposure.
Risk-Reward Ratio
Enter:
2
This enforces a minimum 1:2 risk-reward ratio on all trades.
Click Complete to finish configuration.
Step 7: Test in Paper Mode
Before risking real money, test your algorithm with paper trading.
- Your algorithm is now in Stopped state
- Click the Settings icon next to your algorithm
- Under Execution Mode, select Paper Trading
- Click Save
- Click the Start button to activate your algorithm
- Uses real market data
- No real money at risk
- Tests your strategy logic
- Builds confidence before going live
- Identifies issues early
Run paper trading for at least 2-4 weeks before going live.
Step 8: Monitor Your Algorithm
View Dashboard
Navigate to the Dashboard to see:
- Active algorithms
- Open positions
- Recent trades
- Performance metrics
Check Signals
Go to Signals to see:
- When entry conditions were met
- Why signals were generated
- Which conditions triggered
Review Trades
Go to Trades to analyze:
- Entry and exit prices
- Profit/loss per trade
- Win rate
- Average profit vs average loss
Understanding the Results
After running for a few days, you'll see metrics like:
Win Rate
Win Rate: 45%
Percentage of profitable trades. For a 1:2 risk-reward strategy, 45% is profitable.
Profit Factor
Profit Factor: 1.8
Ratio of gross profit to gross loss. Above 1.5 is good, above 2.0 is excellent.
Total Trades
Total Trades: 12
Number of trades executed. Need at least 30-50 trades for statistical significance.
Total Profit/Loss
Total P&L: +₹2,450 (+2.45%)
Net profit or loss from all trades.
Common Issues and Solutions
No Signals Generated
Problem: Algorithm is running but not generating any signals.
Solutions:
- Check if market is open (NSE: 9:15 AM - 3:30 PM IST)
- Verify symbol is correct:
NSE:RELIANCE - Ensure algorithm status is Active (not Stopped or Paused)
- Check if moving averages have crossed recently (may take time)
Too Many Signals
Problem: Getting too many entry signals.
Solutions:
- Increase confirmation candles from 1 to 2-3
- Add additional filters (RSI, volume, trend confirmation)
- Use a longer timeframe (30m or 1h instead of 15m)
Losses Exceeding Expectations
Problem: Losing more than expected.
Solutions:
- Verify stop loss is configured correctly (2%)
- Check if slippage is high (use limit orders)
- Ensure risk-reward ratio is at least 1:2
- Review if market conditions suit your strategy (trending vs ranging)
Next Steps
Congratulations! You've created your first trading algorithm. Here's what to do next:
1. Run Backtests
Test your strategy against historical data:
- Go to Backtests
- Click New Backtest
- Select your algorithm
- Choose date range (last 6-12 months)
- Review performance metrics
Learn more: Backtesting Basics
2. Optimize Parameters
Experiment with different settings:
- Try different MA periods (10/30, 50/200)
- Test various timeframes (5m, 30m, 1h)
- Adjust risk-reward ratios (1:2, 1:3)
Learn more: Strategy Optimization
3. Add Complexity
Enhance your strategy:
- Add RSI filter to avoid overbought/oversold entries
- Include volume confirmation
- Implement trailing stops
Learn more: EMA Crossover Strategy
4. Go Live
When ready to trade with real money:
- Run paper trading for 2-4 weeks minimum
- Verify consistent profitability
- Start with small position sizes
- Monitor closely for first week
Learn more: Paper to Live Transition
Summary
You've learned how to:
- ✅ Create a new algorithm from scratch
- ✅ Configure all 5 steps (basic info, position sizing, entry, exit, risk)
- ✅ Test in paper mode
- ✅ Monitor performance
- ✅ Troubleshoot common issues
Key Takeaways:
- Start simple (moving average crossover is a great first strategy)
- Use proper position sizing (2% per trade)
- Always set stop losses (2% maximum loss)
- Aim for favorable risk-reward (minimum 1:2)
- Test thoroughly before going live (paper trading for 2-4 weeks)
Ready to build more sophisticated strategies? Check out the EMA Crossover Strategy tutorial next!