Stock Backtester Question

LakeDog

New Member
Joined
Oct 18, 2017
Messages
18
Office Version
  1. 365
Platform
  1. Windows
Trying to do a stock backtester for excel. Have the data, the indices and even the entry and exit as well as the stop-loss in the spreadsheet. The issue I'm having is that the Exit and Stop-loss points, because the criteria extends for several rows, I get a value for multiple rows and I only need the first one. I've tried to add the IF statement that excludes if the cell prior is "0" but that only works for one cell, then the third one has the value again.

Looking for any options and thoughts.

Also, if there are any templates out there that I could learn from, would appreciate it.

Thanks!!!
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
I use this simple macro which is very easy to use. It does multiple entry and exits and puts the profit in percentage terms in a column of your choice. It needs a column for entry triggers and a column for exit triggers ( TRUE/FALSE)
You just set the numbers in the macro to tie up with those. You set the start and end rows as well:
It assumes that the stock prices are in column B , however you can change this by changing the line indicated in the comments
Note I have also done this sort of macro which includes the stoploss and multiple exit terms and trailing stoploss, you can make it a sophisticated as you want.

Code:
Sub adaptema()
' Set all these numbers to the columns and rows you want 
buycol = 23
sellcol = 24
profitcol = 25
firstrow = 4
LastRow = 98
' ***********
bytrig = Range(Cells(firstrow, buycol), Cells(LastRow, buycol))
selltrig = Range(Cells(firstrow, sellcol), Cells(LastRow, sellcol))
Range(Cells(firstrow, profitcol), Cells(LastRow, profitcol)) = ""


Profit = Range(Cells(firstrow, profitcol), Cells(LastRow, profitcol))
' closep are the close prices, change the column number to change this from Column B
closep = Range(Cells(firstrow, 2), Cells(LastRow, 2))
Longf = False
For I = 1 To (LastRow - firstrow)
 If Not (Longf) Then
   If bytrig(I, 1) Then
     buyp = closep(I, 1)
     Longf = True
   End If
  Else
'    If bytrig(i - 5, 1) Then
    
    If selltrig(I, 1) Then
     Profit(I, 1) = 100 * (closep(I, 1) - buyp) / buyp
     Longf = False
     End If
  End If
Next I
Range(Cells(firstrow, profitcol), Cells(LastRow, profitcol)) = Profit


End Sub
 
Last edited:
Upvote 0
Thanks offthelip, I'm not so hot with macros, but part of this is to do it for fun and learn, so I'll dive in and give it a try. I'll see if I can interpret each line first and then apply it.

Your help and suggestion are greatly appreciated!!
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,312
Members
452,634
Latest member
cpostell

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top