Overall Statistics
Total Orders
17
Average Win
16.04%
Average Loss
-13.98%
Compounding Annual Return
32.098%
Drawdown
37.300%
Expectancy
0.670
Start Equity
100000
End Equity
164421.95
Net Profit
64.422%
Sharpe Ratio
0.696
Sortino Ratio
0.845
Probabilistic Sharpe Ratio
34.537%
Loss Rate
22%
Win Rate
78%
Profit-Loss Ratio
1.15
Alpha
0.193
Beta
0.328
Annual Standard Deviation
0.329
Annual Variance
0.108
Information Ratio
0.355
Tracking Error
0.336
Treynor Ratio
0.699
Total Fees
$0.00
Estimated Strategy Capacity
$190000.00
Lowest Capacity Asset
BTCUSD E3
Portfolio Turnover
1.83%
# region imports
from AlgorithmImports import *
# endregion


class TrumpFactorOnBitcoinPerformanceAlgorithm(QCAlgorithm):

    def initialize(self):
        self.set_start_date(2022, 11, 1)
        self.set_end_date(2024, 8, 13)  # These start/end dates align with the data file
        self.set_cash(100_000)
        self.add_crypto('BTCUSD', market=Market.BITFINEX)
        self._trump_lead = self.add_data(TrumpLead, 'TrumpLead').symbol

    def on_data(self, data):
        if self._trump_lead in data:
            self.set_holdings('BTCUSD', int(data[self._trump_lead].value >= 0))


class TrumpLead(PythonData):
    
    def get_source(self, config, date, is_live_mode):
        # Original source: https://www.realclearpolling.com/polls/president/general/2024/trump-vs-harris
        return SubscriptionDataSource("https://www.dropbox.com/scl/fi/f87o5azouo09sqecpebw2/parsed_election_data.csv?rlkey=abuch8e53oi8jlgmnt0s58jbj&st=05wgpd6s&dl=1", SubscriptionTransportMedium.REMOTE_FILE)

    def reader(self, config, line, date, is_live_mode):
        if not line[0].isdigit():
            return None
        lead = TrumpLead()
        lead.symbol = config.symbol
        data = line.split(',')
        lead.end_time = datetime.fromisoformat(data[0])
        lead.value = float(data[2]) - float(data[1]) - float(data[3]) # Lead = Trump - Harris - Biden 
        return lead