bool checkExit(Bitcoin *btcLong, Bitcoin *btcShort, Result &res, Parameters params, time_t period) {
  // close btcLong:  sell looking to match bid
  // close btcShort: buy looking to match ask
  double priceLong  = btcLong->getBid();
  double priceShort = btcShort->getAsk();
  if (priceLong > 0.0 && priceShort > 0.0) {
    res.spreadOut = (priceShort - priceLong) / priceLong;
  } else {
    res.spreadOut = 0.0;
  }
  int longId = btcLong->getId();
  int shortId = btcShort->getId();
  if (res.spreadOut > res.maxSpread[longId][shortId]) {
    res.maxSpread[longId][shortId] = res.spreadOut;
  }
  if (res.spreadOut < res.minSpread[longId][shortId]) {
    res.minSpread[longId][shortId] = res.spreadOut;
  }
  if (params.verbose) {
    std::cout << "   " << btcLong->getExchName() << "/" << btcShort->getExchName() << ":\t" << percToStr(res.spreadOut);
    std::cout << " [target " << percToStr(params.spreadExit) << ", min " << percToStr(res.minSpread[longId][shortId]) << ", max " << percToStr(res.maxSpread[longId][shortId]) << "]";
    if (res.trailing[longId][shortId] != 1.0) {
      std::cout << "   (trailing " << res.trailing[longId][shortId] * 100.0 << "%)";
    }
  }
  std::cout << std::endl;
  if (period - res.entryTime >= params.maxLength) {
    res.priceLongOut  = priceLong;
    res.priceShortOut = priceShort;
    return true;
  }
  if (priceLong > 0.0) {
    if (priceShort > 0.0) {
      if (res.spreadOut <= params.spreadExit) {
        double newTrailValue = res.spreadOut + params.trailingLim;
        if (res.trailing[longId][shortId] == 1.0) {
          if (newTrailValue < params.spreadExit) {
            res.trailing[longId][shortId] = newTrailValue;
          } else {
            res.trailing[longId][shortId] = params.spreadExit;
          }
        } else {
          if (newTrailValue <= res.trailing[longId][shortId]) {
            res.trailing[longId][shortId] = newTrailValue;
          }
          if (res.spreadOut > res.trailing[longId][shortId]) {
            res.priceLongOut  = priceLong;
            res.priceShortOut = priceShort;
            return true;
          }
        }
      } else {
        res.trailing[longId][shortId] = 1.0;
      }
    }
  }
  return false;
}
Exemple #2
0
bool checkEntry(Bitcoin* btcLong, Bitcoin* btcShort, Result& res, Parameters& params) {

  if (btcShort->getHasShort()) {
    // btcLong:  "buy" looking to match ask
    // btcShort: "sell" looking to match bid
    double priceLong = btcLong->getAsk();
    double priceShort = btcShort->getBid();
    if (priceLong > 0.0 && priceShort > 0.0) {
      res.spreadIn = (priceShort - priceLong) / priceLong;
    } else {
      res.spreadIn = 0.0;
    }
    int longId = btcLong->getId();
    int shortId = btcShort->getId();
    if (res.spreadIn > res.maxSpread[longId][shortId]) {
      res.maxSpread[longId][shortId] = res.spreadIn;
    }
    if (res.spreadIn < res.minSpread[longId][shortId]) {
      res.minSpread[longId][shortId] = res.spreadIn;
    }
    if (params.verbose) {
      *params.logFile << "   " << btcLong->getExchName() << "/" << btcShort->getExchName() << ":\t" << percToStr(res.spreadIn);
      *params.logFile << " [target " << percToStr(params.spreadEntry) << ", min " << percToStr(res.minSpread[longId][shortId]) << ", max " << percToStr(res.maxSpread[longId][shortId]) << "]";

      if (res.trailing[longId][shortId] != -1.0) {
        *params.logFile << "   trailing " << percToStr(res.trailing[longId][shortId]) << "  " << res.trailingWait[longId][shortId] << "/2";
      }
      if ((btcLong->getIsImplemented() == false || btcShort->getIsImplemented() == false) && params.demoMode == false) {
        *params.logFile << "   info only"  << std::endl;
      } else {
        *params.logFile << std::endl;
      }
    }
    if (btcLong->getIsImplemented() == true) {
      if (btcShort->getIsImplemented() == true) {
        if (priceLong > 0.0) {
          if (priceShort > 0.0) {
            if (res.spreadIn >= params.spreadEntry) {
              double newTrailValue = res.spreadIn - params.trailingLim;
              if (res.trailing[longId][shortId] == -1.0) {
                if (newTrailValue > params.spreadEntry) {
                  res.trailing[longId][shortId] = newTrailValue;
                } else {
                  res.trailing[longId][shortId] = params.spreadEntry;
                }
              } else {
                if (newTrailValue >= res.trailing[longId][shortId]) {
                  res.trailing[longId][shortId] = newTrailValue;
                  res.trailingWait[longId][shortId] = 0;
                }
                if (res.spreadIn < res.trailing[longId][shortId]) {
                  if (res.trailingWait[longId][shortId] < 2) {
                    res.trailingWait[longId][shortId]++;
                  } else {
                    res.idExchLong = longId;
                    res.idExchShort = shortId;
                    res.feesLong = btcLong->getFees();
                    res.feesShort = btcShort->getFees();
                    res.exchNameLong = btcLong->getExchName();
                    res.exchNameShort = btcShort->getExchName();
                    res.priceLongIn = priceLong;
                    res.priceShortIn = priceShort;
                    res.exitTarget = res.spreadIn - params.spreadTarget - (2.0 * btcLong->getFees() + 2.0 * btcShort->getFees());
                    res.trailingWait[longId][shortId] = 0;
                    return true;
                  }
                } else {
                  res.trailingWait[longId][shortId] = 0;
                }
              }
            } else {
              res.trailing[longId][shortId] = -1.0;
              res.trailingWait[longId][shortId] = 0;
            }
          }
        }
      }
    }
  }
  return false;
}
bool checkEntry(Bitcoin *btcLong, Bitcoin *btcShort, Result &res, Parameters params) {
  double limit = 2.0 * btcLong->getFees() + 2.0 * btcShort->getFees() + params.spreadEntry;
  if (btcShort->getHasShort()) {
    // btcLong:  buy looking to match ask
    // btcShort: sell looking to match bid
    double priceLong = btcLong->getAsk();
    double priceShort = btcShort->getBid();
    if (priceLong > 0.0 && priceShort > 0.0) {
      res.spreadIn = (priceShort - priceLong) / priceLong;
    } else {
      res.spreadIn = 0.0;
    }
    int longId = btcLong->getId();
    int shortId = btcShort->getId();
    if (res.spreadIn > res.maxSpread[longId][shortId]) {
      res.maxSpread[longId][shortId] = res.spreadIn;
    }
    if (res.spreadIn < res.minSpread[longId][shortId]) {
      res.minSpread[longId][shortId] = res.spreadIn;
    }
    if (params.verbose) {
      std::cout << "   " << btcLong->getExchName() << "/" << btcShort->getExchName() << ":\t" << percToStr(res.spreadIn);
      std::cout << " [target " << percToStr(limit) << ", min " << percToStr(res.minSpread[longId][shortId]) << ", max " << percToStr(res.maxSpread[longId][shortId]) << "]";

      if (res.trailing[longId][shortId] != -1.0) {
        std::cout << "   (trailing " << res.trailing[longId][shortId] * 100.0 << "%)";
      }
      if (btcLong->getIsImplemented() == false || btcShort->getIsImplemented() == false) {
        std::cout << "   (info only)"  << std::endl;
      } else {
        std::cout << std::endl;
      }
    }

    if (btcLong->getIsImplemented() == true) {
      if (btcShort->getIsImplemented() == true) {
        if (priceLong > 0.0) {
          if (priceShort > 0.0) {
            if (res.spreadIn >= limit) {
              double newTrailValue = res.spreadIn - params.trailingLim;
              if (res.trailing[longId][shortId] == -1.0) {
                if (newTrailValue > limit) {
                  res.trailing[longId][shortId] = newTrailValue;
                } else {
                  res.trailing[longId][shortId] = limit;
                }
              } else {
                if (newTrailValue >= res.trailing[longId][shortId]) {
                  res.trailing[longId][shortId] = newTrailValue;
                }
                if (res.spreadIn < res.trailing[longId][shortId]) {
                  res.idExchLong = longId;
                  res.idExchShort = shortId;
                  res.feesLong = btcLong->getFees();
                  res.feesShort = btcShort->getFees();
                  res.exchNameLong = btcLong->getExchName();
                  res.exchNameShort = btcShort->getExchName();
                  res.priceLongIn = priceLong;
                  res.priceShortIn = priceShort;
                  return true;
                }
              }
            } else {
              res.trailing[longId][shortId] = -1.0;
            }
          }
        }
      }
    }
  }
  return false;
}