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.

Only one open order at time. Please help.
 Post subject: Only one open order at time. Please help. Post rating: 0   New post Posted: Sat 20 Nov, 2021, 16:45 

User rating: 4
Joined: Wed 23 May, 2018, 20:08
Posts: 44
Location: FinlandFinland
Hello, how can I limit my orders to only one order at a time?


 
 Post subject: Re: Only one open order at time. Please help. Post rating: 1   New post Posted: Sun 21 Nov, 2021, 12:18 

User rating: 2
Joined: Fri 06 Apr, 2018, 17:06
Posts: 23
Location: Poland,
Maybe something like this. Create global variable, like counter. In function onBar for example use period object to generate "interrupt" for a given interval. In this example code I use One_hour period, it means strategy in every hour will execute the code in this conditional statement. And then you can reset the timer, counters etc in other period like DAILY. Next you can create function sendOrder and in body this function check value of counter. Something like that...
import java.util.Random; 
import com.dukascopy.api.IEngine.OrderCommand;

public class Strategy implements IStrategy {

    private int counter = 0; // global variable

    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
       
        // per one hour do something
        // https://www.dukascopy.com/client/javadoc3/com/dukascopy/api/Period.html
        if (period.equals(Perdiod.ONE_HOUR)) {
            // generate random value
            Random r = new Random();
            // if random value larger than 0.5 send BUY, if less than send SELL order
            sendOrder(instrument, (r.nextDouble() > 0.5 ? OrderCommand.BUY : OrderCommand>SELL));
            // increment counters, timers etc
            counter++;
        } else if (period.equals(Period.DAILY)) {
            // reset timer, coutners etc
            reset();
        }

    }
   
    public void sendOrder(Instrument instrument, OrderCommand orderCommand) throws JFException {
        if (counter > 0) return;
        IOrder order = engine.submitOrder(getLabel(instrument), instrument, orderCommand, 1);
        order.waitForUpdate(2000);
    }
   
    protected String getLabel(Instrument instrument) {
        String label = instrument.name();
        label = label.substring(0, 2) + label.substring(3, 5);
        label = label + (tagCounter++);
        label = label.toLowerCase();
        return label;
    }

    public void reset() {
        counter = 0;
    }

}


You can use onTick function to increment counter etc. For example in every 500 ticks counter has reset
public void onTick(Instrument instrument, ITick tick) throws JFException {
      if (counter >= 500) {
        counter = 0;
    }
    counter++;
}


 
 Post subject: Re: Only one open order at time. Please help. Post rating: 0   New post Posted: Sun 21 Nov, 2021, 14:17 

User rating: 4
Joined: Wed 23 May, 2018, 20:08
Posts: 44
Location: FinlandFinland
Thank you goose_ for your answer.

I have experience in systems that count to a limit and reset.
Now I am looking for a system that makes an order and then checks if there is any open orders, and if there are no open orders, then it will open an order again.
I have poor coding skills.

OnBar:
>OPEN ORDER WITH SL/TP
>CHECK IF ORDER IS STILL OPEN
>IF NO OPEN ORDER THEN OPEN NEW ORDER

I am looking at https://www.dukascopy.com/client/javadoc/ and asking this forum for help.

Thank you.


 
 Post subject: Re: Only one open order at time. Please help. Post rating: 0   New post Posted: Sun 21 Nov, 2021, 20:06 

User rating: 2
Joined: Fri 06 Apr, 2018, 17:06
Posts: 23
Location: Poland,
To check if there is no open orders is method engine.getOrders() https://www.dukascopy.com/client/javado ... ngine.html
This is na array. Examples how to use arraylist https://beginnersbook.com/2013/12/java-arraylist/ how it works. If You send order then this IOrder object is store to this array. And You can read this array using .size() method to verified is there any orders opened, any object in there. If You put something to array, ten array's size increase. And this method is how to track if there's any order opened right know.

engine.getOrders() track current opened order, but there is also a method context.getHistory().getOrderHistory to get list of closed orders
https://www.dukascopy.com/wiki/en/devel ... er-history

package jforex;

import java.util.*;

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

public class Strategy implements IStrategy {
    private IEngine engine;
    private IConsole console;
    private IHistory history;
    private IContext context;
    private IIndicators indicators;
    private IUserInterface userInterface;
   
    @Configurable("Instrument")
    public Instrument instrument = Instrument.EURUSD;
    @Configurable("Amount")
    public double amount = 0.02;
    @Configurable("Stop loss")
    public int stopLossPips = 20;
    @Configurable("Take profit")
    public int takeProfitPips = 90;
   
    private OrderCommand orderCmd = null;
   
    private int counter = 0;
   
    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();
       
    }

    public void onAccount(IAccount account) throws JFException {
    }

    public void onMessage(IMessage message) throws JFException {
    }

    public void onStop() throws JFException {
    }

    public void onTick(Instrument instrument, ITick tick) throws JFException {
    }
   
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
       
        // for example strategy choose BUY order and store to global variable orderCmd
        // in this point strategy make a signal to create an order
        // https://www.dukascopy.com/wiki/en/development/strategy-api/strategy-examples
        this.orderCmd = OrderCommand.BUY;
       
        // then sends the order
        submitOrder(orderCmd);
       
        // resubmit
        resubmitOrder();
       
    }
   
    public void resubmitOrder() throws JFException {
        // check if theres no open order in globara array
        // engine.getOrders() for selected instrument
        // if the array is not empty quit from the function
        // but if the array engine.getOrders is empty then send order again
        if (engine.getOrders(this.instrument).size() != 0) return;
        submitOrder(orderCmd);
    }
       
    // https://www.dukascopy.com/wiki/en/development/strategy-api/strategy-examples/sma-simple
    public void submitOrder(OrderCommand orderCmd) throws JFException {
       
        // limit to one order
        if (engine.getOrders(this.instrument).size() > 0) return;
   
        double stopLossPrice, takeProfitPrice;
       
        // Calculating stop loss and take profit prices
        if (orderCmd == OrderCommand.BUY) {
            stopLossPrice = history.getLastTick(this.instrument).getBid();
            stopLossPrice -= getPipPrice(this.stopLossPips);
            takeProfitPrice = history.getLastTick(this.instrument).getBid();
            takeProfitPrice += getPipPrice(this.takeProfitPips);
        } else {
            stopLossPrice = history.getLastTick(this.instrument).getAsk();
            stopLossPrice += getPipPrice(this.stopLossPips);
            takeProfitPrice = history.getLastTick(this.instrument).getAsk();
            takeProfitPrice -= getPipPrice(this.takeProfitPips);
        }
       
        // Submitting an order for the specified instrument at the current market price
        IOrder order =  engine.submitOrder(getLabel(instrument),
            this.instrument, orderCmd, this.amount, 0, 20, stopLossPrice, takeProfitPrice);
        order.waitForUpdate(2000);
    }

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

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


 
 Post subject: Re: Only one open order at time. Please help. Post rating: 0   New post Posted: Tue 30 Nov, 2021, 18:58 

User rating: 4
Joined: Wed 23 May, 2018, 20:08
Posts: 44
Location: FinlandFinland
Hey and thank you goose_ for your excelent answer! :mrgreen:
Now can you pretty please make it such that it both sells ands buys?
Thank you! :) :ugeek: 8-) :D :D :D


 
 Post subject: Re: Only one open order at time. Please help. Post rating: 0   New post Posted: Sat 25 Dec, 2021, 09:16 

User rating: 4
Joined: Wed 23 May, 2018, 20:08
Posts: 44
Location: FinlandFinland
Hello goose_

You are amazing at coding!

Can you make this so that it goes

BUY, SELL, BUY, SELL... infinite ?

That would be super

...and if it is not too hard, make it please so that it has stop loss and take profit and a time out when the order gets closed.

Thanks you are the superb!

:)

Then I can close this as solved.


 

Jump to:  

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