Developing the code for a strategy that trades during the open: 8:30a - 9:30a.
The very first bar that is printed uses the high of that bar as a pivotHigh and the low of that bar as pivotLow. At the end of the trade session every day (9:30a) I'm having the code reset the values of pivotHigh and pivotLow so that we don't go into the next day using any of the previous pivots. I do that with this code within onBarUpdate:
{
// Reset pivot variables after 9:30 AM
if (Time[0].TimeOfDay > new TimeSpan(9, 30, 0))
{
if (!isSessionReset)
{
pivotHigh = 0;
pivotLow = 0;
isSEMDetected = false;
Print("Pivot High and Pivot Low reset");
isSessionReset = true;
}
return;
}
else
{
isSessionReset = false;
}
Then I use instructions to set the pivotHigh and pivotLow to the High and Low of previous bar if current bar is 1.
// Wait for the first bar after 8:30 AM
if (Time[0].TimeOfDay < new TimeSpan(8, 30, 0) || Time[0].TimeOfDay > new TimeSpan(9, 30, 0))
return;
// Set Pivot High and Pivot Low on the first bar
if (CurrentBar == 1)
{
if (pivotHigh == 0 && pivotLow == 0) // Check if not already set
{
pivotHigh = High[0];
pivotLow = Low[0];
Print("Initial Pivot High: " + pivotHigh);
Print("Initial Pivot Low: " + pivotLow);
}
In my print logs I'm seeing that at the end of the previous day, pivotHigh and pivotLow are being reset.
But on the following day, as an example, I see that a short trade is entered (even though conditions haven't been met because the strategy will require at least 2 bars to trade), it is setting the pivotHigh to the correct price (high of the first bar at 8:30a close) but it has not set the pivotLow to the low of the first bar. A short trade is taken (without the conditions being met), and then when the long trade is taken (conditions are met) it does correctly set the new pivotLow. I really need that pivotLow set at the low of the first bar though. I'm using Calculate.OnBarClose
Here are the print logs starting from post session (9:30a+) and the opening session of today:
Pivot High and Pivot Low reset
7/12/2023 9:32:30 AM Strategy 'VectorPointsv4/256058138: Cancelled pending exit order, since associated position is closed, orderId='NT-00026-8490' account='SimMATest' name='Profit target' orderState=Working instrument='NQ SEP23' orderAction=Sell orderType='Limit' limitPrice=15514.25 stopPrice=0 quantity=1 tif=Gtc oco='NT-00016-8490' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2023-07-12 09:24:00' gtd='2099-12-01' statementDate='2023-07-13'
7/12/2023 9:32:30 AM Strategy 'VectorPointsv4/256058138': Cancelled OCO paired order: BarsInProgress=0, orderId='NT-00026-8490' account='SimMATest' name='Profit target' orderState=Cancelled instrument='NQ SEP23' orderAction=Sell orderType='Limit' limitPrice=15514.25 stopPrice=0 quantity=1 tif=Gtc oco='NT-00016-8490' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2023-07-12 09:24:00' gtd='2099-12-01' statementDate='2023-07-13'
Short Trade Entry
7/13/2023 8:30:30 AM Strategy 'VectorPointsv4/256058138': Entered internal SubmitOrderManaged() method at 7/13/2023 8:30:30 AM: BarsInProgress=0 Action=SellShort OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='' FromEntrySignal=''
Pivot High reset to: 15579.25
7/13/2023 8:30:30 AM Strategy 'VectorPointsv4/256058138': Entered internal SetStopTarget() method: Type=Target FromEntrySignal='' Mode=Ticks Value=40 IsSimulatedStop=False IsMarketIfTouched=False
7/13/2023 8:30:30 AM Strategy 'VectorPointsv4/256058138': Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Ticks Value=100 IsSimulatedStop=False IsMarketIfTouched=False
7/13/2023 8:31:30 AM Strategy 'VectorPointsv4/256058138': Entered internal SubmitOrderManaged() method at 7/13/2023 8:31:30 AM: BarsInProgress=0 Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='' FromEntrySignal=''
Long Trade Entry
Pivot Low reset to: 15552.25
TL;DR: pivotHigh and pivotLow are not being accurately set based on the price action of the first bar of session. I'm using a private double variable to set pivotHigh and pivotLow:
private double pivotHigh = 0; // Pivot High
private double pivotLow = 0; // Pivot Low
Hoping someone can help.

Comment