Write Your First Pine Script Strategy in AlsaTrade
A step-by-step tutorial to create, backtest, and deploy a simple moving average crossover strategy using AlsaTrade's Pine Script IDE.
AlsaTrade includes a full Pine Script IDE — you can write strategies, backtest them against historical data, and deploy them for live auto-execution. No need to switch between TradingView and your trading platform.
This tutorial walks you through creating a simple SMA crossover strategy from scratch.
What You’ll Build
A moving average crossover strategy that:
- Buys when the fast SMA (10-period) crosses above the slow SMA (50-period)
- Sells when the fast SMA crosses below the slow SMA
- Uses percentage-based position sizing (10% of balance per trade)
Step 1: Open the Pine Script IDE
Navigate to the Pine Script section in AlsaTrade. You’ll see the Monaco-based code editor with syntax highlighting, auto-completion, and error detection.
Step 2: Write the Strategy
Here’s the complete Pine Script for our crossover strategy:
//@version=5
strategy("SMA Crossover", overlay=true)
// Inputs
fastLength = input.int(10, "Fast SMA Length", minval=1)
slowLength = input.int(50, "Slow SMA Length", minval=1)
// Calculate indicators
fastSMA = ta.sma(close, fastLength)
slowSMA = ta.sma(close, slowLength)
// Plot on chart
plot(fastSMA, "Fast SMA", color=color.green)
plot(slowSMA, "Slow SMA", color=color.red)
// Entry conditions
longCondition = ta.crossover(fastSMA, slowSMA)
shortCondition = ta.crossunder(fastSMA, slowSMA)
// Execute trades
if longCondition
strategy.entry("Long", strategy.long)
if shortCondition
strategy.close("Long")
Paste this into the editor. You should see the syntax highlighting activate immediately — keywords in blue, strings in green, numbers in orange.
Step 3: Validate
Click the Validate button in the toolbar. AlsaTrade’s Pine parser checks your script for syntax errors and unsupported functions. You’ll see a green checkmark if everything is valid.
The IDE supports 17 built-in indicators: SMA, EMA, RSI, MACD, Bollinger Bands, SuperTrend, ATR, Stochastic, ADX, VWAP, CCI, MFI, Williams %R, Ichimoku, and crossover/crossunder conditions.
Step 4: Backtest
Click Run Backtest and configure:
- Symbol: BTCUSDT
- Timeframe: 1H (1 hour)
- Period: Last 6 months
- Initial capital: $10,000
- Position size: 10% of balance
The backtesting engine runs your strategy against historical data and generates:
- Equity curve: Visual chart of your portfolio value over time
- Trade list: Every entry and exit with prices, P&L, and duration
- Performance metrics: Win rate, Sharpe ratio, max drawdown, profit factor
Reading Backtest Results
Key metrics to evaluate:
| Metric | What It Tells You |
|---|---|
| Win Rate | Percentage of profitable trades |
| Profit Factor | Gross profit / gross loss (above 1.0 = profitable) |
| Max Drawdown | Largest peak-to-trough decline |
| Sharpe Ratio | Risk-adjusted return (above 1.0 is acceptable, above 2.0 is good) |
| Calmar Ratio | Annual return / max drawdown |
Step 5: Optimize (Optional)
If your initial results are promising, use the Parameter Optimizer to find better inputs:
- Set ranges for your parameters (e.g., fast SMA from 5 to 20, slow SMA from 30 to 100)
- Run the grid search optimization
- View the Optimization Heatmap to see which parameter combinations perform best
- Select the best parameters and re-run the backtest
A word of caution: Over-optimization leads to curve-fitting. Always validate optimized parameters with Walk-Forward Analysis — this splits your data into in-sample (training) and out-of-sample (testing) periods to detect overfitting.
Step 6: Deploy for Live Trading
Once you’re satisfied with backtest results:
- Click Create Trigger in the Pine Trigger Panel
- Select your exchange (Binance, Bybit, or Alpaca)
- Configure position sizing and the trading pair
- Enable Auto-Execute
Your strategy now runs continuously. When the SMA crossover condition is met on live market data, AlsaTrade executes the trade on your exchange account automatically.
Step 7: Monitor
The Signals panel shows every signal your strategy generates:
- Signal type (entry/exit)
- Timestamp
- Execution status (pending, executed, failed)
- Exchange order ID and fill price
You can pause or stop your strategy at any time without losing the configuration.
Next Steps
Once you’re comfortable with simple crossovers, try:
- Adding RSI confirmation: Only enter when RSI is below 30 (oversold) for longs
- Multiple timeframes: Use higher-timeframe trend direction to filter signals
- Stop-loss and take-profit: Add risk management rules directly in your Pine Script
- Monte Carlo simulation: Run 1,000 simulated equity paths to understand the probability distribution of your strategy’s returns
The Pine Script IDE gives you a complete workflow — from idea to backtest to live execution — all within AlsaTrade.
Ready to start trading?
Create a free account and connect your exchange in minutes.
Get Started Free