Overall Statistics
Total Orders
266
Average Win
2.32%
Average Loss
-2.33%
Compounding Annual Return
23.219%
Drawdown
28.700%
Expectancy
0.196
Start Equity
100000
End Equity
169258.09
Net Profit
69.258%
Sharpe Ratio
0.761
Sortino Ratio
0.634
Probabilistic Sharpe Ratio
32.344%
Loss Rate
40%
Win Rate
60%
Profit-Loss Ratio
1.00
Alpha
0.036
Beta
0.733
Annual Standard Deviation
0.224
Annual Variance
0.05
Information Ratio
-0.07
Tracking Error
0.181
Treynor Ratio
0.233
Total Fees
$2200.20
Estimated Strategy Capacity
$520000000.00
Lowest Capacity Asset
AAPL R735QTJ8XC9X
Portfolio Turnover
28.74%
from AlgorithmImports import *
from QuantConnect.DataSource import *

class BrainSentimentDataAlgorithm(QCAlgorithm):
    
    latest_sentiment_value = None
    target_holdings = 0
    
    def initialize(self) -> None:
        self.set_start_date(2019, 1, 1)
        self.set_end_date(2021, 7, 8)
        self.set_cash(100000) 
        
        # Requesting data
        self.aapl = self.add_equity("AAPL", Resolution.DAILY).symbol
        self.dataset_symbol = self.add_data(BrainSentimentIndicator30Day, self.aapl).symbol
        
        # Historical data
        history = self.history(self.dataset_symbol, 100, Resolution.DAILY)
        self.debug(f"We got {len(history)} items from our history request for {self.dataset_symbol}")
        if history.empty:
            return
        
        # Warm up historical sentiment values
        previous_sentiment_values = history.loc[self.dataset_symbol].sentiment.values
        for sentiment in previous_sentiment_values:
            self.update(sentiment)
            
    def update(self, sentiment: float) -> None:
        if self.latest_sentiment_value is not None:
            self.target_holdings = int(sentiment > self.latest_sentiment_value)
        self.latest_sentiment_value = sentiment
        
    def on_data(self, slice: Slice) -> None:
        if slice.contains_key(self.dataset_symbol):
            sentiment = slice[self.dataset_symbol].sentiment
            self.update(sentiment)
           
        # Ensure we have security data in the current slice
        if not (slice.contains_key(self.aapl) and slice[self.aapl] is not None):
            return
            
        if self.target_holdings != self.portfolio.invested:
            self.set_holdings(self.aapl, self.target_holdings)