Objective: To develop and validate a risk algorithm (QStroke) to estimate risk of stroke or transient ischaemic attack in patients without prior stroke or transient ischaemic attack at baseline; to compare (a) QStroke with CHADS 2 and CHA2DS2VASc scores in patients with atrial fibrillation and (b) the performance of QStroke with the Framingham stroke score in the full population free of stroke or transient ischaemic attack. Design: Prospective open cohort study using routinely collected data from general practice during the study period 1 January 1998 to 1 August 2012. Setting: 451 general practices in England and Wales contributing to the national QResearch database to develop the algorithm and 225 different QResearch practices to validate the algorithm. Participants: 3.5 million patients aged 25-84 years with 24.8 million person years in the derivation cohort who experienced 77 578 stroke events. For the validation cohort, we identified 1.9 million patients aged 25-84 years with 12.7 million person years who experienced 38 404 stroke events. We excluded patients with a prior diagnosis of stroke or transient ischaemic attack and those prescribed oral anticoagulants at study entry. Main outcome measures: Incident diagnosis of stroke or transient ischaemic attack recorded in general practice records or linked death certificates during follow-up. Risk factors: Self assigned ethnicity, age, sex, smoking status, systolic blood pressure, ratio of total serum cholesterol to high density lipoprotein cholesterol concentrations, body mass index, family history of coronary heart disease in first degree relative under 60 years, Townsend deprivation score, treated hypertension, type 1 diabetes, type 2 diabetes, renal disease, rheumatoid arthritis, coronary heart disease, congestive cardiac failure, valvular heart disease, and atrial fibrillation Results: The QStroke algorithm explained 57% of the variation in women and 55% in men without a prior stroke. The D statistic for QStroke was 2.4 in women and 2.3 in men. QStroke had improved performance on all measures of discrimination and calibration compared with the Framingham score in patients without a prior stroke. Among patients with atrial fibrillation, levels of discrimination were lower, but QStroke had some improved performance on all measures of discrimination compared with CHADS2 and CHA2DS 2VASc. Conclusion: QStroke provides a valid measure of absolute stroke risk in the general population of patients free of stroke or transient ischaemic attack as shown by its performance in a separate validation cohort. QStroke also shows some improvement on current risk scoring methods, CHADS 2 and CHA2DS2VASc, for the subset of patients with atrial fibrillation for whom anticoagulation may be required. Further research is needed to evaluate the cost effectiveness of using these algorithms in primary care.