NinjaTrader Support Forum  
X

Attention!

This website will be down for maintenance from Friday May 24th at 6PM MDT until Sunday May 26th at 12PM MDT. We apologize for the inconvenience. If you need assistance during this time, please email sales@ninjatrader.com


Go Back   NinjaTrader Support Forum > Application Technical Support > Automated Trading

Automated Trading Support for automated trading systems using NinjaScript. Support for our ATI (Automated Trading Interface) used to link an external application such as TradeStation and eSignal to NinjaTrader.

Reply
 
Thread Tools Display Modes
Old 12-13-2011, 02:37 AM   #1
tonynt
Senior Member
 
Join Date: Jun 2009
Posts: 740
Thanks: 19
Thanked 14 times in 9 posts
Default entry - condition

Hello,

I have a question - maybe this is ridiculous - I canīt find the solution:

How can I have a if(...) {....;} when the if(...) should be the entry of a trade? Of course with conditions everything is clear but within a strategy I need to have a variable or tradecounter++ only when the entry is done (and not referring to the conditions for the entry)

Thanks
Tony
tonynt is offline  
Reply With Quote
Old 12-13-2011, 04:39 AM   #2
bukkan
Senior Member
 
Join Date: Feb 2009
Posts: 285
Thanks: 2
Thanked 52 times in 41 posts
Default

tonynt,
you can refer to this example for your need http://www.ninjatrader.com/support/f...ad.php?t=19182

alternatively you can assign the value of tradeCounter in OnExecution or OnPositionUpdate when the entry gets Filled like
Code:
protected override void OnExecution(IExecution execution)
{
	if (execution.Order.OrderState == OrderState.Filled)
		tradecounter++;
}
bukkan is offline  
Reply With Quote
The following user says thank you to bukkan for this post:
Old 12-13-2011, 03:17 PM   #3
tonynt
Senior Member
 
Join Date: Jun 2009
Posts: 740
Thanks: 19
Thanked 14 times in 9 posts
Default

Bukkan,

thank you for your reply. The sample with tradelimiter I know and this is how I do now. But this doesnīt work when there are 2 condition-setups true (and including "flat") then because of the condition true it is counted++ both setups but only one entry.

Thank you for your alternatively "OnExecution". This is logical but probably thereīs an error on my end because when I do so there is only 1 entry while a certain variable is true (eg HMA55 falling) and after initializing the variable with HMA55 rising then againg there is only one entry while HMA55 falling - no matter which value I set for variable maxTrades. (???)

Thanks
Tony
tonynt is offline  
Reply With Quote
Old 12-13-2011, 08:05 PM   #4
bukkan
Senior Member
 
Join Date: Feb 2009
Posts: 285
Thanks: 2
Thanked 52 times in 41 posts
Default

logic is a logic. if logic is right and coded right it will work.

what exactly you are trying to do and if possible pls post the code.
bukkan is offline  
Reply With Quote
Old 12-13-2011, 10:55 PM   #5
tonynt
Senior Member
 
Join Date: Jun 2009
Posts: 740
Thanks: 19
Thanked 14 times in 9 posts
Default

Thank you for your reply,

the interesting thing is that when I set maxtrades to 6 I have less # of trades than with maxtrades 2

Here is the basic code (without ATM....)
public class basic1 : Strategy
{
#region Variables
double stop1 = 0;
double stop2 = 0;
double stop3 = 0;
double stop4 = 0;


private int tradeCounter = 0;
private int maxTrades = 6;


#endregion

/// <summary>
/// This method is used to configure the strategy and is called once before any strategy method is called.
/// </summary>
protected override void Initialize()
{
EntriesPerDirection = 1;
EntryHandling = EntryHandling.UniqueEntries;


Add(PeriodType.Range,210);//1
Add(PeriodType.Range,130);//2
Add(PeriodType.Range,80);//3
Add(PeriodType.Range,70);//4
Add(PeriodType.Range,50);//5
Add(PeriodType.Range,40);//6
Add(PeriodType.Range,30);//7
Add(PeriodType.Minute,1);//8

CalculateOnBarClose = true;
}

/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{if (BarsInProgress !=0)
return;


stop1=DonchianChannel(BarsArray[5],3).Upper[0]+30*TickSize;
stop2=DonchianChannel(BarsArray[5],3).Upper[0]+30*TickSize;

if (Position.MarketPosition == MarketPosition.Flat)
{ SetStopLoss("S1A", CalculationMode.Ticks, 80, false);
SetStopLoss("S2A", CalculationMode.Ticks, 80, false);

Print("setting initial stops at bar: " + Time[0].ToString());

Variable8 = 0;
}

// If a long position is open, allow for stop loss modification to breakeven
else if (Position.MarketPosition == MarketPosition.Short)
{
// Once the price is greater than entry price + 20 ticks, set stop loss to breakeven
if (GetCurrentBid(0) < Position.AvgPrice - 80 * TickSize)
//&& step1)
{
SetStopLoss("S1A",CalculationMode.Price, stop1,false);
SetStopLoss("S2A",CalculationMode.Price, stop1,false);
//SetStopLoss("S3",CalculationMode.Price, stop1,false);
//SetStopLoss("S4",CalculationMode.Price, stop2,false);
//step1 = false;
DrawDot("step1" + CurrentBar, true, 0, Position.AvgPrice - 70 * TickSize, Color.Blue);
}

}

if (Falling(HMA(BarsArray[3],55)))
{Variable1 = 1;}

if(Variable1==1)
{BackColor=Color.MistyRose;}

if (Rising(HMA(BarsArray[3],55)))

{Variable1 = 0;
tradeCounter = 0;
}



if (tradeCounter < maxTrades
&& Position.MarketPosition == MarketPosition.Flat
&& Variable1 == 1
&& (Highs[5][0] > KAMA(BarsArray[5],2, 10, 30)[0])
&& (Highs[8][0] > KAMA(BarsArray[8],2, 10, 30)[0])
&& Close[1] > Open[1]
&& Close[0] < Open[0])

{//tradeCounter++;
EnterShort(2000, "S1A");
EnterShort(2000, "S2A");

Variable8 = 1;
}

if (tradeCounter < maxTrades
&& Position.MarketPosition == MarketPosition.Flat
&& Variable1 == 1
&& (Highs[3][0] > KAMA(BarsArray[3],2, 10, 30)[0])
&& (Highs[8][0] > KAMA(BarsArray[8],2, 10, 30)[0])
&& Close[1] > Open[1]
&& Close[0] < Open[0])

{//tradeCounter++;
EnterShort(2000, "S1A");
EnterShort(2000, "S2A");

Variable8 = 1;
}



if (GetCurrentAsk(0) < Position.AvgPrice - 50 * TickSize)
ExitShort(2000, "SX1","S1A");

if (GetCurrentAsk(0) < Position.AvgPrice - 80 * TickSize)
ExitShort(2000, "SX2","S2A");




if(Variable8==1)
{BackColor=Color.Silver;}


if(Variable9==1)
{BackColor=Color.White;}



}

protected override void OnExecution(IExecution execution)

{ if (execution.Order.OrderState == OrderState.Filled)
tradeCounter++;
}





#region Properties

#endregion
}
}



Thanks
Tony
tonynt is offline  
Reply With Quote
Old 12-14-2011, 02:56 AM   #6
bukkan
Senior Member
 
Join Date: Feb 2009
Posts: 285
Thanks: 2
Thanked 52 times in 41 posts
Default

tonynt,
you got to add filter to check its the entry orders only that get counted.

like


{ if (execution.Order.OrderState == OrderState.Filled && (execution.Order.Name == "S1A" )) // || execution.Order.Name == "S2A")) //S2A and S1A origanate from same logic so checking one will be sufficient
tradeCounter++;
}


also you are resetting the tradeCounter via code

if (Rising(HMA(BarsArray[3],55)))

{Variable1 = 0;
tradeCounter = 0;
}

are you sure you want to do that.
bukkan is offline  
Reply With Quote
The following user says thank you to bukkan for this post:
Old 12-14-2011, 03:25 AM   #7
tonynt
Senior Member
 
Join Date: Jun 2009
Posts: 740
Thanks: 19
Thanked 14 times in 9 posts
Default

Hello bukkan,

thank you for your support! I try to check what I have done wrong.

Why do you mean maybe not to reset tradecounter when the HMA is going other direction? (for your information I do longs from one computer and shorts from other computer). Should I do other way? My idea - in these choppy markets - is to take eg 1 or 2 shorts when eg HMA in higher timeframe is falling, then resetting when HMA rising. Next short when HMA falling again. You think I should reset tradecounter other way?

Thanks in advance for your hint.

Thanks
Tony

Quote:
Originally Posted by bukkan View Post
tonynt,
you got to add filter to check its the entry orders only that get counted.

like


{ if (execution.Order.OrderState == OrderState.Filled && (execution.Order.Name == "S1A" )) // || execution.Order.Name == "S2A")) //S2A and S1A origanate from same logic so checking one will be sufficient
tradeCounter++;
}


also you are resetting the tradeCounter via code

if (Rising(HMA(BarsArray[3],55)))

{Variable1 = 0;
tradeCounter = 0;
}

are you sure you want to do that.
tonynt is offline  
Reply With Quote
Old 12-14-2011, 03:56 AM   #8
bukkan
Senior Member
 
Join Date: Feb 2009
Posts: 285
Thanks: 2
Thanked 52 times in 41 posts
Default

i didnt have the exact idea of what you were doing, and thus i mentioned that. so currently your code works like x trades (as per maxtrades) per slope. if thats you want then its fine.

just check the OnExecution as i mentioned.
bukkan is offline  
Reply With Quote
Old 12-14-2011, 03:58 AM   #9
tonynt
Senior Member
 
Join Date: Jun 2009
Posts: 740
Thanks: 19
Thanked 14 times in 9 posts
Default

THANKS!

Have a great day
Tony

Quote:
Originally Posted by bukkan View Post
i didnt have the exact idea of what you were doing, and thus i mentioned that. so currently your code works like x trades (as per maxtrades) per slope. if thats you want then its fine.

just check the OnExecution as i mentioned.
tonynt is offline  
Reply With Quote
Old 12-15-2011, 04:10 PM   #10
tonynt
Senior Member
 
Join Date: Jun 2009
Posts: 740
Thanks: 19
Thanked 14 times in 9 posts
Default

bukkan,

thanks again for your advice. But now appear error-message:

"Error on calling OnExecution method for strategy.... Object reference not set to an instance of an object."

Please can you tell me where to add for checking null references - I have no idea how this works.

Thanks and best regards
Tony

Quote:
Originally Posted by bukkan View Post
tonynt,
you got to add filter to check its the entry orders only that get counted.

like


{ if (execution.Order.OrderState == OrderState.Filled && (execution.Order.Name == "S1A" )) // || execution.Order.Name == "S2A")) //S2A and S1A origanate from same logic so checking one will be sufficient
tradeCounter++;
}


also you are resetting the tradeCounter via code

if (Rising(HMA(BarsArray[3],55)))

{Variable1 = 0;
tradeCounter = 0;
}

are you sure you want to do that.
tonynt is offline  
Reply With Quote
Old 12-15-2011, 04:34 PM   #11
NinjaTrader_RyanM
NinjaTrader Customer Service
 
NinjaTrader_RyanM's Avatar
 
Join Date: Sep 2009
Location: Denver, CO
Posts: 8,117
Thanks: 249
Thanked 418 times in 415 posts
Default

Hi Tony,

In the future please do not use multiple threads to post the same question.
NinjaTrader_RyanM is offline  
Reply With Quote
Old 12-15-2011, 08:35 PM   #12
bukkan
Senior Member
 
Join Date: Feb 2009
Posts: 285
Thanks: 2
Thanked 52 times in 41 posts
Default

Quote:
Originally Posted by tonynt View Post
bukkan,

thanks again for your advice. But now appear error-message:

"Error on calling OnExecution method for strategy.... Object reference not set to an instance of an object."

Please can you tell me where to add for checking null references - I have no idea how this works.

Thanks and best regards
Tony
most probably it is the system generated orders for which you are getting the issues. check for a null check for the same

if (execution.Order == null )
{
//do something
}
else
{
//do something
}
bukkan is offline  
Reply With Quote
The following user says thank you to bukkan for this post:
Old 12-15-2011, 11:51 PM   #13
tonynt
Senior Member
 
Join Date: Jun 2009
Posts: 740
Thanks: 19
Thanked 14 times in 9 posts
Default

Thank you!

Quote:
Originally Posted by bukkan View Post
most probably it is the system generated orders for which you are getting the issues. check for a null check for the same

if (execution.Order == null )
{
//do something
}
else
{
//do something
}
tonynt is offline  
Reply With Quote
Old 12-17-2011, 04:11 AM   #14
tonynt
Senior Member
 
Join Date: Jun 2009
Posts: 740
Thanks: 19
Thanked 14 times in 9 posts
Default

Hi Ryan,

if you would give me an answer to my question (the links I can find on my own), I need not trying to contact the supporter I received already accurate answers.

Quote:
Originally Posted by NinjaTrader_RyanM View Post
Hi Tony,

In the future please do not use multiple threads to post the same question.
tonynt is offline  
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
condition based on entry signal price? ch9090 Strategy Development 3 09-09-2011 04:25 PM
Recall indicator value from entry bar for exit condition? Scotty.Trump General Programming 2 06-25-2010 03:56 PM
Order entry based on open condition in EOD backtest whipsaw Strategy Development 4 02-16-2010 08:52 AM
entry condition kaywai Strategy Development 2 12-14-2009 07:17 AM
Entry date shows as the next day of actual entry TintuLal Strategy Analyzer 6 09-25-2009 02:26 PM


All times are GMT -6. The time now is 02:55 AM.