popopo wrote:
public void openChartAddIndicator(){
for(IChart openedChart : context.getCharts(feedDescriptor.getInstrument())){
IFeedDescriptor chartFeed = openedChart.getFeedDescriptor();
if(chartFeed.getPeriod() == feedDescriptor.getPeriod() && chartFeed.getOfferSide() == feedDescriptor.getOfferSide()){
chart = openedChart;
}
}
if(chart == null){
chart = context.openChart(feedDescriptor);
}
indicator = indicators.getIndicator(indicatorName);
chart.add(indicator, optionalInputs);
}
When running on local machine, you have
Chart object from jforex, while running on remote server there is no chart object, so context.getCharts(feedDescriptor.getInstrument()) will return null.
Quick n dirty fix
public void openChartAddIndicator(){
if ( context.getCharts(feedDescriptor.getInstrument()) == null ) return;
...
You might also want to move OnTick body to OnBar, for performance.
---
Hello Guys,
I think I have similar issue, but I cannot find out the source of problem.
Would you be so kind and look into the following codes, that where can be the problem: I cannot run Remote mode. Srtategy is only working on Local run.
ps.: I have requested slot for Remote mode already

Thanks in advance.
Code as follow:
import com.dukascopy.api.*;
import java.util.HashSet;
import java.util.Set;
import com.dukascopy.api.drawings.IChartObjectFactory;
import com.dukascopy.api.drawings.IOhlcChartObject;
import com.dukascopy.api.indicators.OutputParameterInfo.DrawingStyle;
import java.awt.Color;
import java.awt.Dimension;
import java.util.HashMap;
import java.util.Map;
import com.dukascopy.api.drawings.IChartDependentChartObject;
import com.dukascopy.api.drawings.ITriangleChartObject;
import com.dukascopy.api.drawings.ITextChartObject;
public class Vterv02 implements IStrategy {
private IEngine engine;
private IConsole console;
private IHistory history;
private IContext context;
private IIndicators indicators;
private IUserInterface userInterface;
private IBar previousBar;
private IOrder order;
private IChart openedChart;
private IChartObjectFactory factory;
private int signals;
private static final int PREV = 1;
private static final int SECOND_TO_LAST = 0;
private int uniqueOrderCounter = 1;
private SMATrend previousSMADirection = SMATrend.NOT_SET;
private SMATrend currentSMADirection = SMATrend.NOT_SET;
private Map<IOrder, Boolean> createdOrderMap = new HashMap<IOrder, Boolean>();
private int shorLineCounter;
private int textCounterOldSL;
private int textCounterNewSL;
@Configurable(value = "Instrument value")
public Instrument myInstrument = Instrument.EURUSD;
@Configurable(value = "Offer Side value", obligatory = true)
public OfferSide myOfferSide = OfferSide.BID;
@Configurable(value = "Period value")
public Period myPeriod = Period.ONE_HOUR;
@Configurable("SMA time period")
public int smaTimePeriod = 30;
@Configurable("Add OHLC Index to chart")
public boolean addOHLC = true;
@Configurable("Filter")
public Filter filter = Filter.WEEKENDS;
@Configurable("Draw SMA")
public boolean drawSMA = true;
@Configurable("Close chart on onStop")
public boolean closeChart;
@Configurable("Stop loss in pips")
public int stopLossPips = 400;
@Configurable("Take profit in pips")
public int takeProfitPips = 10;
@Configurable("Break event pips")
public double breakEventPips = 10;
@Configurable("Amount")
public double amount = 0.005;
private enum SMATrend {
UP, DOWN, NOT_SET;
}
public void onStart(IContext context) throws JFException {
this.engine = context.getEngine();
this.console = context.getConsole();
this.history = context.getHistory();
this.context = context;
this.indicators = context.getIndicators();
this.userInterface = context.getUserInterface();
this.openedChart = context.getChart(myInstrument);
this.factory = openedChart.getChartObjectFactory();
Set<Instrument> instruments = new HashSet<Instrument>();
instruments.add(myInstrument);
context.setSubscribedInstruments(instruments, true);
if (drawSMA) {
if (!addToChart(openedChart)) {
printMeError("Indicators did not get plotted on chart. Check the chart values!");
}}
}
public void onAccount(IAccount account) throws JFException {
}
public void onMessage(IMessage message) throws JFException {
if (message.getOrder() != null) {
printMe("order: " + message.getOrder().getLabel() + " || message content: " + message);
}
}
public void onStop() throws JFException {}
public void onTick(Instrument instrument, ITick tick) throws JFException {
if (instrument != myInstrument) {
return;
}
for (Map.Entry<IOrder, Boolean> entry : createdOrderMap.entrySet()) {
IOrder currentOrder = entry.getKey();
boolean currentValue = entry.getValue();
if (currentValue == false && currentOrder.getProfitLossInPips() >= breakEventPips) {
printMe("Order has profit of " + currentOrder.getProfitLossInPips() + " pips! Moving the stop loss to the open price.");
addBreakToChart(currentOrder, tick, currentOrder.getStopLossPrice(), currentOrder.getOpenPrice());
currentOrder.setStopLossPrice(currentOrder.getOpenPrice());
entry.setValue(true);
}
}
}
public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
if (!instrument.equals(myInstrument) || !period.equals(myPeriod)) {
return;
}
int candlesBefore = 2, candlesAfter = 0;
long completedBarTimeL = myOfferSide == OfferSide.ASK ? askBar.getTime() : bidBar.getTime();
double sma[] = indicators.sma(instrument, period, myOfferSide, IIndicators.AppliedPrice.CLOSE,
smaTimePeriod, Filter.NO_FILTER, candlesBefore, completedBarTimeL, candlesAfter);
printMe(String.format("Bar SMA Values: Second-to-last = %.5f; Last Completed = %.5f", sma[SECOND_TO_LAST], sma[PREV]));
IEngine.OrderCommand myCommand = null;
printMe(String.format("Bar SMA Values: Second-to-last = %.5f; Last Completed = %.5f", sma[SECOND_TO_LAST], sma[PREV]));
if (sma[PREV] > sma[SECOND_TO_LAST]) {
printMe("SMA in up-trend");
myCommand = IEngine.OrderCommand.BUY;
currentSMADirection = SMATrend.UP;
} else if (sma[PREV] < sma[SECOND_TO_LAST]) {
printMe("SMA in down-trend");
myCommand = IEngine.OrderCommand.SELL;
currentSMADirection = SMATrend.DOWN;
} else {
return;
}
double lastTickBid = history.getLastTick(myInstrument).getBid();
double lastTickAsk = history.getLastTick(myInstrument).getAsk();
double stopLossValueForLong = myInstrument.getPipValue() * stopLossPips;
double stopLossValueForShort = myInstrument.getPipValue() * takeProfitPips;
double stopLossPrice = myCommand.isLong() ? (lastTickBid - stopLossValueForLong) : (lastTickAsk + stopLossValueForLong);
double takeProfitPrice = myCommand.isLong() ? (lastTickBid + stopLossValueForShort) : (lastTickAsk - stopLossValueForShort);
if (currentSMADirection != previousSMADirection) {
previousSMADirection = currentSMADirection;
IOrder newOrder = engine.submitOrder("MyStrategyOrder" + uniqueOrderCounter++, instrument, myCommand, 0.005, 0, 1, stopLossPrice, takeProfitPrice);
createdOrderMap.put(newOrder, false);
if(openedChart == null){
return;
}
long time = bidBar.getTime() + myPeriod.getInterval();
double space = myInstrument.getPipValue() * 2;
IChartDependentChartObject signal = myCommand.isLong()
? factory.createSignalUp("signalUpKey" + signals++, time, bidBar.getLow() - space)
: factory.createSignalDown("signalDownKey" + signals++, time, bidBar.getHigh() + space);
signal.setStickToCandleTimeEnabled(false);
signal.setText("MyStrategyOrder" + (uniqueOrderCounter - 1));
openedChart.addToMainChart(signal);
}
}
private void printMe(Object toPrint) {
console.getOut().println(toPrint);
}
private void printMeError(Object o) {
console.getErr().println(o);
}
private boolean addToChart(IChart chart) {
if (!checkChart(chart)) {
return false;
}
chart.addIndicator(indicators.getIndicator("SMA"), new Object[]{smaTimePeriod},
new Color[]{Color.BLUE}, new DrawingStyle[]{DrawingStyle.LINE}, new int[]{3});
if (addOHLC) {
IOhlcChartObject ohlc = null;
for (IChartObject obj : chart.getAll()) {
if (obj instanceof IOhlcChartObject) {
ohlc = (IOhlcChartObject) obj;
}
}
if (ohlc == null) {
ohlc = chart.getChartObjectFactory().createOhlcInformer();
ohlc.setPreferredSize(new Dimension(100, 200));
chart.addToMainChart(ohlc);
}
ohlc.setShowIndicatorInfo(true);
}
return true;
}
private void addBreakToChart(IOrder changedOrder, ITick tick, double oldSL, double newSL) throws JFException {
}
private boolean checkChart(IChart chart) {
if (chart == null) {
printMeError("chart for " + myInstrument + " not opened!");
return false;
}
if (chart.getSelectedOfferSide() != this.myOfferSide) {
printMeError("chart offer side is not " + this.myOfferSide);
return false;
}
if (chart.getSelectedPeriod() != this.myPeriod) {
printMeError("chart period is not " + this.myPeriod);
return false;
}
if (chart.getFilter() != this.filter) {
printMeError("chart filter is not " + this.filter);
return false;
}
return true;
}
}