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.

Order close on/around predefined price
 Post subject: Order close on/around predefined price Post rating: 0   New post Posted: Mon 09 Apr, 2012, 23:47 

User rating: 0
Joined: Mon 09 Apr, 2012, 23:30
Posts: 14
Hey.
I've added a condition to a strategy, that it should close an open order if onTick carries a tick.getAsk() or tick.getBid() past high/low of a stored bar.
Somehow it don't trigger half the time tho.

heres the close order trigger:
    public void onTick(Instrument instrument, ITick tick) throws JFException {
        if (!instrument.equals(this.instrument) || buyBar == null) {
            return;
        }
.........

if (enableShort && isActive(orderShort)) {
            // Short Exit:
            if (tick.getAsk() > buyBar.getHigh()) {
                exitShort = true;
                print("trigger! :"+orderShort.getLabel()+":"+buyBar.getHigh() + ":"+tick.getAsk() + ":"+tick.getTime());       
            }   

            // Exit
            // Short exit
            if (exitShort) {
                closeOrder(orderShort);
                print("kicker! :"+orderShort.getLabel()+":"+buyBar.getHigh() + ":"+tick.getAsk() + ":"+tick.getTime());
                orderShort = null;
                exitShort = false;
            }
        }
    }



The buyBar is set under onBar() as follows:
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
        if (!instrument.equals(this.instrument) || !period.equals(selectedPeriod)) {
            return;
        }
....
        // Begin Short
        if (enableShort ){
            // Short Open: Open if bar close under ma0
            if (askBar.getClose() < ma0) {
                enterShort = true;
            }
           
            // Open order
            if (enterShort && !isActive(orderShort)) {
                orderShort = submitOrder(OrderCommand.SELL);
                buyBar = askBar;
                print(":"+ buyBar.getHigh());
            }
            enterShort = false;
           
           
            // Short Exit: Bar closing past MA 0
            if (askBar.getClose() > ma0) {
                exitShort = true;
            }
           
            // Close order
            if (exitShort) {
                closeOrder(orderShort);
                orderShort = null;
                exitShort = false;   
            }
        }
    }



When i run this on USDJPY 1H from 4th april 2012 and till today, it'll open an order at 10:00 4/4 2012 but wont close it till 15:00 4/4 2012.
And im pretty sure even the next bar at 11:00 has a high well over 10:00 high.
Is it not reliable to store the bar I've opened an order at, and if so, how would you recommend it done instead?


Filip


 
 Post subject: Re: Order close on/around predefined price Post rating: 0   New post Posted: Tue 10 Apr, 2012, 09:41 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
The order gets submitted already on the next bar, not the one that you receive in onBar - in onBar you receive already a finshed bar. Consider retrieving the bar in the following way:
    private IBar getOrderCreationBar(IOrder order, Period period) throws JFException{
        long barTime = history.getBarStart(period, order.getCreationTime());
        List<IBar> bars = history.getBars(Instrument.EURUSD, period, OfferSide.BID, Filter.NO_FILTER, barTime, barTime);
        return bars.get(0);
    }
Mind that if the retrieved bar is the currently forming bar then you should not store it in a field, since the IBar object won't get updated on current market price change.

Also consider adding additional print statements for the cases when you think that the strategy should make a trade, but it does not.


 
 Post subject: Re: Order close on/around predefined price Post rating: 0   New post Posted: Fri 13 Apr, 2012, 22:43 

User rating: 0
Joined: Mon 09 Apr, 2012, 23:30
Posts: 14
Ok. Thanks for that.

I was storing it in order to bypass the history exception bug.
Its not needed for up to date info so it dont need to be updated.


 

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