NexusFi: Find Your Edge


Home Menu

 





Absolute beginner here, strategy not working, any help greatly appreciated!


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one alecsaccount with 13 posts (5 thanks)
    2. looks_two trendwaves with 6 posts (6 thanks)
    3. looks_3 shodson with 2 posts (2 thanks)
    4. looks_4 Quick Summary with 1 posts (0 thanks)
    1. trending_up 6,331 views
    2. thumb_up 13 thanks given
    3. group 3 followers
    1. forum 21 posts
    2. attach_file 11 attachments




 
Search this Thread

Absolute beginner here, strategy not working, any help greatly appreciated!

  #1 (permalink)
 alecsaccount 
San Diego, California
 
Experience: Beginner
Platform: NinjaTrader 8
Trading: Emini ES, EUR/USD
Posts: 65 since Dec 2016
Thanks Given: 229
Thanks Received: 13

So I recently started messing around with automated strategy development using the strategy builder, which I tested out with this super simple bollinger cross strategy.. It's supposed to enter when the ask or bid is above or below the band, and exit if there's an open position when that happens.. But when I backtested it on high resolution (1 tick), there were all these parts where it just totally ignored exits, and that resulted in a lot of losses. I'm confused, not really experienced with any of this.. If anyone can explain what's wrong with this so I can prevent it from happening, or if I'll have to debug it could you please walk me through that as if you were explaining it to a child? Thank you so much, and sorry I'm such a noob.. Here's the code:

namespace NinjaTrader.NinjaScript.Strategies
{
public class MyCustomStrategy : Strategy
{
private Bollinger Bollinger1;

protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Strategy here.";
Name = "MyCustomStrategy";
Calculate = Calculate.OnEachTick;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
IsExitOnSessionCloseStrategy = false;
ExitOnSessionCloseSeconds = 30;
IsFillLimitOnTouch = false;
MaximumBarsLookBack = MaximumBarsLookBack.Infinite;
OrderFillResolution = OrderFillResolution.Standard;
Slippage = 0;
StartBehavior = StartBehavior.WaitUntilFlat;
TimeInForce = TimeInForce.Gtc;
TraceOrders = false;
RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
StopTargetHandling = StopTargetHandling.PerEntryExecution;
BarsRequiredToTrade = 20;
// Disable this property for performance gains in Strategy Analyzer optimizations
// See the Help Guide for additional information
IsInstantiatedOnEachOptimizationIteration = true;
}
else if (State == State.Configure)
{
Bollinger1 = Bollinger(2, 14);
Bollinger1.Plots[0].Brush = Brushes.SaddleBrown;
Bollinger1.Plots[1].Brush = Brushes.SaddleBrown;
Bollinger1.Plots[2].Brush = Brushes.SaddleBrown;
AddChartIndicator(Bollinger1);
}
}

protected override void OnBarUpdate()
{
if (CurrentBars[0] < 1)
return;

// Set 1
if ((GetCurrentAsk() > Bollinger1.Upper[0])
&& (Time[0].TimeOfDay >= new TimeSpan(7, 0, 0))
&& (Time[0].TimeOfDay < new TimeSpan(14, 30, 0)))
{
EnterShort(Convert.ToInt32(DefaultQuantity), "");
}
// Set 2
if ((GetCurrentBid() < Bollinger1.Lower[0])
&& (Time[0].TimeOfDay >= new TimeSpan(7, 0, 0))
&& (Time[0].TimeOfDay < new TimeSpan(14, 30, 0)))
{
EnterLong(Convert.ToInt32(DefaultQuantity), "");
}
// Set 3
if ((Position.MarketPosition == MarketPosition.Short)
&& (GetCurrentBid() < Bollinger1.Lower[0])
&& (Time[0].TimeOfDay >= new TimeSpan(7, 0, 0))
&& (Time[0].TimeOfDay < new TimeSpan(14, 30, 0)))
{
ExitShort(Convert.ToInt32(DefaultQuantity), "", "");
}
// Set 4
if ((Position.MarketPosition == MarketPosition.Long)
&& (GetCurrentAsk() > Bollinger1.Upper[0])
&& (Time[0].TimeOfDay >= new TimeSpan(7, 0, 0))
&& (Time[0].TimeOfDay < new TimeSpan(14, 30, 0)))
{
ExitLong(Convert.ToInt32(DefaultQuantity), "", "");
}

}
}
}

Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Increase in trading performance by 75%
The Elite Circle
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
Exit Strategy
NinjaTrader
How to apply profiles
Traders Hideout
Trade idea based off three indicators.
Traders Hideout
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Just another trading journal: PA, Wyckoff & Trends
36 thanks
Spoo-nalysis ES e-mini futures S&P 500
27 thanks
Tao te Trade: way of the WLD
24 thanks
Bigger Wins or Fewer Losses?
20 thanks
GFIs1 1 DAX trade per day journal
16 thanks
  #3 (permalink)
 
shodson's Avatar
 shodson 
OC, California, USA
Quantoholic
 
Experience: Advanced
Platform: IB/TWS, NinjaTrader, ToS
Broker: IB, ToS, Kinetick
Trading: stocks, options, futures, VIX
Posts: 1,976 since Jun 2009
Thanks Given: 533
Thanks Received: 3,709


I never use bid and ask in a backtest. I just always look at the closing price.

I think you would have to do Market Replay to test bid/ask values.

Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #4 (permalink)
 alecsaccount 
San Diego, California
 
Experience: Beginner
Platform: NinjaTrader 8
Trading: Emini ES, EUR/USD
Posts: 65 since Dec 2016
Thanks Given: 229
Thanks Received: 13


shodson View Post
I never use bid and ask in a backtest. I just always look at the closing price.

I think you would have to do Market Replay to test bid/ask values.

Not sure why I never tried that, but I'm running a backtest right now to see if that was it. I also tried it with an SMA set to 1, and had the same results... wouldn't that have the same effect as using the closing price?

Edit: backtest came back with the exact same results.

Started this thread Reply With Quote
  #5 (permalink)
 
shodson's Avatar
 shodson 
OC, California, USA
Quantoholic
 
Experience: Advanced
Platform: IB/TWS, NinjaTrader, ToS
Broker: IB, ToS, Kinetick
Trading: stocks, options, futures, VIX
Posts: 1,976 since Jun 2009
Thanks Given: 533
Thanks Received: 3,709


alecsaccount View Post
Not sure why I never tried that, but I'm running a backtest right now to see if that was it. I also tried it with an SMA set to 1, and had the same results... wouldn't that have the same effect as using the closing price?

Edit: backtest came back with the exact same results.

Stop using GetCurrentBid() or GetCurrentAsk(), they won't work in a backtest, just use Close[0]. Also you don't need to check the time in every if, make it its own if statement

 
Code
protected override void OnBarUpdate()
{
	if (CurrentBars[0] < 14)    // this needs to more than 1, the bollinger alone needs 14 bars
		return;

	if (Time[0].TimeOfDay >= new TimeSpan(7, 0, 0) && Time[0].TimeOfDay < new TimeSpan(14, 30, 0))
	{
		if (Close[0] > Bollinger1.Upper[0])
			EnterShort(Convert.ToInt32(DefaultQuantity), "");
		else if (Close[0] < Bollinger1.Lower[0])
			EnterLong(Convert.ToInt32(DefaultQuantity), "");
		else if (Position.MarketPosition == MarketPosition.Short && Close[0] < Bollinger1.Lower[0])
			ExitShort(Convert.ToInt32(DefaultQuantity), "", "");
		else if (Position.MarketPosition == MarketPosition.Long && Close[0] > Bollinger1.Upper[0])
			ExitLong(Convert.ToInt32(DefaultQuantity), "", "");
	}
}

Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #6 (permalink)
 alecsaccount 
San Diego, California
 
Experience: Beginner
Platform: NinjaTrader 8
Trading: Emini ES, EUR/USD
Posts: 65 since Dec 2016
Thanks Given: 229
Thanks Received: 13


shodson View Post
Stop using GetCurrentBid() or GetCurrentAsk(), they won't work in a backtest, just use Close[0].

Thanks, I will from now on.. But I still have the same problem; the backtest with Close[0] was identical to the GetCurrentBid() / GetCurrentAsk() backtest. Still missing exits. Any other ideas why it's not working?


 
Code
protected override void OnBarUpdate()
{
	if (CurrentBars[0] < 14)    // this needs to more than 1, the bollinger alone needs 14 bars
		return;

	if (Time[0].TimeOfDay >= new TimeSpan(7, 0, 0) && Time[0].TimeOfDay < new TimeSpan(14, 30, 0))
	{
		if (Close[0] > Bollinger1.Upper[0])
			EnterShort(Convert.ToInt32(DefaultQuantity), "");
		else if (Close[0] < Bollinger1.Lower[0])
			EnterLong(Convert.ToInt32(DefaultQuantity), "");
		else if (Position.MarketPosition == MarketPosition.Short && Close[0] < Bollinger1.Lower[0])
			ExitShort(Convert.ToInt32(DefaultQuantity), "", "");
		else if (Position.MarketPosition == MarketPosition.Long && Close[0] > Bollinger1.Upper[0])
			ExitLong(Convert.ToInt32(DefaultQuantity), "", "");
	}
}
Wow, thanks haha.. I'm an idiot for copying it to 4 sets.

Started this thread Reply With Quote
  #7 (permalink)
 
trendwaves's Avatar
 trendwaves 
Florida
Legendary Market Wizard
 
Experience: Advanced
Platform: NinjaTrader 8
Trading: ES, NQ, CL
Posts: 703 since Dec 2012
Thanks Given: 2,898
Thanks Received: 2,525

 
Code
protected override void OnBarUpdate()
{
	if (CurrentBars[0] < 14)    // this needs to more than 1, the bollinger alone needs 14 bars
		return;

	if ((Position.MarketPosition == MarketPosition.Flat) &&
	((Time[0].TimeOfDay < new TimeSpan(7, 0, 0)) || (Time[0].TimeOfDay >= new TimeSpan(14, 30, 0))))
		return;

	if (Position.MarketPosition == MarketPosition.Flat)
	{
		if (Close[0] > Bollinger1.Upper[0])
			EnterShort();
		else if (Close[0] < Bollinger1.Lower[0])
			EnterLong();
	}
	else if ((Position.MarketPosition == MarketPosition.Short) && (Close[0] < Bollinger1.Lower[0]))
		ExitShort();
	else if ((Position.MarketPosition == MarketPosition.Long) && (Close[0] > Bollinger1.Upper[0]))
		ExitLong();
}

Be Patient and Trade Smart
Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #8 (permalink)
 alecsaccount 
San Diego, California
 
Experience: Beginner
Platform: NinjaTrader 8
Trading: Emini ES, EUR/USD
Posts: 65 since Dec 2016
Thanks Given: 229
Thanks Received: 13


trendwaves View Post
 
Code
protected override void OnBarUpdate()
{
	if (CurrentBars[0] < 14)    // this needs to more than 1, the bollinger alone needs 14 bars
		return;

	if ((Position.MarketPosition == MarketPosition.Flat) &&
	((Time[0].TimeOfDay < new TimeSpan(7, 0, 0)) || (Time[0].TimeOfDay >= new TimeSpan(14, 30, 0))))
		return;

	if (Position.MarketPosition == MarketPosition.Flat)
	{
		if (Close[0] > Bollinger1.Upper[0])
			EnterShort();
		else if (Close[0] < Bollinger1.Lower[0])
			EnterLong();
	}
	else if ((Position.MarketPosition == MarketPosition.Short) && (Close[0] < Bollinger1.Lower[0]))
		ExitShort();
	else if ((Position.MarketPosition == MarketPosition.Long) && (Close[0] > Bollinger1.Upper[0]))
		ExitLong();
}

Uhh, so like I said, 'absolute beginner'... You guys are walking me through literally my first time coding.

I'm getting an "Extern alias declarations, using clauses, assembly.." error 2 lines after the "}" at the end of what you posted?

(I'm in way over my head and it's awesome)

Nevermind, I just had to take out a "}"!

Just compiled and backtested, and you fixed it!!! I fucking love you, man!

Also, thanks Shodson!

Started this thread Reply With Quote
Thanked by:
  #9 (permalink)
 alecsaccount 
San Diego, California
 
Experience: Beginner
Platform: NinjaTrader 8
Trading: Emini ES, EUR/USD
Posts: 65 since Dec 2016
Thanks Given: 229
Thanks Received: 13


trendwaves View Post
 
Code
protected override void OnBarUpdate()
{
	if (CurrentBars[0] < 14)    // this needs to more than 1, the bollinger alone needs 14 bars
		return;

	if ((Position.MarketPosition == MarketPosition.Flat) &&
	((Time[0].TimeOfDay < new TimeSpan(7, 0, 0)) || (Time[0].TimeOfDay >= new TimeSpan(14, 30, 0))))
		return;

	if (Position.MarketPosition == MarketPosition.Flat)
	{
		if (Close[0] > Bollinger1.Upper[0])
			EnterShort();
		else if (Close[0] < Bollinger1.Lower[0])
			EnterLong();
	}
	else if ((Position.MarketPosition == MarketPosition.Short) && (Close[0] < Bollinger1.Lower[0]))
		ExitShort();
	else if ((Position.MarketPosition == MarketPosition.Long) && (Close[0] > Bollinger1.Upper[0]))
		ExitLong();
}

Huh.. Okay, wait, so it mostly fixed it, but (and this isn't that much of an issue) it does veery occasionally miss 1 exit.

It was always like 4 or 5 before, and quite often, so this is a massive improvement and I'd be very happy with it even if I can't make it perfect, but..

ah, whatever. I'm so happy I can move on to the rest of the strategy now!

Started this thread Reply With Quote
  #10 (permalink)
 
trendwaves's Avatar
 trendwaves 
Florida
Legendary Market Wizard
 
Experience: Advanced
Platform: NinjaTrader 8
Trading: ES, NQ, CL
Posts: 703 since Dec 2012
Thanks Given: 2,898
Thanks Received: 2,525


Can you please post a chart with the entry and exit markers and text enabled so we can see what is happening on your chart with the missed exits.

Be Patient and Trade Smart
Visit my NexusFi Trade Journal Reply With Quote
Thanked by:




Last Updated on December 16, 2016


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts