Skip to content

Instantly share code, notes, and snippets.

@mahdi13
Created June 10, 2020 17:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mahdi13/4b725e77516c1d736c8585f787e5d910 to your computer and use it in GitHub Desktop.
Save mahdi13/4b725e77516c1d736c8585f787e5d910 to your computer and use it in GitHub Desktop.
import random
import time
class Market:
def __init__(self):
self.time = 0
self.change = 0.0
self.market_price = 10000.0
def tick(self):
self.time += 1
# self.market_price += self.market_price / 100.0 * random.choice([1.0, -1.0])
# self.market_price += 100.0 * random.choice([1.0, -1.0])
self.change = 2 * random.choice([1.0, -1.0])
self.market_price += self.change
print(f'price: {int(self.market_price)} Change: {int(self.change)}')
class Agent:
def __init__(self, market):
self.market = market
self.fee = 0.001
self.usd_starting_balance = 10000.0 * 10.0
self.btc_starting_balance = 1.0
self.usd_starting_round_balance = self.usd_starting_balance
self.usd_balance = self.usd_starting_balance
self.btc_balance = self.btc_starting_balance
self.m_start = 1
self.m_co = 2.0
self.current_bet = self.m_start
self.last_move = 'b'
self.round = 0
self.last_amount = self.m_start / 100.0
def finish(self):
# raise RuntimeError('Finish :)')
# print(f'Profit ----------->>>>>>>>>>>>>>>>>>>> {self.usd_balance - self.usd_starting_round_balance} USD')
first_totaaal = self.btc_starting_balance * self.market.market_price + self.usd_starting_balance
now_totaaal = self.btc_balance * self.market.market_price + self.usd_balance
print(f'Profit ----------->>>>>>>>>>>>>>>>>>>> {now_totaaal - first_totaaal} USD')
self.last_amount = self.m_start / 100.0
self.round = 0
self.usd_starting_round_balance = self.usd_balance
def go_further(self):
self.last_move = random.choice(['s', 'b'])
self.last_amount = self.last_amount * self.m_co
self.trade(self.last_amount, self.last_move)
self.round += 1
def trade(self, amount, side):
if side == 'b':
self.btc_balance += amount
self.usd_balance -= amount * self.market.market_price
# TODO: FEE!!!
print(f'+++ {"{:.5f}".format(amount)} -> BTC: {"{:.4f}".format(self.btc_balance)} USD: {int(self.usd_balance)}')
if side == 's':
self.btc_balance -= amount
self.usd_balance += amount * self.market.market_price
# TODO: FEE!!!
print(f'--- {"{:.5f}".format(abs(amount))} -> BTC: {"{:.4f}".format(self.btc_balance)} USD: {int(self.usd_balance)}')
def reset_btc(self):
# Sell enough btc to be the same as the start
pass
def tick(self):
if self.round > 1 and self.market.change > 0 and self.last_move == 'b':
# self.trade(self.btc_balance - self.btc_starting_balance, 's')
self.finish()
elif self.round > 1 and self.market.change < 0 and self.last_move == 's':
# self.trade(self.btc_starting_balance - self.btc_balance, 'b')
self.finish()
else:
self.go_further()
if __name__ == '__main__':
market = Market()
agent = Agent(market)
while True:
market.tick()
agent.tick()
time.sleep(0.2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment