Overall Statistics
Total Trades
156
Average Win
6.06%
Average Loss
-0.16%
Compounding Annual Return
782439924759905.00%
Drawdown
7.300%
Expectancy
11.612
Net Profit
604.350%
Sharpe Ratio
11.821
Loss Rate
67%
Win Rate
33%
Profit-Loss Ratio
37.18
Alpha
37.382
Beta
-811.447
Annual Standard Deviation
2.129
Annual Variance
4.532
Information Ratio
11.814
Tracking Error
2.129
Treynor Ratio
-0.031
Total Fees
$0.00
#
#   QuantConnect Basic Template:
#    Fundamentals to using a QuantConnect algorithm.
#
#    You can view the QCAlgorithm base class on Github: 
#    https://github.com/QuantConnect/Lean/tree/master/Algorithm
#

import numpy as np
import csv
import pandas as pd
from datetime import datetime, timezone
from dateutil import parser
import pytz
# from data_service import DataService

class BasicTemplateAldgorithm(QCAlgorithm):

    def Initialize(self):
        # Set the cash we'd like to use for our backtest
        # This is ignored in live trading 
        self.SetCash(100000)
        self.SetTimeZone(TimeZones.Utc)
        
        # Set Brokerage model to load OANDA fee structure.
        self.SetBrokerageModel(BrokerageName.OandaBrokerage)
        
        alg = 'a1'
        symbol = 'USDCAD'
        granularity = 'M5'
        
        # Add assets you'd like to see
        self.pair = self.AddForex(symbol, Resolution.Minute).Symbol
        
        file = self.Download('https://twitter-copy-pip.herokuapp.com/signal_data/{0}/{1}/{2}'.format(alg, symbol, granularity))
        # self.Debug(str(file))
        sig_data = pd.read_json(file, orient='split')
        sig_data = sig_data.sort_values(by=['time'])
        first_date = datetime.utcfromtimestamp(sig_data.iloc[0]['time']/1000)
        self.Debug('first_date'+str(first_date))
        last_date = datetime.utcfromtimestamp(sig_data.iloc[len(sig_data)-1]['time']/1000)
        self.Debug('last_date'+str(last_date))
        
        # Start and end dates for the backtest.
        # These are ignored in live trading.
        self.SetStartDate(first_date.year, first_date.month, first_date.day)
        self.SetEndDate(last_date.year, last_date.month, last_date.day)
        
        if alg == 'a1':
            action_key = 'action'
        else:
            action_key = '{0}_action'.format(alg)
        
        # self.Debug(str(sig_data))
        for i, row in sig_data.iterrows():
            dt = datetime.utcfromtimestamp(row['time']/1000)
            if row[action_key] == 'BUY':
                self.Debug('creating buy order '+ str(dt))
                self.Schedule.On(
                    self.DateRules.On(dt.year, dt.month, dt.day),
                    self.TimeRules.At(dt.hour, dt.minute),
                    Action(self.ScheduledBuy))
            if row[action_key] == 'SELL':
                self.Debug('creating sell order '+ str(dt))
                self.Schedule.On(
                    self.DateRules.On(dt.year, dt.month, dt.day),
                    self.TimeRules.At(dt.hour, dt.minute),
                    Action(self.ScheduledSell))
            if row[action_key] == 'HOLD':
                self.Debug('CLOSING order '+ str(dt))
                self.Schedule.On(
                    self.DateRules.On(dt.year, dt.month, dt.day),
                    self.TimeRules.At(dt.hour, dt.minute),
                    Action(self.ScheduledHold))
    
    def ScheduledBuy(self):
        # if self.Portfolio.Invested:
            # self.Liquidate(self.pair)
        # self.MarketOrder(self.pair, 1000000)
        self.SetHoldings(self.pair, 50.0, liquidateExistingHoldings=True)
    
    
    def ScheduledSell(self):
        # if self.Portfolio.Invested:
        #     self.Liquidate(self.pair)
        # self.MarketOrder(self.pair, -1000000)
        self.SetHoldings(self.pair, -50.0, liquidateExistingHoldings=True)
    
    def ScheduledHold(self):
        if self.Portfolio.Invested:
            self.Liquidate(self.pair)

    def OnData(self, slice):
        pass