def doBollingTrade(pool, prices, ps, period, deviate): global highest sname = 'BOLLING_' + str(period) + '_' + str(deviate) bollings = bolling.calc_bolling(prices, period, deviate) t = Trader(sname) for i in range(period, len(prices)): if ps[i-1] > bollings['lower'][i-1] and ps[i] < bollings['lower'][i] and t.bsflag < 1: notes = 'LAST p: ' + str(ps[i - 1]) + ';boll lower: ' + str(bollings['lower'][i-1]) + 'CURRENT p: ' + str(ps[i]) + ';boll lower: ' + str(bollings['lower'][i]) t.buy(prices[i]['date'], prices[i]['time'], prices[i]['rmb'], notes) if ps[i-1] < bollings['mean'][i-1] and ps[i] >= bollings['mean'][i] and t.bsflag == 1: notes = 'LAST p: ' + str(ps[i - 1]) + ';boll mean: ' + str(bollings['mean'][i-1]) + 'CURRENT p: ' + str(ps[i]) + ';boll mean: ' + str(bollings['mean'][i]) t.buy(prices[i]['date'], prices[i]['time'], prices[i]['rmb'], notes) if ps[i-1] < bollings['upper'][i-1] and ps[i] > bollings['upper'][i] and t.bsflag > -1: notes = 'LAST p: ' + str(ps[i - 1]) + ';boll upper: ' + str(bollings['upper'][i-1]) + 'CURRENT p: ' + str(ps[i]) + ';boll upper: ' + str(bollings['upper'][i]) t.sell(prices[i]['date'], prices[i]['time'], prices[i]['rmb'], notes) if ps[i-1] > bollings['mean'][i-1] and ps[i] <= bollings['mean'][i] and t.bsflag == -1: notes = 'LAST p: ' + str(ps[i - 1]) + ';boll mean: ' + str(bollings['mean'][i-1]) + 'CURRENT p: ' + str(ps[i]) + ';boll mean: ' + str(bollings['mean'][i]) t.sell(prices[i]['date'], prices[i]['time'], prices[i]['rmb'], notes) t.show(prices[i]['date'], prices[i]['time'], prices[i]['rmb']) pool.estimate(t)
def doRSITrade(pool, prices, period, up, down): global highest sname = 'RSI_' + str(period) + '_' + str(up) + '_' + str(down) rsis = rsi.calc_rsi(prices, period) t = Trader(sname) for i in range(period, len(prices)): if rsis['rsi'][i] < down and rsis['rsi'][i-1] >= down: notes = 'RSI: ' + str(rsis['rsi'][i]) + ';pre: ' + str(rsis['rsi'][i-1]) + ';down: ' + str(down) t.buy(prices[i]['date'], prices[i]['time'], prices[i]['rmb'], notes) if rsis['rsi'][i] >= down and rsis['rsi'][i - 1] < down: notes = 'RSI: ' + str(rsis['rsi'][i]) + ';pre: ' + str(rsis['rsi'][i-1]) + ';down: ' + str(down) t.sell(prices[i]['date'], prices[i]['time'], prices[i]['rmb'], notes, True) if rsis['rsi'][i] > up and rsis['rsi'][i-1] <= up: notes = 'RSI: ' + str(rsis['rsi'][i]) + ';pre: ' + str(rsis['rsi'][i-1]) + ';up: ' + str(up) t.sell(prices[i]['date'], prices[i]['time'], prices[i]['rmb'], notes) if rsis['rsi'][i] <= up and rsis['rsi'][i-1] > up: notes = 'RSI: ' + str(rsis['rsi'][i]) + ';pre: ' + str(rsis['rsi'][i-1]) + ';up: ' + str(up) t.buy(prices[i]['date'], prices[i]['time'], prices[i]['rmb'], notes, True) t.show(prices[i]['date'], prices[i]['time'], prices[i]['rmb']) pool.estimate(t)
def doRSITrade(pool, prices, period, up, down): global highest sname = 'RSI_' + str(period) + '_' + str(up) + '_' + str(down) rsis = rsi.calc_rsi(prices, period) t = Trader(sname) for i in range(period, len(prices)): if rsis['rsi'][i] < down and rsis['rsi'][i - 1] >= down: notes = 'RSI: ' + str(rsis['rsi'][i]) + ';pre: ' + str( rsis['rsi'][i - 1]) + ';down: ' + str(down) t.buy(prices[i]['date'], prices[i]['time'], prices[i]['rmb'], notes) if rsis['rsi'][i] >= down and rsis['rsi'][i - 1] < down: notes = 'RSI: ' + str(rsis['rsi'][i]) + ';pre: ' + str( rsis['rsi'][i - 1]) + ';down: ' + str(down) t.sell(prices[i]['date'], prices[i]['time'], prices[i]['rmb'], notes, True) if rsis['rsi'][i] > up and rsis['rsi'][i - 1] <= up: notes = 'RSI: ' + str(rsis['rsi'][i]) + ';pre: ' + str( rsis['rsi'][i - 1]) + ';up: ' + str(up) t.sell(prices[i]['date'], prices[i]['time'], prices[i]['rmb'], notes) if rsis['rsi'][i] <= up and rsis['rsi'][i - 1] > up: notes = 'RSI: ' + str(rsis['rsi'][i]) + ';pre: ' + str( rsis['rsi'][i - 1]) + ';up: ' + str(up) t.buy(prices[i]['date'], prices[i]['time'], prices[i]['rmb'], notes, True) t.show(prices[i]['date'], prices[i]['time'], prices[i]['rmb']) pool.estimate(t)
def doMacdTrade(pool, prices, ps, fast, slow, sign): global highest, openLevel, closeLevel sname = 'MACD_' + str(fast) + '_' + str(slow) + '_' + str(sign) macds = macd.calc_macd(prices, fast, slow, sign) mas = ma.calc_ema(ps, slow) t = Trader(sname) for i in range(slow + sign, len(prices)): if macds['macd'][i] < 0 and macds['macd'][i] > macds['sign'][i] and macds['macd'][i-1] < macds['sign'][i-1] and abs(macds['macd'][i]) > openLevel and mas[i] > mas[i-1]: notes = 'macd under 0, and abs larger than openlevel' t.buy(prices[i]['date'], prices[i]['time'], prices[i]['rmb'], notes) if macds['macd'][i] < 0 and macds['macd'][i] > macds['sign'][i] and macds['macd'][i-1] < macds['sign'][i-1] and abs(macds['macd'][i]) > closeLevel and mas[i] > mas[i-1]: notes = 'macd under 0, and abs larger than closelevel' t.buy(prices[i]['date'], prices[i]['time'], prices[i]['rmb'], notes, True) if macds['macd'][i] > 0 and macds['macd'][i] < macds['sign'][i] and macds['macd'][i-1] > macds['sign'][i-1] and abs(macds['macd'][i]) > openLevel and mas[i] < mas[i-1]: notes = 'macd above 0, and abs larger than openlevel' t.sell(prices[i]['date'], prices[i]['time'], prices[i]['rmb'], notes) if macds['macd'][i] > 0 and macds['macd'][i] < macds['sign'][i] and macds['macd'][i-1] > macds['sign'][i-1] and abs(macds['macd'][i]) > closeLevel and mas[i] < mas[i-1]: notes = 'macd above 0, and abs larger than closeLevel' t.sell(prices[i]['date'], prices[i]['time'], prices[i]['rmb'], notes, True) t.show(prices[i]['date'], prices[i]['time'], prices[i]['rmb']) pool.estimate(t) return
def doKDJTrade(pool, prices, kPeriod, dPeriod, slowing): global highest sname = 'KDJ_' + str(kPeriod) + '_' + str(dPeriod) + '_' + str(slowing) kds = kdj.calc_kd(prices, kPeriod, dPeriod, slowing) t = Trader(sname) for i in range(kPeriod + slowing, len(prices)): if kds['k'][i - 1] <= 30 and kds['k'][i - 1] < kds['d'][ i - 1] and kds['k'][i] > kds['d'][i]: notes = 'KDJ: pre' + str(kds['k'][i - 1]) + ';' + str( kds['d'][i - 1]) + ';cur: ' + str(kds['k'][i]) + ';' + str( kds['d'][i]) t.buy(prices[i]['date'], prices[i]['time'], prices[i]['rmb'], notes) if kds['k'][i - 1] >= 70 and kds['k'][i - 1] > kds['d'][ i - 1] and kds['k'][i] < kds['d'][i]: notes = 'KDJ: pre' + str(kds['k'][i - 1]) + ';' + str( kds['d'][i - 1]) + ';cur: ' + str(kds['k'][i]) + ';' + str( kds['d'][i]) t.sell(prices[i]['date'], prices[i]['time'], prices[i]['rmb'], notes) t.show(prices[i]['date'], prices[i]['time'], prices[i]['rmb']) pool.estimate(t)
def doBollingTrade(pool, prices, ps, period, deviate): global highest sname = 'BOLLING_' + str(period) + '_' + str(deviate) bollings = bolling.calc_bolling(prices, period, deviate) t = Trader(sname) for i in range(period, len(prices)): if ps[i - 1] > bollings['lower'][ i - 1] and ps[i] < bollings['lower'][i] and t.bsflag < 1: notes = 'LAST p: ' + str(ps[i - 1]) + ';boll lower: ' + str( bollings['lower'][i - 1]) + 'CURRENT p: ' + str( ps[i]) + ';boll lower: ' + str(bollings['lower'][i]) t.buy(prices[i]['date'], prices[i]['time'], prices[i]['rmb'], notes) if ps[i - 1] < bollings['mean'][ i - 1] and ps[i] >= bollings['mean'][i] and t.bsflag == 1: notes = 'LAST p: ' + str(ps[i - 1]) + ';boll mean: ' + str( bollings['mean'][i - 1]) + 'CURRENT p: ' + str( ps[i]) + ';boll mean: ' + str(bollings['mean'][i]) t.buy(prices[i]['date'], prices[i]['time'], prices[i]['rmb'], notes) if ps[i - 1] < bollings['upper'][ i - 1] and ps[i] > bollings['upper'][i] and t.bsflag > -1: notes = 'LAST p: ' + str(ps[i - 1]) + ';boll upper: ' + str( bollings['upper'][i - 1]) + 'CURRENT p: ' + str( ps[i]) + ';boll upper: ' + str(bollings['upper'][i]) t.sell(prices[i]['date'], prices[i]['time'], prices[i]['rmb'], notes) if ps[i - 1] > bollings['mean'][ i - 1] and ps[i] <= bollings['mean'][i] and t.bsflag == -1: notes = 'LAST p: ' + str(ps[i - 1]) + ';boll mean: ' + str( bollings['mean'][i - 1]) + 'CURRENT p: ' + str( ps[i]) + ';boll mean: ' + str(bollings['mean'][i]) t.sell(prices[i]['date'], prices[i]['time'], prices[i]['rmb'], notes) t.show(prices[i]['date'], prices[i]['time'], prices[i]['rmb']) pool.estimate(t)
def doKDJTrade(pool, prices, kPeriod, dPeriod, slowing): global highest sname = 'KDJ_' + str(kPeriod) + '_' + str(dPeriod) + '_' + str(slowing) kds = kdj.calc_kd(prices, kPeriod, dPeriod, slowing) t = Trader(sname) for i in range(kPeriod + slowing, len(prices)): if kds['k'][i-1] <= 30 and kds['k'][i-1] < kds['d'][i-1] and kds['k'][i] > kds['d'][i]: notes = 'KDJ: pre' + str(kds['k'][i-1]) + ';' + str(kds['d'][i-1]) + ';cur: ' + str(kds['k'][i]) + ';' + str(kds['d'][i]) t.buy(prices[i]['date'], prices[i]['time'], prices[i]['rmb'], notes) if kds['k'][i-1] >= 70 and kds['k'][i-1] > kds['d'][i-1] and kds['k'][i] < kds['d'][i]: notes = 'KDJ: pre' + str(kds['k'][i-1]) + ';' + str(kds['d'][i-1]) + ';cur: ' + str(kds['k'][i]) + ';' + str(kds['d'][i]) t.sell(prices[i]['date'], prices[i]['time'], prices[i]['rmb'], notes) t.show(prices[i]['date'], prices[i]['time'], prices[i]['rmb']) pool.estimate(t)
def doMaTrade(pool, prices, ps, front, matype, fast, slow, slow2=0, slow3=0): global highest sname = matype + '_' + str(fast) + '_' + str(slow) if slow2 > 0: sname += '_' + str(slow2) if slow3 > 0: sname += '_' + str(slow3) if matype == 'MA': maMethod = ma.calc_ma elif matype == 'EMA': maMethod = ma.calc_ema elif matype == 'SMA': maMethod = ma.calc_sma maf = maMethod(ps, fast) mas = maMethod(ps, slow) if slow2 > 0: mas2 = maMethod(ps, slow2) if slow3 > 0: mas3 = maMethod(ps, slow3) if front == 0: front = max(slow, slow2, slow3) t = Trader(sname) for i in range(front, len(prices)): if maf[i - 1] < mas[i - 1] and maf[i] >= mas[i]: notes = 'LAST maf: ' + str(maf[i - 1]) + ';mas: ' + str( mas[i - 1]) + 'CURRENT maf: ' + str(maf[i]) + ';mas: ' + str( mas[i]) t.buy(prices[i]['dt'], prices[i]['rmb'], notes=notes) if slow2 > 0 and maf[i - 1] < mas2[i - 1] and maf[i] >= mas2[i]: notes = 'LAST maf: ' + str(maf[i - 1]) + ';mas2: ' + str( mas2[i - 1]) + 'CURRENT maf: ' + str(maf[i]) + ';mas2: ' + str( mas2[i]) t.buy(prices[i]['dt'], prices[i]['rmb'], notes=notes) if slow3 > 0 and maf[i - 1] < mas3[i - 1] and maf[i] >= mas3[i]: notes = 'LAST maf: ' + str(maf[i - 1]) + ';mas3: ' + str( mas3[i - 1]) + 'CURRENT maf: ' + str(maf[i]) + ';mas3: ' + str( mas3[i]) t.buy(prices[i]['dt'], prices[i]['rmb'], notes=notes) if maf[i - 1] > mas[i - 1] and maf[i] <= mas[i]: notes = 'LAST maf: ' + str(maf[i - 1]) + ';mas: ' + str( mas[i - 1]) + 'CURRENT maf: ' + str(maf[i]) + ';mas: ' + str( mas[i]) t.sell(prices[i]['dt'], prices[i]['rmb'], notes=notes) if slow3 > 0 and maf[i - 1] > mas3[i - 1] and maf[i] <= mas3[i]: notes = 'LAST maf: ' + str(maf[i - 1]) + ';mas2: ' + str( mas3[i - 1]) + 'CURRENT maf: ' + str(maf[i]) + ';mas3: ' + str( mas3[i]) t.sell(prices[i]['dt'], prices[i]['rmb'], notes=notes) t.show(prices[i]['dt'], prices[i]['rmb']) pool.estimate(t) return
def doMaTrade(pool, prices, ps, front, matype, fast, slow, slow2 = 0, slow3 = 0): global highest sname = matype + '_' + str(fast) + '_' + str(slow) if slow2 > 0: sname += '_' + str(slow2) if slow3 > 0: sname += '_' + str(slow3) if matype == 'MA': maMethod = ma.calc_ma elif matype == 'EMA': maMethod = ma.calc_ema elif matype == 'SMA': maMethod = ma.calc_sma maf = maMethod(ps, fast) mas = maMethod(ps, slow) if slow2 > 0: mas2 = maMethod(ps, slow2) if slow3 > 0: mas3 = maMethod(ps, slow3) if front == 0: front = max(slow, slow2, slow3) t = Trader(sname) for i in range(front, len(prices)): if maf[i - 1] < mas[i - 1] and maf[i] >= mas[i]: notes = 'LAST maf: ' + str(maf[i - 1]) + ';mas: ' + str(mas[i - 1]) + 'CURRENT maf: ' + str(maf[i]) + ';mas: ' + str(mas[i]) t.buy(prices[i]['dt'], prices[i]['rmb'], notes=notes) if slow2 >0 and maf[i - 1] < mas2[i - 1] and maf[i] >= mas2[i]: notes = 'LAST maf: ' + str(maf[i - 1]) + ';mas2: ' + str(mas2[i - 1]) + 'CURRENT maf: ' + str(maf[i]) + ';mas2: ' + str(mas2[i]) t.buy(prices[i]['dt'], prices[i]['rmb'], notes=notes) if slow3 >0 and maf[i - 1] < mas3[i - 1] and maf[i] >= mas3[i]: notes = 'LAST maf: ' + str(maf[i - 1]) + ';mas3: ' + str(mas3[i - 1]) + 'CURRENT maf: ' + str(maf[i]) + ';mas3: ' + str(mas3[i]) t.buy(prices[i]['dt'], prices[i]['rmb'], notes=notes) if maf[i - 1] > mas[i - 1] and maf[i] <= mas[i]: notes = 'LAST maf: ' + str(maf[i - 1]) + ';mas: ' + str(mas[i - 1]) + 'CURRENT maf: ' + str(maf[i]) + ';mas: ' + str(mas[i]) t.sell(prices[i]['dt'], prices[i]['rmb'], notes=notes) if slow3 >0 and maf[i - 1] > mas3[i - 1] and maf[i] <= mas3[i]: notes = 'LAST maf: ' + str(maf[i - 1]) + ';mas2: ' + str(mas3[i - 1]) + 'CURRENT maf: ' + str(maf[i]) + ';mas3: ' + str(mas3[i]) t.sell(prices[i]['dt'], prices[i]['rmb'], notes=notes) t.show(prices[i]['dt'], prices[i]['rmb']) pool.estimate(t) return
def backtesting( test_data, ticker="KRW-XRP", ): seed_movey = 100000 count = 30 bot = Trader( ticker=ticker, seed_movey=seed_movey, ) results = [] for i in range(test_data.shape[0] - count): data = test_data.iloc[i:count + i] low, high = data["low"][-1], data["high"][-1] bot.current_price = data["close"][-2] status, price = bot.check_market_status_price(data) result = {"timepoint": data.index[-1]} if status == "buy": available, price = check_available_bought_price(price, low, high) if available: if bot.buy(price): result["status"] = "buy" elif status == "sell": available, price = check_available_sold_price(price, low, high) if available: if bot.sell(price): result["status"] = "sell" if not hasattr(result, "status"): result["status"] = "none" result.update(bot.wallet) results.append(result) ROI = (bot.total_money / seed_movey) * 100 print(ROI, "!!!!!") return ROI, results
# Compute behaviour of the last two changes current_change = price_log[0] - price_log[1] previous_change = price_log[1] - price_log[2] # If we were ascending but last two changes are negative, jump out if status is Status.ASCENDING and current_change <= 0 and previous_change <= 0: status = Status.DESCENDING print("Price is falling!") Trader.sell(INVESTMENT, price) # Compute how much we earned (if any lol) money = (price - last_buy_price) / last_buy_price * INVESTMENT money_made += money print( "I made {}€ with the last investment. Total balance is: {}".format( money, money_made)) if money > 0: print("I am a good bot :D") else: print("I am a total disaster... :(") # If we are descending, but last two changes were positive, jump in if status is Status.DESCENDING and current_change >= 0 and previous_change >= 0: status = Status.ASCENDING print("Price is going up!") Trader.buy(INVESTMENT, price) # Store buy price last_buy_price = price
class Interpreter(object): def __init__(self): self.trader = Trader() self.markets = settings.MARKETS self.coins = settings.COINS def _balance(self): try: balance_results = [self.trader.get_balance(i) for i in self.markets] balance = dict(zip(self.markets, balance_results)) except: logger.exception("An error occurred when trying to fetch the balance") # Log totals = {key: 0 for key in self.coins} for market in self.markets: for coin in self.coins: balance_coin = balance.get(market).get(coin).get('free') totals[coin] += balance_coin logger.info("{}: {} {}".format(market, balance_coin, coin)) for coin, volume in totals.items(): logger.info("Total {}: {}".format(coin, volume)) def _order_info(self, market, coin, order_id): order = self.trader.fetch_order(market.upper(), coin.upper(), order_id) logger.info("\n{}".format(pprint.pformat(order))) def _order_cancel(self, market, coin, order_id): cancellation = self.trader.cancel_order(market.upper(), coin.upper(), order_id) logger.info("\n{}".format(pprint.pformat(cancellation))) def _order_sell(self, market, coin, volume, rate): order = self.trader.sell(market.upper(), coin.upper(), volume, rate) logger.info("\n{}".format(pprint.pformat(order))) def _order_buy(self, market, coin, volume, rate): order = self.trader.buy(market.upper(), coin.upper(), volume, rate) logger.info("\n{}".format(pprint.pformat(order))) def _ticker(self, market, coin): ticker = self.trader.fetch_rates(market.upper(), coin.upper()) logger.info("\n{}".format(ticker)) def interpret(self, args): command = args.command if command == "balance": self._balance() elif command == "order": self._order_info(args.market, args.coin, args.order_id) elif command == "cancel": self._order_cancel(args.market, args.coin, args.order_id) elif command == "sell": self._order_sell(args.market, args.coin, args.volume, args.rate) elif command == "buy": self._order_buy(args.market, args.coin, args.volume, args.rate) elif command == "ticker": self._ticker(args.market, args.coin) else: logger.error("Unknown command: {}".format(command))
# from portfolio import Portfolio # p = Portfolio() # p.inject(500) # print(p.get_value()) from trader import Trader t = Trader() print(t.portfolio.liquid) print(t.buy('TICK', 5)) print(t.sell('TICK', 1)) print(t.get_holdings()) print(t.get_pl())
class DiscoTrader(commands.Cog): trader: Trader def __init__(self, bot): # does not spark joy self.bot = bot bot.remove_command('help') self.bot.add_cog(self) self.bot.run(TOKEN) @commands.command(name='ping') async def ping(self, ctx): dprint("pong! ") await ctx.send("pong! ") @commands.command(name='echo') async def echo(self, ctx, *, msg: str): await ctx.send(f"message \"{msg}\" from {ctx.message.author.id}") @commands.command(name='init') async def trader_init(self, ctx, override=False): if ctx.message.author.id != ADMIN and not override: await ctx.send(AUTH_ERR) else: self.trader = Trader() self.trader.load() await ctx.send("Trader initialized and loaded") @commands.command(name='buy') async def buy(self, ctx, ticker: str, quantity: float): if self.trader is None: self.trader_init(ctx, override=True) self.trader.addUser(ctx.message.author.id) #await ctx.send(f"Saw BUY from {ctx.message.author.id} for {quantity} shares of {ticker.upper()}") resp = self.trader.buy(ctx.message.author.id, ticker.upper(), quantity) await ctx.send(resp.message) @commands.command(name='sell') async def sell(self, ctx, ticker: str, quantity: float): if self.trader is None: self.trader_init(ctx, override=True) #await ctx.send(f"Saw SELL from {ctx.message.author.id} for {quantity} shares of {ticker.upper()}") resp = self.trader.sell(ctx.message.author.id, ticker.upper(), quantity) await ctx.send(resp.message) @commands.command(name='bp') async def buyingPower(self, ctx): resp = self.trader.getBuyingPower(ctx.message.author.id) await ctx.send(resp.message) @commands.command(name='backup') async def backup(self, ctx): if ctx.message.author.id != ADMIN: await ctx.send(AUTH_ERR) return resp = self.trader.backup() await ctx.send(resp.message) @commands.command(name='clear') async def clear(self, ctx): if ctx.message.author.id != ADMIN: await ctx.send(AUTH_ERR) return self.trader.backup() self.trader.user_db = [] await ctx.send("Destroyed database!") @commands.command(name='pf') async def pf(self, ctx): resp = self.trader.getPortfolio(ctx.message.author.id) await ctx.send(resp.message) @commands.command(name='stock') async def stock(self, ctx, ticker): resp = self.trader.getStockInfo(ticker) await ctx.send(resp.message) @commands.command(name='?', aliases=['help', 'h']) async def qmark(self, ctx): await ctx.send(f"""DiscoTrader v{VERSION} Testing Commands: ?ping: pongs you ?echo: echos you Administrator Commands: ?init: initialize the trader ?load: initialize the trader and load the database ?backup: make a copy of the database object ?clear: clear the database ?split TICKER R: manually split stocks by a ratio R User Commands: ?buy TICKER X: purchase X shares of TICKER ?sell TICKER X: sell X shares of TICKER ?bp: get your buying power ?pf: get your full portfolio ?stock TICKER: get information about a stock ??: Display this message ?help and ?h: alias for ??""") @commands.command(name='split') async def split(self, ctx, ticker, ratio): if ctx.message.author.id != ADMIN: await ctx.send(AUTH_ERR) return resp = self.trader.fixAfterSplit(ticker.upper(), ratio) await ctx.send(resp.message)
def doTrade(pool, stdPeriod, stdGuage, afmt, af, as1mt, as1, as2mt, as2, bfmt, bf, bs1mt, bs1, bs2mt, bs2): global std, prices sname = str(stdPeriod) + '_' + str(stdGuage) sname += '_' + afmt + '_' + str(af) + '_' + as1mt + '_' + str(as1) if as2 > 0: sname += '_' + as2mt + '_' + str(as2) sname += '_' + bfmt + '_' + str(bf) + '_' + bs1mt + '_' + str(bs1) if bs2 > 0: sname += '_' + bs2mt + '_' + str(bs2) afma, as1ma, as2ma = getMas(afmt, af), getMas(as1mt, as1), getMas(as2mt, as2) bfma, bs1ma, bs2ma = getMas(bfmt, bf), getMas(bs1mt, bs1), getMas(bs2mt, bs2) front = max(as1, as2, bs1, bs2) t = Trader(sname) t.args = [ stdPeriod, stdGuage, afmt, af, as1mt, as1, as2mt, as2, bfmt, bf, bs1mt, bs1, bs2mt, bs2 ] for i in range(front, len(prices)): price = prices[i] if std[stdPeriod][i] > stdGuage: t.switchActiveCounter(1, price['dt'], price['rmb']) else: t.switchActiveCounter(0, price['dt'], price['rmb']) #if std[stdPeriod][i] > 1.3: # t.switchActiveCounter(1, price['dt'], price['rmb']) #elif std[stdPeriod][i] > stdGuage: # t.switchActiveCounter(0, price['dt'], price['rmb']) #else: # t.switchActiveCounter(2, price['dt'], price['rmb']) if as1 > 0 and afma[i - 1] <= as1ma[i - 1] and afma[i] > as1ma[i]: notes = 'af>as1;' + str(afma[i - 1]) + ';' + str( as1ma[i - 1]) + ';' + str(afma[i]) + ';' + str(as1ma[i]) t.buy(price['dt'], price['rmb'], cntNo=0, notes=notes) if as1 > 0 and afma[i - 1] >= as1ma[i - 1] and afma[i] < as1ma[i]: notes = 'af<as1;' + str(afma[i - 1]) + ';' + str( as1ma[i - 1]) + ';' + str(afma[i]) + ';' + str(as1ma[i]) t.sell(price['dt'], price['rmb'], cntNo=0, notes=notes) if as2 > 0 and afma[i - 1] <= as2ma[i - 1] and afma[i] > as2ma[i]: notes = 'af>as2;' + str(afma[i - 1]) + ';' + str( as2ma[i - 1]) + ';' + str(afma[i]) + ';' + str(as2ma[i]) t.buy(price['dt'], price['rmb'], cntNo=0, notes=notes) if as2 > 0 and afma[i - 1] >= as2ma[i - 1] and afma[i] < as2ma[i]: notes = 'af<as2;' + str(afma[i - 1]) + ';' + str( as2ma[i - 1]) + ';' + str(afma[i]) + ';' + str(as2ma[i]) t.sell(price['dt'], price['rmb'], cntNo=0, notes=notes) if bs1 > 0 and bfma[i - 1] <= bs1ma[i - 1] and bfma[i] > bs1ma[i]: notes = 'bf>bs1;' + str(bfma[i - 1]) + ';' + str( bs1ma[i - 1]) + ';' + str(bfma[i]) + ';' + str(bs1ma[i]) t.buy(price['dt'], price['rmb'], cntNo=1, notes=notes) if bs1 > 0 and bfma[i - 1] >= bs1ma[i - 1] and bfma[i] < bs1ma[i]: notes = 'bf<bs1,' + str(bfma[i - 1]) + ';' + str( bs1ma[i - 1]) + ';' + str(bfma[i]) + ';' + str(bs1ma[i]) t.sell(price['dt'], price['rmb'], cntNo=1, notes=notes) if bs2 > 0 and bfma[i - 1] <= bs2ma[i - 1] and bfma[i] > bs2ma[i]: notes = 'bf>bs2;' + str(bfma[i - 1]) + ';' + str( bs2ma[i - 1]) + ';' + str(bfma[i]) + ';' + str(bs2ma[i]) t.buy(price['dt'], price['rmb'], cntNo=1, notes=notes) if bs2 > 0 and bfma[i - 1] >= bs2ma[i - 1] and bfma[i] < bs2ma[i]: notes = 'bf<bs2;' + str(bfma[i - 1]) + ';' + str( bs2ma[i - 1]) + ';' + str(bfma[i]) + ';' + str(bs2ma[i]) t.sell(price['dt'], price['rmb'], cntNo=1, notes=notes) t.show(price['dt'], price['rmb']) pool.estimate(t) return t
def doTrade(pool, stdPeriod, stdGuage, afmt, af, as1mt, as1, as2mt, as2, bfmt, bf, bs1mt, bs1, bs2mt, bs2): global std, prices sname = str(stdPeriod) + '_' + str(stdGuage) sname += '_' + afmt + '_' + str(af) + '_' + as1mt + '_' + str(as1) if as2 > 0: sname += '_' + as2mt + '_' + str(as2) sname += '_' + bfmt + '_' + str(bf) + '_' + bs1mt + '_' +str(bs1) if bs2 > 0: sname += '_' + bs2mt + '_' + str(bs2) afma, as1ma, as2ma = getMas(afmt, af), getMas(as1mt, as1), getMas(as2mt, as2) bfma, bs1ma, bs2ma = getMas(bfmt, bf), getMas(bs1mt, bs1), getMas(bs2mt, bs2) front = max(as1, as2, bs1, bs2) t = Trader(sname) t.args = [stdPeriod, stdGuage, afmt, af, as1mt, as1, as2mt, as2, bfmt, bf, bs1mt, bs1, bs2mt, bs2] for i in range(front, len(prices)): price = prices[i] if std[stdPeriod][i] > stdGuage: t.switchActiveCounter(1, price['dt'], price['rmb']) else: t.switchActiveCounter(0, price['dt'], price['rmb']) #if std[stdPeriod][i] > 1.3: # t.switchActiveCounter(1, price['dt'], price['rmb']) #elif std[stdPeriod][i] > stdGuage: # t.switchActiveCounter(0, price['dt'], price['rmb']) #else: # t.switchActiveCounter(2, price['dt'], price['rmb']) if as1 > 0 and afma[i - 1] <= as1ma[i - 1] and afma[i] > as1ma[i]: notes = 'af>as1;' + str(afma[i - 1]) + ';' + str(as1ma[i - 1]) + ';' + str(afma[i]) + ';' + str(as1ma[i]) t.buy(price['dt'], price['rmb'], cntNo=0, notes=notes) if as1 > 0 and afma[i - 1] >= as1ma[i - 1] and afma[i] < as1ma[i]: notes = 'af<as1;' + str(afma[i - 1]) + ';' + str(as1ma[i - 1]) + ';' + str(afma[i]) + ';' + str(as1ma[i]) t.sell(price['dt'], price['rmb'], cntNo=0, notes=notes) if as2 > 0 and afma[i - 1] <= as2ma[i - 1] and afma[i] > as2ma[i]: notes = 'af>as2;' + str(afma[i - 1]) + ';' + str(as2ma[i - 1]) + ';' + str(afma[i]) + ';' + str(as2ma[i]) t.buy(price['dt'], price['rmb'], cntNo=0, notes=notes) if as2 > 0 and afma[i - 1] >= as2ma[i - 1] and afma[i] < as2ma[i]: notes = 'af<as2;' + str(afma[i - 1]) + ';' + str(as2ma[i - 1]) + ';' + str(afma[i]) + ';' + str(as2ma[i]) t.sell(price['dt'], price['rmb'], cntNo=0, notes=notes) if bs1 > 0 and bfma[i - 1] <= bs1ma[i - 1] and bfma[i] > bs1ma[i]: notes = 'bf>bs1;' + str(bfma[i - 1]) + ';' + str(bs1ma[i - 1]) + ';' + str(bfma[i]) + ';' + str(bs1ma[i]) t.buy(price['dt'], price['rmb'], cntNo=1, notes=notes) if bs1 > 0 and bfma[i - 1] >= bs1ma[i - 1] and bfma[i] < bs1ma[i]: notes = 'bf<bs1,' + str(bfma[i - 1]) + ';' + str(bs1ma[i - 1]) + ';' + str(bfma[i]) + ';' + str(bs1ma[i]) t.sell(price['dt'], price['rmb'], cntNo=1, notes=notes) if bs2 > 0 and bfma[i - 1] <= bs2ma[i - 1] and bfma[i] > bs2ma[i]: notes = 'bf>bs2;' + str(bfma[i - 1]) + ';' + str(bs2ma[i - 1]) + ';' + str(bfma[i]) + ';' + str(bs2ma[i]) t.buy(price['dt'], price['rmb'], cntNo=1, notes=notes) if bs2 > 0 and bfma[i - 1] >= bs2ma[i - 1] and bfma[i] < bs2ma[i]: notes = 'bf<bs2;' + str(bfma[i - 1]) + ';' + str(bs2ma[i - 1]) + ';' + str(bfma[i]) + ';' + str(bs2ma[i]) t.sell(price['dt'], price['rmb'], cntNo=1, notes=notes) t.show(price['dt'], price['rmb']) pool.estimate(t) return t