def _make_start_time(self, arg): if arg['<date>'] and arg['<time>']: datetime_str = '{} {}'.format(arg['<date>'], arg['<time>']) return parse_to_aware(datetime_str) if self.first_match: return to_local(self.first_match.time) raise Exception("*** no date-time specified or match record")
def show_strategy_detail(self, name): s = self._query_strategy(name) created_time = to_local(s.created_time) print ' Name:', s.name print ' Symbol:', s.symbol print ' Cost:', s.cost print 'Created:', created_time.strftime(TIME_FORMAT)
def open_order(self, strategy, open_type, open_time, open_price, size): self._verify_open_type(open_type) type_symbol = Order.get_open_type_symbol(open_type) new_order = Order.objects.create(strategy=strategy, open_type=type_symbol, open_time=open_time, open_price=open_price, size=size, state='O') local_time = to_local(open_time) local_time = local_time.strftime(TIME_FORMAT) print "Opened a {} order #{} at [{}], price is {}".format( open_type.upper(), new_order.pk, local_time, open_price)
def show_active_orders(self): print ' Name:', self.strategy.name print ' Symbol:', self.strategy.symbol print ' Method:', self.method print 'Active orders:' active_orders = Order.objects.filter(strategy=self.strategy, state='O') if not active_orders.exists(): print '--- None ---' return # show orders in table print '{} | {:>5} | {:^19} | {:>6} | {} |'.format( 'Ticket', 'Type', 'Time', 'Price', 'Size') print '-' * 54 for order in active_orders: temp = to_local(order.open_time) formatted_open_time = temp.strftime(TIME_FORMAT) print '{:>6} | {:>5} | {} | {:>6} | {:>4} |'.format( order.pk, order.get_open_type_display(), formatted_open_time, order.open_price, order.size)
def close_order(self, ticket, close_time, close_price): order = Order.objects.get(pk=ticket) if order.get_state_display() == 'Close': raise Exception("*** order has already been closed") if order.open_time > close_time: raise Exception("*** close time is before open time") order.close_time = close_time order.close_price = close_price order.state = 'C' if order.get_open_type_display() == 'Long': profit = order.close_price - order.open_price if order.get_open_type_display() == 'Short': profit = order.open_price - order.close_price order.per_size_gross_profit = profit order.gross_profit = profit * order.size order.save() local_time = to_local(close_time) local_time = local_time.strftime(TIME_FORMAT) print "Closed order #{} at [{}], price is {}".format( ticket, local_time, close_price) print "Diff is {}, total diff is {}".format( order.per_size_gross_profit, order.gross_profit)
def _format_to_local(self, datetime_obj): local = to_local(datetime_obj) return local.strftime(TIME_FORMAT)
def format_close_time(point): if not point.close_time: return None local = to_local(point.close_time) return local.strftime(TIME_FORMAT)
def format_open_time(point): local = to_local(point.open_time) return local.strftime(TIME_FORMAT)