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.

Random submitOrder Label. Help, please.
 Post subject: Random submitOrder Label. Help, please. Post rating: 0   New post Posted: Mon 28 May, 2018, 22:21 

User rating: 4
Joined: Wed 23 May, 2018, 20:08
Posts: 44
Location: FinlandFinland
Hello Forums,
how can I generate a random submitOrder label. The "example"- string could be a random number, can someone tell me, please?

order = engine.submitOrder("EXAMPLE", Instrument.EURUSD, OrderCommand.BUY, 0.01);


 
 Post subject: Re: Random submitOrder Label. Help, please. Post rating: 0   New post Posted: Tue 29 May, 2018, 19:33 

User rating: 18
Joined: Thu 20 Apr, 2017, 22:42
Posts: 165
Location: Russian Federation,
import java.util.Random;
...
Random rnd = new Random();
...
String randomString = Long.toHexString(rnd.nextLong()).toLowerCase()


 
 Post subject: Re: Random submitOrder Label. Help, please. Post rating: 0   New post Posted: Thu 31 May, 2018, 20:50 

User rating: 4
Joined: Wed 23 May, 2018, 20:08
Posts: 44
Location: FinlandFinland
order = engine.submitOrder("EXAMPLE", Instrument.EURUSD, OrderCommand.BUY, 0.01);


How do I implement it to the example code as Label number? Can you show me, please?


 
The Best Answer  Post subject: Re: Random submitOrder Label. Help, please. Post rating: 0   New post Posted: Thu 31 May, 2018, 21:25 
User avatar

User rating: 13
Joined: Mon 27 Jul, 2015, 16:30
Posts: 110
Location: Canada, Mission
private String getLabel(OrderCommand cmd) {
return cmd.toString() + UUID.randomUUID().toString().replace('-', '0'); // + System.currentTimeMillis();
}
////label order comm


public void onTick(Instrument instrument, ITick tick) throws JFException {
if (!ordersSubmitted)
{
ordersSubmitted = true;
// engine.submitOrder("LABEL", Instrument.EURCHF, IEngine.OrderCommand.BUY, LOT , Price, Slip, SL, TP, GTC; "note"
engine.submitOrder("Buy_EURCHF", Instrument.EURCHF, IEngine.OrderCommand.BUY, 0.001, 0, 2, 0, 0, 0, "B");
engine.submitOrder("Sell_EURUSD", Instrument.EURUSD, IEngine.OrderCommand.SELL, 0.001, 0, 2, 0, 0, 0, "S");
engine.submitOrder("SellUSDCHF", Instrument.USDCHF, IEngine.OrderCommand.SELL, 0.001, 0, 2, 0, 0, 0, "S");


}}

One solution used by may is to auto generate a label with a unique number. Example:
protected String getLabel() {
String label;
label = "IVF" + generateRandom(10000) + generateRandom(10000);
return label;


}

private static String generateRandom(int n) {
int randomNumber = (int) (Math.random() * n);
String answer = "" + randomNumber;
if (answer.length() > 3) {
answer = answer.substring(0, 4);

But the simplest easiest way is to add to the label System.currentTimeMillis which is unique and continuously calculates time
in milliseconds since 1 January 1970. Example:

public void onTick(Instrument instrument, ITick tick) throws JFException {
if (!ordersSubmitted)
{
ordersSubmitted = true;
// engine.submitOrder("LABEL", Instrument.EURCHF, IEngine.OrderCommand.BUY, LOT , Price, Slip, SL,TP, GTC; "note"
engine.submitOrder("X"+System.currentTimeMillis(), Instrument.EURCHF, IEngine.OrderCommand.BUY, 0.001, 0, 2, 0, 0, 0, "Buy");
engine.submitOrder("X"+System.currentTimeMillis(), Instrument.EURUSD, IEngine.OrderCommand.SELL, 0.001, 0, 2, 0, 0, 0, "Sell");
engine.submitOrder("X"+System.currentTimeMillis(), Instrument.USDCHF, IEngine.OrderCommand.SELL, 0.001, 0, 2, 0, 0, 0, "Sell");


}}
//:)

protected String getLabel() {
String label;
label = "IVF" + getCurrentTime(LastTick.getTime()) + generateRandom(10000) + generateRandom(10000);
return label;
}

private String getCurrentTime(long time) {
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
return sdf.format(time);
}

private static String generateRandom(int n) {
int randomNumber = (int) (Math.random() * n);
String answer = "" + randomNumber;
if (answer.length() > 3) {
answer = answer.substring(0, 4);
}
return answer;
}


 
 Post subject: Re: Random submitOrder Label. Help, please. Post rating: 0   New post Posted: Thu 31 May, 2018, 22:04 

User rating: 4
Joined: Wed 23 May, 2018, 20:08
Posts: 44
Location: FinlandFinland
Thank you JP7 for you awesome answer! You are a true Hero!


 

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