def execute_schedule_operations(schedule_op): print("Executing Schedule operation") print(schedule_op) user = schedule_op.user symbol = schedule_op.symbol quantity = schedule_op.quantity operation = schedule_op.operation constraint_max_price = schedule_op.max_price constraint_min_price = schedule_op.min_price profile = Profile.objects.get(user=user) price = f_shares.get_price(symbol) amount = quantity * price if not f_profile.has_funds(profile, amount): return if price < constraint_min_price or price > constraint_max_price: schedule_op.delete() return f_profile.payoff_balance(profile, amount) share = Share.objects.create( symbol = symbol, quantity = quantity, price = price, operation = operation, portfolio = profile.portfolio ) schedule_op.delete() f_profile.save(profile)
def validate(self, data): profile = self.context['profile'] symbol = data['symbol'] if not f_shares.exists(symbol): raise serializers.ValidationError("Symbol doesn't exist") amount = data['quantity'] * f_shares.get_price(symbol) if not f_profile.has_funds(profile, amount): raise serializers.ValidationError( "Your funds aren't enough as required") return data
def create(self, data): profile = self.context['profile'] shares = Share.objects.get(ref=data['ref'], portfolio=profile.portfolio) shares.closed = True amount = shares.quantity * f_shares.get_price(shares.symbol) f_profile.close_operation(profile, amount) f_profile.save(profile) shares.save() return shares
def create(self, data): profile = self.context['profile'] symbol = data['symbol'] quant = data['quantity'] op = self.context['op'] price = f_shares.get_price(symbol) amount = quant * price f_profile.payoff_balance(profile, amount) shares = Share.objects.create(symbol=symbol, quantity=quant, price=price, operation=op, portfolio=profile.portfolio) f_profile.save(profile) return shares
def validate(self, data): user = self.context['user'] symbol = data['symbol'] schedule_start = data['schedule_start'] operation = data['operation'] if not f_shares.exists(symbol): raise serializers.ValidationError("Symbol doesn't exist") amount = data['quantity'] * f_shares.get_price(symbol) profile = Profile.objects.get(user=user) if not f_profile.has_funds(profile, amount): raise serializers.ValidationError( "Your funds aren't enough as required") if schedule_start < timezone.localtime(): print(schedule_start) print(timezone.localtime()) print(schedule_start >= timezone.localtime()) raise serializers.ValidationError("Schedule start invalid") if data['min_price'] < 0: raise serializers.ValidationError( "Minimun price lesss than zero, seriously?") if data['max_price'] > 9999999999: raise serializers.ValidationError("Maximun price problem") return data
def price(self, request): symbol = request.data['symbol'] return Response(f_shares.get_price(symbol))