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 charlesugo_1, 05-26-2026, 05:03 PM
        0 responses
        47 views
        0 likes
        Last Post charlesugo_1  
        Started by DannyP96, 05-18-2026, 02:38 PM
        1 response
        141 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by CarlTrading, 05-11-2026, 05:56 AM
        0 responses
        160 views
        0 likes
        Last Post CarlTrading  
        Started by CarlTrading, 05-10-2026, 08:12 PM
        0 responses
        96 views
        0 likes
        Last Post CarlTrading  
        Started by Hwop38, 05-04-2026, 07:02 PM
        0 responses
        275 views
        0 likes
        Last Post Hwop38
        by Hwop38
         
        Working...
        X