Dukascopy
 
 
Wiki JStore Search Login

Attention! Read the forum rules carefully before posting a topic.

    Try to find an answer in Wiki before asking a question.
    Submit programming questions in this forum only.
    Off topics are strictly forbidden.

Any topics which do not satisfy these rules will be deleted.

how to add a condition of opening the closure of the
 Post subject: how to add a condition of opening the closure of the Post rating: 0   New post Posted: Fri 24 Apr, 2015, 12:12 

User rating: 0
Joined: Thu 24 May, 2012, 20:49
Posts: 4
I have a strategy based on the average SMA, buying on average. How to add a condition of purchase: the opening of the candles in the average and closing above the average?


 
 Post subject: Re: how to add a condition of opening the closure of the Post rating: 0   New post Posted: Mon 27 Apr, 2015, 08:43 

User rating: 0
Joined: Thu 24 May, 2012, 20:49
Posts: 4
Maybe I'll ask for help again, but I will show you the problem. SMAStrategy.java strategy is. I want to add a condition to using buy: candle opening under sma10 and closing the sma10. If you sell it or vice versa. Who can help?

package dukascopy.strategies.indicators;



import com.dukascopy.api.*;
import com.dukascopy.api.IEngine.OrderCommand;
import com.dukascopy.api.IIndicators.AppliedPrice;

public class SMAStrategy implements IStrategy {
    private IEngine engine;
    private IConsole console;
    private IHistory history;
    private IIndicators indicators;
    private int counter =0;
    private double [] filteredSma90;
    private double [] filteredSma10;
    private IOrder order = null;

    @Configurable("Instrument")
    public Instrument selectedInstrument = Instrument.EURUSD;
    @Configurable("Period")
    public Period selectedPeriod = Period.THIRTY_MINS;
    @Configurable("SMA filter")
    public Filter indicatorFilter = Filter.NO_FILTER;
   

    public void onStart(IContext context) throws JFException {
        this.engine = context.getEngine();
        this.console = context.getConsole();
        this.history = context.getHistory();
        this.indicators = context.getIndicators();
    }

    public void onAccount(IAccount account) throws JFException {
    }

    public void onMessage(IMessage message) throws JFException {
    }

    public void onStop() throws JFException {
        for (IOrder order : engine.getOrders()) {
            engine.getOrder(order.getLabel()).close();
        }
    }

    public void onTick(Instrument instrument, ITick tick) throws JFException {
        if (!instrument.equals(selectedInstrument)) {
            return;
        }
        IBar prevBar = history.getBar(instrument, selectedPeriod, OfferSide.BID, 1);
        filteredSma90 = indicators.sma(instrument, selectedPeriod, OfferSide.BID, AppliedPrice.CLOSE, 90,
                indicatorFilter, 2, prevBar.getTime(), 0);
        filteredSma10 = indicators.sma(instrument, selectedPeriod, OfferSide.BID, AppliedPrice.CLOSE, 10,
                indicatorFilter, 2, prevBar.getTime(), 0);

        // SMA10 crossover SMA90 from UP to DOWN
        if ((filteredSma10[1] < filteredSma10[0]) && (filteredSma10[1] < filteredSma90[1]) && (filteredSma10[0] >= filteredSma90[0])) {
            if (engine.getOrders().size() > 0) {
                for (IOrder orderInMarket : engine.getOrders()) {
                    if (orderInMarket.isLong()) {
                        print("Closing Long position");
                        orderInMarket.close();
                    }
                }
            }
            if ((order == null) || (order.isLong() && order.getState().equals(IOrder.State.CLOSED)) ) {
                print("Create SELL");
                order = engine.submitOrder(getLabel(instrument), instrument, OrderCommand.SELL, 0.01);
            }
        }
        // SMA10 crossover SMA90 from DOWN to UP
        if ((filteredSma10[1] > filteredSma10[0]) && (filteredSma10[1] > filteredSma90[1]) && (filteredSma10[0] <= filteredSma90[0])) {
            if (engine.getOrders().size() > 0) {
                for (IOrder orderInMarket : engine.getOrders()) {
                    if (!orderInMarket.isLong()) {
                        print("Closing Short position");
                        orderInMarket.close();
                    }
                }
            }
            if ((order == null) || (!order.isLong() && order.getState().equals(IOrder.State.CLOSED)) ) {
                order = engine.submitOrder(getLabel(instrument), instrument, OrderCommand.BUY, 0.01);
            }
        }
    }   

    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
    }

    protected String getLabel(Instrument instrument) {
        String label = instrument.name();
        label = label + (counter++);
        label = label.toUpperCase();
        return label;
    }

    public void print(String message) {
        console.getOut().println(message);
    }

}


Attachments:
SMAStrategy.java [3.19 KiB]
Downloaded 54 times
DISCLAIMER: Dukascopy Bank SA's waiver of responsability - Documents, data or information available on this webpage may be posted by third parties without Dukascopy Bank SA being obliged to make any control on their content. Anyone accessing this webpage and downloading or otherwise making use of any document, data or information found on this webpage shall do it on his/her own risks without any recourse against Dukascopy Bank SA in relation thereto or for any consequences arising to him/her or any third party from the use and/or reliance on any document, data or information found on this webpage.
 
 Post subject: Re: how to add a condition of opening the closure of the Post rating: 0   New post Posted: Fri 08 May, 2015, 08:39 
User avatar

User rating: 164
Joined: Mon 08 Oct, 2012, 10:35
Posts: 676
Location: NetherlandsNetherlands
Hi,

You can try something like this in your onTick() function:
public void onTick(Instrument instrument, ITick tick) throws JFException {
        if (!instrument.equals(selectedInstrument)) {
            return;
        }
        IBar prevBar = history.getBar(instrument, selectedPeriod, OfferSide.BID, 1);
        IBar currBar  = history.getBar(instrument, selectedPeriod, OfferSide.BID, 0);
...
        if ( (currBar.getOpen() < filteredSma10)
        && ( (order == null) || (order.isLong() && order.getState().equals(IOrder.State.CLOSED)) )
           ) {
                print("Create SELL");
                order = engine.submitOrder(getLabel(instrument), instrument, OrderCommand.SELL, 0.01);
        }


 

Jump to:  

  © 1998-2025 Dukascopy® Bank SA
On-line Currency forex trading with Swiss Forex Broker - ECN Forex Brokerage,
Managed Forex Accounts, introducing forex brokers, Currency Forex Data Feed and News
Currency Forex Trading Platform provided on-line by Dukascopy.com