-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
-
Hi, I am new to the backtesting and trading field, however, this package is great! The center of my problem revolves around keeping a certain percentage of my portfolio always invested. I want to make normal trades based on moving averages, however, I never want my portfolio to own less than 20% of the asset. I understand the buy function allows me to buy based on a certain percentage of liquidity, however, I want to sell based on my percentage of assets. I know I am able to get the total equity, so if there was a way to track my asset amount, that would work great. In my code I have a variable asset_value
which would solve my issue I believe, so the script is written like It is a valid variable. I would also like to know more about the sell function, what would happen if had a size < 1 like it would be in my code below? I would appreciate any advice for keeping a certain percentage invested, and how I should approach this. Thank you.
from backtesting import Backtest, Strategy
from backtesting.lib import crossover
from backtesting.test import SMA, GOOG
from numpy.core.fromnumeric import size
class SmaCross(Strategy):
min_size = .2
def init(self):
price = self.data.Close
self.ma1 = self.I(SMA, price, 10)
self.ma2 = self.I(SMA, price, 20)
self.buy(size = self.min_size) # Start with 20% portfolio invested
def next(self):
if crossover(self.ma1, self.ma2):
self.buy(size = .99) #Size corellates to the current liquididy (First trade would be 80% of full portfolio)
elif crossover(self.ma2, self.ma1) and (self.position.size > 0):
if (asset_value / self.equity > self.min_size): #checking to see how much of the portfolio is invested
self.sell(size = asset_value/self.equity) #selling off anything above the minimum requried amount
bt = Backtest(GOOG, SmaCross, cash=1000000, commission=.002,
exclusive_orders=True)
stats = bt.run()
print(stats)
new_trades = stats._trades
print(new_trades)
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment 4 replies
-
if there was a way to track my asset amount, that would work great
Did you get to review the API reference, namely backtesting.Position.size
?
Beta Was this translation helpful? Give feedback.
All reactions
-
Yes, I understand the Position.size
gives me the amount of shares, so would you recommend taking the asset amount and multiplying it by the current asset value? Is there any other way to keep my investments at least 20%. Could I ask how you would approach trying to keep 20% of your portfolio always invested, and other than that just using any trading strategy?
Beta Was this translation helpful? Give feedback.
All reactions
-
I guess so. Does this help?
price = self.data.Close[-1] # Solve: ideal_size * price == equity * .20 ideal_size = self.equity * .2 / price assert self.position.size > 0 sell_amount = max(0, self.position.size - ideal_size) assert sell_amount
Beta Was this translation helpful? Give feedback.
All reactions
-
Yes, thank you. So every time we go to sell we are calculating the amount of shares we need to keep in order to maintain the 20%. What a great solution. Thank you.
Beta Was this translation helpful? Give feedback.
All reactions
-
Don't know about great, but I like to keep the framework minimal, and this seems all that's needed. Thanks. 😊
Beta Was this translation helpful? Give feedback.