Close partial postions  [SOLVED]

Questions about MultiCharts .NET and user contributed studies.
christephan
Posts: 17
Joined: 04 Dec 2014
Has thanked: 2 times
Been thanked: 3 times

Close partial postions

Postby christephan » 20 Dec 2014

I meet some problems to close part of my position.

Sell order always sell all of my Long position. Looks using Contracts.UserSpecified in Create() doesn't help~

For example,

Code: Select all

BuyOrder1.Send ("Buy1", 1); //at time 1
BuyOrder2.Send ("Buy2", 1); //at time 2
SellOrder1.Send ("Sell1", 1); //at time 3 - Will sell all 2 lots even if I specify "1"
It will always sell all position instead of the amount I specified. Any idea?

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: Close partial postions

Postby JoshM » 21 Dec 2014

With `OrderExit.FromEntry()` you can specify which entry order should be closed by an exit order.

For example:

Code: Select all

using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;
using ATCenterProxy.interop;

namespace PowerLanguage.Strategy
{
public class Snippet_Strategy : SignalObject
{
public Snippet_Strategy(object _ctx) : base(_ctx) { }

private IOrderMarket enterLong1, enterLong2, exitLong1, exitLongAll;

protected override void Create()
{
enterLong1 = OrderCreator.MarketNextBar(new SOrderParameters(Contracts.UserSpecified, EOrderAction.Buy));
enterLong2 = OrderCreator.MarketNextBar(new SOrderParameters(Contracts.UserSpecified, EOrderAction.Buy));

exitLong1 = OrderCreator.MarketNextBar(new SOrderParameters(Contracts.UserSpecified, EOrderAction.Sell, OrderExit.FromEntry(enterLong1)));
exitLongAll = OrderCreator.MarketNextBar(new SOrderParameters(Contracts.UserSpecified, EOrderAction.Sell, OrderExit.FromEntry(enterLong2)));
}

protected override void CalcBar()
{
if (Bars.CurrentBar == 100)
enterLong1.Send("Buy1", 1);
else if (Bars.CurrentBar == 110)
enterLong2.Send("Buy2", 1);
else if (Bars.CurrentBar == 120)
exitLong1.Send("Sell1", 1);
else if (Bars.CurrentBar == 130)
exitLongAll.Send("Sell2", 1);
}
}
}
Gives the following:

Image
Attachments
scr.21-12-2014 08.26.45.png
(3.56 KiB) Downloaded 2147 times

christephan
Posts: 17
Joined: 04 Dec 2014
Has thanked: 2 times
Been thanked: 3 times

Re: Close partial postions

Postby christephan » 21 Dec 2014

Thanks Josh~ Will try it out~

Looks it should be specified in Create()? Is it possible to specify which position to close at run-time, in CalcBar()?

I have a lot of signals generating Buy and SellShort orders.
And have another monitor signal to determine when to sell (or cover) a position. Where this monitor signal doesn't have the order objects from other signals which generated the positions.

For example, in simple, to stop a long position if it losses more than 50 points:

Code: Select all

protected override void CalcBar() {
for (int i = 0; i < CurrentPosition.OpenTrades.Count; i++) {
if (CurrentPosition.OpenTrades[i].IsLong) {
if (Bars.CloseValue - CurrentPosition.OpenTrades[i].EntryOrder.Price < -50)
SellOrder.Send (CurrentPosition.OpenTrades[i].EntryOrder.Contracts);
}
}
}
The above code will sell all position. But I want to keep other positions running until they also meet the criteria.
Is it possible to make it work?

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: Close partial postions

Postby JoshM » 21 Dec 2014

Looks it should be specified in Create()? Is it possible to specify which position to close at run-time, in CalcBar()?
Good question, I'm not sure about this, but don't think so. I don't see an overloaded `Send()` method for that, and perhaps in real-time the TradeManager can be used for that, but that wouldn't help with backtesting. Perhaps Henry (or someone else) might have an idea.

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

Re: Close partial postions

Postby Henry MultiСharts » 22 Dec 2014

Hello christephan,

Contracts.UserSpecified - Specifies the amount of contracts in the code. If you do not specify the amount to buy/sell in the code then the value specified in Trade Size of the Properties tab of the Strategy Properties would be used. Example.

OrderExit.FromEntry - Completely exits from the specified entry. Example above and here.

OrderExit.Total - Used to indicate that only the number of contracts specified by the numerical expression to be sold or covered in total, regardless of the number of open entries. The contracts or shares will be sold or covered in the same order they were bought or shorted: First In, First Out. If the word Total is not used, the number of contracts or shares specified by the numerical expression will be sold or covered for each one of the open entries.
Example.

If you do not want to create each order (name) individually in Create() - you can create the required order types, then dynamically assign order names in CalcBar():

Code: Select all

....
protected override void Create()
{
buy_order = OrderCreator.MarketNextBar(new SOrderParameters(Contracts.Default, "order", EOrderAction.Buy));
}

protected override void CalcBar()
{
var _cb = Bars.CurrentBar.ToString();
buy_order.Send("b_" + _cb);
}
...
Additional info about orders in the Wiki: 1, 2.

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: Close partial postions

Postby JoshM » 23 Dec 2014

Just out of a willingness to learn this myself, which of those mentioned links Henry address the quote below? As I understand it, none of them, but it would be quite helpful if this were possible:
Looks it should be specified in Create()? Is it possible to specify which position to close at run-time, in CalcBar()?
As I read this: without the `OrderExit.FromEntry` in the `Create()` method. Which doesn't seem possible, or at least is undocumented.

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

Re: Close partial postions

Postby Henry MultiСharts » 23 Dec 2014

Attached is a sample code and workspace that partially exits the position without the OrderExit.FromEntry.
Attachments
PartialClose.zip
(16.19 KiB) Downloaded 390 times

christephan
Posts: 17
Joined: 04 Dec 2014
Has thanked: 2 times
Been thanked: 3 times

Re: Close partial postions

Postby christephan » 23 Dec 2014

Thanks Henry,

What does this means exactly?

Code: Select all

[ExitFromOneEntryOnce(ExitFromOneEntryOnce.FillAndKillExitOrder)]
Looks it's still not possible to choose which position to close.
That is, if I Buy 3 orders first,
Buy.Send ("A");
Buy.Send ("B");
Buy.Send ("C");
And then I want to close "B". Looks it's not possible in current version. right?

I think the EL version support this feature but not for .NET?
For now, I am going to write code to maintain my own copy of opening trades to fulfill what I want~

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

Re: Close partial postions

Postby Henry MultiСharts » 24 Dec 2014

ExitFromOneEntryOnce Enumeration - Describes mode for processing exit orders.

FillAndKillExitOrder - Any one exit order can be applied an unlimited number of times to one entry. After this exit filled it cannot be applied again until the script recalculates and generates again. Example has been provided in my previous post.

StandardCalculation - Default. Any one exit order cannot be applied to one entry more than once. Exit order that was filled can be generated again and applied to a different entry. This mode requires individual code line for each exit:

Code: Select all

SellOrder1.Send(1);
SellOrder2.Send(1);
SellOrder3.Send(1);
....
SellOrderN.Send(1);
If you want to exit from a particular entry - ExitFromEntry command is the only way to do that, and it is required to have an order created in Create ().
Still all supported brokers are using FIFO accounting, so ExitFromEntry command is mainly for internal strategy logic / visual representation of information.

christephan
Posts: 17
Joined: 04 Dec 2014
Has thanked: 2 times
Been thanked: 3 times

Re: Close partial postions

Postby christephan » 06 Feb 2015

Hi,

Partial exit doesn't work as expected. Am I missing something?

Here is code:

Code: Select all

using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;
using ATCenterProxy.interop;

namespace PowerLanguage.Strategy {
public class Test2 : SignalObject {
public Test2(object _ctx):base(_ctx){}

private IOrderMarket orderLE, orderLX;

protected override void Create() {
orderLE = OrderCreator.MarketThisBar(new SOrderParameters(Contracts.UserSpecified, EOrderAction.Buy));
orderLX = OrderCreator.MarketThisBar(new SOrderParameters(Contracts.UserSpecified, EOrderAction.Sell, OrderExit.Total));
}
protected override void CalcBar(){
if (Bars.CurrentBar == 1) {
orderLE.Send(3);
} else {
orderLX.Send(1);
}
}
}
}
It is expected to Buy 3 lots at first bar, and then to sell 1 lot each in second, third, and fourth. But it doesn't sell at third and fourth bar. (See attachment)
Untitled.png
(4.45 KiB) Downloaded 1901 times

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: Close partial postions  [SOLVED]

Postby JoshM » 07 Feb 2015

Hi,

Partial exit doesn't work as expected. Am I missing something?

It is expected to Buy 3 lots at first bar, and then to sell 1 lot each in second, third, and fourth. But it doesn't sell at third and fourth bar.
Your code is working as expected. That's because, by default, you'll need to create an exit order for each order that scales out of the position.

So for scaling out of a position with three exit orders, something like this is needed (untested I must say):

Code: Select all

using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;
using ATCenterProxy.interop;
using PowerLanguage.TradeManager;

namespace PowerLanguage.Strategy
{
public class Snippet_Strategy : SignalObject
{
public Snippet_Strategy(object _ctx) : base(_ctx) { }

private IOrderMarket orderLE, orderLX1, orderLX2, orderLX3;

protected override void Create()
{
orderLE = OrderCreator.MarketThisBar(new
SOrderParameters(Contracts.UserSpecified, EOrderAction.Buy));

orderLX1 = OrderCreator.MarketThisBar(new
SOrderParameters(Contracts.UserSpecified, EOrderAction.Sell));

orderLX2 = OrderCreator.MarketThisBar(new
SOrderParameters(Contracts.UserSpecified, EOrderAction.Sell));

orderLX3 = OrderCreator.MarketThisBar(new
SOrderParameters(Contracts.UserSpecified, EOrderAction.Sell));
}

protected override void CalcBar()
{
if (Bars.CurrentBar == 1)
{
orderLE.Send(3);
}
else if (Bars.CurrentBar == 3)
{
orderLX1.Send(1);
}
else if (Bars.CurrentBar == 5)
{
orderLX2.Send(1);
}
else if (Bars.CurrentBar == 7)
{
orderLX3.Send(1);
}
}
}
}
This is, of course, a little bit unwieldy if you have a lot of exit orders. An alternative would be to use the ExitFromOneEntryOnce attribute. Then exit orders can be reapplied to the same entry an unlimited times. With that attribute, the code above becomes the following (untested):

Code: Select all

using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;
using ATCenterProxy.interop;
using PowerLanguage.TradeManager;

namespace PowerLanguage.Strategy
{
[ExitFromOneEntryOnce(ExitFromOneEntryOnce.FillAndKillExitOrder)]
public class Snippet_Strategy : SignalObject
{
public Snippet_Strategy(object _ctx) : base(_ctx) { }

private IOrderMarket orderLE, orderLX;

protected override void Create()
{
orderLE = OrderCreator.MarketThisBar(new
SOrderParameters(Contracts.UserSpecified, EOrderAction.Buy));

orderLX = OrderCreator.MarketThisBar(new
SOrderParameters(Contracts.UserSpecified, EOrderAction.Sell));
}

protected override void CalcBar()
{
if (Bars.CurrentBar == 1)
{
orderLE.Send(3);
}
else if (Bars.CurrentBar == 3 ||
Bars.CurrentBar == 5 ||
Bars.CurrentBar == 7)
{
orderLX.Send(1);
}
}
}
}


Return to “MultiCharts .NET”