Looking for help with writing a no hedge piece of code

 

Hi,

I'm hoping to get some help with a slice of coding. I have a support and resistance system and I'm trying to edit the ea to include the option of hedging. (Since I live in the US and am with a no-hedge broker).

I know that when my EA is activated and I have an open order, the opposite pending order will automatically be deleted. The problem is, I can't properly backtest and tweak the system because the Strategy Tester doesn't have hedge/no hedge options.

So, I'm trying to edit the code to include that option. Obviously I need to put in a bool and set it to false (which Ive done), but I'm not sure where to go from there.

The EA is a typical support/resistance EA. It places pending orders x distance from either the support or resistance. Using an example, if the market is in a down trend and hits my pending sell and turns it into a market order, and then the trend reverses, I need to delete pending buy orders (or disallow pending buy orders) until the sell order is closed.

The EA also deletes pending orders on an expiration timer and at the end of the inputted trading hours. I wasn't sure if I could modify some of that code to do what I'm thinking of.

Here's the code from the EA that deletes the pending orders. Is there some way I could modify this to do what I'm trying to do, or if not can someone point me in the right direction?


void deletebuypending(int magic)
{
for(i=0;i<OrdersTotal();i++)
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()==Symbol() &&
OrderMagicNumber()==magic &&
OrderType()==OP_BUYSTOP)
{
OrderDelete(OrderTicket());
}
}
}

void deletesellpending(int magic)
{
for(i=0;i<OrdersTotal();i++)
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()==Symbol() &&
OrderMagicNumber()==magic &&
OrderType()==OP_SELLSTOP)
{
OrderDelete(OrderTicket());
}
}

Any help or pointing me in the right direction is appreciated.

 

I think I'm sort of on the right track. I've written this piece of code, but what happens is pending sell orders (sell stop orders) are deleted as soon as they are placed, whether there is an active buy order or not.

Still hoping someone can assist me.


if (hedge==false)
{
int total = OrdersTotal();
for(m=total-1;m>=0;m--)
{
OrderSelect(m, SELECT_BY_POS);
int type=OrderType();
int type1=OP_BUY;
int type2=OP_BUYSTOP;
int type3=OP_SELL;
int type4=OP_SELLSTOP;

if(type==type2 && type1>0)
{
OrderDelete(OrderTicket());
}
if(type==type4 && type3>0)
{
OrderDelete(OrderTicket());
}
}
}

 

Update:

I worked out the no hedge option. It seems to be working well.


if (hedge==false)
{
int total = OrdersTotal();
for(m=total-1;m>=0;m--)
{
OrderSelect(m, SELECT_BY_POS);
if(OrderType()==OP_BUY)
{
deletesellpending(magic);
}
if(OrderType()==OP_SELL)
{
deletebuypending(magic);
}

}
}

The only thing wrong with it is that it still places a pending order, then immediately deletes it. That means my chart gets filled up with continuous indicators of placement and deletion. So, I tried to "clean up" the chart screen by adding a modification to the ordering funtion.


I changed this:
if((count(OP_BUYSTOP,magic)+count(OP_BUY,magic))<maxtrades && buy && tpb<tradesperbar && IsTradeAllowed())

To this:
if((count(OP_BUYSTOP,magic)+count(OP_BUY,magic))<maxtrades && buy && tpb<tradesperbar && count(OP_SELL,magic)==0 && IsTradeAllowed())

And the opposite on the Sell function

On the strategy tester, this change worked perfectly and the chart screen no longer had opposite position pending orders placed on screen. When the market opened, I set the EA to work on a demo account to forward test it. Checking on it now, I find that this second fix is not working properly. It still places continuous indicators and then deletes them, leaving my chart cluttered.

If anyone has a suggestion how I could prevent a pending order being placed if an opposite position has been turned into a market order (ex: prevent pending buys if sell order is active), it would be much appreciated.