Пример #1
0
def bid_stack(state, isodate):
    state = state.upper()
    participant_meta = participant_service.participant_metadata
    
    if state != "ALL":
        participants_in_state = [p for p in participant_meta if participant_meta[p]['state'] == state ]
    else:
        participants_in_state = [p for p in participant_meta]

    date = pendulum.from_timestamp(int(isodate))
    try :
        stack = BidStack.objects(trading_period = date).get()
        bids = {}
        for name in stack.getParticipants():
            if name in participants_in_state:
                bid = stack.getBid(name)
                bid_dict = bid_to_dict(bid)
                bid_dict['meta'] = participant_meta[name]
                bids[name] = bid_dict

        print(date, stack)
        
        return jsonify(bids)
    except DoesNotExist:
        print("Couldn't find stack")
        return jsonify({'message':'None found.'})
Пример #2
0
def generate(start_date, end_date):
    counter = 0
    date = start_date
    # for date in dates:
    while date < end_date:
        # if counter > 131:
        print(date)
        try:
            if BidStack.objects(trading_period=date).count() == 0:
                s = BidStack(date)
                s.save()
            else:
                print("Already exists")
        except mongoengine.errors.NotUniqueError:
            print("Bidstack already exists for ", date)
        # counter += 1
        date = date.add(minutes=30)
Пример #3
0
def process_bidstacks():
    print("Processing Bidstacks")
    request = BidStack.objects().fields(trading_period=1, id=1)
    print('Retrieved Bidstacks')

    PRICE_MIN = -300
    PRICE_MAX = -10

    i = 0
    timeseries = {}
    relevant_bidders = []

    for bidstack in request:
        # Get the trading period label.
        dt = pendulum.instance(bidstack.trading_period)

        if (dt.hour == 12 and dt.minute == 0 and dt.day % 5 == 0):
            bidstack = BidStack.objects.get(id=bidstack.id)
            print("Processing Bidstack", dt)
            participants = bidstack.getParticipants()
            for participant in participants:
                # Get some relevant data about the participant.
                # tech_type = participant_meta[participant]['technology_type_primary'] if participant in participant_meta else None
                # fuel_source_descriptor = participant_meta[participant]['fuel_source_descriptor'] if participant in participant_meta else None
                # fuel_source_primary = participant_meta[participant]['fuel_source_primary'] if participant in participant_meta else None

                # Look at the participant's bids - see if any fall within the range we are interested in.
                # print(participant)
                bid = bidstack.getBid(participant)
                for band in range(1, 9):
                    price = bid.get_price(band)
                    volume = bid.get_volume(band)
                    if volume > 0 and price >= PRICE_MIN and price <= PRICE_MAX:
                        # If we haven't seen this participant before, add them to the list.
                        if participant not in relevant_bidders:
                            relevant_bidders.append(participant)
                        # Add the data point to the timeseries
                        timeseries[
                            dt] = {} if not dt in timeseries else timeseries[dt]
                        timeseries[dt][participant] = price

                        # print(participant, band, bid.get_price(band), bid.get_volume(band), tech_type, fuel_source_descriptor, fuel_source_primary)

                # In future runs, could use these to narrow down. Leave it for the moment.

                # print(tech_type, fuel_source_descriptor, fuel_source_primary)
            # break
        # if i == 5:
        #     break
        # i += 1
    print("Finished Processing Bidstack")
    return timeseries, relevant_bidders
Пример #4
0
from application.model.participants import ParticipantService
from application.model.demand import Demand
from application.model.price import Price
participant_service = ParticipantService()
import pendulum
from application import config

from application.model.bidstack import BidStack, DoesNotExist, bid_to_dict, bid_to_list

dates = BidStack.objects().distinct('trading_period')
dates = [
    date for date in dates if (pendulum.instance(date).minute == 0
                               or pendulum.instance(date).minute == 30)
]
states = ['NSW', 'QLD', 'VIC', 'QLD', 'TAS']

participants = {}
for state in states:
    participants[state] = [
        p for p in participant_service.participant_metadata
        if participant_service.participant_metadata[p]['state'] == state
    ]

for state in states:
    for date in dates:
        if not Price.objects(date_time=date, region=state, price_type='BASIC'):
            print('Adding', state, date)
            # Basic settlement.
            rounded_time = pendulum.instance(date, tz=config.TZ)
            if rounded_time.minute > 30:
                rounded_time = rounded_time.start_of('hour').add(hours=1)
Пример #5
0
def bid_stack_dates():
    dates = BidStack.objects().distinct('trading_period')
    return jsonify([pendulum.instance(date).timestamp() for date in dates])
Пример #6
0
def process_bidstacks(start_date, end_date, timeseries={}):
    """ Derives competition indicators from bids submitted by generators.  """

    print("Processing Bidstacks")
    # Get bidstacks for every time period.
    query = BidStack.objects(trading_period__gte=start_date,
                             trading_period__lte=end_date).fields(
                                 trading_period=1, id=1)
    print('Retrieved Bidstacks')

    i = 0

    for bidstack in query:
        # Get the trading period label.
        dt = pendulum.instance(bidstack.trading_period)

        # if(dt.hour == 12 and dt.minute == 0 and dt.day %5 == 0):
        # Filter based on hour so we don't process tonnes of them
        if (dt.hour in RESEARCH_HOURS and dt.minute == 0):
            timeseries[dt] = {} if not dt in timeseries else timeseries[dt]
            print("Bid Analysis", dt)
            # Grab the bids and order in economic dispatch order.
            bidstack = BidStack.objects.get(id=bidstack.id)
            # simple_bids, srmc_bids, lrmc_bids = settle(bidstack)
            # print("Got bid stacks.")

            # Grab demand data.
            demand_req = Demand.objects(date_time=dt)
            regional_demand = {d.region: d.demand for d in demand_req}
            total_demand = int(
                float(
                    sum([
                        regional_demand[region] for region in regional_demand
                    ])))

            # Grab price data
            price_req = Price.objects(date_time=dt, price_type='AEMO_SPOT')
            regional_prices = {p.region: p.price for p in price_req}
            # print(regional_prices)
            weighted_average_price = float(
                sum([
                    regional_prices[p] * regional_demand[p]
                    for p in regional_prices
                ])) / float(total_demand)
            # print(weighted_average_price)

            # Get a dict of all the residual supply indices, augmented with max network flows.
            network_residual_supply_indices = get_network_extended_residual_supply_indices(
                bidstack, regional_demand)

            timeseries[dt]['weighted_average_price'] = weighted_average_price

            timeseries[dt]['demand_ALL'] = total_demand
            timeseries[dt]['datetime'] = dt
            timeseries[dt]['price'] = regional_prices

            for key in regional_demand:
                timeseries[dt]['demand_' + key] = regional_demand[key]
            for key in regional_prices:
                timeseries[dt]['price_' + key] = regional_prices[key]

            for state in STATES:
                # Get a dict of all the bid-based market shares for this time period
                generator_bid_shares = get_generator_bid_market_shares(
                    bidstack, state)

                # Get a dict of all the residual supply indices for this time period
                residual_supply_indices = get_residual_supply_indices(
                    bidstack, total_demand, state)

                # Get a dict of all the pivotal supplier indices for this time period.
                pivotal_supplier_indices = get_pivotal_supplier_indices(
                    bidstack, total_demand, state)

                if state != "ALL":
                    # Get a dict of all firm weighted offers
                    firm_weighted_offer_prices = get_firm_volume_weighted_offer_price(
                        bidstack, state)
                    for firm in firm_weighted_offer_prices:
                        timeseries[dt][
                            firm.lower() + '_weighted_offer_price_' +
                            state] = firm_weighted_offer_prices[firm]

                    # Calculate average NERSI for all firms in the state.
                    for firm in network_residual_supply_indices[state]:
                        timeseries[dt][
                            firm.lower() + '_nersi_' +
                            state] = network_residual_supply_indices[state][
                                firm]
                    timeseries[dt]['average_nersi_' + state] = float(
                        sum([
                            network_residual_supply_indices[state][firm]
                            for firm in network_residual_supply_indices[state]
                        ])) / float(len(
                            network_residual_supply_indices[state]))
                    timeseries[dt]['minimum_nersi_' + state] = min([
                        network_residual_supply_indices[state][firm]
                        for firm in network_residual_supply_indices[state]
                    ])

                # Record results.
                timeseries[dt]['hhi_bids_' +
                               state] = get_hhi(generator_bid_shares)
                timeseries[dt]['entropy_bids_' +
                               state] = get_entropy(generator_bid_shares)
                timeseries[dt]['four_firm_concentration_ratio_bids_' +
                               state] = get_four_firm_concentration_ratio(
                                   generator_bid_shares)
                timeseries[dt]['average_rsi_' + state] = float(
                    sum([
                        residual_supply_indices[firm]
                        for firm in residual_supply_indices
                    ])) / float(len([f for f in residual_supply_indices]))
                timeseries[dt]['minimum_rsi_' + state] = min([
                    residual_supply_indices[firm]
                    for firm in residual_supply_indices
                ])
                timeseries[dt]['sum_psi_' + state] = sum([
                    pivotal_supplier_indices[firm]
                    for firm in pivotal_supplier_indices
                ])

                for firm in residual_supply_indices:
                    timeseries[dt][firm.lower() + '_rsi_' +
                                   state] = residual_supply_indices[firm]
                for firm in pivotal_supplier_indices:
                    timeseries[dt][firm.lower() + '_psi_' +
                                   state] = pivotal_supplier_indices[firm]

    print("Finished Processing Bidstack")
    return timeseries
Пример #7
0
def process_bidstacks(start_date, end_date):
    print("Processing Bidstacks")
    request = BidStack.objects(trading_period__gte=start_date,
                               trading_period__lte=end_date).fields(
                                   trading_period=1, id=1)
    print('Retrieved Bidstacks')

    i = 0
    timeseries = {}

    for bidstack in request:
        # Get the trading period label.
        dt = pendulum.instance(bidstack.trading_period)

        # if(dt.hour == 12 and dt.minute == 0 and dt.day %5 == 0):
        if (dt.hour in [0, 6, 12, 18, 24] and dt.minute == 0):
            print(dt)
            # Grab the bids and order in economic dispatch order.
            bidstack = BidStack.objects.get(id=bidstack.id)
            simple_bids, srmc_bids, lrmc_bids = settle(bidstack)
            # print("Got bid stacks.")

            # Grab demand data.
            demand_req = Demand.objects(date_time=dt)
            regional_demand = {d.region: d.demand for d in demand_req}
            total_demand = int(
                float(
                    sum([
                        regional_demand[region] for region in regional_demand
                    ])))

            # Grab price data
            price_req = Price.objects(date_time=dt, price_type='AEMO_SPOT')
            regional_prices = {p.region: p.price for p in price_req}
            weighted_average_price = float(
                sum([
                    regional_prices[p] * regional_demand[p]
                    for p in regional_prices
                ])) / float(total_demand)

            # Grab the representative strings.
            bids_string = get_representative_string(simple_bids, total_demand)
            srmc_string = get_representative_string(srmc_bids, total_demand)
            lrmc_string = get_representative_string(lrmc_bids, total_demand)
            # print("Got Representative Strings")

            metrics = {
                'srmc_string_comp':
                compare_representative_strings(bids_string, srmc_string),
                'lrmc_string_comp':
                compare_representative_strings(bids_string, lrmc_string),
                'srmc_fraction_MWh_different':
                get_fraction_MWh_different(bids_string, srmc_string,
                                           total_demand),
                'lrmc_fraction_MWh_different':
                get_fraction_MWh_different(bids_string, lrmc_string,
                                           total_demand),
                'datetime':
                dt,
                'price':
                regional_prices,
                'weighted_average_price':
                weighted_average_price,
                'total_demand':
                total_demand,
                'regional_demand':
                regional_demand,
                'strike': {
                    'simple': get_strike_price(simple_bids, total_demand),
                    'srmc': get_strike_price(srmc_bids, total_demand),
                    'lrmc': get_strike_price(lrmc_bids, total_demand),
                }
            }
            timeseries[dt] = metrics

    print("Finished Processing Bidstack")
    return timeseries