Peer-to-Peer Electricity Market Based on Local Supervision

The active participation of small-scale prosumers and consumers with demand-response capability and renewable resources can be a potential solution to the environmental issues and flexibility-related challenges. Local energy markets based on peer-to-peer trading is defined as one of solutions to exploit the maximum flexibility potential of prosumers. However, the existing literature that proposed peer-to-peer based local energy markets did not lead to respecting the peers’ energy trading preferences simultaneously in the profitable market settlement. To solve this issue, a new local energy market model is presented in which network users can trade with their preferred trading partners within the local market as well as the grid. The proposed trading model includes two levels to consider both the democracy and the profitability of energy trading. At the first level, the model considers the trading preferences of each player to respect the peers’ choices. The second level matches the rest of the bids and offers of the local buyers and sellers aiming to maximize the social welfare of all of the players participating in the local market. Our proposed local market is implemented for a test system consisting of fifteen residential players, and the results are compared to other trading models through different comparison criteria such as social-welfare of all players and the net cost of each individual player from consuming electricity. According to the results, the proposed model stands in the second rank compared to the other models that do not simultaneously consider preferences and social welfare of the peers, in terms of social welfare, total profits of the players, and the sustainability and liquidity-based criteria. The proposed model achieves 1416-Cent as the total net energy costs of all peers and the total accepted blocks equaling 76. This means that the proposed local market model can still be profitable and liquid while respecting the players’ trading preferences and choices.

The upward flexibility for trading partner j (kW). The downward flexibility for trading partner j (kW). The scheduled power bought in P2P trading for trading partner j (kW). P s,p2p j,t,ω The scheduled power sold in P2P trading for trading partner j (kW). P b,lm j,t,ω The scheduled power bought in the LM for trading partner j (kW). P s,lm j,t,ω The scheduled power sold in the LM for trading partner j (kW).
The total scheduled power bought for trading partner j (kW). P s,T j,t,ω The total scheduled power sold for trading partner j (kW). v j,t,ω A binary variable for expressing selling/buying energy status of trading partner j. x j,t,ω A binary variable for expressing upward/ downward flexibility status of trading partner j.
Variables for clearing the proposed two-level LM The quantity of offering block m for trading partner j accepted to be sold to trading partner i (kW). p neg,bl j,i,t,m The quantity of bidding block m for trading partner j accepted to be supplied by trading partner i (kW). P buy j,t Power bought from the grid by trading partner j at time t (kW). P sell j,t Power sold to the grid by trading partner j (kW). u j,i,t,m A binary variable for showing the acceptance of offering block m offered by trading partner j to be sold to trading partner i.
Parameters for building offering/bidding strategies τ ω Probability of scenario ω. π da t,ω Day-ahead market price at time t and scenario ω (Cent /kW). γ j Flexibility coefficient for trading partner j.
Parameters for clearing the proposed two-level LM L j,t Power scheduled to be consumed by trading partner j (kW). L bl j,t,m Quantity of bidding block m for trading partner j (kW). P bl j,t,m The quantity of offering block m for trading partner j (kW). P j,t Power scheduled to be produced by trading partner j (kW). π l j,t,m Price of bidding block m offered by trading partner j (cent/kWh).  Retail buying price at time t (cent/kWh). π sell t Retail selling price at time t (cent/kWh). α j,i A binary parameter showing the preference of trading partners j and i for trading energy with each other.

I. INTRODUCTION A. MOTIVATION
Recently, the roles of electricity consumers are undergoing a considerable change. These consumers who were previously regarded as ''submissive rate-payers'' can manage their consumption, produce electricity and make profits through the use of their distributed energy resources (DER) [1]. Also, the high ratio of active prosumers with efficient energy storage, scheduling, and trading possibility can be the most promising ways of balancing energy demand and supply [2]. The high utilization of the DERs along with the technological development in the energy area such as the advent of smart meters and home energy management systems empower consumers and encourage them to change their roles from consumers to pro-active consumers or so-called ''prosumers''. Prosumers need to be incentivized and be constantly flexible to the changes happening in the power system to exploit the maximum potential of the DERs. However, the existing feedin-tariff [3], [4] receiving from selling surplus generation to the grid has not provided the prosumers with enough motivation [5].
In addition to these problems, approximately 11% of the world populations still do not have access to electricity [6]. Therefore, they must be equipped with the local resources and trade with each other to meet their own and even their neighbors' demand [7].
Along with technological development, new business models are required to engage prosumers and consumers in producing electricity and react to the system changes by managing their production and consumption [8]. In this way, the concepts of LMs (local market) and P2P (peer-to-peer) energy trading have attracted much attention aiming to put small-scale prosumers and consumers at the heart of energy markets.

B. LITERATURE REVIEW
The concept of P2P trading was introduced for different scale of energy trading to increase democracy and exploit peers' maximum resource potential for producing energy and flexibility [9]. In this regard, a local market can provide peers with the environment so that they can trade energy with each other bilaterally, or in an aggregated manner.

1) DIFFERENT TYPES OF LM DESIGN AND P2P TRADING
LM designs can fit into three categories. The first category is called a full P2P trading model in which two peers may agree on a transaction, leading to the multi-bilateral economic dispatch [10]. The research included in this category respects the preferences of players to choose their trading partners. Ref. [12] is an example of these studies.
The second category is called community-based P2P trading in which prosumers join a community to trade energy with other communities through trading models. For instance, in [12], microgrids can trade with each other. In this research, small-scale prosumers and consumers are not considered as individual players. Ref. [13] also proposed a three-level hierarchical energy sharing and transactions for residential microgrids, which can belong to the community-based trading.
Finally, the hybrid model is a combination of the two previous models in which both small-scale prosumers and communities can trade energy with each other [10]. For example, in a model proposed by [17], residential units and communities trade energy with each other aiming to assist the system with fulfilling the demand.
In another categorization, P2P trading and LMs can be designed for system-wide or local level purposes. In this way, system-wide trading models aim to trade energy or flexibility services in large scales and for system-wide requirements whereas local trading models trade energy or flexibility to satisfy local energy or flexibility needs [15].

2) SYSTEM-WIDE P2P TRADING MODELS
In terms of the system-wide trading models, [16] developed a P2P energy contract between individual customers and/or utilities. The authors of [17] put forward the idea of bilateral contracts between large-scale peers in a forward market. The direct interaction between suppliers and consumers of the electricity market was also proposed in [18]. Similarly, [11] presented a bottom-up approach for future decentralized electricity markets in which consumers can choose their products considering various energy product differentiation. According to [11], the product differentiation in energy trading allows consumers to set a dynamic value on the other important aspects of electricity than its energy content. For example, the source of energy can make product differentiation since it highly affects the environment.
The participation of small-scale players in the wholesale energy market and providing system-wide energy was suggested in [19]. The authors of [20] presented a method that encourages customers to perform P2P trading to provide system-wide flexibility services by alleviating the congestion at peak hours.

3) LOCAL P2P TRADING MODELS
In the context of local energy trading at customer (local) levels, there exist some research proposing LM structures with different objectives.
Game theory-based approaches were deployed in most of the studies with the objectives to model the P2P trading. For instance, a Stackelberg game was utilized in which sellers play the role of leaders, followed by the buyers in [20]. The authors of Ref. [21] inserted the output of participants' non-cooperative game as the input of the evolutionary game to update the strategy selection of the sellers. A method associated with game theory was also employed in [22] to reach the LM equilibrium of P2P trading and increase the social welfare of the players. In a full P2P model, authors of [24] proposed a model in which peers negotiate together to trade energy and flexibility. Similarly, [25] presented a full P2P structure, in which players can trade with their preferred trading partners. However, they need to follow multiple rules so that their bids and offers are accepted. A decentralized LM clearing mechanism was also suggested by [26], where each agent should communicate with its neighbors to achieve the optimal trading. In another game-based approach that was suggested by [27], the number of local transactions was maximized so that local production can be consumed locally. The work also tried to maximize the social welfare of the strategic participants. In the mentioned studies, LM participants play a key role in the LM clearing mechanism.
Ref. [18] designed a novel P2P trading model in which both energy and uncertainty can be traded. The authors of [19] present auction-based LM clearing rules that aim to increase seller profits while minimizing the total saving costs of the buyers. Authors of [30] suggested that each player participating in the P2P trading can have a reputation index and the proposed LM tries to maximize the traders' reputation indexes as well as their social welfare in its matching process. In [31], a P2P trading model was built based on social-welfare maximization formulation regardless of the preference of peers for choosing their trading partners. In other research conducted for [32], the distribution system operator was proposed to be responsible of marching bids and offers of local players in the LM. The authors of [33], proposed a slimemould-inspired optimization method to find best matches for offers and bids of peers in the LM.
In the work proposed by [34], the matching of small-scale players' bids and offers are based on the local network flexibility needs. However, it ignores the trading preferences of the peers. In [35], authors suggested a P2P market structure that seeks the maximum benefits for the local players. Considering this method, the LM players can maximize their profits compared to the way that they should trade their surplus with the retailer. However, the players do not have an option to select their trading partners freely. In addition, they are not allowed to submit their preferred buying/selling prices and the P2P transaction prices are determined based on the grid prices, not those offered by the peers. Finally, [36] used different trading functionalities such as bilateral contracts, trading with the retailer and Vickrey-Clarke-Groves mechanism. Although the introduced method considered the trading preference of the peers, one can argue that the matching process did not lead to the most profitable point for the participants. In this research, trading energy with the grid was the option that can be selected by the players, not an option that can lead to the maximum profits for the LM players.
Generally, the existing LM and P2P trading model structures mainly suffer from the following limitations: VOLUME 9, 2021 1-The game theory-based approaches need the contribution and cooperation of rational participants in the process of matching bids with offers, which may not be a valid assumption. Moreover, as stated in [34], if the local players want to maximize their profits in a collaborative game-based clearing approach, they need to truthfully disclose their information and the LM requires their cooperation in solving the Nash equilibrium problem. However, LM clearing mechanisms should be able to match bids with offers regardless of the behavior and the cooperation of participants and with respect to their preferences.
2-A prosumer or consumer may set value on some aspects of energy other than economic aspect. Thus, in order to engage small-scale customers to participate in local energy trading, first, prosumers and consumers should be allowed to choose their trading partners freely. Second, the LM clearing mechanism needs to profit all players participating in the LM by maximizing the revenues of local sellers from selling electricity to both the LM and the upstream grid and minimizing the costs of buyers from buying electricity from both the grid and the LM. Thus, the research dealing with local energy trading at customer levels needs to guarantee these two factors i.e. profitability and the choice of peers. However, most of the existing literature (if not all) did not fully cover these two factors to maintain the balance between social welfare and the energy democracy in the LM environment.

C. PAPER CONTRIBUTION, ASSUMPTIONS, AND ORGANIZATION
This paper proposes a novel local P2P trading model for prosumers and consumers under the supervision of a local market operator (LMO). The proposed model aims to satisfy two factors. First, it respects the preference of peers to choose their trading partners and their buying/selling prices. Second, it seeks profitable energy trading in the LM because it aims to maximize the social welfare of the LM players.
The main contributions of the paper are as follows: C1 (bidding/offering blocks): Local players can submit several blocks with different quantities and prices to the LMO at each time slot. The LM clearing mechanism matches bidding blocks with the offering blocks, according to different offered and bided prices.

C2 (price-based constraints):
In the proposed LM, pricebased constraints are imposed on the block matching process to respect players' offered prices. In other words, the peers' offered blocks are matched according to the prices. In this way, all of the LM players are satisfied with the LM clearing mechanism.
C3 (choice of peers): The proposed market structure not only can settle imbalances in the local community and maximize the social welfare of the players, but it also increases consumer choice and value. In other words, it has the benefits of both centralized and P2P markets by proposing a hybrid model. C4 (maximizing/minimizing the revenues/costs of the sellers/buyers): After respecting the peers' trading preferences, the proposed LM tries to settle all of the transactions with the aim of maximizing the local sellers' revenues and minimizing the local buyers' costs. The proposed LM aims to fulfil this objective in trading within the LM as well as trading with the upstream grid. Hence, the local players would trade energy with the upstream grid whenever trading with the grid leads to the revenue maximization or the cost minimization. Thus, local sellers can sell their surplus energy in a way to ensure that they achieve maximum revenues while local buyers can buy their required energy ensuring that it minimizes their energy costs. Table 1 compares the proposed P2P trading model with similar research presenting P2P and LM concepts at the distribution network level. As can be seen in the table, there is no previous research that has the features of both C3 and C4, meaning that they did not simultaneously consider choice of peers while trying to minimize the costs of local buyers from buying electricity and maximize the revenues of local sellers from selling electricity. In addition, the price-based market clearing mechanisms (C2) of the previous research were totally different as they proposed different clearing mechanisms. However, all of the papers that considered C2 tried to take into account prices offered by the local players in their proposed clearing mechanism.
The remainder of this paper is organized as follows. The bidding strategies of LM players are defined in section II. The architecture of the proposed market and market-related formulation are discussed in section III. The case study and numerical results are expressed and discussed in section IV. Finally, the paper is concluded in Section V.

II. HOUSEHOLD BIDDING AND OFFERING STRATEGIES
The energy management system of players should build their optimal offering and bidding strategies so that they will be able to participate in the LM. The local-market players are considered to build their bidding/offering strategies based on their net consumption and production in different scenarios using the method proposed in [37]. It should be highlighted that the paper's focus is on introducing a novel P2P local market clearing mechanism and the mechanism is independent of the players 'contribution and their cooperation in matching bids with offers.
Stochastic programming is deployed to capture uncertainties of prices, the consumption and production of each player. In this regard, a set of scenarios is generated for different market prices, production and consumption, using a scenario tree and the method introduced in [37]. By considering different scenarios, different offering and bidding blocks are obtained for each player through optimization problem that will be introduced in the following. Accordingly, a player schedules its flexible resources and simultaneously obtains its bidding/offering strategy according to its total costs. At each time slot, the player determines to either play the role of consumer and submit bids or to play the role of prosumer and submits offers to the LM based on each scenario's production and consumption.
Here, expected cost of player j (EC j ) is defined as an objective function for players which needs to be minimized.
Eq. (1) presents an objective function for player j consisting of three terms: I. expected cost/revenue of P2P trading, II. expected cost of electricity bought from the LM and III. expected revenue of electricity sold to the LM, respectively. The player should minimize (1) to obtain its optimal bidding/offering strategy. It should be noted that in the proposed bidding strategy, prices of different scenarios are parameters and the offered/bided quantities are the variables of the optimization problem.
Balancing is an indispensable equation in all energy systems represented in (2) for the proposed home energy management problem.
In Eq. (2), L f ,up j,t,ω and L f ,dn j,t,ω are defined as upward and downward flexibilities for player j. P b,T j,t,ω and P s,T j,t,ω represent total power bought and power sold of player j, respectively, to other players and the LM as seen in (3,4).
Besides, player j can act as either a seller or a buyer of energy at time slot t and scenario ω, which is denoted by (5) and (6). Besides, these constraints restrict the maximum generation and consumption of the player.
Eqs. (7,8) express upward and downward flexibility constraints for player j. Here, γ j is a parameter between zero and one and represents potential flexibility provided from consumer-side (e.g. energy storage system, shiftable and interruptible loads) defined in [38].
According to (7,8), upward and downward flexibilities cannot be provided simultaneously at time slot t and scenario ω, 0 Finally, Eqs. (9, 10) present the corresponding constraints of offering and bidding strategies for player j.
(10) According to (9,10), the prices of different scenarios are compared to each other and accordingly optimal offering and bidding curves are obtained in ascending and descending stepwise functions, respectively [37]. In this regard, the nonequality constraint, ∀ω ≥ ω , tries to avoid the repetition in the process of comparing scenarios.
In the optimal offer curves, the quantity of offered P2P to be sold in scenario ω is higher (or equal) than offered P2P to be sold in ω , if its offered price in scenario ω is higher (or equal) than its offered price at scenario ω . On the other hand, in their optimal bidding curves, the quantity of P2P bid to be purchased in scenario ω is lower (or equal) than P2P bid to be purchased in ω , if the bid's price in scenario ω is higher (or equal) than the price of scenario ω .
In this way, the sellers and buyers submit their ''offers'' and ''bids'' to the LM based on offering and bidding blocks, respectively. However, in addition to the cost minimization objective, the consumers and prosumers may have other generic preferences for choosing their trading partners. Hence, the LM players should also be given an option to choose the peer(s) with whom they are willing to trade.
These generic preferences of local consumers and prosumers can be as follows: -A player chooses to trade with the peers in its neighborhood intending to empower its neighboring local community. -A player decides to trade with the peers who are more likely to fulfil their promises related to selling energy, called high-rated peers in this paper. -A consumer may choose its peers based on their utilized energy resources. For instance, environmentally aware consumers prefer to select peers with renewable resources. Binary parameters model the generic preferences of players. For example, the preference of player i for trading with player j is denoted by a binary parameter α i,j . In other words, player i associates α i,j = 1 to peer j with whom she/he is willing to trade. In this way, the smart system that facilitates the bidirectional communication between local market participants and the operator is in charge of determining these binary parameters. It needs to determine the binary parameters based on the generic preferences of the local market players. For example, if buyer i prefers to buy electricity from those with a battery as the energy resource, the system sets α i,j = 1 for all j sellers who sell electricity from their batteries. The player may also select a combination of preferences. Thus, the system needs to find the available peers according to the selected preferences and define their associated binary parameters to equal one.
Having determined the binary variables for the preferred trading partners, the player should submit these binary parameters along with its optimal offering and bidding curves to the LM. Fig. 1 illustrates an example of players submitting bidding curves and preference parameters to the LM.

III. PROPOSED P2P LOCAL MARKET
In this paper, residential consumers and prosumers can trade energy through a platform provided by the local market operator (LMO) as shown in Fig. 2.
Households as players of the LM submit their bidding and offering curves. The LMO also receives their preference parameters. Note that an LMO is a non-profit agent responsible for clearing energy transactions according to the households' offers and bids and their corresponding preferences. The LMO supplies the local demand and trades the local power imbalances with the upstream grid. These imbalances can be the result of local day-ahead generation/demand mismatching or generation/demand uncertainties in real-time.
After receiving the bids and offers, the LMO forms a P2P local market seeking to maximize revenues and minimize the costs of all of the players within the LM. It also respects the preferences of peers for choosing their trading partners as the priority of the LM clearing mechanism. To this end, the proposed model follows two sequential levels: In each time slot, the LMO receives a list of the peer(s) (binary parameters) that a player prefers to trade with and its hourly bidding/offer curves. After choosing the preferred peer(s), at the first level, the LMO matches offers and bids submitted by the players who both preferred to trade with each other. In other words, it matches the bidding blocks with offering blocks of peers if α ij = α i,j α j,i = 1. In addition to binary parameters, the matched bids and offers need to respect a price constraint introduced in the next section. At the first level of the LM, the LMO aims to maximize the matched offering and bidding blocks based on players' preferences. In this regard, the trading priority is given to those players leading to the greater overall bids' and offers' matching based on the participants' preferences. After matching bids and offers based on preference parameters and price constraints, the surplus of net demand and net production that were not matched at the first level are transferred to the second level.
At the second level, the blocks of offers are matched with the bidding blocks aiming to maximize the social welfare of all LM participants. In other words, a bidding block would be matched with an offering block providing that the transaction can maximize the social welfare of all LM participants. Finally, the local market surplus (both in net production and net consumption) is settled through the upstream grid. Fig. 3 provides a comprehensive overview on the matching process performed by the LMO based on the formulation presented in the next section.

A. FIRST LEVEL: PREFERENCE-BASED P2P TRADING
Firstly, it is assumed that each player i has a binary preference-based vector with N j elements (α i,j ) introducing the peer(s) it chose to trade with, where N j denotes the number of players participating in the LM. The players also submit several blocks illustrating their offers and bids for each time slot as illustrated in Fig. 1.
After the local-market gate closure, the LMO matches the bids and offers. The main objective of the LMO is to maximize the quantities offered in the proposed LM to highly consider the trading preference of the peers as represented in (11).
According to the first-level objective function, the priority of trading is given to those trading partners who help achieve the maximum matching capacities based on players' preference. The introduced optimization problem is restricted to some constraints which are presented in the following equations.
Eq. (12) represents a balance-related constraint explaining that the demand of user j at each time slot should be met by the power bought from other peers at the first level of the LM and the remaining demand is transferred to the second level of the trading.
Similarly, (13) expresses that the net generation of the player j at each time is traded with the preferred peers' demand and the remaining net generation is transferred to the second level. This paper assumes that the player is either a seller or a buyer and submits either the offer or the bid at each time slot.
Moreover, each bidding block would be matched with one or several offering blocks or vice versa, considering the objective of the LM. Eqs. (14,15) state that the total amount of the quantities for offering and bidding blocks should not exceed the offered blocks' capacity.
The total power traded between players i and j is obtained from the summation of all the blocks matched for these two peers as represented in (16,17).
The constraints taking into accounts the preference of the peers are represented in (18,19). According to these constraints, the power cannot be traded between two players if they did not choose each other as their preferred trading partners. Here, α i,j represents a binary parameter indicating the preference of players j and i for trading energy with each other. It obtains from multiplying α i,j by α j,i . If the binary parameter representing the preference of player j trading with i equals zero, p pos,l1 j,i,t , p neg,l1 j,i,t are equal to zero, accordingly. It means that these players' offers and bids cannot be matched at the first level of the LM. Besides, Eqs. (18,19) indicate the upper limits for the trading capacities between players j and i. p pos,l1 j,i,t ≤ α i,j P j,t , ∀t, ∀j, ∀i.
It is noticeable that the network constraints are not taken into account in this level. This assumption could be valid for a system with a limited number of players [21]. Thus, the amount traded between two players should be the same, meaning that the power that player i sells to player j at time t is equal to the power that player j buys from player i at t as VOLUME 9, 2021 illustrated in (20).
The offering quantity of player j, which is sold in the LM should not exceed its maximum offered capacity. Further, the bidding quantity of player j, which is supplied from other peers should not exceed the player's demand as represented in (21,22).
A peer can choose several trading partners and has several transactions with different peers at one trading time slot. However, two offering and bidding blocks are matched whenever buyers' offered prices are equal or higher than the prices of sellers as represented in (23). Fig. 4. shows all the possible situations that can happen concerning the prices of offers and bids. As seen in Fig. 4, the offered price of a buyer peer should be higher than that of the seller peer. Accordingly, the first row offers and bids can match while those of the second row are not allowed to be matched with each other. As a result of a zero value for u l1 j,i,t,m , the matched blocks between players i and j should also equal zero. Thus, Eq. (24) restricts the traded amount according to the binary variable determined by (23). Finally, the traded amount of power should be a positive value, as in (25

B. SECOND LEVEL: SOCIAL-WELFARE-BASED TRADING
The second level of the trading model aims at maximizing the social welfare of all of the players participating in the LM. For this purpose, the LMO matches the bidding by offering blocks of the players considering the social welfare of all of the participants in the LM. Furthermore, suppose more than one option exist for matching offering with bidding blocks. In this case, the priority is given to the trading couple who are benefiting all of the LM players through maximizing the social welfare of the LM. The accepted first-level offering and bidding quantities should be subtracted from the total offering and bidding capacities of players to obtain the second-level bidding and offering quantities. The remaining offering and bidding capacities should be traded at the second level of the proposed LM.
Eqs. (26,27) determine the total remaining supply and demand of player j, respectively. The remaining capacities for each block offered by player j, which is ready to be traded at the second level are determined in (28,29). In these equations, Similarly, j,i,t,m is the obtained amount of buying capacity submitted by block m of player j which was bought from the peers in the first-level of LM. As two trading peers may offer different prices for their blocks, the trading price is considered an average of buying and selling prices to benefit both parties as given in (30) [29].
In this way, the social welfare of the LM is defined as the revenues of all players from selling electricity to the LM (I) and/or the grid (IV) minus the total costs of buying electricity from the LM (II) and/or the grid (III). Thus, the social welfare of the proposed LM is obtained from (31).  (32) Constraints associated with the balance of the demand and supply are considered in (33,34), explaining that each player's net generation should be consumed in the P2P local market and/or be sold to the grid. Likewise, each player's demand should be met by the local net generation and/or be supplied from the grid.
Besides, the offering and bidding blocks traded with different peers should not exceed their maximum capacity as represented in (35,36 Furthermore, other constraints restrict the capacities of offering and bidding blocks and the total trading quantities between two peers. Eqs. (37,38) present that the total trading power between players i and j are equal to the summation of their corresponding traded block quantities. Eq. (39) states that the traded power between players i and j should be the same since the power loss is negligible. Eqs. (40,41) Also, only the bidding blocks with higher or equal prices can be matched with the offering blocks at the second level of trading. The related constraints are denoted with (42) and (43). Finally, the trading power at the second level should also be a positive value, as represented in (44).
As a third level of the LM, the LMO can also run a power flow optimization to ensure that the matched offers and bids do not jeopardize the security of the local network. As an example, [15] utilized a linearized power flow to check whether the network constraints are satisfied. It should be noted that P2P energy trading in an LM environment can decrease losses as it avoids power flows through different voltage levels and networks [21]. However, there would be still other types of loss caused by other factors including serious harmonic loss resulted from the high penetration of renewable resources, the increasingly use of electric equipment, the dielectric loss of the capacitor, as well as reactor loss such as conductor, hysteresis, and eddy current losses [39].
The proposed optimization problem was coded and solved in GAMS software using CPLEX solver performed in a PC with a 2 GHz processor and 8 GB memory.

A. PROPOSED MODEL IMPLEMENTATION
A case study includes ten residential consumers and prosumers h1-h10 who are willing to participate in the LM. In addition, there are five local PV producers p1-p5 who are willing to sell their production in the LM.
Players h1,. . . ,h10 submit their bidding/offering blocks for their net demand/generation. In this regard, h1-h4 can be 'prosumers' in some time slots, meaning that their net generation can be positive during these time slots while h5-h10 are denoted as 'consumers' in all time slots. In comparison, p1,. . . ,p5 are small-scale utility that installed PV panels to sell electricity and make profits. Hence, they can only play the role of sellers in the LM.
The information about the maximum local net generation capacities and the daily net consumption obtained from their optimal bidding strategy is illustrated in Fig. 5. The prosumers and consumers submit their hourly net consumption and generation to the LMO. Producers p1,. . . ,p5 also submit their offers to the LM.
The amount of offered hourly net production for each prosumer of the LM is shown in Fig. 6 [37]. The amounts are the net generation of the seller obtained from its offering strategy. It is assumed that h2 is selected as the only preferred VOLUME 9, 2021 FIGURE 6. The hourly amount of net production for each prosumer. seller for users h5, h7, h8, and h10, regarding transactions at the first level. The other players did not identify their trading priorities at the first level of the LM. The retail prices for buying power from the grid are equal to 5.27 cent/kWh for t=1-7, 6.24 cent/kWh for t=8-22, and 5.27 cent/kWh for t=23-24 based on the data extracted from [40], where 1 Cent is equal to e0.01.
This way, the proposed P2P local market is simulated for the case study. Fig. 7 shows the total output and input power from/to the grid obtained from solving the proposed model's optimization problem. As seen in Fig. 7, the LM sold power to the upstream grid during timeslots in which it had local net production, i.e. 8-16. The input and output power obtained from the proposed method leads to the LM social welfare maximization.
Our proposed P2P local market model results are compared with four different models in the following sections, as described in table 2.
The first model is called ''community-empowering'' whose main objective is to maximize trading power within the local community, e.g. [28]. In the second model, there exists no LM. Thus, the households are trading with the grid considering the retail prices for selling and buying power. This model is named ''tariff-based''. In the third model called ''unsupervised P2P'', after matching the offers with bids of players at the first level, the remaining power will be traded with the grid. Finally, the fourth model, named ''social-welfare-based'' (SW-based), is the proposed model with eliminating the first level, meaning that players' trading preferences are not regarded in this model.
The proposed model and the first, third, and fourth models have local markets, whereas there is no local market in the tariff-based model. In other words, players' needs are supplied from the grid in the tariff-based model. The problem formulations related to these trading models selected for comparison are presented in Table 2. Also, the computing time associated with solving the model's optimization problem(s) is shown in the table. As previously mentioned, the problems were solved using CPLEX solver.
Since the tariff-based model does not have local trade, the following equations should be considered as constraints:   self-sufficiency as it needs less electricity to be met from the upstream grid.

1) FROM THE VIEWPOINT OF THE LM
The models described above are simulated for the same test case, and the results will be compared together. The daily and the presented hourly criteria are calculated for the introduced models, and the results are shown in table 3 and Figs. 10-14. It is noticeable that the producers (p1,. . . ,p10) has net production only during 8:00-16:00 and the households mostly play the role of consumers. Hence, the total SW of the LM is obtained as a negative value. In other words, the SW's absolute value can be regarded as the outgoing of the LM. From the perspective of the community, the SW-based model is more beneficial for the LM. A lower SW index for the proposed model was obtained by considering peers' preferences compared to the SW-based one. The unsupervised P2P model and tariff-based trading had high outgoing from the local community, meaning that they were less beneficial for the LM as presented in table 3.
Finally, the lowest SW index is obtained for the community-empowering model in which self-sufficiency of the LM comes at the cost of decreasing the SW of the LM.    self-sufficient LM as whose TLT and AB indexes are slightly lower than those of the proposed model. In comparison, the unsupervised P2P models did not have a good performance to bring self-sufficiency and liquidity-related benefits for the local community. The TLT and AB indicators for the tariff-based model equal zero as long as this model does not consider the concept of LM.

2) FROM THE VIEWPOINT OF INDIVIDUAL PLAYERS
As stated before, households (h1,. . . ,h10) play the role of consumers rather than prosumers in most time slots. As a result, considering the daily scheduling, the households total profit is a minus value. This paper defines a criterion named the total net costs (TNC) for each household obtained as represented in (45).
In other words, the TNC for player j is the total monetary amount paid to the local suppliers or/and the grid for meeting the player's demand minus the total amount that receives from selling electricity to the LM as well as the grid. Accordingly, from the viewpoints of players, a profitable model should incur less TNC for the player. This criterion was calculated for the households, and Fig. 13 depicts the distribution of this indicator for different households. The data points are the TNC of households (h1,. . . ,h10) participating in different trading models while the box plots denote the maximum, minimum and the mean values of the TNCs. Besides, Fig. 15 demonstrates the hourly results for a selected household, h1, as an example.
Additionally, the revenues of producers (p1,. . . ,p5) were estimated for the introduced models and the distribution of revenues for different producers is illustrated in Fig. 14. Again, the data points show the revenues of producers considering different trading models while the box plots indicate the maximum, minimum, and the mean values of producers' revenues. The producers' revenues are very close to each   other for the SW-based, unsupervised, tariff-based, and the proposed trading models. Accordingly, the box plots of these models depict lines. Fig. 16 indicates the hourly revenues of a selected producer (p5) that has participated in different trading models.
As shown in Fig. 13 and Fig. 14, the proposed model and the SW-based model can be beneficial for both households and producers. This is due to the fact that these two models aim to benefit all of the players participating in the LM. Thus, the consumers' costs are minimized while the producers' revenues are maximized in these two models. Considering TNC criterion, the proposed model had less cost after the community-empowering model. However, the community-empowering model was unable to benefit producers. As Fig. 14 states, the producers gain less revenues in the community-empowering trading model compared to the other models. Fig. 15 depicts the TNC index for household h1. According to this figure, the player achieves more revenue when it participates in the community-empowering and the proposed trading model. SW-based model also offers low costs for this player. The figure also states that the household played the role of prosumers at 11:00, 13:00, and 14:00 whereas it was consumers at 8:00, 9:00, 15:00, and 16:00. Unlike h1, the community-empowering trading model was the least profitable trading model for p5, as illustrated in Fig. 16. It can be concluded that in this case, trading with the upstream grid was more profitable for local producers. However, the proposed model and SW-based model can still provide acceptable revenues for p5.

C. DISCUSSION
Considering peers' preferences can be regarded as the only advantage of the unsupervised P2P model. The other indicators state that this model is not profitable for both the community and individuals. On the other hand, the SW-based model and the proposed supervised model offer considerable advantages from the viewpoints of the LM and individual players. Although the SW-based may perform better in terms of the individuals' profitability, its main drawback is its weakness in considering peers' trading preferences. It would be better to give players the option to find their best partners and consider their choices as the priority so that they understand that the LM fully appreciates their decisions which can incentivize all the players.
When it comes to the community-empowering trading model, its only benefit is its effort to make the LM more self-sufficient. However, in this case, it was less profitable than other trading models. According to table 2, this model seeks to maximize the local trades. However, if the players do not achieve enough revenues from the LM, they may decide to quit participating in the local energy trading, which may harm the LM self-sufficiency in long-term. As a result, considering long-term trading, the community-empowering model would not be a good option compared to our proposed supervised and SW-based models which seek to maximize the social-welfare of the participants rather than their trades. Thus, it is necessary that the LM model includes the second-level formulations to ensure the profitability of the market for local players and incentivize them to persist with their active participation in the LM.
Finally, the indicators express that most of the models with the permission of local energy trading perform better than the tariff-based trading. According to Fig. 17, SW-based and proposed models lead to the highest social welfare compared to the tariff-based model. In these two models, if trading with the grid is more beneficial for the players, they will trade with the upstream grid. As a result, the benefits of local players participating in SW-based and proposed model are always equal or higher than those of the tariffbased model. Not only can the tariff-based manner of selling and buying electricity to/from the grid increase the costs of individual players, but also it fails to benefit the whole community of households locating in the neighboring areas. Accordingly, the importance of forming the local market can be more evident from the simulation results of this paper.
The proposed model tries to consider different factors to obtain a model which is profitable and simultaneously respect the peers' trading preferences. However, depending on the choice of the local players, the results of our model may be more close to the unsupervised P2P model or close to those of the SW-based model. In the case that the local players prefer to choose their trading partners freely, it results in less benefit for the players. The results may be also more close to the SW-based model in the case where players prefer the market operator to choose their trading partners which lead to their revenue maximization. VOLUME 9, 2021 Thus, the proposed model can be different for different combinations of prosumers and consumers. However, all of the participants are given the option to choose between their principles, preferences and their economic benefits. To this end, with the proposed model, each participant knows that the market mechanism is highly flexible according to its choice and preference.

V. CONCLUSION
This paper proposed a new local market structure for peerto-peer trading that incentivizes small-scale prosumers and consumers to play a more active role in energy markets. The proposed model consists of two levels. At the first level, the local market operator matches bids and offers to consider the preferred peers for each player and the prices offered by consumers and prosumers. In this regard, the offers are matched with bids so that the sellers' prices should be lower or equal than the buyers' prices. The remaining demand and generation, which has not been matched during the first level, is transferred to the second level where the trading is based on social welfare maximization. Additionally, imbalances of the local market are settled through the upstream grid.
The proposed two-level local market model has been implemented for a case study. The results have been assessed and compared to three different local market-based models and a tariff-based trading model in which there is no local electricity market. The results are as follows: First, the proposed local market can be profitable for all participants because it increases the social welfare of all of the local players. It respects the generic preferences of participants for choosing their trading partners since at the first level, the local market operator aims to maximize accepted capacities based on players' preferences.
Second, the proposed local market can reach sufficient liquidity and self-sufficiency by incentivizing local and smallscale prosumers and consumers to play active roles in the local market. Considering the players' preferences can also lead to the liquid market on the long-term horizon. Finally, future works can be conducted in the following directions: a) Analyzing the optimal sizing of local markets to obtain tractable optimization problems. b) Considering the situations in which local markets as a player can trade energy with other local markets as well as trading with the upstream grid. c) Considering the strategic behavior of a local market that can participate in the wholesale markets as an individual player. Note that in this paper, the local market could trade with the grid in a tariff-based format.