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.​

    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 Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        666 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        376 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        110 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        575 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        580 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X