Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Simple C# problem

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

    Simple C# problem

    protected override void OnMarketData(MarketDataEventArgs marketDataUpdate)
    {
    if (marketDataUpdate.MarketDataType == MarketDataType.Last)
    {
    if(marketDataUpdate.Price != LastTradePrice)
    {
    LastTradePrice = marketDataUpdate.Price;
    Print("Last = " + LastTradePrice);
    }
    }
    Print("Last high 1 = " + LastHigh);
    if (LastTradePrice > LastHigh);
    {
    LastHigh = LastTradePrice;
    Print("Last high = " + LastHigh);
    }


    }

    ​I am tying to get LastHigh to only change its value when the price goes higher, but it also goes down. I am doing something wrong can you see what?

    Thanks

    #2
    Hello Malcolm,

    Thank you for your post.

    I recommend updating your print statements to include the time as well as all variables involved before and after LastTradePrice and LastHigh are updated. Here is an example of your snippet with some updated print statements:

    Code:
    protected override void OnMarketData(MarketDataEventArgs marketDataUpdate)
    {
    if (marketDataUpdate.MarketDataType == MarketDataType.Last)
    {
    if(marketDataUpdate.Price != LastTradePrice)
    {
    // note: the above could be simplified to if (marketDataUpdate.MarketDataType == MarketDataType.Last && marketDataUpdate.Price != LastTradePrice)
    Print(String.Format("{0} MarketDataType: {1} Price: {2} != LastTradeprice: {3}", Time[0], marketDataUpdate.MarketDataType, marketDataUpdate.Price, LastTradePrice));
    LastTradePrice = marketDataUpdate.Price;
    Print(Time[0] + "LastTradePrice Updated: " + LastTradePrice);
    }
    }
    Print(Time[0] + "Last high 1 = " + LastHigh);
    if (LastTradePrice > LastHigh);
    {
    Print(String.Format("{0} LastTradePrice: {1} > LastHigh: {2}", Time[0], LastTradePrice, LastHigh));
    LastHigh = LastTradePrice;
    Print(Time[0] + "Last high Updated: " + LastHigh);
    }
    }
    For more information about using prints to debug your script:


    Please let me know the results after adding prints with all relevant information. If you are unsure of the results in the output window, you may right-click and Save As to save the output to a file that you may attach to your reply here.

    Thank you for your patience.​
    Emily C.NinjaTrader Customer Service

    Comment


      #3
      You are using the != operator which means Not Equal, so if price is not equal to the last saved then it will save the latest different price. I think you want to use the “>;” greater than operator.

      Ex: if(marketDataUpdate.Price > LastTradePrice)

      Comment


        #4
        malcolm You could try something like this:
        Code:
        protected override void OnMarketData(MarketDataEventArgs marketDataUpdate)
        {
            if (marketDataUpdate.MarketDataType == MarketDataType.Last)
            {
                double PriorHigh = LastHigh;
                LastHigh = Math.Max(LastHigh,marketDataUpdate.Price);
                if (LastHigh > PriorHigh)
                    Print("New High = " + LastHigh);
        ​    }
        }​
        This eliminates the use of LastTradePrice, so if you need that, modify accordingly.

        Thanks.
        Last edited by jeronymite; 11-17-2022, 06:23 PM.
        Multi-Dimensional Managed Trading
        jeronymite
        NinjaTrader Ecosystem Vendor - Mizpah Software

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by lightsun47, Today, 03:51 PM
        0 responses
        5 views
        0 likes
        Last Post lightsun47  
        Started by 00nevest, Today, 02:27 PM
        1 response
        10 views
        0 likes
        Last Post 00nevest  
        Started by futtrader, 04-21-2024, 01:50 AM
        4 responses
        46 views
        0 likes
        Last Post futtrader  
        Started by Option Whisperer, Today, 09:55 AM
        1 response
        14 views
        0 likes
        Last Post bltdavid  
        Started by port119, Today, 02:43 PM
        0 responses
        10 views
        0 likes
        Last Post port119
        by port119
         
        Working...
        X