class Player(object): def __init__(self, name): self.wallet = Wallet(100, name) self.cards = [] self.name = name def pick_card(self, card): self.cards.append(card) def clean_hand(self): self.cards = [] def get_sum_from_cards(self): '''retorna a soma das cartas de acordo com a regra do blackjack''' sum = 0 aces = 0 # guarda a quantidade de aces for card in self.cards: number = card.number if number == 1: # se for ace: aces += 1 elif number > 10: sum += 10 else: sum += number if aces: sum += sum + aces - 1 # soma a quantidade de aces -1 if (sum + 11) <= 21: sum += 11 else: sum += 1 return sum def show_hand(self): print('Cards from %s' % (self.name)) for card in self.cards: print(card) print('sum: %d' % self.get_sum_from_cards()) def pay_bet(self,bet): self.wallet.add(bet) def show_wallet(self): print(self.wallet)
class CTrader(): def __init__(self, budget, interval, currency): self.api = API() self.wallet = Wallet(currency) self.analyzer = Analyzer(budget) self.APIS = "http://api.coinmarketcap.com/v1/ticker/" self.running = True self.BUDGET = int(budget) # USD self.investing_pr_round = self.BUDGET * 0.03 # 3% self.currency = currency self.interval = interval self.talent = 0.00005 self.wallet.create_setting(self.BUDGET) print colored("[+] cryptomate initialized.\r\n", "yellow") # TALKER TO API def get_data(self, currency): r = requests.get(self.APIS + currency) r = r.json() return r def get_data_specific(self, currency, specific): r = requests.get(self.APIS + currency) r = r.json() return r[0][specific] def buy_currency(self, investing_pr_round): #calc how much you can buy current_price = self.get_data_specific(self.currency, self.api.price) amount = float(investing_pr_round) / float(current_price) # place order self.wallet.spend_money(investing_pr_round) self.wallet.add(amount, self.currency) return amount def sell_currency(self, cryp_amount): current_price = self.get_data_specific(self.currency, self.api.price) money = float(cryp_amount) * float(current_price) self.wallet.get_money(money) self.wallet.sub(cryp_amount, self.currency) def check_interval(self, mins, maxs): if mins >= maxs: self.running = False else: self.running = True def get_wallet_info(self): price = self.get_data_specific(self.currency, self.api.price) self.wallet.info(price) def reset(self): self.wallet.reset() def observe(self, intervals, observe=False): i = 1 while self.running: data = self.get_data(self.currency) MA = self.analyzer.review_data(data) print colored( str(datetime.datetime.now()) + " | PERIOD " + str(i) + " | " + str(data[0][self.api.price]) + "$ | WEEK " + str(data[0][self.api.change7d]) + "%" + " | HOUR " + str(data[0][self.api.change1h]) + "%" + " | SMA " + str(MA) + " |", "cyan") # here you implement the logic of the trading algorithm you've written to decide whether to buy/sell self.check_interval(i, intervals) i += 1 time.sleep(self.interval)
def test_add(): my_wallet = Wallet() my_wallet.add(10) assert my_wallet.balance == 10, 'Should be 10'
if(sys.argv[2] == "e"): wallet.editWallet() elif(sys.argv[2] == "list"): if(len(sys.argv) > 3): if(sys.argv[3] == "distribution" or sys.argv[3] == "dist" or sys.argv[3] == "percent" ): wallet.distribution() elif(sys.argv[3] == "value"): if(len(sys.argv) > 4): wallet.distributionAndWorth(sys.argv[4]) else: print("Need currency argument") else: wallet.listWallet() elif(sys.argv[2] == "add"): if(len(sys.argv) > 4): wallet.add(sys.argv[3], sys.argv[4]) else: print("Need more arguments") elif(sys.argv[2] == "remove"): if(len(sys.argv) > 4): wallet.add("-" + sys.argv[3], sys.argv[4]) else: print("Need more arguments") else: sumWallet = wallet.calculateWorth(sys.argv[2]) if(sumWallet == 0): print("Unkown currency or empty wallet") print(str(sumWallet) + sys.argv[2].upper()) else: print("Wallet command requires additional arguments")