Example #1
0
 def test__autochartist(self, mock_get):
     """get autochartist information for an instrument."""
     tid = "_v3_forexlabs_autochartist"
     td = TestData(responses, tid)
     r = labs.Autochartist(params=td.params)
     mock_get.register_uri('GET',
                           "{}/{}".format(api.api_url, r),
                           text=json.dumps(td.resp))
     api.request(r)
     self.assertTrue(td.resp == r.response)
Example #2
0
def get_signals(client):

    print('getting signals')

    # params          = {}
    # r               = labs.Autochartist(params=params)

    params = {}
    r = labs.Autochartist()
    client.request(r)
    rv = r.response
    signals = pd.DataFrame(rv['signals'])

    metakeys = signals.loc[0, 'meta'].keys()
    datakeys = signals.loc[0, 'data'].keys()
    for m in metakeys:
        signals[m] = signals.apply(lambda x: x['meta'][m], axis=1)
    for d in datakeys:
        signals[d] = signals.apply(lambda x: x['data'][d], axis=1)
    signals = signals[[
        c for c in signals.columns if c not in ['meta', 'data']
    ]]

    scorekeys = signals.loc[0, 'scores'].keys()
    for s in scorekeys:
        signals[s] = signals.apply(lambda x: x['scores'][s], axis=1)
    signals = signals[[c for c in signals.columns if c not in ['scores']]]

    historykeys = signals.loc[0, 'historicalstats'].keys()
    predictionkeys = signals.loc[0, 'prediction'].keys()
    for h in historykeys:
        signals[h] = signals.apply(lambda x: x['historicalstats'][h], axis=1)
    for p in predictionkeys:
        signals[p] = signals.apply(lambda x: x['prediction'][p], axis=1)
    signals = signals[[
        c for c in signals.columns
        if c not in ['historicalstats', 'prediction']
    ]]

    for c in ['pattern', 'symbol', 'hourofday']:
        signals[c + '_percent'] = signals.apply(lambda x: x[c]['percent'],
                                                axis=1)
    signals = signals[[
        c for c in signals.columns
        if c not in ['pattern', 'symbol', 'hourofday']
    ]]

    pointskey = signals.loc[0, 'points'].keys()
    for p in pointskey:
        signals[p] = signals.apply(lambda x: x['points'][p], axis=1)
    signals = signals[[c for c in signals.columns if c not in ['points']]]

    for c in ['support', 'resistance']:
        signals[c + '_y0'] = signals.apply(lambda x: x[c]['y0'], axis=1)
        signals[c + '_y1'] = signals.apply(lambda x: x[c]['y1'], axis=1)
        signals[c + '_x0'] = signals.apply(lambda x: x[c]['x0'], axis=1)
        signals[c + '_x1'] = signals.apply(lambda x: x[c]['x1'], axis=1)
    signals = signals[[
        c for c in signals.columns if c not in ['support', 'resistance']
    ]]

    time_cols = [
        'patternendtime', 'timefrom', 'timeto', 'support_x0', 'support_x1',
        'resistance_x0', 'resistance_x1'
    ]
    for c in time_cols:
        signals[c] = signals[c].apply(
            lambda t: convert_ToLocal(convert_UnixTime(t)))

    return signals
Example #3
0
def autochartist():

    dt = datetime.datetime.now()
    utc_time = dt.replace(tzinfo=timezone.utc)
    utc_timestamp = int(utc_time.timestamp())

    oanda = oandapy.API(environment="practice", access_token=OANDA_API_KEY)
    oandaV20 = oandapyV20.API(environment="practice",
                              access_token=OANDA_API_KEY)

    recommended = []
    #response = oanda.get_autochartist()
    r = labs.Autochartist()
    oandaV20.request(r)
    response = r.response
    signals = response.get("signals")
    for x in signals:
        inst = x.get("instrument")
        instrument = inst.replace('_', '')
        recommended.append(instrument)
        pricelow = 0.0
        pricehigh = 0.0
        probability = 0.0
        meta = x.get("meta")
        if (meta):
            #print (meta)
            probability = meta.get("probability")
            interval = meta.get("interval")
            if (interval == 0):
                interval = 1440

            scoresTotal = 0
            quality = 0

            scores = meta.get("scores")
            if (scores):
                quality = scores.get("quality")
                clarity = scores.get("clarity")
                initialTrend = scores.get("initialtrend")
                breakout = scores.get("breakout")
                uniformity = scores.get("uniformity")
                try:
                    scoresTotal = quality + clarity + initialTrend + breakout + uniformity
                except:
                    pass

            direction = meta.get("direction")

        data = x.get("data")
        if (data):
            prediction = data.get("prediction")
            if (prediction):
                #print (prediction)
                pricelow = prediction.get("pricelow")
                pricehigh = prediction.get("pricehigh")
                timefrom = prediction.get("timefrom")
                timeto = prediction.get("timeto")

        print(
            "{} direction {} interval {} probability {} quality {} scoresTotal {} pricelow {} pricehigh {} age {}"
            .format(instrument, direction, interval, probability, quality,
                    scoresTotal, pricelow, pricehigh, utc_timestamp - timeto))
        if (pricelow > 0.0):
            for y in symbol_id:
                if (y == instrument):
                    #if (probability > 65.0 and quality >= 6):
                    if (probability > 75.0 or quality >= 6):
                        response = oanda.get_prices(instruments=inst)
                        prices = response.get("prices")
                        asking_price = prices[0].get("ask")
                        print("Current price {}, pricelow {}, pricehigh{}".
                              format(asking_price, pricelow, pricehigh))

                        limit = 0
                        stop = 0

                        midprice = (pricehigh - pricelow) / 2 + pricelow
                        if (direction == 1 and midprice > asking_price):
                            limit = midprice
                            stop = asking_price - (
                                (midprice - asking_price) / 2)
                        elif (direction == -1 and midprice < asking_price):
                            limit = midprice
                            stop = asking_price + (
                                (asking_price - midprice) / 2)

                        if (limit > 0):
                            epic_id = getEpicId(y)

                            deal, oldStop = getDealId(epic_id, direction)
                            print("Deal is {} oldStop is {} stop is {}".format(
                                deal, oldStop, stop))

                            if (deal == "None"):
                                if (direction == 1
                                        and asking_price < pricelow):
                                    performTradeAbsolute(
                                        epic_id, "BUY", stop, limit, 1)
                                elif (direction == -1
                                      and asking_price > pricehigh):
                                    performTradeAbsolute(
                                        epic_id, "SELL", stop, limit, 1)
                            else:
                                if (direction == 1 and oldStop > stop):
                                    stop = oldStop
                                elif (direction == -1 and oldStop < stop):
                                    stop = oldStop
                                if (stop != oldStop):
                                    updatePosition(deal, y, stop, limit)

    print("Current time is {}".format(utc_timestamp))
    return (recommended)
Example #4
0
def ForexlabsAutochartist(access_token, params=None):  # check
    'Get the ‘autochartist data’.'
    r = forexlabs.Autochartist(params=params)
    client = API(access_token=access_token)
    client.request(r)
    return readable_output(Munch(r.response)), Munch(r.response)