Category Archives: Python

Restructuring Trade Entry with PSB

It has been brought to my attention by a very astute reader of the book that the ordering of the buy/sell/longliq/shortliq directives creates a potential error by giving preference to entries.  The code in the book and my examples thus far test for a true condition first in the entry logic and then in the exit logic.  Let’s say your long exit stop in the Euros is set at 11020, but your reversal is set at 11000.  The python back tester will skip the exit at 11020 and reverse at 11000.  This only happens if both stops are hit on the same day.  If this happens you will incur a 20 point additional loss.  You can prevent this by using logic similar to:

if ((mp == 0 or (mp == -1 and stb < stopb)) and myHigh[D0] >= stb) :
Eliminate Reversal Bias

Notice bow I compare the price level of stb and stopb [stb – reversal and stopb – liquidation].  I added this to the long entry logic – the code is only executed if the entry is less than the exit (closer to the current market).  I am in the process of restructuring the flow so that all orders will be examined on a bar by bar basis and the ones that should take place (chronologically speaking) will do so and be reflected in the performance metrics.  This can cause multiple trades on a single bar.  This is what happens in real trading and should be reflected in the PSB.  This is where the lack of a GOTO creates a small headache in Python.  The ESB already takes this into consieration.  I will post when I finalize the code.

Version 2.0 of Python System Back-tester Available

I have just wrapped up the latest version of the Python System Back-tester (PSB).

I have added some more portfolio performance metrics and you will see these in the performance reports.  The most useful addition is the concept of the .POR file when you run a system.  Instead of having to select your data files each time you run a system, you can build a .POR file with a list of files/markets you want to batch run.

Here is an example of a Portfolio file:

TY.CSV

CU.CSV

SB.CSV

S2.CSV

QG.CSV

QM.CSV

C2.CSV

Just make sure you put the .POR file inside the same folder that contains your testing data.  I have included a TestPortfolio.por file in version 2.0.  I would treat the different versions as completely separate applications.  Your existing .py algorithm files will need to be slightly modified to work with version 2.0.

This line of code needs to be modified from this:

systemMarket.setSysMarkInfo(sysName,myComName,listOfTrades,equity)

to this:

systemMarket.setSysMarkInfo(sysName,myComName,listOfTrades,equity,initCapital)

This line is near the bottom of the overall loop.  I added the initCapital variable so you could do position sizing and the performance metrics would reflect this initial value.

And set initCapital to a pertinent value.  I put it right below the sysName variable:

sysName = ‘BollingerBandSys’ #System Name here

initCapital = 100000 #starting account balance

Also I corrected a small bug in the main loop.  You should change this:

for i in range(len(myDate) – numBarsToGoBack,len(myDate)):

to

for i in range(len(myDate) – (numBarsToGoBack-rampUp),len(myDate)):

In another post I will show how the portfolio performance metrics have changed.  I hope you like the new version.  I will be adding a library of trading systems utilizing this new version in a few days.

If you want to download Version 2.0 – just go the the following link

PSBVersion2.0

If You Can Do This – You Can Test Any Algorithm!

All the unnecessary lines of the Python System Back-Testing Module have been hidden.  Only the lines that you need to develop the next great algorithm are included.  Reads sort  of like English.  This snippet introduces you to the use of functions and lists – two major components of the Python language.  If you buy my latest book – “The Ultimate Algorithmic Trading System Toolbox” then simply email me or sign up through the contact form and you will get version 2.0 for free!

PythonReduction

Turtle Volatility Loss in Python Back Tester – Part 3 in Series

The Turtle N or Volatility is basically a 20-day Average True Range in terms of  Dollars.  This amount is considered the market volatility.  In other words the market, based on the average, can either move up or down by this amount.  It can move much less or much further; this is just an estimate.  If the market moves 2 N against a position, then as a Turtle you were to liquidate your stake in that commodity.  The logic indicates that if the market has a break out and then moves 2 N in the opposite direction, then the break out has failed.  First the code must be defined to represent the market volatility.  This is simple enough by using the sAverage function call and passing it the trueRanges and 20 days.  There’s no use in converting this to dollars because what we want is a price offset.  Once a position is entered the turtleN  is either added to the price [short position] or subtracted from the price [long position] to determine the respective stop levels.  Look at lines 2, 8 and 17 to see how this is handled.  An additional  trade code block must be added to facilitate this stop.  Lines 17 to 28 takes care of exiting a long position when the market moves 2 N in the opposite direction.   This new stop is in addition to the highest/lowest high/low stops for the past 10 -20 days.

        atrVal = sAverage(trueRanges,20,i,1)
        turtleN = atrVal*2


            if lastTradeLoser == True :
                tradeName = "Turt20Buy"
                mp += 1
                longNExitStop = price - turtleN
                marketPosition[i] = mp
                entryPrice.append(price)
                entryQuant.append(numShares)
                curShares = curShares + numShares
                trades = tradeInfo('buy',myDate[i],tradeName,entryPrice[-1],numShares,1)
                barsSinceEntry = 1
                listOfTrades.append(trades)
#long Exit - 2 N Loss
        if mp >= 1 and myLow[i] <= longNExitStop and barsSinceEntry > 1:
            price = min(myOpen[i],longNExitStop)
            tradeName = "LongNExitLoss"
            exitDate =myDate[i]
            numShares = curShares
            exitQuant.append(numShares)
            profit,trades,curShares = exitPos(price,myDate[i],tradeName,numShares)
            if curShares == 0 : mp = marketPosition[i] = 0
            totProfit += profit
            todaysCTE = profit
            listOfTrades.append(trades)
            maxPositionL = maxPositionL - 1
Turtle Part 3

 

Turtle (Last Trade Was A Loser (LTL)) Filter – Part 2 in Series

How many of you believe if the last trade was a winner, the probability of the next trade being a loser is higher? The Turtles believed this and in this post I introduce the concept of filtering trades based on the prior trade’s success.

Here is a list of trades without the filter:


20090506    Turt20Buy  1  91.75000       0.00       0.00
20090622   Long10-Liq  1 103.14000   11290.00   11290.00
20090702   Turt20Shrt  1 102.26000       0.00       0.00
20090721   Shrt10-Liq  1 100.80000    1360.00   12650.00
20090803    Turt20Buy  1 104.61000       0.00       0.00
20090817   Long10-Liq  1 101.99000   -2720.00    9930.00
20090824    Turt20Buy  1 107.71000       0.00       0.00
20090902   Long10-Liq  1 100.97000   -6840.00    3090.00
20090902   Turt20Shrt  1 100.10000       0.00       0.00
20090917   Shrt10-Liq  1 105.87000   -5870.00   -2780.00
20090924   Turt20Shrt  1 100.02000       0.00       0.00
20091008   Shrt10-Liq  1 104.72000   -4800.00   -7580.00
20091012    Turt20Buy  1 106.13000       0.00       0.00
20091030   Long10-Liq  1 109.43000    3200.00   -4380.00
20091113   Turt20Shrt  1 108.95000       0.00       0.00
20091223   Shrt10-Liq  1 106.03000    2820.00   -1560.00

And here are the trades with filter engaged:


20090506    Turt20Buy  1  91.75000       0.00       0.00
20090622   Long10-Liq  1 103.14000   11290.00   11290.00
20090824    Turt20Buy  1 107.71000       0.00       0.00
20090902   Long10-Liq  1 100.97000   -6840.00    4450.00
20090902   Turt20Shrt  1 100.10000       0.00       0.00
20090917   Shrt10-Liq  1 105.87000   -5870.00   -1420.00
20090924   Turt20Shrt  1 100.02000       0.00       0.00
20091008   Shrt10-Liq  1 104.72000   -4800.00   -6220.00
20091012    Turt20Buy  1 106.13000       0.00       0.00
20091030   Long10-Liq  1 109.43000    3200.00   -3020.00
20100126   Turt20Shrt  1 104.09000       0.00       0.00
20100218   Shrt10-Liq  1 108.12000   -4130.00   -7150.00
20100219    Turt20Buy  1 109.37000       0.00       0.00
20100322   Long10-Liq  1 108.96000    -510.00   -7660.00

Check for yourself – you will notice that a trade after a winner is skipped. Trades are not picked back up until a loser is reported. Trading like this is quite easy but backtesting is quite a bit more difficult. I talk about this in the book where you have to switch between actual trading and simulated trading. The beauty of the Python BackTester is that it is somewhat easy to incorporate this into the testing logic. All you have to do is determine if the prior real or simulated trade is a loser. If it isn’t then you still must keep track of all trades, but don’t book the trades that follow the winner. I have created a testing module that does just that. Here is a snippet of the code:

# Short Logic
        if (mp == 0 or mp == 1) and barsSinceEntry > 1 and myLow[i] <= ll20: profit = 0 price = min(myOpen[i],ll20) numShares = max(1,int(dollarRiskPerTrade/(atrVal*myBPV))) if mp >= 1:
            profit,trades,curShares=exitPos(price,myDate[i],"RevLongLiq",curShares,lastTradeLoser)
                if lastTradeLoser < 1 : listOfTrades.append(trades) todaysCTE = profit if profit > 0 :
                    if lastTradeLoser < 0 : lastTradeLoser = 0 if lastTradeLoser > 0 : lastTradeLoser +=1
                else:
                    lastTradeLoser = -1
                mp = 0
            mp -= 1
            tradeName = "Turt20Shrt"
            marketPosition[i] = mp
            entryPrice.append(price)
            entryQuant.append(numShares)
            curShares = curShares + numShares
            trades = tradeInfo('sell',myDate[i],tradeName,entryPrice[-1],numShares,1)
            barsSinceEntry = 1
            if lastTradeLoser < 1:
               listOfTrades.append(trades) <strong># book the trade if lastTradeLoser < 1 else don't</strong>
               totProfit += profit         <strong># book the profit from the trade</strong>
Turtle Part 1

I will include this in an update to the Python backtester for registered users of this site.

Implementing Turtle Algorithm into the Python Backtester

I include the Python Backtester in my latest book “The Ultimate Algorithmic Trading System Toolbox” book.  A good tutorial on how to use it would be to program the Turtle Algorithm in three different parts.   Here is part 1:

Entry Description: Buy on stop at highest high of last twenty days.  Short on lowest low of last twenty days.

Exit Description: Exit long on stop at lowest low of last ten days.  Exit short on highest high of past ten days.

Position Sizing:  Risk 2% of simulated 100K account on each trade.  Calculate market risk by utilizing the ten day ATR.  Size(shares or contracts) = $2,000/ATR in dollars.

Python code to  input into the backtester:

 

initCapital = 100000
riskPerTrade = .02
dollarRiskPerTrade = initCapital * riskPerTrade

        hh20 = highest(myHigh,20,i,1)
        ll20 = lowest(myLow,20,i,1)
        hh10 = highest(myHigh,10,i,1)
        ll10 = lowest(myLow,10,i,1)
        hh55 = highest(myHigh,55,i,1)
        ll55 = lowest(myLow,55,i,1)
        
        atrVal = sAverage(trueRanges,10,i,1)

 #Long Entry Logic
        if (mp==0 or mp==-1) and barsSinceEntry>1 and myHigh[i]>=hh20:
            profit = 0
            price = max(myOpen[i],hh20)
            numShares = max(1,int(dollarRiskPerTrade/(atrVal*myBPV)))
            tradeName = "Turt20Buy"

 #Short Logic
        if (mp==0 or mp==1) and barsSinceEntry>1 and myLow[i] <= ll20: 
            profit = 0 
            price = min(myOpen[i],ll20) 
            numShares = max(1,int(dollarRiskPerTrade/(atrVal*myBPV)))
            tradeName = "Turt20Shrt"

 #Long Exit Loss 
        if mp >= 1 and myLow[i] <= ll10 and barsSinceEntry > 1:
            price = min(myOpen[i],ll10)
            tradeName = "Long10-Liq"

 #Short Exit Loss
        if mp <= -1 and myHigh[i] >= hh10 and barsSinceEntry > 1:
            price = max(myOpen[i],hh10)
            tradeName = "Shrt10-Liq"
Turtle Part 1

 

This snippet only contains the necessary code to use in the Python Backtester – it is not in its entirety.

This algorithm utilizes a fixed fractional approach to position sizing.  Two percent or $2000 is allocated on each trade and perceived market risk is calculated by the ten-day average true range (ATR.)   So if we risk $2000 and market risk is $1000 then 2 contracts are traded.  In Part 2, I will introduce the N risk stop and the LAST TRADE LOSER Filter.