Skip to content

Instantly share code, notes, and snippets.

@eklitzke
Created April 6, 2010 03:35
Show Gist options
  • Save eklitzke/357200 to your computer and use it in GitHub Desktop.
Save eklitzke/357200 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
#
# Simulates a leveraged ETF, with a stop-loss ability. The default model assumes
# that you guess right 1/3 times, and when you guess right the ETF increases in
# value by 4% and when you guess wrong the ETF decreases in value by 2%.
import optparse
import random
def main(opts):
capital = opts.principal
for x in xrange(opts.runs):
if random.random() < 1.0 / opts.accuracy:
change = capital * opts.volatility / 100.0
capital += change
msg = '+$%1.2f' % (change,)
else:
change = capital * opts.stop_volatility / 100.0
capital -= change
msg = '-$%1.2f' % (change,)
msg += ' ' * (10 - len(msg))
msg += '$%1.2f' % (capital,)
print msg
print 'final: $%1.2f' % capital
if __name__ == '__main__':
parser = optparse.OptionParser()
parser.add_option('--accuracy', dest='accuracy', type='float', default=3.0,
help='1/N times you guess right; default N = 3')
parser.add_option('--principal', dest='principal', type='float', default=1000.0,
help='how much capital you start with')
parser.add_option('--volatility', dest='volatility', type='float', default=4.0,
help='the percentage increase when you guess right; default is 4%')
parser.add_option('--stop-volatility', dest='stop_volatility', type='float', default=2.0,
help='if you guess wrong, you lose this amount; default is 2%')
parser.add_option('--runs', dest='runs', type='int', default=100,
help='how many days to simulate')
opts, args = parser.parse_args()
main(opts)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment