You must sign in to read all of Forexqna.
Login to Forexqna
By continuing, I agree that I am at least 18 years old and have read and agree to Forex's Terms of Service and Privacy Policy.
Password reset
You deleted your answer
You deleted your answer to this question.
You may restore your answer at any time.
1 Answer
Tamara Nikolaeva
Answered 6 months, 3 weeks ago
<p id="isPasted">We are going to use this “macd_norm” column to determine how much stock to buy. The thinking is that when the short moving average crosses the long moving average it indicates the stock is increasing in value and we should buy. When the macd_norm is negative, we can short or sell the stock.</p><p>Backtesting code</p><p>Let’s start with $1000.</p><p>To backtest our code, we will create a new column in our dataframe called “position” which is the exact same as our indicator, “macd_norm”. This indicates how many shares we will buy or sell. To visualize our stock trades better, I’ll increase the weight of our strategy by 100x which means multiplying our position by 100. Using the our position and the open price of the stock, we can determine if we can afford the stock trade we will do.</p><p>Check each line’s comments for how we set this up.</p><p><em>initial_capital = 1000 # our $1000<br style="box-sizing: inherit; color: rgb(36, 36, 36); font-family: source-code-pro, Menlo, Monaco, "Courier New", Courier, monospace; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: -0.308px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre; background-color: rgb(249, 249, 249); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;" id="isPasted">df['position'] = 100 * df['macd_norm'] # renaming macd_norm to position for readability and multiplying by 100 to increase the weight of the strategy<br style="box-sizing: inherit; color: rgb(36, 36, 36); font-family: source-code-pro, Menlo, Monaco, "Courier New", Courier, monospace; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: -0.308px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre; background-color: rgb(249, 249, 249); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">df['position'].at[0] = 0 # we set it to zero since we start with no stock position<br style="box-sizing: inherit; color: rgb(36, 36, 36); font-family: source-code-pro, Menlo, Monaco, "Courier New", Courier, monospace; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: -0.308px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre; background-color: rgb(249, 249, 249); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">df['position_diff'] = df['position'].diff() # gets the difference in positions so we know how to adjust our stock trades<br style="box-sizing: inherit; color: rgb(36, 36, 36); font-family: source-code-pro, Menlo, Monaco, "Courier New", Courier, monospace; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: -0.308px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre; background-color: rgb(249, 249, 249); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">df['position_diff'].at[0] = 0 # set to zero since it's NaN since there is no difference on the first day<br style="box-sizing: inherit; color: rgb(36, 36, 36); font-family: source-code-pro, Menlo, Monaco, "Courier New", Courier, monospace; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: -0.308px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre; background-color: rgb(249, 249, 249); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">df['holdings'] = 0 # represents how much our stock position is worth, starts at 0 since we don't have any stocks yet<br style="box-sizing: inherit; color: rgb(36, 36, 36); font-family: source-code-pro, Menlo, Monaco, "Courier New", Courier, monospace; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: -0.308px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre; background-color: rgb(249, 249, 249); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">df['cash'] = initial_capital # tracks how much cash we have<br style="box-sizing: inherit; color: rgb(36, 36, 36); font-family: source-code-pro, Menlo, Monaco, "Courier New", Courier, monospace; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: -0.308px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre; background-color: rgb(249, 249, 249); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">df.head()</em></p><p><img data-fr-image-pasted="true" src="https://miro.medium.com/v2/resize:fit:875/1*PidhSpH6QQR9eqAqLCgnIg.png" style="width: 300px;" class="fr-fic fr-dib fr-draggable"></p><p><br></p><p><br></p><p><em> </em>Next we are going to loop through our positions.</p><ul><li>First we check if we have enough cash for our trade. If so, we don’t need to adjust our position, only deduct how much cash was needed from our amount.</li><li>If we don’t have enough cash, check if we are shorting the stock. If so, we update our cash and check if it brings us out of bankruptcy.</li><li>If the above two conditions arn’t satisfied, it means we don’t have enough money and we are not shorting the stock. There are a number of things we can do if we don’t have enough money. Some people quit trading, and some take a second loan on their house and double down. In our case, if it’s the initial trade, we calculate the max amount of stock we could buy instead. If it’s not the initial trade, then we can just keep the previous day’s position.</li></ul><p>Here is what I described above in code:</p><p><em>outOfCash = False<br style="box-sizing: inherit; color: rgb(36, 36, 36); font-family: source-code-pro, Menlo, Monaco, "Courier New", Courier, monospace; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: -0.308px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre; background-color: rgb(249, 249, 249); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;" id="isPasted">for index, row in df.iterrows():<br style="box-sizing: inherit; color: rgb(36, 36, 36); font-family: source-code-pro, Menlo, Monaco, "Courier New", Courier, monospace; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: -0.308px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre; background-color: rgb(249, 249, 249); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"> if index==0:<br style="box-sizing: inherit; color: rgb(36, 36, 36); font-family: source-code-pro, Menlo, Monaco, "Courier New", Courier, monospace; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: -0.308px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre; background-color: rgb(249, 249, 249); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"> continue<br style="box-sizing: inherit; color: rgb(36, 36, 36); font-family: source-code-pro, Menlo, Monaco, "Courier New", Courier, monospace; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: -0.308px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre; background-color: rgb(249, 249, 249); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"> cash = df.loc[index -1]['cash']<br style="box-sizing: inherit; color: rgb(36, 36, 36); font-family: source-code-pro, Menlo, Monaco, "Courier New", Courier, monospace; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: -0.308px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre; background-color: rgb(249, 249, 249); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"> position = df.loc[index]['position']<br style="box-sizing: inherit; color: rgb(36, 36, 36); font-family: source-code-pro, Menlo, Monaco, "Courier New", Courier, monospace; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: -0.308px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre; background-color: rgb(249, 249, 249); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"> price = row['Open']<br style="box-sizing: inherit; color: rgb(36, 36, 36); font-family: source-code-pro, Menlo, Monaco, "Courier New", Courier, monospace; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: -0.308px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre; background-color: rgb(249, 249, 249); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"> pos_diff = df.loc[index]['position'] - df.loc[index -1]['position'] <br style="box-sizing: inherit; color: rgb(36, 36, 36); font-family: source-code-pro, Menlo, Monaco, "Courier New", Courier, monospace; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: -0.308px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre; background-color: rgb(249, 249, 249); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"> cash_needed = pos_diff * price<br style="box-sizing: inherit; color: rgb(36, 36, 36); font-family: source-code-pro, Menlo, Monaco, "Courier New", Courier, monospace; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: -0.308px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre; background-color: rgb(249, 249, 249); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"> df.at[index, 'cash_needed'] = cash_needed<br style="box-sizing: inherit; color: rgb(36, 36, 36); font-family: source-code-pro, Menlo, Monaco, "Courier New", Courier, monospace; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: -0.308px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre; background-color: rgb(249, 249, 249); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><br style="box-sizing: inherit; color: rgb(36, 36, 36); font-family: source-code-pro, Menlo, Monaco, "Courier New", Courier, monospace; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: -0.308px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre; background-color: rgb(249, 249, 249); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><br style="box-sizing: inherit; color: rgb(36, 36, 36); font-family: source-code-pro, Menlo, Monaco, "Courier New", Courier, monospace; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: -0.308px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre; background-color: rgb(249, 249, 249); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"> if (cash > cash_needed) and (not outOfCash):<br style="box-sizing: inherit; color: rgb(36, 36, 36); font-family: source-code-pro, Menlo, Monaco, "Courier New", Courier, monospace; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: -0.308px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre; background-color: rgb(249, 249, 249); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"> cash = cash - cash_needed<br style="box-sizing: inherit; color: rgb(36, 36, 36); font-family: source-code-pro, Menlo, Monaco, "Courier New", Courier, monospace; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: -0.308px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre; background-color: rgb(249, 249, 249); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"> elif (cash_needed < 0):<br style="box-sizing: inherit; color: rgb(36, 36, 36); font-family: source-code-pro, Menlo, Monaco, "Courier New", Courier, monospace; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: -0.308px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre; background-color: rgb(249, 249, 249); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"> cash = cash-cash_needed<br style="box-sizing: inherit; color: rgb(36, 36, 36); font-family: source-code-pro, Menlo, Monaco, "Courier New", Courier, monospace; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: -0.308px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre; background-color: rgb(249, 249, 249); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"> if (cash > (position * price)):<br style="box-sizing: inherit; color: rgb(36, 36, 36); font-family: source-code-pro, Menlo, Monaco, "Courier New", Courier, monospace; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: -0.308px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre; background-color: rgb(249, 249, 249); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"> outOfCash = False<br style="box-sizing: inherit; color: rgb(36, 36, 36); font-family: source-code-pro, Menlo, Monaco, "Courier New", Courier, monospace; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: -0.308px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre; background-color: rgb(249, 249, 249); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"> else:<br style="box-sizing: inherit; color: rgb(36, 36, 36); font-family: source-code-pro, Menlo, Monaco, "Courier New", Courier, monospace; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: -0.308px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre; background-color: rgb(249, 249, 249); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"> # ran out of money<br style="box-sizing: inherit; color: rgb(36, 36, 36); font-family: source-code-pro, Menlo, Monaco, "Courier New", Courier, monospace; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: -0.308px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre; background-color: rgb(249, 249, 249); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"> if (index == 1):<br style="box-sizing: inherit; color: rgb(36, 36, 36); font-family: source-code-pro, Menlo, Monaco, "Courier New", Courier, monospace; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: -0.308px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre; background-color: rgb(249, 249, 249); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"> df[index, position] = cash / price # when we don't have enough money for the initial trade, then we buy the most stock that we can afford<br style="box-sizing: inherit; color: rgb(36, 36, 36); font-family: source-code-pro, Menlo, Monaco, "Courier New", Courier, monospace; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: -0.308px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre; background-color: rgb(249, 249, 249); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"> cash = 0<br style="box-sizing: inherit; color: rgb(36, 36, 36); font-family: source-code-pro, Menlo, Monaco, "Courier New", Courier, monospace; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: -0.308px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre; background-color: rgb(249, 249, 249); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"> else:<br style="box-sizing: inherit; color: rgb(36, 36, 36); font-family: source-code-pro, Menlo, Monaco, "Courier New", Courier, monospace; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: -0.308px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre; background-color: rgb(249, 249, 249); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"> df.at[index,'position'] = df.at[index -1, 'position'] # don't do anything since we are don't have enough money to trade on our startegy<br style="box-sizing: inherit; color: rgb(36, 36, 36); font-family: source-code-pro, Menlo, Monaco, "Courier New", Courier, monospace; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: -0.308px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre; background-color: rgb(249, 249, 249); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"> <br style="box-sizing: inherit; color: rgb(36, 36, 36); font-family: source-code-pro, Menlo, Monaco, "Courier New", Courier, monospace; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: -0.308px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre; background-color: rgb(249, 249, 249); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"> outOfCash = True<br style="box-sizing: inherit; color: rgb(36, 36, 36); font-family: source-code-pro, Menlo, Monaco, "Courier New", Courier, monospace; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: -0.308px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre; background-color: rgb(249, 249, 249); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"> <br style="box-sizing: inherit; color: rgb(36, 36, 36); font-family: source-code-pro, Menlo, Monaco, "Courier New", Courier, monospace; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: -0.308px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre; background-color: rgb(249, 249, 249); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"> df.at[index,'cash'] = cash<br style="box-sizing: inherit; color: rgb(36, 36, 36); font-family: source-code-pro, Menlo, Monaco, "Courier New", Courier, monospace; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: -0.308px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre; background-color: rgb(249, 249, 249); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><br style="box-sizing: inherit; color: rgb(36, 36, 36); font-family: source-code-pro, Menlo, Monaco, "Courier New", Courier, monospace; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: -0.308px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre; background-color: rgb(249, 249, 249); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">df['holdings'] = df['position'] * df['Open']<br style="box-sizing: inherit; color: rgb(36, 36, 36); font-family: source-code-pro, Menlo, Monaco, "Courier New", Courier, monospace; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: -0.308px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre; background-color: rgb(249, 249, 249); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">df['total'] = df['holdings'] + df['cash']<br style="box-sizing: inherit; color: rgb(36, 36, 36); font-family: source-code-pro, Menlo, Monaco, "Courier New", Courier, monospace; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: -0.308px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre; background-color: rgb(249, 249, 249); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">df.plot(x=f'Date', y='total')</em></p><p><br></p><p id="isPasted">Take your time to comb through the code above.Here is how we did:</p><div style="box-sizing: inherit; width: 680px; position: relative; cursor: zoom-in; z-index: auto; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1);" tabindex="0"><div style="box-sizing: inherit; margin-left: auto; margin-right: auto; max-width: 906px;" class="forexqa-img-container"><img alt="" width="700" height="422" src="https://prod-forexqna.s3.amazonaws.com/uploads/froala_editor/images/1738390766142.png" style="box-sizing: inherit; background-color: rgb(255, 255, 255); width: 730px; max-width: 100%; height: auto;" class="fr-fic fr-dii fr-draggable forexqa-img"></div></div><p>You can see that we end up with just under $1100. That’s about a 10% return on the year. That’s pretty good considering the stock price went down during that time.</p><p>Here is another visualization. Here you can see that when our position increases, we end up emptying our cash reserves (blue) to buy lots of stock (holdings are in orange). When the position (red) decreases, we end up selling some that stock.</p><div style="box-sizing: inherit; width: 680px; position: relative; cursor: zoom-in; z-index: auto; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1);" tabindex="0"><div style="box-sizing: inherit; margin-left: auto; margin-right: auto; max-width: 943px;" class="forexqa-img-container"><img alt="" width="700" height="405" src="https://prod-forexqna.s3.amazonaws.com/uploads/froala_editor/images/1738390797523.png" style="box-sizing: inherit; background-color: rgb(255, 255, 255); width: 730px; max-width: 100%; height: auto;" class="fr-fic fr-draggable fr-dib forexqa-img"></div><p><br></p><div style="box-sizing: inherit; margin-left: auto; margin-right: auto; max-width: 943px;"><br></div></div><p><em> </em></p><p><br></p>
<p id="isPasted">We are going to use this “macd_norm” column to determine how much stock to buy. The thinking is that when the short moving average crosses the long moving average it indicates the stock is increasing in value and we should buy. When the macd_norm is negative, we can short or sell the stock.</p><p>Backtesting code</p><p>Let’s start with $1000.</p><p>To backtest our code, we will create a new column in our dataframe called “position” which is the exact same as our indicator, “macd_norm”. This indicates how many shares we will buy or sell. To visualize our stock trades better, I’ll …</p>
2 Views
Loading...