Back

SMA and EMA of NASDAQ and S&P

Hi Can someone help me make an SMA and EMA of the nasdaq and S&P mini? i'm at a brick wall here..i've explored 

https://github.com/QuantConnect/Lean/blob/fdc866fda04e3de5a2c4ec892a4569b3face5700/Algorithm.Python/HistoryAlgorithm.py

 and other links but i can't get the code implementation right. 

Update Backtest








0

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.


Hey, this one should help you with the implementation. It's just a case of missing parameters and incorrect syntax.

https://github.com/QuantConnect/Lean/blob/master/Algorithm.Python/IndicatorSuiteAlgorithm.py
0

Thanks for your help. I read over this code and implemented some changes. This is the error i'm getting now. 

 

During the algorithm initialization, the following exception has occurred: Trying to dynamically access a method that does not exist throws a TypeError exception. To prevent the exception, ensure each parameter type matches those required by the EMA method. Please checkout the API documentation.
at Initialize in main.py:line 35
TypeError : No method matches given arguments for EMA

 

# https://quantpedia.com/Screener/Details/100
from QuantConnect.Data import SubscriptionDataSource
from QuantConnect.Python import PythonData
from QuantConnect.Python import PythonQuandl
from QuantConnect.Data.Custom import *
from sklearn import datasets, linear_model
from datetime import date, timedelta, datetime
from collections import deque
import statsmodels.api as sm
from decimal import Decimal
import numpy as np


class TradeWtiBrentSpreadAlgorithm(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2018, 6, 1)
        self.SetEndDate(2018, 12, 1)
        self.SetCash(1000000)
        # import the custom data 
        self.ES = self.AddFuture(Futures.Indices.SP500EMini, Resolution.Daily) 
        self.ES.SetFilter(timedelta(0), timedelta(days=180))
     
            
        #Create the EMA spread for the liquidate signal
      
        self.es1ema = self.EMA(self.ES, 7, Resolution.Daily)

0

If you check the file I linked, you can see that your syntax is still not correct. self.EMA takes a string as the first parameter, you've given it method.

0

Hi Douglas, i've tried using a string. when i do self.es1ema = self.EMA("ES", 7, Resolution.Daily) i get this error. i then follow the suggestion of the error by adding the security with

self.AddSecurity(SecurityType.Future, "ES", Resolution.Minute) and that doesn't work

During the algorithm initialization, the following exception has occurred: Exception : Please register to receive data for symbol ' ' using the AddSecurity() function.
at QuantConnect.Algorithm.QCAlgorithm.GetSubscription (QuantConnect.Symbol symbol, System.Nullable`1[T] tickType) [0x00098] in <1d3439f9966d4d178af513edc463b9bd>:0
at QuantConnect.Algorithm.QCAlgorithm.GetSubscription (QuantConnect.Symbol symbol) [0x00001] in <1d3439f9966d4d178af513edc463b9bd>:0
at QuantConnect.Algorithm.QCAlgorithm.CreateIndicatorName (QuantConnect.Symbol symbol, System.String type, System.Nullable`1[T] resolution) [0x00010] in <1d3439f9966d4d178af513edc463b9bd>:0
at QuantConnect.Algorithm.QCAlgorithm.EMA (QuantConnect.Symbol symbol, System.Int32 period, System.Nullable`1[T] resolution, System.Func`2[T,TResult] selector) [0x00013] in <1d3439f9966d4d178af513edc463b9bd>:0
at (wrapper managed-to-native) System.Reflection.MonoMethod.InternalInvoke(System.Reflection.MonoMethod,object,object[],System.Exception&)
at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00032] in <2e7c1c96edae44d496118948ca617c11>:0
at Initialize in main.py:line 51
Exception : Please register to receive data for symbol ' ' using the AddSecurity() function.
at QuantConnect.Algorithm.QCAlgorithm.GetSubscription (QuantConnect.Symbol symbol, System.Nullable`1[T] tickType) [0x00098] in <1d3439f9966d4d178af513edc463b9bd>:0
at QuantConnect.Algorithm.QCAlgorithm.GetSubscription (QuantConnect.Symbol symbol) [0x00001] in <1d3439f9966d4d178af513edc463b9bd>:0
at QuantConnect.Algorithm.QCAlgorithm.CreateIndicatorName (QuantConnect.Symbol symbol, System.String type, System.Nullable`1[T] resolution) [0x00010] in <1d3439f9966d4d178af513edc463b9bd>:0
at QuantConnect.Algorithm.QCAlgorithm.EMA (QuantConnect.Symbol symbol, System.Int32 period, System.Nullable`1[T] resolution, System.Func`2[T,TResult] selector) [0x00013] in <1d3439f9966d4d178af513edc463b9bd>:0
at (wrapper managed-to-native) System.Reflection.MonoMethod.InternalInvoke(System.Reflection.MonoMethod,object,object[],System.Exception&)
at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00032] in <2e7c1c96edae44d496118948ca617c11>:0

0

Again, please look at the link I sent you. It's done correctly in there and it would help if you read it line by line to see what's going on.

  1. You need to first register and subcribe to your security by using the Add[...]() function (line 51 in the file). This is just a method and you do not need to use its returned value or assign it to anything. The purpose of that method is to register the algorithm to receive data for a particular security.
  2. Next, you need to pass the symbol of the security you just registered to the EMA() method (line 58 in the file). As you can see in the file, self.symbol is used. That variable is a string and is defined on line 42.
So, in summary - always register your securities first, and always pass appropriate parameters to functions. If you're unsure of the syntax of a function, I'd recommend you go through one of the algorithm examples (such as the one I sent) and pay close attention to what data type each parameter is.Good luck!
0

Hi Douglas, 

Thanks for explaining this to me. what am i still doing wrong? I have copied it word for word except i changed the quandl and resolution.

from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Common")
AddReference("QuantConnect.Indicators")

from System import *
from QuantConnect import *
from QuantConnect.Indicators import *
from QuantConnect.Data import *
from QuantConnect.Data.Market import *
from QuantConnect.Algorithm import *
from QuantConnect.Python import PythonData
from QuantConnect.Python import PythonQuandl
from sklearn import datasets, linear_model
from datetime import date, timedelta, datetime
from collections import deque
import statsmodels.api as sm
from decimal import Decimal
import numpy as np

class TradeWtiBrentSpreadAlgorithm(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2018, 6, 1)
        self.SetEndDate(2018, 12, 1)
        self.SetCash(1000000)
        # import the custom data 
               
        self.AddSecurity(SecurityType.Future, "ES", Resolution.Minute)
        self.ESEMA = self.EMA("ES", 14, Resolution.Minute)
                            

I'm still getting the same error. 

0

i finally got it to work.

def Initialize(self):
        self.SetStartDate(2018, 6, 1)
        self.SetEndDate(2018, 12, 1)
        self.SetCash(1000000)
        # import the custom data 
        self.AddData(WTI, "WTI", Resolution.Daily)
        self.AddData(BRENT, "BRENT", Resolution.Daily)
        
        self.es = "SCF/CME_ES1_ON"
        self.AddData(QuandlFuture, self.es, Resolution.Daily)
        self.nq = "SCF/CME_NQ1_ON"
        self.AddData(QuandlFuture, self.nq, Resolution.Daily)
       
        self.esema = self.EMA(self.es, 7)
        self.nqema = self.EMA(self.nq, 7)

0

Glad to hear that :)

0

Update Backtest





0

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.


Loading...

This discussion is closed