I've code simple program to test my writing of sell and buy orders.
The program to make that is very simple.
It sends a sell order when cci one minute is bigger than 0 (called "sellorder"),
and sends a buy order when the cci one is smaller than 0 (called "buyorder")
And the program closes the sell order if a buy order is opened,
and closes the buy order if a sell order is opened.
The program do it very well, but the problem is when an order "sell" or "buy" is taken, it creates automatically a buy order called "EURUSD0", "EURUSD1", "EURUSD3", etc..... each time cci passes through 0....
So, it duplicates the buy order when cci is bigger than 0, and creates a buy order in addition the sell order created by the EA.
DO you know how it's possible stop these additional buy orders.
Thanks for your help !!!
here's my code:
package jforex.feed.test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import com.dukascopy.api.*;
import com.dukascopy.api.IIndicators.AppliedPrice;
import com.dukascopy.api.feed.*;
import com.dukascopy.api.feed.util.*;
import com.dukascopy.api.indicators.IIndicator;
import com.dukascopy.api.IEngine.OrderCommand;
/**
* The following strategy demonstrates how one can bind an arbitrary set of indicators
* with corresponding feed descriptors, such that each indicator with its feed descriptor
* can be used both for indicator calculation and indicator plotting on the chart.
*
*/
public class StrategyBATI implements IStrategy {
private IConsole console;
private IIndicators indicators;
private IContext context;
private IEngine engine;
private IHistory history;
private IUserInterface userInterface;
@Configurable("")
public Instrument instrument = Instrument.EURUSD;
public int counter;
@Configurable("Period")
public Period selectedPeriod = Period.FIFTEEN_MINS;
@Configurable("Period")
public Period selectedPeriodh = Period.ONE_HOUR;
@Configurable("Period")
public Period selectedPeriodc = Period.FIVE_MINS;
@Configurable("Period")
public Period selectedPeriodm = Period.ONE_MIN;
@Configurable("SMA filter")
public Filter indicatorFilter = Filter.NO_FILTER;
@Configurable("Amount")
public double amount = 0.1;
@Configurable("Stop loss")
public int stopLossPips = 20;
@Configurable("Take profit")
public int takeProfitPips = 100;
@Configurable("")
public OfferSide side = OfferSide.BID;
public int w=0;
public double ccio;
class IndDataAndFeed{
private IFeedDescriptor feedDescriptor;
private String indicatorName;
private Object[] optionalInputs;
private int outputIndex;
private IIndicator indicator;
private IChart chart;
public IndDataAndFeed(String indicatorName, Object[] optionalInputs, int outputIndex, IFeedDescriptor feedDescriptor) {
this.feedDescriptor = feedDescriptor;
this.indicatorName = indicatorName;
this.optionalInputs = optionalInputs;
this.outputIndex = outputIndex;
}
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);
}
public double getCurrentValue() throws JFException{
Object[] outputs = indicators.calculateIndicator(feedDescriptor, new OfferSide[] { side },indicatorName,
new AppliedPrice[] { AppliedPrice.CLOSE }, optionalInputs, 0);
double value = (Double) outputs[outputIndex];
return value;
}
public void removeFromChart(){
if(chart != null && indicator != null){
chart.removeIndicator(indicator);
}
}
@Override
public String toString(){
return String.format("%s %s on %s feed", indicatorName, Arrays.toString(optionalInputs), feedDescriptor.getPeriod());
}
}
private List<IndDataAndFeed> calculatableIndicators = new ArrayList<IndDataAndFeed>(Arrays.asList(new IndDataAndFeed[]{
new IndDataAndFeed("MACD", new Object[] {12,26,9}, 0, new TimePeriodAggregationFeedDescriptor(instrument, Period.FIVE_MINS, side)),
new IndDataAndFeed("RSI", new Object[] {50}, 0, new TimePeriodAggregationFeedDescriptor(instrument, Period.FIVE_MINS, side)),
new IndDataAndFeed("RSI", new Object[] {50}, 0, new TimePeriodAggregationFeedDescriptor(instrument, Period.ONE_HOUR, side)),
new IndDataAndFeed("CCI", new Object[] {14}, 0, new TimePeriodAggregationFeedDescriptor(instrument, Period.FIFTEEN_MINS, side)),
new IndDataAndFeed("CCI", new Object[] {14}, 0, new TimePeriodAggregationFeedDescriptor(instrument, Period.ONE_HOUR, side)),
new IndDataAndFeed("CCI", new Object[] {14}, 0, new TimePeriodAggregationFeedDescriptor(instrument, Period.ONE_MIN, side))
}));
@Override
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();
if(!context.getSubscribedInstruments().contains(instrument)){
context.setSubscribedInstruments(new HashSet<Instrument>(Arrays.asList(new Instrument [] {instrument})), true);
}
this.context = context;
console = context.getConsole();
indicators = context.getIndicators();
for(IndDataAndFeed indDataAndFeed : calculatableIndicators){
indDataAndFeed.openChartAddIndicator();
}
}
@Override
public void onTick(Instrument instrument, ITick tick) throws JFException {
if (instrument != this.instrument) {
return;
}
int i=0;
for (IndDataAndFeed indDataAndFeed : calculatableIndicators) {
double value = indDataAndFeed.getCurrentValue();
// print("%s current value=%.5f", indDataAndFeed, value);
i=i+1;
if(i==6)
{print("%s current value pour le CCI M1=%.5f", indDataAndFeed, value);
ccio = value;}
}
// ORDERS
if (w==0 && ccio>0)
{engine.submitOrder("buyorder", instrument, OrderCommand.BUY, 0.1);
submitOrder(OrderCommand.BUY);
w=300;}
if (w==0 && ccio<0)
{engine.submitOrder("sellorder", instrument, OrderCommand.SELL, 0.1);
submitOrder(OrderCommand.BUY);
w=400;}
// CLOSING
if (w==300 && ccio<0)
{engine.getOrder("buyorder").close();
engine.getOrder("buyorder").waitForUpdate(IOrder.State.CLOSED); //wait till the order is closed};
w=0;
}
if (w==400 && ccio>0)
{engine.getOrder("sellorder").close();
engine.getOrder("sellorder").waitForUpdate(IOrder.State.CLOSED); //wait till the order is closed};
w=0;
}
}
@Override
public void onStop() throws JFException {
for(IndDataAndFeed indDataAndFeed : calculatableIndicators){
indDataAndFeed.removeFromChart();
}
//close all orders
for (IOrder order : engine.getOrders()) {
engine.getOrder(order.getLabel()).close();
}
}
private void print(Object o) {
console.getOut().println(o);
}
private void print(String format, Object... args) {
print(String.format(format, args));
}
private IOrder submitOrder(OrderCommand orderCmd) throws JFException {
return engine.submitOrder(getLabel(instrument), this.instrument, orderCmd, this.amount, 0, 5);
}
protected String getLabel(Instrument instrument) {
String label = instrument.name();
label = label + (counter++);
label = label.toUpperCase();
return label;
}
@Override
public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
}
@Override
public void onMessage(IMessage message) throws JFException {
}
@Override
public void onAccount(IAccount account) throws JFException {
}
}