How to Build an AI Sales Forecast Generator
Generate accurate sales forecasts using AI analysis of pipeline and historical data.
Jay Banlasan
The AI Systems Guy
Manual sales forecasts are guesses dressed up as data. An ai sales forecast generator automated with real pipeline data gives you numbers you can actually plan around. I use this to replace the spreadsheet forecasts that are wrong every quarter.
What You Need Before Starting
- Python 3.8+
- CRM API access
- pandas and numpy installed
- SQLite for storing forecast history
Step 1: Gather Historical Data
Pull 12+ months of closed deal data. You need enough history for seasonal patterns.
import pandas as pd
from datetime import datetime, timedelta
def get_forecast_data(crm_client):
deals = crm_client.get_deals(
status=["won"],
date_range="last_18_months",
properties=["amount", "close_date", "deal_type", "source"]
)
df = pd.DataFrame(deals)
df["close_date"] = pd.to_datetime(df["close_date"])
df["month"] = df["close_date"].dt.to_period("M")
return df
Step 2: Build the Forecast Model
Use a weighted average with seasonality adjustment. Start simple.
def generate_forecast(historical_df, months_ahead=3):
monthly = historical_df.groupby("month")["amount"].sum().reset_index()
recent_avg = monthly.tail(3)["amount"].mean()
seasonal_factor = monthly.tail(12).head(3)["amount"].mean() / monthly["amount"].mean()
forecasts = []
for i in range(1, months_ahead + 1):
forecast_amount = recent_avg * seasonal_factor * (0.95 ** i)
forecasts.append({
"month": (datetime.now() + timedelta(days=30*i)).strftime("%Y-%m"),
"forecast": forecast_amount,
"confidence": max(0.5, 0.9 - (i * 0.1)),
})
return forecasts
Step 3: Add Pipeline Weighting
Factor in current pipeline deals weighted by stage probability.
STAGE_PROBABILITIES = {
"discovery": 0.10, "demo": 0.25,
"proposal": 0.50, "negotiation": 0.75,
}
def weighted_pipeline_forecast(open_deals):
total = 0
for deal in open_deals:
prob = STAGE_PROBABILITIES.get(deal["stage"], 0.1)
total += deal["amount"] * prob
return total
Step 4: Combine Both Models
Blend historical trend with pipeline-weighted forecast.
def combined_forecast(historical, pipeline_weighted):
return [{
"month": f["month"],
"amount": (f["forecast"] * 0.6) + (pipeline_weighted * 0.4),
} for f in historical]
Step 5: Track Accuracy
Compare past forecasts to actuals. This is how you improve.
def measure_accuracy(forecasts, actuals):
results = []
for f in forecasts:
actual = actuals.get(f["month"])
if actual:
error = abs(f["amount"] - actual) / actual
results.append({"month": f["month"], "error_pct": error * 100})
avg_error = sum(r["error_pct"] for r in results) / len(results)
print(f"Average forecast error: {avg_error:.1f}%")
What to Build Next
Add deal-level predictions. The aggregate forecast is good for planning, but deal-level scores help reps prioritize their time.
Related Reading
- Building Automated Sales Enablement Content - automated sales enablement content guide
- Building Automated Revenue Forecasting - automated revenue forecasting guide
- AI for Sales Pipeline Management - ai sales pipeline management
Want this system built for your business?
Get a free assessment. We will map every system your business needs and show you the ROI.
Get Your Free Assessment