| Overall Statistics |
|
Total Trades 15 Average Win 5.89% Average Loss -2.20% Compounding Annual Return 184371.007% Drawdown 5.400% Expectancy 1.104 Net Profit 17.111% Sharpe Ratio 1916.713 Probabilistic Sharpe Ratio 100.000% Loss Rate 43% Win Rate 57% Profit-Loss Ratio 2.68 Alpha 605.381 Beta 1.913 Annual Standard Deviation 0.326 Annual Variance 0.106 Information Ratio 3306.129 Tracking Error 0.186 Treynor Ratio 326.622 Total Fees $15.00 Estimated Strategy Capacity $7600000.00 Lowest Capacity Asset TSLA UNU3P8Y3WFAD |
namespace QuantConnect
{
public class StopLossExample : QCAlgorithm
{
const decimal StopLossPercent = 0.02m;
const string Symbol = "TSLA";
private decimal highestPrice = 0.0m;
private OrderTicket CurrentOrder;
private OrderTicket StopLoss;
public override void Initialize()
{
SetStartDate(2022, 3, 14);
SetEndDate(DateTime.Now.Date.AddDays(-1));
SetCash(100000);
AddSecurity(SecurityType.Equity, Symbol, Resolution.Minute);
}
public void OnData(TradeBars data)
{
var currentPrice = data[Symbol].Close;
// If no stock, enter position & set StopLoss
if (!Portfolio.HoldStock)
{
var quantity = (int)Math.Floor(Portfolio.Cash / currentPrice);
CurrentOrder = Order(Symbol, quantity);
StopLoss = StopMarketOrder(Symbol, -quantity, currentPrice * (1m - StopLossPercent));
}
// If has stock, update StopLoss if necessary
else if (currentPrice > highestPrice)
{
highestPrice = currentPrice;
StopLoss.Update(new UpdateOrderFields{ StopPrice = currentPrice * (1m - StopLossPercent) });
}
}
}
}