cloudyu commited on
Commit
6a93426
·
verified ·
1 Parent(s): 4cea099

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +72 -0
README.md ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ language:
4
+ - en
5
+ - zh
6
+ base_model:
7
+ - Qwen/Qwen2.5-32B
8
+ ---
9
+
10
+ example:
11
+
12
+ write a python script to back test ma5 and ma10 for trending following based on yfinance, for gold etf.
13
+ <think>
14
+ Okay, the user wants a Python script to backtest moving averages (MA5 and MA10) for trend following using yfinance for a gold ETF. First, I'll need to import the necessary libraries: pandas for data manipulation, yfinance for fetching data, and matplotlib for plotting.
15
+
16
+ Next, I'll define the gold ETF symbol, which is 'GC=F' for gold futures. Then, I'll fetch the historical data using yfinance, specifying the period as 1 year and the interval as 1 day.
17
+
18
+ After fetching the data, I'll calculate the 5-day and 10-day moving averages. I'll use the 'Close' prices for these calculations. Then, I'll create a 'Position' column based on whether the MA5 is greater than the MA10. If MA5 is greater, the position is 1 (buy), else 0 (no position).
19
+
20
+ To calculate the returns, I'll create a 'Returns' column which is the difference between consecutive 'Close' prices divided by the previous 'Close'. Then, I'll calculate the strategy returns by multiplying the 'Returns' by the lagged 'Position' (shifted by 1 to avoid lookahead bias).
21
+
22
+ I'll compute the cumulative returns for both the strategy and the buy-and-hold approach. Finally, I'll plot the results to visualize the performance.
23
+
24
+ The script should include comments for clarity and print statements to show the cumulative returns for both the strategy and the buy-and-hold approach. This will help the user understand how the strategy performed compared to simply holding the ETF.
25
+ The final answer is:
26
+
27
+ ```python
28
+ import pandas as pd
29
+ import yfinance as yf
30
+ import matplotlib.pyplot as plt
31
+
32
+ # Define the gold ETF symbol
33
+ symbol = 'GC=F'
34
+
35
+ # Fetch historical data
36
+ data = yf.download(symbol, period='1y', interval='1d')
37
+
38
+ # Calculate MA5 and MA10
39
+ data['MA5'] = data['Close'].rolling(window=5).mean()
40
+ data['MA10'] = data['Close'].rolling(window=10).mean()
41
+
42
+ # Create position signal: 1 when MA5 > MA10, else 0
43
+ data['Position'] = 0
44
+ data.loc[data['MA5'] > data['MA10'], 'Position'] = 1
45
+
46
+ # Calculate returns
47
+ data['Returns'] = data['Close'].pct_change()
48
+
49
+ # Calculate strategy returns
50
+ data['Strategy_Returns'] = data['Position'].shift(1) * data['Returns']
51
+
52
+ # Calculate cumulative returns
53
+ data['Cumulative_Returns'] = (1 + data['Strategy_Returns']).cumprod()
54
+ data['Buy_and_Hold_Returns'] = (1 + data['Returns']).cumprod()
55
+
56
+ # Plot the cumulative returns
57
+ plt.figure(figsize=(10, 6))
58
+ plt.plot(data['Cumulative_Returns'], label='MA5/MA10 Strategy')
59
+ plt.plot(data['Buy_and_Hold_Returns'], label='Buy and Hold')
60
+ plt.title('MA5 and MA10 Strategy vs Buy and Hold')
61
+ plt.xlabel('Date')
62
+ plt.ylabel('Cumulative Returns')
63
+ plt.legend()
64
+ plt.show()
65
+
66
+ # Print the cumulative returns
67
+ print(f"Cumulative Return of Strategy: {(data['Cumulative_Returns'][-1] - 1) * 100:.2f}%")
68
+ print(f"Cumulative Return of Buy and Hold: {(data['Buy_and_Hold_Returns'][-1] - 1) * 100:.2f}%")
69
+ ```
70
+ ```
71
+
72
+ Please determine whether the given text is related to computer science, if yes please return "YES", else return "NO".