I have been having some issues on a strategy I have been working on using orderflow pivots. Specifically I am having trouble with retrieving the Value area high and value area low from the prevvios day. It also seems that my pivots for the current day (current open, VAH, VAL) arent retreiving what I am looking for.
When I place the strategy to the 3min timeframe that I like to trade on, it calculates all the values from the previous and current 3min bar, which makes the strategy crazy. The strategy will only trade for the first 30 minutes of the day and hits a trade every single bar before running into and error:
Error: An order has been ignored since the stop price x near the bar x is invalid because of the price range of the bar.
I am sorry if this is asking a lot, I am trying to learn NinjaScript and would really appreciate some guidance here.
Please view the code below:
public class OrderFlowPivots : Strategy
{
// Declaration of all the potential pivots
private double priorHigh;
private double priorLow;
private double priorClose;
private double currentOpen;
private double currentLow;
private double currentHigh;
private double openPrice;
private double currentVAL;
private double currentVAH;
private double priorVAL;
private double priorVAH;
// Set up the order, traget, and stop variables
private Order entryOrder = null;
private Order stopOrder = null;
private Order targetOrder = null;
private int sumFilled = 0;
private int barNumberOfOrder = 0;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Automated trading bot that uses Orderflow pivots to scalp";
Name = "OrderFlowPivots";
Calculate = Calculate.OnEachTick;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
IsExitOnSessionCloseStrategy = true;
ExitOnSessionCloseSeconds = 30;
IsFillLimitOnTouch = false;
MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
OrderFillResolution = OrderFillResolution.Standard;
Slippage = 0;
StartBehavior = StartBehavior.WaitUntilFlat;
TimeInForce = TimeInForce.Day;
TraceOrders = false;
RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
StopTargetHandling = StopTargetHandling.PerEntryExecution;
BarsRequiredToTrade = 20;
OffsetFromPivot = 1;
StopLossTicks = 4;
ProfitTargetTicks = 4;
// Disable this property for performance gains in Strategy Analyzer optimizations
// See the Help Guide for additional information
IsInstantiatedOnEachOptimizationIteration = true;
}
else if (State == State.Configure)
{
// Adds the one second data series
//Adds data series for daily charts
AddDataSeries("ES 06-21", BarsPeriodType.Day, 1);
}
else if (State == State.DataLoaded)
{
if (CurrentBar < 20)
return;
//Get data for the current open
currentOpen = PriorDayOHLC().Open[0];
}
else if (State == State.DataLoaded) {
if (BarsInProgress == 1){
// Sets the values for the prior day's high, low, and close
if (PriorDayOHLC().PriorHigh[1] > 0) {
priorHigh = PriorDayOHLC().PriorHigh[1];
priorLow = PriorDayOHLC().PriorLow[1];
priorClose = PriorDayOHLC().PriorClose[1];
}
//Calculate the VAH and VAL of the prior day
priorVAH = CalculateValueArea(false, @"VWTPO", 0.7, 8, 30, 6.75).VAt[1];
priorVAL = CalculateValueArea(false, @"VWTPO", 0.7, 8, 30, 6.75).VAb[1];
}
}
}
protected override void OnBarUpdate() {
if (CurrentBar < BarsRequiredToTrade)
return;
double[] allPivots = new double[] { priorHigh, priorLow, priorClose, currentOpen, openPrice, currentVAL, currentVAH, priorVAL, priorVAH };
/* Handles all updates for bar updates on the 1min chart (Orgininal Data Series of the Chart) */
if (BarsInProgress == 0) {
/* Loop through the supports array and if the current price is within 10 Ticks (2.5 points)
of the support price, we are going to place a limit order at the support price and one tick below */
for (int index = 0; index < allPivots.Length; index++) {
if ((GetCurrentBid() - allPivots[index]) <= (10 * TickSize) && Position.MarketPosition == MarketPosition.Flat) {
EnterShortLimit(1, allPivots[index] + (OffsetFromPivot * TickSize), "short");
}
else if ((allPivots[index] - GetCurrentBid()) >= (10 * TickSize) && Position.MarketPosition == MarketPosition.Flat) {
EnterShortLimit(1, allPivots[index] + (OffsetFromPivot * TickSize), "short");
EnterLongLimit(1, allPivots[index] - (OffsetFromPivot * TickSize), "long");
}
}
/* If we have a long position and the current price is 4 ticks in profit, raise the stop loss order to breakeven */
if (Position.MarketPosition == MarketPosition.Long && Close[0] >= Position.AveragePrice + (8 * TickSize) || Position.MarketPosition == MarketPosition.Short && Close[0] >= Position.AveragePrice - (8 * TickSize) ) {
// Checks to see if our Stop order has been submitted already
if (stopOrder != null && stopOrder.StopPrice < Position.AveragePrice) {
// Modifies stop-loss to breakeven
stopOrder = ExitLongStopMarket(0, true, stopOrder.Quantity, Position.AveragePrice, "MyStop", "long");
}
}
currentVAH = CalculateValueArea(false, @"VWTPO", 0.7, 8, 30, 6.75).VAt[1];
currentVAL = CalculateValueArea(false, @"VWTPO", 0.7, 8, 30, 6.75).VAb[1];
}
}
Comment