Profile Picture

Order with Profit and Stop Loss

Posted By gbulle 8 Years Ago
Message
Posted Tuesday May 12 2009
Hi,

RightEdge as soon as I send  with OpenPosition  a BUY LONG having a STOP LOSS and a LIMIT automatically generate, after the Fill of the ENTRY ORDER, a  pending  SELL LIMIT and a SELL STOP

..all fine.....

when, for example, there is a FILL of the LIMIT ORDER  RightEdge automatically CLOSE the position for profit  BUT does not cancel the pending SELL STOP order.

It is my own responsability to CANCEL that pending order?

Bye

GB

Posted Tuesday May 12 2009
If you have the automatic stop set in the trading system, it should be up RightEdge to cancel that stop. Also, if the stop were hit and you had a profit target order in place, that should be cancelled as well. If you're maintaining your own stops and profit targets, that's up to you to cancel.

gbulle (5/12/2009)
Hi,

RightEdge as soon as I send withOpenPositiona BUY LONG having a STOP LOSS and a LIMIT automatically generate, after the Fill of the ENTRY ORDER, a pending SELL LIMIT and a SELL STOP

..all fine.....

when, for example, there is a FILL of the LIMIT ORDER RightEdge automaticallyCLOSE the position for profit BUT does not cancel the pending SELL STOP order.

It is my own responsability to CANCEL that pending order?

Bye

GB
Posted Wednesday May 13 2009
Hi,

(below the sample code I use to send an order; nothing else in my trading system)

I see the error:

Unable to cancel order when closing position:  EUR/USD Position Manager 

when RightEdge, after having closed the position, try to cancel the pending order

Can you suggest me what to do?

Bye

GB

s1.PositionType = PositionType.Long

s1.Symbol = symbol

s1.Size = 1 

s1.OrderType = OrderType.Market

s1.StopPrice = targetPrice

s1.LimitPrice = targetPrice + 0.0002

s1.BarsValid = -1 'indefinite

s1.BarCountExit = 0 'no timed Exit

'Inizio OCO

s1.StopLoss = targetPrice - 0.001

s1.StopLossType = TargetPriceType.Price

s1.ProfitTarget = targetPrice + 0.001

s1.ProfitTargetType = TargetPriceType.Price

'Fine OCO

s1.TrailingStop = Double.NaN

s1.TrailingStopType = TargetPriceType.None

s1.Description = "G2_LONG"

s1.tag = Nothing

p2 = OpenPosition(s1)

If p2.Error IsNot Nothing Then

SystemData.Output.Add(OutputSeverityLevel.Error, "Error " + p2.Error + " stop price " + s1.StopPrice.ToString)

End If


Edited: Wednesday May 13 2009 by gbulle
Posted Wednesday May 13 2009
You're writing your own broker plugin, correct? If so, are you seeing a call to CancelOrder in your plugin before this error comes back in RightEdge? Do you know what the order ID of the stop loss is?
Posted Wednesday May 13 2009
I am just tracing the calls of your Positionmanager

I suppose your Positionmanager is synchronous...

the problem is that the FIX protocol is not synchronous..

when you cancel an order you do it directly.. I have to send a Message "cancel order" and later I receive the message "order cancelled"

but the PositionManager is not waiting me......

Right?

If I am right I have to rethink the overall structure... Probably I have to write my own Positon Manager.

Any suggestion?

Bye

GB

Posted Wednesday May 13 2009
No PositionManager is asynchronous when it comes to order processing. All interfacing with an external service such as a broker or data provider must be async. When you send a cancel order, we mark it as cancelled, or more specifically, we mark it with a "cancelpending" flag. So it should still be in the internal collections.

You certainly don't have to rewrite positionmanager. That would be quite a massive undertaking if you did.

But let me see if I follow this. You have a profit target and stop loss and one or the other is hit. You see RightEdge calling your plugin's CancelOrder() for the order that will not be filled. It follows with the error "Unable to cancel order".

Looking at the code in positionmanager, this should only happen if the call to CancelOrder fails. Are you returning false from that function? In this case, the call to your broker plugin is synchronous, but you should call OrderUpdated with a BrokerOrderState of rejected if something happens to where the cancellation doesn't occur. Until then, the system considers the order cancel pending.

gbulle (5/13/2009)
I am just tracing the calls of your Positionmanager

I suppose your Positionmanager is synchronous...

the problem is that the FIX protocol is not synchronous..

when you cancel an order you do it directly.. I have to send a Message "cancel order" and later I receive the message "order cancelled"

but the PositionManager isnot waitingme......

Right?

If I am right I have to rethink the overall structure... Probably I have to write my own Positon Manager.

Any suggestion?

Bye

GB
Posted Wednesday May 13 2009
Hi,

I enter into CancelOrder and internally I do the right things:

If openOrders.ContainsKey(orderId) Then

openOrders(orderId).OrderState = BrokerOrderState.Cancelled

openOrders.Remove(orderId)

end if

but the  trade does not disappear from the PendingPosition tab (I am 100% sure I enter into the above portion of code and I trap errors with try-catch)

so it appear that the trade has been internally removed from the dictionary

but is still existing into the PendingPosition grid

In your opinion is this possible ?

Bye

GB

Posted Wednesday May 13 2009
OK, but when the cancellation confirmation ultimately comes through on your side via the broker, you also need to update RightEdge to let us know that the cancellation was successful via the OrderUpdatedDelgate:

ex:

OrderUpdatedDelegate tmp = OrderUpdated;
if (tmp != null)
{
tmp(openOrder, fill, information);
}

where openOrder is the Order type (and points to the cancelled order), fill would be null and information you could set to Cancelled.

gbulle (5/13/2009)
Hi,

I enter into CancelOrder and internally I do the right things:

If openOrders.ContainsKey(orderId) Then

openOrders(orderId).OrderState = BrokerOrderState.Cancelled

openOrders.Remove(orderId)

end if


but the trade does not disappear from the PendingPosition tab (I am 100% sure I enter into the above portion of code and I trap errors with try-catch)

so it appear that the trade has been internally removed from the dictionary

but is still existing into the PendingPosition grid

In your opinion is thispossible ?

Bye

GB
Posted Wednesday May 13 2009
The code below does not work but probably I am near the truth:

In the CANCELORDER function I have

If openOrders.ContainsKey(orderId) Then

openOrders(orderId).OrderState = BrokerOrderState.Cancelled

return true

end if

and outside of the CancelOrder function  ,just  after having received the CANCEL confirmation from my broker,I have :

If openOrders.ContainsKey(orderId) Then

my_order = openOrders.Item(orderId)

my_order.OrderState = BrokerOrderState.Cancelled

openOrders.Remove(orderId)

RaiseEvent MyOrderUpdated(my_order, Nothing, "CANCELLED")

end if

there is something wrong because the pending order does not disappear.... have you a suggestion?

TX

Bye

GB

Posted Wednesday May 13 2009
That looks perfect.

Do you get the OrderUpdated event in the trading system for this cancellation?

Do you step into your OrderUpdated and make sure that a) my_order is not null and b) MyOrderUpdated is valid?

If it's bubbling all the way to the top and your trading system gets the cancellation confirmation, I may be able to put some debug code into RightEdge to see if the problem lies within.

But looking at our other broker plugins, this is how they behave and they're working. So I suspect we're overlooking something.

gbulle (5/13/2009)
The code below does not work but probably I am near the truth:

In the CANCELORDER function I have

If openOrders.ContainsKey(orderId) Then

openOrders(orderId).OrderState = BrokerOrderState.Cancelled

return true


end if

and outside of the CancelOrder function ,just after having received the CANCEL confirmation from my broker,I have :

If openOrders.ContainsKey(orderId) Then

my_order = openOrders.Item(orderId)

my_order.OrderState = BrokerOrderState.Cancelled

openOrders.Remove(orderId)


RaiseEvent MyOrderUpdated(my_order, Nothing, "CANCELLED")

end if

there is something wrong because the pending order does not disappear.... have you a suggestion?

TX

Bye

GB


Similar Topics


Reading This Topic


2005-2017 © RightEdge Systems