SetProfitTarget("Entry1", CalculationMode.Price, (1*Profitfactor) * (ATR(20)[COLOR=Red][B][0][/B][/COLOR]));
SetProfitTarget("Entry2", CalculationMode.Price, (2*Profitfactor) * (ATR(20)[COLOR=Red][B][0][/B][/COLOR]));
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Beginner Advice Needed
Collapse
X
-
Your 2 SetProfitTarget() statements are attempting to access barObjects that do not yet exist.Originally posted by pswarts View Post
Initialize the SetProfitTarget() statements to static values, then set them in OnStartUp().Code:
-
This is the message ,seems like a problem with Initialize,I dont know what that means or how to fix it though:
07/12/2011 12:15 AMStrategyFailed to call method 'Initialize' for strategy 'TestingEntriesProgressive1/eb21fcb29fee4622a90312ecc6dd760f': Object reference not set to an instance of an object.Last edited by pswarts; 12-07-2011, 12:58 PM.
Leave a comment:
-
pswarts, are there any error messages in the Control Center rightmost Log tab as you attempt to run your script?
Leave a comment:
-
cont'd
// Entry Conditions
if (EMA(Ema)[0] > EMA(Ema)[1]
&& SMA(Sma)[0] > SMA(Sma)[1]
&& EMA(Ema)[0] > SMA(Sma)[0]
&& Close[0] > Open[0])
{
EnterLong(1, "Entry1");
EnterLong(1, "Entry2");
EnterLong(1, "Entry3");
}
}
Leave a comment:
-
cont'd
// If a long position is open, allow for stop loss modification
elseif (Position.MarketPosition == MarketPosition.Long)
{
// Once 1st Profittarget attained, set stop loss to breakeven
if (High[0] >= Position.AvgPrice + ((1*Profitfactor) * (ATR(20)[0]))
&&High[0] <= Position.AvgPrice + ((2*Profitfactor) * (ATR(20)[0]))
&&Close[0] > Position.AvgPrice )
{
SetStopLoss("Entry2", CalculationMode.Price, Position.AvgPrice, true);
SetStopLoss("Entry3", CalculationMode.Price, Position.AvgPrice, true);
}
// Once 2st Profittarget attained, set stop loss to trail by Low of (x)bars
elseif (High[0] > Position.AvgPrice + ((2*Profitfactor) * (ATR(20)[0]))
&&MIN(Low ,3)[0] > Position.AvgPrice)
{
SetStopLoss("Entry3", CalculationMode.Price, MIN(Low, 3)[0], true);
}
}
Leave a comment:
-
cont'd:
///<summary>
/// Called on each bar update event (incoming tick)
///</summary>
protectedoverridevoid OnBarUpdate()
{
// Resets the stop loss to the original value when all positions are closed
if (Position.MarketPosition == MarketPosition.Flat)
{
SetStopLoss("Entry1", CalculationMode.Ticks, Stopfactor*100, false);
SetStopLoss("Entry2", CalculationMode.Ticks, Stopfactor*100, false);
SetStopLoss("Entry3", CalculationMode.Ticks, Stopfactor*100, false);
}
Leave a comment:
-
here is my code:
#region Variables
privateint ema = 5;
privateint sma = 40;
privateint profitfactor = 1;
privateint stopfactor = 1;
#endregion
///<summary>
/// This method is used to configure the strategy and is called once before any strategy method is called.
///</summary>
protectedoverridevoid Initialize()
{
Add(EMA(Ema));
Add(SMA(Sma));
Add(ATR(20));
SetProfitTarget("Entry1", CalculationMode.Price, (1*Profitfactor) * (ATR(20)[0]));
SetProfitTarget("Entry2", CalculationMode.Price, (2*Profitfactor) * (ATR(20)[0]));
CalculateOnBarClose = true;
}
Leave a comment:
-
Oh man this is getting frustrating, I finished writing my code, compiled it and NT7 compiled without a problem but I cannot get the strategy to run or backtest, when I try and backtest it,Strategy Analyser shows it in its list of strategies but when I pick it from the list it does not load(the prev strategy's setting stays in the Parameter box) when I try and load other strategies it loads just fine. I tried to run it on simulated datafeed and the strategy loads on the charts but nothing happens, the Entries does not trigger.WHY??!!
Please help!!!
Leave a comment:
-
To trail at the lowest low of the last 3 bars you would use SetStopLoss. This statement should do it:
SetStopLoss(CalculationMode.Price, MIN(Low, 3)[0]);
Leave a comment:
-
If I want to trail my stop at the lowest low of the last three bars do I use the SetstopLoss or SetTrailStop(that would be my 2nd else if statement in the code below) and is that the correct code to give me the lowest low of last three bars ? I been over the help manual several times but cannot grasp the whole Lows(),Lowestbar(),Low[] thing or how to properly combine these.
elseif (Position.MarketPosition == MarketPosition.Long)
{
// Once the price is equal or greater than 1st Profittarget, set stop loss to breakeven
if (High[0] >= Position.AvgPrice + ((1*Profitfactor) * (ATR(20)[0]))
&&High[0] <= Position.AvgPrice + ((2*Profitfactor) * (ATR(20)[0]))
&&Close[0] > Position.AvgPrice )
{
SetStopLoss("Entry2", CalculationMode.Price, Position.AvgPrice, true);
SetStopLoss("Entry3", CalculationMode.Price, Position.AvgPrice, true);
}
// Once the price is equal or greater than 2st Profittarget, and has not closed lower than breakeven or lower than the lowest low of the prev three bars, set stop loss to trail by low of last three bars
elseif (High[0] > Position.AvgPrice + ((2*Profitfactor) * (ATR(20)[0]))
&&MIN(Low ,3)[0] < MIN(Low ,0)[0])
{
SetStopLoss("Entry3", CalculationMode.Ticks, Low[3], true);
}
Last edited by pswarts; 12-06-2011, 04:31 PM.
Leave a comment:
-
The nice thing about set statements is they can be automatically submitted upon entry execution, and placed in relation to your entry price with mode ticks or percent.
To get the same timing and placement with Exit methods requires advanced handling and familiarity with IOrder properties.
If you have a working exit order and the position associated with it is closed: The internal strategy management will automatically close working exit orders. You do not have to send CancelOrder to manually cancel it.
Else if branch would be used to provide exclusivity to your conditions. When using it, both the if and else if statements would never be true on the same bar. It's a way of preventing it from doing competing actions, but there are also internal mechanisms (in backtest) that wouldn't allow, a long and short position on the same bar.
Leave a comment:
-
Thanks, makes sense.
Few more questions: Will it be better to rather use ExitLong()for profits and ExitLongStop()for Stops and get rid of the Set commands altogether? If I do that and use EnterLongStop() with Unique Entries, with an Exit Order both below and above the Entry but linked to the entry with the "fromEntrySignal" will one Exit order cancel the other or will I have to bring in a CancelOrder(). According to the Internal Order handling section it looks like it will be ignored (not put me in a short position) but I'm not sure if it actually gets cancelled because if not then on the next setup it will still be in effect ,or will the next steup that gets triggered reset the old Exits to the new setup values.
Also if I use If Else() to branch my conditions do I still use the least restrictive conditions first, my logic says that if more than one conditions is true the strategy will basically try to flip the stop back and forth to the setting for the conditions with each bar update or is that automaticaly taken care of by Ninja's internal mechanisms?
Leave a comment:
-
If you use both the ExitLongStop() and SetStopLoss() for the same position, only one of these orders will be used and the other ignored. See here for more info and expand section on Internal Order Handling rules.
Set methods will use the last value that is set so you should be careful with items like Position.AvgPrice as this returns a value of 0 when you're not in a position.
If you ever set your stop loss while in a position, you also need an additional statement that sets it in mode ticks/percent when you are flat. This is used for the initial placement but then as soon as the position update is received, then it will be modified to your ATR based stop.
if(Position.MarketPosition == MarketPosition.Flat)
SetStopLoss(CalculationMode.Ticks, 20);
Leave a comment:
-
A series of "if statements" are not really branches; they are just a series of test conditions. In the sense that the block following the "if condition" may or not be executed, you could call them branching statements, but they are self contained. (I know that the general literature implies that all "if" statements are branching statements. I am just putting a finer twist on it).Originally posted by pswarts View Postthanks koganam
I'll swap them around ,but what do you mean with no branching.I embedded the If conditions or thought I did. Did I do it wrong,should I be using Else if or am I not understanding what exactly you mean with branching
So, in the particular case that you coded, all the conditions will be tested, rather that not testing some conditions based on the truth of other conditions. So yes, when I look at your code, I expect that some "else" clauses will make your logic more robust.Last edited by koganam; 11-29-2011, 04:34 PM.
Leave a comment:
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by SalmaTrader, 07-07-2026, 10:26 PM
|
0 responses
103 views
0 likes
|
Last Post
by SalmaTrader
07-07-2026, 10:26 PM
|
||
|
Started by CarlTrading, 07-05-2026, 01:16 PM
|
0 responses
62 views
0 likes
|
Last Post
by CarlTrading
07-05-2026, 01:16 PM
|
||
|
Started by CaptainJack, 06-17-2026, 10:32 AM
|
0 responses
28 views
0 likes
|
Last Post
by CaptainJack
06-17-2026, 10:32 AM
|
||
|
Started by kinfxhk, 06-17-2026, 04:15 AM
|
0 responses
36 views
0 likes
|
Last Post
by kinfxhk
06-17-2026, 04:15 AM
|
||
|
Started by kinfxhk, 06-17-2026, 04:06 AM
|
0 responses
40 views
0 likes
|
Last Post
by kinfxhk
06-17-2026, 04:06 AM
|

Leave a comment: