Skip to content

Order size for fixed amount of cash #137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
zhixwang opened this issue Sep 9, 2020 · 2 comments
Closed

Order size for fixed amount of cash #137

zhixwang opened this issue Sep 9, 2020 · 2 comments
Labels
question Not a bug, but a FAQ entry

Comments

@zhixwang
Copy link

zhixwang commented Sep 9, 2020

Hi, I'm a beginner in this project.

I want to specify the trading size of the tutorial example:

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!

@arunavo4
Copy link

arunavo4 commented Sep 9, 2020

@zhixwang

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

Check the below link for ref

"""
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.
"""

@kernc 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
@kernc
Copy link
Owner

kernc commented Sep 9, 2020

To place an order for size equivalent of $100 value:

current_price = self.data.Close[-1]
order_size = 100 // current_price

hth

@kernc kernc closed this as completed Sep 9, 2020
@kernc kernc added the question Not a bug, but a FAQ entry label Sep 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Not a bug, but a FAQ entry
Projects
None yet
Development

No branches or pull requests

3 participants