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

Management orders with Custom TimeFrame

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

    Management orders with Custom TimeFrame

    Hello! I'm developing an strategy and I have some problem.

    1º) I use two time frame: 1) RenkoSpectrumBarsType (10,1) 2) Second (1)

    I add the second in the Initialize: base.Add(PeriodType.Second, 1);

    2º) I get the entry flag on the BarsInProgress == 0 and I send the orders on the BarsInProgress == 1 for not to wait to the next renko candle

    if (BarsInProgress == 0)
    {
    flag = true;
    entry = MyIndicator(BarsArray[0]).Entry[0];
    }
    if (BarsInProgress == 1)
    {
    if (flag && (entryOrder1 == null && entryOrder2 == null))
    {
    if (entry == 1)
    {
    entryOrder1 = EnterLong(1, "MyEntry1");
    entryOrder2 = EnterLong(1, "MyEntry2");
    }
    if (entry == -1)
    {
    entryOrder1 = EnterShort(1, "MyEntry1");
    entryOrder2 = EnterShort(1, "MyEntry2");
    }
    }
    flag = false;
    }

    3º) I set the target and stop loss on OnExecution


    protected override void OnExecution(IExecution execution)
    {

    if (entryOrder1 != null && entryOrder1 == execution.Order)
    {
    if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
    {
    if (Position.MarketPosition == MarketPosition.Long)
    {
    // Stop-Loss order 4 ticks below our entry price
    stopOrder1 = ExitLongStop(0, true, execution.Order.Filled, execution.Order.AvgFillPrice - stop * TickSize, "MyStop1", "MyEntry1");
    // Target order 8 ticks above our entry price
    targetOrder1 = ExitLongLimit(0, true, execution.Order.Filled, execution.Order.AvgFillPrice + target * TickSize, "MyTarget1", "MyEntry1");
    }
    else
    {
    // Stop-Loss order 4 ticks above our entry price
    stopOrder1 = ExitShortStop(0, true, execution.Order.Filled, execution.Order.AvgFillPrice + stop * TickSize, "MyStop1", "MyEntry1");
    // Target order 8 ticks above our entry price
    targetOrder1 = ExitShortLimit(0, true, execution.Order.Filled, execution.Order.AvgFillPrice - target * TickSize, "MyTarget1", "MyEntry1");
    }


    // Resets the entryOrder object to null after the order has been filled
    if (execution.Order.OrderState != OrderState.PartFilled)
    {
    entryOrder1 = null;
    }
    }
    }

    if (entryOrder2 != null && entryOrder2 == execution.Order)
    {
    if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
    {
    if (Position.MarketPosition == MarketPosition.Long)
    {
    // Stop-Loss order 4 ticks below our entry price
    stopOrder2 = ExitLongStop(0, true, execution.Order.Filled, execution.Order.AvgFillPrice - stop * TickSize, "MyStop2", "MyEntry2");
    }
    else
    {
    // Stop-Loss order 4 ticks above our entry price
    stopOrder2 = ExitShortStop(0, true, execution.Order.Filled, execution.Order.AvgFillPrice + stop * TickSize, "MyStop2", "MyEntry2");
    }
    // Target order 8 ticks above our entry price
    //targetOrder = ExitLongLimit(0, true, execution.Order.Filled, execution.Order.AvgFillPrice + 8 * TickSize, "MyTarget", "MyEntry");

    // Resets the entryOrder object to null after the order has been filled
    if (execution.Order.OrderState != OrderState.PartFilled)
    {
    entryOrder2 = null;
    }
    }
    }
    // Reset our stop order and target orders' IOrder objects after our position is closed.
    if ((stopOrder1 != null && stopOrder1 == execution.Order) || (targetOrder1 != null && targetOrder1 == execution.Order))
    {
    if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled)
    {
    stopOrder1 = null;
    targetOrder1 = null;
    }
    }
    // Reset our stop order and target orders' IOrder objects after our position is closed.
    if ((stopOrder2 != null && stopOrder1 == execution.Order) || (targetOrder2 != null && targetOrder2 == execution.Order))
    {
    if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled)
    {
    stopOrder2 = null;
    targetOrder2 = null;
    }
    }
    }

    4º) I run the strategy with 6 ticks of stop loss and Target, and I see the error in all orders.
    For example with the las order:

    Entry Price 17742 8:50:31 and exit price stop loss 17752 8:50:43
    Free online storage and sharing with Screencast.com. 2 GB of storage and 2 GB of bandwidth per month for free. We won't compress, alter or take ownership of your content.

    Free online storage and sharing with Screencast.com. 2 GB of storage and 2 GB of bandwidth per month for free. We won't compress, alter or take ownership of your content.


    But I see on the 1 second chart the price is no real, 17742 8:50:31 and 17743 8:50:43
    Free online storage and sharing with Screencast.com. 2 GB of storage and 2 GB of bandwidth per month for free. We won't compress, alter or take ownership of your content.


    So I think the strategy are closing the order because the height renko candle, but it's no a real simulator.


    I don't know how to fix it.

    I have thought

    1º) Adding the RenkoSpectrumBarsType on the second time frame on the Add() but I don't know how do it with the two params.

    Add(PeriodType.Custom4, 10 ,1) ??

    I see on the code:

    public RenkoSpectrumBarsType() : base(PeriodType.Custom4)
    {
    this.NewBarMode = true;
    }

    2º) I dont know how to management the orders only in the secondary time frame (Second, 1)

    Best regards
    David

    #2
    Hello David,

    Thank you for your post.

    Is this what you see on historical data or in backtesting? Renko bars are not accurate in backtesting as the ticks that built the bars are not available. Meaning the bar is built from Open, High, Low, and Close.
    Do you see the same behavior when running on NinjaTrader Renko bars (not the custom ones) in backtesting?

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by lorem, Yesterday, 09:18 AM
    5 responses
    17 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by Spiderbird, Today, 12:15 PM
    0 responses
    5 views
    0 likes
    Last Post Spiderbird  
    Started by cmtjoancolmenero, Yesterday, 03:58 PM
    12 responses
    42 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by FrazMann, Today, 11:21 AM
    0 responses
    6 views
    0 likes
    Last Post FrazMann  
    Started by geddyisodin, Yesterday, 05:20 AM
    8 responses
    52 views
    0 likes
    Last Post NinjaTrader_Gaby  
    Working...
    X