Send an Order Example (MarketMaker)

Custom indicators, trading strategies, data export and recording and more...
Gregory
Posts: 14
Joined: Thu Apr 11, 2019 10:13 pm
Has thanked: 4 times

Send an Order Example (MarketMaker)

Post by Gregory » Thu Apr 11, 2019 10:32 pm

Hi everyone

I was searching the forum, and the demo for how to send an order. I also reviewed the adapter for Bitmex looking for clues. I can see the items needed, but no working example of how to place an order.

I get an exception with the following code found in the MarketMaker example:

Code: Select all

package com.bookmap.api.simple.demo.strategies;

import velox.api.layer1.annotations.Layer1ApiVersion;
import velox.api.layer1.annotations.Layer1ApiVersionValue;
import velox.api.layer1.annotations.Layer1SimpleAttachable;
import velox.api.layer1.annotations.Layer1StrategyName;
import velox.api.layer1.data.InstrumentInfo;
import velox.api.layer1.layers.utils.OrderBook;
import velox.api.layer1.messages.indicators.Layer1ApiUserMessageModifyIndicator.GraphType;
import velox.api.layer1.simplified.Api;
import velox.api.layer1.simplified.Bar;
import velox.api.layer1.simplified.BarDataListener;
import velox.api.layer1.simplified.CustomModule;
import velox.api.layer1.simplified.Indicator;
import velox.api.layer1.simplified.InitialState;
import velox.api.layer1.simplified.Intervals;

import velox.api.layer1.data.OcoOrderSendParameters;
import velox.api.layer1.data.OrderCancelParameters;
import velox.api.layer1.data.OrderDuration;
import velox.api.layer1.data.OrderInfoBuilder;
import velox.api.layer1.data.OrderMoveParameters;
import velox.api.layer1.data.OrderResizeParameters;
import velox.api.layer1.data.OrderSendParameters;
import velox.api.layer1.data.OrderStatus;
import velox.api.layer1.data.OrderType;
import velox.api.layer1.data.OrderUpdateParameters;
import velox.api.layer1.data.SimpleOrderSendParameters;
import velox.api.layer1.data.SimpleOrderSendParametersBuilder;


@Layer1SimpleAttachable
@Layer1StrategyName("Send Orders")
@Layer1ApiVersion(Layer1ApiVersionValue.VERSION1)
public class SendOrders implements
    CustomModule, BarDataListener {
    Api api;
    private String alias;
    

    @Override
    public void initialize(String alias, InstrumentInfo info, Api api, InitialState initialState) {
        this.alias = alias;        
    }    

    @Override
    public void stop() {
    }

    @Override
    public void onBar(OrderBook ob, Bar bar) {
    		OrderSendParameters orderSendParameters = new SimpleOrderSendParametersBuilder(alias, true, 1).build();
    	       	api.sendOrder(orderSendParameters);  
    	}        
    }

    @Override
    public long getInterval() {
        return Intervals.INTERVAL_15_SECONDS;
    }
}

Code: Select all

java.lang.NullPointerException
	at com.bookmap.api.simple.demo.strategies.SendOrders.onBar(SendOrders.java:57)
	at velox.api.layer1.simplified.SimplifiedL1ApiLoader$InstanceWrapper.setTime(SimplifiedL1ApiLoader.java:1669)
	at velox.api.layer1.simplified.SimplifiedL1ApiLoader.onUserMessage(SimplifiedL1ApiLoader.java:2178)
	at velox.bookmap.ed.onUserMessage(SourceFile:359)
	at velox.bookmap.dB.onUserMessage(SourceFile:161)
	at velox.bookmap.fv.a(SourceFile:230)
	at velox.bookmap.dl.a(SourceFile:43)
	at velox.bookmap.fv.onUserMessage(SourceFile:230)
	at velox.api.layer1.layers.Layer1ApiUpstreamRelay.onUserMessage(SourceFile:210)
	at velox.api.layer1.layers.Layer1ApiInjectorRelay.onUserMessage(SourceFile:212)
	at velox.bookmap.eo.onUserMessage(SourceFile:233)
	at velox.api.layer1.layers.Layer1ApiUpstreamRelay.onUserMessage(SourceFile:210)
	at velox.api.layer1.layers.Layer1ApiInjectorRelay.onUserMessage(SourceFile:212)
	at velox.api.layer1.layers.Layer1ApiUpstreamRelay.onUserMessage(SourceFile:210)
	at velox.api.layer1.layers.Layer1ApiUpstreamRelay.onUserMessage(SourceFile:210)
	at velox.bookmap.eT.onUserMessage(SourceFile:212)
	at velox.api.layer1.layers.Layer1ApiUpstreamRelay.onUserMessage(SourceFile:210)
	at velox.api.layer1.layers.Layer1ApiInjectorRelay.onUserMessage(SourceFile:212)
	at velox.bookmap.dA.onUserMessage(SourceFile:105)
	at velox.api.layer1.layers.Layer1ApiUpstreamRelay.onUserMessage(SourceFile:210)
	at velox.api.layer1.layers.Layer1ApiUpstreamRelay.onUserMessage(SourceFile:210)
	at velox.bookmap.eC.onUserMessage(SourceFile:116)
	at velox.api.layer1.layers.Layer1ApiUpstreamRelay.onUserMessage(SourceFile:210)
	at velox.bookmap.fw.onUserMessage(SourceFile:57)
	at velox.api.layer1.layers.Layer1ApiUpstreamRelay.onUserMessage(SourceFile:210)
	at velox.api.layer1.layers.Layer1ApiUpstreamRelay.onUserMessage(SourceFile:210)
	at velox.api.layer1.layers.Layer1ApiInjectorRelay.onUserMessage(SourceFile:212)
	at velox.bookmap.gx.onUserMessage(SourceFile:592)
	at velox.api.layer1.layers.Layer1ApiUpstreamRelay.onUserMessage(SourceFile:210)
	at velox.bookmap.dE.b(SourceFile:119)
	at velox.bookmap.ds.a(SourceFile:32)
	at velox.bookmap.dE.onUserMessage(SourceFile:119)
	at velox.api.layer1.layers.Layer1ApiUpstreamRelay.onUserMessage(SourceFile:210)
	at velox.bookmap.dD.onUserMessage(SourceFile:27)
	at velox.api.layer1.layers.Layer1ApiUpstreamRelay.onUserMessage(SourceFile:210)
	at velox.api.layer1.layers.Layer1ApiUpstreamRelay.onUserMessage(SourceFile:210)
	at velox.api.layer1.layers.Layer1ApiInjectorRelay.onUserMessage(SourceFile:212)
	at velox.bookmap.iZ.a(SourceFile:678)
	at velox.bookmap.iZ.a(SourceFile:532)
	at velox.bookmap.hi.a(SourceFile:51)
	at velox.bookmap.iZ.b(SourceFile:558)
	at velox.bookmap.iZ.e(SourceFile:15509)
	at java.lang.Thread.run(Thread.java:745)
Could anyone please share the simplest example to send an order from the API, or show me what is wrong with my code?
Also how can we discover how the SimpleOrderSendParametersBuilder works?

Thank you so much,
Greg

Andry API support
Posts: 548
Joined: Mon Jul 09, 2018 11:18 am
Has thanked: 25 times
Been thanked: 85 times

Re: Send an Order Example (MarketMaker)

Post by Andry API support » Fri Apr 12, 2019 8:57 pm

Hi Gregory,
your code is fine in general, just missing some details

Code: Select all

java.lang.NullPointerException
	at com.bookmap.api.simple.demo.strategies.SendOrders.onBar(SendOrders.java:57)
line 57 is

Code: Select all

        api.sendOrder(orderSendParameters);
this actually says 'api = null' where null means 'does not exist' which happens if the object has not been assigned a value.
And if the api does not exist it cannot send orders, that is what the exception is about.
Let's have a look at the api field. Your module contains this field but it never gets a value. The api object is acquired in the method 'initialize'. We should assign this object to your field so it looks like

Code: Select all

    @Override
    public void initialize(String alias, InstrumentInfo info, Api api, InitialState initialState) {
        this.alias = alias;
        this.api = api;
 }
Now the api object is stored into your module's field.
Now annotate your module with the @Layer1TradingStrategy annotation to make sending orders possible (otherwise, another exception will be thrown).

Code: Select all

@Layer1SimpleAttachable
@Layer1StrategyName("Send Orders")
@Layer1ApiVersion(Layer1ApiVersionValue.VERSION1)
@Layer1TradingStrategy
public class SendOrders implements CustomModule, BarDataListener {
Now your module sends market orders.
Another thing to mention is an odd number of curly braces in 'onBar' method (one opening and two closing), I think it's a kind of copy-paste issue. Please check it anyway.

Andry API support
Posts: 548
Joined: Mon Jul 09, 2018 11:18 am
Has thanked: 25 times
Been thanked: 85 times

Re: Send an Order Example (MarketMaker)

Post by Andry API support » Fri Apr 12, 2019 9:23 pm

Have you got access to the SimpleOrderSendParametersBuilder source code?

Gregory
Posts: 14
Joined: Thu Apr 11, 2019 10:13 pm
Has thanked: 4 times

Re: Send an Order Example (MarketMaker)

Post by Gregory » Sun Apr 14, 2019 2:10 am

Andrey thank you so much, using those fixes I was able to add buy and sell orders. I haven't seen the source for SimpleOrderSendParametersBuilder yet, where should I be looking?

for SimpleOrderSendParametersBuilder I have seen the following javadoc:

Code: Select all

public SimpleOrderSendParametersBuilder(java.lang.String alias,
  boolean isBuy,
  int size,
  OrderDuration duration,
  java.lang.String clientId,
  double limitPrice,
  double stopPrice,
  int takeProfitOffset,
  int stopLossOffset,
  int stopLossTrailingStep,
  int trailingStep,
  boolean doNotIncrease)
While we are talking, can I ask is there a console output for Bookmap?

Many thanks,
Greg

Andry API support
Posts: 548
Joined: Mon Jul 09, 2018 11:18 am
Has thanked: 25 times
Been thanked: 85 times

Re: Send an Order Example (MarketMaker)

Post by Andry API support » Mon Apr 15, 2019 8:44 am

Yes, there is.
1) view log file from Bookmap (File -> Show log file)
!! its output is only for Log method and you are able to write to this log only using Log method

2) run Bookmap via IDE or batch file/console
2a) run via IDE (preferable) - see
https://github.com/BookmapAPI/Layer0Api ... tarted.pdf
see 'Run from IDE' at page 3
2b) run from batch file/console (least preferable)
run this command from the console (command prompt)
"c:\Program Files\Bookmap\jre\bin\java.exe" -cp "C:\Program Files\Bookmap\Bookmap.jar;C:\Program Files\Bookmap\lib\*" velox.ib.Main
or create a .bat file that contains this command and run it from 'C:\Bookmap\Config' folder

3) set 'console' parameter to 1 in your 'C:\Bookmap\Config\launcher-options.conf' file

Gregory
Posts: 14
Joined: Thu Apr 11, 2019 10:13 pm
Has thanked: 4 times

Re: Send an Order Example (MarketMaker)

Post by Gregory » Tue Apr 16, 2019 11:03 pm

Thanks so much, the bat file works perfectly! I tried with eclipse, but I had this error when following the instructions using the most recent Bookmap 7.1 build 7, and Eclipse 2019-03

Code: Select all

Disabling d3d
Exception in thread "main" java.lang.NoSuchMethodError: velox.api.layer1.common.Log.setListener(Lvelox/api/layer1/common/Log$a;)V
	at velox.ib.Main.b(SourceFile:2151)
	at java.lang.Thread.run(Unknown Source)
20190415 23:09:18.341(UTC) INFO: diagnostics logs archive size = 0
I have settled with the following code to send a long order

Code: Select all

SimpleOrderSendParametersBuilder longOrderSendParameters = new SimpleOrderSendParametersBuilder (alias, true, 1);
longOrderSendParameters.setLimitPrice(askPrice-10*pips);
longOrderSendParameters.setTakeProfitOffset(10);
longOrderSendParameters.setStopLossClientId("longStop");
longOrderSendParameters.setTakeProfitClientId("longTarget");
longOrderSendParameters.setClientId(tempClientId);
OrderSendParameters longOrder = longOrderSendParameters.build();
api.sendOrder(longOrder); 
And sorry to go off topic, but if I update that order, and the order is filled, it seems to lose the attached stop order, but keeps the target order.

Code: Select all

api.updateOrder(new OrderMoveParameters(order.orderId, askPrice-15*pips, askPrice-5*pips));
Is there a way to see the code for OrderMoveParameters, or another way to make sure the stop order is attached to the parent when updating an order? I am unclear why it would be happening, but admittedly could be something obvious that I am not seeing.

Many thanks,
Greg

Andry API support
Posts: 548
Joined: Mon Jul 09, 2018 11:18 am
Has thanked: 25 times
Been thanked: 85 times

Re: Send an Order Example (MarketMaker)

Post by Andry API support » Wed Apr 17, 2019 8:09 am

Hi Greg,
show me your full module code please

Gregory
Posts: 14
Joined: Thu Apr 11, 2019 10:13 pm
Has thanked: 4 times

Re: Send an Order Example (MarketMaker)

Post by Gregory » Wed Apr 17, 2019 2:42 pm

Thanks Andrey, I just sent it to you

Gregory
Posts: 14
Joined: Thu Apr 11, 2019 10:13 pm
Has thanked: 4 times

Re: Send an Order Example (MarketMaker)

Post by Gregory » Wed Apr 17, 2019 9:28 pm

Great news,

It appears that setStopLossOffset is required, so assuming you want an 8 tick stop loss, the missing line of code was

Code: Select all

longOrderSendParameters.setStopLossOffset(8);
There was not any issue with updating orders. Let me take this moment to say thanks for helping me through it.

Thanks,
Greg

Andry API support
Posts: 548
Joined: Mon Jul 09, 2018 11:18 am
Has thanked: 25 times
Been thanked: 85 times

Re: Send an Order Example (MarketMaker)

Post by Andry API support » Thu Apr 18, 2019 11:16 am

Hi Greg,
Great, good for you.

Post Reply