Predicting price in nonlinear and dynamic financial markets has been a highly challenging task. In past studies, models dealing with time series are mostly Long Short-Term Memory (LSTM) models or Gated Recurrent Units (GRU). This thesis employs a self-attention mechanism for stock index prediction, integrating several strategies to enhance model performance such as positional encoding, rolling window validation, and early stopping. We compare the predictive outcomes using self-attention mechanism and those of the LSTM model. The results indicate superior performance of the self-attention mechanism model across varying time spans. Additionally, the proposed model exhibits strong predictive capabilities for stock indices, with mean absolute errors percentage ranging between 0.05% and 0.11% across different time windows.