multiple partial exits from same order line ignored  [SOLVED]

Questions about MultiCharts and user contributed studies.
mjm
Posts: 28
Joined: 22 Aug 2012
Has thanked: 9 times
Been thanked: 4 times

multiple partial exits from same order line ignored

Postby mjm » 10 Oct 2012

(Multicharts 64-bit version 8.0.5622.401)

Given an existing position, I find that exit orders that "leg out" sometimes work but sometimes are partially ignored, depending on whether the exit orders come from the same or different PowerLanguage statement lines. It seems to me that the two variations should be functionally equivalent so I am wondering whether this is by design and how I can be sure my orders fill as intended.

I've created a sample Strategy script to demonstrate what I mean. It accepts a "mode" input to control how exit orders are placed:
  • mode=1 Use two distinct "Sell" lines with literal quantities in each
    mode=2 Use a single "Sell" line with a variable quantity of the same amount as mode 1
To try out this sample, create a PortfolioBacktester project with the Strategy Properties set to "Allow up to X entry orders in the same direction, regardless of the entry that generated the order" with X > 1. Add one instrument with at least 5 bars of quote data.

In both cases the strategy opens a position of 6 shares, then on the next bar adds 1 more share for a total of 7. This yields two open entries of size [6 1]. Then it sells 3 shares total; these come from the first entry, so the new position by entry is [3 1]. On the last bar it tries to sell the remaining 4 shares. In mode=1, the behavior is as expected and we close both entries for a final position of [0 0]. In mode=2, only the shares in the second entry are sold; the first entry is blocked from being sold, and our final position is [3 0].

It appears that when a PowerLanguage order line partially exits from an open entry, that particular PowerLanguage order line is not allowed to operate on that particular open entry again. I tried a simpler version of the experiment where we open only one entry with a single order of 7 shares; in that case the first exit order for 3 shares fills as desired but the second exit order for the remaining 4 shares is ignored completely. So "legging-in" to the position is not required to show the issue, but having two entries illustrates how the exit behavior depends on the composition of the existing position.

Code: Select all

Inputs:
mode(1);

Variables:
q(0);

Once ClearPrintLog;
Once Print("Mode: ", mode:0:0);

if BarNumber < 10 then begin
Print(Newline + "======== " + GetSymbolName +
" Bar " + NumToStr(CurrentBar,0) +
" Position " + NumToStr(MarketPosition * CurrentShares,0) +
" OpenEntries [" + NumToStr(OpenEntryContracts(0),0) +
" " + NumToStr(OpenEntryContracts(1),0) +
" " + NumToStr(OpenEntryContracts(2),0) +
" " + NumToStr(OpenEntryContracts(3),0) +
" " + NumToStr(OpenEntryContracts(4),0) + "]");

if BarNumber = 1 then begin
Print("Buy 6 shares this bar close");
Buy 6 shares this bar close;
end;
if BarNumber = 2 then begin
Print("Buy 1 shares this bar close");
Buy 1 shares this bar close;
end;

if mode = 1 then begin
if BarNumber = 3 then begin
Print("Sell 3 shares total this bar close");
Sell 3 shares total this bar close;
end;
if BarNumber = 4 then begin
Print("Sell 4 shares total this bar close");
Sell 4 shares total this bar close;
end;
end;

if mode = 2 then begin
if BarNumber = 3 or BarNumber = 4 then begin
q = BarNumber;
Print("Sell ", q:0:0, " shares total this bar close");
Sell q shares total this bar close;
end;
end;
end;
Output when mode=1:

Code: Select all

Mode: 1

======== SPY Bar 1 Position 0 OpenEntries [0 0 0 0 0]
Buy 6 shares this bar close

======== SPY Bar 2 Position 6 OpenEntries [6 0 0 0 0]
Buy 1 shares this bar close

======== SPY Bar 3 Position 7 OpenEntries [6 1 0 0 0]
Sell 3 shares total this bar close

======== SPY Bar 4 Position 4 OpenEntries [3 1 0 0 0]
Sell 4 shares total this bar close

======== SPY Bar 5 Position 0 OpenEntries [0 0 0 0 0]

======== SPY Bar 6 Position 0 OpenEntries [0 0 0 0 0]

======== SPY Bar 7 Position 0 OpenEntries [0 0 0 0 0]

======== SPY Bar 8 Position 0 OpenEntries [0 0 0 0 0]

======== SPY Bar 9 Position 0 OpenEntries [0 0 0 0 0]
Output when mode=2:

Code: Select all

Mode: 2

======== SPY Bar 1 Position 0 OpenEntries [0 0 0 0 0]
Buy 6 shares this bar close

======== SPY Bar 2 Position 6 OpenEntries [6 0 0 0 0]
Buy 1 shares this bar close

======== SPY Bar 3 Position 7 OpenEntries [6 1 0 0 0]
Sell 3 shares total this bar close

======== SPY Bar 4 Position 4 OpenEntries [3 1 0 0 0]
Sell 4 shares total this bar close

======== SPY Bar 5 Position 3 OpenEntries [3 0 0 0 0]

======== SPY Bar 6 Position 3 OpenEntries [3 0 0 0 0]

======== SPY Bar 7 Position 3 OpenEntries [3 0 0 0 0]

======== SPY Bar 8 Position 3 OpenEntries [3 0 0 0 0]

======== SPY Bar 9 Position 3 OpenEntries [3 0 0 0 0]
Thanks,
Mike

mjm
Posts: 28
Joined: 22 Aug 2012
Has thanked: 9 times
Been thanked: 4 times

Re: multiple partial exits from same order line ignored

Postby mjm » 10 Oct 2012

So "legging-in" to the position is not required to show the issue, but having two entries illustrates how the exit behavior depends on the composition of the existing position.
A simple illustration of this is below:

Code: Select all

Once ClearPrintLog;

if BarNumber < 10 then begin
Print(Newline + "======== " + GetSymbolName +
" Bar " + NumToStr(CurrentBar,0) +
" Position " + NumToStr(MarketPosition * CurrentShares,0) +
" OpenEntries [" + NumToStr(OpenEntryContracts(0),0) +
" " + NumToStr(OpenEntryContracts(1),0) +
" " + NumToStr(OpenEntryContracts(2),0) +
" " + NumToStr(OpenEntryContracts(3),0) +
" " + NumToStr(OpenEntryContracts(4),0) + "]");

If BarNumber = 1
then Buy 10 shares this bar close;

If BarNumber > 1 and MarketPosition > 0
then Sell 1 shares total this bar close;
end;
The first exit order fills but all subsequent orders are ignored:

Code: Select all

======== SPY Bar 1 Position 0 OpenEntries [0 0 0 0 0]

======== SPY Bar 2 Position 10 OpenEntries [10 0 0 0 0]

======== SPY Bar 3 Position 9 OpenEntries [9 0 0 0 0]

======== SPY Bar 4 Position 9 OpenEntries [9 0 0 0 0]

======== SPY Bar 5 Position 9 OpenEntries [9 0 0 0 0]

======== SPY Bar 6 Position 9 OpenEntries [9 0 0 0 0]

======== SPY Bar 7 Position 9 OpenEntries [9 0 0 0 0]

======== SPY Bar 8 Position 9 OpenEntries [9 0 0 0 0]

======== SPY Bar 9 Position 9 OpenEntries [9 0 0 0 0]
-Mike

mjm
Posts: 28
Joined: 22 Aug 2012
Has thanked: 9 times
Been thanked: 4 times

Re: multiple partial exits from same order line ignored

Postby mjm » 10 Oct 2012

To crystallize the observations above, note that the following two strategies generate different trades:

Code: Select all

If BarNumber = 1 then Buy 10 shares this bar close;
If BarNumber = 2 then Sell 5 shares total this bar close;
If BarNumber = 3 then Sell 5 shares total this bar close;

Code: Select all

If BarNumber = 1 then Buy 10 shares this bar close;
If BarNumber = 2 or BarNumber = 3 then Sell 5 shares total this bar close;

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

Re: multiple partial exits from same order line ignored  [SOLVED]

Postby Henry MultiСharts » 11 Oct 2012

Hello Mike,

Here is how it currently works (MultiCharts 8.0 and below):
[SameExitFromOneEntryOnce = true]
Standard EasyLanguage Calculation (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.

In MultiCharts 8.1 a new mode for processing exit orders would be added to suit your needs:
[SameExitFromOneEntryOnce = false]
Fill and Kill Exit Order - any one exit order can be applied an unlimited number of times to one entry. After this exit is filled it cannot be applied again until the script recalculates and generates it again.

You can switch between the modes for processing exit orders using the signal attribute [SameExitFromOneEntryOnce = True/False]


Return to “MultiCharts”