Scaling Out of a Position  [SOLVED]

Questions about MultiCharts and user contributed studies.
User avatar
fbertram
Posts: 166
Joined: 16 Oct 2014
Location: Seattle, USA
Has thanked: 36 times
Been thanked: 73 times
Contact:

Scaling Out of a Position

Postby fbertram » 01 Apr 2016

I am experimenting with a strategy, that is scaling in and out of a position, instead of opening and closing it completely. This simplified code should scale in and out of a position, and keep the total investment for the strategy close to $100k:

Code: Select all

variables:
DeltaShares (0),
SharesFor100k (0);

once ClearPrintLog;

if date > date[1] then begin
print(ELDateToString(date), " ", SymbolName, " details:");
print("SharesFor100k = ", SharesFor100k);
SharesFor100k = ceiling(100000 / close);
DeltaShares = SharesFor100k - CurrentShares;
if DeltaShares > CurrentShares * 0.01 then begin
print("++ ", DeltaShares);
buy("LE") DeltaShares shares next bar at market;
end else if -DeltaShares > CurrentShares * 0.01 then begin
print("-- ", -DeltaShares);
sell absvalue(DeltaShares) shares total next bar at market;
end;
end;
I have enabled multiple entry orders in the same direction:

Image

Unfortunately, the resulting trades w/ AAPL look like this:

Image

It can be seen that the code is not working: instead of scaling the number of shares back to stay close to a $100k investment, the code fails to sell properly.

This is a topic that has been discussed on this forum several times and I feel stupid about asking again. I am trying to use an 'untied exit', according to this post:
viewtopic.php?f=1&t=48438&hilit=sell+partial+position

There are also other posts, which seem to suggest different approaches:
* viewtopic.php?f=1&t=48200&hilit=sell+partial+position
* viewtopic.php?f=1&t=7827&hilit=sell+partial+position

What am I missing?

Thanks a lot for your help,
best regards, Felix
Attachments
strategy_properties.png
strategy_properties.png (41.44 KiB) Viewed 538 times
list_of_trades.png
list_of_trades.png (38.75 KiB) Viewed 535 times

User avatar
fbertram
Posts: 166
Joined: 16 Oct 2014
Location: Seattle, USA
Has thanked: 36 times
Been thanked: 73 times
Contact:

Re: scaling out of a position  [SOLVED]

Postby fbertram » 02 Apr 2016

ok, found it. The magic line is:

Code: Select all

[SameExitFromOneEntryOnce = false]
Without this setting, any given entry cannot have more than one exit associated with it. I posted the working example in the 'user contributed studies library':
viewtopic.php?f=5&t=49635


Cheers, Felix

User avatar
JoshM
Posts: 2099
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1526 times
Been thanked: 1502 times
Contact:

Re: scaling out of a position

Postby JoshM » 19 Apr 2016

ok, found it. The magic line is:

Code: Select all

[SameExitFromOneEntryOnce = false]
Without this setting, any given entry cannot have more than one exit associated with it.
That's true, unless you create several exit orders for the same entry like this:

Code: Select all

if (CurrentContracts = 10) then
Sell ("XL 1") 2 contracts next bar at market
else if (CurrentContracts = 8) then
Sell ("XL 2") 2 contracts next bar at market
else if (CurrentContracts = 6) then
Sell ("XL 3") 2 contracts next bar at market
else if (CurrentContracts = 4) then
Sell ("XL 4") 2 contracts next bar at market
else if (CurrentContracts = 2) then
Sell ("XL 5") 2 contracts next bar at market;
So `SameExitFromOneEntryOnce` is not necessary when scaling out of a position, but coding all of those separate exit orders do require a lot of additional coding. If you only want to use one exit order (like in your example), then `SameExitFromOneEntryOnce` is necessary when scaling out.


Return to “MultiCharts”