import pandas
import matplotlib.pyplot as plt

from op_futures.op_strategy_parser.op_futures_breakout_strategy import FuturesBreakOutStrategy
from op_futures.op_strategy_parser.op_futures_double_vwap_strategy import FuturesDoubleVwapStrategy
from op_futures.op_strategy_parser.op_futures_double_vwap_breakout_strategy import FuturesDoubleVwapBreakOutStrategy

class ContractParser:

    def __init__(self, contract_name: str, source_contract_df: pandas.DataFrame = None):

        self.contract_name: str = contract_name
        self.source_contract_df: pandas.DataFrame = source_contract_df


    def run_double_vwap_strategy(self):
        futures_double_vwap_strategy = FuturesDoubleVwapStrategy(contract_name=self.contract_name, fast_windows=600, slow_windows=3600)
        [futures_double_vwap_strategy.on_tick(tick={
            'UpdateTime': row.UpdateTime,
            'LastPrice': row.LastPrice,
            'Volume': row.Volume,
        }) for _, row in self.source_contract_df.iterrows()]


    def run_double_vwap_breakout_strategy(self):
        futures_double_vwap_breakout_strategy = FuturesDoubleVwapBreakOutStrategy(contract_name=self.contract_name, fast_windows=600, slow_windows=3600)
        [futures_double_vwap_breakout_strategy.on_tick(tick={
            'UpdateTime': row.UpdateTime,
            'LastPrice': row.LastPrice,
            'Volume': row.Volume,
        }) for _, row in self.source_contract_df.iterrows()]


    def run_breakout_strategy(self):
        futures_breakout_strategy = FuturesBreakOutStrategy(contract_name=self.contract_name, windows=3600)
        [futures_breakout_strategy.on_tick(tick={
            'UpdateTime': row.UpdateTime,
            'LastPrice': row.LastPrice,
            'Volume': row.Volume,
        }) for _, row in self.source_contract_df.iterrows()]

        # -- draw picture.
        self.__draw_time_openInterest_picture()

    def __draw_time_openInterest_picture(self):
        plt.plot(self.source_contract_df.UpdateTime, self.source_contract_df.OpenInterest, alpha=0.75, color='b')

        plt.title(f'{self.contract_name}_UpdateTime_OpenInterest')
        plt.xlabel("UpdateTime")
        plt.ylabel("OpenInterest")
        plt.gca().get_xaxis().set_visible(False)

        plt.savefig(f'{self.contract_name}_UpdateTime_OpenInterest.png')
        plt.close("all")


if __name__ == '__main__':
    _contract_name = 'hc2410'

    _contract_df = pandas.read_csv(f'{_contract_name}.csv')
    _contract_df.columns = [
        'Timestamp',
        'UpdateTime',
        'UpdateMillisec',
        'InstrumentID',
        'ExchangeInstrument',
        'OpenPrice',
        'HighestPrice',
        'LowestPrice',
        'LastPrice',
        'Volume',
        'Turnover',
        'OpenInterest',
        'UpperLimitPrice',
        'LowerLimitPrice',
        'PreClosePrice',
        'PreOpenInterest',
        'PriceTick',
        'VolumeMultiple',
        'ActionDay',
        'AskPrice1', 'AskPrice2', 'AskPrice3', 'AskPrice4', 'AskPrice5',
        'BidPrice1', 'BidPrice2', 'BidPrice3', 'BidPrice4', 'BidPrice5',
        'AskSize1', 'AskSize2', 'AskSize3', 'AskSize4', 'AskSize5',
        'BidSize1', 'BidSize2', 'BidSize3', 'BidSize4', 'BidSize5'
    ]

    ContractParser(contract_name=_contract_name, source_contract_df=_contract_df).run_breakout_strategy()
    # ContractParser(contract_name=_contract_name, source_contract_df=_contract_df).run_double_vwap_strategy()
    # ContractParser(contract_name=_contract_name, source_contract_df=_contract_df).run_double_vwap_breakout_strategy()
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.