This algo has a return of 12,000%. I saw my capital was leveraged up to 500x, but I
don´t know where I should have leveraged it in the code.
Can somebody solve this riddle.
THANKS!
This algo has a return of 12,000%. I saw my capital was leveraged up to 500x, but I
don´t know where I should have leveraged it in the code.
Can somebody solve this riddle.
THANKS!
Code should read this.
def handle_data(context, data):
price_history = history(bar_count=2, frequency='1d', field='high')
todays_high = float(price_history.iloc[-1])
current=data.current(context.asset,"price")
high=data.current(context.asset,"high")
low=data.current(context.asset,"low")
amount=10000000/current
if data.current(context.asset,"price")==high:
order_target(context.asset, int(amount))
Using the order function just caused you to build up multiple times your portfolio in Apple.
i.e.
for i in range(1, 5):
order(context.asset, 1)
Every time you loop over it you add 1 share to your portfolio. (1, 2, 3, 4, 5... etc).
a few more issues... one should generally not order a fixed quantity of shares without checking first if there is enough cash in the account to cover the order. This is a sure way to create high levels of leverage (ie buying more stock than one has cash to cover it). Maybe use 'order_target_percent' which takes care of this behind the scenes?
Also, always take into account any outstanding orders before ordering more. Don't simply look at how much cash there is in the account (or use the 'order_target_percent' method) one also needs to look at how much has been previously committed in outstanding orders. A simplistic way to accomplish this is to not place new orders if there are any outstanding orders. Something like this.
def handle_data(context, data):
# Only try to order more if there are no open orders
if not get_open_orders():
... place ordering code here...
Not sure exactly what the strategy of the algo is (especially since it never sells anything) but the attached algo demonstrates how to check for open orders.
Good luck.