This paper deals with the problem of optimal location and reallocation of battery energy storage systems (BESS) in direct current (dc) microgrids with constant power loads. The optimization model that represents this problem is formulated with two objective functions. The first model corresponds to the minimization of the total daily cost of buying energy in the spot market by conventional generators and the second to the minimization of the costs of the daily energy losses in all branches of the network. Both the models are constrained by classical nonlinear power flow equations, distributed generation capabilities, and voltage regulation, among others. These formulations generate a nonlinear mixed-integer programming (MINLP) model that requires special methods to be solved. A dc microgrid composed of 21-nodes with existing BESS is used for validating the proposed mathematical formula. This system allows to identify the optimal location or reallocation points for these batteries by improving the daily operative costs regarding the base cases. All the simulations are conducted via the general algebraic modeling system, widely known as the General Algebraic Modeling System (GAMS).