Indicator zero line?
-
- Posts: 24
- Joined: Wed May 26, 2021 12:13 pm
- Has thanked: 2 times
Indicator zero line?
Hi, is there a way to show the zero line in a Simplified API Indicator?
Thanks,
Thanks,
-
- Posts: 545
- Joined: Mon Jul 09, 2018 11:18 am
- Has thanked: 22 times
- Been thanked: 83 times
-
- Posts: 24
- Joined: Wed May 26, 2021 12:13 pm
- Has thanked: 2 times
Re: Indicator zero line?
On the bottom chart of Bookmap... Can I make an indicator that shows or highlights the 0 line while also showing its own values?
-
- Posts: 545
- Joined: Mon Jul 09, 2018 11:18 am
- Has thanked: 22 times
- Been thanked: 83 times
Re: Indicator zero line?
Here is an example. There is a zero indicator and a last trade indicator. To show indicators on the same axis (otherwise there would be 2 axes - one for each indicator) an axis group is used so indicators are grouped to use 1 axis.
!!! If the purpose of zero line is just to have zero on the bottom chart you can use 1 indicator but set axis rules - see the second example.
One indicator + axis rules
!!! If the purpose of zero line is just to have zero on the bottom chart you can use 1 indicator but set axis rules - see the second example.
Code: Select all
package velox.api.layer1.simplified.demo;
import java.awt.Color;
import velox.api.layer1.annotations.Layer1ApiVersion;
import velox.api.layer1.annotations.Layer1ApiVersionValue;
import velox.api.layer1.annotations.Layer1SimpleAttachable;
import velox.api.layer1.annotations.Layer1StrategyName;
import velox.api.layer1.data.InstrumentInfo;
import velox.api.layer1.data.TradeInfo;
import velox.api.layer1.messages.indicators.Layer1ApiUserMessageModifyIndicator.GraphType;
import velox.api.layer1.simplified.Api;
import velox.api.layer1.simplified.AxisGroup;
import velox.api.layer1.simplified.CustomModule;
import velox.api.layer1.simplified.Indicator;
import velox.api.layer1.simplified.InitialState;
import velox.api.layer1.simplified.TradeDataListener;
@Layer1SimpleAttachable
@Layer1StrategyName("LastTradeBottomZero")
@Layer1ApiVersion(Layer1ApiVersionValue.VERSION2)
public class LastTradeBottomZero implements CustomModule, TradeDataListener
{
protected Indicator lastTradeIndicator;
protected Indicator zeroIndicator;
private AxisGroup group = new AxisGroup();
InstrumentInfo info;
@Override
public void initialize(String alias, InstrumentInfo info, Api api, InitialState initialState) {
lastTradeIndicator = api.registerIndicator("LastTradeBottomZero", GraphType.BOTTOM);
lastTradeIndicator.setColor(Color.GREEN);
zeroIndicator = api.registerIndicator("Zero", GraphType.BOTTOM, 0);
zeroIndicator.setColor(Color.RED);
group.add(lastTradeIndicator);
group.add(zeroIndicator);
this.info = info;
}
@Override
public void stop() {
}
@Override
public void onTrade(double price, int size, TradeInfo tradeInfo) {
lastTradeIndicator.addPoint(price * info.pips);
}
}
Code: Select all
public class LastTradeBottomZeroAxisRules implements CustomModule, TradeDataListener
{
protected Indicator lastTradeIndicator;
private AxisGroup group = new AxisGroup();
InstrumentInfo info;
@Override
public void initialize(String alias, InstrumentInfo info, Api api, InitialState initialState) {
lastTradeIndicator = api.registerIndicator("LastTradeBottomZeroAxisRules", GraphType.BOTTOM);
lastTradeIndicator.setColor(Color.GREEN);
group.add(lastTradeIndicator);
AxisRules rules = new AxisRules();
rules.setForcedMin(0);
group.setAxisRules(rules);
this.info = info;
}
@Override
public void stop() {
}
@Override
public void onTrade(double price, int size, TradeInfo tradeInfo) {
lastTradeIndicator.addPoint(price * info.pips);
}
}
-
- Posts: 545
- Joined: Mon Jul 09, 2018 11:18 am
- Has thanked: 22 times
- Been thanked: 83 times
Re: Indicator zero line?
If you wish one indicator to alternate between zero and non-zero values the visible result will depend on how often the values change from zero to non-zero. Due to data aggregation some values may be missed.
In this example (a bad example of alternating) non-zero values exist for a tiny fraction of second. The aggregation will simply hide non-zero values.
To make the example look meaningful you may want to use delays for plotting zero values so non-zero values stay on chart for some time
In this example (a bad example of alternating) non-zero values exist for a tiny fraction of second. The aggregation will simply hide non-zero values.
Code: Select all
protected Indicator lastTradeIndicator;
private AxisGroup group = new AxisGroup();
InstrumentInfo info;
@Override
public void initialize(String alias, InstrumentInfo info, Api api, InitialState initialState) {
lastTradeIndicator = api.registerIndicator("LastTradeBottomZeroAxisRulesAlternate", GraphType.BOTTOM);
lastTradeIndicator.setColor(Color.GREEN);
group.add(lastTradeIndicator);
AxisRules rules = new AxisRules();
rules.setForcedMin(0);
group.setAxisRules(rules);
this.info = info;
}
@Override
public void stop() {
}
@Override
public void onTrade(double price, int size, TradeInfo tradeInfo) {
lastTradeIndicator.addPoint(price * info.pips);
lastTradeIndicator.addPoint(0);
}
Code: Select all
@Layer1SimpleAttachable
@Layer1StrategyName("LastTradeBottomZeroAxisRulesAlternate")
@Layer1ApiVersion(Layer1ApiVersionValue.VERSION2)
public class LastTradeBottomZeroAxisRulesAlternate implements CustomModule, TradeDataListener, TimeListener
{
protected Indicator lastTradeIndicator;
private AxisGroup group = new AxisGroup();
InstrumentInfo info;
ExecutorService executor = Executors.newSingleThreadExecutor();
long t;
long lastTradeT;
@Override
public void initialize(String alias, InstrumentInfo info, Api api, InitialState initialState) {
lastTradeIndicator = api.registerIndicator("LastTradeBottomZeroAxisRulesAlternate", GraphType.BOTTOM);
lastTradeIndicator.setColor(Color.GREEN);
group.add(lastTradeIndicator);
AxisRules rules = new AxisRules();
rules.setForcedMin(0);
group.setAxisRules(rules);
this.info = info;
}
@Override
public void stop() {
}
@Override
public void onTrade(double price, int size, TradeInfo tradeInfo) {
executor.execute(() -> {
lastTradeT = t;
lastTradeIndicator.addPoint(price * info.pips);
});
Thread th = new Thread(() -> {
long storedT = lastTradeT;
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
Log.info("", e);
}
if (lastTradeT == storedT) {//if no onTrade events came during the recent second
executor.execute(() -> {
lastTradeIndicator.addPoint(0);
});
}
});
th.start();
}
@Override
public void onTimestamp(long t) {
this.t = t;
}
}
-
- Posts: 24
- Joined: Wed May 26, 2021 12:13 pm
- Has thanked: 2 times