Tracking Last EntryPrice While Pyramiding on Minute Bars

EasyLanguage’s EntryPrice Doesn’t Cut the Mustard

In writing the Hi-Res edition of Easing Into EasyLanguage I should have included this sample program.  I do point out the limitations of the EntryPrice keyword/function in the book.  But recently I was tasked to create a pyramiding scheme template that used minute bars and would initiate a position with N Shares and then pyramid up to three times by adding on N Shares at the last entry price + one 10 -Day ATR measure as the market moves in favor of the original position.  Here is an example of just such a trade.

Pyramid 3 Times After Initial Trade Entry. Where’s the EntryPrice?

EntryPrice only contains the original entry price.  So every time you add on a position, the EntryPrice doesn’t reflect this add on price.  I would like to be able to index into this keyword/function and extract any EntryPrice.  If you enter at the market, then you can keep track of entry prices because a market order is usually issued from an if-then construct:

//Here I can keep track of entry prices because I know
//exactly when and where they occur.

if c > c[1] and value1 > value2 then
begin
buy("MarketOrder") next bar at market;
lastEntryPrice = open of next bar;
end;
Last Entry Price tracking is easy if using Market Orders

But what if you are using a stop or limit order.  You don’t know ahead of time where one of these types of orders will hit up.  It could be the next bar or it could be five bars later or who knows.

AvgEntryPrice Makes Up for the Weakness of EntryPrice

AvgEntryPrice is a keyword/function that  returns the average of the entries when pyramiding.  Assume you buy at 42.00 and pyramid the same number of shares at 46.50 – AvgEntryPrice will be equal to (42.00 + 46.50) / 2 = 44.25.  With this information you can determine the two entry prices.  You already know the original price.  Take a look at this code.

// remember currentShares and avgEntryPrice ARE EasyLanguage Keywords/Functions
if mp[1] = mp and mp = -1 and currentShares > curShares then
begin
totShorts = totShorts + 1;
if currentShares > initShares then
begin
lastEntryPrice = totShorts * avgEntryPrice - entryPriceSums;
entryPriceSums = entryPriceSums + lastEntryPrice;
print(d," Short addon ",lastEntryPrice," ",totShorts," ",avgEntryPrice," ",entryPriceSums);
end;
end;
Calculating the true LastEntryPrice

Remember currentShares is a keyword/function and it is immediately updated when more shares are added or taken off.  CurShares is my own variable where I keep track of the prior  currentShares , so if currentShares (real number of shares) is greater than the prior curShares (currentShares) then I know 100%, a position has been pyramided as long the the mp stays the same.  If currentShares increases and mp stays constant, then you can figure out the last entry price where the pyramid takes place.  First you tick totShorts up by 1.  If currentShares > initShares, then you know you are pyramiding so

lastEntryPrice = totShorts * avgEntryPrice – entryPriceSums

Don’t believe me.  Let’s test it.  Remember original entry was 42.00 and the add on was at 46.50.  TotShorts now equals 2.

  1. Initial entryPrice = 42.00 so entryPriceSums is set to 42.00
  2. After pyramiding avgEntryPrice is set to 44.25
  3. lastEntryPrice = 2 * 44.25 – 42.00 = 46.50
  4. entryPriceSums is then set to 42.00 + 46.50 or 88.50

So every time you add on a position, then you flow through this logic and you can keep track of the actual last entry price even if it is via a limit or stop order.

But wait there is more.  This post is also a small glimpse into what I will be writing about in the Easing Into EasyLanguage:  Advanced Topics.  This system definitely falls into what I discussed in the Hi-Res Edition.  Here is where we tip over into Advanced Topics.  The next book is not about creating dialogs or trading apps using OOEL (object oriented EasyLanguage), but we do use some of those topics to do some rather complicated back testing things.

Now that we know how to calculate the lastEntryPrice wouldn’t it be really cool if we could keep track of all of the entryPrices during the pyramid stream.   If I have pyramided four times, I would like to know entryPrice 1, entryPrice 2, entryPrice 3 and entryPrice 4.

EntryPrice Vector

Dr. VectorLove or How I Learned to Stop Worrying and Love Objects

I have discussed vectors before but I really wanted to discuss them more.  Remember Vectors are just lists or arrays that don’t need all the maintenance.  Yes you have to create them which can be a pain, but once you learn and forget it twenty times it starts to sink in.  Or just keep referring back to this web page.


Using elsystem.collections;

vars: Vector entryPriceVector(Null);

once Begin
entryPriceVector = new Vector;
end;
The Bare Minimum to Instantiate a Vector
  1. Type – “Using elsystem.collections; “
  2. Declare entryPriceVector as a Vector and set it equal to Null
  3. Use Once and instantiate entryPriceVector by using the keyword new < object type>;

A Vector is part of elsystem’s collection objects.  Take a look at this updated code,

if mp[1] = mp and mp = -1 and currentShares > curShares then
begin
totShorts = totShorts + 1;
if currentShares > initShares then
begin
lastEntryPrice = totShorts * avgEntryPrice - entryPriceSums;
entryPriceVector.push_back(lastEntryPrice);
entryPriceSums = entryPriceSums + lastEntryPrice;
print(d," Short addon ",lastEntryPrice," ",entryPrice," ",entryPrice(1)," ",totShorts," ",avgEntryPrice," ",entryPriceSums," ",entryPriceVector.back() astype double," ",entryPriceVector.count asType int);
if not(entryPriceVector.empty()) then
begin
for m = 0 to entryPriceVector.count-1
begin
print(entryPriceVector.at(m) astype double);
end;
end;
end;
end;
LastEntryPrice and Pushing It onto the Vector and Then Printing Out the Vector

After the lastEntryPrice is calculated it is pushed onto the entryPriceVector using the function (method same thing but it is attached to the Vector class).push_back(lastEntryPrice);

entryPriceVector.push_back(lastEntryPrice);

So every time a new lastEntryPrice is calculated it is pushed onto the Vector at the back end.  Now if the entryPriceVector is not empty then we can print its contents by looping and indexing into the Vector.

if not(entryPriceVector.empty()) then
begin
     for m = 0 to entryPriceVector.count-1
     begin
          print(entryPriceVector.at(m) astype double);
     end;
end;
Looping through a Vector and Printing Out its Contents

Remember if you NOT a boolean value then it turns it to off/on or just the opposite of the boolean valueIf entryPriceVector is not empty then proceed.  entryPriceVector.count holds the number of values stuffed into the vectorYou can index into the Vector by using .at(m),  If you want to print out the value of the Vector .at(m), then you will need to typecast the Vector object as what ever it is holding.  We know we are pushing numbers with decimals (double type) onto the Vector so we know we can evaluate them as a double type.  Just remember you have to do this when printing out the values of the Vector.

Okay you can see where we moved into an Advanced Topics area with this code.  But it really becomes useful when trying to overcome some limitations of EasyLanguage.  Remember keep an eye open for Advanced Topics sometime in the Spring.

Another Good Year For Trend Following

Take a Look at the Last Two Years

Simple Donchian on a one contract basis.  $100 Commission/slippage.  Tested from 2000 thru December 31, 2021.  Do you see why most trend followers failed after the 2008 monstrous year.   Many funds caught the 2008 move and more funds were added soon thereafter.  Promises of similar performance came to fruition in 2011.  This kept much of the “new money” on the board.  However, reality set in and weak handed funds left for greener pastures.  Those that stuck it out were rewarded in 2014.  The trend drought of 2014 -2019 eroded most of the confidence in managed futures.  The rationalization that limited resources would eventually rise in price sounded good initially, but then fell on deaf ears after months of draw down.  Well known CTAs and hedge funds shut their doors forever.   The long awaited promise of 2008 came in the form of a pandemic – but it was too late.   Maybe now the deluge that ended the drought will persevere (hopefully not in the form of a pandemic) into the future.  Prices do not need to rise endlessly, but they need to move one direction or another without many hiccups.   

Simple Donchian Caught Most of the Commodities Up Moves

Which Sectors Pushed this Curve through the Roof

These reports were generated by my Python based Trading Simula-18 using Pinnacle continuous data – rollover triggered by date.  This is my new sector analysis report where I graph the last four years performance.  The tabular data is for the entire 21 year history.  The best sectors were energy, grains, financials and metals.  Lumber was extraordinary

Sector Analysis Report
################################################
Currency -------------------------------------
BN -28012 44681
SN -26925 55337
AN 6560 34350
DX 16284 24387
FN 67463 31737
JN -22212 50362
CN -25355 44110
------------------------------------------------
Totals: -12198 141445
------------------------------------------------
Currency Last 4 Years ---------------------
|
||| |
||||||
| ||||||||
| |||||||||
| ||||||||||
|||||||||||||
||||||||||||||
|||||||||||||| |
|||||||||||||||||||
||||||||||||||||||||| |
|||||||||||||||||||||| | |
|||||||||||||||||||||| | |
|||||||||||||||||||||| | || | | |
------------------------------------------------ 0
||||||||||||||||||||||||| ||| |||| ||| |
||||||||||||||||||||||||| |||| |||||||||||
||||||||||||||||||||||||| | |||| ||||||||||||
||||||||||||||||||||||||||| |||||||||||||||||
|||||||||||||||||||||||||||| ||||||||||||||||||
||||||||||||||||||||||||||||||||||||||||||||||||
2018--------2019--------2020--------2021--------
################################################
Energies -------------------------------------
ZU 180750 38330
ZH 155696 85541
ZN 70630 74400
ZB 131874 66651
------------------------------------------------
Totals: 538951 154434
------------------------------------------------
Energies Last 4 Years ---------------------
| |
|| || ||
||| || |||
| ||||||||||
| |||||||||||
||| |||||||||||
|||| | ||||||||||||
||||||| ||||||||||||
|||||||||||||||||||||
|||||||||||||||||||||
|||||||||||||||||||||
|||||||||||||||||||||
| || |||||||||||||||||||||
|| || |||||||||||||||||||||
|| |||| |||||||||||||||||||||
||||||||| ||||||||||||||||||||||
| |||||||||| |||||||||||||||||||||||
|| || |||||||||||||||||||||||||||||||||||||
|||||| |||||||||||||||||||||||||||||||||||||
||||||||||||||||||||||||||||||||||||||||||||||||
2018--------2019--------2020--------2021--------
################################################
Metals -------------------------------------
ZG -17070 43540
ZI 68395 146885
ZK 101888 29475
ZP 82885 27600
ZA 174955 83910
------------------------------------------------
Totals: 411052 166703
------------------------------------------------
Metals Last 4 Years ---------------------
|
| | |
|| || |
|| |||| |||
| ||| | |||||||||
|| |||||||||||||||||
|| |||||||||||||||||||
|||||||||||||||||||||||
|||||||||||||||||||||||
|||||||||||||||||||||||
| | ||||||||||||||||||||||||
||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||
|| |||||||||||||||||||||||||||||||
|||||||||||||||||||||||||||||||||||
||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||
||||||||||||||||||||||||||||||||||||||||
|| |||||||||||||||||||||||||||||||||||||||||
||||||||||||||||||||||||||||||||||||||||||||||||
2018--------2019--------2020--------2021--------
################################################
Grains -------------------------------------
ZS 79175 20312
ZW -43438 51975
ZC 5238 26688
ZL 13248 24588
ZM 29860 28810
------------------------------------------------
Totals: 84083 88850
------------------------------------------------
Grains Last 4 Years ---------------------
|
|
||
||
|||||||||
|||||||||||
|||||||||||
|||||||||||
||||||||||||
| ||||||||||||
| | ||||||||||||
| || ||| ||||||||||||
||||| ||||| |||||||||||||
|||||||||||| |||||||||||||
|||||||||||| ||| | |||||||||||||
||||||||||||| ||||||| || ||||||||||||||
|||||||||||||||||||||||||||| ||||||||||||||
|||||||||||||||||||||||||||||| |||||||||||||||
||||||||||||||||||||||||||||||| |||||||||||||||
||||||||||||||||||||||||||||||||||||||||||||||||
2018--------2019--------2020--------2021--------
################################################
Financials -------------------------------------
US 35991 24959
TY -350 29175
TU 1473 23969
EC 4700 9650
------------------------------------------------
Totals: 41813 56453
------------------------------------------------
Financials Last 4 Years ---------------------
| | ||||
||| | |||||
||| | || ||||||
||||| ||||||||||
|||||| ||||||||||
|||||||| ||||||||||
|||||||| ||||||||||
||||||||| |||||||||||
||||||||||||||||||||||
| ||||||||||||||||||||||
| ||||||||||||||||||||||
| | ||||||||||||||||||||||
| | || ||||||||||||||||||||||
|| |||||| |||||||||||||||||||||||
||| |||||||||||||||||||||||||||||||
|||| || | |||||||||||||||||||||||||||||||
||||| |||| |||||||||||||||||||||||||||||||
||||||| ||||||||||||||||||||||||||||||||||||
|||||||| ||||||||||||||||||||||||||||||||||||
||||||||||||||||||||||||||||||||||||||||||||||||
2018--------2019--------2020--------2021--------
################################################
Softs -------------------------------------
SB 25927 15035
KC -49775 94069
CC -72140 76660
CT 16785 45470
Lumber 218513 51745
JO 2588 15760
------------------------------------------------
Totals: 141898 128540
------------------------------------------------
Softs Last 4 Years ---------------------
| ||
||||
|||||
|||||
||||||
| ||||||
||||||||
||||||||
||||||||
||||||||
------------------------------------------------ 0
| | |||||||||
| | ||||||||||
||||||||||||||||
||||||||||||||||
|||||||||||||||||
|||||||||||||||||
|| | ||||||||||||||||||
| ||||||||||| | |||||||||||||||||||||
||||||||||||||||||||||||||||||||||||||||||||
||||||||||||||||||||||||||||||||||||||||||||||||
2018--------2019--------2020--------2021--------
################################################
Meats -------------------------------------
ZT -29940 57680
ZZ 38480 15080
ZF 18413 57550
------------------------------------------------
Totals: 26952 66515
------------------------------------------------
Meats Last 4 Years ---------------------
| ||
| || ||
|| | || ||
||| | || ||
||||||||| ||||
| |||||||||| ||||
| | ||||||||||| |||||
| | ||||||||||| |||||| |
| || |||||||||||||||||| |
| ||| | |||||||||||||||||||||
|| ||| | |||||||||||||||||||||
|||||| || | ||||||||||||||||||||| |
|||||| || |||||||||||||||||||||||| |
|||||| || ||||||||||||||||||||||||||
|||||||| | || ||||||||||||||||||||||||||
|||||||| | ||| ||||||||||||||||||||||||||
|||||||||| | |||||||||||||||||||||||||||||||
||||||||||| | |||||||||||||||||||||||||||||||
|||||||||||| || ||||||||||||||||||||||||||||||||
||||||||||||||||||||||||||||||||||||||||||||||||
2018--------2019--------2020--------2021--------

How Do You Program this in Python

Here is the module for TS-18.  There is a little extra code to keep track of sectors in case you want to limit sector exposure.  However, this code takes every trade on a one contract basis.  This code reflects my latest version of TS-18, which will be released shortly.

#  Define Long, Short, ExitLong and ExitShort Levels - mind your indentations
buyLevel = highest(myHigh,40,curBar,1)
shortLevel = lowest(myLow,40,curBar,1)
longExit = lowest(myLow,20,curBar,1)
shortExit = highest(myHigh,20,curBar,1)
ATR = sAverage(myTrueRange,30,curBar,1)
stopAmt = 2000/myBPV

ATR = sAverage(myTrueRange,30,curBar,1)

posSize = 1
mmLxit = 99999999
mmSxit = -99999999
if mp == 1 : mmLxit = entryPrice[-1] - stopAmt
if mp ==-1 : mmSxit = entryPrice[-1] + stopAmt



# Long Exit
if mp == 1 and myLow[curBar] <= mmLxit and mmLxit > longExit and barsSinceEntry > 1:
price = min(myOpen[curBar],mmLxit)
tradeName = "LxitMM"
numShares = curShares
exitPosition(price, curShares, tradeName, sysMarkDict)
unPackDict(sysMarkDict)
# Long Exit
if mp == 1 and myLow[curBar] <= longExit and barsSinceEntry > 1:
price = min(myOpen[curBar],longExit)
tradeName = "Lxit"
numShares = curShares
exitPosition(price, curShares, tradeName, sysMarkDict)
unPackDict(sysMarkDict)
# Short Exit
if mp == -1 and myHigh[curBar] >= shortExit and barsSinceEntry > 1:
price = max(myOpen[curBar],shortExit)
tradeName = "Sxit"
numShares = curShares
exitPosition(price, curShares, tradeName, sysMarkDict)
unPackDict(sysMarkDict)
# Short Exit
if mp == -1 and myHigh[curBar] >= entryPrice[-1] + stopAmt and barsSinceEntry > 1:
price = max(myOpen[curBar],entryPrice[-1] + stopAmt)
tradeName = "SxitMM"
numShares = curShares
exitPosition(price, curShares, tradeName,sysMarkDict)
unPackDict(sysMarkDict)
# Long Entry
if myHigh[curBar] >= buyLevel and mp !=1:
price = max(myOpen[curBar],buyLevel)
tradeName = "Simple Buy"
numShares = posSize
enterLongPosition(price,numShares,tradeName,sysMarkDict)
unPackDict(sysMarkDict)
# Short Entry
if myLow[curBar] <= shortLevel and mp !=-1 :
price = min(myOpen[curBar],shortLevel)
if mp == 0 : sectorTradesTodayList[curSector] +=1
tradeName = "Simple Sell"
numShares = posSize
enterShortPosition(price, numShares, tradeName, sysMarkDict)
unPackDict(sysMarkDict)
Python within Trading Simula-18