Overall Statistics
import numpy as np
import pandas as pd
from datetime import datetime
import decimal as d

### <summary>
### Basic template algorithm simply initializes the date range and cash. This is a skeleton
### framework you can use for designing an algorithm.
### </summary>
class BasicTemplateAlgorithm(QCAlgorithm):
    '''Basic template algorithm simply initializes the date range and cash'''

    def Initialize(self):
        '''Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.'''

        self.SetStartDate(2017,1, 10)  #Set Start Date
        self.SetEndDate(2018,12,31)    #Set End Date
        self.SetCash(10000)           #Set Strategy Cash
        # Find more symbols here: http://quantconnect.com/data
        self.AddForex("EURUSD", Resolution.Hour, Market.Oanda)
        self.SetTimeZone("Etc/GMT0")
        #self.Schedule.On(self.DateRules.EveryDay("EURUSD"), self.TimeRules.Every("Hour"), self.EveryHour)
        self.predASK=pd.read_csv('http://sensedrive.science/files/EURUSD1H_CHL_ASK_cat.csv', header=0)
        self.predBID=pd.read_csv('http://sensedrive.science/files/EURUSD1H_CHL_BID_cat.csv', header=0)
        self.Debug("Everything loaded")
    def OnData(self, data):
        '''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.

        Arguments:
            data: Slice object keyed by symbol containing the stock data
        '''
        if data.ContainsKey("EURUSD"):
            quoteBar = data['EURUSD']
            highASK = self.predASK[self.predASK["DateTime"]==datetime.strftime(quoteBar.EndTime, format="%Y-%m-%d %H:%M:%S")]["high"]
            lowASK = self.predASK[self.predASK["DateTime"]==datetime.strftime(quoteBar.EndTime, format="%Y-%m-%d %H:%M:%S")]["low"]
            if sum(highASK.notna()) >=1 & sum(lowASK.notna()) >=1 :
                self.Log(f"Time: {quoteBar.EndTime}") #The time the period closes
                self.Log(f"Open: {quoteBar.Open}")
                self.Log(f"High ASK: {highASK}")
                self.Log(f"Low BID: {lowASK}")
                highASKOpendiff= (highASK.iloc[0]- quoteBar.Open)
                self.Log(f"highASKOpendiff: {highASKOpendiff}")
                lowASKOpendiff= (quoteBar.Open-lowASK.iloc[0])
                self.Log(f"lowASKOpendiff: {lowASKOpendiff}")
                BuyTPASK=d.Decimal(highASKOpendiff*0.8)
                self.Log(f"BuyTPASK: {BuyTPASK}")
                BuySLASK=highASKOpendiff*2
                self.Log(f"BuySLASK: {BuySLASK}")
                SellTPASK=d.Decimal(lowASKOpendiff*0.8)
                self.Log(f"SellTPASK: {SellTPASK}")
                SellSLASK=lowASKOpendiff*2
                self.Log(f"SellSLASK: {SellSLASK}")
                price = float(data["EURUSD"].Price)
                if highASKOpendiff > 0.0030:
                    self.Buy("EURUSD", 100)
                    self.LimitOrder("EURUSD", -100, (price +BuyTPASK))
                    self.StopMarketOrder("EURUSD", -100, (price -BuySLASK))
                if lowASKOpendiff > 0.0030:
                    self.Sell("EURUSD", 100)
                    self.LimitOrder("EURUSD", -100, (price -SellTPASK))
                    self.StopMarketOrder("EURUSD", -100, (price + SellSLASK))
            else:
                pass
    
    def OnOrderEvent(self, orderEvent):
        order = self.Transactions.GetOrderById(orderEvent.OrderId)
        
        if order.Status == OrderStatus.Filled:
            if order.Type == OrderType.Limit or order.Type == OrderType.Limit:
                self.Transactions.CancelOpenOrders(order.Symbol)
                
        if order.Status == OrderStatus.Canceled:
            self.Log(str(orderEvent))
    
    def EveryHour(self):
        pass