My goal is to buy based on a condition of the price of the opening candle and trade on that candle. like if I have a condition to buy based on a series of conditions in an if statement but I also include the open price of the day I can place the trade on that day NOT the next day.
I am using OnBarUpdate() but have tried OnTick() but that doesn't seem to work. I understand that OnBarUpdate() would not work here, because it is set up to read this conditional if statment information and make or not make the trade the NEXT DAY.
You see in my example, I want to buy on a simple condition for illustration purposes if the current days open is greater than the past day candle then buy that candle's open. See in this example I wanted to buy the SECOND GREEN CANDLE from right to left, but because it only applies the trade based on the logic to the next day the trade occurred on the subsequent candle.
protected override void OnBarUpdate()
{
double followingCandleClose = Close[0];
Print("Time Date " + Time[0].Date);
// Get the bar index for the current trading day
int currentDayBarIndex = Bars.GetBar(Time[0].Date);
Print("Current Day Bar Index: " + currentDayBarIndex);
// Get the index of the first bar of the current trading day
int firstBarOfDayIndex = CurrentBar - (Bars.GetBar(Time[0]) - currentDayBarIndex);
// Get the open price of the current trading day
double currentDayOpen = Open[firstBarOfDayIndex];
Print("Current Day Open: " + currentDayOpen);
if (
currentDayOpen >= followingCandleClose
)
{
EnterLong(OrderQuantity, "Buy4");
}
}
Output Window:
Time Date 4/30/2024 12:00:00 AM
Current Day Bar Index: 3886
Current Day Open: 2688.5
Current Time: 17:00:00
Current Day Open: 2688.5
Following Candle Close: 17832
Time Date 5/1/2024 12:00:00 AM
Current Day Bar Index: 3887
Current Day Open: 2688.5
Current Time: 17:00:00
Current Day Open: 2688.5
Following Candle Close: 17699
Time Date 5/2/2024 12:00:00 AM
Current Day Bar Index: 3888
Current Day Open: 2688.5
Current Time: 17:00:00
Current Day Open: 2688.5
Following Candle Close: 17910.5
I've tried Calculate = Calculate.OnEachTick; instead of Calculate = Calculate.OnBarClose;
same results
Please help.
Thank you,
MatHatter

Comment