Python Forum
error on stock indicator code on balance volume
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
error on stock indicator code on balance volume
#19
def backtest( price1,init_capital, max_capital_deploy, buy_margin, sell_margin):
    longwindow = int(parameters[0])
    shortwindow = int(parameters[1])
    if (longwindow < shortwindow) or (shortwindow < 1) or (init_capital <= 0) or (max_capital_deploy <=0) or (buy_margin < 0) or (sell_margin < 0)  :
        return 0
    short_moving_avg = data.ewm(span=5).mean()
    long_moving_avg =  data.ewm(span=60).mean()
    
    
    # Simulate trading
    # Start with no money and no positions
    
    
    
    
    
    
    capital = init_capital
    qty1 = 0
    
    pos = 0
    margin_blocked = 0
    p1=0
    
    trade_pnl = []
    mtm_pl = []
        
    for i in range(len(ratio)):
        
        # if capital is eroded beyond a limit then exit
        if capital <= 0:
            break
        
        # if there is already exiting open position then no new position is to 
        # be taken till exii

        if pos == 0:
            # if there is no exiting open position then check if new position is to be taken 
            
            if short_moving_avg[i] > long_moving_avg:
               
                # Take a short position 
                pos = -1
                        
                # keep track of entry prices
                p1 = price1[i]
               
                # check how much capital is to be deployed
                margin_blocked = capital * max_capital_deploy
                
              
                
                # use FLOOR DIVISION to get integral qty
                qty1 = -(margin_blocked / 2) // (p1 * sell_margin)
                
               
                
                # if enough margin is not available no position will be taken
                
                if -qty1 < 1 :
                    break
                
            elif short_moving_avg[i] > long_moving_avg:
               
                # Take a long position
                pos = 1
                
                # keep track of entry prices
                p1 = price1[i]
               
                
                # check how much capital is to be deployed
                margin_blocked = capital * max_capital_deploy
                
            
               
                qty1 = (margin_blocked / 2) // (p1 * buy_margin)
                
              
                
              
                
                if qty1 < 1  :
                    break
            
            #else:
                # do nothing
            
        elif pos < 0:  
            # if there is exiting open short position then check for exit condition
            
            if short_moving_avg[i] >ong_moving_avg:
               
                # exit the short position
                
                # calculate the PL from the entry prices
                
                pnl = qty1 * (price1[i] - p1)
                if not fixedcapital:
                    capital += pnl
                
                trade_pnl = np.append(trade_pnl, pnl)
                
                # release the margin and all others
                margin_blocked = 0
                qty1 = 0
                
                pos = 0
            else:
                # calculate the MTM PL from the entry prices
                
                pnl = qty1 * (price1[i] - p1)
                mtm_pl = np.append(mtm_pl, pnl)
                
                # check for stop loss or target
                
               
                    
                trade_pnl = np.append(trade_pnl, pnl)
                    
                    # release the margin and all others
                margin_blocked = 0
                qty1 = 0
                   
                pos = 0
        else:
            # if there is exiting open long position then check for exit condition
            
            if short_moving_avg[i] <long_moving_avg:
              
                # exit the long position 
                # calculate the PL from the entry prices
                
                pnl = qty1 * (price1[i] - p1)
                if not fixedcapital:
                    capital += pnl

                trade_pnl = np.append(trade_pnl, pnl)
                
                # release the margin and all others
                margin_blocked = 0
                qty1 = 0
               
                pos = 0

            else:
                # calculate the MTM PL from the entry prices
                
                pnl = qty1 * (price1[i] - p1)
                mtm_pl = np.append(mtm_pl, pnl)
                
                # check for stop loss or target
                
                
                    
                trade_pnl = np.append(trade_pnl, pnl)
                    
                    # release the margin and all others
                margin_blocked = 0
                qty1 = 0
                    
                pos = 0
                
           
    
    return capital, trade_pnl, mtm_pl
i wan to test the basic strategy that is when short ema> long ema short the stock and exit the long position if any
and when short ema < long ema buy the stock and close the already open sell positoion if any
have written code for the same now i want to generate the trading profit and loss columns to pass it to the backtest function

I want to know how can I get the trd_pnl mtm_pnl
below is the error m getig
 cap, trdpnls, mtmpnls = backtest( price1,init_capital, max_capital_deploy, buy_margin, sell_margin)
    
Error:
NameError Traceback (most recent call last) <ipython-input-42-2e95ef66a812> in <module>() ----> 1 cap, trdpnls, mtmpnls = backtest( price1,init_capital, max_capital_deploy, buy_margin, sell_margin) 2 NameError: name 'price1' is not defined
Reply


Messages In This Thread
RE: error on stock indicator code on balance volume - by yatish - Sep-10-2018, 06:22 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to accumulate volume of time series amdi40 3 2,413 Feb-15-2022, 02:23 PM
Last Post: amdi40
  Integrating for the volume of a torus in SciPy Nitram 2 3,786 Jan-08-2020, 04:45 PM
Last Post: Nitram

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020