# PyAlgoTrade
# tick DataSeries encapsulation
# @author <EMAIL>
#calls the bar.Ticks in the same directory
# Copyright 2011-2015 <NAME>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
.. moduleauthor:: <NAME> <<EMAIL>>
"""

from pyalgotrade import dataseries


class TickDataSeries(dataseries.SequenceDataSeries):
    """A DataSeries of :class:`pyalgotrade.bar.Bar` instances.

    :param maxLen: The maximum number of values to hold.
        Once a bounded length is full, when new items are added, a corresponding number of items are discarded from the
        opposite end. If None then dataseries.DEFAULT_MAX_LEN is used.
    :type maxLen: int.
    """

    def __init__(self, maxLen=None):
        super(TickDataSeries, self).__init__(maxLen)
        self.__openDS = dataseries.SequenceDataSeries(maxLen)
        self.__closeDS = dataseries.SequenceDataSeries(maxLen)
        self.__highDS = dataseries.SequenceDataSeries(maxLen)
        self.__lowDS = dataseries.SequenceDataSeries(maxLen)
        self.__volumeDS = dataseries.SequenceDataSeries(maxLen)
        self.__adjCloseDS = dataseries.SequenceDataSeries(maxLen)
        self.__apDS = dataseries.SequenceDataSeries(maxLen)
        self.__bpDS = dataseries.SequenceDataSeries(maxLen)
        self.__avDS = dataseries.SequenceDataSeries(maxLen)
        self.__bvDS = dataseries.SequenceDataSeries(maxLen)
        self.__precloseDS = dataseries.SequenceDataSeries(maxLen)
        self.__boughtAmountDS = dataseries.SequenceDataSeries(maxLen)
        self.__soldAmountDS = dataseries.SequenceDataSeries(maxLen)
        self.__boughtVolumeDS = dataseries.SequenceDataSeries(maxLen)
        self.__soldVolumeDS = dataseries.SequenceDataSeries(maxLen)

        self.__extraDS = {}
        self.__useAdjustedValues = False

    def __getOrCreateExtraDS(self, name):
        ret = self.__extraDS.get(name)
        if ret is None:
            ret = dataseries.SequenceDataSeries(self.getMaxLen())
            self.__extraDS[name] = ret
        return ret

    def setUseAdjustedValues(self, useAdjusted):
        self.__useAdjustedValues = useAdjusted

    def append(self, bar):
        self.appendWithDateTime(bar.getDateTime(), bar)

    def appendWithDateTime(self, dateTime, bar):
        assert (dateTime is not None)
        assert (bar is not None)
        bar.setUseAdjustedValue(self.__useAdjustedValues)

        super(TickDataSeries, self).appendWithDateTime(dateTime, bar)

        self.__openDS.appendWithDateTime(dateTime, bar.getOpen())
        self.__closeDS.appendWithDateTime(dateTime, bar.getClose())
        self.__highDS.appendWithDateTime(dateTime, bar.getHigh())
        self.__lowDS.appendWithDateTime(dateTime, bar.getLow())
        self.__volumeDS.appendWithDateTime(dateTime, bar.getVolume())
        self.__adjCloseDS.appendWithDateTime(dateTime, bar.getAdjClose())

        self.__apDS.appendWithDateTime(dateTime, bar.getAp())
        self.__bpDS.appendWithDateTime(dateTime, bar.getBp())
        self.__avDS.appendWithDateTime(dateTime, bar.getAv())
        self.__bvDS.appendWithDateTime(dateTime, bar.getBv())
        self.__precloseDS.appendWithDateTime(dateTime, bar.getPreclose())
        self.__boughtAmountDS.appendWithDateTime(dateTime, bar.getBoughtAmount())
        self.__boughtVolumeDS.appendWithDateTime(dateTime, bar.getBoughtVolume())
        self.__soldAmountDS.appendWithDateTime(dateTime, bar.getSoldAmount())
        self.__soldVolumeDS.appendWithDateTime(dateTime, bar.getSoldVolume())
        # Process extra columns.
        for name, value in bar.getExtraColumns().items():
            extraDS = self.__getOrCreateExtraDS(name)
            extraDS.appendWithDateTime(dateTime, value)

    def getApDataSeries(self):
        """Returns a :class:`pyalgotrade.dataseries.DataSeries` with the open prices."""
        return self.__apDS

    def getBpDataSeries(self):
        """Returns a :class:`pyalgotrade.dataseries.DataSeries` with the close prices."""
        return self.__bpDS

    def getAvDataSeries(self):
        """Returns a :class:`pyalgotrade.dataseries.DataSeries` with the high prices."""
        return self.__avDS

    def getBvDataSeries(self):
        """Returns a :class:`pyalgotrade.dataseries.DataSeries` with the low prices."""
        return self.__bvDS

    def getPrecloseDataSeries(self):
        """Returns a :class:`pyalgotrade.dataseries.DataSeries` with the volume."""
        return self.__precloseDS

    def getBoughtAmountSeries(self):
        """Returns a :class:`pyalgotrade.dataseries.DataSeries` with the close prices."""
        return self.__boughtAmountDS

    def getBoughtVolumeDataSeries(self):
        """Returns a :class:`pyalgotrade.dataseries.DataSeries` with the high prices."""
        return self.__boughtVolumeDS

    def getSoldAmountDataSeries(self):
        """Returns a :class:`pyalgotrade.dataseries.DataSeries` with the low prices."""
        return self.__soldAmountDS

    def getSoldVolumeDataSeries(self):
        """Returns a :class:`pyalgotrade.dataseries.DataSeries` with the volume."""
        return self.__soldVolumeDS

    def getOpenDataSeries(self):
        """Returns a :class:`pyalgotrade.dataseries.DataSeries` with the open prices."""
        return self.__openDS

    def getCloseDataSeries(self):
        """Returns a :class:`pyalgotrade.dataseries.DataSeries` with the close prices."""
        return self.__closeDS

    def getHighDataSeries(self):
        """Returns a :class:`pyalgotrade.dataseries.DataSeries` with the high prices."""
        return self.__highDS

    def getLowDataSeries(self):
        """Returns a :class:`pyalgotrade.dataseries.DataSeries` with the low prices."""
        return self.__lowDS

    def getVolumeDataSeries(self):
        """Returns a :class:`pyalgotrade.dataseries.DataSeries` with the volume."""
        return self.__volumeDS

    def getAdjCloseDataSeries(self):
        """Returns a :class:`pyalgotrade.dataseries.DataSeries` with the adjusted close prices."""
        return self.__adjCloseDS

    def getPriceDataSeries(self):
        """Returns a :class:`pyalgotrade.dataseries.DataSeries` with the close or adjusted close prices."""
        if self.__useAdjustedValues:
            return self.__adjCloseDS
        else:
            return self.__closeDS

    def getExtraDataSeries(self, name):
        """Returns a :class:`pyalgotrade.dataseries.DataSeries` for an extra column."""
        return self.__getOrCreateExtraDS(name)

# PyAlgoTrade
# tick DataSeries encapsulation
# @author <EMAIL>
#calls the bar.Ticks in the same directory
# Copyright 2011-2015 <NAME>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
.. moduleauthor:: <NAME> <<EMAIL>>
"""

from pyalgotrade import dataseries


class TickDataSeries(dataseries.SequenceDataSeries):
    """A DataSeries of :class:`pyalgotrade.bar.Bar` instances.

    :param maxLen: The maximum number of values to hold.
        Once a bounded length is full, when new items are added, a corresponding number of items are discarded from the
        opposite end. If None then dataseries.DEFAULT_MAX_LEN is used.
    :type maxLen: int.
    """

    def __init__(self, maxLen=None):
        super(TickDataSeries, self).__init__(maxLen)
        self.__openDS = dataseries.SequenceDataSeries(maxLen)
        self.__closeDS = dataseries.SequenceDataSeries(maxLen)
        self.__highDS = dataseries.SequenceDataSeries(maxLen)
        self.__lowDS = dataseries.SequenceDataSeries(maxLen)
        self.__volumeDS = dataseries.SequenceDataSeries(maxLen)
        self.__adjCloseDS = dataseries.SequenceDataSeries(maxLen)
        self.__apDS = dataseries.SequenceDataSeries(maxLen)
        self.__bpDS = dataseries.SequenceDataSeries(maxLen)
        self.__avDS = dataseries.SequenceDataSeries(maxLen)
        self.__bvDS = dataseries.SequenceDataSeries(maxLen)
        self.__precloseDS = dataseries.SequenceDataSeries(maxLen)
        self.__boughtAmountDS = dataseries.SequenceDataSeries(maxLen)
        self.__soldAmountDS = dataseries.SequenceDataSeries(maxLen)
        self.__boughtVolumeDS = dataseries.SequenceDataSeries(maxLen)
        self.__soldVolumeDS = dataseries.SequenceDataSeries(maxLen)

        self.__extraDS = {}
        self.__useAdjustedValues = False

    def __getOrCreateExtraDS(self, name):
        ret = self.__extraDS.get(name)
        if ret is None:
            ret = dataseries.SequenceDataSeries(self.getMaxLen())
            self.__extraDS[name] = ret
        return ret

    def setUseAdjustedValues(self, useAdjusted):
        self.__useAdjustedValues = useAdjusted

    def append(self, bar):
        self.appendWithDateTime(bar.getDateTime(), bar)

    def appendWithDateTime(self, dateTime, bar):
        assert (dateTime is not None)
        assert (bar is not None)
        bar.setUseAdjustedValue(self.__useAdjustedValues)

        super(TickDataSeries, self).appendWithDateTime(dateTime, bar)

        self.__openDS.appendWithDateTime(dateTime, bar.getOpen())
        self.__closeDS.appendWithDateTime(dateTime, bar.getClose())
        self.__highDS.appendWithDateTime(dateTime, bar.getHigh())
        self.__lowDS.appendWithDateTime(dateTime, bar.getLow())
        self.__volumeDS.appendWithDateTime(dateTime, bar.getVolume())
        self.__adjCloseDS.appendWithDateTime(dateTime, bar.getAdjClose())

        self.__apDS.appendWithDateTime(dateTime, bar.getAp())
        self.__bpDS.appendWithDateTime(dateTime, bar.getBp())
        self.__avDS.appendWithDateTime(dateTime, bar.getAv())
        self.__bvDS.appendWithDateTime(dateTime, bar.getBv())
        self.__precloseDS.appendWithDateTime(dateTime, bar.getPreclose())
        self.__boughtAmountDS.appendWithDateTime(dateTime, bar.getBoughtAmount())
        self.__boughtVolumeDS.appendWithDateTime(dateTime, bar.getBoughtVolume())
        self.__soldAmountDS.appendWithDateTime(dateTime, bar.getSoldAmount())
        self.__soldVolumeDS.appendWithDateTime(dateTime, bar.getSoldVolume())
        # Process extra columns.
        for name, value in bar.getExtraColumns().items():
            extraDS = self.__getOrCreateExtraDS(name)
            extraDS.appendWithDateTime(dateTime, value)

    def getApDataSeries(self):
        """Returns a :class:`pyalgotrade.dataseries.DataSeries` with the open prices."""
        return self.__apDS

    def getBpDataSeries(self):
        """Returns a :class:`pyalgotrade.dataseries.DataSeries` with the close prices."""
        return self.__bpDS

    def getAvDataSeries(self):
        """Returns a :class:`pyalgotrade.dataseries.DataSeries` with the high prices."""
        return self.__avDS

    def getBvDataSeries(self):
        """Returns a :class:`pyalgotrade.dataseries.DataSeries` with the low prices."""
        return self.__bvDS

    def getPrecloseDataSeries(self):
        """Returns a :class:`pyalgotrade.dataseries.DataSeries` with the volume."""
        return self.__precloseDS

    def getBoughtAmountSeries(self):
        """Returns a :class:`pyalgotrade.dataseries.DataSeries` with the close prices."""
        return self.__boughtAmountDS

    def getBoughtVolumeDataSeries(self):
        """Returns a :class:`pyalgotrade.dataseries.DataSeries` with the high prices."""
        return self.__boughtVolumeDS

    def getSoldAmountDataSeries(self):
        """Returns a :class:`pyalgotrade.dataseries.DataSeries` with the low prices."""
        return self.__soldAmountDS

    def getSoldVolumeDataSeries(self):
        """Returns a :class:`pyalgotrade.dataseries.DataSeries` with the volume."""
        return self.__soldVolumeDS

    def getOpenDataSeries(self):
        """Returns a :class:`pyalgotrade.dataseries.DataSeries` with the open prices."""
        return self.__openDS

    def getCloseDataSeries(self):
        """Returns a :class:`pyalgotrade.dataseries.DataSeries` with the close prices."""
        return self.__closeDS

    def getHighDataSeries(self):
        """Returns a :class:`pyalgotrade.dataseries.DataSeries` with the high prices."""
        return self.__highDS

    def getLowDataSeries(self):
        """Returns a :class:`pyalgotrade.dataseries.DataSeries` with the low prices."""
        return self.__lowDS

    def getVolumeDataSeries(self):
        """Returns a :class:`pyalgotrade.dataseries.DataSeries` with the volume."""
        return self.__volumeDS

    def getAdjCloseDataSeries(self):
        """Returns a :class:`pyalgotrade.dataseries.DataSeries` with the adjusted close prices."""
        return self.__adjCloseDS

    def getPriceDataSeries(self):
        """Returns a :class:`pyalgotrade.dataseries.DataSeries` with the close or adjusted close prices."""
        if self.__useAdjustedValues:
            return self.__adjCloseDS
        else:
            return self.__closeDS

    def getExtraDataSeries(self, name):
        """Returns a :class:`pyalgotrade.dataseries.DataSeries` for an extra column."""
        return self.__getOrCreateExtraDS(name)

# PyAlgoTrade # tick DataSeries encapsulation # @author <EMAIL> #calls the bar.Ticks in the same directory # Copyright 2011-2015 <NAME> # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # #   http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. .. moduleauthor:: <NAME> <<EMAIL>> A DataSeries of :class:`pyalgotrade.bar.Bar` instances.      :param maxLen: The maximum number of values to hold.         Once a bounded length is full, when new items are added, a corresponding number of items are discarded from the         opposite end. If None then dataseries.DEFAULT_MAX_LEN is used.     :type maxLen: int. # Process extra columns. Returns a :class:`pyalgotrade.dataseries.DataSeries` with the open prices. Returns a :class:`pyalgotrade.dataseries.DataSeries` with the close prices. Returns a :class:`pyalgotrade.dataseries.DataSeries` with the high prices. Returns a :class:`pyalgotrade.dataseries.DataSeries` with the low prices. Returns a :class:`pyalgotrade.dataseries.DataSeries` with the volume. Returns a :class:`pyalgotrade.dataseries.DataSeries` with the close prices. Returns a :class:`pyalgotrade.dataseries.DataSeries` with the high prices. Returns a :class:`pyalgotrade.dataseries.DataSeries` with the low prices. Returns a :class:`pyalgotrade.dataseries.DataSeries` with the volume. Returns a :class:`pyalgotrade.dataseries.DataSeries` with the open prices. Returns a :class:`pyalgotrade.dataseries.DataSeries` with the close prices. Returns a :class:`pyalgotrade.dataseries.DataSeries` with the high prices. Returns a :class:`pyalgotrade.dataseries.DataSeries` with the low prices. Returns a :class:`pyalgotrade.dataseries.DataSeries` with the volume. Returns a :class:`pyalgotrade.dataseries.DataSeries` with the adjusted close prices. Returns a :class:`pyalgotrade.dataseries.DataSeries` with the close or adjusted close prices. Returns a :class:`pyalgotrade.dataseries.DataSeries` for an extra column.