Reversing positions  [SOLVED]

Questions about MultiCharts .NET and user contributed studies.
eroleyikan
Posts: 22
Joined: 01 Jun 2016
Has thanked: 4 times
Been thanked: 6 times

Reversing positions  [SOLVED]

Postby eroleyikan » 29 Jun 2016

Hello,
I have a piece of code that reverses my position at the end of a bar.

Orders for exit from the existing position and entry into the new position are sent together at the same time. The code is below.
This code works MOST of the time as expected. For example, if I am 30K short, I see ONE order to buy 60K that puts me in a 30K long position.

But sometimes, ONLY the exit order is placed and not the entry. I don't see any rejected or cancelled orders or error messages. I am at a complete loss in debugging this issue. I do not see any pattern to identify why this is happening. Does anyone have any idea how I can find and address this issue?

Here is the code that sends the orders:

Code: Select all

protected override void Create()
{
Output.Clear();

//entry orders, when bars switch direction
m_LongEntryOrder = OrderCreator.MarketThisBar(new SOrderParameters(Contracts.UserSpecified, EOrderAction.Buy));
m_ShortEntryOrder = OrderCreator.MarketThisBar(new SOrderParameters(Contracts.UserSpecified, EOrderAction.SellShort));

//protect and profit orders when we have a LONG entry
m_ProtectOrderSell = OrderCreator.Stop(new SOrderParameters(Contracts.UserSpecified, "P-Sell", EOrderAction.Sell, OrderExit.Total));
m_LockOrderSell = OrderCreator.Limit(new SOrderParameters(Contracts.UserSpecified, "L-Sell", EOrderAction.Sell, OrderExit.Total));

//protect and profit orders when we have a SHORT entry
m_ProtectOrderBuy = OrderCreator.Stop(new SOrderParameters(Contracts.UserSpecified, "P-Buy", EOrderAction.BuyToCover, OrderExit.Total));
m_LockOrderBuy = OrderCreator.Limit(new SOrderParameters(Contracts.UserSpecified, "L-Buy", EOrderAction.BuyToCover, OrderExit.Total));

//Exit orders when bars switch direction
m_LongExitOrder = OrderCreator.MarketThisBar(new SOrderParameters(EOrderAction.Sell, "LongExit"));
m_ShortExitOrder = OrderCreator.MarketThisBar(new SOrderParameters(EOrderAction.BuyToCover, "ShortExit"));
}

protected void SendEntryOrder(EOrderAction EntryAction)
{
switch (EntryAction)
{
case EOrderAction.Buy:
m_ShortExitOrder.Send(CurrentPos);
m_LongEntryOrder.Send(EntryTradeSize);
m_LockOrderSell.Send(getProfitTarget(), LockInTradeSize);
m_ProtectOrderSell.Send(getStopTarget(), EntryTradeSize);

break;
case EOrderAction.SellShort:
m_LongExitOrder.Send(CurrentPos);
m_ShortEntryOrder.Send(EntryTradeSize);
m_LockOrderBuy.Send(getProfitTarget(), LockInTradeSize);
m_ProtectOrderBuy.Send(getStopTarget(), EntryTradeSize);

break;
default:
break;
}
}
Thank you all in advance!
Erol

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Reversing positions

Postby Henry MultiСharts » 01 Jul 2016

Hello Erol,

Please continue the analysis with Alex directly.

eroleyikan
Posts: 22
Joined: 01 Jun 2016
Has thanked: 4 times
Been thanked: 6 times

Re: Reversing positions

Postby eroleyikan » 08 Jul 2016

I worked with Dave from MC on this issue. I just wanted to post a follow up on this thread in case the information is useful for others.

It turns out, in the code I posted above, the explicit exit orders are not required. When you send an entry order, the previous position is closed automatically. At least that's the expected functionality. I took the exit orders out of the code, now the reversal seems to be working.

I hope this helps to others.


Return to “MultiCharts .NET”