Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Variable problem

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

    Variable problem

    Can someone help me with this. I can't get the 3rd condition on my strategy to print. When I take the code apart and just run the 3rd condition it works fine, but for some reason doesn't work with the other 2 conditions. Any help would be greatly appreciated.
    Here is the code:
    CurrentPrice = Close[0];
    EntryPrice = High[
    1]+1*TickSize ;
    RangeCompleted = High[
    0] == Low[0]+(Bars.Period.Value*TickSize);
    TradeOKPrice = Low[
    1]-1*TickSize;

    if(FirstTickOfBar)
    {
    if(CurrentPrice == TradeOKPrice)
    {
    Variable0=
    1;
    Print(
    "Trade OK");
    }
    return;
    }

    if(RangeCompleted && Variable0==1)
    {
    Variable0=
    0;
    Print(
    "Take Trade");
    }
    if(FirstTickOfBar)
    {
    if(CurrentPrice == EntryPrice && Variable0==0)
    {
    Print(
    "Do Not Trade");
    }
    return;
    }
    Thanks,
    Rick

    #2
    Rick, how do you initialize your variable0? Does it print if you do not 'return out' in your first condition?

    Comment


      #3
      Hmmm... I don't know what that means unfortunately. What you see there is what is in my code. I guess I've left that part out.
      The only item I have in the Initialize() is CalculateOnBarClose = false, but I don't know if that is what you mean. How would I initialize variable0?

      Also, if I understand you correctly, Trade OK does print when continually when the first condition stops being true but then becomes true again.
      In the Output window I get:
      Trade OK
      Take Trade
      Trade OK
      Take Trade
      etc.
      when the market is trending down, which is what I want it to do, but then when the market trends up the "Do Not Trade" never shows up, but neither does the "Take Trade". So when the market trends up I should just see "Do Not Trade" and when it trends down I should see "Trade OK" and "Take Trade".

      Comment


        #4
        Hello,

        I see that you are using a return statement in your line in FirstTickOfBar. This return statement terminated the rest of the onBarUpdate and not just the if statement for the first tick of the new bar. Therefore this could lead to unexpected results as well. If this does not resolve the issue by removing the return statement please see the below for troubleshooting steps.

        Your doing if statements that will evaluate to true and print the lines to the output window if all conditions are true. Obviously a condition is not true when you think it should be. Therefor you will need to track down where this is. Here are the variables you are using and need to check: These are below:



        TradeOKPrice
        RangeCompleted
        CurrentPrice
        EntryPrice
        Variable0

        We do not see Variable0 be initialized we only see you initialize the following variable:

        CurrentPrice = Close[0];
        EntryPrice = High[
        1]+1
        *TickSize ;
        RangeCompleted = High[
        0] == Low[0
        ]+(Bars.Period.Value*TickSize);
        TradeOKPrice = Low[
        1]-1*TickSize;


        To initialize Variable0 this would look something like this and is found in the variables section of the code that is usually compressed and have to use the Plus sign in the left column to expand the variables section and is above Initialize().

        private int Variable0 = 0;


        You will need to Print these to the output window to debug where the unexpected value comes from. Like Below, do this for all variables:

        Print("TradeOKPrice: " + TradeOKPrice.toString())


        Let me know if I can be of further assistance.
        BrettNinjaTrader Product Management

        Comment


          #5
          New issue

          Hello again and thanks for the help.
          I have placed a Limit order in place of one of the Print() and the strategy is working now in real time to place orders.
          I have used the the SetStopLimit() and SetProfitTarget() in the Initialize(). Everything works in real time, but when I use the Strategy Analyzer I get no trades at all. Any ideas why that would happen?

          Comment


            #6
            Rick, any errors shown in the log tab? Have you checked into with TraceOrders turned on in your strategy?



            Also: I believe you're working with CalculateOnBarClose = false, this will not work in backtesting - it would be defaulted to true as the intrabar tick formation is not known at that point, try for example Market Replay then as viable alternative.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            650 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            370 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            109 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            574 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            577 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X