Back

Help with Very Basic First Code (Python)

Hi - totally new here. I'm familiar with the basics of python but a bit out of my depth here. I am trying to create a sample first trading bot to learn. I want to:

  • Starting at 9:45am EST
  • Get the price of an Equity, store in variable 
  • Add $5.00 to the that price
  • Make a Market Purchase Order if that price is reached (checking every minute)
I've initialized the equity as such:  self.amzn = self.AddEquity("AMZN", Resolution.Minute)

Is that all I need to initialize? (other than start-end date and setCash)

Does everything else happen in the OnData method? Or Do I need to create a separate MarketOrder method? I can do the logical operations, I'm just confused what the final code might look like in terms of what goes where. I've tried looking at the Demonstration Algorithms but couldn't quite follow. 

Any help is appreciated, thank you. 

Update Backtest







0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


I haven't run this code but this might help you:

class Borg(QCAlgorithm):

def Initialize(self):
self.SetStartDate(2019, 1, 1) # Set Start Date
self.SetEndDate(2020, 4, 3) # Set End Date
self.SetCash(100000) # Set Strategy Cash

self.amzn = self.AddEquity("AMZN" Resolution.Minute)
self.orderPrice = -1


def OnData(self, slice):

if self.Time.hour == 9 and self.Time.minute == 45: # at 9:45
self.orderPrice = self.amzn.Close + 5.00 # or Last, or Open, or w/e you want

if self.Time.hour == 9 and self.Time.minute > 45:
if not self.Portfolio.Invested and self.amzn.Close > self.orderPrice:
#quantity = self.CalculateOrderQuantity("AMZN", 1.0)
#self.MarketOrder("AMZN", quantity)
#OR
self.SetHoldings("AMZN", 1.0)

A better way to do what you want to do is just use a limit order set to the price you want to buy at. It'll automatically execute when the price reaches that value so don't have to manually check the price every minute.

Also try some of the Boot Camp tutorials and check out the Documentation, it's worth it in the long run.

1

Thank you so much! I've been going through the bootcamps but this really helps me get an feel for the structure of code and execution to complete a task. (I'm not using limit orders because I want to do more logic with the price value.) Appreciate the help. 

0

It seems this code is executing only once across my start-end dates of a month (there is one trade made in the backtest). How do I have it run every day?

0

It's only running once because it only makes an investement if nothing has yet been invested yet. It also invests the entire portfolio.

If you wanted to make more than one trade, you need to modify the "not self.Portfolio.Invested" and change it to your liking. You may also want to invest a fraction of your porfolio, so do something like "self.SetHoldings("AMZN", 0.05)" to invest 5% of your portfolio every time you want to make a trade.

1

Right, I missed that condition. I'll try running it without it.

Having read the "Understanding Time" document, trying to verify my understanding:

The "slice" of time in the onData event refers to the resolution I set for the equity or currency pair? I can trust the algorithm within OnData will automatically run again for the next period (9:45, 9:46, 9:47... etc.)?

Thanks again!

0

Hi JV! I highly recommend completing Boot Camp. Most of your questions are covered in Boot Camp and you'll be given a step by step tutorial to learn the API and how to the data moves through QuantConnect.

1

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


After going through the Forex Boot Camp a few more times, I think I'm starting to get it. Thanks! Learning the API and finance conventions at the same time is quite the task!

1

Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed