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

Strategy on playback vs Continuum vs simulated

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

    Strategy on playback vs Continuum vs simulated

    My code works on playback but when i try to run it on simulated or continuum it does not.

    protected override void OnBarUpdate()
    {

    if(State == State.Historical)
    {
    return;
    }
    if (CurrentBar < BarsRequiredToTrade)
    return;
    //Add your custom strategy logic here.
    if(setPrice == false)
    {
    LimitPrice = Close[0] + (8 * TickSize);
    AddLimitPrice = Close[0] - (8 * TickSize);
    setPrice = true;
    }


    if(Position.MarketPosition == MarketPosition.Flat && firstEntry == false)
    {


    if(Position.MarketPosition == MarketPosition.Flat)
    {
    EnterLong(1,"long entry");
    }

    if(Position.MarketPosition == MarketPosition.Long)
    {
    EnterShortStopMarket(0,true,2,AddLimitPrice," First short limit entry");
    }

    firstEntry = true;
    }​

    The EnterLong(1,"long entry"); gets executed but that is it. On my orders tab i cannot see EnterShortStopMarket(0,true,2,AddLimitPrice," First short limit entry"); and nothing happens if the price gets to the point when entershortstopmarket should trigger.
    Everything works fine in playback.
    Last edited by cosmin1ke; 11-23-2022, 12:45 AM. Reason: Add more explanations

    #2
    Hello cosmin1ke,

    The code you have posted shouldn't work in general, you have your EnterShortStopMarket inside a condition checking for flat in addition to checking for long. Its not going to be both long and flat so the condition shouldn't be executed. I removed the entry condition so you can see the structure more easily here:

    Code:
    if(Position.MarketPosition == MarketPosition.Flat && firstEntry == false)
    {
       if(Position.MarketPosition == MarketPosition.Long)
       {
          EnterShortStopMarket(0,true,2,AddLimitPrice," First short limit entry");
       }
    
       firstEntry = true;
    }​
    The first parent condition is checking if the bool variable is false in addition to checking for Flat. Inside that condition you check if the position is Long which it won't be while the position is flat.

    Once the entrry condition happens once and you set your bool to false the parent condition no longer works so the condition checking for long is never checked again.

    You need to remove the long condition from inside the flat conditions:


    Code:
    if(Position.MarketPosition == MarketPosition.Flat && firstEntry == false)
    {
    
    
    
        firstEntry = true;
    }​
    
    if(Position.MarketPosition == MarketPosition.Long)
    {
        EnterShortStopMarket(0,true,2,AddLimitPrice," First short limit entry");
    }

    JesseNinjaTrader Customer Service

    Comment


      #3
      Let's assume that the code that i posted , the one that "shouldn't work in general" does work in playback.
      I will also add that i am on eachtick calculation mode.

      Is there any other problem that you can think of that cause it not to work in continuum?

      Comment


        #4
        Hello cosmin1ke,

        The Position.MarketPosition can't be both long and flat at the same time, its a single value so I am not sure what's happening in your test that would be correct. The code you posted is not valid so you would need to fix it and re test in playback and the other modes.

        To do logic based on the position you always want to use multiple conditions like the following:

        Code:
        if(Position.MarketPosition == MarketPosition.Flat)
        {
            // Position.MarketPosition is now flat
        }​
        if(Position.MarketPosition == MarketPosition.Short)
        {
           // Position.MarketPosition is now Short
        }​​
        if(Position.MarketPosition == MarketPosition.Long)
        {
           // Position.MarketPosition is now Long
        }​
        The conditions are all at the same scope so only 1 of the 3 conditions will be true during a call to OnBarUpdate. This helps to seperate logic based on the position. What you provided should not work because you have a sub condition within the parent conditions scope:


        Code:
        if(Position.MarketPosition == MarketPosition.Flat )
        {
            // the position is flat 
            if(Position.MarketPosition == MarketPosition.Long)
           {
                // this cannot be true because the parent condition is true, if the parent condition was true that means Position.MarketPosition is flat and not long or short
           }
        }​

        JesseNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Jonafare, 12-06-2012, 03:48 PM
        5 responses
        3,984 views
        0 likes
        Last Post rene69851  
        Started by Fitspressorest, Today, 01:38 PM
        0 responses
        2 views
        0 likes
        Last Post Fitspressorest  
        Started by Jonker, Today, 01:19 PM
        0 responses
        2 views
        0 likes
        Last Post Jonker
        by Jonker
         
        Started by futtrader, Today, 01:16 PM
        0 responses
        7 views
        0 likes
        Last Post futtrader  
        Started by Segwin, 05-07-2018, 02:15 PM
        14 responses
        1,791 views
        0 likes
        Last Post aligator  
        Working...
        X