Need Help in my strategy !

Discussion in 'App Development' started by chomaxa, Nov 9, 2015.

  1. chomaxa

    chomaxa

    Hello everyone, I need little help from you, I downloaded simple strategy from dukasckopy forum I need only one advice, it opens new orders when it reaches stop loss, but opens new orders in the same price forex example: 0.004 I will write strategy code and can someone tell me how to add some orders to this strategy to let it open in different price and not same as past order ?
    Code:
    package jforex;
    
    import com.dukascopy.api.*;
    import com.dukascopy.api.IEngine.OrderCommand;
    import com.dukascopy.api.IMessage.Type;
    
    /**
    * The strategy maintains one order with TP and SL.
    * As soon as the order gets closed either a new order gets created:
    * - on close by TP the same direction order gets opened
    * - on close by SL the opposite direction order gets opened and with a different TP distance
    * - on close from outside we stop the strategy
    *
    * On strategy stop the strategy closes its order
    */
    public class SimpleTpSlStrategy implements IStrategy {
        // Configurable parameters
        @Configurable("Instrument")
        public Instrument instrument = Instrument.EURUSD;
        @Configurable("Amount")
        public double amount = 0.004;
        @Configurable("Stop loss")
        public int slPips = 100;
        @Configurable("Take profit on loss")
        public int tpPipsOnLoss = 100;
        @Configurable("Take profit on profit")
        public int tpPipsOnProfit = 150;
    
        private IEngine engine;
        private IHistory history;
        private IConsole console;
        private IContext context;
        private IOrder order;
    
        public void onStart(IContext context) throws JFException {
            this.engine = context.getEngine();
            this.history = context.getHistory();
            this.console = context.getConsole();
            this.context = context;
            // subscribe the instrument that we are going to work with
            context.setSubscribedInstruments(java.util.Collections.singleton(instrument));
            // Fetching previous daily bar from history
            IBar prevDailyBar = history.getBar(instrument, Period.DAILY, OfferSide.ASK, 1);
            // Identifying the side of the initial order
            OrderCommand orderCmd = prevDailyBar.getClose() > prevDailyBar.getOpen()
                    ? OrderCommand.BUY
                    : OrderCommand.SELL;
            // submitting the order with the specified amount, command and take profit
            submitOrder(amount, orderCmd, tpPipsOnLoss);
        }
    
        public void onAccount(IAccount account) throws JFException {
        }
    
        public void onMessage(IMessage message) throws JFException {
            if (message.getType() != Type.ORDER_CLOSE_OK
                    || !message.getOrder().equals(order) //only respond to our own order close
                ) {
                return;
            }
            console.getInfo().format("%s closed with P/L %.1f pips", order.getLabel(), order.getProfitLossInPips()).println();
            if (message.getReasons().contains(IMessage.Reason.ORDER_CLOSED_BY_TP)) {
                // on close by TP we keep the order direction
                submitOrder(amount, order.getOrderCommand(), tpPipsOnProfit);
            } else if (message.getReasons().contains(IMessage.Reason.ORDER_CLOSED_BY_SL)) {
                //  on close by SL we change the order direction and use other TP distance
                OrderCommand orderCmd = order.isLong() ? OrderCommand.SELL : OrderCommand.BUY;
                submitOrder(amount, orderCmd, tpPipsOnLoss);
            } else {
                //on manual close or close by another strategy we stop our strategy
                console.getOut().println("Order closed either from outside the strategy. Stopping the strategy.");
                context.stop();
            }
        }
    
        private void submitOrder(double amount, OrderCommand orderCmd, double tpPips) throws JFException {
            double slPrice, tpPrice;
            ITick lastTick = history.getLastTick(instrument);
            // Calculating stop loss and take profit prices
            if (orderCmd == OrderCommand.BUY) {
                slPrice = lastTick.getAsk() - slPips * instrument.getPipValue();
                tpPrice = lastTick.getAsk() + tpPips * instrument.getPipValue();
            } else {
                slPrice = lastTick.getBid() + slPips * instrument.getPipValue();
                tpPrice = lastTick.getBid() - tpPips * instrument.getPipValue();
            }
            // Submitting the order for the specified instrument at the current market price
            order = engine.submitOrder(orderCmd.toString() + System.currentTimeMillis(), instrument, orderCmd, amount, 0, 20, slPrice, tpPrice);
        }
      
        public void onStop() throws JFException {
            if(order.getState() == IOrder.State.FILLED || order.getState() == IOrder.State.OPENED){
                order.close();
            }
        }
    
        public void onTick(Instrument instrument, ITick tick) throws JFException {}
    
        public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {}
    
    }
    
     
    Last edited by a moderator: Nov 9, 2015
  2. 2rosy

    2rosy

    Code:
    SimpleTpSlStrategy t = new SimpleTpSlStrategy();
    t.amount = .003;
    
    
     
  3. chomaxa

    chomaxa

    2rosy thank you for answer, can you explain your answer ? or where I have to put that code to solve my problem. I'm new in java and learning it, please tell me more how to use this:
    SimpleTpSlStrategy t = new SimpleTpSlStrategy();
    t.amount = .003;
     
  4. 2rosy

    2rosy

    how do you run the code now? From what I see you can change amount variable to change the price
     
  5. chomaxa

    chomaxa

    I run it from Jforex=copy code to new strategy and then pres run. I want to add prices. for example when it opens 0.004 and reaches stop loss I want to add so that it can open new price for example in 0.01 and if that price reaches stop loss to open price in for example 0.05. I want to add amount in first place to tell strategy every price I want it to open in next order
     
  6. chomaxa

    chomaxa

    I want to add price list which has to be open after last price reaches stop loss. for example
    1)0.004
    2)0.05
    3)0.01
    4)0.1
    help me if someone knows how to add this to my code
     
  7. 2rosy

    2rosy

  8. chomaxa

    chomaxa

    Thanks again 2rosy, I will try to add it, but if you know can you tell me where I have to add your answer in my code ?
     
  9. 2rosy

    2rosy

    try this. http://bit.ly/rUGFwD
    Code:
    @Configurable("")
    public List prices= new ArrayList(Arrays.asList(new double[] {.004,.05,.01,.1}));
    private int level=0;
    ...
    
    // submitting the order with the specified amount, command and take profit
    submitOrder(prices.get(level++), orderCmd, tpPipsOnLoss);
    
    ...
    
    else if (message.getReasons().contains(IMessage.Reason.ORDER_CLOSED_BY_SL)) {
    // on close by SL we change the order direction and use other TP distance
    OrderCommand orderCmd = order.isLong() ? OrderCommand.SELL : OrderCommand.BUY;
    submitOrder(prices.get(level++), orderCmd, tpPipsOnLoss);