Dukascopy Support Board
http://www.dukascopy.com/swiss/english/forex/jforex/forum/

I need Automatic stoploss and Take profit tool
http://www.dukascopy.com/swiss/english/forex/jforex/forum/viewtopic.php?f=77&t=56721
Page 1 of 1

Author:  martinezfx [ Wed 19 Sep, 2018, 19:11 ]
Post subject:  I need Automatic stoploss and Take profit tool

Please i want to be able to have my set stoploss and take take profit insert automatically when i place new oder. i don't need to be adjusting my trades manually to insert my TP and SL co's it takes time. Who can help and Thank you

Author:  JP7 [ Fri 21 Sep, 2018, 00:09 ]
Post subject:  Re: I need Automatic stoploss and Take profit tool

There you GO, I made this little strategy for you!


import com.dukascopy.api.Configurable;
import com.dukascopy.api.IAccount;
import com.dukascopy.api.IBar;
import com.dukascopy.api.IConsole;
import com.dukascopy.api.IContext;
import com.dukascopy.api.IEngine;
import com.dukascopy.api.IEngine.OrderCommand;
import com.dukascopy.api.IHistory;
import com.dukascopy.api.IMessage;
import com.dukascopy.api.IOrder;
import com.dukascopy.api.IStrategy;
import com.dukascopy.api.ITick;
import com.dukascopy.api.Instrument;
import com.dukascopy.api.JFException;
import com.dukascopy.api.Period;

public class Order_BS_STAT_X1 implements IStrategy {


    private IEngine engine;
    private IConsole console;
    private int counter = 0;
    private IHistory history;
    private String string = "";

    @Configurable("Instrument")
    public Instrument instrument = Instrument.EURUSD;

    @Configurable("Slippage")
    public double slippage = 2;
   
    @Configurable("Amount")
    public double amount = 0.1;

    @Configurable("Take Profit Pips")
    public int takeProfitPips = 20;

    @Configurable("Stop Loss Pips")
    public int slPips = 20;

    @Configurable("Start Buing")
    public boolean startBuing = true;

    @Configurable("Start Buing")
    public boolean startSelling = false;

    @Configurable("Nr of Orders B/S")
    public int ordNR= 1;

    @Override
    public void onStart(IContext context) throws JFException {
        this.console = context.getConsole();
        this.engine = context.getEngine();
        this.setHistory(context.getHistory());


        if(startSelling){
        if (tradesTotalSELL(instrument) < ordNR) {
            string = "SELL@Market"+counter;
            submitOrder(OrderCommand.SELL, instrument);
            context.stop();
        }}
        if(startBuing){
        if (tradesTotalBUY(instrument) < ordNR) {
            string = "BUY@Market"+counter;
            submitOrder(OrderCommand.BUY, instrument);
            context.stop();
        }}

   
    }

    @Override
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
        if (instrument != this.instrument) {
            return;
        }
    }

    public void onTick(Instrument instrument, ITick tick) throws JFException {
       
        if (instrument != this.instrument) {
            return;
        }
    }

    private double getPipPrice(double pips) {
        return pips * this.instrument.getPipValue();
    }

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

    public void onMessage(IMessage message) throws JFException {
        print(message);
    }

    public void onStop() throws JFException {
    }

    private void print(Object o) {
        console.getOut().println(o);
    }

    public IHistory getHistory() {
        return history;
    }

    public void setHistory(IHistory history) {
        this.history = history;
    }

    private IOrder submitOrder(OrderCommand orderCmd, Instrument instrument) throws JFException {
        double stopLossPrice = 0.0, takeProfitPrice = 0.0;
        if (orderCmd == OrderCommand.BUY) {
            if (slPips > 0) {
                stopLossPrice = history.getLastTick(instrument).getBid() - getPipPrice(slPips);
            }
            if (takeProfitPips > 0) {
                takeProfitPrice = history.getLastTick(instrument).getBid() + getPipPrice(takeProfitPips);
            }
        } else if (orderCmd == OrderCommand.SELL) {
            if (slPips > 0) {
                stopLossPrice = history.getLastTick(instrument).getBid() + getPipPrice(slPips);
            }
            if (takeProfitPips > 0) {
                takeProfitPrice = history.getLastTick(instrument).getBid() - getPipPrice(takeProfitPips);
            }
        }

        return engine.submitOrder(getLabel(instrument), instrument, orderCmd, amount, 0, slippage, stopLossPrice,takeProfitPrice,0,string);
    }
    public void onAccount(IAccount account) throws JFException {
    }


    public int tradesTotalBUY(Instrument INS) throws JFException {
        int counter1 = 0;
        for (IOrder ORD : engine.getOrders(INS)) {
            if (ORD.getState() == IOrder.State.FILLED && ORD.isLong()) {
                counter1++;
            }
        }
        return counter1;
    }

    public int tradesTotalSELL(Instrument INS) throws JFException {
        int counter2 = 0;
        for (IOrder ORD : engine.getOrders(INS)) {
            if (ORD.getState() == IOrder.State.FILLED && !ORD.isLong()) {
                counter2++;
            }
        }
        return counter2;
    }

}


Attachments:
Order_BS_STAT_X1.java [4.62 KiB]
Downloaded 196 times
Order_BS_STAT_X1.java [4.62 KiB]
Downloaded 239 times

Author:  martinezfx [ Fri 21 Sep, 2018, 07:19 ]
Post subject:  Re: I need Automatic stoploss and Take profit tool

Please thank you for your help and one more thing
please how do i insert the strategy on my jforex platform and make it active? thank you

Author:  JP7 [ Fri 21 Sep, 2018, 09:03 ]
Post subject:  Re: I need Automatic stoploss and Take profit tool

From the platform menu View Strategies.
From Strategies Open ... Locate the file ... Local Run thereafter
Use demo accounts to test strategies

Author:  martinezfx [ Fri 21 Sep, 2018, 18:10 ]
Post subject:  Re: I need Automatic stoploss and Take profit tool

okay, it's done and am testing it. Thank you

  Page 1 of 1