Creating a trading system from scratch

How many lines of code you are comfortable with


  • Total voters
    61

VJAY

Well-Known Member
i think that will add a layer of complexity because right now we are just generating orders and pushing it through system....now we have to generate orders track it real time n then generate order file at 9.20
Ohh yes...
 
Hey guys just one issues...i have been trying to figure out how to divide capital with weights so it it will show how much capital to put besides weights....and i have been getting wrong percentage of annual returns...please if someone can help it would be really helpful
Python:
import pandas as pd
import numpy as np
import pandas_datareader.data as web
import matplotlib.pyplot as plt
import scipy.optimize as spo
import math
#Fetch data from yahoo and save under DataFrame named 'data'
stock = ['ACCELYA.NS', 'THOMASCOOK.NS', 'GNFC.NS', 'GRAPHITE.NS','MOIL.NS','KMCSHIL.BO', 'VINATIORGA.NS']
data = web.DataReader(stock,data_source="yahoo",start='07/01/2015',end='today')['Adj Close']
#Arrange the data in ascending order
data=data.iloc[::-1]
print(data.round(2))


#Compute stock returns and print the returns in percentage format
stock_ret = data.pct_change()*252
print (stock_ret.round(4)*100)
mybenchmark_return=web.DataReader('^NSEI',data_source="yahoo",start='07/01/2015',end='today')
#Calculate mean returns and covariances of all four the stocks
mean_returns = stock_ret.mean()
cov_matrix = stock_ret.cov()
print (mean_returns)
print (cov_matrix)

capital= 20000

#Set the number of iterations to 10000 and define an array to hold the simulation results; initially set to all zeros
num_iterations = 1000000
simulation_res = np.zeros((4+len(stock)-1,num_iterations))



for i in range(num_iterations):
#Select random weights and normalize to set the sum to 1
        weights = np.array(np.random.random(7))
        weights /= np.sum(weights)
        weights1 = weights/capital
#Calculate the return and standard deviation for every step
        portfolio_return = np.sum(mean_returns * weights)
        portfolio_std_dev = np.sqrt(np.dot(weights.T,np.dot(cov_matrix, weights)))
#Store all the results in a defined array
        simulation_res[0,i] = portfolio_return
        simulation_res[1,i] = portfolio_std_dev
#Calculate Sharpe ratio and store it in the array
        simulation_res[2,i] = simulation_res[0,i] / simulation_res[1,i]
#Save the weights in the array
        for j in range(len(weights)):
                simulation_res[j+3,i] = weights[j]



sim_frame = pd.DataFrame(simulation_res.T,columns=['ret','stdev','sharpe',stock[0],stock[1],stock[2],stock[3],stock[4],
                                                   stock[5],stock[6]])
print (sim_frame.head (5))
print (sim_frame.tail (5))


#Spot the position of the portfolio with highest Sharpe Ratio
max_sharpe = sim_frame.iloc[sim_frame['sharpe'].idxmax()]
#Spot the position of the portfolio with minimum Standard Deviation
min_std = sim_frame.iloc[sim_frame['stdev'].idxmin()]
print ("The portfolio for max Sharpe Ratio:\n", max_sharpe.round(3)*100)
print ("The portfolio for min risk:\n", min_std.round(3)*100)

#Create a scatter plot coloured by various Sharpe Ratios with standard deviation on the x-axis and returns on the y-axis
plt.scatter(sim_frame.stdev,sim_frame.ret,c=sim_frame.sharpe,cmap='RdYlBu')
plt.xlabel('Standard Deviation')
plt.ylabel('Returns')
plt.ylim(0,.003)
plt.xlim(0.0075,0.012)
#Plot a red star to highlight position of the portfolio with highest Sharpe Ratio
plt.scatter(max_sharpe[1],max_sharpe[0],marker=(6,1,0),color='r',s=700)
#Plot a blue star to highlight position of the portfolio with minimum Variance
plt.scatter(min_std[1],min_std[0],marker=(6,1,0),color='b',s=700)
plt.show()
 
Last edited:

VJAY

Well-Known Member
It is not a new notebook. Its a python script - a python file (that ends with .py)
To run a python script
  1. Open command prompt
  2. Navigate to the folder
  3. Type python script_name.py
A python file is usually called a script if it has this code (something similar to this code) at the end of the file
Python:
if __name__ == "__main__":
    main()
Dear UB,
I still unable to use this script code....also is it not open through jupyter notebook?
I tryed from command prompt but its not recognise file....
1540778966214.png
 

UberMachine

Well-Known Member
i think that will add a layer of complexity because right now we are just generating orders and pushing it through system....now we have to generate orders track it real time n then generate order file at 9.20
Yes. but its managebale; we are going to add an another column and name it openafter5min and calculate the price from it and adding the above conditions. But we need data for it.
 

UberMachine

Well-Known Member
Hey guys just one issues...i have been trying to figure out how to divide capital with weights so it it will show how much capital to put besides weights....and i have been getting wrong percentage of annual returns...please if someone can help it would be really helpful
Python:
import pandas as pd
import numpy as np
import pandas_datareader.data as web
import matplotlib.pyplot as plt
import scipy.optimize as spo
import math
#Fetch data from yahoo and save under DataFrame named 'data'
stock = ['ACCELYA.NS', 'THOMASCOOK.NS', 'GNFC.NS', 'GRAPHITE.NS','MOIL.NS','KMCSHIL.BO', 'VINATIORGA.NS']
data = web.DataReader(stock,data_source="yahoo",start='07/01/2015',end='today')['Adj Close']
#Arrange the data in ascending order
data=data.iloc[::-1]
print(data.round(2))


#Compute stock returns and print the returns in percentage format
stock_ret = data.pct_change()*252
print (stock_ret.round(4)*100)
mybenchmark_return=web.DataReader('^NSEI',data_source="yahoo",start='07/01/2015',end='today')
#Calculate mean returns and covariances of all four the stocks
mean_returns = stock_ret.mean()
cov_matrix = stock_ret.cov()
print (mean_returns)
print (cov_matrix)

capital= 20000

#Set the number of iterations to 10000 and define an array to hold the simulation results; initially set to all zeros
num_iterations = 1000000
simulation_res = np.zeros((4+len(stock)-1,num_iterations))



for i in range(num_iterations):
#Select random weights and normalize to set the sum to 1
        weights = np.array(np.random.random(7))
        weights /= np.sum(weights)
        weights1 = weights/capital
#Calculate the return and standard deviation for every step
        portfolio_return = np.sum(mean_returns * weights)
        portfolio_std_dev = np.sqrt(np.dot(weights.T,np.dot(cov_matrix, weights)))
#Store all the results in a defined array
        simulation_res[0,i] = portfolio_return
        simulation_res[1,i] = portfolio_std_dev
#Calculate Sharpe ratio and store it in the array
        simulation_res[2,i] = simulation_res[0,i] / simulation_res[1,i]
#Save the weights in the array
        for j in range(len(weights)):
                simulation_res[j+3,i] = weights[j]



sim_frame = pd.DataFrame(simulation_res.T,columns=['ret','stdev','sharpe',stock[0],stock[1],stock[2],stock[3],stock[4],
                                                   stock[5],stock[6]])
print (sim_frame.head (5))
print (sim_frame.tail (5))


#Spot the position of the portfolio with highest Sharpe Ratio
max_sharpe = sim_frame.iloc[sim_frame['sharpe'].idxmax()]
#Spot the position of the portfolio with minimum Standard Deviation
min_std = sim_frame.iloc[sim_frame['stdev'].idxmin()]
print ("The portfolio for max Sharpe Ratio:\n", max_sharpe.round(3)*100)
print ("The portfolio for min risk:\n", min_std.round(3)*100)

#Create a scatter plot coloured by various Sharpe Ratios with standard deviation on the x-axis and returns on the y-axis
plt.scatter(sim_frame.stdev,sim_frame.ret,c=sim_frame.sharpe,cmap='RdYlBu')
plt.xlabel('Standard Deviation')
plt.ylabel('Returns')
plt.ylim(0,.003)
plt.xlim(0.0075,0.012)
#Plot a red star to highlight position of the portfolio with highest Sharpe Ratio
plt.scatter(max_sharpe[1],max_sharpe[0],marker=(6,1,0),color='r',s=700)
#Plot a blue star to highlight position of the portfolio with minimum Variance
plt.scatter(min_std[1],min_std[0],marker=(6,1,0),color='b',s=700)
plt.show()
Python:
# Since weights add up to one
capital_per_stock = capital * weights
 
Python:
# Since weights add up to one
capital_per_stock = capital * weights
Python:
#Import relevant libraries
import pandas as pd
import numpy as np
import pandas_datareader.data as web
import matplotlib.pyplot as plt
import scipy.optimize as spo
import math
#Fetch data from yahoo and save under DataFrame named 'data'
stock = ['ACCELYA.NS', 'THOMASCOOK.NS', 'GNFC.NS', 'GRAPHITE.NS','MOIL.NS','KMCSHIL.BO', 'VINATIORGA.NS']
data = web.DataReader(stock,data_source="yahoo",start='07/01/2015',end='today')['Adj Close']
#Arrange the data in ascending order
data=data.iloc[::-1]
print(data.round(2))


#Compute stock returns and print the returns in percentage format
stock_ret = data.pct_change()*252
print (stock_ret.round(4)*100)
mybenchmark_return=web.DataReader('^NSEI',data_source="yahoo",start='07/01/2015',end='today')
#Calculate mean returns and covariances of all four the stocks
mean_returns = stock_ret.mean()
cov_matrix = stock_ret.cov()
print (mean_returns)
print (cov_matrix)

capital= 20000

#Set the number of iterations to 10000 and define an array to hold the simulation results; initially set to all zeros
num_iterations = 10
simulation_res = np.zeros((4+len(stock)-1,num_iterations))


for i in range(num_iterations):
#Select random weights and normalize to set the sum to 1
        weights = np.array(np.random.random(7))
        weights /= np.sum(weights)
        capital_per_stock=capital*weights
#Calculate the return and standard deviation for every step
        portfolio_return = np.sum(mean_returns * weights)
        portfolio_std_dev = np.sqrt(np.dot(weights.T,np.dot(cov_matrix, weights)))
#Store all the results in a defined array
        simulation_res[0,i] = portfolio_return
        simulation_res[1,i] = portfolio_std_dev
#Calculate Sharpe ratio and store it in the array
        simulation_res[2,i] = simulation_res[0,i] / simulation_res[1,i]
#Save the weights in the array
        for j in range(len(weights)):
                simulation_res[j+3,i] = weights[j]



sim_frame = pd.DataFrame(simulation_res.T,columns=['ret','stdev','sharpe',stock[0],stock[1],stock[2],stock[3],stock[4],
                                                   stock[5],stock[6]])
print (sim_frame.head (5))
print (sim_frame.tail (5))


#Spot the position of the portfolio with highest Sharpe Ratio
max_sharpe = sim_frame.iloc[sim_frame['sharpe'].idxmax()]
#Spot the position of the portfolio with minimum Standard Deviation
min_std = sim_frame.iloc[sim_frame['stdev'].idxmin()]
Capital_max_sharpe = sim_frame.iloc[sim_frame[max_sharpe*capital_per_stock]]
Capital_min_sharpe =sim_frame.iloc[sim_frame[min_std*capital_per_stock]]
print ("The portfolio for max Sharpe Ratio:\n", max_sharpe.round(3)*100)
print ("The portfolio for min risk:\n", min_std.round(3)*100)
print("Capital required:\n",Capital_max_sharpe.round(3)*100)
print("Capital required:\n",Capital_min_sharpe.round(3)*100)
#Create a scatter plot coloured by various Sharpe Ratios with standard deviation on the x-axis and returns on the y-axis
plt.scatter(sim_frame.stdev,sim_frame.ret,c=sim_frame.sharpe,cmap='RdYlBu')
plt.xlabel('Standard Deviation')
plt.ylabel('Returns')
plt.ylim(0,.003)
plt.xlim(0.0075,0.012)
#Plot a red star to highlight position of the portfolio with highest Sharpe Ratio
plt.scatter(max_sharpe[1],max_sharpe[0],marker=(6,1,0),color='r',s=700)
#Plot a blue star to highlight position of the portfolio with minimum Variance
plt.scatter(min_std[1],min_std[0],marker=(6,1,0),color='b',s=700)
plt.show()
I m still face the problem with the code dear @UberMachine can you help me
Just run it once in your machine n find out what is the problem and tell me please...!!!
 

Attachments

Hey no further development of the system?
The backtest file doesnt take sector file into account so it must be giving improper results..!!
And one more thing can we add india vix in both system and backtest !!!
It would be really helpful if we restart the further development of system...!!!
 

VJAY

Well-Known Member
Hey no further development of the system?
The backtest file doesnt take sector file into account so it must be giving improper results..!!
And one more thing can we add india vix in both system and backtest !!!
It would be really helpful if we restart the further development of system...!!!
UB not active here nowdays ..he might be busy with his developments...I too stuck at the point where when he introduced script.....
 
Master must have cracked two three more alphas and high possibility must be working with with intraday data, he is Uber machine after all,only disciplined people earn and keep profits in long term, returns are slow so I lost interest but still this simple system not only beats but puts dust on any investment instruments by a wide margin.
 
Last edited:

Similar threads