In Need of Help With This Function.

Post Reply
investguy
Posts: 7
Joined: Wed Apr 18, 2012 5:17 am

In Need of Help With This Function.

Post by investguy »

Hi,

Im writing a function for an EA that upon change of trend it will sum all the opened trades and open an order in the opposit direction with a new magic number. This order will only close when the trend changes again. For some reason it is opening multiple orders with the right amount of lots(the sum). What am i doing wrong?

Here is the function:

Code: Select all

void WatchDog() {
	
	double WatchDogLotsBuy = 0;
	double WatchDogLotsSell = 0;
	int OpenBuyOrders = 0;
	int OpenSellOrders = 0;

	if ((IRSI > HighRSILevel) && (XO > 0)) { // BUY ONLY 
		// Opening Watchdog Orders	
		for (int e=0; e<OrdersTotal(); e++) {
			if ( OrderSelect(e,SELECT_BY_POS, MODE_TRADES) && OrderSymbol()==Symbol() && OrderMagicNumber()==Magic){  
				if ((OrderComment() == "WatchDogBuy") || (OrderComment() == "WatchDogSell")) break; 
				if(OrderType()==OP_SELL){
					WatchDogLotsSell = OrderLots() + WatchDogLotsSell;
				}	
			OpenBuyOrders = 1;
			}
		}
		if ( WatchDogLotsSell > 0){
			WatchDogLotsSell = NormalizeDouble(WatchDogLotsSell,LotDigits);
			OrderSend(Symbol(), OP_BUY, WatchDogLotsSell , Ask, SLIPPAGE(), 0, 0, "WatchDogBuy", Magic+8, 0, CLR_NONE);
			Print("Request to Open BUY Watchdog Order w/ " , DoubleToStr(WatchDogLotsSell,LotDigits) , " Lots" ); 
		}
	}
	
	
	if ((IRSI < LowRSILevel) && (XO < 0)) {  // SELL ONLY 
		// Opening Watchdog Orders	
		for (int s=0; s<OrdersTotal(); s++) {
			if ( OrderSelect(s,SELECT_BY_POS, MODE_TRADES) && OrderSymbol()==Symbol() && OrderMagicNumber()==Magic){  
				if ((OrderComment() == "WatchDogBuy") || (OrderComment() == "WatchDogSell")) break; 
				if(OrderType()==OP_BUY){
					WatchDogLotsBuy = OrderLots() + WatchDogLotsBuy;
				}	
			OpenSellOrders = 1;	
			}
		}
		if ( WatchDogLotsBuy > 0){
			WatchDogLotsBuy = NormalizeDouble(WatchDogLotsBuy,LotDigits);
			OrderSend(Symbol(), OP_SELL, WatchDogLotsBuy , Bid, SLIPPAGE(), 0, 0, "WatchDogSell", Magic+8, 0, CLR_NONE);
			Print("Request to Open SELL Watchdog Order w/ " , DoubleToStr(WatchDogLotsBuy,LotDigits) , " Lots" ); 
		}
	}	
	// END Opening Watchdog Orders	
	
	// CLOSING Watchdog Orders	// DO IT BY TICKET NUMBER
	for (int c=0; c<OrdersTotal(); c++) {
		if ( OrderSelect(c,SELECT_BY_POS, MODE_TRADES) && OrderSymbol()==Symbol() && (OrderMagicNumber()==Magic+8)) {  
			
			if((OrderType()==OP_BUY) && ((IRSI < LowRSILevel) && (XO < 0)) && (OrderComment() == "WatchDogBuy")) {   // CLOSING BUY ORDERS
				OrderClose(OrderTicket(),OrderLots(),Bid,SLIPPAGE(),CLR_NONE);
				Print("Request to Close BUY Watchdog Order" ); 
			}
			
			if((OrderType()==OP_SELL) && ((IRSI > HighRSILevel) && (XO > 0)) && (OrderComment() == "WatchDogSell")) {   // CLOSING SELL ORDERS
				OrderClose(OrderTicket(),OrderLots(),Ask,SLIPPAGE(),CLR_NONE);	
				Print("Request to Close SELL Watchdog Order" ); 
			}
	    }
	}
		
	// END CLOSING Watchdog ORDERS
}
You do not have the required permissions to view the files attached to this post.
Radar
Trader
Posts: 437
Joined: Fri Mar 23, 2012 5:39 pm
Location: Round the bend ;)

Re: In Need of Help With This Function.

Post by Radar »

Hey Investguy,

I read on here recently about acting on a change of state, rather than on state, to prevent multiple orders going through...

Try checking that the previous bar's RSI is below HighRSILevel, and that the current bar's RSI is above....

Code: Select all

if ((iRSI(Symbol(),PERIOD_RSI,14,PRICE_CLOSE,1) < HighRSILevel) && (iRSI(Symbol(),PERIOD_RSI,14,PRICE_CLOSE,0) > HighRSILevel) && (XO > 0))
or break it down to nested if statements...

Code: Select all

if (iRSI(Symbol(),PERIOD_RSI,14,PRICE_CLOSE,1) < HighRSILevel)
if (iRSI(Symbol(),PERIOD_RSI,14,PRICE_CLOSE,0) > HighRSILevel)
if (XO > 0)
Either one should do the trick, I think. If you go with the first example, check my parentheses... I always mis-place my parentheses :(

Have fun!

Radar =8^)
Check out my new, (well, old now), manual trade & automatic scale-in manager,
StackManV2
Post Reply

Return to “Coders Hangout”