I am creating an investment/betting simulation; since is the first I create , I am sure it can be done better, both from a coding and a simulation point of view.
I would like to ask you what you think and if you can share your experience.
There are 5 main variables that the user has to input:
z --> it is my average winning percentage. As discuss later, a random number between 0 and 100 will be generate. If the generated number (x) is greater than (z), then the trade is loss; if x < z, is a win.
invest --> how much money invested per trade, es. 10$. In case of a win, the return is always the 70%. i.e. bet 10$, win 7$ (plus the bet)
days --> how many days I trade. there is fixed amount of trade per day, which is between 9 and 33
simulazioni --> the number of simulations
varianza --> is the shifting probability. If varianza is 5% and my percentage (z) is 50%, my actual variance could be either 45% or 55%. This is calculated randomly for each trade.
As you can see from the code at the bottom, the simulation is based on "for loops". The main one is the simulations, second one are the days and the last one calculates if the trading is positive or negative.
There are also dictionaries that I use to collect data from daily profit and total profit (i.e. "datatot"; these dictionaries are used to plot data later on (this bit is not included in the code).
import random
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
import numpy as np
data=[]
data2=[]
datatot = []
portafoliotot = 0
portafolio = 0
pos= 0
neg= 0
#input variables
z= input("Percentuale: ")
invest = input("Investimento:")
days = input ("Giorni:")
simulazioni = input("Simulazioni:")
varianza = input("Varianza:")
for simulazione in range(0,int(simulazioni)):
for giorno in range(0,int(days)):
portafoliotot = 0 #simulation portfolio
portafolio = 0 #daily portfolio
pos = 0 #n of positive trades
neg = 0 #n of negative trades
for trade in range(0,int(random.randint(9,33))):
x=random.randint(0,100) #random number to see if the trade is pos or neg
calcvarianza = 2 * np.random.rand() - 1 # calculation of (varianza). if positive, is going to be added to (z), otherwise subtracted. this is done for each trade.
if calcvarianza > 0:
varianza = - int(varianza)
if x<(int(z)+int(varianza)): #checking if the trade is pos or neg
portafolio += (int(invest)/100)*70
pos +=1
else:
portafolio += -int(invest)
neg +=1
portafoliotot += portafolio
data.append(portafoliotot)
data2.append(portafoliotot)
datatot.append(sum(data2))
data2 = []
portafolio += -int(invest)
why not just-=
? Maybe that's just me, but I think that using your native language in code (English isn't native language for me too by the way) is kind a bad decision. For example you may have to show it on a site like this and everybody would understand your code easier if it would be in English. \$\endgroup\$True
orFalse
instead ofif 2 * np.random.rand() - 1
you can just doif random.randbits(1)
\$\endgroup\$