Code support needed for some modifications causing me error and warning messages.
Please find the attached code and csv data file.
Code support needed for some modifications causing me error and warning messages.
Please find the attached code and csv data file.
Three days and no reply / support from QuantInsti !!!
Error at inp[9] âŚ
[C:\Users\anilh\AppData\Local\Temp\ipykernel_9928\1694609077.py:8](file:///C:/Users/anilh/AppData/Local/Temp/ipykernel_9928/1694609077.py#line=7): FutureWarning: Series.getitem treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use ser.iloc[pos]
y_train, y_test = y[train_index], y[test_index]
Hello Anil,
The warning you are encountering is simply a message saying that in the future the functionality of the code might change. The line in question is: y_train, y_test = y[train_index], y[test_index]
You can try this instead:
y_train, y_test = y.iloc[train_index], y.iloc[test_index]
Hope this helps
Hi Rekhit
Thanks yeh it helped out.
Got another error as
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) ~\AppData\Local\Temp\ipykernel_3528\2376848891.py in ?() 2 #PnL_unopt = pnl(test_data,stop_loss,take_profit) 3 4 # Run the scalping strategy on the testing set using the optimised thresholds 5 #PnL_opt = pnl(test_data,stop_loss,take_profit) ----> 6 PnL_opt = pnl(rates) 7 8 #print('The average PnL on the last one-third minutes using preset thresholds: â +str(PnL_unopt)) 9 print('The average PnL on the last one-third minutes using optimal thresholds: â +str(PnL_opt)) ~\AppData\Local\Temp\ipykernel_3528\1522249504.py in ?(rates) 33 # Long Exit 34 elif (current_position == 1): 35 # Check for sl and tp 36 # if rates.loc[time, âcloseâ] < stop_loss or rates.loc[time, âcloseâ] > take_profit: â> 37 if rates.loc[time, âlowâ] < stop_loss or rates.loc[time, âhighâ] > take_profit: # touch/tag to low/high should exit the position 38 long_exit(data, time, entry_price) 39 current_position = 0 40 ~\anaconda3\Lib\site-packages\pandas\core\generic.py in ?(self) 1575 @final 1576 def nonzero(self) â NoReturn: â 1577 raise ValueError( 1578 f"The truth value of a {type(self).name} is ambiguous. " 1579 âUse a.empty, a.bool(), a.item(), a.any() or a.all().â 1580 ) ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Please bear with me as this code is combination of ATR Scalping and K-Fold Cross Validation Technique, you briefed me earlier. So I do pay attentions to most of the details, but still bound to make some errors.
Revised code is attached as below.
Hi Anil,
Iâve reviewed the trading strategy code you shared and identified several issues that are causing the error. The main problem is with the pnl()
function where thereâs confusion between variable names (rates
vs data
) and how stop loss/take profit values are being calculated.
Specifically, in your code, youâre using data
in some places and rates
in others, which causes reference errors. When calculating stop loss and take profit levels, youâre trying to use the entire ATR column rather than extracting specific values for each time point.
The error message âThe truth value of a Series is ambiguousâ occurs because youâre trying to compare an entire pandas Series to a value instead of extracting specific values at each time point.
Can you try to rectify this on your own first? If youâre still stuck after spending a good amount of time, let me knowâIâll be happy to help
Hi Rishikesh
Thanks for highlighting the problem areas.
I will have relook on the code and revert.
Hi Rishikesh
Sorry for the mess up of rates VS data. I am used to work on MT5 and usually get variable not found errors in such cases. However Python, still trying to get used to it.
ValueError Traceback (most recent call last)
Cell In[12], line 22
20 c = confusion_matrix(y_test, model.predict(X_test))
21 # Add the score to the previous confusion matrix of previous model
â> 22 array = array + c
23 # Create a pandas dataframe that stores the output of confusion matrix
24 df = pd.DataFrame(array, index = [âBuyâ, âSellâ], columns = [âBuyâ, âSellâ])
ValueError: operands could not be broadcast together with shapes (2,2) (3,3)
ANOTHER ISSUES:
I have trained the data with Decision Tree Classifier, however when I am making call to pnl method, it does not seems to be considering the trained data signal Buy or Sell. Instead just using the raw data for pnl calculation. This is possibly due to the fact that original strategy used optimal parameters as threshold limits, where as I am taking a boolean results. Guide me how to incorporate the this into model testing.
To optimize atrMultiple for SL (1.5*ATR) and atrMultiple for TP(10.*ATR)?
Also in calculating entry_price, is the Close of previous bar is considered or the current bar?
How can I have PERIOD_M1 ATR breakout incorporated into this model, whose data is for PERIOD_M5 ?
pcMean calculations seems to have error, as it returned too high values compared to the Close prices.
Revised files are attached herewith.
ATRScalp_v1.02
Hi Anil,
Confusion Matrix Error occurs because your target variable y has three different values (-1, 0, 1) instead of just two (Buy, Sell). This mismatch in dimensions causes the error when trying to process your data.
Your current P/L function has an error - youâre referencing âspreadâ but this column doesnât exist in your dataset, which causes the KeyError. Youâll need to add this column before running your calculations.
Regarding your other questions: I donât see where youâve actually implemented the Decision Tree in your trading decisions. Youâve trained the model but it doesnât appear to be used when making buy/sell decisions in your PnL function.
For questions 2 and 3, I honestly didnât understand what youâre asking. Could you please describe these questions in more detail? Regarding entry prices, when you receive a signal on one bar, you would typically enter on the next barâs open .
Regarding question 4, the high pcMean values are likely occurring because your percentage calculations arenât properly scaled or limited, allowing extreme values to distort your results.
Hi Rishikesh
lets go step-by-step âŚ
Confusion Matrix Error occurs because your target variable y has three different values (-1, 0, 1) instead of just two (Buy, Sell). This mismatch in dimensions causes the error when trying to process your data.
https://blog.quantinsti.com/cross-validation-machine-learning-trading-models/
Source above âŚ
Output variable: If tomorrowâs close price is greater than todayâs close price then the output variable is set to 1 and otherwise set to -1. 1 indicates to buy the stock and -1 indicates to sell the stock.
y = np.where(aapl[âAdj Closeâ].shift(-1) > aapl[âAdj Closeâ], 1, -1)
where it is possible to have either +1 or -1 values.
Whereas in my ATRBreakOut strategy, I may not have condition either as Buy or Sell signal based on following rules:
rates[âsigLongâ] =np.where(
rates[âatrBOâ] & rates[âfour_candle_highâ], +1, 0)
rates[âsigShortâ] =np.where(
rates[âatrBOâ] & rates[âfour_candle_lowâ], -1, 0)
How can I overcome this to have +1/-1 values only in sigPosition column?
Hi Anil,
Right now, there are three states (+1 for long, -1 for short, and 0 for no signal) in your strategy. If you want to match the blog example with only +1/-1 values, youâll need to modify your signal generation. The cases where signal is 0, you would need to classify as either long or short based on some criteria in your strategy. Unfortunately, I canât comment on which cases you have to assign as long and short since it is your strategy you are building.
Hi Rishikesh
So what alternative I could use if the signal remains to be of 3 types (+1, 0, -1) for model?
Hi Anil
In your code (Confusion matrix cell), youâre initializing a fixed 2Ă2 array for the confusion matrix (array = [[0,0],[0,0]]), but your data actually contains three different signal values (+1, 0, -1). The error occurs when sklearnâs confusion_matrix function returns a 3Ă3 matrix that doesnât match your 2Ă2 initialization. You should modify this to create a 3Ă3 array instead. Also when creating the DataFrame, youâll need to update the labels in pd.DataFrame(array, index = [âBuyâ, âSellâ], columns = [âBuyâ, âSellâ]) to include all three of your signal states. Making these two changes will allow your confusion matrix to properly represent the results for your three-state trading model.
Thanks Rishikesh
I will try your suggestions.