Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
ABOVE/BELOW EMAs
Collapse
X
-
ABOVE/BELOW EMAs
I have a strategy that everything works fine but it takes some trades when its not above or below all EMA lines.. how can i add that condition for my long and short entriesTags: None
-
this is the code i have just need to add code to check if above EMAs but cant get it right closeAboveEMAs = true; and closeBelowEMAs = true; if not it doesnt take the trade along with other conditions
region Entry
if (Position.MarketPosition == MarketPosition.Flat)
{
if (emaLong && closeLong && timeOK && longMaxCheck)
{
if (myLongEntry1 == null && myLongEntry2 == null && myLongEntry3 == null)
{
if (qty1 > 0) EnterLongLimit(0, true, qty1, rangeHprice, "LONG-1");
if (qty2 > 0) EnterLongLimit(0, true, qty2, rangeHprice, "LONG-2");
if (qty3 > 0) EnterLongLimit(0, true, qty3, rangeHprice, "LONG-3");
if (showOutput) Print(Time[0] + " " + Instrument.FullName + " Long limit order set at: " + rangeHprice);
}
}
else if (Close[0] >= longMaxEntry && (myLongEntry1 != null || myLongEntry2 != null || myLongEntry3 != null))
{
if (myLongEntry1 != null) CancelOrder(myLongEntry1);
if (myLongEntry2 != null) CancelOrder(myLongEntry2);
if (myLongEntry3 != null) CancelOrder(myLongEntry3);
myLongEntry1 = myLongEntry2 = myLongEntry3 = null;
if (showOutput) Print(Time[0] + " " + Instrument.FullName + " Price is above max entry, pending orders cancelled");
}
else if (emaShort && closeShort && timeOK && shortMinCheck)
{
if (myShortEntry1 == null && myShortEntry2 == null && myShortEntry3 == null)
{
if (qty1 > 0) EnterShortLimit(0, true, qty1, rangeLprice, "SHORT-1");
if (qty2 > 0) EnterShortLimit(0, true, qty2, rangeLprice, "SHORT-2");
if (qty3 > 0) EnterShortLimit(0, true, qty3, rangeLprice, "SHORT-3");
if (showOutput) Print(Time[0] + " " + Instrument.FullName + " Short limit order set at: " + rangeLprice);
}
}
else if (Close[0] <= shortMinEntry && (myShortEntry1 != null || myShortEntry2 != null || myShortEntry3 != null))
{
if (myShortEntry1 != null) CancelOrder(myShortEntry1);
if (myShortEntry2 != null) CancelOrder(myShortEntry2);
if (myShortEntry3 != null) CancelOrder(myShortEntry3);
myShortEntry1 = myShortEntry2 = myShortEntry3 = null;
if (showOutput) Print(Time[0] + " " + Instrument.FullName + " Price is below min entry, pending orders cancelled");
}
}
#endregion
-
this is what i tried - changing this line of code
{
if (emaLong && closeLong && timeOK && longMaxCheck)
to this
{
if (emaLong && closeLong && timeOK && longMaxCheck && Close[0] > emaPeriod1 && Close[0] > emaPeriod2 && Close[0] > emaPeriod3 && Close[0] > emaPeriod4)
but it doesnt work
Comment
-
region Conditions
bool emaLong = (Close[0] > emaPeriod1 && Close[0] > emaPeriod2 && Close[0] > emaPeriod3 && Close[0] > emaPeriod4) ? true : false;
bool emaShort = (Close[0] < emaPeriod1 && Close[0] > emaPeriod2 && Close[0] < emaPeriod3 && Close[0] < emaPeriod4) ? true : false;
bool closeLong = (Close[0] > rangeHprice) ? true : false;
bool closeShort = (Close[0] < rangeLprice) ? true : false;
bool longMaxCheck = (Close[0] < longMaxEntry) ? true : false;
bool shortMinCheck = (Close[0] > shortMinEntry) ? true : false;
Comment
-
Hello SevanKambel,
Thank you for your inquiry.
In order to better understand your strategy's conditions and why it might be taking trades unexpectedly, I suggest adding print statements that print the values and all of the variables used in your conditions. By printing the Close[0] value along with the different EMA values, you can see what values were used when the condition is evaluated to be true or false. This can help you to understand what you will need to adjust for your strategy to behave as expected.
For more information about adding prints and other debugging tips, please see the links below:Please let us know if we may be of further assistance.
Comment
-
i do have prints but aboveEMA isnt oneOriginally posted by NinjaTrader_Emily View PostHello SevanKambel,
Thank you for your inquiry.
In order to better understand your strategy's conditions and why it might be taking trades unexpectedly, I suggest adding print statements that print the values and all of the variables used in your conditions. By printing the Close[0] value along with the different EMA values, you can see what values were used when the condition is evaluated to be true or false. This can help you to understand what you will need to adjust for your strategy to behave as expected.
For more information about adding prints and other debugging tips, please see the links below:Please let us know if we may be of further assistance.
PrintTo = PrintTo.OutputTab2;
Print(Time[0] + " emalong: " + emaLong);
Print(Time[0] + " emaShort: " + emaShort);
Print(Time[0] + " closeLong: " + closeLong);
Print(Time[0] + " closeShort: " + closeShort);
Print(Time[0] + " timeOK: " + timeOK);
Print(Time[0] + " longMaxCheck: " + longMaxCheck);
Print(Time[0] + " shortMinCheck: " + shortMinCheck);
Print("----------------------------");
PrintTo = PrintTo.OutputTab1;
Comment
-
Hello SevanKambel,
Thank you for your reply.
I see you have the following bools:
bool emaLong = (Close[0] > emaPeriod1 && Close[0] > emaPeriod2 && Close[0] > emaPeriod3 && Close[0] > emaPeriod4) ? true : false;
bool emaShort = (Close[0] < emaPeriod1 && Close[0] > emaPeriod2 && Close[0] < emaPeriod3 && Close[0] < emaPeriod4) ? true : false;
You also have the following prints:
Print(Time[0] + " emalong: " + emaLong);
Print(Time[0] + " emaShort: " + emaShort);
The thing is, you are only seeing if emaLong/emaShort are true or false. You are not printing the actual values of Close[0] or the different EMAs in use. I suggest adding the values to your print to see why these bools are true or false. For example:
Print(String.Format("{0} emaLong: {1} emaShort: {2} Close[0]: {3} emaPeriod1: {4} emaPeriod2: {5} emaPeriod3: {6} emaPeriod4: {7}", Time[0], emaLong, emaShort, Close[0], emaPeriod1, emaPeriod2, emaPeriod3, emaPeriod4));
Then, you can see if emaLong or emaShort is true/false as well as the value for Close[0] that is being compared to see if it is greater than/less than the different EMA values.
Please let me know if I may be of further assistance.
Comment
-
Those bools were me attempting to make it work .. this is the originalOriginally posted by NinjaTrader_Emily View PostHello SevanKambel,
Thank you for your reply.
I see you have the following bools:
bool emaLong = (Close[0] > emaPeriod1 && Close[0] > emaPeriod2 && Close[0] > emaPeriod3 && Close[0] > emaPeriod4) ? true : false;
bool emaShort = (Close[0] < emaPeriod1 && Close[0] > emaPeriod2 && Close[0] < emaPeriod3 && Close[0] < emaPeriod4) ? true : false;
You also have the following prints:
Print(Time[0] + " emalong: " + emaLong);
Print(Time[0] + " emaShort: " + emaShort);
The thing is, you are only seeing if emaLong/emaShort are true or false. You are not printing the actual values of Close[0] or the different EMAs in use. I suggest adding the values to your print to see why these bools are true or false. For example:
Print(String.Format("{0} emaLong: {1} emaShort: {2} Close[0]: {3} emaPeriod1: {4} emaPeriod2: {5} emaPeriod3: {6} emaPeriod4: {7}", Time[0], emaLong, emaShort, Close[0], emaPeriod1, emaPeriod2, emaPeriod3, emaPeriod4));
Then, you can see if emaLong or emaShort is true/false as well as the value for Close[0] that is being compared to see if it is greater than/less than the different EMA values.
Please let me know if I may be of further assistance.
region Conditions
bool emaLong = (EMA1[0] > EMA2[0] && EMA2[0] > EMA3[0] && EMA3[0] > EMA4[0]) ? true : false;
bool emaShort = (EMA1[0] < EMA2[0] && EMA2[0] < EMA3[0] && EMA3[0] < EMA4[0]) ? true : false;
bool closeLong = (Close[0] > rangeHprice) ? true : false;
bool closeShort = (Close[0] < rangeLprice) ? true : false;
bool longMaxCheck = (Close[0] < longMaxEntry) ? true : false;
bool shortMinCheck = (Close[0] > shortMinEntry) ? true : false;
Comment
-
Comment
-
Hello SevanKambel,
Thank you for your reply.
For the trade in question from the screenshot, do you also have the output to the NinjaScript Output window that prints all of the values used in your condition to trigger the trade along with the values of the Close[0] price and the EMAs for that bar when the trade was taken? The output will show why the condition was evaluated to be true and why the trade was taken, and that can help to understand what might need to be adjusted. Without the output to match up with the unexpected trade, there is not enough information to determine why the trade is happening. I would be glad to take a look at the output with you if you provide the condition for entry along with the output (you may right-click the NinjaScript Output window and select Save As to save a text file. The text file can be attached to your forum reply) including prints of all of the values used in the condition, the Close price, and the EMA values.
Please let me know if I may be of further assistance.
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by NullPointStrategies, Today, 05:17 AM
|
0 responses
24 views
0 likes
|
Last Post
|
||
|
Started by argusthome, 03-08-2026, 10:06 AM
|
0 responses
120 views
0 likes
|
Last Post
by argusthome
03-08-2026, 10:06 AM
|
||
|
Started by NabilKhattabi, 03-06-2026, 11:18 AM
|
0 responses
63 views
0 likes
|
Last Post
|
||
|
Started by Deep42, 03-06-2026, 12:28 AM
|
0 responses
41 views
0 likes
|
Last Post
by Deep42
03-06-2026, 12:28 AM
|
||
|
Started by TheRealMorford, 03-05-2026, 06:15 PM
|
0 responses
45 views
0 likes
|
Last Post
|

Comment