Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Determining asset value, or selling a certain percentage of portfolio #236

Unanswered
Cyrusb01 asked this question in Q&A
Discussion options

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)
You must be logged in to vote

Replies: 1 comment 4 replies

Comment options

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?

You must be logged in to vote
4 replies
Comment options

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?

Comment options

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
Comment options

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.

Comment options

Don't know about great, but I like to keep the framework minimal, and this seems all that's needed. Thanks. 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /