Mass Index Strategy
How To Trade using the automatic Mass Index 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;
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
endMethodLast updated