QuantConnect Community.
LEAN is the open source algorithmic trading engine powering QuantConnect. Founded in 2013 LEAN has been built by a global community of 80+ engineers and powers more than a dozen hedge funds today.
Alpha League Competition: $1,000 Weekly Prize Pool.
Qualifying Alpha Streams Reentered Weekly Learn more.
Filter Discussion by Tags.
Community Ambassadors.
Earn free QuantConnect cloud credits by helping others, each week we airdrop credits to the most active community members.
222,873 Quants.
Online Now.
Stay Connected.
Stay connected with all the latest updates with email alerts or joining our Discord server.
Forex Spread.
Started By:
Greg L INVESTOR.
| Reward Discussion.
Looking through the API docs and dont seem to be able to find out how / where the spread (in pips usually) is calculated or set for the Forex data. Does anyone know how to do this?
Disclaimer.
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.
Disclaimer.
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.
Hey Greg! The TradeBars (seconds, minutes) use the midpoints of the bid-ask pricing. To get the true-spread on the day you'd have to use tick data for the backtest.
You can apply a slippage factor to your trades which would model the effect of spread without needing to use tick data. This would be a "custom transaction model" which has a method "GetSlippage".
I'd recommend checking out the university for examples of using a custom transaction model. Our FX data (and spread) is provided by FXCM.
Disclaimer.
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.
Thanks Jared! I'm taking a look at the CustomTransactionModel example. Just a heads up that sample does not compile as it is - looks like there are missing methods that the ISecurityTransactionModel Interface specifies. This wont prevent me from my analysis but wanted to let you know.
In the Lean project from github I see there is already a custom ForexTransactionModel - when I AddSecurity(SecurityType.Forex. ) does this automatically use the ForexTransactionModel or do I have to specify this as the TransactionModel to use?
Also was not aware that FXCM went to a commission based trade instead of a bid/ask spread and this is accounted for in the ForexTransactionModel.
The on the first override of ForexTransactionModel.GetOrderFees (approx line 114) this essentially asks my next question - how to handle this change in a backtest environment (i.e. using bid/ask spread up until the point in time when the commissions replaced the spread). Do you have any thoughts on how one might compensate for this?
Thanks again for your time, Greg.
No problem Greg, we're here to help. I've fixed the custom transaction model QCU algorithm.
I'd suggest handling it how I specified above: using second/minute data, which is always the midpoint, and then applying your own fixed spread/slippage on that. I've attached a simplistic example with the "minimum viable slippage model" :)
I've set it to 20 slippage per "unit" purchased to exaggerate the effect (dollars per share/euro per eurusd)
Disclaimer.
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.
Thanks Jared! That'll get me going! Appreciate your help!!
Hi, I have a few classes coded using the FXCM Api ( ForexConnect API ) and I'd like to migrate them to QuanConnect. It's not being easy so far. Particularly the order creation logic. In ForexConnect I would create a Market order specifying: the number of lots (1 lot = 1000 units ), the stop loss and limit (take profit) values in pips. I was looking at the "OrderTicketDemoAlgorithm" class and I'm confused. On a long trade I'd put my stop loss below the market value and my take profit above. But the examples on that class seems to be doing the opposite. Also the comments "When placing a long trade, the stop price must be above current market price". Can you please throw some light on this?. Thanks.
Welcome @nsampietro! We don't have a single order which allows stop-loss/take profit behavior, but you can replicate it with three orders: e.g.
1. Your market order for 1000 EURUSD. 2. Take profit - a sell limit order for -1000 EURUSD which will sell the holdings when a price is reached. 3. Stop loss - a -1000 quantity EURUSD stop market order for less than your market fill price.
"When placing a long trade, the stop price must be above current market price" -> This means when placing a stop which has a positive quantity (not talking about the holdings).
Disclaimer.
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.
Thanks Jared, you are my hero. Pass the order creation I now have another question. I'm trying to understand the "Overall Statistics" values. Perhaps I should explain myself with and example. Imagine I have an algorithm that returns this values:
STATISTICS:: Total Trades 32 STATISTICS:: Average Win 0.67% STATISTICS:: Average Loss -0.61% STATISTICS:: Compounding Annual Return -32.497% STATISTICS:: Drawdown 5.500% STATISTICS:: Expectancy -0.347 STATISTICS:: Net Profit -3.387% STATISTICS:: Sharpe Ratio -3.267 STATISTICS:: Loss Rate 69% STATISTICS:: Win Rate 31% STATISTICS:: Profit-Loss Ratio 1.09 STATISTICS:: Alpha 0 STATISTICS:: Beta 0 STATISTICS:: Annual Standard Deviation 0.09 STATISTICS:: Annual Variance 0.008 STATISTICS:: Information Ratio 0 STATISTICS:: Tracking Error 0 STATISTICS:: Treynor Ratio 0 STATISTICS:: Total Fees $64.00.
This algorithm is trading 1 lot (quantity=1000) of EURUSD at a time. Now, I run again the same algorithm, same period, same symbol, but now trading 10 lots at a time . This are the results:
STATISTICS:: Total Trades 32 STATISTICS:: Average Win 7.30% STATISTICS:: Average Loss -3.93% STATISTICS:: Compounding Annual Return 1034.751% STATISTICS:: Drawdown 27.700% STATISTICS:: Expectancy 0.428 STATISTICS:: Net Profit 23.733% STATISTICS:: Sharpe Ratio 2.097 STATISTICS:: Loss Rate 50% STATISTICS:: Win Rate 50% STATISTICS:: Profit-Loss Ratio 1.86 STATISTICS:: Alpha 0 STATISTICS:: Beta 0 STATISTICS:: Annual Standard Deviation 1.154 STATISTICS:: Annual Variance 1.331 STATISTICS:: Information Ratio 0 STATISTICS:: Tracking Error 0 STATISTICS:: Treynor Ratio 0 STATISTICS:: Total Fees $64.00.
I guess, my question is, how come only the quantity traded can affect so drastically the statistics?. More important, I think that the algorithm makes profit on both cases, even though, the first results doesn't seems to show it. I created a "Custom log" of the trades, calculating the number of pips I collect (or loss ) on each Trade (two orders) . This is what I got:
-------------------------------------- Trades ---------------------------------- # OpenTime CloseTime Type Result 1 1/5/2022 3:11:00 AM 1/5/2022 4:58:00 AM SHORT -18.79p (loss) 2 1/7/2022 4:31:00 AM 1/7/2022 4:58:00 AM SHORT -6.57p (loss) 3 1/9/2022 6:37:00 PM 1/11/2022 5:00:00 AM SHORT -50.66p (loss) 4 1/12/2022 2:48:00 PM 1/13/2022 4:58:00 AM SHORT -19.36p (loss) 5 1/14/2022 3:24:00 PM 1/15/2022 4:58:00 AM SHORT -33.80p (loss) 6 1/15/2022 2:40:00 PM 1/16/2022 4:58:00 AM SHORT 20.17p (win) 7 1/16/2022 9:02:00 PM 1/18/2022 5:00:00 AM SHORT -71.51p (loss) 8 1/21/2022 7:16:00 PM 1/22/2022 4:58:00 AM LONG -24.73p (loss) 9 1/22/2022 6:46:00 PM 1/23/2022 4:58:00 AM SHORT 236.11p (win) 10 1/23/2022 10:38:00 AM 1/25/2022 5:00:00 AM SHORT 115.70p (win) 11 1/26/2022 1:16:00 AM 1/26/2022 4:58:00 AM SHORT 15.22p (win) 12 1/26/2022 5:12:00 AM 1/27/2022 4:58:00 AM LONG 62.76p (win) 13 1/27/2022 2:13:00 PM 1/28/2022 4:58:00 AM LONG 44.08p (win) 14 1/29/2022 12:11:00 AM 1/29/2022 4:58:00 AM SHORT 60.09p (win) 15 1/29/2022 10:08:00 PM 1/30/2022 4:58:00 AM SHORT -38.01p (loss) 16 1/30/2022 6:02:00 PM 2/1/2022 5:00:00 AM SHORT 10.64p (win) ------------------------------------------------------------------------------- Total LOSS (pips) 263.437735, Total WIN (pips) 564.764520 -------------------------------------------------------------------------------
And ,as I excepted , I got exactly the same results trading 1 or 10 lots. what I'm missing here ?