示例#1
0
    def update_tables_imperative(tns):

        ##CandleFetcher.update_tables(tns) ##perform normal update of any legit candles(replacing fake ones)

        for i, tn in enumerate(tns):
            sec_now = time.time()
            last_candle_date = CandleTable.get_last_date(tn)
            target_curr = CandleTable.get_target_currency(tn)
            period = int(CandleTable.get_period(tn))
            curr_pair = "USDT_" + target_curr

            last_candle_date += period
            while (last_candle_date < sec_now):
                ##(top_bid, bottom_ask) = OrderMaker.get_spread(curr_pair)
                ##if curr_avail[target_curr]: ##means it is true, means it is available to be sold
                ##close = bottom_ask
                ##else:
                ##close = top_bid
                close = OrderMaker.get_last_trade_rate(curr_pair,
                                                       last_candle_date)
                c = Candle(tn, last_candle_date, 0, 0, 0, close, 0, 0, 0)
                c.save()
                last_candle_date += period
            dbm = DBManager.get_instance()
            dbm.save_and_close()
def test_add_candle_puts_two_candles_in_list():
    cat = Catalogue("2021 catalogue")
    candle1 = Candle("Beach", 54, "lavender", "square", "4 hours", "10cm by 10cm", "£100")
    candle2 = Candle("Beach", 2, "lavender", "square", "4 hours", "10cm by 10cm", "£100")
    cat.add_candle(candle1)
    cat.add_candle(candle2) 
    assert cat.candles_list == [candle1, candle2]
示例#3
0
    def cut_table(orig_table_name, date_start, date_end=9999999999):
        ##print "Cutting table: ", orig_table_name, " candle data between: ", timestamp_to_date(date_start), " ---- ", timestamp_to_date(date_end)

        ##create new table
        curr_ref = CandleTable.get_ref_currency(orig_table_name)
        curr_target = CandleTable.get_target_currency(orig_table_name)
        period = CandleTable.get_period(orig_table_name)
        new_table = CandleTable(curr_ref, curr_target, date_start, date_end,
                                period)
        new_table_name = new_table.table_name

        if DBManager.exists_table(new_table_name):
            DBManager.drop_table(new_table_name)

        new_table.save()

        ##populate new table with candles from orig_table that lie between the 2 dates
        candle_array = CandleTable.get_candle_array_by_date(
            orig_table_name, date_start, date_end)
        for c in candle_array:
            new_c = Candle(new_table_name, c.date, c.high, c.low, c.open,
                           c.close, c.volume, c.quoteVolume, c.weightedAverage)
            new_c.save()
        dbm = DBManager.get_instance()
        return new_table_name
def is_morning_star(first_candle: Candle, second_candle: Candle,
                    third_candle: Candle):
    """
    Determines if the candles match a Morning Star bullish candlestick pattern.
    """

    # First check if the first is red and the third is green
    if first_candle.is_bearish and third_candle.is_bullish:

        # Make sure the second candle's body does not overlap the first or third
        if second_candle.close < first_candle.close and second_candle.open < first_candle.close \
            and second_candle.close < third_candle.open and second_candle.open < third_candle.open:

            # Then check if the second candle's body is much smaller than the first
            if (second_candle.get_body() / first_candle.get_body()) < 0.3:

                # Check if the third's body is also long (at least 60% of its size)
                if (third_candle.get_body() / first_candle.get_body()) >= 0.6:

                    # Finally check that the third candle overlaps the first
                    third_center = third_candle.get_body_center()
                    if third_center > (first_candle.close *
                                       1.05) and third_center < (
                                           first_candle.open * 0.95):
                        return True

    return False
def test_delete_candle_removes_the_candle_from_list():
    cat = Catalogue("2021 catalogue")
    candle1 = Candle("Beach", 54, "lavender", "square", "4 hours", "10cm by 10cm", "£100")
    candle2 = Candle("Beach", 2, "lavender", "square", "4 hours", "10cm by 10cm", "£100")
    cat.add_candle(candle1)
    cat.add_candle(candle2) 
    cat.delete_candle(54)
    assert cat.candles_list == [candle2]
def test_delete_both_candles_gives_empty_list():
    cat = Catalogue("2021 catalogue")
    candle1 = Candle("Beach", 54, "lavender", "square", "4 hours", "10cm by 10cm", "£100")
    candle2 = Candle("Beach", 2, "lavender", "square", "4 hours", "10cm by 10cm", "£100")
    cat.add_candle(candle1)
    cat.add_candle(candle2) 
    cat.delete_candle(54)
    cat.delete_candle(2)
    assert cat.candles_list == []
示例#7
0
 def getCandleHistoricalData(self):
     self.CandleData = self.conn.api_query(
         "returnChartData", {
             "currencyPair": self.pair,
             "start": self.startTime,
             "end": self.endTime,
             "period": self.period
         })
     self.candles = Candle(self.CandleData)
示例#8
0
	def insert(self):
		for c in self.data['candleStick']:
			try:
				ct = Candle(self.table_name, c['date'], c['high'], c['low'], c['open'], c['close'], c['volume'], c['quoteVolume'], c['weightedAverage'])
				try:
					ct.save()
				except:
						"Duplicate candle, cannot insert"
			except:
				print("Candle cannot be parsed")
		dbm = DBManager.get_instance()
		dbm.save_and_close()
def test_delete_candle_gives_error_if_candle_not_in_list():
    cat = Catalogue("2021 catalogue")
    candle1 = Candle("Beach", 54, "lavender", "square", "4 hours", "10cm by 10cm", "£100")
    candle2 = Candle("Beach", 2, "lavender", "square", "4 hours", "10cm by 10cm", "£100")
    candle3 = Candle("Beach", 3, "lavender", "square", "4 hours", "10cm by 10cm", "£100")
    cat.add_candle(candle1)
    cat.add_candle(candle2) 
    with pytest.raises(ValueError):
        cat.delete_candle(candle3)
    assert cat.candles_list == [candle1, candle2]
    with pytest.raises(ValueError) as error:
        cat.delete_candle(candle3)
    assert str(error.value) == "Item not found"
def build_candles(ticker: str, trader: shift.Trader, state: Dict[str, Any],
                  end_time):
    queue_size = 30

    while trader.get_last_trade_time() < end_time:

        s = 0
        price = trader.get_last_price(ticker)
        candle = Candle(price, price, price, price)
        while s < candle_size:
            price = trader.get_last_price(ticker)
            if price < candle.low:
                candle.setLow(price)
            if price > candle.high:
                candle.setHigh(price)

            sleep(1)
            s += 1
        price = trader.get_last_price(ticker)
        candle.setClose(price)

        try:
            state[candles_key][ticker] += [candle]
        except KeyError:
            state[candles_key][ticker] = deque(maxlen=queue_size)
            sleep(0.5)
            state[candles_key][ticker] += [candle]
示例#11
0
 def make_graph_with_timeframe(self, interval):
     relative = interval / self.timeframe
     assert int(relative) == relative
     relative = int(relative)
     new_graph = Graph()
     new_graph.timeframe = interval
     new_graph.currency = self.currency
     for index in range(0, len(self.ask_candles) + 1 - relative, relative):
         new_graph.ask_candles.append(
             Candle.merge_candles(self.ask_candles[index:index + relative]))
         new_graph.bid_candles.append(
             Candle.merge_candles(self.bid_candles[index:index + relative]))
         new_graph.dates.append(self.dates[index])
     new_graph.set_shortcuts()
     return new_graph
示例#12
0
 def candles_from_file(filename):
     with open(filename) as plik:
         linie = plik.read().split("\n")[1:-1]
         values = [l.split(";") for l in linie]
         dates = [v[0] for v in values]
         candles = [Candle([to_float(s) for s in v[1:5]]) for v in values]
     return dates, candles
 def update_candles(self):
     self.candles.append(
         Candle(self.bar, self.ichimoku.Tenkan.Current.Value,
                self.ichimoku.Kijun.Current.Value,
                self.ichimoku.SenkouA.Current.Value,
                self.ichimoku.SenkouB.Current.Value, self.pattern_long,
                self.pattern_short))
def test_update_price_on_id_not_found_gives_error():
    cat = Catalogue("2021 catalogue")
    candle1 = Candle("Beach", 54, "lavender", "square", "4 hours", "10cm by 10cm", "£100")
    cat.add_candle(candle1)
    with pytest.raises(ValueError) as e:
        cat.update_candle_price(2, "£80")
    assert str(e.value) == "Item not found"
示例#15
0
def handle_message(msg):
    if msg['e'] == 'error':
        print(str(msg))
    elif msg['e'] == 'kline':
        kline = msg['k']
        handle_new_candle(
            msg['s'],
            Candle(kline['t'], float(kline['o']), float(kline['c']),
                   float(kline['h']), float(kline['l'])))
示例#16
0
def test_new_candle_correct():
    candle1 = Candle("Beach", 54, "lavender", "square", "4 hours",
                     "10cm by 10cm", "£100")
    assert candle1.name == "Beach"
    assert candle1.id == 54
    assert candle1.fragrance == "lavender"
    assert candle1.candle_type == "square"
    assert candle1.burn_time == "4 hours"
    assert candle1.dimensions == "10cm by 10cm"
    assert candle1.price == "£100"
示例#17
0
文件: chart.py 项目: martijnbr/forex
    def tick(self, tick):
        # find candle to tick to
        for candle in reversed(self._candles):
            if candle.time <= tick['time'] and candle.close > tick['time']:
                candle.tick(tick)
                return
            if candle.time > tick['time']:
                break

        candle = Candle(self, tick=tick)
        self.candle_builder(candle)
示例#18
0
 def normalize_candle(self, candle, minVal, maxVal):
     ratio = (self.IMG_HEIGHT - self.CHART_MARGIN_BOTTOM -
              self.CHART_MARGIN_TOP - self.CHART_PADDING * 2.0) / (maxVal -
                                                                   minVal)
     # (320 - 30 -10) / 55-24 = 9
     bottom = self.IMG_HEIGHT - self.CHART_MARGIN_BOTTOM - self.CHART_PADDING
     return Candle(bottom - (candle.open - minVal) * ratio,
                   bottom - (candle.high - minVal) * ratio,
                   bottom - (candle.low - minVal) * ratio,
                   bottom - (candle.close - minVal) * ratio,
                   candle.open_time, candle.close_time, candle.volume)
def detect_bullish_patterns(df: DataFrame):
    """
    Finds all bullish candlestick patterns in a DataFrame with daily candlesticks.

    :param df: DataFrame with 3 days of candlestick data
    """
    assert type(df) == DataFrame
    assert len(df) >= 3

    symbol = df['symbol'][0]

    recent_candle1 = Candle(df.iloc[len(df) - 3])
    recent_candle2 = Candle(df.iloc[len(df) - 2])
    recent_candle3 = Candle(df.iloc[len(df) - 1])

    pattern = ''

    # Search for each type of pattern
    if is_bullish_engulfing(recent_candle1, recent_candle2, recent_candle3):
        pattern = 'bullish_engulfing'
    elif is_three_white_knights(recent_candle1, recent_candle2,
                                recent_candle3):
        pattern = 'three_white_knights'
    elif is_morning_star(recent_candle1, recent_candle2, recent_candle3):
        pattern = 'morning_star'

    # Confirm a bullish candlestick pattern with a bullish OBV
    # To prove it's not a false-positive
    if pattern != '':
        obv_df = obv(df).df
        obv_df['obv_ema'] = obv_df['obv'].ewm(span=20).mean()

        # Check to see if the OBV crossed above the EMA signal
        recent_obv_df = pd.DataFrame(obv_df.tail(2))
        if recent_obv_df['obv'][0] <= recent_obv_df['obv_ema'][0] \
            and recent_obv_df['obv'][1] > recent_obv_df['obv_ema'][1]:
            # print(f'{symbol} crossed the OBV EMA today')
            return pattern

    return ''
示例#20
0
文件: chart.py 项目: martijnbr/forex
 def load_chart(self, start):
     con = self.instrument.account.con
     pair = self.instrument.pair
     candles = con.get_instrument_history(pair,
                                          granularity=self.gran,
                                          count=5000,
                                          start=start)
     for candle in candles['candles']:
         candle['time'] = time_to_datetime(candle['time'])
         c = Candle(self, candle=candle)
         self.candle_builder(c)
     if (len(candles['candles']) == 5000):
         self.load_chart(candle['time'].isoformat('T') + 'Z')
示例#21
0
	def get_candle_array_by_date(table_name, date_low = 0, date_high = 9999999999):

		##returns a cursor pointing to all candles linked to the table_name
		cursor = CandleTable.get_candle_cursor_by_date(table_name, date_low, date_high)
	
		candles = []

		##loop through cursor and add all candles to array
		row = cursor.fetchone()
		while row is not None:
			t = Candle.from_tuple(table_name, row) 
			candles.append(t)
			row = cursor.fetchone()
		return candles
示例#22
0
def main():

    app.connect('127.0.0.1', 7496, 123)

    api_thread = threading.Thread(target=run_loop, daemon=True)
    api_thread.start()

    time.sleep(1)

    usdcad_contract = Contract()
    usdcad_contract.symbol = 'USD'
    usdcad_contract.secType = 'CASH'
    usdcad_contract.exchange = 'IDEALPRO'
    usdcad_contract.currency = 'CAD'

    requestID = 100
    app.reqHistoricalData(requestID, usdcad_contract, '', '10 D', '1 hour',
                          'BID', 0, 2, True, [])

    try:
        while True:
            time.sleep(3)
            if app.isDirty():
                candles = []
                ts = datetime.fromtimestamp(int(app.data[1][0]))
                print('checking for big shadow for candle @',
                      ts.strftime('%b %d %Y %H:%M:%S'))
                for i in range(1, config.BS_NUMCANDLES + 1):
                    candles.append(
                        Candle(app.data[i][1], app.data[i][2], app.data[i][3],
                               app.data[i][4]))
                cg = CandleGroup(candles)
                if cg.bigShadow(maxBodyRatio=config.BS_BODYRATIO,
                                wickPercent=config.BS_WICKPERCENTAGE):
                    print('big shadow found for candle @',
                          ts.strftime('%b %d %Y %H:%M:%S'))
                    sendtext(('found big shadow on USDCAD 1H @',
                              ts.strftime('%b %d %Y %H:%M:%S')))

    except KeyboardInterrupt:
        pass

    app.disconnect()
示例#23
0
def candle_sticks(ticker):
    """
    return daily time series candle sticks and volumes for a certain ticker
    """
    candles_json = load_json(ticker)
    time_series = json.loads(candles_json)["Time Series (Daily)"]

    candles = []
    for key in time_series.keys():

        candle_date = key
        candle_value = time_series[candle_date]

        volume = candle_value["5. volume"]
        open_ = candle_value["1. open"]
        close = candle_value["4. close"]
        high = candle_value["2. high"]
        low = candle_value["3. low"]
        candle_ = Candle(candle_date, volume, open_, close, high, low)
        candles.append(candle_)

    return sorted(candles)
示例#24
0
    def test_pearson(self):
        """
        case: empty chart
        """
        c = Chart( Instrument(4) )
        self.assertRaises(Exception, c.pearson, 0, c.get_size() )

        """
        case: one candle
        """
        # TODO

        """
        case: straight line, positive slope
        """
        c = Chart(in_instrument=Instrument(4), count=0)
        fake_candles = []
        fake_timestamp = datetime.utcnow()
        fake_price = 100.1234
        for i in range(0,10):
            fake_candles.append( Candle(
                timestamp=fake_timestamp + timedelta(seconds=i),   
                high_ask=fake_price + i,
                low_bid=(fake_price + i / 2)
            ) )
        c._candles = fake_candles
        pearson = c.pearson( 0, c.get_size() - 1, 'high_low_avg' )
        # Allow some play for float arithmetic
        self.assertTrue( pearson > 0.99999 and pearson <= 1 )

        """
        case: straight line, negative slope
        """
        c = Chart( in_instrument=Instrument(4), count=0 )
        fake_candles = []
        fake_timestamp = datetime.utcnow()
        fake_price = 100.1234
        for i in range(0,10):
            fake_candles.append( Candle(
                timestamp=fake_timestamp - timedelta(seconds=i),   
                high_ask=fake_price + i,
                low_bid=(fake_price + i / 2)
            ) )
        c._candles = fake_candles
        pearson = c.pearson( 0, c.get_size() - 1, 'high_low_avg' )
        # Allow some play for float arithmetic
        self.assertTrue( pearson < -0.99999 and pearson >= -1 )

        """
        case: V shape
        """
        c = Chart( in_instrument=Instrument(4), count=0 )
        fake_candles = []
        fake_timestamp = datetime.utcnow()
        fake_price = 100.1234
        seconds_elapsed = 0
        for i in range(9,-1,-1):
            fake_candles.append( Candle(
                timestamp=fake_timestamp + timedelta(seconds=seconds_elapsed),
                high_ask=fake_price + i,
                low_bid=(fake_price + i / 2)
            ) )
            seconds_elapsed += 1
        for i in range(0,10):
            fake_candles.append( Candle(
                timestamp=fake_timestamp + timedelta(seconds=seconds_elapsed),
                high_ask=fake_price + i,
                low_bid=(fake_price + i / 2)
            ) )
            seconds_elapsed += 1
        c._candles = fake_candles
        pearson = c.pearson( 0, c.get_size() - 1, 'high_low_avg' )
        # Allow some play for float arithmetic
        self.assertTrue( pearson > -0.000001 and pearson < 0.000001 )

        """
        case: random
        """
        c = Chart( in_instrument=Instrument(4), count=0 )
        fake_candles = []
        fake_timestamp = datetime.utcnow()
        fake_price = 100
        seconds_elapsed = 0
        for i in range(0,10000):
            offset = random.randrange(1, fake_price)
            fake_candles.append( Candle(
                timestamp=fake_timestamp + timedelta(seconds=seconds_elapsed),
                high_ask=fake_price + offset,
                low_bid=(fake_price + offset / 2)
            ) )
            seconds_elapsed += 1
        c._candles = fake_candles
        pearson = c.pearson( 0, c.get_size() - 1, 'high_low_avg' )
        # Allow some play for float rounding
        print('pearson of random chart: {}'.format(pearson) )
        self.assertTrue( pearson > -0.02 and pearson < 0.02 )
def test_update_price_changes_to_new_price():
    cat = Catalogue("2021 catalogue")
    candle1 = Candle("Beach", 54, "lavender", "square", "4 hours", "10cm by 10cm", "£100")
    cat.add_candle(candle1)
    cat.update_candle_price(54, "£80")
    assert candle1.price == "£80"
示例#26
0
文件: chart.py 项目: karthikm1/algo
    def __init__(
        self,
        in_instrument,              # <Instrument>    
        granularity='S5',           # string - See Oanda's documentation
        count=None,                 # int - number of candles
        start=None,                 # datetime - UTC
        end=None,                   # datetime - UTC
        price='MBA',                # string
        include_first=None,         # bool
        daily_alignment=None,       # int
        alignment_timezone=None,    # string - timezone
        weekly_alignment=None
    ):
        self._candles = []
        # verify instance of <Instrument> by accessing a member.
        if in_instrument.get_id() == 0:
            pass            

        if not count in [0]: # do send None
            # get candles from broker
            instrument_history = Broker.get_instrument_history(
                instrument=in_instrument,
                granularity=granularity,
                count=count,
                from_time=start,
                to=end,
                price=price,
                include_first=include_first,
                daily_alignment=daily_alignment,
                alignment_timezone=alignment_timezone,
                weekly_alignment=weekly_alignment
            )
            if instrument_history == None:
                Log.write('chart.py __init__(): Failed to get instrument history.')
                raise Exception
            else:
                candles_raw = instrument_history['candles']
                for c_r in candles_raw:
                    new_candle = Candle(
                        timestamp=util_date.string_to_date(c_r['time']),
                        volume=float(c_r['volume']),
                        complete=bool(c_r['complete']),
                        open_bid=float(c_r['bid']['o']),
                        high_bid=float(c_r['bid']['h']),
                        low_bid=float(c_r['bid']['l']),
                        close_bid=float(c_r['bid']['c']),
                        open_ask=float(c_r['ask']['o']),
                        high_ask=float(c_r['ask']['h']),
                        low_ask=float(c_r['ask']['l']),
                        close_ask=float(c_r['ask']['c'])
                    )
                    self._candles.append(new_candle)

        self._instrument = in_instrument
        self._granularity = granularity
        self._start_index = 0 # start
        self._price = price
        self.include_first = include_first
        self.daily_alignment = daily_alignment
        self._alignment_timezone = alignment_timezone
        self.weekly_alignment = weekly_alignment
示例#27
0
def main():

    app.connect('127.0.0.1', 7496, 123)

    api_thread = threading.Thread(target=run_loop, daemon=True)
    api_thread.start()

    time.sleep(1)

    for fc in pairs:
        app.reqHistoricalData(fc.requestId(), fc.contract(), '', '2 D',
                              '1 hour', 'BID', 0, 2, True, [])

    try:
        lastTS = {}
        for cp in pairs:
            lastTS[cp.pair()] = -1

        while True:
            time.sleep(10)
            for cp in pairs:
                data = app.data[cp.pair()]
                print(
                    'last close price for ', cp.pair(), 'is', data[0][4], '@',
                    datetime.fromtimestamp(int(
                        data[0][0])).strftime('%b %d %Y %H:%M:%S'))
                if lastTS[cp.pair()] == data[0][0]:
                    continue
                lastTS[cp.pair()] = data[0][0]
                candles = []
                cnt = 1
                while len(candles) != config.BS_NUMCANDLES:
                    candles.append(
                        Candle(data[cnt][1], data[cnt][2], data[cnt][3],
                               data[cnt][4]))
                    if len(candles) == 1:
                        ts = datetime.fromtimestamp(int(data[cnt][0]))
                        print(cp.pair(), ': checking candle patterns @',
                              ts.strftime('%b %d %Y %H:%M:%S'))
                    cnt += 1

                cg = CandleGroup(candles)
                found = False
                msgString = ''
                if candles[0].isHammer():
                    msgString += cp.pair(
                    ) + ' : hammer found for candle @ ' + ts.strftime(
                        '%b %d %Y %H:%M:%S\n')
                    found = True
                if candles[0].isHangingMan():
                    msgString += cp.pair(
                    ) + ' : hanging man found for candle @ ' + ts.strftime(
                        '%b %d %Y %H:%M:%S\n')
                    found = True
                if cg.bigShadow(maxBodyRatio=config.BS_BODYRATIO,
                                wickPercent=config.BS_WICKPERCENTAGE):
                    msgString += cp.pair(
                    ) + ' : big shadow found for candle @ ' + ts.strftime(
                        '%b %d %Y %H:%M:%S\n')
                    found = True

                if found:
                    print(msgString)
                    sendtext(msgString)

    except KeyboardInterrupt:
        pass

    app.disconnect()
示例#28
0
    def get_candles(self):
        candles: list = self.api.get_realtime_candles(self.active.name,
                                                      self.candle_size)

        return list(map(lambda key: Candle(candles[key]), candles))
示例#29
0
from ichimoku import Ichimoku
from candle import Candle

symbols = ['BTCUSDT']
candles = {}
ichimoku = {}

client = Client('', '')
bsm = BinanceSocketManager(client)

for symbol in symbols:
    klines = client.get_klines(symbol=symbol, interval=KLINE_INTERVAL_1MINUTE)
    candles[symbol] = list(
        map(
            lambda candle: Candle(candle[0], float(candle[1]), float(candle[
                4]), float(candle[2]), float(candle[3])),
            klines[len(klines) - 53:]))

    # print(candles[symbol])
    ichimoku[symbol] = Ichimoku(candles[symbol][:-1])
    print('RESULT')
    print(ichimoku[symbol].result)


def handle_message(msg):
    if msg['e'] == 'error':
        print(str(msg))
    elif msg['e'] == 'kline':
        kline = msg['k']
        handle_new_candle(
            msg['s'],
示例#30
0
class Trader:
    def __init__(self, username, password, startTime, endTime, period):
        periodList = {
            '5': 300,
            '15': 900,
            '30': 1800,
            '120': 7200,
            '240': 14400,
            '1440': 86400
        }
        self.period = periodList[period]
        self.pair = "USDT_BTC"
        self.onTop = "SimpleMM"
        self.order = 'IDLE'

        self.startTime = self.TimeToUnix(startTime)
        self.endTime = self.TimeToUnix(endTime)

        self.username = username
        self.password = password

        self.CandleData = None
        self.candles = []  # CANDLE LIST

        #NECESSIDADES PARA PLOTAR GRAFICO
        self.simple = []
        self.exp20 = []
        self.exp30 = []
        self.lugares_compra = []
        self.lugares_venda = []
        self.BSPoints = ['', '', '']

        self.conn = self.Connection()

        self.player1 = Player()
        self.player2 = Player()

    #Connects with Poloniex
    def Connection(self):
        counter = 0
        while True:
            try:
                print('Trying connection')
                conn = poloniex(self.username, self.password)
                return conn
            except:
                wait(1)
                counter += 1
                pass
                if counter == 10:
                    print('fail connection please try again')
                    break

    #Convert time format (Ymd to Unix)
    def TimeToUnix(self, date):
        return time.mktime(
            datetime.datetime.strptime(date, "%Y%m%d").timetuple())

    #Convert time format (Unix to dmY)
    def UnixToTime(self, date):
        return datetime.datetime.utcfromtimestamp(
            float(date)).strftime('%d-%m-%Y')

    #Estructure for ticker
    def setTickerList(self, ticker):
        tickerListNames = [
            'last', 'lowestAsk', 'highestBid', 'percentChange', 'baseVolume',
            'quoteVolume', 'isFrozen', '24hrHigh', '24hrLow'
        ]
        tickerList = dict()
        tickerList['currencyPair'] = []
        for name in tickerListNames:
            tickerList[name] = []

        for name in ticker:
            tickerList['currencyPair'].append(name)
            for label in tickerListNames:
                try:
                    tickerList[label].append(ticker[name][label])
                except:
                    tickerList[label].append(False)
        return tickerList

    #Get all candles data
    def getCandleHistoricalData(self):
        self.CandleData = self.conn.api_query(
            "returnChartData", {
                "currencyPair": self.pair,
                "start": self.startTime,
                "end": self.endTime,
                "period": self.period
            })
        self.candles = Candle(self.CandleData)

    def Get_Ticker(self):
        ticker = self.conn.returnTicker()
        tickerList = self.setTickerList(ticker)
        self.tickerList = pd.DataFrame(tickerList,
                                       columns=[
                                           'currencyPair', 'last', 'lowestAsk',
                                           'highestBid', 'percentChange',
                                           'baseVolume', 'quoteVolume',
                                           'isFrozen', '24hrHigh', '24hrLow'
                                       ])
        return self.tickerList

    # OBS :Poderia ter apenas dado pass nos 50 primeiros e usar slicing para controlar as janelas - mais facil
    def Backtest(self):
        strat1 = BacktestStrategy()
        strat2 = BacktestStrategy()

        player1 = Player()

        SimpleAvData = []
        ExpAvData = []

        prevExpAv20 = None
        prevExpAv30 = None
        ExpAvData20 = None
        ExpAvData30 = None
        firstCandle = True

        bank = 0
        counter = 0

        for x in range(0, len(self.candles)):
            #starting average vectors
            if x < 50:
                SimpleAvData.append(self.candles.close(x))
                self.simple.append(self.candles.close(x))
                self.exp20.append(self.candles.close(x))
                self.exp30.append(self.candles.close(x))
                if x < 30:
                    ExpAvData.append(self.candles.close(x))
                else:
                    ExpAvData.append(self.candles.close(x))
                    ExpAvData.pop(0)
            else:
                if firstCandle == True:
                    PrevSimpleAv20 = None
                    PrevSimpleAv50 = None
                    firstCandle = False
                else:

                    SimpleAvData.append(self.candles.close(x))
                    SimpleAvData.pop(0)

                    ExpAvData.append(self.candles.close(x))
                    ExpAvData.pop(0)

                    #Momento de compra venda ou idle
                    self.BSPoints = player1.OrderRules(self.candles.open(x),
                                                       self.candles.date(x),
                                                       self.order, x)

                    if self.BSPoints[2] == 'BUY':
                        self.lugares_compra.append(self.BSPoints)

                    elif self.BSPoints[2] == 'SELL':
                        self.lugares_venda.append(self.BSPoints)

                    SimpleAv20, SimpleAv50 = self.MMS(SimpleAvData)
                    ExpAvData30, prevExpAv30 = self.MME(
                        ExpAvData30, self.candles.close(x), 30)
                    ExpAvData20, prevExpAv20 = self.MME(
                        ExpAvData20, self.candles.close(x), 20)

                    self.simple.append(SimpleAv20)
                    self.exp30.append(ExpAvData30)
                    self.exp20.append(ExpAvData20)
                    '''
					Agora abaixo vao as estrategias chamadas da class Strategy, podemos
					instaciar um objeto de class para cada estrategia e ter todos os resultados do 
					backtest ao final da apresentacao
					'''
                    if PrevSimpleAv20 == None:
                        PrevSimpleAv20 = SimpleAv20

                    else:
                        self.onTop, self.order = strat2.ExpSimpleAv(
                            SimpleAv20, ExpAvData20, ExpAvData30, self.onTop)

                    PrevSimpleAv20 = SimpleAv20

        return player1

    #Media Movel Simples
    def MMS(self, SimpleAvData):
        #print (len(SimpleAvData))
        return np.average(SimpleAvData), np.average(SimpleAvData[30:])

    #Media Movel Exponencial
    def MME(self, prevEMA, lastCandleValue, period):
        if prevEMA != None:
            multiplier = 2 / (period + 1)
            new_prevEMA = prevEMA
            mme = ((lastCandleValue - prevEMA)) * multiplier + prevEMA

        else:
            mme = lastCandleValue
            new_prevEMA = mme
        return mme, new_prevEMA

    def graphs(self):
        xdate = []

        fig, ax = plt.subplots()

        ax.xaxis.set_major_locator(ticker.MaxNLocator(6))

        for i in range(0, len(self.candles)):
            xdate.append(self.candles.getTime(i))

        def nested_mydate(x, pos):
            try:
                return xdate[int(x)]
            except IndexError:
                return ''

        ax.xaxis.set_major_formatter(ticker.FuncFormatter(nested_mydate))

        candlestick2_ohlc(ax,
                          self.candles.open2(),
                          self.candles.high2(),
                          self.candles.low2(),
                          self.candles.close2(),
                          width=0.6)

        fig.autofmt_xdate()
        fig.tight_layout()

        #MMS e MME
        plt.plot(self.simple, 'b--')
        plt.plot(self.exp20, 'r--')
        plt.plot(self.exp30, 'r--')

        #pontos de compra
        xcompra_val = [x[0] for x in self.lugares_compra]
        ycompra_val = [x[1] for x in self.lugares_compra]

        #pontos de venda
        xvenda_val = [x[0] for x in self.lugares_venda]
        yvenda_val = [x[1] for x in self.lugares_venda]

        plt.plot(ycompra_val, xcompra_val, 'g^')
        plt.plot(yvenda_val, xvenda_val, 'y^')
        plt.show()