Back

Simple moving average

would love to get help writing a simple 10 steps moving average for last 9 full 1M Bars close price + last tick price quote (total 10)

working by ticks of course

thanks,

Eyal.
Update Backtest








Hi, maybe these can help You, is the code that have the sum of the lastest 100 samples, to get the avg. just need to divide it for 100. I wrote an article explaining the process in my blog.


if( CurrentSample >= 100 )
{
// Add to the queue
ValuesQueue.Enqueue( CurrentPrice );

// sum the price
SumAvg = SumAvg + CurrentPrice;

// deduct the oldest element inserted in the queue
decimal dequeue = ValuesQueue.Dequeue();
SumAvg = SumAvg - dequeue;
}
else
{
ValuesQueue.Enqueue( CurrentPrice );
SumAvg = SumAvg + CurrentPrice;
}
1

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 use tick data you need to overwrite the onTick function.


public override void OnTick(Dictionary ticks)
{
try
{
/*
* The dictionary have the securities that you select in Initialize() function
* and you can select them using the string of their respective symbol.
* ticks[" Symbol "]
*/
}
catch (Exception err)
{
Error("This is an error handler." + err.Message);
}
}


The tick documentation
1

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 @Gustavo, I think he means 10 min moving average using tick data (which is not time synchronized). This is highly inefficient but for example with LINQ + second data

Dictionary archive = new Dictionary();
public OnTradeBar(Dictionary data) {
archive.Add(data["EURUSD"].Time, data["EURUSD"].Close);

decimal last9min = (from time in archive
where time.Key > Time.AddMinutes(-10) && time.Key < Time.AddMinute(-1)
select time.Values.Close).Average();

decimal floatingAvg = 0.1m * data["EURUSD"].Close + 0.9 * last9min;

//Trim Array length:
archive = (from kvp in archive
where kvp.Key > Time.AddMinutes(-11)
select kvp).ToDictionary();
}
1

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.


I want to run the algorithm on ticks (working on every tick), but i want to know the average of the last 9 1M bars + the current tick price ...
0

@Eyal I saw your code and the error that you are getting its because you are not writing well the header for the function.

public override void OnTradeBar(Dictionary data) {
/*
* Your Code
*/
}


Another error that I found is the data can only be sended to one type of resolution. So if you want to work with tick data you have to convert the tick data to a trade bar.
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.


Here's a short class for compiling longer bars, I'll roll it into a Community template for a long term strategy once we release the new features -

// Inline method for capturing tick into minute or second bars:
public class Aggregator {

public DateTime Time;
public TimeSpan SpanOfBar;
public string Symbol = "";

public decimal Open = -1;
public decimal High = 0;
public decimal Low = 0;
public decimal Close = 0;
public long Volume = 0;
public int Count = 0;

public Aggregator(DateTime newCandleTime, TimeSpan timeframe, string symbol) {
Time = newCandleTime;
SpanOfBar = timeframe;
Symbol = symbol;
}

public bool HasData {
get {
return (Count > 0);
}
}

//Add the tick, return false if out of time allocation:
public bool AddTick(decimal price, DateTime newTickTime, long size) {

if (newTickTime > (Time + SpanOfBar)) return true;
//Add the data
Count++;
Volume += size;
if (Open == -1) Open = price;
if (price > High || High == 0) High = price;
if (price < Low || Low == 0) Low = price;
Close = price;
return false;
}

//Return the current tradebar:
public TradeBar GetBar() {
return new TradeBar(Time, Symbol, Open, High, Low, Close, Volume);
}
} // End of Aggregator
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.


Are you going to add capabilities to get all of the standard daily based technical indicators, such as moving averages, exp averages, stochastics and the like? It would be good to have ways to get these which are both efficient from a code standpoint, and efficient from a programmer standpoint.
0

Yes completely agree @Ric, we're trying to find an intern to code a whole technical library. Its been tricky finding one with a strong enough coding background.

If it comes to it I'll spend a weekend and write the library --
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.


Could you just use TA-Lib? It's got all that sort of stuff in it...
1

Thanks @DavidBallantyne good point! Looking at it now :) Should save a lot of work.
1

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.


Hi Jared,

In the AddTick method, the line
if (newTickTime > (Time + SpanOfBar)) return true;
return true, it's not quite
if (newTickTime > (Time + SpanOfBar)) return false;

and in the method end line :
return true;
0

Thanks Ysos - Please submit the joiner to the code library (blocks icon on the left side), I submitted one a while back but it might have the old code you just highlighted.

The return true was meant to be "When returns true - bar is ready, use it". You method would be "When returns false bar is ready"? My method might miss one tick so you're right we should find another way to handle that first tick.
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.


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