# Working Around 0:00 Time in EasyLanguage

Let’s say you want to carve out a special session of data from the 24-hour data session – maybe keep track of the highest high and lowest low from 9:00 p.m. to 4:00 p.m. the next day.  How would you do it?

To start with you would need to reset the highest high and lowest low values each day.  So you could say if the current bars time > StartTime and the prior bars time <= StartTime then you know the first bar of your specialized session has started.  So far so good.  If the time falls outside the boundaries of your special session then you want to ignore that data -right?  What about this:

If t >StartTime and t <= EndTime then…

{Remember EasyLanguage uses the end time stamp for its intraday bars}

Sounds good.  But what happens when time equals 2300 or 11:00 p.m.?  You want to include this time in your session but the if-then construct doesn’t work.    2300 is greater than 2100 but it’s not less than 1600 so it doesn’t pass the test.  The problems arise when the EndTime < StartTime.  It really isn’t since the EndTime is for the next day, but the computer doesn’t know that.  What to do?  Here is a quick little trick to help you solve this problem:  use a special offset if the time falls in a certain range.

EndTimeOffset = 0 ;

If t >=StartTime and t <= 2359 then EndTimeOffset= 2400 – EndTime;

Going back to our example of the current time of 2300 and applying this little bit of code our EndTimeOffset would be equal to 2400 – 1600 or 800.  So if t = 2300, you subtract 800 and get 1500 and that works.

2300 – 800 = 1500 which is less than 1600 –> works

What if t = 300 or 3:00 a.m.  Then EndTimeOffset = 0; 300 – 0 is definitely less than 1600.

That solves the problem with the EndTime.  Or does it?  What if EndTime is like 1503?  So you have 2400 – 1503 which is something like 897.  What if time is 2354 and you subtract 897 you get 1457 and that still works since its less than 1503.  Ok, what about if EndTime = 1859 then you get 2400 – 1859 which equals 541.  If time  = 2354 and you subtract 541 you get 1843 and that still works.

Is there a similar problem with the StartTime?  If t = 3:00 a.m. then it is not greater than our StartTime of 2100, but we want it in our window.  We need another offset.  This time we want to make a StartTime offset equal to 2400 when we cross the 0:00 timeline.  And then reset it to zero when we cross the StartTime timeline.  Let’s see if it works:

t = 2200 : is t > StartTime?  Yes

t=0002 : is t > StartTime?  No, but should be.  We crossed the 0000 timeline so we need to add 2400 to t and then compare to StartTime:

t + 2400 = 2402 and it is greater than StartTime.  Make sense?

Probably not but look at the code:

``````inputs: StartTime(numericSimple),EndTime(numericSimple),StartTimeOffSet(numericRef),EndTimeOffSet(numericRef);

If t >= StartTime and t[1] < StartTime then StartTimeOffSet = 0;
EndTimeOffSet = 0;
If t >= StartTime and t <= 2359 then EndTimeOffSet = 2400 - EndTime;
If t < t[1] then StartTimeOffSet = 2400;

TimeOffsets = 1; ``````
Function To Calculate Start and End Time Offsets

Here is an the indicator code that calls the function:

``````vars: startTimeWindow(2100),endTimeWindow(1600);
vars: startOffSet(0),endOffSet(0);
Value1 = timeOffSets(startTimeWindow,endTimeWindow,startOffSet,endOffSet);

If t+startOffset > startTimeWindow and t-endOffSet <=endTimeWindow then
Begin

end
Else
Begin
print(d," ",t," outside time window ");
end;``````
Calling TimeOffsets Function

Hope this helps you out.  I am posting this for two reasons: 1) to help out and 2) prevent me from reinventing the wheel every time I have to use time constraints on a larger time frame of data.

StartTimeWindow = 2300

EndTimeWindow = 1400

Time = 2200, FALSE

Time = 2315, TRUE [2315 > 2300 and 2315 – (2400 -1400) <1400)]

This code should work with all times.  Shoot me an email if you find it doesn’t.

# Calculating Position Size with Optimal F

I had a reader of the blog ask how to use Optimal F.  That was really a great question.  A few posts back I provided the OptimalFGeo function but didn’t demonstrate on how to use it for allocation purposes.  In this post, I will do just that.

## I Have Optimal F – Now What?

From Ralph Vince’s book, “Portfolio Management Formulas”, he states: “Once the highest f is found, it can readily be turned into a dollar amount by dividing the biggest loss by the negative optimal f.  For example, if our biggest loss is \$100 and our optimal f is 0.25, then -\$100/ 0.25 = \$400.  In other words, we should bet 1 unit for every \$400 we have in our stake.”

### Convert Optimal F to dollars and then to number of shares

In my example strategy, I start out with an initial capital of \$50,000 and allow reinvestment of profit or loss.  The protective stop is set as 3 X ATR(10).  A fixed \$2000 profit objective is also utilized.  The conversion form Optimal F to position size is illustrated by the following lines of code:

``````//keep track of biggest loss
biggestLoss = minList(positionProfit(1),biggestLoss);
//calculate the Optimal F with last 10 trades.
OptF = OptimalFGeo(10);
//reinvest profit or loss
risk\$ = initCapital\$ + netProfit;
//convert Optimal F to \$\$\$
if OptF <> 0 then numShares = risk\$ / (biggestLoss / (-1*OptF));``````
Code snippet - Optimal F to Position Size
1. Keep track of biggest loss
2. Calculate optimal F with OptimalFGeo function – minimum 10 trades
3. Calculate Risk\$ by adding InitCapital to current NetProfit (Easylanguage keyword)
4. Calculate position size by dividing Risk\$  by the quotient of biggest loss and (-1) Optimal F

I applied the Optimal F position sizing to a simple mean reversion algorithm where you buy on a break out in the direction of the 50-day moving average after a lower low occurs.

Code listing:

``````vars: numShares(0),initCapital\$(50000),biggestLoss(0),OptF(0),risk\$(0);

//keep track of biggest loss
biggestLoss = minList(positionProfit(1),biggestLoss);
//calculate the Optimal F with last 10 trades.
OptF = OptimalFGeo(10);
//reinvest profit or loss
risk\$ = initCapital\$ + netProfit;
//convert Optimal F to \$\$\$
if OptF <> 0 then numShares = risk\$ / (biggestLoss / (-1*OptF));
numShares =  maxList(1,numShares);
//if Optf <> 0 then print(d," ",t," ",risk\$ / (biggestLoss / (-1*OptF))," ",biggestLoss," ",optF);

if c > average(c,50) and low < low[1] then Buy numShares shares next bar at open + .25* range stop;

setStopPosition;
setProfitTarget(2000);

setStopLoss(3*avgTrueRange(10)*bigPointValue);``````
Strategy Using Optimal F

I have included the results below.  At one time during the testing the number of contracts jumped up to 23.  That is 23 mini Nasdaq futures (\$20 * 7,300) * 23.  That’s a lot of leverage and risk.  Optimal doesn’t  always mean the best risk mitigation.  Please let me know if you find any errors in the code or in the logic.

Here is the ELD that incorporates the Strategy and the Function.USINGOPTIMALF

# A Bar Scoring System – inspired by Keith Fitschen

In Keith’s wonderful book, “Building Reliable Trading Sytems”, he reveals several algorithms that classify an instruments’ movement potential.  In the part of the book that is titled Scoring by a Bar Type Criterion, he describes eight different two-day patterns that involve 3 different criteriaEight different Bar-Types

He looks at the relationship between today’s open and today’s close, today’s close and yesterday’s close, and today’s close in terms of the day’s range.  Bar-Types 1 to 4 all have the close of today >= close of yesterday.  Bar-Types 5 to 8 have close of today < close of yesterday.

I wanted to program this into my TradeStation and do some research to see if the concept is valid.  In his book, Keith tested a lot of different stocks and commodities.  In this post, I just test the ES, US, and Beans.  This form of research can be used to enhance an existing entry technique.

Here is how I defined the eight different bar types:

``````array : barTypeArray[8](false);

midRange = (h + l)/2;

barTypeArray[0] = c >= c[1] and c > o and c >= midRange;
barTypeArray[1] = c >= c[1] and c > o and c <  midRange;
barTypeArray[2] = c >= c[1] and c < o and c >= midRange;
barTypeArray[3] = c >= c[1] and c < o and c <  midRange;
barTypeArray[4] = c <  c[1] and c > o and c >= midRange;
barTypeArray[5] = c <  c[1] and c > o and c <  midRange;
barTypeArray[6] = c <  c[1] and c < o and c >= midRange;
barTypeArray[7] = c <  c[1] and c < o and c <= midRange;``````
Defining Eight Different Bar Types

I used a brute force approach by creating an 8-element array of boolean values.  Remember EasyLanguage uses a 0 index.  If the two -day pattern matches one of the eight criteria I assign the element a true value.  If it doesn’t match then a false value is assigned.  I use an input value to tell the computer which pattern I am looking for.  If I choose Bar-Type[0] and there is a true value in that array element then I take a trade.   By providing this input I can optimize over all the different Bar-Types.

``````Input :
BarTypeNumber(0), // which bar type
numDaysToHold(2); //how many days to hold position

For cnt = 0 to 7 //remember to start at 0
Begin
If barTypeArray[cnt] = true then whichBarType = cnt;
end;

If whichBarType = BarTypeNumber then
begin
if buyOrSell = 2 then sellshort this bar on close;
end;``````
Loop Thru Array to find Bar Type

Here are some results of looping through all eight Bar-Types, Buy and Sell, and holding from 1 to 5 days.

ES – ten – year results – remember these are hypothetical results with no commission or slippage.

Here’s what the equity curve looks like.   Wild swings lately!!

Beans:

Bonds

Keith was right – look at the Bar Category that bubbled to the top every time – the most counter-trend pattern.  My Bar-Type Number 7  is the same as Keith’s 8.  Here is the code in its entirety.

``````{Bar Scoring by Keith Fitschen
from his book "Building Reliable Trading Systems" 2013 Wiley}

Input : BarTypeNumber(0),
numDaysToHold(2);

vars: midRange(0);
array : barTypeArray[8](false);

midRange = (h + l)/2;

barTypeArray[0] = c >= c[1] and c > o and c >= midRange;
barTypeArray[1] = c >= c[1] and c > o and c <  midRange;
barTypeArray[2] = c >= c[1] and c < o and c >= midRange;
barTypeArray[3] = c >= c[1] and c < o and c <  midRange;
barTypeArray[4] = c <  c[1] and c > o and c >= midRange;
barTypeArray[5] = c <  c[1] and c > o and c <  midRange;
barTypeArray[6] = c <  c[1] and c < o and c >= midRange;
barTypeArray[7] = c <  c[1] and c < o and c <= midRange;

vars: whichBarType(0),cnt(0);

For cnt = 0 to 7
Begin
If barTypeArray[cnt] = true then whichBarType = cnt;
end;

If whichBarType = BarTypeNumber then
begin
if buyOrSell = 2 then sellshort this bar on close;
end;

begin
If marketPosition = 1 then sell this bar on close;
If marketPosition =-1 then buytocover this bar on close;
end;``````
Bar Scoring Example

Keith’s book is very well researched and written.  Pick one up if you can find one under \$500.  I am not kidding.  Check out Amazon.

# Tired of Manipulating Arrays – Try a Vector and a Queue

## Vectors:

An array like structure but are dynamic and have a plethora of tools at your disposal.  Arrays are cool and can be multi-dimensional and can be easily manipulated.  But they require a lot of forethought as to how much size to reserve for their implementation.  Now don’t think this is going to be an advanced EasyLanguage tutorial, because it’s really not.  Most of us TRS-80, Ti-99/4A, Vic-20 and Commodore 64 trained programmers of the early ’80s have not welcomed objects with open arms and that is really a mistake.  In this sense we are like cavemen – we have all of the rudimentary tools at our disposal and can create some really cool stuff and we can really understand what we are doing.  With time and effort, we can get to the same place as object-oriented programmers.  We just don’t like the concept of using other’s tools as much as we like using ours.  So if you aren’t classically trained in programming you may have an advantage when tieing into the objects of a programming language.  This little tutorial is a very brief glimpse into a whole different world of programming.  The beauty is you can combine “old school” programming with objects – even if you don’t understand how the objects are truly constructed.    I want to introduce the concept of the Vector and the Queue-  truly cool Swiss Army knives.  First the vector.  Let’s just jump into some of the code – it really is simple.

##### Object Instantiation – a long word for declaring variable:
``````Using elsystem.collections;

Vars: Vector opVector(NULL),
Vector hiVector(Null),
Vector loVector(Null),
Vector clVector(Null),
Vector barVector(Null),
Queue timeStampQue(Null);
Once
Begin
barVector = new Vector;
opVector = new Vector;
hiVector = new Vector;
loVector = new Vector;
clVector = new Vector;
timeStampQue = new Queue;
end;``````
Instantiating and Declaring Vectors and Queue

You have to tell EasyLanguage you want to use some of the tools in the elsystem.collections.  You do this by simply tell it you are Using elsystem.collections.  The word collections is a catch-all for a bunch of different types of data structures.  Remember data structures are just programming constructs used to hold data – like an array.  All the variables that you declare in EasyLanguage are arrays – you just aren’t really aware of it.   When you index into them to get prior values then you become slightly aware of it.  In this portion of code, I create five vectors and one queue and assign them the Null or an empty value.  I just finished a programming gig where I had to build dynamically sized bars from the base data.  Kind of like creating 15, 30, 60-minute bars from a 5-minute bar chart or stream.   I did this using arrays because I wanted to be able to index into them to go back in time and I didn’t how far I wanted to go back.  So I declared some arrays with large dimensions to be safe.  This really takes a bite out of your resources which costs space and time.  I had played with Vector like objects in Python, so I thought I would post about them here and show how cool they are.  Remember this is a rudimentary program and could be streamlined and cleaned up.  Each vector will store their respective time, open, high, low and close values of the combined bar.  In a later post, I would like to do this with a Dictionary.  So the opVector will hold the open price, the hiVector will hold the high price and so on.

### Build a Queue – why the extra ue?

I want to build 15-minute bars from 5-minute bars so I need to know when to sample the data to properly collect the corresponding data.  If I start at 9:30 then I want to sample the data at 9:45 and look back three bars to get the open and the highest high and the lowest low.  The close will simply be the close of the 9:45 bar.  I want to do this at 9:45, 10:00. 10:15 and so on.  I could manipulate the time and use the modulus function to see if the minutes are multiples of 15 and I tried this but it didn’t work too well.  So I thought since I was already in the collections why not build a list or a queue with all the timestamps I would need.  This is how I did it.

``````vars: hrs(0),mins(0),barMult(3),combBarTimeInterval(0),totBarsInHour(0),startTime(930),endTime(1615),cnt(0);

Once
Begin
mins = fracPortion(t/100);
combBarTimeInterval = barInterval*barMult;
While value1 < endTime
Begin
cnt = cnt + 1;
Value1 = calcTime(startTime,cnt*combBarTimeInterval);
//		print("Inside queue : ",Value1," ",cnt*combBarTimeInterval);
timeStampQue.Enqueue(Value1);
end;
end;``````
Populating A Queue With Time Stamps

I simply use the CalcTime function to add 15-minute intervals to the start time and then I add them to the queue:  timeStampQue.Enqueue(Value1);  You access the methods or tools to a class by using the dot (” . “) notation.  Once I instantiated or created the timeStampQue I gained access to all the tools that belong to that object.  The Enqueue method simply appends the list the value that you pass it.  I would have preferred the method to be labeled simply add.  How did I figure out the right method name you ask?  I accessed the Dictionary from the View menu in the TDE.  Here is a picture to help:

#### Dictionary:

I use the keyword Once to just execute the code one time.  You could have said if BarNumber = 1, but why not use the tools at your disposal,   I figured out the combBarTimeInterval by using the 5-minute bar multiplier (3).  I then looped from startTime to endTime in 15-minute intervals and stored the timeStamps in the queue.  So every time stamp I need is in the timeStampQue.  All I need now is to compare the time of the 5-minute bar to the time stamps inside the queue.  This is where using object really come in handy.

#### Queue Methods:

Old school would have looped through all of the elements in the list and compared them to the value I was seeking and if found it would return true.  In the object world, I can simply ask the object itself to see if the value is in it:

condition1 = timeStampQue.Contains(t);

Cool!  If condition1 is true then I know I am sitting on the 5-minute bar that shares the same timestamp as a 15-minute bar.  If the time stamps are the same then I can start building the large timeframe from the lower timeframe.  You add elements to a vector by using the insert method.  I simply looked it up in the dictionary.   I had to specify where to insert the value in the vector.  I simply inserted each value into the [0] location.  Remember we are inserting so everything else in the vector is moved down.

#### Vector Methods:

``````If condition1 then
Begin
barVector.insert(0,t);
opVector.insert(0,open[2]);
hiVector.insert(0,highest(h[0],3));
loVector.insert(0,lowest(l[0],3));
clVector.insert(0,close[0]);
end;``````
Inserting Values at Vector Location 0

I only need to keep track of the last 10 15-minute bars, so once the vector count exceeded 10, I simply popped off the value at the back end – pop_back().  I figured this out by looking at Martin Whittaker’s awesome website – www.markplex.com.

``````If opVector.Count > 10 then
begin
barVector.pop_back();
opVector.pop_back();
hiVector.pop_back();
loVector.pop_back();
clVector.pop_back();
end;``````
Popping the Back-End

To check my work I printed the 15-minute bars on each 5-minute bar to make sure the bars were being built properly.  These data structures expect an object to be inserted, added, popped so when you print out one of their values you have to tell the print statement what the object should be translated as.  Here the keyword asType comes into play.  Take a look at my code, and you will see what I mean.  I hope this gets you excited about objects because the collections class can save you a ton of time and is really cool.  Use it and you can brag that you are an OOP programmer at your next cocktail party.

###### Code Listing:
``````Using elsystem.collections;

Vars: Vector opVector(NULL),
Vector hiVector(Null),
Vector loVector(Null),
Vector clVector(Null),
Vector barVector(Null),
Queue timeStampQue(Null);
Once
Begin
barVector = new Vector;
opVector = new Vector;
hiVector = new Vector;
loVector = new Vector;
clVector = new Vector;
timeStampQue = new Queue;
end;

vars: hrs(0),mins(0),barMult(3),combBarTimeInterval(0),totBarsInHour(0),startTime(930),endTime(1615),cnt(0);

Once
Begin
mins = fracPortion(t/100);
combBarTimeInterval = barInterval*barMult;
While value1 < endTime
Begin
cnt = cnt + 1;
Value1 = calcTime(startTime,cnt*combBarTimeInterval);
//		print("Inside queue : ",Value1," ",cnt*combBarTimeInterval);
timeStampQue.Enqueue(Value1);
end;
end;

condition1 = timeStampQue.Contains(t);

Print(d," ",t," ",condition1);

If condition1 then
Begin
barVector.insert(0,t);
opVector.insert(0,open[2]);
hiVector.insert(0,highest(h[0],3));
loVector.insert(0,lowest(l[0],3));
clVector.insert(0,close[0]);
end;

If opVector.Count > 10 then
begin
barVector.pop_back();
opVector.pop_back();
hiVector.pop_back();
loVector.pop_back();
clVector.pop_back();
end;

vars:vectCnt(0);
print(d," ",t);
If opVector.Count > 9 then
Begin
For vectCnt = 0 to 9
begin
print(vectCnt," ",barVector.at(vectCnt) astype int," ",opVector.at(vectCnt) astype double," ",hiVector.at(vectCnt) astype double," ",loVector.at(vectCnt) astype double," ",clVector.at(vectCnt) astype double);
end;
end;``````
Program in its Entirety

# George’s EasyLanguage BarsSince Function – How Many Bars Since?

Have you ever wondered how many bars have transpired since a certain condition was met?  Some platforms provide this capability:

If ExitFlag and (c crosses above average within 3 bars) then

TradeStation provides the MRO (Most Recent Occurrence) function that provides a very similar capability.  The only problem with this function is that it returns a -1 if the criteria are not met within the user provided lookback window.  If you say:

myBarsSinceCond = MRO(c crosses average(c,200),20,1) < 3

And c hasn’t crossed the 200-day moving average within the past twenty days the condition is still set to true because the function returns a -1.

I have created a function named BarsSince and you can set the false value to any value you wish.  In the aforementioned example, you would want the function to return a large number so the function would provide the correct solution.  Here’s how I did it:

``````inputs:
Test( truefalseseries ),
Length( numericsimple ),
Instance( numericsimple ) , { 0 < Instance <= Length}

value1 = RecentOcc( Test, Length, Instance, 1 ) ;
If value1 = -1 then
Else

And here’s a strategy that uses the function:

``````inputs: profTarg\$(2000),protStop\$(1000),
rsiOBVal(60),rsiOSVal(40),slowAvgLen(100),

Value1 = BarsSince(rsi(c,rsiLen) crosses above rsiOSVal,rsiLen,1,999);
Value2 = BarsSince(rsi(c,rsiLen) crosses below rsiOBVal,rsiLen,1,999);

If c > average(c, slowAvgLen) and c < average(c,fastAvgLen) and Value1 <barsSinceMax then buy next bar at open;

If c < average(c, slowAvgLen) and c > average(c,fastAvgLen) and Value2 <barsSinceMax then sellshort next bar at open;

setStopLoss(protStop\$);
setProfitTarget(profTarg\$)``````

The function requires four arguments:

1. The condition that is being tested [e.g.  rsi > crosses above 30]
2. The lookback window [rsiLen – 14 bars in this case]
3. Which occurrence [1 – most recent; 2- next most recent; etc…]
4. False return value [999 in this case; if condition is not met in time]

## A Simple Mean Reversion Using the Function:

Here are the results of this simple system utilizing the function.

### Optimization Results:

I came up with this curve through a Genetic Optimization:

The BarsSince function adds flexibility or fuzziness when you want to test a condition but want to allow it to have a day (bar) or two tolerance.  In a more in-depth analysis, the best results very rarely occurred on the day the RSI crossed a boundary.   Email me with questions of course.

# An ES Day Trading Model Explained – Part 2

### This is a continuation post or Part 2 of the development of the ES day trading system with EasyLanguage.

If you can understand this model you can basically program any of your day trading ideas.

#### Inputs Again:

First I want to revisit our list of inputs and make a couple of changes before proceeding.

``````inputs: volCalcLen(10),orboBuyPer(.2),orboSellPer(.2);
inputs: volStopPer(.7),Stop\$(500);
inputs: volThreshPer(.3),ProfThresh\$(250);
inputs: volTrailPer(.2),Trail\$(200);
Modification to our inputs

If we want to optimize these values then we can’t use the keyword bigPointValue in the input variable default value.  So I removed them – also I added an input endTradingTime(1500).  I wanted to cut off our trading at a given time – no use entering a trade five minutes prior to the closing.

#### Disengage the Vol or \$Dollar Trade Management:

I may have muddied the waters a little with having volatility and \$ values simultaneously.  You can use either for the initial protective stop, profit threshold, and trailing stop amount.  You can disengage them by using large values.  If you want to ignore all the \$ inputs just add a couple of 00 to each of the values:

Stop\$(50000), ProfThres\$(25000),Trail\$(50000)

If you want to ignore the volatility trade management stops just put a large number in front of the decimal.

volStopPer(9.7), volThreshPer(9.3), volTrailPer(9.2)

If you make either set large then the algorithm will use the values closest to the current market price.

#### Computations:

Let’s now take a look at the computations that are done on the first bar of the day:

``````If d <> d[1] then
Begin
rangeSum = 0.0;  // range calculation for entry
For iCnt = 1 to volCalcLen
Begin
rangeSum = rangeSum + (highD(iCnt) - lowD(iCnt));
end;
vol = rangeSum/volCalcLen;
sellPoint = openD(0) - vol*orboSellPer;

longStopAmt = vol * volStopPer;
longStopAmt = minList(longStopAmt,Stop\$/bigPointValue);

shortStopAmt = vol *volStopPer;
shortStopAmt = minList(shortStopAmt,Stop\$/bigPointValue);

longThreshAmt = vol  * volThreshPer;
longThreshAmt = minList(longThreshAmt,ProfThresh\$/bigPointValue);

shortThreshAmt = vol * volThreshPer;
shortThreshAmt = minList(shortThreshAmt,ProfThresh\$/bigPointValue);

longTrailAmt = vol * volTrailPer;
longTrailAmt = minList(longTrailAmt,Trail\$/bigPointValue);

shortTrailAmt = vol * volTrailPer;
shortTrailAmt = minList(shortTrailAmt,Trail\$/bigPointValue);

longTrailLevel = 0;
shortTrailLevel = 999999;
shortsToday = 0;
end;``````
Once a day computations

I determine it is the first bar of the day by comparing the current 5-minute bar’s date stamp to the prior 5-minute bar’s date stamp.  If they are not the same then you have the first bar of the day.  The first thing I do is calculate the volatility of the current market by using a for-loop to accumulate the day ranges for the past volCalcLen days.  I start the iterative process using the iCnt index and going from 1 back in time to volCalcLen.  I use iCnt to index into the function calls HighD and LowD.  Indexing is not really the right word here – that is more appropriate when working with arrays.  HighD and LowD are functions and we are passing the values 1 to volCalcLen into the functions and summing their output.  When you do this you will get a warning “A series function should not be called more than once with a given set of parameters.”  Sounds scary but it seems to work just fine.  If you don’t do this then you have to include a daily bar on the chart.  I like to keep things as simple as possible.   Once I sum up the daily ranges I then divide by volCalcLen to get the average range over the few days.  All of the vol based variables will use this value.

#### Entries:

Entry is based off a move away from the opening in terms of volatility.  If we use 0.2 (twenty percent) as orboBuyPer then the algorithm will buy on a stop 20% of the average range above the open tick.  Sell short is just the opposite.   We further calculate the longStopAmt as a function of vol and a pure \$ amount.  I am using the minList function to determine the smaller of the two values  .This is how I disengage either the vol value or the \$ value.  The other variables are also calculated just once a day: shortStopAmt, longThreshAmt, shortThreshAmt, longTrailAmt, shortTrailAmt. You could calculate every bar but that would be inefficient. I am also resetting four values at the beginning of the day:  longTrailLevel, shortTrailLevel, buysToday and shortsToday.

#### The Mighty MP:

``````mp = marketPosition;

If mp = 1 and mp[1] <> 1 then buysToday = buysToday + 1;
If mp = -1 and mp[1] <> -1 then shortsToday = shortsToday + 1;``````
MarketPosition monitoring and determining Buys/Shorts Today

I like using a variable to store each bar’s marketPosition.  In this case I am using MP.  By aliasing the marketPosition function call to a variable allows us to do this:

if mp = 1 and mp[1] <> 1 then buysToday = buysToday + 1;

This little line does a bunch of stuff.  If the current bar’s position is 1 and the prior bars position is not one then we know we have just entered a long position.  So every time this happens throughout the day the buysToday is incremented.  ShortsToday works just the same.  Pitfall warning:  If you strategy enters and exits on the same bar then this functionality will not work!  Neither will the call to the marketPosition function.  It will look like nothing happened.  If you need to keep track of the number of trades make sure you can only enter or exit on different bars.  If you stuff is so tight then drop down to a 1 minute or tick bar.

#### Controlling the Nmber of Buys/Shorts for the Day:

``````if time < endTradingTime and buysToday < 1 then Buy("ORBo-B") next bar at buyPoint stop;
if time < endTradingTime and shortsToday < 1 then Sellshort("ORBo-S") next bar at sellPoint stop;

If marketposition = 1 then
Begin
longExitPoint = entryPrice - longStopAmt;
sell("L-Exit") next bar at longExitPoint stop;
end;

If marketposition = -1 then
Begin
shortExitPoint = entryPrice + shortStopAmt;
buyToCover("S-Exit") next bar at shortExitPoint stop;
end;

If marketPosition = 1 and maxContractProfit/bigPointValue >= longThreshAmt then
Begin
sell("TrailSell") next bar at longTrailLevel stop;
end;

If marketPosition = -1 and maxContractProfit/bigPointValue >= shortThreshAmt then
Begin
buyToCover("TrailCover") next bar at shortTrailLevel stop;
end;

SetExitOnClose;``````

Notice how I am controlling the trade directives using the if statements.  I only want to enter a long position when the time is right and I haven’t already entered a long position for the day.  If you don’t control the trade directives, then these orders are placed on every bar, in our case every 5-minutes.  If you have pyramiding turned off then once you are long the buy directive is ignored.  This is an important concept – let’s say you just want to buy and short only one time per day trade session.  If you don’t control this directive, then it will fire off an order every five minutes.    You don’t want this -at least I hope you don’t.

So controlling the time and number of entries is paramount.  If you don’t control the time of entry then the day can arrive at the last bar of the day and fire off an order for the opening of the next day.  A big no, no !

#### Put To Work:

Here are the inputs I used to generate the trades in the graphic that follows.

##### Not Doing Exactly What You Want:

Here is what most day traders are looking for.   I made a comment on the chart – make sure you read it – it is another pitfall.

The trailing stop had to wait for the bar to complete to determine if the profit reached the threshold.  A little slippage here.  You can overcome this if you use the BuiltIn Percent Trailing Strategy or by using the SetPercentTrailing function call.

However, you lose the ability to really customize your algorithms by using the builtin functionalility.  You could drop down to a one minute bar and probably get out nearer your trailing stop amount.

Here you go!

# Open Range BreakOut with Trade Management

How difficult is it to program a day trading system using an open range break out, protective stop and a trailing stop?  Let’s see.  I started working on this and it got a little more detailed than I wanted so I have now split it up into two posts.  Part 2 will follow very soon.

What inputs do we need?  How about the number of days used in the volatility calculation?  What percentage of the volatility from the open do you want to buy or sell?  Should we have a different value for buys and sells?  Do we want to use a volatility based protective stop or a fixed dollar?  How about a trailing stop?  Should we wait to get to a certain profit level before engaging the trailing stop?  Should it also be based on volatility or fixed dollar amt?  How much should we trail – again an amount of volatility or fixed dollar?

## Proposed inputs:

inputs: volCalcLen(10), orboBuyPer(.2), orboSellPer(.2), volStopPer(.7), \$Stop(500), volProfThreshPer(.5), \$ProfThresh(250),volTrailPer(.2),\$Trail(200);

That should do it for the inputs – we can change later if necessary.

## Calculations:

We need to determine the volatility and a good way do this is calculating the average range over the past N days.   There are two ways to do this: 1) incorporate a daily chart as data2 and use a built-in function for the calculation or 2) use a for-loop and use the built-in functions HighD and LowD and just use one data feed.   Both have their drawbacks.  The first is you need to have a multi-data chart and the second you get a warning that you shouldn’t put a series function call inside the body of a for-loop.   I have done it both ways and I prefer to deal with the warning – so far it has worked out nicely – so let’s go with a single data chart.

## Building the code:

### Inputs:

Since we are combining a volatility and fixed \$ amount in our trade management, you will need to set either the vol or dollar amounts to a high value to disable them.  You can use both but I am taking the smaller of the respective values.

``````inputs: volCalcLen(10),orboBuyPer(.2),orboSellPer(.2);
inputs: volStopPer(.7),\$Stop(500/bigPointValue);
inputs: volThreshPer(.5),\$ProfThresh(250/bigPointValue);
inputs: volTrailPer(.2),\$Trail(200/bigPointValue);``````
Inputs We Will Need - Can Changer Later

### Variables:

``````vars:vol(0),buyPoint(0),sellPoint(0),
longStopAmt(0),shortStopAmt(0),longExitPoint(0),shortExitPoint(0),
longThreshAmt(0),shortThreshAmt(0),
longTrailAmt(0),shortTrailAmt(0),
longTrailLevel(0),shortTrailLevel(0),
hiSinceLong(0),loSinceShort(0),mp(0),
rangeSum(0),iCnt(0),
Variables That We Might Need

### Once A Day Calculations:

Since we will be working with five-minute bars we don’t want to do daily calculations on each bar.  If we do it will slow down the process.  So let’s do these calculation on the first bar of the day only.

``````If d <> d[1] then
Begin
rangeSum = 0.0;  // range calculation for entry
For iCnt = 1 to volCalcLen
Begin
rangeSum = rangeSum + (highD(iCnt) - lowD(iCnt));
end;
vol = rangeSum/volCalcLen;
sellPoint = openD(0) - vol*orboSellPer;

longStopAmt = vol * volStopPer;
longStopAmt = minList(longStopAmt,Stop\$);

shortStopAmt = vol *volStopPer;
shortStopAmt = minList(shortStopAmt,Stop\$);

longThreshAmt = vol  * volThreshPer;
longThreshAmt = minList(longThreshAmt,ProfThresh\$);

shortThreshAmt = vol * volThreshPer;
shortThreshAmt = minList(shortThreshAmt,ProfThresh\$);

longTrailAmt = vol * volTrailPer;
longTrailAmt = minList(longTrailAmt,Trail\$);

shortTrailAmt = vol * volTrailPer;
shortTrailAmt = minList(shortTrailAmt,Trail\$);

longTrailLevel = 0;
shortTrailLevel = 999999;
shortsToday = 0;
end;``````
Do These Just Once A Day

#### For All of You Who Don’t Want To Wait – Beta Version Is Available Below:

In my next post, I will dissect the following code for a better understanding.  Sorry I just ran out of time.

``````inputs: volCalcLen(10),orboBuyPer(.2),orboSellPer(.2);
inputs: volStopPer(.7),Stop\$(500/bigPointValue);
inputs: volThreshPer(.3),ProfThresh\$(250/bigPointValue);
inputs: volTrailPer(.2),Trail\$(200/bigPointValue);

longStopAmt(0),shortStopAmt(0),longExitPoint(0),shortExitPoint(0),
longThreshAmt(0),shortThreshAmt(0),
longTrailAmt(0),shortTrailAmt(0),
longTrailLevel(0),shortTrailLevel(0),
hiSinceLong(0),loSinceShort(0),mp(0),
rangeSum(0),iCnt(0),

If d <> d[1] then
Begin
rangeSum = 0.0;  // range calculation for entry
For iCnt = 1 to volCalcLen
Begin
rangeSum = rangeSum + (highD(iCnt) - lowD(iCnt));
end;
vol = rangeSum/volCalcLen;
sellPoint = openD(0) - vol*orboSellPer;

longStopAmt = vol * volStopPer;
longStopAmt = minList(longStopAmt,Stop\$);

shortStopAmt = vol *volStopPer;
shortStopAmt = minList(shortStopAmt,Stop\$);

longThreshAmt = vol  * volThreshPer;
longThreshAmt = minList(longThreshAmt,ProfThresh\$);

shortThreshAmt = vol * volThreshPer;
shortThreshAmt = minList(shortThreshAmt,ProfThresh\$);

longTrailAmt = vol * volTrailPer;
longTrailAmt = minList(longTrailAmt,Trail\$);

shortTrailAmt = vol * volTrailPer;
shortTrailAmt = minList(shortTrailAmt,Trail\$);

longTrailLevel = 0;
shortTrailLevel = 999999;
shortsToday = 0;
end;

mp = marketPosition;

If mp = 1 and mp[1] <> 1 then buysToday = buysToday + 1;
If mp = -1 and mp[1] <> -1 then shortsToday = shortsToday + 1;

if time < sessionendTime(0,1) and shortsToday < 1 then Sellshort("ORBo-S") next bar at sellPoint stop;

If marketposition = 1 then
Begin
longExitPoint = entryPrice - longStopAmt;
sell("L-Exit") next bar at longExitPoint stop;
end;

If marketposition = -1 then
Begin
shortExitPoint = entryPrice + shortStopAmt;
buyToCover("S-Exit") next bar at shortExitPoint stop;
end;

If marketPosition = 1 and maxContractProfit/bigPointValue >= longThreshAmt then
Begin
sell("TrailSell") next bar at longTrailLevel stop;
end;

If marketPosition = -1 and maxContractProfit/bigPointValue >= shortThreshAmt then
Begin
buyToCover("TrailCover") next bar at shortTrailLevel stop;
end;

SetExitOnClose;``````
Beta Version - I will clean up later and post it

# The Perfect System

WARNING CHEATING AHEAD 😉 There is no Holy Grail!

Having the results of a Perfect Profit System for any given market can be helpful when trying to determine the quality and viability  of one’s own algorithm (ala Bob PardoThe Evaluation and Optimization of Trading Strategies).  The level of correlation between a user defined algorithm and the PPS can indicate the quality of an algorithm – if its not been overly curve-fit.

Michael Bryant of AdapTrade.com has written an excellent article on how to use the correlation between a user defined algo and the PPS to optimize the algo to increase the correlation co-efficient between the two.  The premise being the closer you get to the PPS with your own strategy the better off you are.

Bob Pardo’s perfect system simply accumulated the absolute differences between consecutive closes to create the PPS equity stream.  In Michael Bryant’s article he utilizes price swings based of either price points or fractions of ATR (average true range.)  Bryant’s version takes advantage of 20/20 hindsight and creates a more realistic trade count.  Bob’s version would trade every day.  Michael’s version trades much, much, less.

My version, with the benefit of hindsight, picks the highest close and lowest close of every month and uses the data to sellShort and buy at these levels respectively.  I wanted to actually show the trades on the charts as they maybe helpful as well.  So to do this you have to run two strategies : 1) one that picks and outputs the trades and 2) one that takes the output from one and executes the trades.

There were two ways to accomplish this : 1) calculate the monthly turning points and output the data to  a file and then insert the data as 3rd party index into another chart of the same market and program a strategy that interprets the index or 2) calculate the monthly turning points and output the information as pure EasyLanguage code snippet and paste that code into another strategy.  I chose the second method as it also helps show the reader how to do some really cool stuff.

Before I started to program this research tool I asked myself “How can you pick the monthly peaks and valleys as you run through the data?”  Well I knew I had to be at the end of the month and then look back enough bars to choose these points.  Since the number of trading days in a month are variable I knew I had to use a while loop.  While loops are really cool and easy to use, but you must be careful or you will land in Steve Jobs 1 infinite loop.  TradeStation is very patient and will let you know before crashing and burning though.  Here is the snippet of code that loops back in time to gather the monthly peaks and valleys:

``````If month(d) <> month(d[1]) then // are we in a new month?
Begin
Value1 = 1;
hiMonthPrice = 0;
loMonthPrice = 999999;
testMonth = month(date);
While month(d[value1]) = month(d[value1+1]) //loop until the prior month
Begin
If c[value1] > hiMonthPrice then
Begin
hiMonthPrice = c[value1]; //store the peak price
hiMonthBar = value1; // store the peak bar number
end;
If c[value1] < loMonthPrice then
Begin
loMonthPrice = c[value1]; //store the valley price
loMonthBar = value1; // store the valley bar number
end;
Value1 = value1 + 1;
end;
//	print(d," monthHi ",hiMonthPrice," ",hiMonthBar);
//	print(d," monthLo ",loMonthPrice," ",loMonthBar);``````
Looping Backward - Collecting Peaks 'n Valleys

Okay we now have the prior month’s peak and valley values.  So we need to store this information into a list.  EasyLanguage allow for user defined arrays, which are really just indexable  (I think that is a word) lists.  Here is a list of arrays that we will be using and how they are declared:

``````
Array: equPerf[5000](0),  { equity at each bar, perfect trades }
trIdeal[2000](0),      { equity at end of perfect trades }
datesPerf[2000](0),   { dates of perfect trades }
pricePerf[2000](0); { prices of perfect trades}``````
I over dimensioned these Arrays - 2 per month right?

To declare an array you use the Keyword array and then give the array a name and then use square brackets to tell the computer the maximum number of elements in the array then instantiate each array element to a value that is surrounded by parenthesis.  Here we assigned each value in the arrays to zero.

``````If tempBors = 1 then
Begin
If hiMonthBar > loMonthBar then
Begin  // long and peak occurs first
BorSPerf[trdCnt]  = -1;
datesPerf[trdCnt] = d[hiMonthBar];
pricePerf[trdCnt] = c[hiMonthBar];
print(datesPerf[trdCnt]," Sell ",pricePerf[trdCnt]);
trdCnt = trdCnt + 1;
BorSPerf[trdCnt]  = 1;
datesPerf[trdCnt] = d[loMonthBar];
pricePerf[trdCnt] = c[loMonthBar];
trdCnt = trdCnt + 1;
end
else
begin // long and valley occurs first
BorSPerf[trdCnt]  = -1;
datesPerf[trdCnt] = d[value1];
pricePerf[trdCnt] = c[value1];
print(datesPerf[trdCnt]," Sell Conflict ",pricePerf[trdCnt]);
trdCnt = trdCnt + 1;
BorSPerf[trdCnt]  = 1;
datesPerf[trdCnt] = d[loMonthBar];
pricePerf[trdCnt] = c[loMonthBar];
trdCnt = trdCnt + 1;
BorSPerf[trdCnt]  = -1;
datesPerf[trdCnt] = d[hiMonthBar];
pricePerf[trdCnt] = c[hiMonthBar];
print(datesPerf[trdCnt]," Sell ",pricePerf[trdCnt]);
trdCnt = trdCnt + 1;
end;
end;``````
Decision Process When Coming Into Month Long

Once you go through the entire chart you should have all of you trade arrays set up.  So on the last bar of the chart you need to print this data out to a file.  But you want to print it out so you can simply copy and paste the output into another strategy – in other words you want to be syntactically correct.

Use the keyword LastBarOnChart to predicate the output process.  You can only print out strings to a file in EasyLanguage so all numbers have to be converted to strings – use NumToStr to accomplish this feat.  I think I have discussed formatted output somewhere in a post, but here is a quick reminder.  Let’s say you want to convert 1234.5678 to the string 1234.56, you would use NumToStr(value1 , 2).  The second argument in the function call informs the computer to truncate the decimal to two places. If you want to concatenate multiple strings together (i.e. connect strings together) you simply use the + sign.  Look at this line of code:

StrOut = “DateArr[“+NumtoStr(value1,0)+”] = ” + numToStr(19000000+datesPerf[value1], 0) + “;TradeArr[“+numtoStr(value1,0)+”] = ” +numToStr(BorSPerf[value1],0) + “;PriceArr[“+NumtoStr(value1,0)+”] = ” + numToStr(pricePerf[value1],2) + “;”+ Newline;

This is what it creates:

DateArr[1] = 20001211;TradeArr[1] = -1;PriceArr[1] = 1368.50;

This is a neat line of code where I mix hard coded word strings with converted numerical values to strings to create syntactically correct EasyLanguage in one complete, albeit long, string.

Now all you need now is an interpreter strategy.  The output from the first pass program creates the arrays needed for the interpreter program and then outputs all of the trades as pre-filled arrays.  Here is the output program in its entirety:

`````` input:  FName       ("c:\PerfectSystem\output.txt");     { file name to write results to }

Var:  HighBar(0), LowBar(0),ibar(0), StrOut(""),
BorS  (0),daysInEquity(0),equCnt(0),dataIndex(0),tempMP(0),trdCnt(0),trdNum(0),
oteEqu(0),clsTrdEqu(0), hiMonthPrice(0),hiMonthBar(0),
loMonthPrice(0),loMonthBar(0),testMonth(0),firstTime(true);

Array: equPerf[5000](0),trPerf[2000](0),datesPerf[2000](0),
BorSPerf[2000](0),pricePerf[2000](0);

If month(d) <> month(d[1]) then // are we in a new month
Begin
Value1 = 1;
hiMonthPrice = 0;
loMonthPrice = 999999;
testMonth = month(date);
While month(d[value1]) = month(d[value1+1]) //loop until the prior month
Begin
If c[value1] > hiMonthPrice then
Begin
hiMonthPrice = c[value1]; //store the peak price
hiMonthBar = value1; // store the peak bar number
end;
If c[value1] < loMonthPrice then
Begin
loMonthPrice = c[value1]; //store the valley price
loMonthBar = value1; // store the valley bar number
end;
Value1 = value1 + 1;
end;
//	print(d," monthHi ",hiMonthPrice," ",hiMonthBar);
//	print(d," monthLo ",loMonthPrice," ",loMonthBar);

vars: tempBorS(0);
If firstTime then
begin
tempBorS = -1;
If hiMonthBar > loMonthBar then tempBorS = 1;
trdCnt = 1;
firstTime = false;
end
else
begin
tempBorS = BorSPerf[trdCnt-1];
end;

If tempBors = 1 then
Begin
If hiMonthBar > loMonthBar then
Begin  // long and peak occurs first
BorSPerf[trdCnt]  = -1;
datesPerf[trdCnt] = d[hiMonthBar];
pricePerf[trdCnt] = c[hiMonthBar];
print(datesPerf[trdCnt]," Sell ",pricePerf[trdCnt]);
trdCnt = trdCnt + 1;
BorSPerf[trdCnt]  = 1;
datesPerf[trdCnt] = d[loMonthBar];
pricePerf[trdCnt] = c[loMonthBar];
trdCnt = trdCnt + 1;
end
else
begin // long and valley occurs first
BorSPerf[trdCnt]  = -1;
datesPerf[trdCnt] = d[value1];
pricePerf[trdCnt] = c[value1];
print(datesPerf[trdCnt]," Sell Conflict ",pricePerf[trdCnt]);
trdCnt = trdCnt + 1;
BorSPerf[trdCnt]  = 1;
datesPerf[trdCnt] = d[loMonthBar];
pricePerf[trdCnt] = c[loMonthBar];
trdCnt = trdCnt + 1;
BorSPerf[trdCnt]  = -1;
datesPerf[trdCnt] = d[hiMonthBar];
pricePerf[trdCnt] = c[hiMonthBar];
print(datesPerf[trdCnt]," Sell ",pricePerf[trdCnt]);
trdCnt = trdCnt + 1;
end;
end;
If tempBors = -1 then
Begin
If loMonthBar > hiMonthBar then
Begin
BorSPerf[trdCnt]  = 1;
datesPerf[trdCnt] = d[loMonthBar];
pricePerf[trdCnt] = c[loMonthBar];
trdCnt = trdCnt + 1;
BorSPerf[trdCnt]  = -1;
datesPerf[trdCnt] = d[hiMonthBar];
pricePerf[trdCnt] = c[hiMonthBar];
print(datesPerf[trdCnt]," Sell ",pricePerf[trdCnt]);
trdCnt = trdCnt+ 1;
end
else
begin
BorSPerf[trdCnt]  = 1;
datesPerf[trdCnt] = d[value1];
pricePerf[trdCnt] = c[value1];
trdCnt = trdCnt + 1;
BorSPerf[trdCnt]  = -1;
datesPerf[trdCnt] = d[hiMonthBar];
pricePerf[trdCnt] = c[hiMonthBar];
print(datesPerf[trdCnt]," Sell ",pricePerf[trdCnt]);
trdCnt = trdCnt + 1;
BorSPerf[trdCnt]  = 1;
datesPerf[trdCnt] = d[loMonthBar];
pricePerf[trdCnt] = c[loMonthBar];
trdCnt = trdCnt + 1;
end;
end;

end;

tempMP = 0;
vars:cntDown(0);
If lastBarOnChart then
Begin
FileAppend(FName, NewLine + "arrays: DateArr[5000](0),TradeArr[5000](0),PriceArr[5000](0);" + NewLine);
For value1 = 1 to trdCnt
Begin
StrOut = "DateArr["+NumtoStr(value1,0)+"] = " + numToStr(19000000+datesPerf[value1], 0) + ";TradeArr["+numtoStr(value1,0)+"] = " +numToStr(BorSPerf[value1],0) + ";PriceArr["+NumtoStr(value1,0)+"] = " + numToStr(pricePerf[value1],2) + ";"+ Newline;
FileAppend(FName, StrOut);
end;
end;``````
Almost Perfect System EasyLanguage Creator Code

Here’s the output that was created when this strategy as was added to a 19 year long ES daily bar chart:

``````arrays: DateArr[5000](0),TradeArr[5000](0),PriceArr[5000](0);
DateArr[1] = 20001211;TradeArr[1] = -1;PriceArr[1] = 1368.50;
DateArr[2] = 20001220;TradeArr[2] = 1;PriceArr[2] = 1245.75;
DateArr[3] = 20010102;TradeArr[3] = -1;PriceArr[3] = 1265.50;
DateArr[4] = 20010105;TradeArr[4] = 1;PriceArr[4] = 1269.50;
DateArr[5] = 20010131;TradeArr[5] = -1;PriceArr[5] = 1346.50;
DateArr[6] = 20010201;TradeArr[6] = 1;PriceArr[6] = 1348.00;
DateArr[7] = 20010205;TradeArr[7] = -1;PriceArr[7] = 1328.25;
DateArr[8] = 20010228;TradeArr[8] = 1;PriceArr[8] = 1203.75;
DateArr[9] = 20010307;TradeArr[9] = -1;PriceArr[9] = 1231.25;
DateArr[10] = 20010322;TradeArr[10] = 1;PriceArr[10] = 1070.00;
DateArr[11] = 20010402;TradeArr[11] = -1;PriceArr[11] = 1100.75;
DateArr[12] = 20010403;TradeArr[12] = 1;PriceArr[12] = 1059.25;
DateArr[13] = 20010419;TradeArr[13] = -1;PriceArr[13] = 1209.75;
DateArr[14] = 20010501;TradeArr[14] = 1;PriceArr[14] = 1223.50;
DateArr[15] = 20010521;TradeArr[15] = -1;PriceArr[15] = 1265.50;
DateArr[16] = 20010530;TradeArr[16] = 1;PriceArr[16] = 1201.00;``````
Sample OutPut

Here is the interpreter strategy code.  I simply copied and pasted the output from the file as the header to this strategy.

``````arrays: DateArr[5000](0),TradeArr[5000](0),PriceArr[5000](0);//
DateArr[1] = 20001211;TradeArr[1] = -1;PriceArr[1] = 1368.50;// All these lines
DateArr[2] = 20001220;TradeArr[2] = 1;PriceArr[2] = 1245.75;//  created by Perfect
DateArr[3] = 20010102;TradeArr[3] = -1;PriceArr[3] = 1265.50;// System Out Put
DateArr[4] = 20010105;TradeArr[4] = 1;PriceArr[4] = 1269.50;//
DateArr[5] = 20010131;TradeArr[5] = -1;PriceArr[5] = 1346.50;//
DateArr[6] = 20010201;TradeArr[6] = 1;PriceArr[6] = 1348.00;//
DateArr[7] = 20010205;TradeArr[7] = -1;PriceArr[7] = 1328.25;//
DateArr[8] = 20010228;TradeArr[8] = 1;PriceArr[8] = 1203.75;//
DateArr[9] = 20010307;TradeArr[9] = -1;PriceArr[9] = 1231.25;//
DateArr[10] = 20010322;TradeArr[10] = 1;PriceArr[10] = 1070.00;//
...
...
...
...
DateArr[539] = 20180920;TradeArr[539] = -1;PriceArr[539] = 2939.50;//

vars: cnt(1);

If date +19000000 = DateArr[cnt]  then
Begin
print("Found Date : ",date);
begin
end;
begin
sell this bar on close;
end;
begin
sellShort this bar on close;
end;
begin
end;
cnt = cnt + 1;
If DateArr[cnt] = DateArr[cnt-1] then
Begin
print("two trades same day ",d," ",dateArr[cnt]);
begin
buy this bar on close ;
end;
begin
sell this bar on close;
end;
begin
print("looking to go short at ",PriceArr[cnt]);
sellShort this bar on close;
end;
begin
end;
cnt = cnt + 1;
end;
end;``````
Almost Perfect System Interpreter Code

I think I discussed how this code works in a prior post.  If I go back and see that I didn’t I will edit and update this post.  So how did it do?

Here is a zip file that contains both ELDs for the strategies : PERFECTSYSTEM

# EasyLanguage Code for Optimal F (Multi-Charts and VBA too!)

## Optimal F in EasyLanguage for TradeStation and MultiCharts

Here is the code for the Optimal F calculation.  For a really good explanation of Optimal f, I refer you to Ralph Vince’s Book Portfolio Management FORMULAS.  We had programmed this years ago for our Excalibur software and I was surprised the EasyLanguage code was not really all that accessible on the internet.  Finding the Optimal f is found through an iterative process or in programmers terms a loop.  The code is really quite simple and I put it into a Function.  I decided to create this function because I wanted to demonstrate the ideas from my last post on how a function can store variable and array data.  Plus this code should be readily available somewhere out there.

``````//OptimalFGeo by George Pruitt
//My interpretation Sept. 2018
//www.georgepruitt.com
//georgeppruitt@gmail.com

vars: iCnt(00),jCnt(00),grandTot(0),highI(0);
vars: optF(0.0),gMean(0.0),fVal(0.0),HPR(0.0),TWR(0.0),hiTWR(0.0);
vars: biggestLoser(0.0),gat(0.0);

begin
end;

// Taken from my Fortran library - GPP and Vince Book PMF

optF = 0.0;
gMean = 1.00;
gat   = 0.00;
Begin
biggestLoser = 0;
grandTot = 0;
For iCnt = 1 to tradeCnt //get the biggest loser
begin
end;
//	print(grandTot," ",biggestLoser);
IF({grandTot > 0 and} biggestLoser <0) then
begin
//		print("Inside TWR Calculations");
highI = 0;
hiTWR = 0.0;
for iCnt = 1 to 100
begin
fVal = .01 * iCnt;
TWR = 1.0;
for jCnt = 1 to tradeCnt // calculate the Terminal Wealth Relative
begin
HPR = 1. + (fVal * (-1*tradesArray[jCnt]) / biggestLoser);
TWR = TWR * HPR;
//   			print(fVal," ",iCnt," " ,jCnt," Trades ",tradesArray[jCnt]," HPR ",HPR:6:4," TWR : ",TWR:6:4," hiTWR",hiTWR:6:4," bl ",biggestLoser);
end;
//			print(iCnt," ",TWR," ",hiTWR);
IF(TWR>hiTWR) THEN
begin
hiTWR = TWR;
optF = fVal;    	// assign optF to fVal in case its the correct one
end
else
break;                     //highest f found - stop looping
end;
If (TWR <= hiTWR or optF >= 0.999999) then
begin
TWR  = hiTWR;
OptimalFGeo = optF;  //assign optF to the name of the function
end;

if(optF<>0) then GAT   = (gMean - 1.0) * (biggestLoser / -(optF));
print(d," gmean ",gmean:6:4," ",GAT:6:4);  // I calculate the GMEAN and GeoAvgTrade
end;
end;``````
Optimal F Calculation by Ralph Vince code by George Pruitt

#### VBA version of Optimal F

For those of you who have a list of trades and want to see how this works in Excel here is the VBA code:

``````Sub OptimalF()

i = 0
biggestLoser = 0#
Do While (Cells(3 + i, 1) <> "")
tradesArray(i) = Cells(3 + i, 1)
i = i + 1
Loop
highI = 0
hiTWR = 0#
rc = 3
For fVal = 0.01 To 1 Step 0.01
TWR = 1#
For jCnt = 0 To tradeCnt
HPR = 1# + (fVal * (-1 * tradesArray(jCnt)) / biggestLoser)
TWR = TWR * HPR
Cells(rc, 5) = jCnt
Cells(rc, 7) = HPR
Cells(rc, 8) = TWR
rc = rc + 1
Next jCnt
Cells(rc, 9) = fVal
Cells(rc, 10) = TWR
rc = rc + 1

If (TWR > hiTWR) Then
hiTWR = TWR
optF = fVal
Else
Exit For
End If

Next fVal
If (TWR <= hiTWR Or optF >= 0.999999) Then
TWR = hiTWR
OptimalFGeo = optF
End If
Cells(rc, 8) = "Opt f"
Cells(rc, 9) = optF
rc = rc + 1
gMean = TWR ^ (1# / (tradeCnt + 1))
If (optF <> 0) Then GAT = (gMean - 1#) * (biggestLoser / -(optF))
Cells(rc, 8) = "Geo Mean"
Cells(rc, 9) = gMean
rc = rc + 1
Cells(rc, 8) = "Geo Avg Trade"
Cells(rc, 9) = GAT

End Sub``````
VBA code for Optimal F

I will attach the eld and .xlsm file a little later.

# Function Variable Data Survives Between Calls

### Function Variable Data Survives from One Call to the Next – A Pretty Nifty Tool in EasyLanguage!

Creating a function that can store data and then have that data survive on successive function calls without having to pass information back and forth is really a cool and powerful tool in EasyLanguage.  In most programming languages, the variables defined in a function are local to that particular bit of code and once program execution exits the function, then the data is destroyed.  There are two exceptions (in other languages) that come to mind – if the variable is passed back and forth via their addresses, then the data can be maintained or if the variable is global in scope to the function and the calling program.  EasyLanguage prevents you from having to do this and this can definitely save on headaches.  I wrote a function that defines an array that will hold a list of trades.  Once the number of trades reaches a certain level, I then calculate a moving average of the last 10 trades.  The average is then passed back to the calling strategy.  Here is the simple code to the function.

``````{Function Name:   StoreTradesFunc by George Pruitt}
----------------------------------------------------------
It also remembers the values in the array tradesArray.
It does this between function calls.
Values - simple and array - undoubtedly are global to the function}

begin
end;

begin
Value2 = 0;