A New Vision on the Prosumers Energy Surplus Trading Considering Smart Peer-to-Peer Contracts

: A growing number of households beneﬁt from government subsidies to install renewable generation facilities such as PV panels, used to gain independence from the grid and provide cheap energy. In the Romanian electricity market, these prosumers can sell their generation surplus only at regulated prices, back to the grid. A way to increase the number of prosumers is to allow them to make higher proﬁt by selling this surplus back into the local network. This would also be an advantage for the consumers, who could pay less for electricity exempt from network tari ﬀ s and beneﬁt from lower prices resulting from the competition between prosumers. One way of enabling this type of trade is to use peer-to-peer contracts traded in local markets, run at microgrid ( µ G) level. This paper presents a new trading platform based on smart peer-to-peer (P2P) contracts for prosumers energy surplus trading in a real local microgrid. Several trading scenarios are proposed, which give the possibility to perform trading based on participants’ locations, instantaneous active power demand, maximum daily energy demand, and the principle of ﬁrst come ﬁrst served implemented in an anonymous blockchain trading ledger. The developed scheme is tested on a low-voltage (LV) microgrid model to check its feasibility of deployment in a real network. A comparative analysis between the proposed scenarios, regarding traded quatities and ﬁnancial beneﬁts is performed.


Introduction
In distribution systems, intelligent networks (known as 'smart grids') are implemented for encouraging energy savings and the integration of distributed generation sources, to help distribution utilities choose the optimal investment plans, achieve optimal operation of their systems, and to increase system efficiency. Other issues that need to be taken into consideration are the proliferation of prosumers and the creation of new consumer services. These research directions are in agreement with the European Union (EU) priorities, stated in the European Commission (EC) Communication published in 28 November 2018: renewable technologies, which must be the core of the new energy systems, smart grids, better energy efficiency, and low-carbon technologies. The fight against climate change is one of the five main topics of the EU extensive strategy for smart, sustainable and inclusive growth.
A microgrid can be defined as a LV network with loads, distributed energy resources (DER), and energy storage systems (ESS) connected to it, which can be operated in standalone or grid connected mode. The capacity of the DER considered in µG is in relatively small scale, but without universal agreement. It is mentioned as smaller than 100 kW by Huang et al. [1]. One of the main concepts in the active distribution network (ADN) is demand side management (DSM). Demand response (DR) as one of subcategories of DSM is defined by the EC as "voluntary changes by end-consumers of their usual electricity use patterns-in response to market signals". It is a shift of electricity usage in response to price signals or certain requests [2]. optimum pricing scheme for local electricity trading in µGs considering four particular priorities. In other words, the prosumers become the new actors in local electricity power market, considered as µM [19,20]. A different formulation of the PEST optimization follows a hierarchical framework considering the future energy price uncertainty in [21], information and communication technologies (ICT) in [22], and multi-layer architecture model in [23,24]. Paper [25] proposes a comprehensive analysis regarding the P2P communication architectures and highlights the performance of common protocols evaluated in accordance with IEEE 1547. 3-2007. In study [26], a P2P index optimization process was proposed. Here, a compromise regarding the balancing between the demand and generation in the LV network are identified. An incentive mechanism for PEST is presented in [27]. In the aforementioned paper, the authors consider three prices for prosumers profit maximization. Moreover, in [20,21,28], the authors proposed an evolutionary game theory-based approach for a dynamic modelling of the consumers (as buyers), in order to select the prosumers (as sellers). Thus, the evolutionary game theory was used for a dynamic modelling of the buyers for selecting sellers. The particular approach from [29] consider a Model Productive Control (MPC) method, for transactions only between two SSRES (prosumers), to avoid selling the surplus electricity production to classical traders or suppliers. This work considers the direct transactions without P2P contracts and blockchain technologies. Another category of the published papers regards the transactions of the PEST in the context of transactive energy in µGs [30][31][32]. The authors in [33] the transactions consider different preference of prices.

References
Path of Supply (S1)  [7,17] no no no no yes yes [11,12,25] yes no no no no yes [13] no no yes yes no yes [14,15] no no yes yes yes yes [16,23] yes no no yes no yes [18] no no yes no no no [20,26] no no yes no no yes [21,22,30] no no no no yes no [27] no no no yes yes no [28] no no yes no yes yes [29] no yes no no no yes [31] no yes no yes no no [32,33] no no no no yes yes Proposed approach yes yes yes yes yes yes A previous work of the authors, in [34], proposes only at principle level a particular approach for prosumers energy trading in µGs as an efficient P2P exchange based on the blockchain technology. Specifically, the algorithm solves a mathematical model for the latest challenges regarding both the ADN and the newest type of electricity market participants (prosumers) using virtual or crypto price as the transaction currency. In other words, this work emphasizes the capabilities and plausible benefits of P2P contracts for energy trading in local µGs from both prosumers and consumers perspectives. Taking into account that the Smart Meters are able to perform automatic energy transfer from the prosumers to the µG, the energy exchanged between the µGs peers, the utilities will be reduced, trough the minimization of active power losses. In the aforementioned context, the proposed algorithm implemented in the MATLAB environment is developed as a final energy market transaction platform for both the prosumers and traders.

A New Vision for Prosumer Energy Surplus Trading Algorithm
As described in the previous sections, an increasing number of consumers from LV EDN are using SSRES such as PV panels and wind turbines to gain energy independence by reducing the electricity need from the classic grid. This trend is driven by incentives provided by governments, such as subsidies for installing equipment or legislative provisions that allow them to sell the generation surplus back to the grid or to other consumers, thus becoming prosumers. The trading model that gives prosumers the ability to sell the surplus generation to the grid uses often-regulated tariffs, which results in low profits. The financial gain of the prosumers can increase if they get the possibility to sell energy to the consumers from their vicinity, at negotiated prices, via new trading tools, such as P2P contracts. Furthermore, to ensure equal access and transaction anonymity, the blockchain technology can be implemented to secure prosumer-consumer transactions.
The paper presents an algorithm for electricity transactions between prosumers and consumers belonging to the same local network or µG, using P2P contracts and, optionally, the blockchain technology.
In this section, prosumers and consumers' selection process, P2P pricing methodology, and the surplus trading mathematical model will be explained in detail.
The trading model implemented in the algorithm uses the following assumptions: • Transactions are settled by the local non-profit µG manager or aggregator using the consumer or prosumer merit order derived from the priority mechanism agreed for trading and data from the metering system.

•
The prosumer-consumer acquisition priority rules are the same for the entire µG.

•
To be able to acquire electricity from a prosumer Pk, a consumer Cj must have previously signed a P2P contract that includes the bilateral trading agreement, price, and other supplemental information, such as trading priority. • By default, any prosumer and prosumers in the µG have signed bilateral P2P trading contracts. In other words, any prosumer who has a generation surplus can theoretically sell electricity to any consumer in the microgrid. This setting is changeable to exclude any consumer from the trading process. • When a consumer is awarded a P2P contract, the power supplied by the prosumer will try to match the entire load of the consumer, within the limit of the available surplus, as in Equation (1). This setting is changeable to allow specified quantity requirements for each consumer.
where P trade,k,j,h is the power traded at hour h (h = 1, ..., nh), to consumer j (j = 1, ..., nc) by prosumer k (k = 1, ..., np); P j,h is the own consumption of prosumer k at hour h, and P surplus,k,h is the power surplus at hour h of the prosumer k.

•
The selling price of a prosumer is considered fixed for all trading intervals of a day. This assumption is made because only PV panels are used at this point as generation sources, and no storage capabilities are present in the µG. Thus, the local generation does not cover evening peak load or low consumption night hours, which would favor the application of differentiated tariffs.

•
The consumers in the network are generally one-phase, supplied through a four-wire three-phase network. Prosumers are supplying their surplus generation in the µG using a three-phase balanced connection point, as required by technical regulations for LV distribution systems [35]. • When transactions take place between certain prosumers and consumers, the prosumers will deliver and the consumer will receive electricity from the same grid.

•
If the surplus exceeds the local demand traded via P2P contracts, the µG market administrator will sell the untraded electricity back to the grid, at regulated tariffs.
The main input data needed by the algorithm refers to the consumption and local generation available in the µG. For this, two matrices are provided: matrix C = C (h, j) ∈ R nh×nc for consumptions and matrix G = G (h, k) ∈ R nh×np for generation. Generation will be available for prosumers for which, at the same hour h and prosumer k, G (h, k) > C (h, k), and the surplus available for trading follows as: computed into a matrix S = S (h, k) ∈ R nh×np . Also, for prosumers, the daily selling price is provided as a matrix PR = PR(h, k) ∈ R nhxnp , where any element PR(h, k) represents the selling price for a generic prosumer k at hour h.
This surplus will be sold to local consumers if P2P contracts exist, or to the grid. The local transactions are governed by a priority of supply mechanism agreed at the µG level, which describes the order in which any consumer Cj can acquire electricity from any prosumer Pk. In the algorithm, the complete list of priorities is encoded in a matrix M x = M x (k, j) ∈ 1 ℤ np×nc . A generic element M x (k, j) denotes the merit order of consumer j in the priority list of prosumer k, for the trading scenario x.
The trading algorithm proposed in the paper offers improved flexibility by considering two trading paradigms: consumer-driven, where the minimum price for consumers is sought, as in any traditional electricity market, and prosumer-driven, where the aim is to incentivize prosumer offers.
In the prosumer-driven scenarios, trading is performed to prioritize the selling of the generation surplus to consumers. The prosumer selling price is not considered, and the selling offers are fulfilled using the FCFS principle [34]. When trading is consumer-driven, the fulfillment of the consumer needs is sought first, and the prosumers with the lowest selling prices are prioritized for trading, as shown in Figure 1. and matrix G = G (h, k) ∈ ℝ nh×np for generation. Generation will be available for prosumers for which, at the same hour h and prosumer k, G (h, k) > C (h, k), and the surplus available for trading follows as: computed into a matrix S = S (h, k) ∈ ℝ nh× np . Also, for prosumers, the daily selling price is provided as a matrix PR = PR(h, k) ∈ ℝ nhxnp , where any element PR(h, k) represents the selling price for a generic prosumer k at hour h. This surplus will be sold to local consumers if P2P contracts exist, or to the grid. The local transactions are governed by a priority of supply mechanism agreed at the μG level, which describes the order in which any consumer Cj can acquire electricity from any prosumer Pk. In the algorithm, the complete list of priorities is encoded in a matrix Mx = Mx(k, j) ∈ ℤ np× nc . A generic element Mx(k, j) denotes the merit order of consumer j in the priority list of prosumer k, for the trading scenario x.
The trading algorithm proposed in the paper offers improved flexibility by considering two trading paradigms: consumer-driven, where the minimum price for consumers is sought, as in any traditional electricity market, and prosumer-driven, where the aim is to incentivize prosumer offers.
In the prosumer-driven scenarios, trading is performed to prioritize the selling of the generation surplus to consumers. The prosumer selling price is not considered, and the selling offers are fulfilled using the FCFS principle [34]. When trading is consumer-driven, the fulfillment of the consumer needs is sought first, and the prosumers with the lowest selling prices are prioritized for trading, as shown in Figure 1.  In each scenario, when the primary priorities are equal, a second dissociation criterion is applied. A description of these scenarios follows.  Five scenarios for assigning consumer priorities for P2P trading are available: In each scenario, when the primary priorities are equal, a second dissociation criterion is applied. A description of these scenarios follows. If this criterion is used, the prosumers will sell their electricity surplus to consumers using as ranking criterion the minimal network length between the generation and consumption locations. The consumer(s) with minimal network length from a given prosumer will be awarded first its available surplus, followed by other consumers in the ascending order of the connection distance. If two consumers are located at equal network lengths from a prosumer, the one with the higher power request will be preferred: This prioritization approach is modelling the true load flows occurring in an EDN, where the energy generated locally would predominantly supply the consumptions located at the closest locations, following the shortest path. Thus, the consumers most likely to physically receive the surplus are preferred for trading in this case.

Trading Priority Based on Consumer Hourly Demand-Scenario 2 (Prosumer-Driven)
In this scenario, the prosumers will sell their electricity surplus to consumers ranked in descending order of their trading offer or instantaneous consumption measured in the trading hour. If two consumers have equal power trading requirements at the same time, the one located closer to the seller prosumer will be preferred: Priority level 1 max(P h,j )Priority level 2 min(L j,k ) This prioritization is favoring for trading the consumers with the highest instantaneous demand, reducing the number of contracts fulfilled simultaneously by one prosumer. The use of this prioritization procedure minimizes the number of financial settlements required in each trading interval and in a day. In most cases, if a consumer is accepted for trading, its financial saving resulting from the lower electricity prices offered by prosumers, compared with standard regulated prices, is maximized. Larger profits can act as an incentive for consumers with high demand to be involved in the retail electricity market operated at microgrid level.

Trading Priority Based on Consumer Daily Demand-Scenario 3
In this scenario, the trading priority considers the total electricity demand of the consumers over 24 h. The consumers prioritized for receiving the prosumers' surplus will be those with the highest daily demand. For this purpose, the Ward hierarchical clustering method was applied.
The Ward method is an agglomerative hierarchical method that first assigns each observation to its own cluster and then groups adjacent clusters so that minimum variance within a cluster is obtained. The distance between two clusters a and b is computed with: where: d ab refers to the distance between cluster a and cluster b, c X is the mean of cluster X, is the Euclidean length, and n x is number of elements grouped in cluster X. The minimum variance criterion used by the Ward method is grouping the consumers in clusters of similar demand level and pattern over 24 h. In the algorithm, a maximum of five priority levels were considered for grouping, and within the same priority level, the criterion of the maximum instantaneous hourly demand was applied: Priority level 1 max(W j )Priority level 2 max(P h, j ) 3.4. Trading Priority Based on the Blockchain Technology-Scenario 4 The blockchain technology allows secure anonymous transactions that are fulfilled on the FCFS principle. This means that prosumers or the market administrator cannot choose the trading partners, and buying offers are fulfilled regardless of quantity and price, based only on the time of placement in the trading system.
The algorithm simulates this scenario by assigning randomly generated priorities for each consumer and prosumer, at each trading interval. In addition, as a rule, no two consumers can have equal trading priorities, as the time index of each offer is unique in the blockchain system. Thus, no second ranking criterion is required in this case.

Trading Priority Based on the Minimum Price for Consumers-Scenario 5
A standard market procedure is to accept trading offers based on the minimum selling price. This approach is modeled in the last scenario implemented in the algorithm, where consumers will acquire the electricity from prosumers in the ascending order of the selling process. The consumer offers will be fulfilled in the sequence taken from the blockchain system ledger, on the FCFS principle. If two prosumers have the same price offer, the highest traded quantity will be preferred.
Scenarios 1 and 2 require the knowledge of the length of the supply paths from each prosumer to each consumer. Based on these distances, the priority matrix M 1 = M 1 (k, j) ∈ ℤ np×nc is determined, where a generic element M 1 (k, j) denotes the trading priority of consumer j for prosumer k. Priorities are positive integer numbers. Lower distances between prosumer k and consumer j result in higher trading priority between the two peers. The highest priority level is 1.
Similarly, Scenario 3 requires the priority matrix M 2 = M 2 (k, j) ∈ ℤ np×nc where each element M 2 (k, j) denotes the trading priority of consumer j for prosumer k determined by the Ward clustering of consumers according to the daily energy demand. Higher demand is equivalent with higher priority. Scenarios 4 and 5 use the priority matrix M 3 = M 3 (k, j, h) ∈ ℤ np×ncxnh , where each element M 3 (k,'j, h) is the priority of consumer j for prosumer k at hour h, determined by the time index at which consumer j inputs its purchasing offer for hour h. An earlier time index is equivalent with higher priority. In all priority matrices, the highest priority level is 1. A higher value denotes a lower priority.
For the prosumer-driven scenarios, the surplus is computed using Equation (2) for each prosumer. Then, for each hour and prosumer, if the surplus exists, it is distributed to the consumers using one of the priorities from Scn 1 ÷ Scn 4 . For the consumer-driven scenario (Scn 5 ), at each hour h where surplus exists, it is distributed amongst the consumers using the priority determined by the blockchain system, prioritizing the prosumers with the lowest prices.
The results are stored in an acquisition matrix A = A (h, j, k) ∈ ℤ nh × nc x np , where each element A (h, j, k) represents the electricity sold at hour h to consumer j by prosumer k. Similarly, the financial settlement matrix F = F (h, j, k) ∈ ℤ nh × nc x np is computed, where each element F (h, j, k) represents the payment made by consumer j to prosumer k at hour h. The mathematical model used in determining the hourly surplus sold by prosumers to local consumers via a P2P contract is presented in Algorithm 1. Algorithm 1 uses Subroutine 1, Subroutine 2 and Subroutine 3.
Step 2. Load input data: the consumer load profile matrix C, the prosumer generation matrix G, the supply path lengths of the network, the prosumer price matrix PR.
Step 3. According to the selected scenario, compute priority matrices M 1 , M 2 , M 3 .
Step 4. Initialize the acquisition matrix A and financial settlement matrix F.
Step 6. Trading: for prosumer-driven scenarios for each hour h, h = 1..24 for each prosumer k, k = 1, . . . , np compute surplus S (h, k); if S (h, k) > 0 srp = S (h, k); find ix, the row index corresponding to prosumer k in matrix M 1 case Scenario 1-network length build a temporary consumer priority matrix MTC with two rows: Step 7. Compute the hourly and total electricity sold by prosumers to each consumer and the electricity traded hourly and daily by all prosumers, using matrices A and F.

Subroutine 1
Step 1. Read input data: the priority matrix MTC, acquisition matrix A, the financial settlement matrix F, the surplus to be distributed between consumers srp, the current prosumer index ix, the current hour h.
Step 2. Transpose matrix MTC into matrix MC.
Step 3. Sort matrix MC ascending by column 1, and for equal values in column 1, sort descending the corresponding values in column 2.
Step 4. Distribute the surplus srp: set initial consumer index: k = 0; while srp > 0 or (k < nc) k = k + 1; if the consumer has a P2P contract subtract the available surplus from its trading offer MC (k, 2) = MC (k, 2) − srp; if the surplus exceeds the consumer contract quantity: MC (k, 2) < 0 update remaining surplus: srp = − MC (k, 2); the contract from consumer k is fulfilled: MC (k, 2) = 0; else the contract from consumer k is partially fulfilled and the surplus is depleted: srp = 0; update matrix MTC for by subtracting from the served consumer demand the fulfilled contract; update acquisition matrix A for hour h according to the served consumer k, serving prosumer ix and traded quantity Subroutine 2 Step 1. Read input data: the priority matrix MTC, the acquisition matrix A, the financial settlement matrix F, the surplus to be distributed between consumers srp, the current prosumer index ix, the number of consumers nc, the current hour h.
Step 2. Transpose matrix MTC into matrix MC.
Step 3. Sort matrix MC descending by column 1, and for equal values in column 1, sort ascending the corresponding values in column 2.
Step 4. Distribute the surplus srp: set initial consumer index: k = 0; while srp > 0 or (k < nc) k = k + 1; if the consumer has a P2P contract subtract the available surplus from its trading offer MC (k, 1) = MC (k, 1) − srp; if the surplus exceeds the consumer contract quantity: MC (k, 1) < 0 update remaining surplus: srp = − MC (k, 1); the contract from consumer k is fulfilled: MC (k, 1) = 0; else the contract from consumer k is partially fulfilled and the surplus is depleted: srp = 0; update matrix MTC for by subtracting from the served consumer demand the fulfilled contract; update acquisition matrix A and financial settlement matrix F for hour h according to the served consumer k, serving prosumer ix and traded quantity.

Subroutine 3
Step 1. Read input data: the priority matrix for consumers MTC, the priority matrix for prosumers MTP, the acquisition matrix A, the financial settlement matrix F, hour h.
Step 2. Transpose matrix MTC into matrix MC, and matrix MTP into matrix MP Step 3. Sort matrix MC in ascending order of consumer priority (column 1). Keep original consumer order in vector idxk.
Step 4. Sort matrix MT ascending by column 1, and for equal values in column 1, sort descending the corresponding values in column 2. Keep original prosumer order in vector idxp.
Step 5. Compute the total surplus and consumption (st, ct).

Results
The proposed algorithm was tested on a real 0.4 kV EDN from the northeastern Romania. The network, whose one-line diagram is given in Figure 2, supplies 27 one-phase residential consumers using four-wire three-phase overhead lines, mounted on concrete poles. The distance between poles is of 40 m in average. This network is modeling a µG in which the prosumers located at buses 6, 7, 15, 21, and 27 want to sell their electricity surplus to other consumers. The case study considers that all the consumers in the µG are integrated in the local µM and can receive electricity from the prosumers through P2P contracts. The consumption and generation of the consumers and prosumers are modelled as 24-h profiles taken from the Smart Metering system installed in the µG. The consumption and generation profiles are provided in Table A1 and A2 from Appendix A. Table 2 presents the electricity surplus available for trading in the considered interval, for all the prosumers. This surplus will be distributed between the consumers or/and prosumers using one of the priority scenarios built in the proposed algorithm, as presented in the previous section.

Results
The proposed algorithm was tested on a real 0.4 kV EDN from the northeastern Romania. The network, whose one-line diagram is given in Figure 2, supplies 27 one-phase residential consumers using four-wire three-phase overhead lines, mounted on concrete poles. The distance between poles is of 40 m in average. This network is modeling a μG in which the prosumers located at buses 6, 7, 15, 21, and 27 want to sell their electricity surplus to other consumers. The case study considers that all the consumers in the μG are integrated in the local μM and can receive electricity from the prosumers through P2P contracts. The consumption and generation of the consumers and prosumers are modelled as 24-h profiles taken from the Smart Metering system installed in the μG. The consumption and generation profiles are provided in Table A1 and A2 from Appendix A. Table 2 presents the electricity surplus available for trading in the considered interval, for all the prosumers. This surplus will be distributed between the consumers or/and prosumers using one of the priority scenarios built in the proposed algorithm, as presented in the previous section.
The electricity price is considered constant for each prosumer over the trading interval, and is also given in Table 2. The regulated price at which consumers can buy electricity from the classic market operator has an average level of 0.72 MU/kWh, including taxes. On the other hand, the regulated price at which prosumers can sell electricity back to the grid is set at 0.235 MU/kWh for 2018 [36,37]. Thus, the selling prices for the local prosumers were set in the [0.40, 0.55] MU/kWh interval. As it can be seen from Table 2 and Figure 3, the local generation amounts to 22.8% from the consumption, in the 06:00-18.00 interval, and the hourly surplus does not exceed the demand in any  The electricity price is considered constant for each prosumer over the trading interval, and is also given in Table 2. The regulated price at which consumers can buy electricity from the classic market operator has an average level of 0.72 MU/kWh, including taxes. On the other hand, the regulated price at which prosumers can sell electricity back to the grid is set at 0.235 MU/kWh for 2018 [36,37]. Thus, the selling prices for the local prosumers were set in the [0.40, 0.55] MU/kWh interval. As it can be seen from Table 2 and Figure 3, the local generation amounts to 22.8% from the consumption, in the 06:00-18.00 interval, and the hourly surplus does not exceed the demand in any trading interval. This means that all the local generation will be sold in the local µM, through P2P contracts. The generation surplus from Table 2 will be distributed to the consumers with different priorities, according to each scenario. Table 3 presents the priorities computed according to the distance between prosumers and consumers (Scenario 1) and daily energy demand (Scenario 3). For Scenario 1, the priorities are straightforward, the consumers close to the prosumer having maximum trading priority. For instance, if prosumer 21 is used as reference, consumers 22 and 20 will have maximum trading priority, while consumer 14 or prosumer 15 (in case of deficit) will be the last in the priority list. In all scenarios, consumers or prosumers marked with X in Table 3 are excluded from trading. Bus 1 has no load, and each prosumer cannot sell to itself, because it is considered that it is selling on the market its surplus.  The priorities for Scenario 2 are computed in the same manner, but using the hourly demand values indicated in Table A1 from Appendix A as ranking criterion, instead of distance.
Scenarios 1-4, prosumer-oriented, do not take into account prosumer prices. The prosumer priority order is preset, to take into account the incentivization of specific prosumers, based on criteria particular to each µG, such as date of connection, generation technology, common agreement or maximization of the social welfare. For convenience, the results presented in the following subparagraphs use the bus index as prioritization index, but the algorithm can consider any user-preferred priority.
Scenario 5, consumer-oriented, uses FCFS principle for consumers as a primary trading prioritization tool, and the consumer has the benefit of selecting available prosumer offers with the lowest price.
The main reasons for creating µMs are to promote generation from small-scale renewable sources, and to lower consumer electricity prices. Next, a comparative study regarding the advantages of each prosumer-oriented scenario is presented. The main focus is on the financial savings of the consumers and market flexibility, in terms of the number of served contracts.
In these scenarios, because the prosumer price is not relevant, all the consumers are integrated into the local µM and the hourly total consumption always exceeds the available surplus from the prosumers, thus all prosumers will sell their surplus to consumers via P2P contracts. However, the prioritization of the consumers for trading will change in each scenario, together with the financial settlements between parties.
Regardless of the first four prosumers-oriented scenarios (Scn 1 − Scn 4 ) and the unique consumer-oriented scenario (Scn 5 ), the prosumers will sell the same quantities, as is indicated in Table 4. On the other hand, the quantities purchased by consumers are different in accordance with each proposed scenario. These values can be viewed in Table 5. For the first scenario (Scn 1 ), the quantities traded by prosumers to consumers are shown in Figure 6. It can be seen that the consumers geographically close from prosumers locations purchase the higher quantities. For example, the prosumer P7 sells energy to consumer C8, prosumer P15 to consumer C14, and the prosumer P21 to consumer C20. Similar results are obtained for Scenario 2 (Scn 2 ) where the prioritization is made according to the instantaneous power required by consumers. In this scenario, the consumers with the highest demand are preferred in the same manner, in each trading interval (C10, C9, C8, C5), as seen in Figure 6 and Table 5. geographically close from prosumers locations purchase the higher quantities. For example, the prosumer P7 sells energy to consumer C8, prosumer P15 to consumer C14, and the prosumer P21 to consumer C20. Similar results are obtained for Scenario 2 (Scn2) where the prioritization is made according to the instantaneous power required by consumers. In this scenario, the consumers with the highest demand are preferred in the same manner, in each trading interval (C10, C9, C8, C5), as seen in Figure 6 and Table 5.  For Scenario 3, where consumers are allocated in five priority clusters according to the daily electricity demand (Figure 5), it is observed that cluster I already contains three prosumers (P6, P7 and P15) and one consumer (C10). Cluster II has a prosumer (P21) and two consumers (C5 and C16), and cluster III comprises of eight peers, and the last two clusters group the rest of the peers. From Figure 7, it can be observed that the peers from the first two clusters have priority for trading, and the remaining surplus is sold only three consumers from cluster III, respectively C8, C9 and C24. In this scenario, the prosumer from bus 6 receives electricity from the local market, in the hours with deficit (see Table 2).
In the last two scenarios, that use the blockchain technology based on the FCFS principle, depending on the P2P contracts already signed, it is observed that the only ones who do not receive the surplus of electricity are prosumers an the consumer from bus 28, which has an insignificant consumption (see Table A1, Appendix A). For Scenario 3, where consumers are allocated in five priority clusters according to the daily electricity demand (Figure 5), it is observed that cluster I already contains three prosumers (P6, P7 and P15) and one consumer (C10). Cluster II has a prosumer (P21) and two consumers (C5 and C16), and cluster III comprises of eight peers, and the last two clusters group the rest of the peers.
From Figure 7, it can be observed that the peers from the first two clusters have priority for trading, and the remaining surplus is sold only three consumers from cluster III, respectively C8, C9 and C24. In this scenario, the prosumer from bus 6 receives electricity from the local market, in the hours with deficit (see Table 2).  For all five scenarios, the daily electricity quantities from prosumers purchased by consumers are presented in Tables 6-10. Moreover, the last four columns from the aforementioned tables contain the total quantities purchased by each consumer, the price paid by consumer(s) to prosumers for this quantity trough P2P contracts, the regulated price that should have been paid by consumers to the classical supplier at 0.72 MU/kWh, and also by prosumers to the grid aggregator with a regulated price of 0.223 MU/kWh. The last columns present the financial advantages for all the transaction participants.  In the last two scenarios, that use the blockchain technology based on the FCFS principle, depending on the P2P contracts already signed, it is observed that the only ones who do not receive the surplus of electricity are prosumers an the consumer from bus 28, which has an insignificant consumption (see Table A1, Appendix A). Figure 8 shows the similarities in traded quantities, resulting from applying the mathematical model proposed for the last two scenarios. The differences between Scn 4 and Scn 5 are seen in the purchase price of the surplus according to the type of P2P contract concluded between prosumers and the rest of the participants in the network.
For all five scenarios, the daily electricity quantities from prosumers purchased by consumers are presented in Tables 6-10. Moreover, the last four columns from the aforementioned tables contain the total quantities purchased by each consumer, the price paid by consumer(s) to prosumers for this quantity trough P2P contracts, the regulated price that should have been paid by consumers to the classical supplier at 0.72 MU/kWh, and also by prosumers to the grid aggregator with a regulated price of 0.223 MU/kWh. The last columns present the financial advantages for all the transaction participants. For example, in Figure 9 the prosumers financial benefits were presented, with the price paid for the consumers to each prosumer trough the smart considered P2P contracts compared to the regulated price received if they injected the surplus directly into the μG.
The benefits of using the local market are also present for the consumers. In Figure 10, the differences between the regulated price that would be paid by consumers and the P2P price used in trading with the prosumers are presented, which is always lower. For the equal quantities traded in Scenarios 4 and 5, the differences in financial settlements resulting from the blockchain merit order, but with different prosumer-consumer trading prices are presented in Figure 11.   To highlight the prosumer/consumer advantages using the proposed PEST algorithm, from Tables 6-10 can be seen the benefits registered by each participant in the trading process, regardless of the chosen prioritization scenario.
For example, in Figure 9 the prosumers financial benefits were presented, with the price paid for the consumers to each prosumer trough the smart considered P2P contracts compared to the regulated price received if they injected the surplus directly into the µG.
The benefits of using the local market are also present for the consumers. In Figure 10, the differences between the regulated price that would be paid by consumers and the P2P price used in trading with the prosumers are presented, which is always lower. For the equal quantities traded in Scenarios 4 and 5, the differences in financial settlements resulting from the blockchain merit order, but with different prosumer-consumer trading prices are presented in Figure 11.   . The difference between P2P prices obtained by the consumers in the P2P market, for Scenario 4 and 5.

Discussion
As the results presented in the study case show, both the consumers and the prosumers can obtain significant profits from the implementation of a local μM in which prosumers sell directly to the prosumers. In this market, prosumer can sell electricity to prosumers at prices lower than the Figure 9. The difference between P2P and regulated prices obtained by the prosumers in the P2P market.      . The difference between P2P prices obtained by the consumers in the P2P market, for Scenario 4 and 5.

Discussion
As the results presented in the study case show, both the consumers and the prosumers can obtain significant profits from the implementation of a local μM in which prosumers sell directly to the prosumers. In this market, prosumer can sell electricity to prosumers at prices lower than the Figure 11. The difference between P2P prices obtained by the consumers in the P2P market, for Scenario 4 and 5.

Discussion
As the results presented in the study case show, both the consumers and the prosumers can obtain significant profits from the implementation of a local µM in which prosumers sell directly to the prosumers. In this market, prosumer can sell electricity to prosumers at prices lower than the regulated tariff established for residential consumers, but higher than the price at which they can sell back to the grid their generation surplus. As in Figure 9, the daily profits for prosumers can vary from 1.8 to 6.2 MU (1 MU = 1 Romanian leu or 0.21 EUR), and for consumers from 1.8 to 6.2 MU.
For consumers, the daily financial gain can amount to up to 2.2 MU (consumer C16). The consumer's total demand for the considered day is of 23.84 kWh, amounting to an electricity bill of 17.16 MU, which means that the daily saving of the consumer is of 12.8%, in the scenario with the maximum number of consumers involved in trading. Our proposed mechanism was tested also for the cases when the PV generation of the prosumers is small. In these cases, if it is a surplus, the most convenient turned out to be Scenario 4 based on the blockchain technologies, which consider both quantities and price (from P2P contracts).
For a technical consideration, it should be noted that the trading results presented in the paper do not account for the energy losses in the LV distribution network, because they have the same influence on all the scenarios considered in the algorithm. In the physical network, prosumers would inject the surplus in the local network, and the consumers would draw power in the same manner. The difference is only in the financial settlement performed in the µM. The losses need to be settled at the market level, but this is a separate mechanism that needs future research. In Table 11, the number of consumers which benefits form the trading process are presented. It can be seen that only three consumers are commonly to the five considered scenarios. For the three consumers in Figures 12-14 the purchased energy and the costs of consumers, and the revenue of prosumers.     Scn1  16  13  Scn2  10  7  Scn3  8  5  Scn4  21  18  Scn5 23 20   Considering the obtained results from Tables 5-10 and Figures 7 and 12-14, it is emphasized that the third scenario is the least favorable for the participants. In this scenario, the distribution network operators win due to an optimization of power flows between the prosumers and the consumers with high power demand.

Consumers Consumers
The time granularity and period of day was considered. Our study was conduct only hourly trading for a day, but the mechanism can be easily used for other period. A complete transaction depends upon the proposed scenarios, taking into account the surplus of the prosumers, consumers power demand, as well as the distance between peers and P2P contracts.
The proposed algorithm is only the first step in developing a trading platform for consumers and prosumers in microgrids, and is aimed to serve as a simulation tool for developing alternatives for the current regulation framework regarding prosumer activity in the Romanian electricity market. However, future research will extend its capabilities for other trading scenarios.

Patents
National Patent Application "Innovative method of decision-making assistance aimed at streamlining the management of prosumer activity", Romania, 2019, in press. Considering the obtained results from Tables 5-10 and Figures 7 and 12, Figures 13 and 14, it is emphasized that the third scenario is the least favorable for the participants. In this scenario, the distribution network operators win due to an optimization of power flows between the prosumers and the consumers with high power demand.
The time granularity and period of day was considered. Our study was conduct only hourly trading for a day, but the mechanism can be easily used for other period. A complete transaction depends upon the proposed scenarios, taking into account the surplus of the prosumers, consumers power demand, as well as the distance between peers and P2P contracts.
The proposed algorithm is only the first step in developing a trading platform for consumers and prosumers in microgrids, and is aimed to serve as a simulation tool for developing alternatives for the current regulation framework regarding prosumer activity in the Romanian electricity market. However, future research will extend its capabilities for other trading scenarios.

Patents
National Patent Application "Innovative method of decision-making assistance aimed at streamlining the management of prosumer activity", Romania, 2019, in press.  The index for consumers k The index for prosumers l The consumer (l, . . . , 1, . . . , n c ) p The number of priority matrix. L j,k The length between consumer j and prosumer k LV