Announcement
Collapse
Looking for a User App or Add-On built by the NinjaTrader community?
Visit NinjaTrader EcoSystem and our free User App Share!
Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less
Partner 728x90
Collapse
NinjaTrader
EMA crossover signal (bar late)
Collapse
X
-
EMA crossover signal (bar late)
I have an issue on my strategy that when the slow EMA crosses the Fast i want it to buy on that bar. however its waiting one bar after - how do i get it to enter on current bar with the crossLast edited by SevanKambel; 04-12-2023, 09:26 PM.Tags: None
-
Originally posted by NinjaTrader_ChelseaB View PostHello SevanKambel,
Calculate.OnPriceChange (with TickReplay and 1-tick intra-bar granularity for historical).
Below is a link to a forum post that discusses.
https://forum.ninjatrader.com/forum/...992#post782992
Comment
-
Hello SevanKambel,
With Calculate OnBarClose the logic is evaluated, and any orders submitted, after the bar has fully closed.
If the order is sent after the bar is closed, then it was sent after the new bar opened.
If the order is submitted before the bar is closed with Calculate.OnPriceChange, then the order appears on that bar.
Is this historical or real-time?
To confirm, you have implemented 1-tick intra-bar granularity, enabled TickReplay, and set Calculate to OnPriceChange, but the order is still appearing on the next bar?
This would imply that something was not implemented correctly.
Are you certain that you are submitting the orders to BarsInProgress 1 of the 1-tick series?
If you Print("Calculate: " + Calculate); from OnBarUpdate() to the output window, this is showing that Calculate is OnPriceChange?
If you Print("State: " + State); from OnBarUpdate() when the order is submitted, is this showing real-time?
If you Print("Bars.IsTickReplay: " + Bars.IsTickReplay); from OnBarUpdate(), this is showing true?
If you Print("BarsArray.Count: " + BarsArray.Count); this is outputting a value of 2? (For the primary bars and the added 1 tick series)
is there a way to create a entry at the very last second of the close of the bar so it tags that bar?Chelsea B.NinjaTrader Customer Service
Comment
-
I have tried this all - attached is my code also a screen shot from Tradingview and from NINJA and you will see the difference in entry and exit... NINJA takes a 95%winrate strategy to a massive loser by having the wrong bar entry
public class FuturesGold : Strategy
{
private double ATRStop;
private double ATRPrice;
private EMA EMA1;
private EMA EMA2;
private SMA SMA1;
private RSI RSI1;
private ATR ATR1;
private EMA EMA3;
private EMA EMA4;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Strategy here.";
Name = "FuturesGold";
Calculate = Calculate.OnPriceChange;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
IsExitOnSessionCloseStrategy = true;
ExitOnSessionCloseSeconds = 30;
IsFillLimitOnTouch = false;
MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
OrderFillResolution = OrderFillResolution.Standard;
Slippage = 0;
StartBehavior = StartBehavior.WaitUntilFlat;
TimeInForce = TimeInForce.Gtc;
TraceOrders = false;
RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
StopTargetHandling = StopTargetHandling.PerEntryExecution;
BarsRequiredToTrade = 20;
// Disable this property for performance gains in Strategy Analyzer optimizations
// See the Help Guide for additional information
IsInstantiatedOnEachOptimizationIteration = true;
ATRPeriod = 14;
ATRMultiply = 0.3;
ATRStop = 1;
ATRPrice = 1;
}
else if (State == State.Configure)
{
}
else if (State == State.DataLoaded)
{
EMA1 = EMA(Close, 7);
EMA2 = EMA(Close, 26);
SMA1 = SMA(Close, 50);
RSI1 = RSI(Close, 17, 1);
ATR1 = ATR(Close, Convert.ToInt32(ATRPeriod));
EMA3 = EMA(Close, 7);
EMA4 = EMA(Close, 26);
EMA1.Plots[0].Brush = Brushes.Blue;
EMA2.Plots[0].Brush = Brushes.Red;
AddChartIndicator(EMA1);
AddChartIndicator(EMA2);
}
}
protected override void OnBarUpdate()
{
if (BarsInProgress != 0)
return;
if (CurrentBars[0] < 1)
return;
// Set 1
if ((CrossAbove(EMA1, EMA2, 1))
&& (Close[0] > SMA1[0])
&& (RSI1.Avg[0] <= 60)
&& (RSI1.Avg[0] >= 40))
{
EnterLong(Convert.ToInt32(DefaultQuantity), "");
ATRPrice = (ATR1[0] * ATRMultiply) ;
ATRStop = (Position.AveragePrice - (ATRPrice)) ;
}
// Set 2
if ((Position.MarketPosition == MarketPosition.Long)
|| (CrossBelow(EMA3, EMA4, 1)))
{
ExitLongStopMarket(Convert.ToInt32(DefaultQuantity ), ATRStop, "", "");
ExitLong(Convert.ToInt32(DefaultQuantity), "", "");
}
}
Comment
-
Hello SevanKambel,
Below is a link to a forum post that demonstrates how to use Print() and TraceOrders to understand behavior.
The issue you are having is that the order is appearing at the open of the bar after the signal bar, is this correct?
Please provide print output that shows the strategy is submitting the order before the bar closes and not after the bar closes.Chelsea B.NinjaTrader Customer Service
Comment
-
Originally posted by NinjaTrader_ChelseaB View PostHello SevanKambel,
Below is a link to a forum post that demonstrates how to use Print() and TraceOrders to understand behavior.
The issue you are having is that the order is appearing at the open of the bar after the signal bar, is this correct?
Please provide print output that shows the strategy is submitting the order before the bar closes and not after the bar closes.
Comment
-
Hello SevanKambel,
The strategy code provided does not show 1-tick intra-bar granularity has been implemented, does not have prints added to show what Calculate setting has been chosen in the strategy parameters window (iIf the value is set in State.SetDefaults, the defaults are only pulled for new instances, and the user can still change the setting), and does not print to show that TickReplay is enabled.
To make sure the order is being submitted before the bar closes in historical, implement 1-tick intra-bar granularity, enable TickReplay, and set Calculate to OnPriceChange. (Ensure that the condition is not checking for IsFirstTickOfBar to be true).
Please provide the requested print output and we can verify these things.Chelsea B.NinjaTrader Customer Service
Comment
-
Originally posted by NinjaTrader_ChelseaB View PostHello SevanKambel,
The strategy code provided does not show 1-tick intra-bar granularity has been implemented, does not have prints added to show what Calculate setting has been chosen in the strategy parameters window (iIf the value is set in State.SetDefaults, the defaults are only pulled for new instances, and the user can still change the setting), and does not print to show that TickReplay is enabled.
To make sure the order is being submitted before the bar closes in historical, implement 1-tick intra-bar granularity, enable TickReplay, and set Calculate to OnPriceChange. (Ensure that the condition is not checking for IsFirstTickOfBar to be true).
Please provide the requested print output and we can verify these things.
Comment
-
Hello SevanKambel,
You would not be able to tell the order what bar it will fill on. Instead, you can only choose to submit the order while the bar is open, instead of waiting for it to close before sending the order.
If the order is submitted while the bar is open, the order will appear on that bar. If the order is submitted after the bar closes, it will appear at the open of the next bar.
The SampleMACrossover included with NinjaTrader was not designed to place orders before the bar closes in historical data.
In the forum post on why orders appear on the next bar I have linked in post # 2, there is a link to a forum post on intra-bar granularity, with a link to the help guide reference sample 'SampleIntrabarBacktest_NT8' that is designed to place orders before the bar closes.
Be sure to run the sample with TickReplay enabled and Calculate set to OnPriceChange in the strategy parameters window.
Below is a direct link in case you are choosing not to read the information I have provided you.
Chelsea B.NinjaTrader Customer Service
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by lezlebric, Yesterday, 06:32 PM
|
1 response
26 views
0 likes
|
Last Post Today, 10:12 AM | ||
Started by ETFVoyageur, Today, 08:39 AM
|
10 responses
16 views
0 likes
|
Last Post Today, 10:06 AM | ||
Started by tkaboris, Yesterday, 07:53 PM
|
5 responses
18 views
0 likes
|
Last Post
by tkaboris
Today, 09:40 AM
|
||
Started by GLFX005, Today, 09:31 AM
|
1 response
8 views
0 likes
|
Last Post Today, 09:40 AM | ||
Started by tgn55, Today, 09:15 AM
|
1 response
4 views
0 likes
|
Last Post Today, 09:34 AM |
Comment