Change of direction code

 

Hi Everyone

I'm trying to figure out how to code a direction in price.  Lets say the price is rising, but then changes direction and I want to include both the initial rise and then the decrease.  I think it would look something like this, but I cannot get it to work properly:

void Stoploss(){
//For loop and Orderselect 
if(OrderType()==OP_BUY){
               if(OrderOpenPrice()-Bid>TSTP && lastdir==0){ //Price is rising.  Waiting for OOP-Bid to exceed TSTP trigger
               lastdir = 1;   
              }
               else
               if (OrderOpenPrice()-Bid<TSTP && lastdir == 1){ //Above trigger was hit, and price starts to fall
               chgdirup = true;
               lastdir = 1;
              }
      
            if (chgdirup == true){
               if(OrderStopLoss()<Bid-Trail){
                     if(OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Trail,OrderTakeProfit(),0,Orange))
                     RefreshRates();
                    }
                  }
                }
              }
Reason: