Hi,
I was trying to implement on optuma the below code. The backtest function return a rolling cumulative PnL of strategy that follows some signals.
I would like to basically plot it along the selected security (possibly on the same scale – avoided in the below code for other reasons) but nothing is produced after having reprocessed the code.
Please find below my code. Disregard the backtest function per se (I’ve only added it to give you the complete picture).
What is wrong on the process input and output?
import Tool
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import math as m
CDovAB=[1.5,2.0,3.0]
CDovBC=[1.5,2.0,3.0]
BCovAB=[1.5,2.0,3.0]
targ_AD =0.20
def backtest(CDovAB,CDovBC,BCovAB,targ_AD,stck_pr):
pfolio =
stop_loss_long = 0
stop_loss_short = 106
target_long = 106
target_short = 0
open_position = None # ‘long’, ‘short’, or None
PnLcum = 0
PnL =
k = 0
dict_pfolio={‘position’:,‘price’:,‘StopLoss’:,‘Target’:,‘Pnl Posit’: }
i=4
while i < len(stck_pr)-5:
if k !=0:
i=k
AB=round((stck_pr[i-2]-stck_pr[i-3]),4)
BC=round((stck_pr[i-1]-stck_pr[i-2]),4)
CD=round((stck_pr[i]-stck_pr[i-1]),4)
AD=round((stck_pr[i]-stck_pr[i-3]),4)
if open_position is not None:
#dovrei fare due casi: SL e Target
if open_position == 'long' and stck_pr[i]<=stop_loss_long:
#pfolio.append(-stck_pr[i+3]) #something to sell
PnLcum+=stop_loss_long-pfolio[-1]
PnL.append(PnLcum)
dict_pfolio['position'].append('Close')
dict_pfolio['price'].append(stop_loss_long)
dict_pfolio['StopLoss'].append('-')
dict_pfolio['Target'].append('-')
dict_pfolio['Pnl Posit'].append(stop_loss_long-pfolio[-1])
open_position = None
elif open_position == 'long' and stck_pr[i]>=target_long:
#pfolio.append(-stck_pr[i+3]) #something to sell
PnLcum+=target_long-pfolio[-1]
PnL.append(PnLcum)
dict_pfolio['position'].append('Close')
dict_pfolio['price'].append(target_long)
dict_pfolio['StopLoss'].append('-')
dict_pfolio['Target'].append('-')
dict_pfolio['Pnl Posit'].append(target_long-pfolio[-1])
open_position = None
elif open_position == 'short' and stck_pr[i]>=stop_loss_short:
#pfolio.append(stck_pr[i+3])
PnLcum+= -pfolio[-1] -stop_loss_short #(-*-)-(+)
PnL.append(PnLcum)
dict_pfolio['position'].append('Close')
dict_pfolio['price'].append(stop_loss_short)
dict_pfolio['StopLoss'].append('-')
dict_pfolio['Target'].append('-')
dict_pfolio['Pnl Posit'].append(-pfolio[-1] -stop_loss_short )
open_position = None
elif open_position == 'short' and stck_pr[i]<=target_short:
#pfolio.append(stck_pr[i+3])
PnLcum+= -pfolio[-1] - target_short
PnL.append(PnLcum)
dict_pfolio['position'].append('Close')
dict_pfolio['price'].append(target_short)
dict_pfolio['StopLoss'].append('-')
dict_pfolio['Target'].append('-')
dict_pfolio['Pnl Posit'].append(-pfolio[-1] - target_short)
open_position = None
elif open_position == 'long':
PnL.append(PnLcum + stck_pr[i] - pfolio[-1])
elif open_position == 'short':
PnL.append(PnLcum - stck_pr[i] - pfolio[-1])
else:
if len(PnL) > 0:
PnLcum+=0
PnL.append(PnLcum) # Append the last available PnL
if AB !=0 and BC !=0 and open_position is None:
if any([abs(CD/AB) > ratio for ratio in CDovAB]) and any([abs(CD/BC) > ratio for ratio in CDovBC]) and any([abs(BC/AB) > ratio for ratio in BCovAB]):
min_5m = np.min([stck_pr[i],stck_pr[i-3]]) #min of last candle
max_5m = np.max([stck_pr[i],stck_pr[i-3]]) #max of last candle
if stck_pr[i] < stck_pr[i-3]:
for w,j in enumerate(stck_pr[i:(i+4)]): #occhio qui, se ultimo i +1 fuori loop
if j > max_5m:
pfolio.append(round(j,2))
stop_loss_long = round(min_5m,4)-0.01
target_long = round((-targ_AD*AD + j),4)
open_position = 'long'
dict_pfolio['position'].append(open_position)
dict_pfolio['price'].append(round(j,4))
dict_pfolio['StopLoss'].append(stop_loss_long)
dict_pfolio['Target'].append(target_long)
dict_pfolio['Pnl Posit'].append('-')
k=i+w
break
elif stck_pr[i] > stck_pr[i-3]:
for w,j in enumerate(stck_pr[i:(i+4)]):
#occhio qui, se ultimo i +1 fuori loop
if j < min_5m:
pfolio.append(-round(j,2))
stop_loss_short = round(max_5m,4)+0.01
target_short = round((-targ_AD*AD + j),4)
open_position = 'short'
dict_pfolio['position'].append(open_position)
dict_pfolio['price'].append(round(j,4))
dict_pfolio['StopLoss'].append(stop_loss_short)
dict_pfolio['Target'].append(target_short)
dict_pfolio['Pnl Posit'].append('-')
k=i+w
break
else:
pass
i+=1
k+=1
#return PnL
return(PnL)
def Process(start, end):
myArray = np.array()
for i in range(start, end):
myArray = np.append(myArray, Tool.Source.Row(i).Close)
array_to_plot = backtest(CDovAB,CDovBC,BCovAB,targ_AD,myArray)
for i in range(array_to_plot.shape[0]):
Tool.DataOut.Row(i).Close = array_to_plot[i]
Thanks in advance and regards