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.

IOrder.getLabel() wrong in the onMessage method,please help!!
 Post subject: IOrder.getLabel() wrong in the onMessage method,please help!! Post rating: 0   New post Posted: Tue 04 Sep, 2018, 04:48 

User rating: 2
Joined: Mon 30 Apr, 2018, 12:38
Posts: 17
Location: ChinaChina
I created a simple strategy,which submit an stop loss order once a day,and then track the exact Order name in the onMessage() method,test the strategy in the JForex platform but didn't get the correct results.
the JForex version :3.41; the JForex API version:2.13.57.


the onBar method is:
    public Instrument myInstrument=Instrument.EURUSD;
    public Period myPeriod=Period.DAILY;
    private int myOrderCounter=0;

    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
        if(instrument==myInstrument && period==myPeriod){
            myOrder=myEngine.submitOrder("mySimpleOrder"+ ++myOrderCounter,myInstrument,IEngine.OrderCommand.SELL,0.01);
            myOrder.waitForUpdate(1000);
            myConsole.getOut().println(myOrder.getLabel()+" is opened and filled!");
            double myStopLossPrice=myOrder.getOpenPrice()+60*myInstrument.getPipValue();
            myOrder.setStopLossPrice(myStopLossPrice,OfferSide.ASK,20);
            myOrder.waitForUpdate(1000);
            myConsole.getOut().println(myOrder+"stopLoss is setted!");
        }
    }


the onMessage method is:
    public void onMessage(IMessage message) throws JFException {
        if(message.getOrder()==myOrder && message.getReasons().contains(IMessage.Reason.ORDER_CHANGED_SL)){
            boolean OrderLabelEqual=message.getOrder().getLabel()=="mySimpleOrder"+myOrderCounter;//something Wrong!!
            myConsole.getInfo().println(myOrder.getLabel()+"'s stop loss changed to: "
                    +myOrder.getStopLossPrice()+"|| OrderLabelEqual: "+OrderLabelEqual);
        }


but the results is(part):
...
16:39:36 mySimpleOrder4's stop loss changed to: 1.16453|| OrderLabelEqual: false
15:38:34 mySimpleOrder4's stop loss changed to: 1.16653|| OrderLabelEqual: false
15:10:21 mySimpleOrder4's stop loss changed to: 1.16854|| OrderLabelEqual: false
11:27:13 mySimpleOrder4's stop loss changed to: 1.17054|| OrderLabelEqual: false
00:00:00 FILLED SELL 0.01 @ 1.16654stopLoss is setted!
....
the OrderLabelEqual object ought to be "true",but always return "false",WHY!!!

the full class is testOrderLabel.java in the attachment.
thanks for help!!!!!!!!!!!


Attachments:
File comment: something wrong in the 60-61 lines,please help,thanks.
testOrderLabel.java [2.6 KiB]
Downloaded 172 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: IOrder.getLabel() wrong in the onMessage method,please help!! Post rating: 0   New post Posted: Tue 04 Sep, 2018, 09:42 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Hello,

In Java == operator compares not objects themselves, but object references. And it correctly returns false, because you compare different entities. Use "equals" instead of "==".

 boolean OrderLabelEqual=message.getOrder().getLabel().equals("mySimpleOrder"+myOrderCounter);/


 
 Post subject: Re: IOrder.getLabel() wrong in the onMessage method,please help!! Post rating: 1   New post Posted: Wed 05 Sep, 2018, 02:33 

User rating: 2
Joined: Mon 30 Apr, 2018, 12:38
Posts: 17
Location: ChinaChina
thanks a lot!!
the results work well now!


 
 Post subject: Re: IOrder.getLabel() wrong in the onMessage method,please help!! Post rating: 0   New post Posted: Sun 09 Sep, 2018, 20:01 
User avatar

User rating: 13
Joined: Mon 27 Jul, 2015, 16:30
Posts: 110
Location: Canada, Mission
Strategy Elaborated - TestOrderLabel_X2

import com.dukascopy.api.*;

import java.util.HashSet;
import java.util.Set;

public class TestOrderLabel_X2 implements IStrategy {
    private IConsole myConsole;
    private IContext myContext;
    private IHistory myHistory;
    private IEngine myEngine;
    private IOrder SellOrder;
    private IOrder BuyOrder;
    private int SellOrderCounter=0;
    private int BuyOrderCounter=0;


    public Instrument myInstrument=Instrument.EURUSD;
    public Period myPeriod=Period.DAILY;

   

    @Configurable("Stop Loss Pips")
    public int stoplossPips = 60;
   
    @Configurable("Take Profit Pips")
    public int takeProfitPips = 120;

    @Configurable("Amount")
    public double amount = 0.1;


    @Override
    public void onStart(IContext context) throws JFException {
        myContext=context;
        myConsole=context.getConsole();
        setMyHistory(context.getHistory());
        myEngine=context.getEngine();


        Set<Instrument> instruments=new HashSet<Instrument>();
        instruments.add(myInstrument);
        myContext.setSubscribedInstruments(instruments,true);

    }

    @Override
    public void onTick(Instrument instrument, ITick tick) throws JFException {

    }

    @Override
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {

        if(instrument==myInstrument && period==myPeriod){
           
            SellOrder=myEngine.submitOrder("SellOrder"+ ++SellOrderCounter,myInstrument,IEngine.OrderCommand.SELL,amount);
            SellOrder.waitForUpdate(1000);
            myConsole.getOut().println(SellOrder.getLabel()+" is opened and filled!");

            double myStopLossPrice=SellOrder.getOpenPrice()+stoplossPips*myInstrument.getPipValue();
            SellOrder.setStopLossPrice(myStopLossPrice);
            SellOrder.waitForUpdate(1000);
            myConsole.getOut().println(SellOrder+"stopLoss is setted!");

            double myTakeProfitPrice=SellOrder.getOpenPrice()-takeProfitPips*myInstrument.getPipValue();
            SellOrder.setTakeProfitPrice(myTakeProfitPrice);
            SellOrder.waitForUpdate(1000);
            myConsole.getOut().println(SellOrder+"takeProfit is setted!");

           
            BuyOrder=myEngine.submitOrder("BuyOrder"+ ++BuyOrderCounter,myInstrument,IEngine.OrderCommand.BUY,amount);
            BuyOrder.waitForUpdate(1000);
            myConsole.getOut().println(BuyOrder.getLabel()+" is opened and filled!");

            double myStopLossPrice1=BuyOrder.getOpenPrice()-stoplossPips*myInstrument.getPipValue();
            BuyOrder.setStopLossPrice(myStopLossPrice1);
            BuyOrder.waitForUpdate(1000);
            myConsole.getOut().println(BuyOrder+"stopLoss is setted!");

            double myTakeProfitPrice1=BuyOrder.getOpenPrice()+takeProfitPips*myInstrument.getPipValue();
            BuyOrder.setTakeProfitPrice(myTakeProfitPrice1);
            BuyOrder.waitForUpdate(1000);
            myConsole.getOut().println(BuyOrder+"takeProfit is setted!");

        }

    }

    @Override
    public void onMessage(IMessage message) throws JFException {
       
        if(message.getOrder()==SellOrder && message.getReasons().contains(IMessage.Reason.ORDER_CHANGED_SL)){
            boolean OrderLabelEqual=message.getOrder().getLabel().equals("SellOrder"+SellOrderCounter);
            myConsole.getInfo().println(SellOrder.getLabel()+"'s stop loss changed to: "
                    +SellOrder.getStopLossPrice()+"|| OrderLabelEqual: "+OrderLabelEqual);
        }

        if(message.getOrder()==SellOrder && message.getReasons().contains(IMessage.Reason.ORDER_CLOSED_BY_SL)){

            myConsole.getWarn().println(SellOrder.getLabel()+" is closed at: "
                    +SellOrder.getClosePrice()+"|| PL is: "+SellOrder.getProfitLossInPips());
        }

        if(message.getOrder()==BuyOrder && message.getReasons().contains(IMessage.Reason.ORDER_CHANGED_SL)){
            boolean OrderLabelEqual=message.getOrder().getLabel().equals("BuyOrder"+BuyOrderCounter);
            myConsole.getInfo().println(BuyOrder.getLabel()+"'s stop loss changed to: "
                    +BuyOrder.getStopLossPrice()+"|| OrderLabelEqual: "+OrderLabelEqual);
        }

        if(message.getOrder()==BuyOrder && message.getReasons().contains(IMessage.Reason.ORDER_CLOSED_BY_SL)){

            myConsole.getWarn().println(BuyOrder.getLabel()+" is closed at: "
                    +BuyOrder.getClosePrice()+"|| PL is: "+BuyOrder.getProfitLossInPips());
        }
       
        if(message.getOrder()==SellOrder && message.getReasons().contains(IMessage.Reason.ORDER_CHANGED_TP)){
            boolean OrderLabelEqual=message.getOrder().getLabel().equals("SellOrder"+SellOrderCounter);
            myConsole.getInfo().println(SellOrder.getLabel()+"'s stop loss changed to: "
                    +SellOrder.getStopLossPrice()+"|| OrderLabelEqual: "+OrderLabelEqual);
        }

        if(message.getOrder()==SellOrder && message.getReasons().contains(IMessage.Reason.ORDER_CLOSED_BY_TP)){

            myConsole.getWarn().println(SellOrder.getLabel()+" is closed at: "
                    +SellOrder.getClosePrice()+"|| PL is: "+SellOrder.getProfitLossInPips());
        }

        if(message.getOrder()==BuyOrder && message.getReasons().contains(IMessage.Reason.ORDER_CHANGED_TP)){
            boolean OrderLabelEqual=message.getOrder().getLabel().equals("BuyOrder"+BuyOrderCounter);
            myConsole.getInfo().println(BuyOrder.getLabel()+"'s stop loss changed to: "
                    +BuyOrder.getStopLossPrice()+"|| OrderLabelEqual: "+OrderLabelEqual);
        }

        if(message.getOrder()==BuyOrder && message.getReasons().contains(IMessage.Reason.ORDER_CLOSED_BY_TP)){

            myConsole.getWarn().println(BuyOrder.getLabel()+" is closed at: "
                    +BuyOrder.getClosePrice()+"|| PL is: "+BuyOrder.getProfitLossInPips());
        }


    }

    @Override
    public void onAccount(IAccount account) throws JFException {

    }

    @Override
    public void onStop() throws JFException {

    }

    public IHistory getMyHistory() {
        return myHistory;
    }

    public void setMyHistory(IHistory myHistory) {
        this.myHistory = myHistory;
    }
}


 
 Post subject: Re: IOrder.getLabel() wrong in the onMessage method,please help!! Post rating: 0   New post Posted: Sun 09 Sep, 2018, 20:13 
User avatar

User rating: 13
Joined: Mon 27 Jul, 2015, 16:30
Posts: 110
Location: Canada, Mission
TestOrderLabel_X2 strategy report for EUR/USD instrument(s) from 2018-09-03 00:00:00 to 2018-09-09 23:59:59
Account Currency USD
Initial deposit 50000
Finish deposit 65924.52
Turnover 18582540
Comission 334.48
Parameters

stoplossPips 100
takeProfitPips 20
amount 1.0

Instrument EUR/USD

First tick time 2018-09-03 00:00:00
First tick bid value 1.15978
First tick ask value 1.1598
Last tick time 2018-09-07 20:59:58
Last tick bid value 1.1552
Last tick ask value 1.15545
Positions total 8
Closed positions 8
Orders total 8
Bought 8.00
Sold 8.00
Turnover 18582540
Comission 334.48

Opened orders:

Label Amount Direction Open price Profit/Loss at the end Profit/Loss at the end in pips Open date Comment

Closed orders:

Label Amount Direction Open price Close price Profit/Loss Profit/Loss in pips Open date Close date Comment
SellOrder1 1.000 SELL 1.16138 1.15935 2030.00 20.3 2018-09-04 00:00:00 2018-09-04 06:16:11
BuyOrder2 1.000 BUY 1.15874 1.16077 2030.00 20.3 2018-09-05 00:00:00 2018-09-05 02:51:19
SellOrder2 1.000 SELL 1.15871 1.15671 2000.00 20.0 2018-09-05 00:00:00 2018-09-05 06:51:11
BuyOrder1 1.000 BUY 1.161481 1.16362 2139.00 21.4 2018-09-04 00:00:00 2018-09-05 13:17:58
BuyOrder3 1.000 BUY 1.16347 1.16547 2000.00 20.0 2018-09-06 00:00:00 2018-09-06 00:15:22
SellOrder3 1.000 SELL 1.16344 1.16142 2020.00 20.2 2018-09-06 00:00:00 2018-09-06 07:01:19
BuyOrder4 1.000 BUY 1.16201 1.16405 2040.00 20.4 2018-09-07 00:00:00 2018-09-07 06:11:32
SellOrder4 1.000 SELL 1.16198 1.15998 2000.00 20.0 2018-09-07 00:00:00 2018-09-07 12:30:09

Processing statistic:

Function Time Calls Percent
Account information calculations 8s 947.36ms 546650 42.34%
Ticks/Bars internal processing 7s 455.01ms 480061 35.28%
Ticks/Bars/Account Info data writing for charts 4s 476.01ms 490033 21.18%
onTick method calls 86.95ms 437942 0.41%
Conditional orders processing 83.38ms 238567 0.39%
onBar method calls 35.01ms 52091 0.17%
Margin call/cut checks 29.81ms 480086 0.14%
onAccount method calls 11.33ms 66543 0.05%
Order changes processing "on server side" 2.43ms 24 0.01%
onMessage method calls 2.05ms 41 0.01%
onStart method calls 1 0%
onStop method calls 1 0%
Ticks/Bars data reads 0 0%
Historical data calls (including indicators data calls) 0 0%
Indicator calculations 0 0%
User tasks (IContext.executeTask) processing 0 0%
Other operations 0 0%
Event log:

///////////////////////////////////////////////////////////////////

12:30:09 SellOrder4 is closed at: 1.15998|| PL is: 20.0
06:11:32 BuyOrder4 is closed at: 1.16405|| PL is: 20.4
00:00:00 FILLED BUY 1.0 @ 1.16201takeProfit is setted!
00:00:00 BuyOrder4's stop loss changed to: 1.15201|| OrderLabelEqual: true
00:00:00 FILLED BUY 1.0 @ 1.16201stopLoss is setted!
00:00:00 BuyOrder4's stop loss changed to: 1.15201|| OrderLabelEqual: true
00:00:00 BuyOrder4 is opened and filled!
00:00:00 FILLED SELL 1.0 @ 1.16198takeProfit is setted!
00:00:00 SellOrder4's stop loss changed to: 1.17198|| OrderLabelEqual: true
00:00:00 FILLED SELL 1.0 @ 1.16198stopLoss is setted!
00:00:00 SellOrder4's stop loss changed to: 1.17198|| OrderLabelEqual: true
00:00:00 SellOrder4 is opened and filled!
07:01:19 SellOrder3 is closed at: 1.16142|| PL is: 20.2
00:15:22 BuyOrder3 is closed at: 1.16547|| PL is: 20.0
00:00:00 FILLED BUY 1.0 @ 1.16347takeProfit is setted!
00:00:00 BuyOrder3's stop loss changed to: 1.15347|| OrderLabelEqual: true
00:00:00 FILLED BUY 1.0 @ 1.16347stopLoss is setted!
00:00:00 BuyOrder3's stop loss changed to: 1.15347|| OrderLabelEqual: true
00:00:00 BuyOrder3 is opened and filled!
00:00:00 FILLED SELL 1.0 @ 1.16344takeProfit is setted!
00:00:00 SellOrder3's stop loss changed to: 1.17344|| OrderLabelEqual: true
00:00:00 FILLED SELL 1.0 @ 1.16344stopLoss is setted!
00:00:00 SellOrder3's stop loss changed to: 1.17344|| OrderLabelEqual: true
00:00:00 SellOrder3 is opened and filled!
06:51:11 SellOrder2 is closed at: 1.15671|| PL is: 20.0
02:51:19 BuyOrder2 is closed at: 1.16077|| PL is: 20.3
00:00:00 FILLED BUY 1.0 @ 1.15874takeProfit is setted!
00:00:00 BuyOrder2's stop loss changed to: 1.14874|| OrderLabelEqual: true
00:00:00 FILLED BUY 1.0 @ 1.15874stopLoss is setted!
00:00:00 BuyOrder2's stop loss changed to: 1.14874|| OrderLabelEqual: true
00:00:00 BuyOrder2 is opened and filled!
00:00:00 FILLED SELL 1.0 @ 1.15871takeProfit is setted!
00:00:00 SellOrder2's stop loss changed to: 1.16871|| OrderLabelEqual: true
00:00:00 FILLED SELL 1.0 @ 1.15871stopLoss is setted!
00:00:00 SellOrder2's stop loss changed to: 1.16871|| OrderLabelEqual: true
00:00:00 SellOrder2 is opened and filled!
06:16:11 SellOrder1 is closed at: 1.15935|| PL is: 20.3
00:00:00 FILLED BUY 1.0 @ 1.16144takeProfit is setted!
00:00:00 BuyOrder1's stop loss changed to: 1.15144|| OrderLabelEqual: true
00:00:00 FILLED BUY 1.0 @ 1.16144stopLoss is setted!
00:00:00 BuyOrder1's stop loss changed to: 1.15144|| OrderLabelEqual: true
00:00:00 BuyOrder1 is opened and filled!
00:00:00 FILLED SELL 1.0 @ 1.16138takeProfit is setted!
00:00:00 SellOrder1's stop loss changed to: 1.17138|| OrderLabelEqual: true
00:00:00 FILLED SELL 1.0 @ 1.16138stopLoss is setted!
00:00:00 SellOrder1's stop loss changed to: 1.17138|| OrderLabelEqual: true
00:00:00 SellOrder1 is opened and filled!


Attachments:
TestOrderLabel_X2.java [6.12 KiB]
Downloaded 185 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: IOrder.getLabel() wrong in the onMessage method,please help!! Post rating: 1   New post Posted: Sun 16 Sep, 2018, 04:22 

User rating: 2
Joined: Mon 30 Apr, 2018, 12:38
Posts: 17
Location: ChinaChina
@JP7 Thanks you very much. It's very useful and the strategy works fine!!


 

Jump to:  

  © 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