Esempio n. 1
0
class AccountUtil:
    def __init__(self, exchange, isTest=False):
        logging.debug("Initialising AccountUtil()")
        self.exchange = exchange
        self.isTest = isTest
        self.P = Pull()

    def _calcValue(self, accountDetails):
        logging.debug("Starting AccountUtil._calcValue")
        value = 0
        for coin in [c for c in list(accountDetails) if c != "BTC"]:
            value += accountDetails[coin] * (
                self.P.assetPrice(
                    exchange=self.exchange, asset="%sBTC" % coin, dir="sell"
                )
                if not self.isTest
                else self.isTest[coin]
            )
        return value

    def getValue(self, initCapital, isTest=False):
        logging.debug("Starting AccountValue.getValue")
        capDict = {"initialCapital": initCapital}
        accountDetails = (
            self.P.getAccount(exchange=self.exchange) if not isTest else isTest
        )
        capDict["liquidCurrent"] = round(accountDetails["BTC"], 8)
        capDict["paperCurrent"] = round(
            capDict["liquidCurrent"] + self._calcValue(accountDetails), 8
        )
        capDict["paperPnL"] = round(
            capDict["paperCurrent"] / capDict["initialCapital"], 2
        )
        capDict["percentAllocated"] = round(
            (capDict["paperCurrent"] - capDict["liquidCurrent"])
            / capDict["paperCurrent"],
            2,
        )
        return capDict
Esempio n. 2
0
class Enter:
    def __init__(self, stratName, isTest=False, testAssets=None):
        logging.debug("Initialising Enter()")
        self.compPath = "%s/Pipeline/resources/%s" % (Settings.BASE_PATH, stratName)
        self.stratName = stratName
        self.isTest = isTest
        with open("%s/config.yml" % self.compPath) as stratFile:
            self.config = yaml.load(stratFile)
        self.assetList = Select(stratName).assets() if not isTest else testAssets
        self.enterStrat = eval(self.config["enter"]["name"])(
            stratName=stratName, assetList=self.assetList, isTest=isTest
        )
        self.OT = (
            OpenTrade(stratName=stratName, isLive=self.config["isLive"])
            if not isTest
            else None
        )
        self.pull = Pull()
        self.col = MongoClient("localhost", 27017)[stratName]["currentPositions"]

    def run(self):
        try:
            logging.info("Starting Enter.run: %s" % datetime.now())
            startTime = time.time()
            openList = []
            currentPositions = [
                val["assetName"] for val in list(self.col.find({}, {"assetName": 1}))
            ]
            self.OT.initRun() if not self.isTest else None
            self.enterStrat.before() if not self.isTest else None
            for asset, exchange in [
                val
                for val in self.assetList
                if "%sBTC" % val[0] not in currentPositions
            ]:
                logging.debug("Starting asset: %s" % asset)
                if self.enterStrat.run(asset):
                    logging.info("Entering trade: %s" % asset)
                    openPrice = (
                        self.pull.assetPrice(
                            exchange=exchange, asset="%sBTC" % asset, dir="buy"
                        )
                        if not self.isTest
                        else 1
                    )
                    if openPrice != -1:
                        openList.append(asset)
                        self.OT.open(
                            assetVals=("%sBTC" % asset, exchange, openPrice)
                        ) if not self.isTest else None
            self.OT.updateBooks() if not self.isTest else None
            logging.info(
                "Ending Enter run. Took: %s seconds" % round(time.time() - startTime)
            )
            logging.info("%s assets analysed" % len(self.assetList))
            logging.info(
                "Entering trades: \n %s" % openList
                if len(openList) != 0
                else "0 trades entered"
            )
            return openList if self.isTest else None
        except Exception as e:
            EmailUtil(strat=self.stratName).errorExit(
                file=self.stratName,
                funct="Enter.run()",
                message=f"Error message: {e}, \n Asset: {asset}",
            )
            raise Exception