Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Difference between Open Close
Collapse
X
-
Hello,
You first would need to declare a variable to store the calculation of the difference between the open and the close.
Next, you would then need to create a condition to check when this was more than 30 pipsCode:double difference = Open[0] - Close[0];
Code:if (difference > 30) { // enter your short position }MatthewNinjaTrader Product Management
-
Matthew....I am getting an error message from this.....any idea why?????
#region Variables
// Wizard generated variables
private int myInput0 = 1; // Default setting for MyInput0
// User defined variables (add any user defined variables below)
double difference = Open[0]-Close[0];
#endregion
protected override void Initialize()
{
SetProfitTarget("", CalculationMode.Ticks, 10);
TraceOrders = true;
CalculateOnBarClose = true;
ExitOnClose = false;
}
protected override void OnBarUpdate()
{
// Condition set 1
if (Position.MarketPosition == MarketPosition.Flat
&& (difference >= 10)
&& (Close[0] > Open[0]))
{
EnterLong(10000, "");
}
// Condition set 2
if (Position.MarketPosition == MarketPosition.Flat
&& (difference >= 10)
&& (Close[0] < Open[0]))
{
EnterShort(10000, "");
}
Comment
-
Hi edgeliner,
You can't access those bar objects there in variables region. Best practice here is declaring in Variables region, and then assign in OnBarUpdate() as example below.
Code:#region Variables // Wizard generated variables private int myInput0 = 1; // Default setting for MyInput0 // User defined variables (add any user defined variables below) private double difference; #endregion protected override void OnBarUpdate() { difference = Open[0]-Close[0]; //rest of code here. }Last edited by NinjaTrader_RyanM1; 10-12-2011, 09:14 AM.Ryan M.NinjaTrader Customer Service
Comment
-
OK.....all set with regard to compiling.....but for some reason, it never opens a position?????
protected override void OnBarUpdate()
{
{
//Only run on real-time data. This will prevent your strategy from executing on historical data and basically will now run on real-time data only!
if (Historical)
return;
}
difference = Open[0]-Close[0];
// Condition set 1
if (Position.MarketPosition == MarketPosition.Flat
&& (difference >= 10)
&& (Close[0] > Open[0]))
{
EnterLong(10000, "");
}
// Condition set 2
if (Position.MarketPosition == MarketPosition.Flat
&& (difference <= -10)
&& (Close[0] < Open[0]))
{
EnterShort(10000, "");
}
Comment
-
No entries usually means your condition is never true.
If you're talking about pips and a difference of 10, this may explain it. You would never see a difference of 10 from Open[0] - Close[0] when looking at EURUSD, for example.
To check difference of 10 pips, make this change:
difference >= 10 * TickSize
It's always good to print all values and verify through Tools > Output Window. This post can help with this:
Ryan M.NinjaTrader Customer Service
Comment
-
This is crazy Ryan! You were right about the EURUSD.....and I change it, but it still did not open any positions????
difference = Open[0]-Close[0];
// Condition set 1
if (Position.MarketPosition == MarketPosition.Flat
&& (difference >= 10 * TickSize)
&& (Close[0] > Open[0]))
{
EnterLong(10000, "");
}
// Condition set 2
if (Position.MarketPosition == MarketPosition.Flat
&& (difference <= -10 * TickSize)
&& (Close[0] < Open[0]))
{
EnterShort(10000, "");
}
Comment
-
It's the same item - the condition never evaluates true. You will need to learn to check values through print statements so you can verify this on your end.
Print(difference); and check using Tools > Output Window.
These two conditions will never be true together.
difference >= 10 * TickSize
Close[0] > Open[0]
Break this down with some hypothetical values and you can see why it might never be true.
Open = 100
Close = 112
110 - 112 = -12
Basically -- for every bar where Close > Open, the difference between Open - Close on these bars will always be a negative value.Last edited by NinjaTrader_RyanM1; 10-13-2011, 11:14 AM.Ryan M.NinjaTrader Customer Service
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
635 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
365 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
106 views
0 likes
|
Last Post
by Mindset
02-09-2026, 11:44 AM
|
||
|
Started by Geovanny Suaza, 02-02-2026, 12:30 PM
|
0 responses
567 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
571 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment