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.

Struggling with basic order placing logic
 Post subject: Struggling with basic order placing logic Post rating: 0   New post Posted: Tue 01 Jan, 2013, 16:52 
User avatar

User rating: 0
Joined: Mon 10 Dec, 2012, 17:48
Posts: 3
Hi chaps,

I'm very new to Java, and I'm struggling to get together a pretty basic routine to place a single order once per day.

Basically, I want the following:

EUR/USD only
ontick
check current time
is time 8am (london open)
if time is 8am, then place buy and sell orders with good till time of 8 hours
if an order is filled, delete the opposite order

however im unable to narrow down the time check to 8:00am, I am having to check is the time between 8:00am and 8:01am which is resulting a load of orders being placed between that 1 minute.

I got around that buy putting a boolean "has_order_been_placed" to true after the order has been placed, but I'm struggling to find a way to find a trigger to reset this to false when the order is closed?

So far I have something like this:

   
 public void onTick(Instrument instrument, ITick tick) throws JFException {
       // boolean all_orders_closed = false;
       int total_closed_orders = closed_orders_total(instrument);
       console.getOut().println("closed ORDERS = " + last_closed_orders);
       if (total_closed_orders > last_closed_orders){
           active_orders = false;
           // reset to new val
           last_closed_orders = total_closed_orders;
       }
       
       // long current_bar_time = askBar.getTime();
       
       // List<IOrder> current_orders = engine.getOrders();
        // foreach loop to check if last order was closed
       
    // If there are no active orders then proceed.
        if (positionsTotal(instrument) == 0){
            // Is it the correct time to trade (8 in the morning)
            boolean is_time_to_trade = is_valid_time(8,0,8,1);
           // console.getOut().println("is it time to trade??? = " + is_time_to_trade);
            if (is_time_to_trade && active_orders == false){
                double current_tick_value = tick.getAsk();
                long current_tick_time = tick.getTime();
                // Place a London order
                console.getOut().println("PLACING A TRADE OK!!!");
                // set active orders flag to true
                active_orders = true;
                place_order(current_tick_value, current_tick_time, ln_order_time_period);
               
            }
        }


 protected int closed_orders_total(Instrument instrument) throws JFException {
         int counter = 0;
         for (IOrder order : engine.getOrders(instrument)) {
             if (order.getState() == IOrder.State.CLOSED) {
                 counter++;
             }
         }
         return counter;
     }
     


Cheers for any help!

Regards,

Jon.


 
 Post subject: Re: Struggling with basic order placing logic Post rating: 0   New post Posted: Wed 02 Jan, 2013, 09:22 

User rating: 0
Joined: Mon 31 Dec, 2012, 15:03
Posts: 5
Hi bodleytunes, I am also new in Java, but maybe this advice give you the way:
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
calendarTick = Calendar.getInstance(TimeZone.getTimeZone("GMT"));

......

//check instrument
if (instrument != Instrument.EURUSD;) {
   return;
}
//check working hours
if(!calendarTick.get(Calendar.HOUR_OF_DAY) >= 8){
   return;
}

// example to check amount of current opened orders
if (totalPositions(instrument) != 0) {
   return;
}

   
//get amount of current orders
    protected int totalPositions(Instrument instrument) throws JFException {
        int positions = 0;
        for (IOrder order : engine.getOrders(instrument)) {
            if (order.getState() == IOrder.State.FILLED) {
                positions++;
            }
        }
        return positions;
    }

    private void CloseAllOrders() throws JFException{
        for (IOrder order : engine.getOrders()) {
            engine.getOrder(order.getLabel()).close();
//            console.getOut().println("Close Order = " + order.getLabel());
        }
    }

//also you may need an event:
    public void onMessage(IMessage message) throws JFException {
        if (message != null) {
            IOrder order = message.getOrder();
                switch (message.getType()) {
.....
                   case ORDER_FILL_OK:
              console.getOut().println("ORDER_FILL_OK: " + order.getId());


 
 Post subject: Re: Struggling with basic order placing logic Post rating: 0   New post Posted: Sun 06 Jan, 2013, 12:56 
User avatar

User rating: 0
Joined: Mon 10 Dec, 2012, 17:48
Posts: 3
Hi!

I managed it in the end,

Thanks for that though I'll give it a read :)

Cheers,

Jon.


 

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