You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
class SmaCross(Strategy):
# Define the two MA lags as *class variables*
# for later optimization
n1 = 10
n2 = 20
def init(self):
# Precompute the two moving averages
self.sma1 = self.I(SMA, self.data.Close, self.n1)
self.sma2 = self.I(SMA, self.data.Close, self.n2)
def next(self):
# If sma1 crosses above sma2, close any existing
# short trades, and buy the asset
if crossover(self.sma1, self.sma2):
self.position.close()
self.buy(size = 100)
# Else, if sma1 crosses below sma2, close any existing
# long trades, and sell the asset
elif crossover(self.sma2, self.sma1):
self.position.close()
self.sell(size = 100)
However, by setting size = 100, I suppose it will trade for 100 units (shares of GOOGL) each time. Instead, I want to trade for an amount of size that is equivalent to 100 in cash for every trade.
For example, if the stock price is 10$ per share, the trading size will be 10. If the price goes to $20, trade 5 shares then.
How shall I write it properly?
Thanks a lot!
The text was updated successfully, but these errors were encountered:
Order size (negative for short orders).
If size is a value between 0 and 1, it is interpreted as a fraction of current
available liquidity (cash plus Position.pl minus used margin).
A value greater than or equal to 1 indicates an absolute number of units.
self.buy(size=0.95) # Means use 95% available cash to purchase
If size is a value between 0 and 1, it is interpreted as a fraction of current
available liquidity (cash plus `Position.pl` minus used margin).
A value greater than or equal to 1 indicates an absolute number of units.
"""
kernc
changed the title
How to place an order specifying the amount in cash instead of the size units of asset
Order size for fixed amount of cash
Sep 9, 2020
Hi, I'm a beginner in this project.
I want to specify the trading size of the tutorial example:
However, by setting
size = 100
, I suppose it will trade for 100 units (shares of GOOGL) each time. Instead, I want to trade for an amount of size that is equivalent to 100 in cash for every trade.For example, if the stock price is 10$ per share, the trading size will be 10. If the price goes to $20, trade 5 shares then.
How shall I write it properly?
Thanks a lot!
The text was updated successfully, but these errors were encountered: