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

Using a BUY/SELL button to open trades but price rates are incorrect...

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

    Using a BUY/SELL button to open trades but price rates are incorrect...

    Hi,
    My issue is that when I run this strategy (I created) on a playback connection, the prices that the strategy see's are from the Live chart instead of the playback chart, which is what I am connected to.
    To calrify this is only happening with the button I created, to open a limit order (or position).
    How can I have the button operation utilize the chart rates instead of the live rates?

    The button works fine and calls up a function that opens a limit or order, but the price value of Close[0] is not from the chart but its from the actual current Close[0] value.

    Here's an example for a user opening a limit order.

    1. User clicks on BUY button ==> function openTrade(OP_BUY) is called

    The function openTrade(int op) is comprised of the following:

    Code:
    void openTrade(int op){            
    
                double limitprice=0;
    
                limitprice=(op==OP_BUY?Close[0]-TickSize:Close[0]+TickSize);
    
                if(StopLoss>0) SetStopLoss("stopLoss"), CalculationMode.Ticks, StopLoss,false);
                if(TakeProfit>0) SetProfitTarget("takeProfit"), CalculationMode.Ticks, TakeProfit);                                
    
                if(op==OP_BUY) EnterLongLimit(0,true,contractSize,limitprice,"Time[1].ToString("yyyy.MM.dd HH:mm"));
                if(op==OP_SELL) EnterShortLimit(0,true,contractSize,limitprice,"Time[1].ToString("yyyy.MM.dd HH:mm"));
    
            }​


    Thnx.
    Jess

    EDIT:
    I included a video describing the issue, hope it helps

    Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.

    Last edited by xmess777; 01-27-2023, 03:13 PM.

    #2
    Hello xmess777,

    You need to use TriggerCustomEvent within your method or button event that calls the method. BarsAgo only works within bar events like OnBarUpdate. Button events have no bars contexts to know what [0] BarsAgo would be, its just returning the first bar of the series instead.

    Code:
    TriggerCustomEvent(o =>
    {
        // code that uses BarsAgo here
    }, null);



    https://ninjatrader.com/support/help...tsub=TriggerCu stomEvent
    JesseNinjaTrader Customer Service

    Comment


      #3
      Jesse! Awesome thnx for the info. I will look into this.

      Jess

      Comment


        #4
        ISSUE SOLVED:

        Before diving into the TriggerCustomEvent() approach. I figured less complicated approach. It's sort of similar to the examples of using the TriggerCostumEvent() approach.

        Since only the Time, OHLC values of the latest bar (on the chart) are needed for when a user hits the BUY/SELL button. ...

        1. Created a class with 5 members (time,open,high,low,close) and set it to currData
        2. At each bar open updated currData with Time[0],Open[0],High[0],Low[0],Close[0].
        3. When user clicks on Buy/Sell button a function called clickTrade() is fired.
        4. In the clickTrade() function the currData is accessed.

        Its similar to the TriggerCustomEvent​() in that it access a "custom" set of data via currData members, which are constanly updated with the last bar data.

        This did the trick, the button operation now works with no issues.

        If anyone needs more info please don't hesitate to contact me.
        Thnx
        Jess




        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Segwin, 05-07-2018, 02:15 PM
        14 responses
        1,789 views
        0 likes
        Last Post aligator  
        Started by Jimmyk, 01-26-2018, 05:19 AM
        6 responses
        837 views
        0 likes
        Last Post emuns
        by emuns
         
        Started by jxs_xrj, 01-12-2020, 09:49 AM
        6 responses
        3,293 views
        1 like
        Last Post jgualdronc  
        Started by Touch-Ups, Today, 10:36 AM
        0 responses
        13 views
        0 likes
        Last Post Touch-Ups  
        Started by geddyisodin, 04-25-2024, 05:20 AM
        11 responses
        63 views
        0 likes
        Last Post halgo_boulder  
        Working...
        X