Day Trading Strategy for Beginners | ATR Scalping Strategy

Code support needed for some modifications causing me error and warning messages.

Please find the attached code and csv data file.

ATRScalping

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

1 Like

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.

ATR Scalping_v1.01

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

1 Like

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.

The code is working with your suggestions, except the Confusion Matrix is giving error as below:

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:

  1. 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.

  2. 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?

  3. How can I have PERIOD_M1 ATR breakout incorporated into this model, whose data is for PERIOD_M5 ?

  4. 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.

1 Like

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.

1 Like

Thanks Rishikesh
I will try your suggestions.