![]() |
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
|
|||||||
| 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. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Senior Member
Join Date: Jun 2009
Posts: 740
Thanks: 19
Thanked 14 times in 9 posts
|
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 |
|
|
|
|
|
#2 |
|
Senior Member
Join Date: Feb 2009
Posts: 285
Thanks: 2
Thanked 52 times in 41 posts
|
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++;
}
|
|
|
|
|
The following user says thank you to bukkan for this post: |
|
|
|
#3 |
|
Senior Member
Join Date: Jun 2009
Posts: 740
Thanks: 19
Thanked 14 times in 9 posts
|
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 |
|
|
|
|
|
#4 |
|
Senior Member
Join Date: Feb 2009
Posts: 285
Thanks: 2
Thanked 52 times in 41 posts
|
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. |
|
|
|
|
|
#5 |
|
Senior Member
Join Date: Jun 2009
Posts: 740
Thanks: 19
Thanked 14 times in 9 posts
|
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 |
|
|
|
|
|
#6 |
|
Senior Member
Join Date: Feb 2009
Posts: 285
Thanks: 2
Thanked 52 times in 41 posts
|
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. |
|
|
|
|
The following user says thank you to bukkan for this post: |
|
|
|
#7 | |
|
Senior Member
Join Date: Jun 2009
Posts: 740
Thanks: 19
Thanked 14 times in 9 posts
|
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:
|
|
|
|
|
|
|
#8 |
|
Senior Member
Join Date: Feb 2009
Posts: 285
Thanks: 2
Thanked 52 times in 41 posts
|
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. |
|
|
|
|
|
#9 |
|
Senior Member
Join Date: Jun 2009
Posts: 740
Thanks: 19
Thanked 14 times in 9 posts
|
|
|
|
|
|
|
#10 | |
|
Senior Member
Join Date: Jun 2009
Posts: 740
Thanks: 19
Thanked 14 times in 9 posts
|
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:
|
|
|
|
|
|
|
#11 |
|
NinjaTrader Customer Service
Join Date: Sep 2009
Location: Denver, CO
Posts: 8,117
Thanks: 249
Thanked 418 times in 415 posts
|
Hi Tony,
In the future please do not use multiple threads to post the same question.
Ryan M
NinjaTrader Customer Service |
|
|
|
|
|
#12 | |
|
Senior Member
Join Date: Feb 2009
Posts: 285
Thanks: 2
Thanked 52 times in 41 posts
|
Quote:
if (execution.Order == null ) { //do something } else { //do something } |
|
|
|
|
|
The following user says thank you to bukkan for this post: |
|
|
|
#13 |
|
Senior Member
Join Date: Jun 2009
Posts: 740
Thanks: 19
Thanked 14 times in 9 posts
|
|
|
|
|
|
|
#14 |
|
Senior Member
Join Date: Jun 2009
Posts: 740
Thanks: 19
Thanked 14 times in 9 posts
|
|
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
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 |