def spreads_json(request, date, context=None): """ Get positions for that date then make each position into spreads return it using json data format :param request: dict :param date: str :param context: str :return: render """ spreads = None if date and context: # get all date position positions = models.Position.objects.filter(date=date) spreads = list() for position in positions: spread = Identify(position=position).spread """ :type: Spread """ if spread and spread.context == context: spreads.append('%s' % spread.json()) # todo: until here, convert into json format # todo: go into each spread, do json output return HttpResponse( '[' + ','.join(spreads) + ']', content_type='application/json' )
def is_methods(self, key, attr, expect): """ Use for each test is_strategy :param key: int :param attr: str :param expect: bool """ self.ready_all(key) for position in Position.objects.all(): pos_set = PositionSet(position) self.identify = Identify(pos_set) method = getattr(self.identify, attr) result = method() print 'symbol: %s, stock qty: %d, options len: %d, result: %s' \ % (position.symbol, self.identify.pos_set.stock.quantity, self.identify.pos_set.options.count(), result) self.assertEqual(result, expect) Position.objects.all().delete() print '\n' + '-' * 100 + '\n'
def spreads_json(request, date, context=None): """ Get positions for that date then make each position into spreads return it using json data format :param request: dict :param date: str :param context: str :return: render """ spreads = None if date and context: # get all date position positions = models.Position.objects.filter(date=date) spreads = list() for position in positions: spread = Identify(position=position).spread """ :type: Spread """ if spread and spread.context == context: spreads.append('%s' % spread.json()) # todo: until here, convert into json format # todo: go into each spread, do json output return HttpResponse('[' + ','.join(spreads) + ']', content_type='application/json')
def test_identify(self): """ Test core method identify """ self.ready_all() for position in Position.objects.all(): pos_set = PositionSet(position) self.identify = Identify(pos_set) self.identify.identify() print self.identify.spread
def symbols_json(request, date): """ Return a list of symbols with fields [state, status, symbol, pl_open] :param request: dict :param date: str :return: render json """ symbols = [] if date: positions = models.Position.objects.filter(date=date) if positions.exists(): for position in positions: spread = Identify(position).spread """ :type: Spread """ symbols.append({ 'symbol': position.symbol, 'spread': spread.__name, #'status': spread.current_status(), #'pl_open': pl_open, #'state': symbol_state(status, instrument.pl_open) }) # todo: later return HttpResponse(symbols, content_type='application/json')
class TestIdentify(TestReadyUp): def is_methods(self, key, attr, expect): """ Use for each test is_strategy :param key: int :param attr: str :param expect: bool """ self.ready_all(key) for position in Position.objects.all(): pos_set = PositionSet(position) self.identify = Identify(pos_set) method = getattr(self.identify, attr) result = method() print 'symbol: %s, stock qty: %d, options len: %d, result: %s' \ % (position.symbol, self.identify.pos_set.stock.quantity, self.identify.pos_set.options.count(), result) self.assertEqual(result, expect) Position.objects.all().delete() print '\n' + '-' * 100 + '\n' def test_is_closed(self): """ Test identity with closed only positions """ self.is_methods(key=0, attr='is_closed', expect=True) self.is_methods(key=1, attr='is_closed', expect=False) def test_is_stock(self): """ Test identity with stock only positions """ self.is_methods(key=1, attr='is_stock', expect=True) self.is_methods(key=0, attr='is_stock', expect=False) def test_is_hedge(self): """ Test identity with hedge (stock + options) positions """ self.is_methods(key=2, attr='is_hedge', expect=True) self.is_methods(key=0, attr='is_hedge', expect=False) def test_is_one_leg(self): """ Test identity with one leg option positions """ self.is_methods(key=3, attr='is_one_leg', expect=True) self.is_methods(key=0, attr='is_one_leg', expect=False) def test_is_two_legs(self): """ Test identity with two leg options positions """ self.is_methods(key=4, attr='is_two_legs', expect=True) self.is_methods(key=0, attr='is_two_legs', expect=False) def test_is_three_legs(self): """ Test identity with three leg options positions """ self.is_methods(key=5, attr='is_three_legs', expect=True) self.is_methods(key=0, attr='is_three_legs', expect=False) def test_is_four_legs(self): """ Test identity with four leg options positions """ self.is_methods(key=6, attr='is_four_legs', expect=True) self.is_methods(key=0, attr='is_four_legs', expect=False) def test_identify(self): """ Test core method identify """ self.ready_all() for position in Position.objects.all(): pos_set = PositionSet(position) self.identify = Identify(pos_set) self.identify.identify() print self.identify.spread