'How can set value in pandas
Please I have a data frame like this:
high low close h-l l-c h-c tr
time
2014-01-02 1.37749 1.36294 1.36643 0.01455 0.01286 0.00169 0.01455
2014-01-03 1.36716 1.35821 1.35874 0.00895 0.00822 0.00073 0.00895
2014-01-06 1.36528 1.35712 1.36334 0.00816 0.00162 0.00654 0.00816
2014-01-07 1.36562 1.35966 1.36138 0.00596 0.00368 0.00228 0.00596
2014-01-08 1.36349 1.35533 1.35749 0.00816 0.00605 0.00211 0.00816
2014-01-09 1.36328 1.35484 1.36017 0.00844 0.00265 0.00579 0.00844
2014-01-10 1.36871 1.35721 1.36665 0.01150 0.00296 0.00854 0.01150
2014-01-13 1.36846 1.36373 1.36680 0.00473 0.00292 0.00181 0.00473
2014-01-14 1.36992 1.36490 1.36722 0.00502 0.00190 0.00312 0.00502
2014-01-15 1.36760 1.35812 1.36042 0.00948 0.00910 0.00038 0.00948
2014-01-16 1.36492 1.35830 1.36162 0.00662 0.00212 0.00450 0.00662
2014-01-17 1.36208 1.35168 1.35405 0.01040 0.00994 0.00046 0.01040
2014-01-20 1.35677 1.35074 1.35557 0.00603 0.00331 0.00272 0.00603
2014-01-21 1.35687 1.35162 1.35591 0.00525 0.00395 0.00130 0.00525
2014-01-22 1.35832 1.35344 1.35447 0.00488 0.00247 0.00241 0.00488
2014-01-23 1.36981 1.35303 1.36931 0.01678 0.00144 0.01534 0.01678
2014-01-24 1.37393 1.36627 1.36752 0.00766 0.00304 0.00462 0.00766
2014-01-27 1.37163 1.36529 1.36728 0.00634 0.00223 0.00411 0.00634
2014-01-28 1.36882 1.36290 1.36517 0.00592 0.00438 0.00154 0.00592
and I want the value of the Atr to start from index 14 cause I want the sma of the first 14 days then the second value before setting the Subsequent Values
df_tr["Atr"] = 0
period = 14
df_tr['Atr'].iloc[period] = df_tr['tr'].rolling(period).mean().iloc[period]
df_tr["Atr"].iloc[period + 1] = df_tr["Atr"].iloc[period] - (df_tr["Atr"].iloc[period]/period)
+ df_tr["tr"].iloc[period + 1]
df_tr["Atr"].iloc[perioid + 2] = = df_tr["Atr"].iloc[period+2:].shift(1) - (
df_tr["Atr"].iloc[period+2:].shift(1)/period) + df_tr["tr"].iloc[period+2:]
but this is what I really want:
First Atr = sma 14 periods of tr
Second Atr = First Atr - (First Atr/14) + Current tr
Subsequent Values = Prior Atr - (Prior Atr/14) + Current tr
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
