Back

Simple code to translate from NinjaTrader

Hi everyone,

I am new to Quantconnect, I used to code with NinjaTrader that is also in C# but is not exactly the same syntax.
Could you please help me to translate this very basic strategy from NT to QuantConnect ?

if (Close[0] && (Close[0]/Open[0]-1)>-0.01)
{
EnterLong(100, "");
}

if (Close[0]>Open[0]
&& (Close[0]/Open[0]-1)>0.01)
{
EnterShort(100, "");
}

// Exit conditions
if(Position.MarketPosition==MarketPosition.Long
&& GetCurrentAsk() {
ExitLong();
}

if(Position.MarketPosition==MarketPosition.Short
&& GetCurrentAsk()>High[1])
{
ExitShort();
}


Thank you in advance.
Chris
Update Backtest








Here's, but you should check your code as some logic is missing. And you think on how exactly you want to liquidate as getting ask price assumes you want to trade on tick data, which wouldn't work.


//before init
RollingWindow _window = new RollingWindow(2);

//OnData
_window.Add(data[_symbol]);
if(!_window.IsReady) return;

if(!Portfolio.HoldStock && (_window[0].Close / _window[0].Open-1) > -0.01)
{
Order(_symbol, 100);
}

if(!Portfolio.HoldStock && _window[0].Close > _window[0].Open && (_window[0].Close / _window[0].Open-1) > 0.01)
{
Order(_symbol, -100);
}

// Exit conditions
if(Portfolio[_symbol].IsLong)
{
Liquidate();;
}

if(Portfolio[_symbol].IsShort && _window[0].Close > _window[1].High)
{
Liquidate();
}
2

Thank you very much Tadas.
I will try to manipulate this code to have a better understanding.
You said that the tick data wouldn't work, is it because QuantConnect don't work with tick data or because I need to change the structure of the code?
0

If you need to work with tick data, you need to change here a lot, ticks aren't bars.
0

I think Close[0] is close of the last price bar right?

@Chris - Ticks are points (individual sales), TradeBars are groups of points made into a line with open, high, low and close values. I think you want TradeBars because your psuedo code uses Close.
0

Thank you Damien, but I actually want the entry to be base on bar but the exit to be based on the ask price so on ticks.
0

The easiest way is probably to use stop/limit orders for exits then.
0

@Chris - I think TickConsolidator is the simplest way:


// in Initialize
AddSecurity(SecurityType.Forex, symbol, Resolution.Tick)
var consolidator = new TickConsolidator(TimeSpan.FromMinutes(5))
consolidator.DataConsolidated += (sender, consolidated) =>
{
// entry logic on bar close
}

// in OnData(Ticks)
{
// exit logic on each tick
}
2

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.


If you want to make decisions on tick and trade bar data, you'll need to subscribe for tick data and perform the aggregation yourself like @Steffano suggested.
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.


Chiming in on this - admittedly a bit late ;-)

In NinjaTrader everything is a series - even NT8 uses a typed array (double) where you access a series like this:

Closes[0][0] - this would be the current bar of the first series.
Closes[1][0] - this would be the current bar of the second series.

Now in my strategies I often use the first Closes/Highs/Lows/Opens series for the candles that drive my chart rules engine. But for execution and especially for historical execution I'm using the second series and assign it tick data. So Closes[1][0] would actually be the current tick. It's an NT work around of course as a tick is a point as someone already pointed out above. Highs[1][0] would get you the very same value. One is able to filter out a particular series like this:

if (BarsInProgress == 0) {
if (FirstTickOfBar) DoSomething();
...
}

As you can imagine this can get messy quickly. I'm used to that paradigm by now but there are certain issues which affect live vs. historical tape. For example in historical tape the current Closes[0][0] bar (e.g. an hourly series) is the PREVIOUS (already closed) bar and the latest Closes[1][0] tick bar is actually the ONGOING bar. They reason they do that is because you can't have say Highs[0][0] unless you know the entire bar. However in live tape Closes[0][0] is the ONGOING bar as well and thus in live tape I have to switch the index from 0 to 1 when evaluating the last closed/complete candle. This is when their paradigm kind of breaks down and things get nasty in a heartbeat.

NinjaTrader crashes quite a bit, at least NT7 does under more complicated strategies, so picking up an ongoing campaign is very important, which is why I spent quite a bit of time implementing code that would try to restore an ongoing campaign. However things get nasty quickly and there are never guarantees that you can retain the same state. Just try to calculate position sizing based on account size - well, a day later your account size changed and thus you need to persist the ongoing campaign's units somehow because you can't recalc the position size. If you don't then you have a 2nd campaign on your hand and the old one needs to be closed manually. Just to give you a little taste of the issues we've been facing. I run a trading signal which serves several client accounts via LAMM - it is extremely difficult to guarantee an error free run due to all these types of issues.

How would I handle something like this in QuantConnect? If I have candle patterns that I evaluate like I used to via Closes[0][0] I need to check those at the close of the first series. If I want to know if the previous bar (not the open one) was a hammer then I assume I use TradeBars, right? That information may now trigger logic that tells my tick series to entry at a particular price - let's say at the breach of the high of the hammer (or low of shooting star). Clearly that is something where I will need ticks.

The issue in NT in particular is that in historical testing the system breaks down with tick data and that's the ugly truth. If I use more than a few weeks of tick data it takes hours on a super fast number cruncher and potentially days on a regular dev system. But that's theoretical as it usually just crashes and wastes your time - I have spent years trying to make it work and in the end I just gave up on tick data.

My current work around is to use 1-min bars in back testing instead of tick data. I also implemented my own backtester because I require testing via R sizes which NT doesn't support AFAIK - maybe NT8 does. It kind of works but clearly they are a ton of issues and I once again had to find work arounds. Would you expect QuantConnect to perform better here? How does it internally handle the huge amount of tick data when back testing more than a few weeks? Is it possible to define position sizes in R and test accordingly?

Not having touched QC my assumption is that there is less of a focus on charts and more on data processing and execution. I welcome that approach but wonder if it's still possible to show basic charting and implement indicators for visualization.

Many thanks in advance for any insights.
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