Dukascopy
 
 
Wiki JStore Search Login

I need Automatic stoploss and Take profit tool
 Post subject: I need Automatic stoploss and Take profit tool Post rating: 0   New post Posted: Wed 19 Sep, 2018, 19:11 

User rating: 1
Joined: Wed 19 Sep, 2018, 19:06
Posts: 3
Location: Nigeria, Uyo
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


 
 Post subject: Re: I need Automatic stoploss and Take profit tool Post rating: 0   New post Posted: Fri 21 Sep, 2018, 00:09 
User avatar

User rating: 13
Joined: Mon 27 Jul, 2015, 16:30
Posts: 110
Location: Canada, Mission
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 192 times
Order_BS_STAT_X1.java [4.62 KiB]
Downloaded 235 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: I need Automatic stoploss and Take profit tool Post rating: 0   New post Posted: Fri 21 Sep, 2018, 07:19 

User rating: 1
Joined: Wed 19 Sep, 2018, 19:06
Posts: 3
Location: Nigeria, Uyo
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


 
 Post subject: Re: I need Automatic stoploss and Take profit tool Post rating: 0   New post Posted: Fri 21 Sep, 2018, 09:03 
User avatar

User rating: 13
Joined: Mon 27 Jul, 2015, 16:30
Posts: 110
Location: Canada, Mission
From the platform menu View Strategies.
From Strategies Open ... Locate the file ... Local Run thereafter
Use demo accounts to test strategies


 
 Post subject: Re: I need Automatic stoploss and Take profit tool Post rating: 1   New post Posted: Fri 21 Sep, 2018, 18:10 

User rating: 1
Joined: Wed 19 Sep, 2018, 19:06
Posts: 3
Location: Nigeria, Uyo
okay, it's done and am testing it. Thank you


 

Jump to:  

cron
  © 1998-2024 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