Category Archives: Uncategorized

How to Create a Dominant Cycle Class in Python

John Ehlers used the following EasyLanguage code to calculate the Dominant Cycle in a small sample of data.  If you are interested in cycles and noise reduction, definitely check out the books by John Ehlers – “Rocket Science for Traders” or “Cybernetic Analysis for Stocks and Futures.”  I am doing some research in this area and wanted to share how I programmed the indicator/function in Python.  I refer you to his books or online resources for an explanation of the code.  I can tell you it involves an elegantly simplified approach using the Hilbert Transform.

 

Inputs:	Price((H+L)/2);

Vars:	Imult(.635),
		Qmult (.338),
		InPhase(0),
		Quadrature(0),
		count(0),
		Re(0),
		Im(0),
		DeltaPhase(0),
		InstPeriod(0),
		Period(0);

If CurrentBar > 8 then begin
	Value1 = Price - Price[7];
 	Inphase = 1.25*(Value1[4]  - Imult*Value1[2]) + Imult*InPhase[3];
	 	
//    print(price," ",price[7]," ",value1," ",inPhase," ",Quadrature," ",self.im[-1]," ",self.re[-1])	
//	print(d," ",h," ",l," ",c," ",Value1[4]," ",Imult*Value1[2]," ", Imult*InPhase[3]," ",Inphase);
	Quadrature = Value1[2] - Qmult*Value1 + Qmult*Quadrature[2];
	Re = .2*(InPhase*InPhase[1] + Quadrature*Quadrature[1]) + .8*Re[1];
	Im = .2*(InPhase*Quadrature[1] - InPhase[1]*Quadrature)   + .8*Im[1];
	print(d," ",o," ",h," ",l," ",c," ",value1," ",inPhase," ",Quadrature," ",Re," ",Im);
	If Re <> 0 then DeltaPhase = ArcTangent(Im/Re);

	{Sum DeltaPhases to reach 360 degrees.  The sum is the instantaneous period.}
	InstPeriod = 0;
	Value4 = 0;
	For count = 0 to 50 begin
		Value4 = Value4 + DeltaPhase[count];
		If Value4 > 360 and InstPeriod = 0 then begin
			InstPeriod = count;
		end;
	end;

	{Resolve Instantaneous Period errors and smooth}
	If InstPeriod = 0 then InstPeriod = InstPeriod[1];
	Period = .25*InstPeriod + .75*Period[1];

	Plot1(Period, "DC");
EasyLanguage Code For Calculating Dominant Cycle

In my Python based back tester an indicator of this type is best programmed by using a class.  A class is really a simple construct, especially in Python, once you familiarize yourself with the syntax.   This indicator requires you to refer to historical values to calculate the next value in the equation:  Value1[4], inPhase[1], re[2], etc.,.  In EasyLanguage these values are readily accessible as every variable is defined as a BarArray – the complete history of a variable is accessible by using indexing.  In my PSB I used lists to store values for those variables most often used such as Open, High, Low, Close.  When you need to store the values of let’s say the last five bars its best to just create a list on the fly or build them into a class structure.  A Class stores data and data structures and includes the methods (functions) that the data will be pumped into.  The follow code describes the class in two sections:  1) data declaration and instantiation and 2) the function to calculate the Dominant Cycle.  First off I create the variables that will hold the constant values: imult and qmult.  By using the word self I make these variables class members and can access them using “.” notation.  I will show you later what this means.  I also make the rest of the variables class members, but this time I make them lists and instantiate the first five values to zero.  I use list comprehension to create the lists and zero out the first five elements – all in one line of code.  This is really just a neat short cut, but can be used for much more powerful applications.  Once you create a dominantCycleClass object the object is constructed and all of the data is connected to this particular object.  You can create many dominantCycleClass objects and each one would maintain its own data.  Remember a class is just a template that is used to create an object.

class dominantCycleClass(object):
    def __init__(self):
        self.imult = 0.635
        self.qmult = 0.338
        self.value1 = [0 for i in range(5)]
        self.inPhase = [0 for i in range(5)]
        self.quadrature = [0 for i in range(5)]
        self.re = [0 for i in range(5)]
        self.im = [0 for i in range(5)]
        self.deltaPhase = [0 for i in range(5)]
        self.instPeriod = [0 for i in range(5)]
        self.period = [0 for i in range(5)]
Data Portion of Class

 

The second part of the class template contains the method or function for calculating the Dominant Cycle.  Notice how I index into the lists to extract prior values.  You will also see the word self. preceding the variable names used in the calculations Initially I felt like this redundancy hurt the readability of the code and in this case it might.  But by using self. I know I am dealing with a class member.  This is an example of the ” . ” notation I referred to earlier.  Basically this ties the variable to the class.

def calcDomCycle(self,dates,hPrices,lPrices,cPrices,curBar,offset):
        tempVal1 = (hPrices[curBar - offset] + lPrices[curBar-offset])/2
        tempVal2 = (hPrices[curBar - offset - 7] + lPrices[curBar-offset - 7])/2
        self.value1.append(tempVal1 - tempVal2)
        self.inPhase.append(1.25*(self.value1[-5] - self.imult*self.value1[-3]) + self.imult*self.inPhase[-3])        
        self.quadrature.append(self.value1[-3] - self.qmult*self.value1[-1] + self.qmult*self.quadrature[-2])
        self.re.append(.2*(self.inPhase[-1]*self.inPhase[-2]+self.quadrature[-1]*self.quadrature[-2])+ 0.8*self.re[-1])
        self.im.append(.2*(self.inPhase[-1]*self.quadrature[-2] - self.inPhase[-2]*self.quadrature[-1]) +.8*self.im[-1])
        if self.re[-1] != 0.0: self.deltaPhase.append(degrees(atan(self.im[-1]/self.re[-1])))
        if len(self.deltaPhase) > 51:
            self.instPeriod.append(0)
            value4 = 0
            for count in range(1,51):
                value4 += self.deltaPhase[-count]
                if value4 > 360 and self.instPeriod[-1] == 0:
                    self.instPeriod.append(count)
            if self.instPeriod[-1] == 0: self.instPeriod.append(self.instPeriod[-1])
            self.period.append(.25*self.instPeriod[-1]+.75*self.period[-1])
            return(self.period[-1])
Dominant Cycle Method

Okay we now have the class template to calculate the Dominant Cycle but how do we us it?

#---------------------------------------------------------------------------------
#Instantiate Indicator Classes if you need them
#---------------------------------------------------------------------------------
#    rsiStudy = rsiClass()
#    stochStudy = stochClass()
    domCycle = dominantCycleClass()
#---------------------------------------------------------------------------------
#Call the dominantCycleClass method using " . " notation.
	tempVal1 = domCycle.calcDomCycle(myDate,myHigh,myLow,myClose,i,0)
#Notice how I can access class members by using " . " notation as well!
	tempVal2 = domCycle.imult
Dominant Cycle Object Creation

Here I assign domCycle the object created by calling the dominantCycleClass constructor.  TempVal1 is assigned the Dominant Cycle when the function or method is called using the objects name (domCycle) and the now familiar ” . ” notation.  See how you can also access the imult variable using the same notation.

Here is the code in its entirety.  I put this in the indicator module of the PSB.

class dominantCycleClass(object):
    def __init__(self):
        self.imult = 0.635
        self.qmult = 0.338
        self.value1 = [0 for i in range(5)]
        self.inPhase = [0 for i in range(5)]
        self.quadrature = [0 for i in range(5)]
        self.re = [0 for i in range(5)]
        self.im = [0 for i in range(5)]
        self.deltaPhase = [0 for i in range(5)]
        self.instPeriod = [0 for i in range(5)]
        self.period = [0 for i in range(5)]

    def calcDomCycle(self,dates,hPrices,lPrices,cPrices,curBar,offset):
        tempVal1 = (hPrices[curBar - offset] + lPrices[curBar-offset])/2
        tempVal2 = (hPrices[curBar - offset - 7] + lPrices[curBar-offset - 7])/2
        self.value1.append(tempVal1 - tempVal2)
        self.inPhase.append(1.25*(self.value1[-5] - self.imult*self.value1[-3]) + self.imult*self.inPhase[-3])        
        self.quadrature.append(self.value1[-3] - self.qmult*self.value1[-1] + self.qmult*self.quadrature[-2])
        self.re.append(.2*(self.inPhase[-1]*self.inPhase[-2]+self.quadrature[-1]*self.quadrature[-2])+ 0.8*self.re[-1])
        self.im.append(.2*(self.inPhase[-1]*self.quadrature[-2] - self.inPhase[-2]*self.quadrature[-1]) +.8*self.im[-1])
        if self.re[-1] != 0.0: self.deltaPhase.append(degrees(atan(self.im[-1]/self.re[-1])))
        if len(self.deltaPhase) > 51:
            self.instPeriod.append(0)
            value4 = 0
            for count in range(1,51):
                value4 += self.deltaPhase[-count]
                if value4 > 360 and self.instPeriod[-1] == 0:
                    self.instPeriod.append(count)
            if self.instPeriod[-1] == 0: self.instPeriod.append(self.instPeriod[-1])
            self.period.append(.25*self.instPeriod[-1]+.75*self.period[-1])
            return(self.period[-1])
Dominant Cycle Class - Python

 

Mean Reversion Regime Change?

Many traders of late have done quite well buying the dips (Mean Reversion) in the stock market.  I mean if you look at the big picture and just not the last two months.  The hyper aggressive buyers might have bitten off more than they could chew.  The laid back fully funded traders were able to shrug off the volatility – even when the DOW was down nearly quadruple digits some kept going in and buying in a very respectful manner.  Of course they had their protective stops as well as profit objectives in place as soon as their buys went in.  Take a look at this equity curve:The huge draw down in just a matter of a few days shook out a ton of traders – even though the drawdown had probably been seen a few times in the history of the algorithm.   When you buy after the DOW was down 1175 you have to be crazy right?  That’s exactly what a lot of systems did and if you didn’t have a relatively respectable stop then you might have gotten wiped out.  This system risks $1000 to make $4000 and only buys when you have two lower lows.  Oh and the close has to be above the 200 day moving average.  The intra-trade draw down was around $6000 during the February Vixpocalypse.  How can that be?  If I am only risking $1000 why did I lose almost $6000 on one given trade.

Many of the more popular mean reversion systems fell into this same situation because they utilized a stop on a close basis only.  In other words the market could collapse during the day, but you had to wait to get out on the close.  This can be dangerous even if historical performance has shown respectable performance metrics.  Waiting for the close has been able to turn large losing trades into either much smaller losing trades or even winners.

Here is the same system with an intraday stop of $2000.  I had to increase the stop to get a similar looking equity curve.  I also expanded the profit objective.  Trading these systems requires a balancing act – do you want the security of a daily fixed stop or the chance at a better looking equity curve.

A volatility cut off could definitely help with draw down but at the same time its going to bite into the bottom line.  I don’t see a regime change yet.  A fixed stop might provide enough safety to continue trading with these types of algorithms.

 

Pyramiding and then Scaling Out at Different Price Levels – EasyLanguage

TOTAL, TOTAL, TOTAL – an important keyword

I just learned something new!  I guess I never programmed a strategy that pyramided at different price levels and scaled out at different price levels.

Initially I thought no problem.  But I couldn’t get it to work – I tried everything and then I came across the keyword Total and then I remembered.  If you don’t specify Total in you exit directives then the entire position is liquidated.  Unless you are putting all your positions on at one time – like I did in my last post.   So remember if you are scaling out of a pyramid position use Total in your logic.

vars: maxPosSize(2);

If currentContracts < maxPosSize - 1 and c > average(c,50) and c = lowest(c,3) then buy("L3Close") 1 contract this bar on close;
If currentContracts < maxPosSize and c > average(c,50) and c = lowest(c,4) then buy("L4Close") 1 contract this bar on close;


If currentContracts = 2 and c = highest(c,5) then sell 1 contract total this bar on close;
If currentContracts = 1 and c = highest(c,10) then sell 1 contract total this bar on close;
Scaling Out Of Pyramid

Why you have to use the Total I don’t know.  You specify the number of contracts in the directive and that is sufficient if you aren’t pyramiding.  The pyramiding throws a “monkey wrench” in to the works.

Scaling Out of Position with EasyLanguage

First Put Multiple Contracts On:

If c > average(c,200) and c = lowest(c,3) then buy("5Large") 5 contracts this bar on close;
Using keyword contracts to put on multiple positions

Here you specify the number of contracts prior to the keyword contracts.

Easylanguage requires you to create a separate order for each exit.  Let’s say you want to get out of the 5 positions at different times and possibly prices.  Here’s how you do it:

If currentContracts = 5 and c > c[1] then sell 1 contracts this bar on close;
If currentContracts = 4 and c > c[1] then sell 1 contracts this bar on close;
If currentContracts = 3 and c > c[1] then sell 1 contracts this bar on close;
If currentContracts = 2 and c > c[1] then sell 1 contracts this bar on close;
If currentContracts = 1 and c > c[1] then sell 1 contracts this bar on close;
One order for each independent exit

The reserved word currentContracts hold the current position size.  Intuitively this should work but it doesn’t.

{If currentContracts > 0 then sell 1 contract this bar on close;}

You also can’t put order directives in loops.  You can scale out using percentages if you like.

Value1 = 5;

If currentContracts = 5 and c > c[1] then sell 0.2 * Value1 contracts this bar on close;
If currentContracts = 4 and c > c[1] then sell 1 contracts this bar on close;
If currentContracts = 3 and c > c[1] then sell 1 contracts this bar on close;
If currentContracts = 2 and c > c[1] then sell 1 contracts this bar on close;
If currentContracts = 1 and c > c[1] then sell 1 contracts this bar on close;
Using a percentage of original order size

 

That’s all there is to scaling out.  Just remember to have an independent exit order for each position you are liquidating.  You could have just two orders:  scale out of 3 and then just 2.

 

Setting Stop Loss and Profit Target Utilizing EntryPrice with EasyLanguage

One Problem with the “Next Bar” Paradigm – market position nor entryPrice are adjusted by the end of the bar

Whenever I develop a strategy I like to program all of my entries and exits without utilizing TradeStations built-in execution functions.  I just got use to doing this when I started programming in Fortran many years ago.  However, there a few scenarios where this isn’t possible.  If you enter a trade and use the following logic to get you out with a loss or a profit when referencing your entryPrice, you will be surprised with your results.  This is because you are telling the computer to use entryPrice before you know what it is.

This logic is absolutely correct in its intention.  However, TradeStation doesn’t realize you are in a position at the end of the bar and can’t properly reference entryPrice.  Okay so we force TradeStation to only issue orders once it has a valid entryPrice.TradeStation only realizes the correct marketPosition the following day and then issues an order for the next bar.  So we get the one bar delay.  It would be helpful if TradeStation would set the marketPosition at the close of the bar on the bar of entry.   However, you can overcome this with TradeStation’s built-in execution functions.  For some reason these functions know exactly when you get in – you can also get the same results by inserting the respective strategies on the chart.

An Easy Fix Though

But this little bug can creep into other areas of your programming.  Keep an eye on this.

Using TradeStation’s Development Environment Projects

I have started using the TDE’s projects more and more.  Before, when I was working on multiple related files I would lose track of them in the editor tabs, or discover, when working over time on different projects, I would have twenty or thirty files open – this of course would slow the editor down.  Using projects can clean up your workspace and speed up productivity.  Projects have been around forever and most professional IDEs  incorporate them to aid in the organization and productivity of the programmer.

Here is simple Project with three components:  indicator, function, and strategy.

 

 

Notice how you can focus just on the components of the project.

I also like to split the editor window and populate the bottom pane with code I might need to refer.

Check out the multiple output function – geoTriMavIndic.

 

Re-Entry After Taking A Profit

Here is some code I have been working on.  I will go into detail on the code a little later.  But this is how you monitor re-entering at a better price after taking a profit.  The problem with taking profits on longer term trend following systems is that the logic that got you into the position is probably still true and you will notice your algorithm will re-enter in the same direction.  So you need to inform your algorithm not to re-enter until a certain condition is met.  In this example, I only re-enter at a better price if the condition that got me into the trade is still valid.

Inputs: swingHiStrength(2),swingLowStrength(2),numDaysToLookBack(30),stopAmt$(500),profitAmt$(1000),getBackInAfterProfAmt$(250);
vars: mp(0),longProfTaken(false),shortProfTaken(false);

mp = marketPosition;

if not(longProfTaken) and mp <> 1 then Buy("BreakOut-B") next bar at highest(h[1],20) on a stop;
if not(shortProfTaken) and mp <>-1 then SellShort("BreakOut-S") next bar at lowest(l[1],20)  on a stop;

//If mp[0] = 0 and mp[1] = 1 then print(date," ",exitPrice(1)," ",entryPrice(1));

If longProfTaken then 
Begin
	If c < exitPrice(1) - getBackInAfterProfAmt$/bigPointValue then
	begin 
		longProfTaken = false;
		If mp <> -1 then buy("longRe-Entry") next bar at open;
	end;
end;

If shortProfTaken then 
Begin
	If c > exitPrice(1) + getBackInAfterProfAmt$/bigPointValue then
	begin 
		shortProfTaken = false;
		If mp <>-1 then sellShort("shortRe-Entry") next bar at open;
	end;
end;


If mp = 1 and c > entryPrice + profitAmt$/bigPointValue then 
begin
	sell this bar on close;
	longProfTaken = true;
end;

If mp =-1 and c < entryPrice - profitAmt$/bigPointValue then 
begin
	buyToCover this bar on close;
	shortProfTaken = true;
end;

If mp = -1 then longProfTaken = false;
If mp = 1 then shortProfTaken = false;

//if mp = 1 then setStopLoss(stopAmt$);
//if mp = 1 then setProfitTarget(profitAmt$);
Re-Entering At Better Price After Profit

A Slightly More Eloquent Approach to Programming Our Pyramiding E-Mini DayTrading Algorithm.

Okay let’s see how I was able to add some eloquence to the brute force approach to this pyramiding algorithm.  The original code included multiple entry directives and a ton of hard coded numerical values.   So let me show you how I was able to refine the logic/code and in doing so make it much more flexible.  We might lose a little bit of the readability, but we can compensate by using extra commentary.

First off, let’s add flexibility by employing input variables.  In this case, we need to inform the algorithm the distance from the open to add additional positions and the max number of entries allowed for the day.

inputs : pyramidDistance(5),maxDailyEntries(3);

Now we need to set somethings up for the first bar of the day.  Comparing the date of today with the date of yesterday is a good way to do this.

if d<>d[1] then 
begin
	canSell = true;
	sellMult = 1;
	sellStop = -999999;
	entries = 0;
end;
First bar of the day housekeeping.

Here is a neat way to keep track of the number of entries as they occur throughout the trading day.  Remember the function EntriesToday(date) will not provide the information we need.

mp = marketPosition * currentShares;

if mp[1] <> mp and mp <> 0 then entries = entries + 1;
How to track the number of entries for today.

If the last bar’s mp[1] is not equal to the current bar’s mp then and mp is not equal to zero then we know we have added on another entry.  Okay now let’s think about eliminating the “brute force” approach.

Instead of placing multiple order entry directives I  only want to use one with a variable stop level.  This stop level will be guided by the variable SellMult.  We start the day with a wacky sell stop level and then calculate it based on the SellMult variable and PyramidDistance input.

if low <= sellStop  then
begin
	sellMult = sellMult + 1;
end;

sellStop = openD(0) - sellMult * pyramidDistance;
Calculate and adapt sell stop level as we go along.

So on the first bar of the day the sellStop = openD(0) – sellMult * pyramidDistance or sellStop = openD(0) – 1 * 5.  Or 5 handles below the open.  Note you an change the pyramidDistance input and make it three to match the previous examples.

if entries = maxDailyEntries then canSell = false;
if time < sess1EndTime and canSell then sellShort 1 contract next bar at sellStop stop;
if mp <=-1 {and barsSinceEntry > 0} then buyToCover next bar at sellStop + 2* pyramidDistance stop;

setexitonclose;
That's it! Pretty simple isn't it?

Ok, we need to tell the computer to turn off the ability to place orders if one of two things happens:  1) we have reached the maxDailyEntries or 2) time >= sess1EndTime.    You could make the time to stop entering trades an input as well.  If neither criteria applies then place an order to sellShort at our sellStop level.   If price goes below our sell stop level then we know we have been filled and the new sellStop level needs to be recalculated.  See how we use a calculation to adapt the stop level with a single order placement directive?  This is where the eloquence comes into play.  QED.

Now you code the opposite side and then see if you can make money  (hypothetically speaking of course) with it.  If you think about it, why does this not work.  And the not so obvious reason is that it trades too much.  Other than trading too much it makes perfect sense – buy or sell by taking a nibbles at the market.  If the market takes off then take a big bite.  The execution costs of the nibbles are just way too great.  So we need to think of a filtering process to determine when it is either better to buy or sell or when to trade at all.  Good Luck with this ES [emini S&P ]day trading algorithm!

inputs : pyramidDistance(5),maxDailyEntries(3);
vars: mp(0),icnt(0),sellStop(0),sellMult(0),canSell(true),entries(0);

if d<>d[1] then 
begin
	canSell = true;
	sellMult = 1;
	sellStop = -999999;
	entries = 0;
end;

mp = marketPosition * currentShares;

if mp[1] <> mp and mp <> 0 then entries = entries + 1;
if mp[1] = -1 and mp[0] = 0 then canSell = false;
if time > 1430 then canSell = false;

if low <= sellStop  then
begin
	sellMult = sellMult + 1;
end;

sellStop = openD(0) - sellMult * pyramidDistance;
if entries = maxDailyEntries then canSell = false;
if time < sess1EndTime and canSell then sellShort 1 contract next bar at sellStop stop;
if mp <=-1 {and barsSinceEntry > 0} then buyToCover next bar at sellStop + 2* pyramidDistance stop;

setexitonclose;
Much More Flexible Code

Long Entry Logic and EasyLanguage Code for Pyramiding Algorithm

Sorry for the delay in getting this up on the web.  Here is the flip side of the pyramiding day trade scheme from the buy side perspective.  I simply flipped the rules.  In some cases, to keep the programming a little cleaner I like to keep the buy and sellShort logic in two separate strategies.  So with this chart make sure you insert both strategies.

And here is the code:

vars: mp(0),lastTradePrice(0),canBuy(true);

mp = marketPosition * currentContracts;

if date[0] <> date[1] then
begin
	canBuy = true;
end;

if mp = 1 then canBuy = false; 
if time > 1430 then canBuy = false;

if mp = 0 and canBuy = true then buy next bar at OpenD(0) + 3 stop;
if mp = 1 then buy next bar at OpenD(0) + 6 stop;
if mp = 2 then buy next bar at OpenD(0) + 9 stop;

if mp = 1 then lastTradePrice = OpenD(0) + 3;
if mp = 2 then lastTradePrice = OpenD(0) + 6;
if mp = 3 then lastTradePrice = OpenD(0) + 9;


if mp <> 0 then sell next bar at lastTradePrice - 3 stop;

if mp = 3 and barsSinceEntry > 0 and highD(0) > lastTradePrice + 3 then sell next bar at highD(0) - 3 stop;

setExitOnClose;
Pyramiding 3 Handles Up and Trailing Stop

EasyLanguage Code for Pyramiding a Day-Trading System w/video [PART-2]

 

Check out the latest video on Pyramiding.

Here is the finalized tutorial on building the pyramiding ES-day-trade system that was presented in the last post.

I will admit this video should be half as long as the end result.  I get a bit long-winded.  However, I think there are some good pointers that should save you some time when programming a similar system.

EasyLanguage Source:

Here is the final code from the video:

vars: mp(0),lastTradePrice(0),canSell(true);

mp = marketPosition * currentContracts;

if date[0] <> date[1] then
begin
	canSell = true;  // canSell on every day
end;

if mp = -1 then canSell = false; // one trade on - no more
if time > 1430 then canSell = false; //no entries afte 230 central

if mp = 0 and canSell = true then sellShort next bar at OpenD(0) - 3 stop;

if mp = -1 then sellShort next bar at OpenD(0) - 6 stop; //add 1
if mp = -2 then sellShort next bar at OpenD(0) - 9 stop; //add 2

if mp = -1 then lastTradePrice = OpenD(0) - 3; //keep track of entryPrice
if mp = -2 then lastTradePrice = OpenD(0) - 6;
if mp = -3 then lastTradePrice = OpenD(0) - 9;


if mp <> 0 then buyToCover next bar at lastTradePrice + 3 stop; // 3 handle risk on last trade

// next line provides a threshold prior to engaging trailing stop
if mp = -3 and barsSinceEntry > 0 and lowD(0) < lastTradePrice - 3 then buyToCover next bar at lowD(0) + 3 stop;

setExitOnClose;
EasyLanguage for Pyramiding and Day-Trading ES

What we learned here:

  • can’t use entriesToday(date) to determine last entry price
  • must use logic to not issue an order to execute on the first bar of the next day
  • mp = marketPosition * currentContracts is powerful stuff!

In the next few days, I will publish the long side version of this code and also a more eloquent approach to the programming that will allow for future modifications and flexibility.

Let me know how it works out for you.

Take this code and add some filters to prevent trading every day or a filter to only allow long entries!