scaling into and out of positions

Studies that have been contributed to the community by other users. If you’ve got something useful to share, that’s great!
fbertram
Posts: 128
Joined: 16 Oct 2014
Has thanked: 37 times
Been thanked: 51 times

scaling into and out of positions

Postby fbertram » 02 Apr 2016

This example shows a simple strategy that will keep the total investment close to $100k by scaling into and out of positions:

Code: Select all

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

[SameExitFromOneEntryOnce = false]

once ClearPrintLog;

if date > date[1] then begin
   print(ELDateToString(date), " ", SymbolName, ": ",
      CurrentShares:4:0, " x ", close:6:2, " = ",
      (CurrentShares * close):9:2);
   SharesFor100k = ceiling(100000 / close);
   DeltaShares = SharesFor100k - CurrentShares;
   if DeltaShares > CurrentShares * 0.01 then begin
      print("++ ", DeltaShares:0:0);
      buy DeltaShares shares next bar at market;
   end else if -DeltaShares > CurrentShares * 0.01 then begin
      print("-- ", -DeltaShares:0:0);
      sell absvalue(DeltaShares) shares total next bar at market;
   end;
end;


There are a few important things to notice here:

(1) Allow multiple orders in the same direction as the currently held position
Without this setting, MultiCharts will be unable to scale into positions properly. See this screenshot:

Image

(2) [SameExitFromOneEntryOnce = false]
Without this setting, MultiCharts will not allow more than one exit per entry. Scaling out of positions will fail.

(3) sell xxx shares total
Without the keyword total, the specified # of shares will be applied to each entry, which is not what we want.


The strategy will print log output like this, to help follow its operation. The ++ and -- lines indicate buy or sell orders:

    01/04/2005 AAPL: 0 x 4.23 = 0.00
    ++ 23641
    01/05/2005 AAPL: 23641 x 4.31 = 101892.71
    -- 439
    01/06/2005 AAPL: 23202 x 4.27 = 99072.54
    01/07/2005 AAPL: 23202 x 4.33 = 100464.66
    01/10/2005 AAPL: 23202 x 4.61 = 106961.22
    -- 1510
    01/11/2005 AAPL: 21692 x 4.55 = 98698.60
    ++ 287
    01/12/2005 AAPL: 21979 x 4.21 = 92531.59


Cheers, Felix
Attachments
strategy_properties.png
strategy_properties.png (41.97 KiB) Viewed 593 times

Return to “User Contributed Studies and Indicator Library”