Programming A Swing System in TradingSimula-18 and Python

Swing-Pivot System in TS-18

Hello I just released a video that details this system  Here is the pseudo-code.

  1. Start off looking for the completion of 6 daily bars
  2. If Looking for High Pivot: UpCnt +=1
    1. UpCnt > 6 – Once six bars has transpired then start looking for a High Pivot Pt
      1. myHigh[D1] > myHigh[D2] and myHigh[D1] > myHigh[D]
        1. if High Pivot found
          1. BuyLevel =  myHigh[D1] + minMove
          2. SellLevel = 0
          3. LookingForHighPivot = False
          4. LookingForLowPivot = True
          5. DnCnt = 0
  3. If Looking for Low Pivot: DnCnt +=1
    1. DnCnt > 6 Once six bar has transpired since HighPivot look for a Low Pivot Pt
      1. myLow[D1]< myLow[D2] and myLow[D1] < myLow[D]
        1. IF Low Pivot found
          1. ShortLevel = myLow[D1] – minMove
          2. BuyLevel = 99999999
          3. LookingForLowPivot = False
          4. LookingForHighPivot = True
          5. UpCnt = 0
  4. Buy at BuyLevel Stop
  5. Short at ShortLevel Stop
  6. ExitLong at lowest(myLow,8,D,1) stop
  7. ExitShort at highest(myHigh,8,D,1) stop
#----------------------------------------------------------------------------------
# Set up algo parameters here
#----------------------------------------------------------------------------------
startTestDate = 20000101 #must be in yyyymmdd
stopTestDate = 99999999 #must be in yyyymmdd
rampUp = 50 # need this minimum of bars to calculate indicators
sysName = 'Swing-Pivot' #System Name here
initCapital = 500000
commission = 50

#--------------------------------------------------------------------------------
# If you want to ignore a bunch of non-eseential stuff then
# S C R O L L A L M O S T H A L F W A Y D O W N
#--------------------------------------------------------------------------------
#TradingSimula18.py - programmed by George Pruitt
#Built on the code and ideas from "The Ultimate Algorithmic Tradins System T-Box"
#Code is broken into sections
#Most sections can and should be ignored
#Each trading algorithm must be programmed with this template
#This is the main entry into the platform
#--------------------------------------------------------------------------------
#Import Section - inlcude functions, classes, variables from external modules
#--------------------------------------------------------------------------------
# --- Do not change below here
from getData import *
from equityDataClass import *
from tradeClass import *
from systemMarket import *
from indicators import highest,lowest,rsiClass,stochClass,sAverage,bollingerBands,\
adxClass,sAverage2,laguerreRSIClass
from portfolio import *
from systemAnalytics import *
from utilityFunctions import *
from portManager import *
from positionMatrixClass import *
from barCountCalc import *
from sectorClass import sectorClass, parseSectors, numPosCurrentSector,getCurrentSector, \
defineSectorUniverse
from tradeProcessing import *
from sysMarkDictionary import *
#-------------------------------------------------------------------------------------------------
# Pay no attention to these two functions - unless you want to
#-------------------------------------------------------------------------------------------------
def unPackDict(aDict):
global mp, todaysCTE,curShares,cumuProfit,barsSinceEntry,barsSinceExit,entryPrice,entryQuant,\
sectorTradesTodayList,curSector
mp = aDict.get("mp")
todaysCTE = aDict.get("todaysCTE")
curShares = aDict.get("curShares")
cumuProfit = aDict.get("cumuProfit")
barsSinceEntry = aDict.get("barsSinceEntry")
barsSinceExit = aDict.get("barsSinceExit")
entryPrice = aDict.get("entryPrice")
entryQuant = aDict.get("entryQuant")
# sectorTradesTodayList[curSector] = aDict.get("sectorTradesToday")
# marketMonitorList[curMarket] = aDict.get("marketMonitorList")

#-------------------------------------------------------------------------------------------------
marketList = getData() # loads data from .csv or .por file and sets attributes
#-------------------------------------------------------------------------------------------------

#-----initialize first set of internals here -- I G N O R E
marketMonitorList,masterDateList,masterDateGlob,entryPrice = ([] for i in range(4))
buy = entry = 1; sell = exit = -1; ignore = 0;
entryQuant,exitQuant,trueRanges,myBPVList = ([] for i in range(4))
myComNameList,myComLongNameList,myMinMoveList,systemMarketList = ([] for i in range(4))
marketVal1,marketVal2,marketVal3,marketVal4 = ([] for i in range(4))
cond1,cond2,cond3,cond4 = ([] for i in range(4))
marketList1,marketList2,marketList3,marketList4 = ([] for i in range(4))
portManager = portManagerClass();portfolio = portfolioClass()
sysMarkDict = dict();cumuProfit = 0
#------completed initializing set of first internals -- I G N O R E

numMarkets = len(marketList);positionMatrix = positionMatrixClass();positionMatrix.numMarkets = numMarkets
firstMarketLoop = True; dailyPortCombEqu = portEquItm = barsSinceEntry = barsSinceExit = curShares = 0

#---------------------------------------------------------------------------------
# Do not change code below
#---------------------------------------------------------------------------------
for curMarket in range(0,numMarkets):
systemMarkTracker = systemMarkTrackerClass()
equity = equityClass()
systemMarkTracker.setSysMarkTrackingData(marketList[curMarket])
systemMarkTracker.setSysMarkTrackingEquity(equity)
marketMonitorList.append(systemMarkTracker)
myBPV,myComName,myComLongName,myMinMove= getDataAtribs(marketMonitorList[curMarket].marketData)
myBPVList.append(myBPV);myComNameList.append(myComName);myMinMoveList.append(myMinMove)
myComLongNameList.append(myComLongName)
marketList1.append([]);marketList2.append([]);
marketList3.append([]);marketList4.append([])
cond1.append(False);cond2.append(False);cond3.append(False),cond4.append(False)
marketVal1.append(0);marketVal2.append(0);marketVal3.append(0),marketVal4.append(0)
masterDateGlob += marketMonitorList[curMarket].marketData.date
positionMatrix.marketNames.append(myComNameList[curMarket])
masterDateList = removeDuplicates(masterDateGlob);masterDateList = sorted(masterDateList)
#---------------------------------------------------------------------------------
# Optional - use this area to create user lists and
# lists of indicator classes - include the list in the loop
# if you need to instantiate or initialize
#---------------------------------------------------------------------------------

adxList = list()
for curMarket in range(0,numMarkets):
adxList.append(adxClass())

#---------------------------------------------------------------------------------
# Set Up Sectors Here - remember you must use the exact symbol for each marketData
# Review process in book if necessary - predfined for Pinnalce and CSI data
#---------------------------------------------------------------------------------
sectorList = list()
sectorTradesTodayList = list()
numSectors = 7
defineSectorUniverse(sectorList,"Pinnacle",myComNameList,numSectors)
#---------------------------------------------------------------------------------
# Do not change code below
#---------------------------------------------------------------------------------
barCount, endBarCount = barCountCalc(masterDateList,startTestDate,stopTestDate,rampUp)
portEquItm = barsSinceEntry = 0;dailyPortCombEqu = initCapital


for curPortBar in range(barCount,endBarCount+1):
portManager.portDate.append(masterDateList[curPortBar])
if curPortBar % 225 == 0 : print("Working on bar #: ",curPortBar)
for curMarket in range(0,numMarkets):
if curMarket == 0 : indivMktAccum = initCapital
myDate,myOpen,myHigh,myLow,myClose,myVolume,myOpInt,myRange,myTrueRange = setDataLists(marketMonitorList[curMarket].marketData)
equItm = marketMonitorList[curMarket].equItm;equItm += 1
myBPV = myBPVList[curMarket];myComName = myComNameList[curMarket];myMinMove = myMinMoveList[curMarket]
if curMarket == 0 : sectorTradesTodayList = [0] * numSectors
if myComName not in portManager.marketSymbols:
portManager.marketSymbols.append(myComName)
portManager.numConts.append(1)
curShares = 0;todaysCTE = todaysOTE = 0;mktsToday = 0
#---------------------------------------------------------------------------------
# Do not change code above
#---------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------
# Assign lists based on marketMonitor here - assigning temp lists here is great idea
#--------------------------------------------------------------------------------------------------
curTradesList = marketMonitorList[curMarket].tradesList
curMarketData = marketMonitorList[curMarket].marketData
#---------------------------------------------------------------------------------
# Do not change code below
#---------------------------------------------------------------------------------

if masterDateList[curPortBar] in marketMonitorList[curMarket].marketData.date:
mp = 0;curBar = marketMonitorList[curMarket].marketData.date.index(masterDateList[curPortBar])
numSectorPositions = numPosCurrentSector(sectorList,myComName,myComNameList,positionMatrix.posMatrixSize[-numMarkets:])
numSectorPositions = 0
curSector = getCurrentSector(myComName,sectorList)
if len(marketMonitorList[curMarket].mp)!=0: mp = marketMonitorList[curMarket].mp[-1]
myADX,myADXR,myDMI = adxList[curMarket].calcADX(myHigh,myLow,myClose,14,curBar,1)
entryPrice,entryQuant,curShares,cumuProfit,barsSinceEntry,barsSinceExit = \
marketMonitorList[curMarket].getSysMarkTrackingInfo()
setSysMarkDict(sysMarkDict,todaysCTE,barsSinceEntry,barsSinceExit,curShares,mp,marketMonitorList[curMarket],curMarket,myDate, \
entryPrice,entryQuant,curBar,cumuProfit,myBPV,commission,sectorTradesTodayList,curSector)
D = curBar;D1 = curBar-1;D2 = curBar-2;D3 = curBar-3;D4 = curBar-4;D5 = curBar-5
D6 = curBar-6;D7 = curBar-7;D8 = curBar-8;D9 = curBar-9;D10 = curBar-10
#---------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------
# Start programming your great trading ideas below here - don't touch stuff above
#---------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------
# Define Long, Short, ExitLong and ExitShort Levels - mind your indentations
# upBand,dnBand, midBand = bollingerBands(myDate,myClose,80,2,curBar,1)

# Set up bar/market lists - use built-in storage
# marketList1[curMarket].append(lastUp)

lastUp = cond1
upCounter = marketVal1
dnCounter = marketVal2
buyLevel = marketVal3
shortLevel = marketVal4

if curMarket == 0 and firstMarketLoop == True:
buyLevel[curMarket] = 999999999
shortLevel[curMarket] = 0

if lastUp[curMarket] == False:
upCounter[curMarket] +=1
if upCounter[curMarket] > 6:
if myHigh[D1] > myHigh[D2] and myHigh[D] < myHigh[D1]:
buyLevel[curMarket] = myHigh[D1] + myMinMove
shortLevel[curMarket] = 0 #can only buy on lastUp
dnCounter[curMarket] = 0
lastUp[curMarket] = True
print(myDate[curBar]," Found Hi-Pivot ",upCounter[curMarket])
else:
dnCounter[curMarket] +=1
if dnCounter[curMarket] > 6:
if myLow[D1] < myLow[D2] and myLow[D] > myLow[D1]:
shortLevel[curMarket] = myLow[D1] - myMinMove
buyLevel[curMarket] = 99999999 #can only short on lastDn
upCounter[curMarket] = 0
lastUp[curMarket] = False
print(myDate[curBar]," Found Low-Pivot ",dnCounter[curMarket])

trailB = highest(myHigh,8,D,1)
trailS = lowest(myLow,8,D,1)

posSize = 1

# Long Exit
if mp == 1 and myLow[D] <= trailS and trailS > shortLevel[curMarket] and barsSinceEntry > 1:
price = min(myOpen[D],trailS)
tradeName = "Lxit"
numShares = curShares
exitPosition(price, curShares, tradeName, sysMarkDict)
unPackDict(sysMarkDict)
# Short Entry
# Okay Let's put in some logic to create a short position
if myLow[D] <= shortLevel[curMarket] and mp !=-1:
price = min(myOpen[D],shortLevel[curMarket])
tradeName = "PivtSwng-S"
numShares = posSize
enterShortPosition(price, numShares, tradeName,sysMarkDict)
unPackDict(sysMarkDict)
# Short Exit
if mp == -1 and myHigh[D] >= trailB and trailB < buyLevel[curMarket] and barsSinceEntry > 1:
price =max(myOpen[curBar],trailB)
tradeName = "Sxit"
numShares = curShares
exitPosition(price, curShares, tradeName,sysMarkDict)
unPackDict(sysMarkDict)
# Long Entry
# Okay Let's put in some logic to create a long position
if myHigh[D] >= buyLevel[curMarket] and mp !=1 and barsSinceExit > 1:
price = max(myOpen[D],buyLevel[curMarket])
tradeName = "PivtSwng-B"
numShares = posSize
enterLongPosition(price,posSize,tradeName,sysMarkDict)
unPackDict(sysMarkDict)

#----------------------------------------------------------------------------------------------------------------------------
# - Do not change code below - trade, portfolio accounting - our great idea should stop here
#----------------------------------------------------------------------------------------------------------------------------
if mp != 0 :barsSinceEntry += 1
if mp == 0: barsSinceExit +=1
todaysOTE = calcTodaysOTE(mp,marketList[curMarket].close[curBar],marketMonitorList[curMarket].entryPrice,marketMonitorList[curMarket].entryQuant,myBPV)
marketMonitorList[curMarket].setSysMarkTrackingInfoB(barsSinceEntry,barsSinceExit,curShares,equItm)
marketMonitorList[curMarket].equity.setEquityInfo(marketList[curMarket].date[curBar],equItm,todaysCTE,todaysOTE)
portManager.individEquity.append((curMarket,marketMonitorList[curMarket].equity.dailyEquityVal[-1]))
indivMktAccum += portManager.individEquity[portEquItm][1]
portEquItm += 1
if curPortBar == endBarCount:
if mp >= 1:
price = marketList[curMarket].close[curBar]
exitPosition(price, curShares, "Lx-EOFData",sysMarkDict)
unPackDict(sysMarkDict)
if mp <= -1:
price = marketList[curMarket].close[curBar]
exitPosition(price, curShares, "Sx-EOFData",sysMarkDict)
unPackDict(sysMarkDict)
else:
equityStreamLen = len(marketMonitorList[curMarket].equity.dailyEquityVal)
if equityStreamLen > 0: portManager.individEquity.append((curMarket,marketMonitorList[curMarket].equity.dailyEquityVal[-1]))
else: portManager.individEquity.append((curMarket,0.0))
indivMktAccum += portManager.individEquity[portEquItm][1]
portEquItm += 1
if curMarket == numMarkets - 1 and firstMarketLoop == True: firstMarketLoop = False
dailyPortCombEqu = indivMktAccum
portManager.combinedEquity.append(dailyPortCombEqu)
positionMatrix.posMatrixDate.append(masterDateList[curPortBar])
for mktCnt in range(0,len(marketMonitorList)):
positionMatrix.posMatrixSize.append(marketMonitorList[mktCnt].curShares)

for j in range(0,numMarkets):
systemMarket = systemMarketClass()
systemMarket.setSysMarkInfo(sysName,myComNameList[j],myComLongNameList[j],marketMonitorList[j].tradesList,marketMonitorList[j].equity,initCapital)
systemMarketList.append(systemMarket)
#sectors.setSectorsInfo(numSectors,systemMarketList)
positionMatrix.printPositionMatrix(systemMarketList,portManager)
portfolio.setPortfolioInfo("PortfolioTest",systemMarketList)
parseSectors(sectorList,systemMarketList)
#plotEquityCurve(portfolio)
calcSystemResults(systemMarketList)
Take A Look at the latest Refactored TS-18

Remember to check out the video at YouTube too!

 

classic

yes

About This Site

This site is home to George’s Excellent Adventure into TradingSimula_18 and Python.  George grew tired of the old and expensive back testing software so he created his own and now is able to test and develop  Trend Following Systems utilizing EOD data and EOD intra-testing portfolio management.  This software, TradingSimula_18 can be found in his Trend Following Systems: A DIY Project – Batteries Included book – now in its 2nd edition.

December 2024
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
3031