Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@

import java.nio.ByteBuffer;

public class CancelChildOrder implements Action{
public class CancelChildOrder implements Action {

private final ChildOrder orderToCancel;

public CancelChildOrder(ChildOrder orderToCancel) {
this.orderToCancel = orderToCancel;
}

public long getOrderId() {
return orderToCancel.getOrderId(); // retrieves the order id
}

@Override
public String toString() {
return "CancelChildOrder(" + orderToCancel + ")";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,35 @@
public class CreateChildOrder implements Action {

private static final Logger logger = LoggerFactory.getLogger(CreateChildOrder.class);
private final long quantity;
private final long price;

private final Side side;


private final long quantity; // Quantity of the order
private final long price; // Price at which the order will be placed
private final Side side; // Side of the order (BUY/SELL)

// Constructor to initialize the fields
public CreateChildOrder(final Side side, final long quantity, final long price) {
this.quantity = quantity;
this.price = price;
this.side = side;
}

// Getter for quantity
public long getQuantity() {
return quantity;
}

// Getter for price
public long getPrice() {
return price;
}

// Getter for side
public Side getSide() {
return side;
}

@Override
public String toString() {
return "CreateChildOrder(side=" + side + ",quantity=" + quantity + ",price=" + price + ")";
return "CreateChildOrder(side=" + side + ", quantity=" + quantity + ", price=" + price + ")";
}

@Override
Expand All @@ -38,12 +51,13 @@ public void apply(Sequencer sequencer) {
final UnsafeBuffer directBuffer = new UnsafeBuffer(byteBuffer);
final MessageHeaderEncoder headerEncoder = new MessageHeaderEncoder();

// Wrap and apply the message header
encoder.wrapAndApplyHeader(directBuffer, 0, headerEncoder);
headerEncoder.schemaId(CreateOrderEncoder.SCHEMA_ID);
headerEncoder.version(CreateOrderEncoder.SCHEMA_VERSION);
encoder.price(price);
encoder.quantity(quantity);
encoder.side(side);
sequencer.onCommand(directBuffer);
sequencer.onCommand(directBuffer); // Send the command to the sequencer
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package codingblackfemales.marketdata.api;

public class MarketTick {

private double price;
private int volume;
private double spread;

public MarketTick(double price, int volume, double spread) {
this.price = price;
this.volume = volume;
this.spread = spread;
}

public double getPrice() {
return price;
}

public int getVolume() {
return volume;
}

public double getSpread() {
return spread;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import codingblackfemales.sequencer.event.OrderEventListener;
import codingblackfemales.sotw.ChildOrder;
import codingblackfemales.sotw.OrderState;
import codingblackfemales.sotw.OrderStatus;
import messages.order.*;

import java.util.LinkedList;
Expand All @@ -12,27 +13,25 @@
public class OrderService extends OrderEventListener {

private final RunTrigger runTrigger;

private List<ChildOrder> children = new LinkedList<>();

public OrderService(RunTrigger runTrigger) {
this.runTrigger = runTrigger;
}


private void triggerRun(){
private void triggerRun() {
runTrigger.triggerRun();
}

private ChildOrder createChildOrder(final CreateOrderDecoder create){
return new ChildOrder(create.side(), create.orderId(), create.quantity(), create.price(), OrderState.PENDING);
private ChildOrder createChildOrder(final CreateOrderDecoder create) {
return new ChildOrder(create.side(), create.orderId(), create.quantity(), create.price(), OrderStatus.PENDING);
}

private void updateState(ChildOrder child, int state){
private void updateState(ChildOrder child, int state) {
child.setState(state);
}

private void addChildFill(ChildOrder child, long filledQuantity, long filledPrice){
private void addChildFill(ChildOrder child, long filledQuantity, long filledPrice) {
child.addFill(filledQuantity, filledPrice);
}

Expand All @@ -42,8 +41,10 @@ public void onCreateOrder(final CreateOrderDecoder create) {
triggerRun();
}

private ChildOrder find(long orderId){
return children.stream().filter( order -> order.getOrderId() == orderId).findFirst().get();
private ChildOrder find(long orderId) {
return children.stream().filter(order -> order.getOrderId() == orderId).findFirst().orElse(null); // Use orElse
// to avoid
// NoSuchElementException
}

@Override
Expand All @@ -70,7 +71,7 @@ public void onPendingOrder(final PendingOrderDecoder pending) {
triggerRun();
}

public List<ChildOrder> children(){
public List<ChildOrder> children() {
return this.children;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package codingblackfemales.sotw;

import messages.order.Side;

import messages.order.Side; // Assuming Side is defined in another package
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -11,17 +10,38 @@ public class ChildOrder {
private long orderId;
private long quantity;
private long price;
private long filledQuantity; // Store total filled quantity as long

private int state;

private List<ChildFill> fills = new LinkedList<>();
private OrderStatus orderStatus; // Field to hold the order status
private long creationTime; // Add creationTime field
private List<ChildFill> fills; // Declare the fills list

public ChildOrder(Side side, long orderId, long quantity, long price, int state) {
public ChildOrder(Side side, long orderId, long quantity, long price, OrderStatus orderStatus) {
this.side = side;
this.orderId = orderId;
this.quantity = quantity;
this.price = price;
this.state = state;
this.orderStatus = orderStatus; // Set the order status in the constructor

this.creationTime = System.currentTimeMillis(); // Initialize creation time when the order is placed
this.fills = new LinkedList<>(); // Initialize the fills list
this.filledQuantity = 0; // Initialize filled quantity
}

public long getCreationTime() {
return creationTime;
}

public void setCreationTime(long creationTime) {
this.creationTime = creationTime; // Store the creation time
}

public OrderStatus getStatus() {
return orderStatus;
}

public OrderStatus getState() { // New method to get the state
return this.orderStatus; // Return the current order status
}

public Side getSide() {
Expand All @@ -41,18 +61,29 @@ public long getPrice() {
}

public long getFilledQuantity() {
return fills.stream().map( cf -> cf.getQuantity()).collect(Collectors.summingLong(Long::longValue));
return filledQuantity; // Directly return filled quantity
}

public void setFilledQuantity(long filledQuantity) {
this.filledQuantity = filledQuantity; // Set filled quantity
}

public long getTotalFilledQuantity() {
return fills.stream().map(ChildFill::getQuantity).collect(Collectors.summingLong(Long::longValue));
}

public int getState() {
return state;
// Add a method to update order status if needed
public void setOrderStatus(OrderStatus orderStatus) {
this.orderStatus = orderStatus;
}

// New method to set state using an integer
public void setState(int state) {
this.state = state;
this.orderStatus = OrderStatus.fromInt(state); // Convert int to OrderStatus
}

public void addFill(long filledQuantity, long filledPrice) {
this.fills.add(new ChildFill(filledQuantity, filledPrice));
this.fills.add(new ChildFill(filledQuantity, filledPrice)); // Add a new fill to the list
this.filledQuantity += filledQuantity; // Update filled quantity when a new fill is added
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package codingblackfemales.sotw;

public class MarketTick {
private final int price;
private final int volume;
private final int depth;

public MarketTick(int price, int volume, int depth) {
this.price = price;
this.volume = volume;
this.depth = depth;
}

public int getPrice() {
return price;
}

public int getVolume() {
return volume;
}

public int getDepth() {
return depth;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package codingblackfemales.sotw;

public enum OrderStatus {
PENDING(1), // order is pending
ACKED(2), // order processing
CANCELED(3), // order has been cancelled
FILLED(4); // order has been filled

private final int value;

OrderStatus(int value) {
this.value = value;
}

public int getValue() {
return value;
}

// Convert integer to OrderStatus
public static OrderStatus fromInt(int value) {
for (OrderStatus status : OrderStatus.values()) {
if (status.getValue() == value) {
return status;
}
}
throw new IllegalArgumentException("Invalid OrderStatus value: " + value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ public interface SimpleAlgoState {
public List<ChildOrder> getActiveChildOrders();

public long getInstrumentId();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public List<ChildOrder> getChildOrders() {

@Override
public List<ChildOrder> getActiveChildOrders() {
return orderService.children().stream().filter(order -> order.getState() != OrderState.CANCELLED).collect(Collectors.toList());
return orderService.children().stream()
.filter(order -> order.getStatus() != OrderStatus.CANCELED) // Use getStatus() instead of getState()
.collect(Collectors.toList());
}
}
}
Loading