2019 Elsevier Ltd This paper investigates a novel real-time stochastic multi-period management strategy of a virtual power plant (VPP) using a three-layer language protocol based on computer program compiler principle, which takes advantage of the availability of the battery storage in a VPP to maximize the revenue of the VPP over the entire trading horizon considering the predicted prices in each slice of that horizon. When the conventional scenario tree method is used to solve the computational complexity of the multi-period stochastic optimization problem, it may cause the problem to become intractable when the problem-scale increases. This paper proposes a deterministic lookahead approach that makes use of a novel formal language that implements a special formal grammar to manage the real-time control on the battery storages of the VPP. The control of charging/discharging of the battery storages, which is driven by the real-time spot price and the rolling price prediction, is formalized by using the proposed recursive grammar and the corresponding non-deterministic finite automaton (NFA). For validation, the proposed approach is applied to a simple three-bus and an adapted IEEE 14-bus test system. The simulation results show that the proposed method can obtain optimal revenue by managing each battery in the VPP to operate as a local generator, a local load, an energy buyer, an energy seller, or by being in an idle state when the battery is full or empty.