def main(argv): getEnv(argv) cryptsy_py = CryptsyPy(public=public, private=private) market_data = cryptsy_py.getMarkets() cryptsy_mongo = CryptsyMongo(host="192.168.1.33") last_trades = cryptsy_mongo.getLastTrades() if len(userMarketIds) > 0: market_ids = userMarketIds else: market_ids = set([int(last_trade['marketid']) for last_trade in last_trades]) for market_id in market_ids: market_name = next((market_name for market_name in market_data if int(market_data[market_name]) == market_id)) plot_diagram(market_name, market_id)
def plot_diagram(market_name, market_id): interval = timedelta(days=1, hours=4) cryptsy_mongo = CryptsyMongo(host="192.168.1.33") timeStart = datetime.utcnow() - interval cryptoCurrencyDataSamples = cryptsy_mongo.markets_collection.find( {"name": market_name, "lasttradetime": {"$gt": timeStart.strftime("%Y-%m-%d %H:%M:%S")}}) tradeData = [(cryptoCurrencySample['lasttradetime'], cryptoCurrencySample['lasttradeprice']) for cryptoCurrencySample in cryptoCurrencyDataSamples] uniqueTradeData = list(set(tradeData)) uniqueTradeData = sorted(uniqueTradeData, key=(lambda x: x[0])) times = [(datetime.strptime(tradeDataSample[0], '%Y-%m-%d %H:%M:%S') - epoch).total_seconds() for tradeDataSample in uniqueTradeData] prices = [float(tradeDataSample[1]) for tradeDataSample in uniqueTradeData] trades = cryptsy_mongo.trades_collection.find( {"marketid": str(market_id), "datetime": {"$gt": timeStart.strftime("%Y-%m-%d %H:%M:%S")}}) trade_samples = [] for trade in trades: trade_samples.append(((datetime.strptime(trade['datetime'], '%Y-%m-%d %H:%M:%S') - epoch).total_seconds(), float(trade['tradeprice']), trade['tradetype'])) buy_trade_times = [trade_sample[0] for trade_sample in trade_samples if trade_sample[2] == 'Buy'] buy_trade_price = [trade_sample[1] for trade_sample in trade_samples if trade_sample[2] == 'Buy'] sell_trade_times = [trade_sample[0] for trade_sample in trade_samples if trade_sample[2] == 'Sell'] sell_trade_price = [trade_sample[1] for trade_sample in trade_samples if trade_sample[2] == 'Sell'] times_x = [] prices_y = [] market_trend = cryptsy_mongo.calculateMarketTrend(market_name, market_id) for hour in range(0, 24): time_x = datetime.utcnow() - timedelta(hours=hour) price_y = getNormalizedEstimatedPrice(market_trend, time_x) times_x.append((toCryptsyServerTime(time_x) - epoch).total_seconds()) prices_y.append(price_y) plt.plot(times, prices) plt.plot(buy_trade_times, buy_trade_price, 'ro') plt.plot(sell_trade_times, sell_trade_price, 'go') plt.plot(times_x, prices_y, 'ko') plt.savefig("{}.png".format("{}-{}".format(market_name.replace('/', '-'), market_id)), format='png') plt.close()
def setUp(self): self.mongo_client = CryptsyMongo()
class TestSequenceFunctions(unittest.TestCase): def setUp(self): self.mongo_client = CryptsyMongo() def test_simple(self): self.mongo_client.truncate_market_trend_collection() market_trend = self.mongo_client.calculateMarketTrend(market_name="LTC/BTC", market_id=3) self.assertIsNotNone(market_trend) def test_recent(self): self.mongo_client.truncate_market_trend_collection() num_market_trend = self.mongo_client.market_trend_collection.find().count() self.assertEqual(num_market_trend, 0) market_trend_1 = self.mongo_client.getRecentMarketTrend(market_name="LTC/BTC", market_id=3) num_market_trend = self.mongo_client.market_trend_collection.find().count() self.assertEqual(num_market_trend, 1) self.assertIsNotNone(market_trend_1) market_trend_2 = self.mongo_client.getRecentMarketTrend(market_name="LTC/BTC", market_id=3) num_market_trend = self.mongo_client.market_trend_collection.find().count() self.assertEqual(num_market_trend, 1) self.assertEqual(market_trend_1, market_trend_2) def test_recent(self): self.mongo_client.getRecentMarketTrend(market_name="LTC/BTC", market_id=3, timedelta=timedelta(minutes=1)) self.mongo_client.getRecentMarketTrend(market_name="DOGE/BTC", market_id=132, timedelta=timedelta(minutes=1)) self.mongo_client.getRecentMarketTrend(market_name="TEK/BTC", market_id=114, timedelta=timedelta(minutes=1))
def main(argv): global public global private getEnv(argv) global cryptsy_client cryptsy_client = CryptsyPy(public, private) # cryptsy_mongo = CryptsyMongo(host="192.168.1.33") cryptsy_mongo = CryptsyMongo() recent_market_trends = cryptsy_mongo.getRecentMarketTrends() recent_trades = cryptsy_client.getRecentTrades() if recent_trades is not None: cryptsy_mongo.persistTrades(recent_trades) if hours is not None: start_time = toCryptsyServerTime(datetime.utcnow() - timedelta(hours=int(hours))) else: now = datetime.utcnow() start_time = toCryptsyServerTime(datetime(now.year, now.month, now.day)) total_buy_best = 0.0 total_sell_best = 0.0 total_fee_best = 0.0 print "Best markets:" mongotradeStats = cryptsy_mongo.getAllTradesFrom(start_time) filteredTradeStats = filter( lambda x: mongotradeStats[x]['Sell'] >= mongotradeStats[x]['Fee'] + mongotradeStats[x]['Buy'], mongotradeStats) sortedTradeStats = sorted(filteredTradeStats, key=lambda x: mongotradeStats[x]['Sell'] - mongotradeStats[x]['Buy'] - mongotradeStats[x][ 'Fee'], reverse=True) for tradeStat in sortedTradeStats: sell = float(mongotradeStats[tradeStat]['Sell']) buy = float(mongotradeStats[tradeStat]['Buy']) fee = float(mongotradeStats[tradeStat]['Fee']) total_buy_best += buy total_sell_best += sell total_fee_best += fee std = next((toEightDigit(market_trend.std) for market_trend in recent_market_trends if int(market_trend.marketId) == int(tradeStat)), None) print "MarketId: {}, Std:{}, Sell: {}, Buy: {}, Earn: {}".format(tradeStat, std, toEightDigit(sell), toEightDigit(buy), toEightDigit(sell - buy - fee)) print "Best markets total: buy: {}, sell: {}, fee:{} - earnings: {}".format(total_buy_best, total_sell_best, total_fee_best, total_sell_best - total_buy_best - total_fee_best) total_buy_worst = 0.0 total_sell_worst = 0.0 total_fee_worst = 0.0 print "Worst markets:" mongotradeStats = cryptsy_mongo.getAllTradesFrom(start_time) filteredTradeStats = filter( lambda x: 0 < mongotradeStats[x]['Sell'] < mongotradeStats[x]['Fee'] + mongotradeStats[x]['Buy'], mongotradeStats) sortedTradeStats = sorted(filteredTradeStats, key=lambda x: mongotradeStats[x]['Sell'] - mongotradeStats[x]['Buy'] - mongotradeStats[x][ 'Fee']) for tradeStat in sortedTradeStats: sell = float(mongotradeStats[tradeStat]['Sell']) buy = float(mongotradeStats[tradeStat]['Buy']) fee = float(mongotradeStats[tradeStat]['Fee']) total_buy_worst += buy total_sell_worst += sell total_fee_worst += fee std = next((toEightDigit(market_trend.std) for market_trend in recent_market_trends if int(market_trend.marketId) == int(tradeStat)), None) print "MarketId: {}, Std:{}, Sell: {}, Buy: {}, Earn: {}".format(tradeStat, std, toEightDigit(sell), toEightDigit(buy), toEightDigit(sell - buy - fee)) print "Worst markets total: buy: {}, sell: {}, fee:{} - earnings: {}".format(total_buy_worst, total_sell_worst, total_fee_worst, total_sell_worst - total_buy_worst - total_fee_worst) print "Total stats: total earning: {}".format( (total_sell_best + total_sell_worst) - (total_buy_best + total_buy_worst + total_fee_best + total_fee_worst))