Profile Picture

Broker Service

Posted By BigBerner 10 Years Ago
Message
Posted Monday February 19 2007
I have started building a Broker Service and have a few questions.

1. Could you explain to me what the purpose of AddOrderUpdatedDelegate, AddPositionAvailableDelegate, RemoveOrderUpdatedDelegate, RemovePositionAvailableDelegate is. I couldn't really work out what I was supposed to be doing here from the sample.

2. My Broker does not have a function to return the cash in the account for GetBuyingPower. Should I just set this to 0, or will that stop orders going through? (in which case I guess I would set it to 10,000,000 or something like that)

3. My broker API only allows for limit and market orders to be placed. Am I correct in saying that this means that when I write a strategy in RE, that whilst I can use stop orders for back testing, that when I switch to live trading I will need to change my code to handle the stop from within RE. I am assuming that when a stop order is generated by a strategy in RE that it is sent directly to the broker for monitoring within the broker software, rather than being moinitored by RE and released as a market order when the level is hit. (What I am getting at here is, is the functionality of the BrokerOrderType enumeration members handled by RE, or does your broker software need to be able to handle this functionality?)

4. Really just a comment here, but I thought the BrokerTransactionType enumeration members names to be a bit confusing (Buy = Buy, Short = Sell, Sell = Close long, Cover = close short). Would have made more sense to have something like, Buy, Sell, ExitLong, ExitShort.

Posted Monday February 19 2007
BigBerner (2/19/2007)
I have started building a Broker Service and have a few questions.

1. Could you explain to me what the purpose of AddOrderUpdatedDelegate, AddPositionAvailableDelegate, RemoveOrderUpdatedDelegate, RemovePositionAvailableDelegate is. I couldn't really work out what I was supposed to be doing here from the sample.

2. My Broker does not have a function to return the cash in the account for GetBuyingPower. Should I just set this to 0, or will that stop orders going through? (in which case I guess I would set it to 10,000,000 or something like that)

3. My broker API only allows for limit and market orders to be placed. Am I correct in saying that this means that when I write a strategy in RE, that whilst I can use stop orders for back testing, that when I switch to live trading I will need to change my code to handle the stop from within RE. I am assuming that when a stop order is generated by a strategy in RE that it is sent directly to the broker for monitoring within the broker software, rather than being moinitored by RE and released as a market order when the level is hit. (What I am getting at here is, is the functionality of the BrokerOrderType enumeration members handled by RE, or does your broker software need to be able to handle this functionality?)

4. Really just a comment here, but I thought the BrokerTransactionType enumeration members names to be a bit confusing (Buy = Buy, Short = Sell, Sell = Close long, Cover = close short). Would have made more sense to have something like, Buy, Sell, ExitLong, ExitShort.

1.  You should keep a list of OrderUpdatedDelegates and PositionAvailableDelegates in your plugin.  The add and remove methods should add or remove items from the lists.  When an order is filled, canceled, or in any other case where an order is updated, you should call all of the delegates in the OrderUpdatedDelegate list.  The PositionAvailableDelegate is used to communicate the size of each currently open holding.  The PositionAvailableDelegate is not really used right now, but you will need to make sure the OrderUpdatedDelegates are working or RightEdge will never know what happens to the orders you submit.

2.  You're saying the broker has no way of letting you know programmatically what the size of your account is?  If it's not implemented as a function you can call, it may be a message you receive every so often with account information.  If you don't have any access to this value, it's probably better to return something like $10,000.  Be aware that if you are using a percentage-based position sizing, the position sizes will be based on this value.

3.  You are correct that it is up to the broker to implement the various order types, such as stops, limits, etc.  If your broker does not support some of the order types you would like to use, you could implement those types in your broker plugin, and you wouldn't have to change your system code.  For this to work your broker would probably have to provide you with current price information (so your broker plugin would probably also be a live data plugin).  If a stop limit order was submitted to the broker plugin, it would monitor the price of the symbol, and when it fell below a certain level, it would submit that order as a market order.

4.  I understand, but I'm not sure if it's possible to make perfect sense to everyone here Smile.  If someone doesn't use short positions at all, then they will just use Buy and Sell, which seems to make sense.

Thanks,
Daniel

Posted Monday February 19 2007
Thanks,

I am still not clear on what exactly I am doing with the delegates. For example, lets say that my broker's OnOrderFilled Event fires to let me know that an order has been filled. I grab the information about the order that has been filled, and then what do I do? How do I let RE know what has happened. (As much detail as possible would be appeciated!)

Posted Monday February 19 2007
Here is some code that shows how you can use an event to implement this.  You could also just use a list of OrderUpdatedDelegates.  In that case, when the event occured, you would loop through each delegate in the list and call it.

protected event OrderUpdatedDelegate OrderUpdated;

public void AddOrderUpdatedDelegate(OrderUpdatedDelegate orderUpdated)

{

OrderUpdated += orderUpdated;

}

public void RemoveOrderUpdatedDelegate(OrderUpdatedDelegate orderUpdated)

{

OrderUpdated -= orderUpdated;

}

// This function would be called when an order is filled

public void OnOrderFilled(string orderId, double price)

{

Order order;

if (openOrders.TryGetValue(orderId, out order))

{

order.OrderState = BrokerOrderState.Filled;

order.FillPrice = price;

openOrders.Remove(orderId);

OrderUpdated(order, BrokerOrderState.Filled);

}

}

Of course, an actual order filled function would probably do more work, such as setting the filled date for example.  You should also call the OrderUpdated event when an order is canceled, rejected, etc.

Let us know if you need more explanation.

Thanks,
Daniel

Posted Monday February 19 2007
Still lost on this. Whenever I see delegates my mind goes hazy - I have used them for threading before, but it is not one of my strongest points. I am going to need a step by step explanation of what I need to do, apologies but I am a trader that writes code out of necessity.

So the aim is to tell RE that something has happened to an order, i.e it got filled. So I have an event something like this

Private Sub myOrderBook_OnOrderFill(ByVal orderID as integer, ByVal FilledQuantity as integer, ByVal Status as OrderStatus)   Handles myOrderBook.OnOrderFill

End Sub

So what do I do next?

Posted Thursday February 22 2007
Does anyone want to have a crack at this? Whistling  Any help is appeciated.

Edited: Thursday February 22 2007 by BigBerner
Posted Thursday February 22 2007
BigBerner (2/22/2007)
Does anyone want to have a crack at this? Whistling  Any help is appeciated.

I'm going to try to get you some sample code in VB.  The problem is I don't usually program in Visual Basic so I'm not sure what the syntax for delegates and events is Smile  I haven't forgotten about you though!

Daniel

phg
Supreme Being (44,857 reputation)Supreme Being (44,857 reputation)Supreme Being (44,857 reputation)Supreme Being (44,857 reputation)Supreme Being (44,857 reputation)Supreme Being (44,857 reputation)Supreme Being (44,857 reputation)Supreme Being (44,857 reputation)Supreme Being (44,857 reputation)
Posted Sunday February 25 2007

I too am interested in how this works. I need to track when (DateTime) the most recent buy occures for each symbol in order the implement pacing: no more than one buy in some interval. There are a couple of things that are not clear to me about the code above.

1. Where is orderUpdated used? It can't be to point to OnOderFilled because the signature is wrong?

2. What causes OnOrderFilled to be called?

3. I don't think I need any of the body of OnOrderFilled. All I want to do is to set the DateTime for that fill for that symbol in one of my variables. Is it reguired that what is shown be implemented?

4. What is the last line doing? It seems like it is calling the delegate, but why?

Inquiring, but inexperienced, minds want to know Smile A simple working example would probably be most helpful, in C# or VB.

-Pete



-Pete

See also Yahoo group about applying RE.

phg
Supreme Being (44,857 reputation)Supreme Being (44,857 reputation)Supreme Being (44,857 reputation)Supreme Being (44,857 reputation)Supreme Being (44,857 reputation)Supreme Being (44,857 reputation)Supreme Being (44,857 reputation)Supreme Being (44,857 reputation)Supreme Being (44,857 reputation)
Posted Sunday February 25 2007

I'm beginning to think I was making things more complicated than they really are. I put this under Startup():

PositionManager.OrderFilled += new EventHandler(whenFilled); // Designate filled handler.

This as a sub:

private void whenFilled(Trade trade, Position pos, State state)

{

}

The compiler complained because 'State' could not be found. So we changed that to Position.State.

But now there is no overload for whenFilled.

I think I am on the right track, but I'm missing a point or two some place ???

(Thanks for your help)

(The new developer guide has less cross-referencing than before and that is making life a bit more difficult.)

-Pete



-Pete

See also Yahoo group about applying RE.


Edited: Sunday February 25 2007 by phg
Posted Wednesday February 28 2007
Here is some sample VB code for how you can use the order updated event in a broker plugin.

If you call the OnOrderFilled method from your myOrderBook_OnOrderFill Sub, then the message should get back to RE that your order was filled.  RightEdge subscribes to the order updated event by calling AddOrderUpdatedDelegate, so when you raise MyOrderUpdated, RightEdge gets the information.

' These events are fired when the appropriate actions occur. These events

' are subscribed to by the RightEdge user interface to update the user

' when something of note happens on the broker.

Public Event MyOrderUpdated As OrderUpdatedDelegate

Public Event MyPositionAvailable As PositionAvailableDelegate

Public Sub AddOrderUpdatedDelegate(ByVal orderUpdated As RightEdge.Common.OrderUpdatedDelegate) _

Implements RightEdge.Common.IBroker.AddOrderUpdatedDelegate

AddHandler MyOrderUpdated, orderUpdated

End Sub

Public Sub AddPositionAvailableDelegate(ByVal positionAvailable As RightEdge.Common.PositionAvailableDelegate) _

Implements RightEdge.Common.IBroker.AddPositionAvailableDelegate

AddHandler MyPositionAvailable, positionAvailable

End Sub

Public Sub RemoveOrderUpdatedDelegate(ByVal orderUpdated As RightEdge.Common.OrderUpdatedDelegate) _

Implements RightEdge.Common.IBroker.RemoveOrderUpdatedDelegate

RemoveHandler MyOrderUpdated, orderUpdated

End Sub

Public Sub RemovePositionAvailableDelegate(ByVal positionAvailable As RightEdge.Common.PositionAvailableDelegate) _

Implements RightEdge.Common.IBroker.RemovePositionAvailableDelegate

RemoveHandler MyPositionAvailable, positionAvailable

End Sub

Private Sub OnOrderFilled(ByVal orderID As String, ByVal price As Double)

Dim myOrder As Order = Nothing

If (openOrders.TryGetValue(orderID, myOrder)) Then

myOrder.OrderState = BrokerOrderState.Filled

myOrder.FillPrice = price

openOrders.Remove(orderID)

RaiseEvent MyOrderUpdated(myOrder, myOrder.OrderState)

End If

End Sub

Hopefully this is helpful.  Let us know if you need more explanation.

Thanks,
Daniel



Similar Topics


Reading This Topic


2005-2017 © RightEdge Systems