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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

#Long Exit - Loss

trades = tradeInfo('buy',myDate[i],tradeName,

entryPrice[-1],numShares,1)

barsSinceEntry = 1

totProfit += profit

listOfTrades.append(trades)

if mp >= 1 and myLow[i] <= entryPrice[-1] - stopAmt

and barsSinceEntry > 1:

price = min(myOpen[i],entryPrice[-1] - stopAmt)

tradeName = "L-MMLoss"

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

# Long Exit - Bollinger Based

if mp >= 1 and myLow[i] <= exitLevel:

price = min(myOpen[i],exitLevel)

tradeName = "L-BollExit"

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

This is just the code for long entries and exits, and I must admit it looks a little

bit ugly. Like I mentioned earlier, everything is exposed and the user has to do a

little more of the programming that is taken care of behind the scenes with software

such as AmiBroker and TradeStation. However, if you can program in the PSB, you

can program anywhere.

The first thing we need to calculate is the Bollinger Bands. We are going to buy

on a stop when the high of the day exceeds a 60-day two-deviation upper band.

195

USING PYTHON TO BACKTEST YOUR ALGORITHM

www.rasabourse.com

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

Saved successfully!

Ooh no, something went wrong!