Back

Getting current state of TEMA?

I am using this code to get data from a TEMA and add it to a rolling window of data.

TripleExponentialMovingAverage _tema; public override void Initialize()
{

//Start and End Date range for the backtest:
SetStartDate(2015, 1, 1);
UniverseSettings.Leverage = 0;
SetEndDate(DateTime.Now.Date.AddDays(-1));

//Cash allocation
SetCash(25000);


SetBrokerageModel(BrokerageName.FxcmBrokerage);
//AddForex("EURUSD", Resolution.Hour, Market.FXCM);
AddSecurity(SecurityType.Forex,"EURUSD", Resolution.Hour);

var hourlyConsolidator = new TradeBarConsolidator(TimeSpan.FromHours(30));

hourlyConsolidator.DataConsolidated += OnDataHourly;

SubscriptionManager.AddConsolidator("EURUSD", hourlyConsolidator);
}

//hourly data handler
private void OnDataHourly(object sender, TradeBar consolidated)
{
_eurusdHourly = consolidated;
var tema = TEMA("EURUSD", 17, Resolution.Hour);
}
public class TEMAState
{
public readonly decimal Current;
public TEMAState(TripleExponentialMovingAverage tema)
{
Current = _tema.ComputeNextValue("EURUSD", 17, Resolution.Hour);
}
}

I get an error for "Current = _tema.ComputeNextValue("EURUSD", 17, Resolution.Hour);"

"An object reference is required for a non-static field, method, or property, 'BasicTemplateAlgorithm._tema' "

Does anyone have any ideas to get what I want done?

 

 

 

Update Backtest








When we are saving an indicator in a rolling window, we need to create the indicator once and every time we want to add it to the rolling window, we nee to save a reference of it:

// In Initialize: Create the indicator
_tema = TEMA("EURUSD", 17);

// In OnData
// Save the reference
var temaState = new TEMAState(_tema);
// Add to rolling window
_win.Add(temaState)

The class that we use to save the reference just copies the property/field from the indicator to a property/field in the class, we do not need to call method:

public class TEMAState
{
public readonly decimal Value;

public TEMAState(TripleExponentialMovingAverage tema)
{
Value = tema.Current;
}
}

Please checkout the attached project for a full working simple example.

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.


Thanks, @AlexandreCatarino, this was very helpful.

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