Hello,
I have cleaned the code shared yesterday.
Anyone please has an idea as to why this code doesn't take automatic trades while the "
autoEntry=true,"?
The piece of code which seems to pose problem is the following:
//@Override
public void onBarUpdate(int index, OrderContext ctx)
{
Instrument instr = ctx.getInstrument();
var series = ctx.getDataContext().getDataSeries();
int ema1Period = getSettings().getInteger(Inputs.PERIODMAF);
int ema2Period = getSettings().getInteger(Inputs.PERIODMAM);
if (series.size() < Math.max(ema1Period, ema2Period)) {
// Not enough data to calculate EMAs
return;
}
Double ema1 = series.ma(getSettings().getMAMethod(Inputs.METHODMAF), series.size() - 1, ema1Period, getSettings().getInput(Inputs.INPUTMAF));
Double ema2 = series.ma(getSettings().getMAMethod(Inputs.METHODMAM), series.size() - 1, ema2Period, getSettings().getInput(Inputs.INPUTMAM));
if (ema1 == null || ema2 == null) {
// One or both EMAs couldn't be calculated
return;
}
if (crossedAbove(series, series.size() - 1, ema1, ema2)) {
// EMA crossover condition met, place a market order for entry
Order entryOrder = createMarketEntry(ctx, true, getSettings().getInteger(TRADE_LOTS));
ctx.submitOrders(entryOrder);
} else if (crossedBelow(series, series.size() - 1, ema1, ema2)) {
// EMA crossover condition met for the opposite direction, place a market order for entry
Order entryOrder = createMarketEntry(ctx, false, getSettings().getInteger(TRADE_LOTS));
ctx.submitOrders(entryOrder);
}
/////////
///
//////
//System.out.println("onBarUpdate(): " + com.motivewave.common.Util.formatDateMMMDDHHSS(ServiceInstance.getCurrentTime()));
if (getEntryState() != Enums.EntryState.OPEN) return;
// Check to see if we have hit the break even state, if so adjust the trail orders.
// Instrument instr = ctx.getInstrument();
boolean pips = Util.compare(getSettings().getString(STEP, PIPS), PIPS);
float step = (float)(pips ? instr.getPointSize() : instr.getTickSize());
float breakEven = getSettings().getInteger(BREAK_EVEN)*step;
if (isLong()) {
if (instr.round(instr.getSellPrice() - ctx.getAvgEntryPrice()) >= instr.round(breakEven)) {
doBreakEven(ctx);
}
}
else {
if (instr.round(ctx.getAvgEntryPrice() - instr.getBuyPrice()) >= instr.round(breakEven)) {
doBreakEven(ctx);
}
}
}
Anyone can help PLEASE.
Thanks in advance.
Khaled
I have cleaned the code shared yesterday.
Anyone please has an idea as to why this code doesn't take automatic trades while the "
autoEntry=true,"?
The piece of code which seems to pose problem is the following:
//@Override
public void onBarUpdate(int index, OrderContext ctx)
{
Instrument instr = ctx.getInstrument();
var series = ctx.getDataContext().getDataSeries();
int ema1Period = getSettings().getInteger(Inputs.PERIODMAF);
int ema2Period = getSettings().getInteger(Inputs.PERIODMAM);
if (series.size() < Math.max(ema1Period, ema2Period)) {
// Not enough data to calculate EMAs
return;
}
Double ema1 = series.ma(getSettings().getMAMethod(Inputs.METHODMAF), series.size() - 1, ema1Period, getSettings().getInput(Inputs.INPUTMAF));
Double ema2 = series.ma(getSettings().getMAMethod(Inputs.METHODMAM), series.size() - 1, ema2Period, getSettings().getInput(Inputs.INPUTMAM));
if (ema1 == null || ema2 == null) {
// One or both EMAs couldn't be calculated
return;
}
if (crossedAbove(series, series.size() - 1, ema1, ema2)) {
// EMA crossover condition met, place a market order for entry
Order entryOrder = createMarketEntry(ctx, true, getSettings().getInteger(TRADE_LOTS));
ctx.submitOrders(entryOrder);
} else if (crossedBelow(series, series.size() - 1, ema1, ema2)) {
// EMA crossover condition met for the opposite direction, place a market order for entry
Order entryOrder = createMarketEntry(ctx, false, getSettings().getInteger(TRADE_LOTS));
ctx.submitOrders(entryOrder);
}
/////////
///
//////
//System.out.println("onBarUpdate(): " + com.motivewave.common.Util.formatDateMMMDDHHSS(ServiceInstance.getCurrentTime()));
if (getEntryState() != Enums.EntryState.OPEN) return;
// Check to see if we have hit the break even state, if so adjust the trail orders.
// Instrument instr = ctx.getInstrument();
boolean pips = Util.compare(getSettings().getString(STEP, PIPS), PIPS);
float step = (float)(pips ? instr.getPointSize() : instr.getTickSize());
float breakEven = getSettings().getInteger(BREAK_EVEN)*step;
if (isLong()) {
if (instr.round(instr.getSellPrice() - ctx.getAvgEntryPrice()) >= instr.round(breakEven)) {
doBreakEven(ctx);
}
}
else {
if (instr.round(ctx.getAvgEntryPrice() - instr.getBuyPrice()) >= instr.round(breakEven)) {
doBreakEven(ctx);
}
}
}
Anyone can help PLEASE.
Thanks in advance.
Khaled
Attachments
Last edited: