NexusFi: Find Your Edge


Home Menu

 





highest bar close occuring within the last X number of bars . . . .


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one Loukas with 16 posts (0 thanks)
    2. looks_two forrestang with 12 posts (8 thanks)
    3. looks_3 jmejedi with 6 posts (0 thanks)
    4. looks_4 Fat Tails with 2 posts (1 thanks)
    1. trending_up 23,403 views
    2. thumb_up 10 thanks given
    3. group 4 followers
    1. forum 37 posts
    2. attach_file 5 attachments




 
Search this Thread

highest bar close occuring within the last X number of bars . . . .

  #31 (permalink)
 
forrestang's Avatar
 forrestang 
Chicago IL
 
Experience: None
Platform: Ninja, MT4, Matlab
Broker: CQG, AMP, MB, DTN
Trading: E/U, G/U
Posts: 1,329 since Jun 2010
Thanks Given: 354
Thanks Received: 1,047


Loukas View Post
I am running this in strategy analyzer, I think your are speaking about Order Properies which has 3 options 1. By default quantity 2. by strategy 3. by account size. I selected By Default quantity and then at the default quuantity box I entered 2 but it doesnt execute more trades.

No not that one.

Go to THIS link, and look under the tab "Understanding Backtest Properties."

It should explain everything you need to understand the order handling. Pay attention to "order handling."

Reply With Quote
Thanked by:

Can you help answer these questions
from other members on NexusFi?
Better Renko Gaps
The Elite Circle
Increase in trading performance by 75%
The Elite Circle
REcommedations for programming help
Sierra Chart
PowerLanguage & EasyLanguage. How to get the platfor …
EasyLanguage Programming
ZombieSqueeze
Platforms and Indicators
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Just another trading journal: PA, Wyckoff & Trends
30 thanks
Spoo-nalysis ES e-mini futures S&P 500
28 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
  #32 (permalink)
Loukas
London
 
Posts: 27 since Jun 2011
Thanks Given: 4
Thanks Received: 2

Hi Forrestang,
I have an issue this Stop Loss and take Profit Target and I want your thoughts on it. I have applied stop loss(SL) and take profit(TP) with CalculationMode.Percent and at OnBarUpdate() a dynamic SL when I am in a trade. The problem is arizing when I am running back test. I believe the worst loss could be iccure should be the ST at CalculationMode.Percent because is lower. However, I had losses and profits much more than my orders. How is this possible? a part of my code is below.

if (Position.MarketPosition== MarketPosition.Long)
{
if( Close[0] < EMA (30)[0] )
{
ExitLong();
Print(Time[0]+"Exit Long Close below EMA");
}

else if (Close[0]>= Position.AvgPrice+BreakEvenTarget * TickSize)

{
NewStopTarget = Position.AvgPrice + SetNewTarget * TickSize;
SetStopLoss(CalculationMode.Price, Position.AvgPrice);
Print(Time[0]+" Exit Long due to BreakEven Ticks");
}
}

if (Position.MarketPosition== MarketPosition.Short)
{
if( Close[0] > EMA (30)[0] )
{
ExitShort();
Print(Time[0]+" Exit Short because of Close cross above EMA");
}

else if (Close[0]<= Position.AvgPrice-BreakEvenTarget * TickSize)

{
NewStopTarget = Position.AvgPrice- SetNewTarget * TickSize;
SetStopLoss(CalculationMode.Price, NewStopTarget);
Print(Time[0]+" Exit Short due to BreakEven Ticks");
}
}

Reply With Quote
  #33 (permalink)
 
forrestang's Avatar
 forrestang 
Chicago IL
 
Experience: None
Platform: Ninja, MT4, Matlab
Broker: CQG, AMP, MB, DTN
Trading: E/U, G/U
Posts: 1,329 since Jun 2010
Thanks Given: 354
Thanks Received: 1,047


Not sure exactly what your error is based on your explanation. Here is a general structure of how you should program your strategy. This is just for a long. Please take note of all the comments in there.

Basically, you have some setup in the initialize block with setting your target and stop.

Next in your update block, you want to:
Check if flat, if so set your stoploss to your hard stop.

Next, I've added in the OPTION to use a Breakeven adjust stop, you see that there. If you don't need it, you can just delete that statement. Also done at this point while checking if we are long, you'll see there is an option there to EXIT immediately if some condition happens. For example if you wanted to put oscillator crossing below zero in there.

Lastly, we check if we are NOT flat, if we are IN a trade, then exit the loop and start over. But this is basically how you do it.

 
Code
        protected override void Initialize()
        {
            
		CalculateOnBarClose = true;
		EntryHandling       = EntryHandling.UniqueEntries;
		EntriesPerDirection = 1;
		ExitOnClose = true;
		ExitOnCloseSeconds = 30;
		TraceOrders = true;
		SetProfitTarget(CalculationMode.Ticks, profitTarget);	//Note replace with whatever variable you have[/COLOR]
		SetStopLoss(CalculationMode.Ticks, initialStop);		//Note replace with whatever variable you have
			
        }
		
	protected override void OnBarUpdate()
        {

		//Notice here we set the initial stop EACH time we execute the loop IF we are flat,
		//I.e. reinitializing at the very beginning of the OnBarUpdate
		if (Position.MarketPosition == MarketPosition.Flat)
		{
			SetStopLoss(CalculationMode.Ticks, initialStop);	//again replace with YOUR variable
		}
				
				
		//Now we check if we are long, if we are, then we want to initialize our stops
		if (Position.MarketPosition == MarketPosition.Long)
		{
			//Loukas, this first IF statement sets the stop loss to BE+2 tics if price moves
			//past a certain point of entry.  If you do not wish to havea BE adjustment, 
			//just delete it
			if (High[0] >= Position.AvgPrice + beMove*TickSize)	//beMove is the distance after entry before going to BE
				SetStopLoss( CalculationMode.Price, Position.AvgPrice + 2*TickSize); //Note we are changing the stoploss
					
			//This IF statement provides an emergency exit based on something. Say you have a condition
			//that you will want to exit a trade immediately if it occurs. Delete if you dont need
			if (Something happens ......)
				ExitLong("ExitLong1", "Long1");
		}
				
		//This IF statement says that if we are in a position, then exit the loop, b/c we don't need 
		//to execute the buy signal.
		if (Position.MarketPosition != MarketPosition.Flat) return;
				
		//Now, put your ENTRY SIGNAL @ the bottom of the OnBarUpdate.  This is the last thing you want to
		//do each time through.  Replace with your conditions for buy signal.
		if (Buy signal occurrs.....)
			EnterLong("Long1");
		
        }

Attached Thumbnails
Click image for larger version

Name:	Prime2011-09-23_231531.png
Views:	249
Size:	54.9 KB
ID:	50132  
Reply With Quote
Thanked by:
  #34 (permalink)
Loukas
London
 
Posts: 27 since Jun 2011
Thanks Given: 4
Thanks Received: 2

Many thanks forrestang, my losses have been reduced significanlty! I didnt know that it is so important to put at the beging of the of strategy the SL and TP orders. I will always following this structure. Once again thanks for your imput. I would like to ask you also why the following code execute the same number of trades if I have 3 or 5 condition ?

if ( EMA(5)[0]>EMA(40)[0]
// && EMA(90)[0]>EMA(100)[0]/// If I add those two conditions
// && RSI(14,3)[0]> 60////// the number of the executed trades stay the same.
&& Rising(ATR(Close,14))
&& Closes[1][0]>EMA(150)[0])/// Different time frame
{
EnterLong()
}

I had made the following changes and the code was working well but I cannot understand why I need to make this

if ( EMA(5)[0]>EMA(40)[0]
&& Rising(ATR(Close,14))
&& Closes[1][0]>EMA(150)[0])/// Different time frame
{
if ( EMA(90)[0]>EMA(100)[0]/
&& RSI(14,3)[0]> 60)
{
EnterLong()
}
}

Reply With Quote
  #35 (permalink)
 
forrestang's Avatar
 forrestang 
Chicago IL
 
Experience: None
Platform: Ninja, MT4, Matlab
Broker: CQG, AMP, MB, DTN
Trading: E/U, G/U
Posts: 1,329 since Jun 2010
Thanks Given: 354
Thanks Received: 1,047

Well if you are adding more conditions and getting the same number of trades, it's either because you have some formatting issues, which kinda is what that first code block looks like?

Or you have some conditions that are too highly correlated.

So the check is easy, just run the strategy either in a sim or backtest, put the indicators you are using on a chart, and check and see if your system is plotting the proper trade based on whatever indicators you are using. If need be isolate each condition and check again.

Reply With Quote
  #36 (permalink)
Loukas
London
 
Posts: 27 since Jun 2011
Thanks Given: 4
Thanks Received: 2

The code "if ( EMA(5)[0]>EMA(40)[0] && EMA(90)[0]>EMA(100)[0]) "execute the same number of trades as the code if ( EMA(5)[0]>EMA(40)[0]
&& EMA(90)[0]>EMA(100)[0]
&& RSI(14,3)[0]> 60
&& Rising(ATR(Close,14))
&& Closes[1][0]>EMA(BarsArray[1],10)[0])
{
EnterLong(DefaultQuantity);
Print(Time[0] +" Enter Long");
}
However, when I am adding an extra if statment is working well
if ( EMA(5)[0]>EMA(40)[0]
&& EMA(90)[0]>EMA(100)[0])
// && RSI(14,3)[0]> 60
// && Rising(ATR(Close,14))
// && Closes[1][0]>EMA(BarsArray[1],10)[0])
{
if ( RSI(14,3)[0]> 60
&& Rising(ATR(Close,14))
&& Closes[1][0]>EMA(BarsArray[1],10)[0])

Print(Time[0] + " Long Momentum");

{
EnterLong(DefaultQuantity);
Print(Time[0] +" Enter Long");
}
}
Why the first code stracture is not working as the second one?

Reply With Quote
  #37 (permalink)
 
forrestang's Avatar
 forrestang 
Chicago IL
 
Experience: None
Platform: Ninja, MT4, Matlab
Broker: CQG, AMP, MB, DTN
Trading: E/U, G/U
Posts: 1,329 since Jun 2010
Thanks Given: 354
Thanks Received: 1,047

I cant tell what goes with what. Use the 'code' tags or take a screenshot of your editor or something.

Reply With Quote
Thanked by:
  #38 (permalink)
 Spikeygirl 
Orlando, Florida USA
 
Experience: Master
Platform: Think or Swim [How utterly appropriate]
Trading: es, eur/usd
Posts: 18 since May 2012
Thanks Given: 4
Thanks Received: 12

I just wanted to say thank you on behalf of most of us traders who glean the little nuggets of gold dust
from folks like you who assist those traders in need. You did a STELLAR job assisting this guy and folks
like us, iitting on the cloud want to send you a big ray of sunshine!

Reply With Quote
Thanked by:




Last Updated on October 6, 2014


© 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