Overall Statistics
Total Trades
3
Average Win
0.44%
Average Loss
0%
Compounding Annual Return
10.506%
Drawdown
0.300%
Expectancy
0
Net Profit
0.677%
Sharpe Ratio
4.881
Loss Rate
0%
Win Rate
100%
Profit-Loss Ratio
0
Alpha
0.078
Beta
0.675
Annual Standard Deviation
0.018
Annual Variance
0
Information Ratio
3.934
Tracking Error
0.018
Treynor Ratio
0.133
Total Fees
$3.00
import numpy as np
from decimal import *

class stopLossTakeprofitExample(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2013,10, 7)  #Set Start Date
        self.SetEndDate(2013,10,31)    #Set End Date
        self.SetCash(10000)           #Set Strategy Cash
        self.AddEquity("SPY", Resolution.Hour)
    
    def OnData(self, data):
        if not self.Portfolio.Invested:
            price = data["SPY"].Close
            self.Debug('price : ' + str(price))
            plusThreePercent = Decimal(1.03)
            minusThreePercent = Decimal(0.97)
            ### Buy at current market price and simultaneously send limit sell order and stop loss order
            self.Buy("SPY", 10)
            self.LimitOrder("SPY", -10, price * plusThreePercent)
            self.StopMarketOrder("SPY", -10, price * minusThreePercent)
    
    def OnOrderEvent(self, orderEvent):
        order = self.Transactions.GetOrderById(orderEvent.OrderId)
        
        ### Cancel remaining order if limit order or stop loss order is executed
        if order.Status == OrderStatus.Filled:
            if order.Type == OrderType.Limit or OrderType.StopLimit:
                self.Transactions.CancelOpenOrders(order.Symbol)
            
            if order.Status == OrderStatus.Canceled:
                self.Log(str(orderEvent))