class BotCandlestick(object): def __init__(self, period=300,open=None,close=None,high=None,low=None,priceAverage=None): self.current = None self.open = open self.close = close self.high = high self.low = low self.startTime = time.time() self.period = period self.output = BotLog() self.priceAverage = priceAverage def tick(self,price): self.current = float(price) if (self.open is None): self.open = self.current if ( (self.high is None) or (self.current > self.high) ): self.high = self.current if ( (self.low is None) or (self.current < self.low) ): self.low = self.current if ( time.time() >= ( self.startTime + self.period) ): self.close = self.current self.priceAverage = ( self.high + self.low + self.close ) / float(3) self.output.log("Open: "+str(self.open)+" Close: "+str(self.close)+" High: "+str(self.high)+" Low: "+str(self.low)+" Current: "+str(self.current)) def isClosed(self): if (self.close is not None): return True else: return False
def __init__(self): #connect; read and write to db self.TradeDatabase = TradeDatabase() self.TradeDatabase.connect() #amount to trade (capital): self.amountInUSD = 300 #prices information self.prices = [] self.currentPrice = "" #graph and indicators self.output = BotLog() self.indicators = BotIndicators() #self.graphdataPoints = [] self.dataDate = "" self.SMA = "" self.CResistance = 0.0 self.EMA9 = [] self.MACD = [] #trade details self.tradePlaced = [] self.typeOfTrade = [] self.cumulatedProfits = 0.0 #wins and loses self.numofwins = 0 self.numofloses = 0
def __init__(self, exchange, pair, period, backtest=True ): # TODO: pass in start and end timestamps for backtesting self.output = BotLog() self.exchange = exchange self.pair = pair self.period = period self.startTime = time.time( ) - SECONDS_IN_DAY * 2 # TODO: use passed in start and end timestamps self.endTime = time.time( ) # Backtest up to now TODO: move this to __init__ param so it can be changed self.data = [] if self.exchange == "kraken" and backtest: logger.info("Fetching historical OHLC data for back testing...") kraken_data = OHLC(pair='XXBTZUSD', since=self.startTime, interval=self.period) historical_kraken_data = kraken_data['result'][self.pair] for datum in historical_kraken_data: if datum[1] and datum[4] and datum[2] and datum[3] and datum[5]: # list entries: (0: <timestamp>, 1: <open>, 2: <high>, 3: <low>, 4: <close>, 5: <vwap>, 6: <volume>, 7: <count>) self.data.append( BotCandlestick(self.period, datum[1], datum[4], datum[2], datum[3], datum[5]))
def __init__(self): self.output = BotLog() self.prices = [] self.currentPrice = "" self.indicators = BotIndicators() self.GRC_bal = False self.BTC_bal = False self.Total_bal = False self.can_buy = False self.can_sell = False self.trade_amount = .00059 # BTC self.to_buy = False self.to_sell = False self.pair = 'BTC_GRC' self.order_num = False self.orders = [] self.high_bid_price = False self.low_ask_price = False self.low_ask_amount = False self.high_bid_amount = False self.price_to_buy = False self.price_to_sell = False self.openTrades = [] self.open_Bids = [] self.open_Asks = [] self.orders = [] self.last_sell_price = False self.last_buy_price = False self.open_sell_flag = False self.open_buy_flag = False self.raw_spread = 0 self.mod_spread = 0 self.place_orders_flag = True
def __init__(self): self.output = BotLog() self.prices = [] self.trades = [] self.currentPrice = "" self.numSimulTrades = 1 self.indicators = BotIndicators()
def __init__(self, backtest=True, forwardtest=True): self.output = BotLog() self.pair = shared.exchange['pair'] self.coinsInOrder = shared.exchange['coinsInOrder'] self.marketInOrder = shared.exchange['marketInOrder'] self.trades = [] self.currentPrice = "" self.currentClose = "" self.lowestAsk = 0.00 self.highestBid = 0.00 self.simultaneousTrades = 4 self.tradeMultiplier = 0.1 self.ticker = {} self.backTest = backtest self.forwardTest = forwardtest self.indicators = BotIndicators() self.candlesticks = [] self.movingAverages = [] self.movingAveragePeriod = shared.strategy['movingAverageLength'] self.trueRanges = [] self.averageTrueRanges = [] # portfolio self.openOrders = [] #api self.api = BotApi()
def __init__(self, backtest=True, live=False): self.output = BotLog() self.pair = shared.exchange['pair'] self.coinsInOrder = shared.exchange['coinsInOrder'] self.marketInOrder = shared.exchange['marketInOrder'] self.trades = [] self.currentPrice = "" self.currentClose = "" self.live = live self.lowestAsk = 0.00 self.highestBid = 0.00 self.simultaneousTrades = 1 self.tradeMultiplier = 1 self.ticker = {} self.backTest = backtest self.indicators = BotIndicators() self.candlesticks = [] self.movingAverages = [] self.movingAveragePeriod = 3 self.trueRanges = [] self.averageTrueRanges = [] self.openOrders = [] # API self.api = BotApi()
def __init__(self): self.vars = botVariables() self.investement = self.vars.initialInvestment self.makeFee = self.vars.makeFee self.takeFee = self.vars.takeFee self.output = BotLog() self.prices = [] self.closes = [] # Needed for Momentum Indicator self.trades = [] self.numOfTrades = 0 self.currentPrice = "" self.currentTime = "" self.currentClose = "" self.numSimulTrades = 1 self.indicators = BotIndicators() self.absMargin = 0 self.relMargin = 0 #these are the values of the indicators qat each endTime self.SMA1 = 0 self.SMA2 = 0 self.EMA1 = 0 self.EMA2 = 0 self.RSI = 0 self.BollUp = 0 self.BollDown = 0
class BotCandlestick(object): def __init__(self, period=None, open=None, close=None, high=None, low=None, typical_price=None): """""" self.output = BotLog() self.current = None self.open = open self.close = close self.high = high self.low = low self.period = period self.startTime = time.time() self.typical_price = typical_price def tick(self, price, **kwargs): self.current = float(price) # If it's a brand new price the open price hasn't been set yet # then the current price is the open price. if self.open is None: self.open = self.current # If it's a brand new price the high price hasn't been set yet, # or if the current price is greater than the current high # then set this current price as the high. if (self.high is None) or (self.current > self.high): self.high = self.current # If it's a brand new price the low price hasn't been set yet, # or if the current price is less than the current low # then set this current price as the low. if (self.low is None) or (self.current < self.low): self.low = self.current # If the current time is at or after the start time plus the period # (i.e. this will be the last price that goes into this candlestick before # it is added to the list of past candlesticks) then set this current price # as the closing price. if time.time() >= (self.startTime + (self.period * 60)): self.close = self.current # Determine the typical price over entire period of the candlestick. self.typical_price = (self.high + self.low + self.close) / float(3) # Show OHLC data on each tick self.output.log(" Open: " + str(self.open) + " Close: " + str(self.close) + " High: " + str(self.high) + " Low: " + str(self.low) + " Current: " + str(self.current)) statsd.histogram('candlestick.price.close', self.close, tags=['candlestick.price.close', 'bot_name:{}.bot_id:{}'.format(BOT_NAME, BOT_ID)]) statsd.histogram('candlestick.price.high', self.high, tags=['candlestick.price.high', 'bot_name:{}.bot_id:{}'.format(BOT_NAME, BOT_ID)]) statsd.histogram('candlestick.price.low', self.low, tags=['candlestick.price.low', 'bot_name:{}.bot_id:{}'.format(BOT_NAME, BOT_ID)]) statsd.histogram('candlestick.price.typical_price', self.typical_price, tags=['candlestick.price.typical_price', 'bot_name:{}.bot_id:{}'.format(BOT_NAME, BOT_ID)]) def isClosed(self): if self.close is not None: return True else: return False
def __init__(self, currentPrice, stopLoss=0): self.output = BotLog() self.status = "OPEN" self.entryPrice = currentPrice self.exitPrice = "" self.output.log("Trade opened") if (stopLoss): self.stopLoss = currentPrice - stopLoss
def __init__(self): self.output = BotLog() self.prices = [] self.closes = [] # Needed for Momentum Indicator self.trades = [] self.currentPrice = "" self.currentClose = "" self.numSimulTrades = 1 self.indicators = BotIndicators()
def __init__(self, pair, period): self.output = BotLog() self.prices = [] self.trades = [] self.current_price = NotImplemented self.period = period self.num_simultaneous_trades = 3 # Max number of simultaneous trades
def __init__(self, currentPrice, startTime, stopLoss=0): self.output = BotLog() self.status = "OPEN" self.entryPrice = currentPrice self.exitPrice = 0. self.startTime = startTime self.exitTime = 0. self.stopLoss = stopLoss self.output.log("Trade opened: " + str(self.entryPrice) + " StopLoss: " + str(self.stopLoss))
def __init__(self, period=300): self.current = None self.open = None self.close = None self.high = None self.low = None self.startTime = time.time() self.period = period self.output = BotLog() self.priceAverage = None
def __init__(self, period=300,open=None,close=None,high=None,low=None,priceAverage=None): self.current = None self.open = open self.close = close self.high = high self.low = low self.startTime = time.time() self.period = period self.output = BotLog() self.priceAverage = priceAverage
def __init__(self): self.exchange = shared.exchange['name'] self.key = shared.api['key'] self.secret = shared.api['secret'] self.output = BotLog() if shared.exchange['name'] == 'poloniex': self.api = poloniex.Poloniex(self.key, self.secret) elif shared.exchange['name'] == 'kraken': self.api = krakenex.API(self.key, self.secret)
def __init__(self, currentPrice, stopLossEdge): self.output = BotLog() self.status = "OPEN" self.entryPrice = currentPrice self.exitPrice = 0 self.pnl = 0 self.side = "Buy" self.entryTime = datetime.datetime.now() self.exitTime = "" self.output.log("Trade opened") self.stopLoss = currentPrice - stopLossEdge
def __init__(self, period=None, open=None, close=None, high=None, low=None, typical_price=None): """""" self.output = BotLog() self.current = None self.open = open self.close = close self.high = high self.low = low self.period = period self.startTime = time.time() self.typical_price = typical_price
class BotStrategy(object): def __init__(self): self.output = BotLog() self.prices = [] self.closes = [] # Needed for Momentum Indicator self.trades = [] self.currentPrice = "" self.currentClose = "" self.numSimulTrades = 1 self.indicators = BotIndicators() def tick(self, candlestick): print candlestick.priceAverage self.currentPrice = float(candlestick.priceAverage) self.prices.append(self.currentPrice) #self.currentClose = float(candlestick['close']) #self.closes.append(self.currentClose) self.output.log("Price: " + str(candlestick.priceAverage) + "\tMoving Average: " + str(self.indicators.movingAverage(self.prices, 15))) self.evaluatePositions() self.updateOpenTrades() self.showPositions() def evaluatePositions(self): openTrades = [] for trade in self.trades: if (trade.status == "OPEN"): openTrades.append(trade) if (len(openTrades) < self.numSimulTrades): if (self.currentPrice < self.indicators.movingAverage( self.prices, 15)): self.trades.append(BotTrade(self.currentPrice, stopLoss=.0001)) for trade in openTrades: if (self.currentPrice > self.indicators.movingAverage( self.prices, 15)): trade.close(self.currentPrice) def updateOpenTrades(self): for trade in self.trades: if (trade.status == "OPEN"): trade.tick(self.currentPrice) def showPositions(self): for trade in self.trades: trade.showTrade()
def __init__(self,date=None,open=None,high=None,low=None,close=None,volume=None): self.close = close self.currentPrice = close self.date = date self.high = high self.low = low self.open = open self.output = BotLog() self.priceAverage = False self.startTime = time.time() self.volume = volume if self.close: self.currentPrice = self.close
class BotPosition(object): def __init__(self, current_price, start_time, short, takeProfit=0, stopLoss=0): self.output = BotLog() self.status = "OPEN" self.entry_price = current_price self.exit_price = 0. self.start_time = start_time self.exitTime = 0. self.stopLoss = stopLoss self.takeProfit = takeProfit self.short = short if short: self.output.log("OPEN SELL : " + str(self.entry_price)) else: self.output.log("OPEN BUY : " + str(self.entry_price)) def close(self, current_price, endTime): self.status = "CLOSED" self.exit_price = current_price self.exitTime = endTime if self.short: self.output.log("CLOSE BUY : " + str(current_price) + "\n") else: self.output.log("CLOSE SELL : " + str(current_price) + "\n") def tick(self, current_price, candlestick): if self.stopLoss != 0: if self.short and current_price > self.stopLoss: self.close(current_price, candlestick.start_time) return elif not (self.short) and current_price < self.stopLoss: self.close(current_price, candlestick.start_time) return if self.takeProfit != 0: if self.short and current_price < self.takeProfit: self.close(current_price, candlestick.start_time) return elif not (self.short) and current_price > self.takeProfit: self.close(current_price, candlestick.start_time) return def showTrade(self): tradeStatus = "Entry Price: " + str( self.entry_price) + " Status: " + str( self.status) + " Exit Price: " + str(self.exit_price) if (self.status == "CLOSED"): tradeStatus = tradeStatus + " Profit: " if (self.exit_price > self.entry_price): tradeStatus = tradeStatus + "\033[92m" else: tradeStatus = tradeStatus + "\033[91m" tradeStatus = tradeStatus + str(self.exit_price - self.entry_price) + "\033[0m"
def __init__(self, short_mode=False, backtest=True): self.capital = 1000 self.output = BotLog() self.prices = [] self.trades = [] self.low = [] self.high = [] self.currentPrice = "" self.numSimulTrades = 100000000 # inutile si seulement signaux ! self.indicators = BotIndicators() # self.stopLoss = 2 * self.indicators.average_true_range(self.high,self.low,self.prices) self.stopLoss = 0 # inutile pour signaux self.short_mode = short_mode self.backtest = backtest
def __init__(self, order_num, currentPrice, type_trade, loss_break): self.output = BotLog() self.status = "OPEN" self.entryPrice = currentPrice self.exitPrice = "" self.output.log("Trade opened") self.type_trade = type_trade self.exit_time = False self.Order_num = order_num self.Loss_break = loss_break self.Balance = False self.GRC_bal = False self.BTC_bal = False self.pair = "BTC_GRC" self.out_amount = False
def __init__(self): self.output = BotLog() self.prices = [] self.opens = [] self.closes = [] #for Momentum self.trades = [] self.MACD_History = [] # MACD History self.MACD_Signal_History = [] # MACD Signal History self.currentPrice = None self.numSimulTrades = 1 self.takeProfit = 0.0001 self.stopLoss = 1 self.indicators = BotIndicators() self.trendPeriod = 3 # ETH : 3 # DASH : 3 self.minVolume = 1.2 # ETH : 1.2 # DASH : 1
def __init__(self, period=300, date=0, open=0, close=0, high=0, low=0, priceAverage=0): self.date = date self.current = 0 self.open = open self.close = close self.high = high self.low = low self.startTime = time.time() self.period = period self.output = BotLog() self.priceAverage = priceAverage self.graph_data = []
def __init__(self): self.result = 0 self.output = BotLog() self.prices = [] self.positions = [] self.low = [] self.high = [] self.current_price = "" self.indicators = BotIndicators() self.num_simulpositions = 1
class BotTrade(object): def __init__(self,currentPrice,stopLoss=0): self.output = BotLog() self.status = "OPEN" self.entryPrice = currentPrice self.exitPrice = "" self.output.log("Trade opened") if (stopLoss): self.stopLoss = currentPrice - stopLoss #shows closing price def close(self,currentPrice): self.status = "CLOSED" self.exitPrice = currentPrice self.output.log("Trade closed") #shows previous ticks def tick(self, currentPrice): if (self.stopLoss): if (currentPrice < self.stopLoss): self.close(currentPrice)
def __init__(self, current_price, start_time, short, takeProfit=0, stopLoss=0): self.output = BotLog() self.status = "OPEN" self.entry_price = current_price self.exit_price = 0. self.start_time = start_time self.exitTime = 0. self.stopLoss = stopLoss self.takeProfit = takeProfit self.short = short if short: self.output.log("OPEN SELL : " + str(self.entry_price)) else: self.output.log("OPEN BUY : " + str(self.entry_price))
class BotTrade(object): def __init__(self, currentPrice, stopLossEdge): self.output = BotLog() self.status = "OPEN" self.entryPrice = currentPrice self.exitPrice = 0 self.pnl = 0 self.side = "Buy" self.entryTime = datetime.datetime.now() self.exitTime = "" self.output.log("Trade opened") self.stopLoss = currentPrice - stopLossEdge def close(self, currentPrice): self.exitTime = datetime.datetime.now() self.status = "CLOSED" self.side = "Sell" self.exitPrice = currentPrice self.output.log("Trade closed") self.pnl = self.exitPrice - self.entryPrice tradeStatus = "Entry Price: " + str(round(self.entryPrice, 3)) + " Status: " + \ str(self.status) + " Exit Price: " + str(round(self.exitPrice, 3)) tradeStatus = tradeStatus + " Pnl: " + str(round(self.pnl, 3)) if (self.pnl > 0): tradeStatus = crayons.green(tradeStatus) else: tradeStatus = crayons.red(tradeStatus) self.output.log(tradeStatus) def tick(self, currentPrice): if (self.stopLoss > 0): if (currentPrice <= self.stopLoss): self.output.log(crayons.magenta("Exit By Stop Loss")) self.close(currentPrice) def showStatus(self): if (self.status == "OPEN"): tradeStatus = "Entry Price: " + str(round( self.entryPrice, 3)) + " Status: " + str(self.status) tradeStatus = crayons.yellow(tradeStatus) self.output.log(tradeStatus)
class BotTrade(object): def __init__(self,currentPrice,stopLoss=0): self.output = BotLog() self.status = "OPEN" self.currentPrice = currentPrice self.entryPrice = currentPrice self.exitPrice = 0 self.output.log("Trade opened") if (stopLoss): self.stopLoss = currentPrice - stopLoss def close(self,currentPrice): self.status = "CLOSED" self.exitPrice = currentPrice self.output.log("Trade closed") #trade_end_time.append(datetime.fromtimestamp(int(time.time())).strftime('%Y-%m-%d %H:%M:%S')) # #total.append(sum(ar_pro)) def tick(self, currentPrice): if (self.stopLoss): if (currentPrice < self.stopLoss): self.close(currentPrice) def showTrade(self): tradeStatus = "Entry Price: "+str(self.entryPrice)+" Status: "+str(self.status)+" Exit Price: "+str(self.exitPrice) if (self.status == "CLOSED"): tradeStatus = tradeStatus + " Profit: " if (self.exitPrice > self.entryPrice): tradeStatus = tradeStatus + "\033[92m" else: tradeStatus = tradeStatus + "\033[91m" tradeStatus = tradeStatus+str(self.exitPrice - self.entryPrice)+"\033[0m" ar_pro.append(self.exitPrice - self.entryPrice) self.output.log(tradeStatus) self.output.log("SubTotal: " + str((sum(ar_pro)))) trade_end_time.append(time.time())#(datetime.fromtimestamp(int(time.time())).strftime('%Y-%m-%d %H:%M:%S')) # total.append(sum(ar_pro)) def export(): filename = open("export.csv",'w') #for i in total: # print('{:.20f}'.format(i[0]) + ',' + str(i[1]) ,file=filename) for i in range(len(trade_end_time)): filename.write("{},{}\n".format(trade_end_time[i],total[i]))