Back

How to log Profit/Loss after a sell order

Hi all,

In a simple buy low sell high equity algo, I am trying to log profit/loss for every sell order. I assumed it would be simple but am having a hard time. 

I have tried "self.Log(self.Portfolio["SPY"].LastTradeProfit)" placed right under Set.Holdings code below and also have tried "self.Log(self.Securities["SPY"].Price-self.Portfolio["SPY"].AveragePrice)."

if self.Securities["SPY"].Price > self.Portfolio["SPY"].AveragePrice:
self.SetHoldings("SPY", 0.0, False, "TAKE PROFIT")

The log posts the wrong value and also posts it every minute instead of just when the sell order executes. I can't seem to solve both of those issues. The algo runs on minute intervals and is coded in python. Any help would be 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.


Hi Shan,

For numeric values, we suggest Plotting instead of Logging to because of the logging quota, as well as to prevent browser flooding. To make the last trade profit not Log every minute, we can check if the current profit is the same as the previous profit, and use this information to only log when a new profit occurs. As for why the profit may seem off, this is because we account for costs such as fees and spreads. If users would like to not include fees in the model, I suggest this post here.

As another side note, instead of setting holdings of a security to 0, we can liquidate it instead by using self.Liquidate(ticker/symbol). Please see the mentioned changes in the attached backtest.

Best,
Shile Wen

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.


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