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
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;
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]
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]
Mike