// TODO - make possible to parameterize the Trend conditions Lips/Jaw and
  // Gator
  private boolean isAlligatorCoherent(List<double[]> alligators, Period basePeriod) {
    boolean result = false;

    console.getOut().println("are coherent alis  " + alligators.size());

    List<double[]> miniGators = new LinkedList<double[]>();
    for (double[] ali : alligators) {
      console.getOut().println("are coherent alis  " + alligators.size());
      miniGators.add(new double[] {Math.signum(ali[0] - ali[1]), Math.signum(ali[1] - ali[2])});
    }
    // if theres a different momentum in any timeframe
    double[] current = miniGators.get(0);
    ListIterator<double[]> gat = miniGators.subList(1, miniGators.size()).listIterator();
    while (gat.hasNext()) {
      double[] gator = gat.next();
      if (gator[0] == current[0] || gator[1] == current[1]) {
        current = gator;
        result = true;
      } else {
        result = false;
        break;
      }
    }

    String isit = result ? "COHERENT" : "UNCOHERENT";
    console.getOut().println("alligator is " + isit);

    return result;
  }
  private HashMap<String, double[]> findMTMShorterAlligators(
      int width, Period basePeriod, int shift) throws JFException {
    HashMap<String, double[]> result = new HashMap<String, double[]>();

    int baseIndex = this.periods.indexOf(basePeriod);
    Period shortPeriod = this.periods.get(baseIndex + width);
    console.getOut().println("Short period " + shortPeriod.toString());

    IBar shortBar = history.getBar(instrument, shortPeriod, OfferSide.BID, shift);

    /*
     * not necessarily quickest double[][] longAlligator =
     * indicators.alligator(instrument, longPeriod, OfferSide.BID,
     * AppliedPrice.MEDIAN_PRICE, jaw, teeth, lips, longBar.getTime(),
     * longBar.getTime());
     *
     * double[][] shortAlligator = indicators.alligator(instrument,
     * shortPeriod, OfferSide.BID, AppliedPrice.MEDIAN_PRICE, jaw, teeth,
     * lips, shortBar.getTime(), shortBar.getTime());
     */

    double[] shortAlligator =
        indicators.alligator(
            instrument,
            shortPeriod,
            OfferSide.BID,
            AppliedPrice.MEDIAN_PRICE,
            jaw,
            teeth,
            lips,
            shift);

    double[] alligator =
        indicators.alligator(
            instrument,
            alligatorTimeframe,
            OfferSide.BID,
            AppliedPrice.MEDIAN_PRICE,
            jaw,
            teeth,
            lips,
            shift);

    result.put(shortPeriod.name(), shortAlligator);
    result.put(alligatorTimeframe.name(), alligator);

    console.getOut().println("result " + result.size());
    return result;
  }
 private void makeDough() throws JFException {
   console.getOut().println("rucham pape");
   List<IOrder> orders = engine.getOrders();
   if (orders.isEmpty()) {
     console.getOut().println("attempt to open");
     feedTheAlligator(alligatorShift);
   } else {
     for (IOrder order : orders) {
       console.getOut().println("attempt to close...");
       if (putTheAlligatorToSleep(order, alligatorShift) != null) {
         console.getOut().println("...and open");
         feedTheAlligator(alligatorShift);
       }
     }
   }
 }
  // TODO - make possible to parameterize the Trend conditions Lips/Jaw and
  // Gator
  public Trend askTheAlligator(double[][] alligator, IBar alligatorBar, int shift)
      throws JFException {
    Trend result = Trend.UNDEFINED;

    double alligatorJaw = alligator[0][0];
    double alligatorTeeth = alligator[1][0];
    double alligatorLips = alligator[2][0];

    double[] gator =
        calculateDirtyGator(new double[] {alligatorJaw, alligatorTeeth, alligatorLips});

    if (alligatorLips < alligatorTeeth) {
      console.getOut().println("alligator says trend is bearish");
      result = Trend.BEARISH;
    } else if (alligatorLips > alligatorTeeth) {
      console.getOut().println("alligator says trend is bullish");
      result = Trend.BULLISH;
    }

    return result;
  }
  private HashMap<String, double[]> findMTMLongerAlligators(int width, Period basePeriod, int shift)
      throws JFException {
    HashMap<String, double[]> result = new HashMap<String, double[]>();

    int baseIndex = this.periods.indexOf(basePeriod);
    Period longPeriod = this.periods.get(baseIndex - width);
    console.getOut().println("Long period " + longPeriod.toString());

    IBar longBar = history.getBar(instrument, longPeriod, OfferSide.BID, shift);

    double[] longAlligator =
        indicators.alligator(
            instrument,
            longPeriod,
            OfferSide.BID,
            AppliedPrice.MEDIAN_PRICE,
            jaw,
            teeth,
            lips,
            shift);

    double[] alligator =
        indicators.alligator(
            instrument,
            alligatorTimeframe,
            OfferSide.BID,
            AppliedPrice.MEDIAN_PRICE,
            jaw,
            teeth,
            lips,
            shift);

    result.put(alligatorTimeframe.name(), alligator);
    result.put(longPeriod.name(), longAlligator);

    return result;
  }
  private IOrder feedTheAlligator(int shift) throws JFException {

    IOrder result = null;
    OrderCommand feed = null;
    double stopLossPrice, takeProfitPrice;

    // dirty
    // get alligator from previous bar and the alligator from alligatorWidth
    // bars back
    // to check for alligator reversal
    IBar prevBar = history.getBar(instrument, alligatorTimeframe, OfferSide.BID, shift);
    // dirty

    double[][] alligator =
        indicators.alligator(
            instrument,
            alligatorTimeframe,
            OfferSide.BID,
            AppliedPrice.MEDIAN_PRICE,
            jaw,
            teeth,
            lips,
            prevBar.getTime(),
            prevBar.getTime());

    double alligatorJaw = alligator[0][0];
    double alligatorTeeth = alligator[1][0];
    double alligatorLips = alligator[2][0];

    Date date = new Date(prevBar.getTime());
    console
        .getOut()
        .println(
            "On "
                + date
                + " Alligator says - Jaw: "
                + alligatorJaw
                + " Teeth: "
                + alligatorTeeth
                + " Lips: "
                + alligatorLips);

    Trend alligatorSays = askTheAlligator(alligator, prevBar, 1);
    HashMap<String, double[]> mtmAlligators =
        findMTMShorterAlligators(timeframeWidth, alligatorTimeframe, shift);

    boolean isAliCoherent =
        isAlligatorCoherent(new ArrayList(mtmAlligators.values()), alligatorTimeframe);

    if (alligatorSays == Trend.BEARISH && isAliCoherent) feed = OrderCommand.SELL;
    else if (alligatorSays == Trend.BULLISH && isAliCoherent) feed = OrderCommand.BUY;

    if (feed != null) {
      if (feed == OrderCommand.BUY) {
        stopLossPrice =
            history.getLastTick(this.instrument).getAsk()
                - this.stopLossPips * this.instrument.getPipValue();
        takeProfitPrice =
            history.getLastTick(this.instrument).getAsk()
                + takeProfitPips * this.instrument.getPipValue();
      } else {
        stopLossPrice =
            history.getLastTick(this.instrument).getBid()
                + this.stopLossPips * this.instrument.getPipValue();
        takeProfitPrice =
            history.getLastTick(this.instrument).getBid()
                - takeProfitPips * this.instrument.getPipValue();
      }

      result =
          engine.submitOrder(
              getLabel(feed),
              this.instrument,
              feed,
              amount,
              0,
              20,
              stopLossPips != 0 ? stopLossPrice : stopLossPips,
              takeProfitPips != 0 ? takeProfitPrice : takeProfitPips);

      // engine.submitOrder("order", Instrument.EURUSD, feed, amount, 0,
      // 20, stopLossPrice, 0);
    }

    return result;
  }