I send you the code for you to observe
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
How to make purchases and sales at the close of the bar and update the stop in ticks
Collapse
X
-
-
Hello jmarcano,
This is a first step.
You have now gotten to the point that you can see that you are able to trigger actions only on the first tick of a new bar after a bar has closed. This should confirm that this works correctly.
The next step is to add the values of the condition print.
In the action block for 'if (IsFirstTickOfBar)' begin printing the values in the condition.
For example the next condition:
&&(Times[0][0].TimeOfDay >= Apertura.TimeOfDay)
Print("\r\n" + Time[0].ToString() + " Times[0][0].TimeOfDay: " + Times[0][0].TimeOfDay.ToString() + " => Apertura.TimeOfDay: " + Apertura.TimeOfDay.ToString());
What does this print to the output window?
Please include the saved output as a text file as discussed in the video linked in the forum post that demonstrates how to use prints to understand behavior, linked from post #6 instead of a screenshot.
I personally prefer string.Format() as demonstrated in the video linked that the forum post that demonstrates how to use prints to understand behavior.Chelsea B.NinjaTrader Customer Service
Comment
-
Good afternoon Chelsea, sorry for the delay in sending the file with the print output.Attached Files
Comment
-
code
protected override void OnBarUpdate()
{
if (IsFirstTickOfBar
&& (Times[0][0].TimeOfDay >= Apertura.TimeOfDay))
{
Print("\r\n" + Time[0].ToString() + " FIRST TICK OF BAR!!!!!!!!!!!!!!!!!");
Print("\r\n" + Time[0].ToString() + " Times[0][0].TimeOfDay: " + Times[0][0].TimeOfDay.ToString() + " => Apertura.TimeOfDay: " + Apertura.TimeOfDay.ToString());
}
else
{
Print(Time[0].ToString() + " not first tick of bar");
}
// Set 1
if (Position.MarketPosition == MarketPosition.Flat)
{
SetStopLoss(CalculationMode.Ticks, Stop);
}
// Set 2
if (IsFirstTickOfBar
&&(Times[0][0].TimeOfDay >= Apertura.TimeOfDay)
&& (Times[0][0].TimeOfDay < Cierre.TimeOfDay)
&& (Position.MarketPosition == MarketPosition.Flat)
&& (VolumeUpDown1.UpVolume[0] > 1300)
&& (CrossAbove(Close, Swing1.SwingHigh, 1))
&& (Close[0] > EMA1[0])
&& (Operaciones < TradeDay)
&& (Long == true))
{
EnterLong(Convert.ToInt32(DefaultQuantity), "");
Operaciones = Operaciones + 1;
Print("\r\n" + Time[0].ToString() + " FIRST TICK OF BAR LONG!!!!!!!!!!!!!!!!!");
Print("\r\n" + Time[0].ToString() + " Times[0][0].TimeOfDay: " + Times[0][0].TimeOfDay.ToString() + " => Apertura.TimeOfDay: " + Apertura.TimeOfDay.ToString());
}
// Set 3
// If a long position is open, allow for stop loss modification to breakeven
else if ((Position.MarketPosition == MarketPosition.Long)
&& (Close[0] >= Position.AveragePrice))
{
// Once the price is greater than entry price+50 ticks, set stop loss to breakeven
if (Close[0] >= Position.AveragePrice + BE * TickSize)
{
SetStopLoss(CalculationMode.Price, Position.AveragePrice + (6 * TickSize));
Print(Time[0].ToString() + " not first tick of bar Long");
}
}
Comment
-
Hello jmarcano,
A good attempt.
It is important that we see the print (on the first tick of a bar) for every bar and not just when the condition is true.
Please replace:
Code:if (IsFirstTickOfBar && (Times[0][0].TimeOfDay >= Apertura.TimeOfDay)) { Print("\r\n" + Time[0].ToString() + " FIRST TICK OF BAR!!!!!!!!!!!!!!!!!"); Print("\r\n" + Time[0].ToString() + " Times[0][0].TimeOfDay: " + Times[0][0].TimeOfDay.ToString() + " => Apertura.TimeOfDay: " + Apertura.TimeOfDay.ToString()); } else { Print(Time[0].ToString() + " not first tick of bar"); }
Code:if (IsFirstTickOfBar && (Times[0][0].TimeOfDay >= Apertura.TimeOfDay)) { Print("\r\n" + Time[0].ToString() + " Times[0][0].TimeOfDay: " + Times[0][0].TimeOfDay.ToString() + " => Apertura.TimeOfDay: " + Apertura.TimeOfDay.ToString()); }
The next line of the condition set is:
Code:&& (Times[0][0].TimeOfDay < Cierre.TimeOfDay)
Chelsea B.NinjaTrader Customer Service
Comment
-
Good afternoon Chelsea, sending the file with the print output.
You do not send me any impressions because I think we are out of the hour of operation?
Attached Files
Comment
-
Update Code
protected override void OnBarUpdate()
{
if (IsFirstTickOfBar
&& (Times[0][0].TimeOfDay >= Apertura.TimeOfDay)
&& (Times[0][0].TimeOfDay < Cierre.TimeOfDay))
{
Print("\r\n" + Time[0].ToString() + " Times[0][0].TimeOfDay: " + Times[0][0].TimeOfDay.ToString() + " => Apertura.TimeOfDay: " + Apertura.TimeOfDay.ToString());
}
// else
// {
// Print(Time[0].ToString() + " not first tick of bar");
// }
Comment
-
Hello jmarcano,
Good job.
The next line of the condition set is:
Code:&& (Times[0][0].TimeOfDay < Cierre.TimeOfDay)
Include the output from the output window with your reply.Chelsea B.NinjaTrader Customer Service
Comment
-
Good afternoon Chelsea, sending the file with the print output.
Thank you very much for the support you are giving me.
Attached Files
Comment
-
code
if (IsFirstTickOfBar
&& (Times[0][0].TimeOfDay >= Apertura.TimeOfDay)
&& (Times[0][0].TimeOfDay < Cierre.TimeOfDay))
{
Print("\r\n" + Time[0].ToString() + " Times[0][0].TimeOfDay: " + Times[0][0].TimeOfDay.ToString() + " => Apertura.TimeOfDay: " + Apertura.TimeOfDay.ToString());
Print("\r\n" + Time[0].ToString() + " Times[0][0].TimeOfDay: " + Times[0][0].TimeOfDay.ToString() + " < Cierre.TimeOfDay: " + Cierre.TimeOfDay.ToString());
}
// else
// {
// Print(Time[0].ToString() + " not first tick of bar");
// }
Comment
-
Hello jmarcano,
You are doing great. That print is exactly what we need.
So far I see lots of bars that meet these requirements.
The rest of the condition is:
Code:&& (Position.MarketPosition == MarketPosition.Flat) && (VolumeUpDown1.UpVolume[0] > 1300) && (CrossAbove(Close, Swing1.SwingHigh, 1)) && (Close[0] > EMA1[0]) && (Operaciones < TradeDay) && (Long == true))
Once we have prints for these conditions, and we have TraceOrders enabled, we will be able to see for any bar why this order was not submitted and filled.Chelsea B.NinjaTrader Customer Service
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by timmbbo, Today, 08:59 AM
|
0 responses
1 view
0 likes
|
Last Post
by timmbbo
Today, 08:59 AM
|
||
Started by bmartz, 03-12-2024, 06:12 AM
|
5 responses
33 views
0 likes
|
Last Post Today, 08:34 AM | ||
Started by Aviram Y, Today, 05:29 AM
|
4 responses
14 views
0 likes
|
Last Post
by Aviram Y
Today, 07:57 AM
|
||
Started by algospoke, 04-17-2024, 06:40 PM
|
3 responses
28 views
0 likes
|
Last Post Today, 07:41 AM | ||
Is it possible to have a drawing, such as a line, cycle through different colors?
by gentlebenthebear
Started by gentlebenthebear, Today, 01:30 AM
|
1 response
8 views
0 likes
|
Last Post Today, 07:30 AM |
Comment