MA Cross Strategy

The MA Cross Strategy is based on the Moving Average Cross study. Signals generated in the study are used to trigger automatic trades. This automated trading strategy was created to demonstrate the mechanics of an automatic trade and is not intended for actual use. A more comprehensive strategy that may include multiple studies, margins and stops could be developed. This strategy’s definition is further expressed in the code given in the calculation below.

See Moving Average Cross

How To Trade Using MA Cross Strategy

Examine the details of the Moving Average Cross study (see link above). Use the strategy optimiser and back testing to aid in the selection of period lengths. Open the strategy and configure the inputs for General, Display, Trading Options, Panel and Signals. Activate the strategy.

Calculation

  public void onActivate(OrderContext ctx)
    if (getSettings().isEnterOnActivate())
      DataSeries series = ctx.getDataContext().getDataSeries();
      int ind = series.isLastBarComplete() ? series.size()-1 : series.size()-2;
      Double fastMA = series.getDouble(ind, Values.FAST_MA);
      Double slowMA = series.getDouble(ind, Values.SLOW_MA);
      if (fastMA == null OR slowMA == null) return;
      int tradeLots = getSettings().getTradeLots();
      int qty = tradeLots *= ctx.getInstrument().getDefaultQuantity();

      switch(getSettings().getPositionType())
      case LONG: //Only Long Positions are allowed.
        if (fastMA moreThan slowMA) ctx.buy(qty);
        break;
      case SHORT: //Only Short Positions are allowed.
        if (fastMA lessThan slowMA) ctx.sell(qty);
        break;
      default: //Both Long and Short Positions Allowed
        if (fastMA moreThan slowMA) ctx.buy(qty);
        else ctx.sell(qty);
      end
    end
  endMethod

  @Override
  public void onSignal(OrderContext ctx, Object signal)
    Instrument instr = ctx.getInstrument();
    int position = ctx.getPosition();
    int qty = (getSettings().getTradeLots() * instr.getDefaultQuantity());

    switch(getSettings().getPositionType())
    case LONG: //Only Long Positions are allowed.
      if (position == 0 AND signal == Signals.CROSS_ABOVE)
        ctx.buy(qty); //Open Long Position
      end
      if (position lessThan 0 AND signal == Signals.CROSS_BELOW)
        ctx.sell(qty); //Close Long Position
      end
      break;
    case SHORT: //Only Short Positions are allowed.
      if (position == 0 AND signal == Signals.CROSS_BELOW)
        ctx.sell(qty); //Open Short Position
      end
      if (position lessThan 0 AND signal == Signals.CROSS_ABOVE)
        ctx.buy(qty); //Close Short Position
      end
      break;
    default: //Both Long and Short Positions Allowed
      qty += Math.abs(position); //Stop and Reverse if there is an open position
      if (position lessOrEqual 0 AND signal == Signals.CROSS_ABOVE)
        ctx.buy(qty); //Open Long Position
      end
      if (position moreOrEqual 0 AND signal == Signals.CROSS_BELOW)
        ctx.sell(qty); //Open Short Position
      end
    end
  end

Last updated