def cli(test_key,
        configuration_file,
        config: Config = Provide[Container.config]) -> None:
    config.from_yaml(configuration_file)
    config.override_config_from_cli(**locals())
    print(config.get(test_key))
    pass
Пример #2
0
 def test_config(self):
     config = Config()
     config.LoadEnv(".env.sample")
     self.assertEqual(config.Endpoint, "mqtt.trailmonitoringsystem.com")
     self.assertEqual(config.Region, "us-east-1")
     self.assertEqual(config.SecretName, "hydrocutcerts")
     self.assertEqual(config.ClientId,
                      "hydrocut-cb044dc2-031f-4876-a15d-5c94e7bbc6a4")
     self.assertEqual(config.Topic, "tms/status/hydrocut")
    def fetch():
        access_token = Config.get('oanda')['api_key']
        account_id = Config.get('oanda')['account_id']

        api = API(access_token=access_token, environment="practice")

        instruments = "DE30_EUR,EUR_USD,EUR_JPY"
        s = PricingStream(accountID=account_id,
                          params={"instruments": instruments})
        try:
            n = 0
            for R in api.request(s):
                print(json.dumps(R, indent=2))
                n += 1
                if n > 10:
                    s.terminate("maxrecs received: {}".format(MAXREC))

        except V20Error as e:
            print("Error: {}".format(e))
Пример #4
0
    def __init_db(self):

        config = Config.get('database')

        self.__engine_url = config['engine'] + '://' + config['user'] + ':' + config['password'] + '@' + config['host'] + '/' + config['name']

        self.engine = create_engine(self.__engine_url, echo=False)

        Session = sessionmaker(bind=self.engine, autoflush=False)
        self.session = Session()
Пример #5
0
    def place_order(currency_pair, stop_loss, take_profit):

        currency_pair = currency_pair[:3] + '_' + currency_pair[-3:]

        access_token = Config.get('oanda')['api_key']
        account_id = Config.get('oanda')['account_id']

        api = oandapyV20.API(access_token=access_token)

        mkt_order = MarketOrderRequest(
            instrument=currency_pair,
            units=1,
            takeProfitOnFill=TakeProfitDetails(price=take_profit).data,
            stopLossOnFill=StopLossDetails(price=stop_loss).data)

        # create the OrderCreate request
        r = orders.OrderCreate(account_id, data=mkt_order.data)
        try:
            # create the OrderCreate request
            rv = api.request(r)
        except oandapyV20.exceptions.V20Error as err:
            print(r.status_code, err)
        else:
            print(json.dumps(rv, indent=2))
class TestSecretsMethods(unittest.TestCase):
    def setUp(self):
        self.configFile = Config()
        self.configFile.LoadEnv(".env.sample")

    def tearDown(self):
        pass

    def test_secrets(self):
        secret = Secrets(self.configFile.Region, self.configFile.SecretName)
        secret.ReadSecrets()

        self.assertIsNotNone(secret.CertFileBin)
        self.assertIsNotNone(secret.PropertyFileBin)

    def test_secretsclassmethod(self):
        secret = Secrets.GetSecrets(self.configFile.Region,
                                    self.configFile.SecretName)

        self.assertIsNotNone(secret.CertFileBin)
        self.assertIsNotNone(secret.PropertyFileBin)
class TestSecretsMethods(unittest.TestCase):
    def setUp(self):
        self.configFile = Config()
        self.configFile.LoadEnv(".env.sample")

    def tearDown(self):
        pass

    def test_publish(self):
        mqttpublish = MQTTPublish(self.configFile)
        try:
            mqttpublish.Connect()
        except Exception as e:
            print(f"Exception: {str(e)}")
            self.assertFalse(True)
        
        mqttpublish.Publish(MQTTPublishResponse(clientid = self.configFile.ClientId, status=TrailStatus.closed.value, status_str=TrailStatus.closed.name, ok=1))
        mqttpublish.Publish(MQTTPublishResponse(clientid = self.configFile.ClientId, status=TrailStatus.trail1.value, status_str=TrailStatus.trail1.name, ok=1))
        mqttpublish.Publish(MQTTPublishResponse(clientid = self.configFile.ClientId, status=TrailStatus.trail2.value, status_str=TrailStatus.trail2.name, ok=1))
        mqttpublish.Publish(MQTTPublishResponse(clientid = self.configFile.ClientId, status=TrailStatus.open.value, status_str=TrailStatus.open.name, ok=1))

        mqttpublish.SendStatus()
        
        del mqttpublish
Пример #8
0
class OandaHistoryPriceFetcher:
    config = Config.get('oanda')
    client = API(access_token=config['api_key'])

    date_format_in = '%Y-%m-%dT%H:%M:%SZ'
    date_format_out = '%Y-%m-%dT%H:%M:%S'

    def fetch_data_frame(self, _from: datetime.datetime,
                         _to: datetime.datetime, gran: str, symbol: str):

        instr = symbol[:3] + '_' + symbol[-3:]

        params = {
            "granularity": gran,
            "from": _from.strftime(self.date_format_in),
            "to": _to.strftime(self.date_format_in)
        }

        candles = {}

        for r in InstrumentsCandlesFactory(instrument=instr, params=params):
            print("REQUEST: {} {} {}".format(r, r.__class__.__name__,
                                             r.params))
            self.client.request(r)
            for candle in r.response.get('candles'):
                dt = datetime.datetime.strptime(
                    candle.get('time')[0:19], self.date_format_out)
                candles[dt] = []
                candles[dt].append(candle['mid']['o'])
                candles[dt].append(candle['mid']['h'])
                candles[dt].append(candle['mid']['l'])
                candles[dt].append(candle['mid']['c'])
                candles[dt].append(candle['volume'])

        df = pd.DataFrame.from_dict(candles, orient='index')
        df.columns = ['open', 'high', 'low', 'close', 'volume']

        return df

    def fetch_to_file(self,
                      _from: datetime.datetime,
                      _to: datetime.datetime,
                      gran='H1',
                      symbol='EURUSD'):
        file_path = os.path.join(os.path.abspath(os.getcwd()), 'resources',
                                 'oanda_prices', symbol + '.csv')
        mode = 'w'
        if (os.path.isfile(file_path)):
            mode = 'a'
            with open(file_path, "r") as O:
                all_lines = O.readlines()
                last_line = all_lines[len(all_lines) - 1]
                last_dt = datetime.datetime.strptime(last_line[0:19],
                                                     self.date_format_out)

                if gran == 'M1':
                    delta = datetime.timedelta(minutes=1)
                elif gran == 'D1':
                    delta = datetime.timedelta(days=1)
                else:
                    delta = datetime.timedelta(hours=1)

                _from = last_dt + delta

        instr = symbol[:3] + '_' + symbol[-3:]

        params = {
            "granularity": gran,
            "from": _from.strftime(self.date_format_in),
            "to": _to.strftime(self.date_format_in)
        }

        with open(file_path, mode) as O:

            for r in InstrumentsCandlesFactory(instrument=instr,
                                               params=params):
                print("REQUEST: {} {} {}".format(r, r.__class__.__name__,
                                                 r.params))
                self.client.request(r)
                OandaHistoryPriceFetcher.__write_rec_to_file(r.response, O)

    @staticmethod
    def __write_rec_to_file(r, h):
        for candle in r.get('candles'):
            ctime = candle.get('time')[0:19]
            try:
                rec = "{time},{complete},{o},{h},{l},{c},{v}".format(
                    time=ctime,
                    complete=candle['complete'],
                    o=candle['mid']['o'],
                    h=candle['mid']['h'],
                    l=candle['mid']['l'],
                    c=candle['mid']['c'],
                    v=candle['volume'],
                )
            except Exception as e:
                print(e, r)
            else:
                h.write("\n" + rec)

    def fetch_to_db(self, _from: datetime.datetime, _to: datetime.datetime,
                    gran: str, symbol: str):

        instr = symbol[:3] + '_' + symbol[-3:]

        params = {
            "granularity": gran,
            "from": _from.strftime(self.date_format_in),
            "to": _to.strftime(self.date_format_in)
        }

        session = Connection.get_instance().get_session()

        existing_quotes = session.query(PriceQuote) \
            .filter_by(symbol=symbol) \
            .filter(PriceQuote.datetime >= (_from - datetime.timedelta(minutes=1)))\
            .filter(PriceQuote.datetime <= _to).all()

        existing_quote_dts = list(
            map(lambda _quote: _quote.datetime.strftime(self.date_format_out),
                existing_quotes))

        try:
            for r in InstrumentsCandlesFactory(instrument=instr,
                                               params=params):
                print("REQUEST: {} {} {}".format(r, r.__class__.__name__,
                                                 r.params))
                rv = self.client.request(r)

                for candle in r.response.get('candles'):
                    dt = candle.get('time')[0:19]
                    print(candle)
                    if candle['complete'] and dt not in existing_quote_dts:
                        quote = PriceQuote(
                            symbol,
                            datetime.datetime.strptime(dt,
                                                       self.date_format_out),
                            candle['mid']['h'], candle['mid']['l'],
                            candle['volume'])
                        existing_quote_dts.append(dt)
                        session.add(quote)

            session.commit()

        except SQLAlchemyError as e:
            session.rollback()
            print(e)

        except Exception as e:
            print(e)
 def setUp(self):
     self.configFile = Config()
     self.configFile.LoadEnv(".env.sample")
Пример #10
0
logging.getLogger('').addHandler(console)

# establish initial retroactive value and modify per arguments
retroactive = False
if args.retro:
    retroactive = True
    logging.info("Retroactive replication required per supplied argument.")

logging.info(
    "Let's take a look at the configuration for the {} profile.".format(
        args.profile))

# pull variables from xml configuration

config_xml_path = os.path.join(os.path.dirname(__file__), 'config.xml')
config = Config(config_xml_path, args.profile)

if config.retro == 1:
    retroactive = True
    logging.info(
        "Retroactive replication required per supplied configuration.")

# connect to database and begin replication process
logging.info("We're starting this thing off right for the {} table.".format(
    config.table))

engine_source = create_engine(config.conn_source,
                              echo=False,
                              connect_args=config.get_ssl_src())
engine_destination = create_engine(config.conn_destination,
                                   echo=False,
class TestHCStatusREST(unittest.TestCase):
    def setUp(self):
        self.configFile = Config()
        self.configFile.LoadEnv(".env.sample")

    def tearDown(self):
        pass

    def test_parsestatus_open(self):
        data = {
            "id": "da89b866-ef8d-4853-aab3-7c0f3a1c2fbd",
            "name": "The Hydrocut Trails",
            "status": "open",
            "message": "All trails open!  Enjoy the sunshine before the next rain.",
            "openHashtag": "#trailsopen",
            "closeHashtag": "#trailsclosed",
            "imageUrl": "https://scontent-ort2-1.cdninstagram.com/v/t51.29350-15/182949379_542177793829878_588216878496601361_n.jpg?_nc_cat=105&ccb=1-3&_nc_sid=8ae9d6&_nc_ohc=n5HRbs0klvMAX86Yxwa&_nc_ht=scontent-ort2-1.cdninstagram.com&oh=3869ef7b392ea66bbc9cc6876191d51f&oe=60BB19A8",
            "instagramPostId": "17866670747435204",
            "instagramPermalink": "https://www.instagram.com/p/COibv8-AfZr/",
            "updatedAt": "2021-05-06T16:27:12.803Z",
            "airTemp": 9,
            "groundTemp": 11,
            "trails": [
                {
                "id": "4aec042a-c1bb-4976-a5bf-9abea20fe5d7",
                "name": "Glasgow",
                "closeHashtag": "#glasgowclosed",
                "status": "open",
                "updatedAt": "2021-05-06T16:27:12.834Z"
                },
                {
                "id": "instagram|17841402338843416|default",
                "name": "Snyder's",
                "closeHashtag": "#snydersclosed",
                "status": "open",
                "updatedAt": "2021-05-05T18:21:12.868Z"
                }
            ],
            "user": {
                "userId": "instagram|17841402338843416",
                "username": "******"
            }
        }        
        rest = HCStatusREST(self.configFile.HCStatusUrl)
        self.assertEqual(rest.ParseStatus(data), TrailStatus.open)

    def test_parsestatus_closed(self):
        data = {
            "id": "da89b866-ef8d-4853-aab3-7c0f3a1c2fbd",
            "name": "The Hydrocut Trails",
            "status": "closed",
            "message": "All trails open!  Enjoy the sunshine before the next rain.",
            "openHashtag": "#trailsopen",
            "closeHashtag": "#trailsclosed",
            "imageUrl": "https://scontent-ort2-1.cdninstagram.com/v/t51.29350-15/182949379_542177793829878_588216878496601361_n.jpg?_nc_cat=105&ccb=1-3&_nc_sid=8ae9d6&_nc_ohc=n5HRbs0klvMAX86Yxwa&_nc_ht=scontent-ort2-1.cdninstagram.com&oh=3869ef7b392ea66bbc9cc6876191d51f&oe=60BB19A8",
            "instagramPostId": "17866670747435204",
            "instagramPermalink": "https://www.instagram.com/p/COibv8-AfZr/",
            "updatedAt": "2021-05-06T16:27:12.803Z",
            "airTemp": 9,
            "groundTemp": 11,
            "trails": [
                {
                "id": "4aec042a-c1bb-4976-a5bf-9abea20fe5d7",
                "name": "Glasgow",
                "closeHashtag": "#glasgowclosed",
                "status": "closed",
                "updatedAt": "2021-05-06T16:27:12.834Z"
                },
                {
                "id": "instagram|17841402338843416|default",
                "name": "Snyder's",
                "closeHashtag": "#snydersclosed",
                "status": "closed",
                "updatedAt": "2021-05-05T18:21:12.868Z"
                }
            ],
            "user": {
                "userId": "instagram|17841402338843416",
                "username": "******"
            }
        }        
        rest = HCStatusREST(self.configFile.HCStatusUrl)
        self.assertEqual(rest.ParseStatus(data), TrailStatus.closed)

    def test_parsestatus_trail1(self):
        data = {
            "id": "da89b866-ef8d-4853-aab3-7c0f3a1c2fbd",
            "name": "The Hydrocut Trails",
            "status": "open",
            "message": "All trails open!  Enjoy the sunshine before the next rain.",
            "openHashtag": "#trailsopen",
            "closeHashtag": "#trailsclosed",
            "imageUrl": "https://scontent-ort2-1.cdninstagram.com/v/t51.29350-15/182949379_542177793829878_588216878496601361_n.jpg?_nc_cat=105&ccb=1-3&_nc_sid=8ae9d6&_nc_ohc=n5HRbs0klvMAX86Yxwa&_nc_ht=scontent-ort2-1.cdninstagram.com&oh=3869ef7b392ea66bbc9cc6876191d51f&oe=60BB19A8",
            "instagramPostId": "17866670747435204",
            "instagramPermalink": "https://www.instagram.com/p/COibv8-AfZr/",
            "updatedAt": "2021-05-06T16:27:12.803Z",
            "airTemp": 9,
            "groundTemp": 11,
            "trails": [
                {
                "id": "4aec042a-c1bb-4976-a5bf-9abea20fe5d7",
                "name": "Glasgow",
                "closeHashtag": "#glasgowclosed",
                "status": "open",
                "updatedAt": "2021-05-06T16:27:12.834Z"
                },
                {
                "id": "instagram|17841402338843416|default",
                "name": "Snyder's",
                "closeHashtag": "#snydersclosed",
                "status": "closed",
                "updatedAt": "2021-05-05T18:21:12.868Z"
                }
            ],
            "user": {
                "userId": "instagram|17841402338843416",
                "username": "******"
            }
        }        
        rest = HCStatusREST(self.configFile.HCStatusUrl)
        self.assertEqual(rest.ParseStatus(data), TrailStatus.trail1)

    def test_parsestatus_trail1(self):
        data = {
            "id": "da89b866-ef8d-4853-aab3-7c0f3a1c2fbd",
            "name": "The Hydrocut Trails",
            "status": "open",
            "message": "All trails open!  Enjoy the sunshine before the next rain.",
            "openHashtag": "#trailsopen",
            "closeHashtag": "#trailsclosed",
            "imageUrl": "https://scontent-ort2-1.cdninstagram.com/v/t51.29350-15/182949379_542177793829878_588216878496601361_n.jpg?_nc_cat=105&ccb=1-3&_nc_sid=8ae9d6&_nc_ohc=n5HRbs0klvMAX86Yxwa&_nc_ht=scontent-ort2-1.cdninstagram.com&oh=3869ef7b392ea66bbc9cc6876191d51f&oe=60BB19A8",
            "instagramPostId": "17866670747435204",
            "instagramPermalink": "https://www.instagram.com/p/COibv8-AfZr/",
            "updatedAt": "2021-05-06T16:27:12.803Z",
            "airTemp": 9,
            "groundTemp": 11,
            "trails": [
                {
                "id": "4aec042a-c1bb-4976-a5bf-9abea20fe5d7",
                "name": "Glasgow",
                "closeHashtag": "#glasgowclosed",
                "status": "closed",
                "updatedAt": "2021-05-06T16:27:12.834Z"
                },
                {
                "id": "instagram|17841402338843416|default",
                "name": "Snyder's",
                "closeHashtag": "#snydersclosed",
                "status": "open",
                "updatedAt": "2021-05-05T18:21:12.868Z"
                }
            ],
            "user": {
                "userId": "instagram|17841402338843416",
                "username": "******"
            }
        }        
        rest = HCStatusREST(self.configFile.HCStatusUrl)
        self.assertEqual(rest.ParseStatus(data), TrailStatus.trail2)

    def test_get(self):
        rest = HCStatusREST(self.configFile.HCStatusUrl)
        result = rest.Get()
        self.assertIsNotNone(result)
        self.assertTrue('status' in result)

    def test_getstatus(self):
        rest = HCStatusREST(self.configFile.HCStatusUrl)
        result = rest.GetStatus()
        # Well, this will only happen if the trails are actually open
        self.assertIsNotNone(result, TrailStatus.open)