Back

Trading on different time frames

Hello everyone, I have a question about changing the time frame in which a program trades. For example I have been playing with the Fractal program Marcus shared and I believe it trades within a one minute time frame. If I wanted to change the code from trading on a one minute chart to a five mintue or hour chart how would I go about making those changes. 

public class ScalpingAlgorithm : QCAlgorithm
{
private WilliamsFractals _wf;


private string symbol = "SPXL";

public override void Initialize()
{
SetStartDate(2016, 8, 1);
SetEndDate(DateTime.Now);

SetCash(10000);

AddSecurity(SecurityType.Equity, symbol, Resolution.Minute);

Securities[symbol].FeeModel = new ConstantFeeModel(1.0m);

_wf = new WilliamsFractals();
}

public void OnData(TradeBars data)
{
_wf.Update(data[symbol]);

if (_wf.IsReady)
{
if (data[symbol].Price >= _wf.BarryUp)
{
SetHoldings(symbol, -1.0m);
}
else if (data[symbol].Price <= _wf.BarryDown)
{
SetHoldings(symbol, 1.0m);
}
}
}
}
}

 

Update Backtest








Hi John,

Please checkout the QuantConnect University (QCU) example "How do I Use Consolidators?" for using different time frames.


On Initialize, you need to include a TradeBarConsolidator:

public override void Initialize()
{
SetStartDate(2016, 8, 1);
SetEndDate(DateTime.Now);

SetCash(10000);

AddSecurity(SecurityType.Equity, symbol, Resolution.Minute);

Securities[symbol].FeeModel = new ConstantFeeModel(1.0m);

_wf = new WilliamsFractals();

// define our 5 minute trade bar consolidator. we can access
// the 5 minute bar from the DataConsolidated events
var fileMinuteConsolidator =
new TradeBarConsolidator(TimeSpan.FromMinutes(5));

// attach our event handler. the event handler
// is a function that will be called each time
// we produce a new consolidated piece of data.
fiveMinuteConsolidator.DataConsolidated += FiveMinuteBarHandler;

// this call adds our 5 minute consolidator to
// the manager to receive updates from the engine
SubscriptionManager
.AddConsolidator(symbol, fiveMinuteConsolidator);
}

Then you move your logic to the new handler and remove it from OnData:

private void FiveMinuteBarHandler(object sender, TradeBar data)
{
_wf.Update(data);

if (_wf.IsReady)
{
if (data.Price >= _wf.BarryUp)
{
SetHoldings(symbol, -1.0m);
}
else if (data.Price <= _wf.BarryDown)
{
SetHoldings(symbol, 1.0m);
}
}
}

public void OnData(TradeBars bars)
{
// we need to declare this method
}

 

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 Alexandre! This helps a lot, I'm very new to coding and it can be very confusing at times 

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