Hi my name is Jared Broad, and today we'regoing to be going over the Buy and Hold with Forex data in QuantConnect.
ThisBoot Camp is going to teach you how to add and request Forex data, how to workwith the lot size rounding, and how the place trades with Forex assets.
So let'sjump into it.
Firstly we're going to learn how the request Forex data.
So justlike equities data you use AddForex to request Forex symbols, so with this taskit's asking us to ask for hourly euro US dollar data for a specific date range, and set the market to be Oanda.
With Forex data you can have multiplebrokerages all with slightly different data sets.
In QuantConnectwe model this as different markets, so the same euro US dollarticker can have slightly different prices depending on the exchanges or themarkets that you're trading on.
So to make sure that our backtests areaccurate you want to request the right market to be trading in.
So let's add forex data.
We can just use AddForex here, and then euro US dollar.
It's askingfor hourly resolution.
And now we need to set the date range tobe the month of May.
Okay awesome, let's send it off to thecloud and see how we did.
QuantConnect has all of the data from FXCMand Oanda which is about 85 tickers all the way back to 2005, 2004 depending on whichbrokerage you're trading on.
Oh look at that, so we've added Forex, butwe forgot to ask for the right market, so we can fix that by adding market here.
In this case we've added market Oanda, but we also have market FXCM.
For the test let's just ask for market Oanda.
Okay awesome, we passed thetask.
Let's move on to the next one.
So now we're going to set the brokeragemodel for this algorithm.
Brokerage models allow you to set a lot of commonsettings that are for one brokerage.
So for example, when you set the Oandaor the FXCM brokerage model it automatically sets the fee model forthat brokerage and the slippage and the leverage that you're allowed to use.
Sothere're a lot of brokerage specific settings that rather than forcing you togo and set them one by one you can just ask for the right brokerage model and itwill automatically set them for you.
So here it's saying make the algorithmrequest the Oanda brokerage model.
This is a setting on the algorithm, you do self.
And then you pass in the brokerage name enum.
You can see a full list of the brokeragesavailable here and the different models that we've implemented for you, but that should be enough to set this.
We passed thetask and on to the next one.
Okay now we're going toaccess Forex price data.
Unlike equities Forex data is mostlybids and asks.
In rare circumstances you can get Forex trade data, but the datathat we have is Forex bid and ask data.
So each of these is available inthe bid and ask property of that slice This task is asking us to save theask price of hourly data at a specific time.
So how are we going to get the time?How we going to get the price? We can use the self.
Time property of thealgorithm to get the algorithms time.
The algorithm self.
Time is the backtest time, and in this case is going to be flowing from May 1st to May 31st, and so if youmake a statement here you would say if self.
Hour You're going to be accessingthe Python date/time object that's stored inside this time property.
Ifthe hour's 10, and we want the first day of the month only, then we want to savethe euro US dollar ask price.
So this is accessible inside the data objecthere, and then you need to pass in the ticker that we're trading, and we wantthe asking price Okay, so we have this ask object here, but what's inside of it? Just like trade data, quote bar data hasa full range of open high low close values of the ask price or the bid price.
So this is actually a bar with its own properties so we can go Ask.
Close toget the close of that asking bar.
So, next one which doesn't actually havean objective but it seems like it's extra points, let's print out the valueof that price.
Okay now just finish that last error there, and let's submit our answer for processing.
You can see here we're debugging the price so we can actually have a look atthe price.
We can see that in the console There you go, you've got the $1.
09 camethrough, and we passed the task.
Let's move on to the next one.
Depending on your brokerage somebrokerages restrict the orders to be round lots.
This means that you ordersneed to be rounded to a specific number.
For most Forex data this is rounded tothe nearest $1, 000 as Forex trade can be rather large sometimes.
So to make theirlife simple of the brokerages say that you need to round it to the nearestthousand and you can do this with the lot size.
So here we can see that we canaccess the lot size for your brokerage using this value.
Get a little hintfrom the documentation over here.
You can see here the securities collectionwhich is the collection of the data that we've added to the algorithm, and we canuse your US dollar.
So now we have saved the lot size for that asset to this variable.
Next, it's asking us to use a debugstatement to print out this lot size information, so we just need to pass thatin.
If you notice here self.
lotSize is a number and so we have to use thestring method to convert that to a string for the debug statement.
Finallyit's asking us to round the order quantity and print the order size value.
So it's given us a little hint here.
We can use the round function here to takethis order quantity, divide it by a lot size and multiply it by the law size tothem rounded to the nearest lot size vaue.
Let's print out that value.
Again here we're using the string helper to print out the rounded order sizefor the debug statement.
Okay let's see how we went.
Okay you can see here in thedebug statement that the lot size for this brokerage is 1, and sothe rounded order size has been round to the nearest one as we expected.
Okay, awesome we passedthe task let's see what's next.
It looks like we're going to beplacing some Forex orders.
This will probably pull together everything we'velearned so far, rounding the orders to the nearest lotsize and asking for data.
We can see here that we can use the market orderand limit order methods just like equities, and also just like equities wecan use the SetHoldings helper to get a fraction of our total portfolio value.
Soit's asking us to request Oanda hourly resolution data andthen ask for the Oanda brokerage model.
Let's see how we go.
Now if you remember you need touse the SetBrokerageModel helper.
And then we need to use the brokeragename enum to set the brokerage model.
Next, in the OnData use a MarketOrderto order two thousand units of AUDUSD.
We need to use Portfolio.
Investedto make sure we only submit one order.
We can say if we're not invested then we place our market order, and we wantto order two thousand units.
Okay let's see how we went.
Alright, awesome we just passedthe Buy and Hold Forex tutorial, and in that time you'velearned how to request Forex data, set a brokerage model, round all the lotsizes, and then finally place a trade for forex to your brokerage.
Thank you verymuch for watching, and I'll see you in the next tutorial.