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!
See more
See less

Partner 728x90

Collapse

Retreiving pivot points from multiple timeframes and acting on them

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Retreiving pivot points from multiple timeframes and acting on them

    Hey everyone,

    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];

    }
    }

    #2
    Hello cpbeamer,

    Welcome to the NinjaTrader forums!

    Are you wanting to use the added series as the input series for the indicator call?

    Something like:
    CalculateValueArea(BarsArray[1], false, @"VWTPO", 0.7, 8, 30, 6.75).VAt[1];

    With the error message, the price for the stop order appears to be on the wrong side of the market.
    The full error messages should state if the order was a buy or a sell stop and the price of the stop.
    A buy stop order must have the stop price below the ask. A sell stop order must have the stop price above the bid.

    if (myPrice >= GetCurrentAsk())
    myPrice = GetCurrentAsk() - TickSize;


    Below are links to examples you may find helpful.

    Hello support team, I have 2 dataseries, a 1minute to detect the ATR value. Now I would like to set the stoploss and profit target based on the actual ATR Value Example: Stoploss 2xATR Profittarget 3xATR value 1. How can this be done with builder, I have no coding experience 2. Later I would like to have the possibility to
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thank you very much ChelseaB!!!

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by sofortune, Yesterday, 11:48 AM
      2 responses
      20 views
      0 likes
      Last Post NinjaTrader_BrandonH  
      Started by Zach55, 02-19-2024, 07:22 PM
      2 responses
      52 views
      0 likes
      Last Post lbadisa1  
      Started by JGriff5646, Today, 05:47 PM
      1 response
      9 views
      0 likes
      Last Post NinjaTrader_BrandonH  
      Started by AlphaOptions, 06-18-2013, 08:24 AM
      9 responses
      2,199 views
      0 likes
      Last Post NinjaTrader_Manfred  
      Started by ttrader23, Today, 09:33 AM
      3 responses
      22 views
      0 likes
      Last Post NinjaTrader_BrandonH  
      Working...
      X