Center Of Gravity Strategy

The Center Of Gravity Strategy is based on the Center Of Gravity 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 definition is further expressed in the code given in the calculation below.
See Center Of Gravity

Center Of Gravity Strategy

How To Trade using the automatic Centre Of Gravity Strategy

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

How To Access in MotiveWave

Go to the top menu, choose Strategies>General>Center Of Gravity Strategy

or go to the top menu, choose Add Study, start typing in this study name until you see it appear in the list, click on the study name, click OK.

Important Disclaimer: The information provided on this page is strictly for informational purposes and is not to be construed as advice or solicitation to buy or sell any security. Please see our Risk Disclosure and Performance Disclaimer Statement.

Calculation

  public void onActivate(OrderContext ctx)
    if (getSettings().isEnterOnActivate())
      DataSeries series = ctx.getDataContext().getDataSeries();
      int ind = series.isLastBarComplete() ? series.size()-1 : series.size()-2;
      Boolean buy = series.getBoolean(ind, Signals.BUY);
      Boolean sell = series.getBoolean(ind, Signals.SELL);
      if (buy == null OR sell == null) return;

      int tradeLots = getSettings().getTradeLots();
      int qty = tradeLots * ctx.getInstrument().getDefaultQuantity();

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

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

    switch(getSettings().getPositionType())
    case LONG: //Only Long Positions are allowed.
      if (position == 0 AND signal == Signals.BUY)
        ctx.buy(qty); //Open Long Position
      end
      if (position moreThan 0 AND signal == Signals.SELL)
        ctx.sell(qty); //Close Long Position
      end
      break;
    case SHORT: //Only Short Positions are allowed.
      if (position == 0 AND signal == Signals.SELL)
        ctx.sell(qty); //Open Short Position
      end
      if (position lessThan 0 AND signal == Signals.BUY)
        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
      //allow for 2 successive transactions 
      if (position lessOrEqual 0 AND signal == Signals.BUY)
        ctx.buy(qty); //Open Long Position
      end
      if (position moreOrEqual 0 AND signal == Signals.SELL)
        ctx.sell(qty); //Open Short Position
      end
    end
  endMethod