Hi. I schedule a user-defined function and I get the following error:
Runtime Error: In Scheduled Event 'EveryDay: Every 60 min', AttributeError : 'float' object has no attribute 'EndTime' AttributeError : 'float' object has no attribute 'EndTime'
The code is the following:
import numpy as np
import pandas as pd
from QuantConnect.Data import SubscriptionDataSource
from QuantConnect.Python import PythonData
from datetime import date, timedelta, datetime
from decimal import Decimal
import math
import calendar
### <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.AddEquity("SPY",Resolution.Minute)
self.AddForex("EURUSD", Resolution.Minute, Market.Oanda)
self.AddData(predEURUSDASK, "predEURUSDASK", Resolution.Hour, fillforward=True)
self.AddData(predEURUSDBID, "predEURUSDBID", Resolution.Hour, fillforward=True)
self.SetTimeZone("Etc/GMT0")
self.Debug("Everything loaded")
self.risk = 1
self.Securities["EURUSD"].SetLeverage(500)
self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.Every(TimeSpan.FromMinutes(60)), Action(self.EveryHour))
def OnData(self, data):
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.StopMarket:
self.Transactions.CancelOpenOrders(order.Symbol)
if order.Status == OrderStatus.Canceled:
self.Log(str(orderEvent))
def EveryHour(self):
quoteBar = self.Securities["EURUSD"].Open
self.Log(f"Time: {quoteBar.EndTime}") #The time the period closes
self.Log(f"Open: {quoteBar.Open}")
# Retrieve High Ask Price from object
highASK=self.Securities["predEURUSDASK"].Value
self.Log(f"High ASK: {highASK}")
# User variables
highASKOpendiff = highASK- quoteBar
self.Log(f"highASKOpendiff: {highASKOpendiff}")
BuyTPASK=highASKOpendiff*0.8
self.Log(f"BuyTPASK: {BuyTPASK}")
BuySLASK=highASKOpendiff*10
self.Log(f"BuySLASK: {BuySLASK}")
# Retrieve Low Bid Price from object
lowBID=self.Securities["predEURUSDBID"].Value
self.Log(f"Low BID: {lowBID}")
# User variables
OpenLowBIDdiff = quoteBar - lowBID
self.Log(f"OpenLowBIDdiff: {OpenLowBIDdiff}")
SellTPBID=OpenLowBIDdiff*0.8
self.Log(f"SellTPBID: {SellTPBID}")
SellSLBID=OpenLowBIDdiff*10
self.Log(f"SellSLBID: {SellSLBID}")
qty = math.floor(self.Portfolio.Cash * self.risk/self.Securities["EURUSD"].Price) #MarginRemaining
if highASKOpendiff >= OpenLowBIDdiff:
if not self.Portfolio.Invested:
self.Buy("EURUSD", qty) # every hour
#Approach 1: self.SetHolding("EURUSD",0.2)
averageFillPrice = self.Portfolio["EURUSD"].AveragePrice
self.LimitOrder("EURUSD", -qty, (averageFillPrice +BuyTPASK))
self.StopMarketOrder("EURUSD", -qty, (averageFillPrice -BuySLASK))
else:
if not self.Portfolio.Invested:
self.Sell("EURUSD", qty)
averageFillPrice = self.Portfolio["EURUSD"].AveragePrice
self.LimitOrder("EURUSD", qty, (averageFillPrice -SellTPBID))
self.StopMarketOrder("EURUSD", qty, (averageFillPrice + SellSLBID))
class predEURUSDASK(PythonData):
#"Import custom data from a csv file"
def GetSource(self, config, date, isLiveMode):
return SubscriptionDataSource("http://sensedrive.science/files/EURUSD1H_CHL_ASK_cat_shifted.csv", SubscriptionTransportMedium.RemoteFile)
def Reader(self, config, line, date, isLiveMode):
if not (line.strip() and line[0].isdigit()): return None
index = predEURUSDASK()
index.Symbol = config.Symbol
try:
data = line.split(',')
index.Time = datetime.strptime(data[0], "%d/%m/%Y %H:%M")
### Retrieve "predicted" High Ask Price
index.Value = Decimal(data[2])
except:
return None
return index
class predEURUSDBID(PythonData):
#"Import custom data from a csv file"
def GetSource(self, config, date, isLiveMode):
#Changed BID -> ASK
return SubscriptionDataSource("http://sensedrive.science/files/EURUSD1H_CHL_ASK_cat_shifted.csv", SubscriptionTransportMedium.RemoteFile)
def Reader(self, config, line, date, isLiveMode):
if not (line.strip() and line[0].isdigit()): return None
index = predEURUSDBID()
index.Symbol = config.Symbol
try:
data = line.split(',')
index.Time = datetime.strptime(data[0], "%d/%m/%Y %H:%M")
### Retrieve "predicted" Low Bid Price
index.Value = Decimal(data[3])
except:
return None
return index
Gurumeher Sawhney
Hi Georgios, the error comes from the line:
quoteBar = self.Securities["EURUSD"].Open
self.Securities["EURUSD"] provides you with a Security object, and the Security object has a field Open that returns the most recent open price. This is a float.
self.Securities["EURUSD"].Open does not return a quoteBar. As a result, the consequent lines that call upon QuoteBar fields gives a runtime error.
Georgios Kourogiorgas
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!