Backtesting oanda/forex
-
Hi guys,
I am trying to create a small forex strategy for later use in oanda. Does anybody have a sample script for me how to:
- Setup the commission with leverage.
- Sizing (taking into the account the used leverage).
- Buying selling currency pairs.
So that it emulates the oanda commissions/leverage properly. I am having hard time figuring out how to use backtrader in that case.
I have found an old thread regarding commissions, but only with sample for Eurostoxx and it does not use the leverage property:
https://community.backtrader.com/topic/333/accurate-commissions-for-ib-and-oanda/3
Any help appreciated!
-
Hello,
What type of Oanda account do you use? The commission is essentially in the spread for small retail accounts. This spread can change / widen depending on market conditions.Depending on the time frame you are using, it might be worthwhile to forget the commission as a couple of pips is negligible unless you are on a very low timeframe. In backtesting you will never get 100% accurate representation of market conditions in my experience. (Perhaps others may disagree ;) )
Having said that, I will be interested to follow this thread to see what others are doing regarding this.
Trading pairs is a matter of selecting the correct data feed to add to cerebro. It can be as simple as using the store to add it using the Oanda data code as a text string then resampling to your desired format. Buying and selling is then done in the same way as all other data feeds.
data = oandastore.getdata(dataname="GBP_USD") cerebro.resampledata(data, timeframe=bt.TimeFrame.Minutes, compression=60)
-
The commission is essentially in the spread for small retail accounts. This spread can change / widen depending on market conditions.
As stated by @ThatBlokeDave the commission is in the spread and cannot be simulated as with other brokers. You can in any case make your matches have a worse price by using slippage in the broker Docs - Slippage
-
Understood. What about sizing and leverage?
If I had a leverage of 1:50 in my practice account, how do I simulate those? What do I put in my sizer to buy as much currency as possible with the current leverage, ofr example? -
classMySizer(bt.Sizer): params = ( ('leverage', 50) ) def _getsizing(self, comminfo, cash, data, isbuy): return math.floor(cash * self.p.leverage / data[0]) ...setcommission(leverage=50) ...addSizer(MySizer)
would that be correct?
-
You would first need to tell the broker that you want to use leverage. Or else your account won't have the resources. (You stated it above, but to avoid having it lost in the mist)
-
Docs - Broker - Method: setcommission
This creates a
CommissionInfo
instance with your parameters of choice (see below) -
BTFD - Reality Bites where leverage is used in the sample in the post.
The
Sizer
receives acomminfo
parameter with is theCommissionInfo
instance, which will already take into account any leverage you have configured in the broker. See:- Docs - Commissions: Stocks vs Futures Method: getsize.
You don't have to keep track of the leverage manually. It seems you want to go all-in. This may fail because the calculation price is the last calculation price (
close
) and a gap can make the next incoming price (theopen
of the next bar) significantly different to generate an order rejection when going all-in.In the
BTFD - Reality Bites
from above you can see that the all-in is exercised withorder_target_percent
and percentages close to1
but leaving some room to avoid order rejection. Andgetsize
will be called for you without having to have aSizer
. -
-
@backtrader Thanks for your informative replies. I will take a look at the blog post!
Have a wonderful day. ^^ -
Warning: one more newbie question about sizing: I want to trade with oanda and have a USD account. If I use order_target_percent to calulate the sizing, in the end the backtrader has to pass "units" parameter to the Oanda API. The end price varies depending on what units you are buying.
- If I buy one lot of USD/ZAR the units will be 100.000 and the price before leverage is also 100.000 USD.
- If I buy one lot of EUR/USD the units will be 100.000, but the "price" paid before the leverage is 100.000/eur_usd_exhange_rate.
I guess my question is whether the size calculation takes into the account that the price/margin needed for one unit of a currency pair depends on the currency pair being traded.
Hence,
order_target_percent
should render different sizes depending on the pair traded and the currency used, right? -
@Roman-Semko said in Backtesting oanda/forex:
I guess my question is whether the size calculation takes into the account that the price/margin needed for one unit of a currency pair depends on the currency pair being traded.
That's why custom sizers can be used. You override
_getsizing
and then provide the actual exchange rate, which may actually be dynamically fetched from somewhere.Or else directly in the
CommissionInfo
object.