I understand win rate is defined as number of profitable trades / total trades.
If I place buy and sell orders separately by calling market_order methods, how does LEAN match my buy and sell orders? to check whether the trade is win or loss?
QUANTCONNECT COMMUNITY
I understand win rate is defined as number of profitable trades / total trades.
If I place buy and sell orders separately by calling market_order methods, how does LEAN match my buy and sell orders? to check whether the trade is win or loss?
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.
Mia Alissi
Here’s how LEAN matches your buy and sell orders when you place them separately with market_order, and how it determines win/loss for each trade:
When you use MarketOrder to buy, LEAN opens a position in that security. Each buy order is tracked—as you accumulate a position, LEAN keeps an ordered record of your individual buys.
If you later use MarketOrder to sell, LEAN offsets (matches) the sell quantity against your oldest open buys, using a FIFO (first-in, first-out) matching logic. That means the earliest buys are matched and closed out first.
If your sell order closes multiple partial or full buy orders, each portion is considered a “closed trade” by LEAN. LEAN automatically tracks the matched buy and sell prices, quantities, and times.
The profit/loss (PnL) for a trade is calculated as the difference between buy and sell prices (accounting for fees or commissions if applicable), at the moment that portion of your position is closed. If this amount is positive, it’s a “winning” trade; if negative, it’s a “losing” trade.
You do not need to manually assign which sell matches which buy—LEAN’s transaction engine does this automatically using the FIFO system. This is also how LEAN computes your trade statistics, including win rate.
In summary, whenever you enter and exit positions through separate buy and sell orders, LEAN pairs them up for you. It tracks and evaluates each resulting trade for you, so your statistics like win rate are always up to date and accurate.
Sources: Trading and Orders - QuantConnect.com, Key Concepts - QuantConnect.com, Terminal Link - QuantConnect.com, Market Orders - QuantConnect.com, Futures Fast Trend Following, with Trend Strength
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.
Creative Cubs
Strange!
That is not what I am seeing…
In below back test.. I see some successful trades (Buy market at 105, sell market at 106), it should be considered win trade, but in backtest stats, win rate is 0.
Creative Cubs
Ok… if I am closing my position using liquidate it is always considered loss trade.
Creative Cubs
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.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!