Trying to calculate Delta in custom Study (Java)

Koliko

Member
Joined
Feb 5, 2025
Posts
6
Likes
2
Hello everyone,

Today I'm trying to calculate my Delta since the SDK doesn't allow it directly. I'm able to get close to reality, but I still have some discrepancies when there are a lot of trades. During calm phases, I manage to obtain the correct Delta, but sometimes when the market speeds up, my Delta is no longer accurate (even though it remains relatively consistent, I would like to get the exact value). Below, I'm sharing the code and a screenshot to illustrate this.

Thanks to anyone who can help me!


delta.png

@Override
public void onBarClose(DataContext ctx) {
getDelta(ctx);
}

private void convert(long time) {
long timestamp = time;

Date date = new Date(timestamp);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("Europe/Paris"));

debug("Date et heure correspondantes : " + sdf.format(date));
}

private void getDelta(DataContext ctx) {
Instrument instrument = ctx.getInstrument();
if (instrument == null) return;


long startTime = ctx.getDataSeries().getStartTime();
long endTime = ctx.getDataSeries().getEndTime();

List<Tick> ticks = instrument.getTicks(startTime, endTime);

int bidCount = 0;
int askCount = 0;

for (Tick tick : ticks) {
if (tick.isAskTick()) {
askCount++;
}
else{
bidCount++;
}
}

debug("Nombre de ticks Bid: " + bidCount);
debug("Nombre de ticks Ask: " + askCount);
debug("Delta : "+ (askCount-bidCount));
debug("Volume :"+ ctx.getDataSeries().getVolume());

convert(startTime);
convert(endTime);

debug("////////////////////////////////////////////////////////////////////////");
}
 
Delta is (contracts at ask) less (contracts at bid), not (trades at ask) less (trades at bid)
 
Yea should be askCount += tick.getVolume(); / bidCount += tick.getVolume(); since delta is based on volume at bid/ask not just ticks or trades which can vary in volume

(or .getVolumeAsDouble();)
 
Thank you both, here is my update :

List<Tick> ticks = instrument.getTicks(startTime, endTime);

if (ticks == null || ticks.isEmpty()) {
debug("⚠️ Aucun tick récupéré entre " + startTime + " et " + endTime);
return;
}

int bidCount = 0;
int askCount = 0;

long firstTickTime = Long.MAX_VALUE;
long lastTickTime = Long.MIN_VALUE;

// Parcourir les ticks et compter les Bid et Ask
for (Tick tick : ticks) {
int contract = tick.getVolume();
if (tick.isAskTick()) {
askCount+=contract;
} else {
bidCount+=contract;
}
// Suivi des timestamps des ticks pour vérifier s'ils couvrent bien la période
firstTickTime = Math.min(firstTickTime, tick.getTime());
lastTickTime = Math.max(lastTickTime, tick.getTime());
}

Working well, appreciate
 
Top