| Overall Statistics |
|
Total Trades 7 Average Win 1.01% Average Loss 0% Compounding Annual Return 490.879% Drawdown 2.900% Expectancy 0 Net Profit 3.802% Sharpe Ratio 15.733 Probabilistic Sharpe Ratio 98.346% Loss Rate 0% Win Rate 100% Profit-Loss Ratio 0 Alpha -5.456 Beta 0.817 Annual Standard Deviation 0.181 Annual Variance 0.033 Information Ratio -56.828 Tracking Error 0.129 Treynor Ratio 3.481 Total Fees $84.93 Estimated Strategy Capacity $1300000.00 Lowest Capacity Asset NB R735QTJ8XC9X |
namespace QuantConnect
{
public class StopLossExample : QCAlgorithm
{
const decimal StopLossPercent = 0.02m;
const string Symbol = "BAC";
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) });
}
}
}
}