Exemplo n.º 1
0
 def create(self, request):
     serializer = CancelOrderSerializer(data=request.DATA)
     if serializer.is_valid():
         object = serializer.save()
         TradeService.cancel_order(**object)
         result = serializer.data
         return Response(result, status=status.HTTP_202_ACCEPTED)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 2
0
 def create(self, request):
     serializer = CancelOrderSerializer(data=request.DATA)
     if serializer.is_valid():
         object = serializer.save()
         TradeService.cancel_order(**object)
         result = serializer.data
         return Response(result, status=status.HTTP_202_ACCEPTED)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 3
0
 def create(self, request):
     serializer = PositionCloseSerializer(data=request.DATA)
     if serializer.is_valid():
         object = serializer.save()
         try:
             TradeService.close_position(**object)
             result = serializer.data
             result.update({'last_client_trade':object['position'].clienttrade_set.order_by('-time')[0].id})
         except WrongAmount:
             raise WrongAmountApi
         return Response(result, status=status.HTTP_202_ACCEPTED)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 4
0
 def create(self, request):
     serializer = PositionCloseSerializer(data=request.DATA)
     if serializer.is_valid():
         object = serializer.save()
         try:
             TradeService.close_position(**object)
             result = serializer.data
             result.update({
                 'last_client_trade':
                 object['position'].clienttrade_set.order_by('-time')[0].id
             })
         except WrongAmount:
             raise WrongAmountApi
         return Response(result, status=status.HTTP_202_ACCEPTED)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 5
0
 def get_take_profit_distance(self, obj):
     return obj.take_profit and TradeService._rate_to_distance_convert(
                 obj.side,
                 obj.take_profit,
                 obj.open_rate,
                 obj.instrument,
                 True
             )
Exemplo n.º 6
0
 def get_stop_loss_distance(self, obj):
     # side, distance_rate, open_rate, instrument
     return TradeService._rate_to_distance_convert(
                 obj.side,
                 obj.stop_loss,
                 obj.open_rate,
                 obj.instrument
             )
Exemplo n.º 7
0
 def create(self, request):
     serializer = RequiredMarginSerializer(data=request.DATA)
     if serializer.is_valid():
         object = serializer.save()
         object['stop_loss_rate'] = object['stop_loss_distance']
         del object['stop_loss_distance']
         margin = TradeService._calculate_margin(**object)
         return Response(margin, status=status.HTTP_201_CREATED)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 8
0
 def create(self, request):
     serializer = RequiredMarginSerializer(data=request.DATA)
     if serializer.is_valid():
         object = serializer.save()
         object['stop_loss_rate'] = object['stop_loss_distance']
         del object['stop_loss_distance']
         margin = TradeService._calculate_margin(**object)
         return Response(margin, status=status.HTTP_201_CREATED)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 9
0
 def save(self):
     self.object['side'] = int(self.object['side'])
     self.object['instrument'] = Instrument.objects.get(url_slug=self.object['instrument'])
     self.object['stop_loss_distance'] = TradeService._distance_to_rate_convert(
         side=int(self.object['side']),
         distance=int(self.object['stop_loss_distance']),
         rate=Decimal(self.object['rate']),
         instrument=self.object['instrument']
     )
     return self.object
Exemplo n.º 10
0
 def create(self, request):
     serializer = PlaceOrderSerializer(data=request.DATA)
     if serializer.is_valid():
         object = serializer.save()
         object['user'] = request.user
         order_id = TradeService.place_conditional_order(**object)
         result = {'order_id': order_id}
         result.update(serializer.data)
         return Response(result, status=status.HTTP_201_CREATED)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 11
0
 def create(self, request):
     serializer = PlaceOrderSerializer(data=request.DATA)
     if serializer.is_valid():
         object = serializer.save()
         object['user'] = request.user
         order_id = TradeService.place_conditional_order(**object)
         result = {'order_id': order_id}
         result.update(serializer.data)
         return Response(result, status=status.HTTP_201_CREATED)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 12
0
 def get_upnl(self):
     try:
         if self.state in (consts.STATE_OPENED, consts.STATE_PARTIALLY_CLOSED):
             from trade.service import TradeService
             rate = TradeService.get_rates(self.instrument, self.user)
             if self.side == consts.TYPE_SELL:
                 current_rate = rate['buy']
                 multiplier = - 1
             else:
                 current_rate = rate['sell']
                 multiplier = 1
             return multiplier * (current_rate - self.open_rate) * self.amount
         else:
             return 0
     except:
         return 0
Exemplo n.º 13
0
 def create(self, request):
     serializer = PositionCreateSerializer(data=request.DATA)
     if serializer.is_valid():
         object = serializer.save()
         object['user'] = request.user
         try:
             position_id = TradeService.open_position(**object)
             result = {'position_id':position_id}
             result.update(serializer.data)
         except InstrumentNotTradeable:
             raise InstrumentNotTradeableApi
         except Overdraft:
             raise OverdraftApi
         except WrongAmount:
             instrument = object['instrument']
             raise WrongOpeningAmountApi(instrument.trade_size_increment, instrument.min_trade_size)
         return Response(result, status=status.HTTP_201_CREATED)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 14
0
 def get_upnl(self):
     try:
         if self.state in (consts.STATE_OPENED,
                           consts.STATE_PARTIALLY_CLOSED):
             from trade.service import TradeService
             rate = TradeService.get_rates(self.instrument, self.user)
             if self.side == consts.TYPE_SELL:
                 current_rate = rate['buy']
                 multiplier = -1
             else:
                 current_rate = rate['sell']
                 multiplier = 1
             return multiplier * (current_rate -
                                  self.open_rate) * self.amount
         else:
             return 0
     except:
         return 0
Exemplo n.º 15
0
 def create(self, request):
     serializer = PositionCreateSerializer(data=request.DATA)
     if serializer.is_valid():
         object = serializer.save()
         object['user'] = request.user
         try:
             position_id = TradeService.open_position(**object)
             result = {'position_id': position_id}
             result.update(serializer.data)
         except InstrumentNotTradeable:
             raise InstrumentNotTradeableApi
         except Overdraft:
             raise OverdraftApi
         except WrongAmount:
             instrument = object['instrument']
             raise WrongOpeningAmountApi(instrument.trade_size_increment,
                                         instrument.min_trade_size)
         return Response(result, status=status.HTTP_201_CREATED)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)