31.07.2021 Views

Ultimate Algorithmic Trading System

Using automated systems for trading in stock markets

Using automated systems for trading in stock markets

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

312

PYTHON SYSTEM BACKTESTER

def calcTradeProfit(self,order,curPos,entryPrice,

exitPrice,entryQuant,numShares):

profit = 0

totEntryQuant = 0

tempNumShares = numShares

numEntriesLookBack = 0

for numEntries in range(0,len(entryPrice)):

## totEntryQuant += entryQuant[numEntries]

if tempNumShares >= entryQuant[numEntries]:

tempNumShares -= entryQuant[numEntries]

numEntriesLookBack += 1

if tempNumShares > 0 : numEntriesLookBack += 1

tempNumShares = numShares

for numEntries in range(0,numEntriesLookBack):

if numEntries < 0:

numEntries = 1

if entryQuant[numEntries] < tempNumShares:

peelAmt = entryQuant[numEntries]

tempNumShares = tempNumShares - peelAmt

if entryQuant[numEntries] >= tempNumShares:

peelAmt = tempNumShares

if order == ’buy’:

if curPos < 0:

profit = profit + (entryPrice[numEntries]

- exitPrice) * peelAmt

elif order == ’sell’:

if curPos > 0:

profit = profit + (exitPrice

- entryPrice[numEntries])

* peelAmt

elif order == ’liqLong’:

if curPos > 0:

profit = profit + (exitPrice

- entryPrice[numEntries])

* peelAmt

elif order == ’liqShort’:

if curPos < 0:

profit = profit + (entryPrice[numEntries]

- exitPrice) * peelAmt

if entryQuant[numEntries] == peelAmt :

entryPrice.pop(numEntries)

entryQuant.pop(numEntries)

www.rasabourse.com

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!