Help in automate hedging problem

 

Hi everyone!


I'm building an EA that will automate hedge based on the scenario. There are two buttons in my EA for buy and sell, if i press buy, then 2 trades will open 1 is market order and 2nd is pending order i.e. sell stop. No if market moves against my buy order and hit the sell stops and opens it then i want to open an other trade that on the exact position my 1st buy order is placed.

But the issue i'm facing is that i'm not able sort out how to do this. My first two orders get executed successfully i.e. market execution and sell stop but i only want to open 3rd buy order if and only if my sell stop executes. But i'm not able to do this. or found any solutions regarding this. 

I'm pasting my approach below but its not making anything better, its still placing new orders while my sell stop is not being executed. Kindly help me through this.

////Buy Hedging Controll
   if(hedgeOrderCountBB >= 2 && hedgeOrderCountBB <=maxOrders)
      {
         for(int i=OrdersTotal()-1; i>=0; i--)
            {
               if(OrderSelect(i, SELECT_BY_POS)) //Checking if no Sell pending order is available
                  {
                     bool executionFlag = OrderType()==OP_SELLSTOP && OrderSymbol() == Symbol() && OrderComment()==string(hedgeOrderCountBB);
                     if(executionFlag == False)
                        {
                           Print("Order Executed, placing hedge order1. ", OrderTicket());
                        }
                  }
            }
      }

////Sell Hedging Controll
   if(hedgeOrderCountSB >= 2 && hedgeOrderCountSB <=maxOrders)
      {
         for(int i=OrdersTotal()-1; i>=0; i--)
            {
               if(OrderSelect(i, SELECT_BY_POS)) //Checking if no Buy pending order is available
                  {
                     bool executionFlag = OrderType()==OP_BUYSTOP && OrderSymbol() == Symbol() && OrderComment()==string(hedgeOrderCountSB);
                     if(executionFlag == False)
                        {
                           Print("Order Executed, placing hedge order2. ", OrderTicket());
                        }
                  }
            }
      }
 
Tamur Taj:

Hi everyone!


I'm building an EA that will automate hedge based on the scenario. There are two buttons in my EA for buy and sell, if i press buy, then 2 trades will open 1 is market order and 2nd is pending order i.e. sell stop. No if market moves against my buy order and hit the sell stops and opens it then i want to open an other trade that on the exact position my 1st buy order is placed.

But the issue i'm facing is that i'm not able sort out how to do this. My first two orders get executed successfully i.e. market execution and sell stop but i only want to open 3rd buy order if and only if my sell stop executes. But i'm not able to do this. or found any solutions regarding this. 

I'm pasting my approach below but its not making anything better, its still placing new orders while my sell stop is not being executed. Kindly help me through this.

Hi Tamur Taj,
Hope you are well.
In this situation, first get the last running buy price. Example:

    double last_buy_price = 0;
    if(OrdersTotal() > 0)
    {
    for(int i = OrdersTotal()-1; i >= 0 && IsStopped() != true; i--)
    {
    bool select = OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
    if(select == true && OrderSymbol() == _Symbol && OrderType() == OP_BUY)
    {
    last_buy_price = NormalizeDouble(OrderOpenPrice(),_Digits); break;
    }
    }
    }

After activating your sell stop as sell trade, place a buy stop order with the last_buy_price. you can do the same for sell side.

Hope, this will help you.

Reason: