Your loop doesn't check that there are no current trades with the magic number, it checks if the selected order does not have the magic number.
Where are the values for number_of_buys and number_of_sells calculated?
...
Any idea guys?
Your loop doesn't check that there are no current trades with the magic number, it checks if the selected order does not have the magic number.
Where are the values for number_of_buys and number_of_sells calculated?
Ohhhhhhhhh I see. So my loop only selects ONE order and sees if it has no magic number = to the one I want and thats why it places so many trades.
Well the fix then would be to make it go through all open orders, make sure THEY dont have the magic number 333 for example and then place a trade. How would I do this? I dont know how to write a loop that would make sure there isnt a magic number in all the open trades before performing an action.
int _get_number_of_trades(int ttype){ //function that returns number of currently opene trades on current pair int tCount = 0; for(int cnt=0;cnt<OrdersTotal();cnt++){ bool result = OrderSelect(cnt,SELECT_BY_POS, MODE_TRADES); if(OrderSymbol() == Symbol() && OrderType() == ttype) tCount++; } return(tCount); } int number_of_buys = _get_number_of_trades(OP_BUY); int number_of_sells = _get_number_of_trades(OP_SELL);
if(OrderSelect(cnt,SELECT_BY_POS, MODE_TRADES)) if(OrderMagicNumber == MagicNumber && OrderSymbol() == Symbol() && OrderType() == ttype) tCount++;
int _get_number_of_trades(int ttype){ //function that returns number of currently opene trades on current pair int tCount = 0; for(int cnt=0;cnt<OrdersTotal();cnt++){ bool result = OrderSelect(cnt,SELECT_BY_POS, MODE_TRADES); if(OrderSymbol() == Symbol() && OrderType() == ttype && OrderMagicNumber()!=333||334) tCount++; } return(tCount); } int _get_number_of_trades_hedge_buy(int ttype){ //function that returns number of currently opene trades on current pair int tCount = 0; for(int cnt=0;cnt<OrdersTotal();cnt++){ bool result = OrderSelect(cnt,SELECT_BY_POS, MODE_TRADES); if(OrderSelect(cnt,SELECT_BY_POS, MODE_TRADES)) if(OrderMagicNumber() == 333 && OrderSymbol() == Symbol() && OrderType() == ttype) tCount++; } return(tCount); } int _get_number_of_trades_hedge_sell(int ttype){ //function that returns number of currently opene trades on current pair int tCount = 0; for(int cnt=0;cnt<OrdersTotal();cnt++){ bool result = OrderSelect(cnt,SELECT_BY_POS, MODE_TRADES); if(OrderSelect(cnt,SELECT_BY_POS, MODE_TRADES)) if(OrderMagicNumber() == 334 && OrderSymbol() == Symbol() && OrderType() == ttype) tCount++; } return(tCount); } void hedger() { int buy_trades_number = _get_number_of_trades(OP_BUY); int sell_trades_number = _get_number_of_trades(OP_SELL); int numberofhedgedbuys = _get_number_of_trades_hedge_buy(OP_BUY); int numberofhedgedsells = _get_number_of_trades_hedge_sell(OP_SELL); if (buy_trades_number == 7 && numberofhedgedbuys == 0) {OrderSend(Symbol(),OP_SELL,50,Bid,3,0,0,"hedged",333,0,clrRed);} if (sell_trades_number == 7 && numberofhedgedsells == 0) {OrderSend(Symbol(),OP_BUY,50,Ask,3,0,0,"hedged",334,0,clrGreen);} }hedger() runs in my onTick() function. Essentially it checks if there are 7 current buys or sells in and if there are 7 buys in, for example, it should place a 50 lot sell trade. Vice versa.
However it doesnt. It still places multiple and I cant see where Im going wrong.
if(OrderSymbol() == Symbol() && OrderType() == ttype && OrderMagicNumber()!=333||334) tCount++;
Why are you using != ??
if(OrderSymbol() == Symbol() && OrderType() == ttype && (OrderMagicNumber()==333 || OrderMagicNumber()==334)) tCount++;
Is that what you intended?
Why are you using != ??
Is that what you intended?
Basically, if I have 7 other trades (not the 50 lot ones), I want to place a 50 lot trade in the other direction
Then you probably intended
if(OrderSymbol() == Symbol() && OrderType() == ttype && OrderMagicNumber()!=333 && OrderMagicNumber()!=334) tCount++;

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
What should be happening is that the loop scans for trades by magic number and if there are no current trades with the magic number 333 and there are 7 buys, then it places one 50 lot sell trade. The vice versa is true for sell.
However what does happen is that it places infinite 50 lot trades until it crashes. It should only place a 50 lot trade if the conditions are true.
Any idea guys?