How can open orders continue in same trading condition
How can open orders continue in same trading condition
Loading saved threads...
linlin · External communityPost link
External question — Stack Overflow Stack Exchange
Author: linlin
Original post: https://stackoverflow.com/questions/67487322
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
I want to open orders by orders whenever a certain trading condition is met.
I want to open pending orders in previous highest and lowest price.
So, I tried like this:
upper=iHigh(Symbol(),Period(),iHighest(Symbol(),Period(),MODE_HIGH,periods,0));
lower=iLow(Symbol(),Period(),iLowest(Symbol(),Period(),MODE_LOW,periods,0));
Comment("Upper: ",upper,"\nLower: ",lower);
int openCount=0;
int openpendCount=0;
for( int i = OrdersTotal()-1; i >= 0 ; i--)
{
if (OrderSelect(i, SELECT_BY_POS) && // Only my orders :
OrderMagicNumber() == 0 && // my magic number
OrderSymbol() == _Symbol) // and my symbol
{
if(OrderType() == OP_SELL && OrderType() ==OP_BUY) // count market orders
openCount++;
if(OrderType() == OP_SELLLIMIT && OrderType() ==OP_BUYLIMIT) // count market orders
openpendCount++;
}
}
if(openCount ==0 && openpendCount == 0 )
{ OrderSend(Symbol(), OP_SELLLIMIT,1,Ask+(upper-Ask), 3,0,0,"Sub Buy", MAGIC,0, Blue);
OrderSend(Symbol(), OP_BUYLIMIT,1,Ask-(Bid-lower), 3,0,0,"Sub Buy", MAGIC,0, Blue);
}
But no success,
Q: How can I make multiple new orders at the same time, and no more new orders when trade condition meet.
Quote
Report
PaulB · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: PaulB
Original post: https://stackoverflow.com/a/67500140
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
There are a number of problems with your code and it is difficult to know all the problems as you have not included your full code. However, try the following, it addresses the main issues I can see which are.
You should only check once on the close of each new bar
You need to check for successful selection of orders before working with them
Do not check for a magic number of 0, this indicates manually placed orders
You are mixing up
&&
and
||
when checking your order types
You are placing your orders at strange levels. You need to set them at the previously found levels (adjusted for spread for buy orders).
int MAGIC=123;
datetime TimeBar;
int periods=50;
int start()
{
if(TimeBar==Time[0]) return(0);
double upper=iHigh(NULL, 0, iHighest(NULL, 0, MODE_HIGH, periods, 0));
double lower=iLow (NULL, 0, iLowest (NULL, 0, MODE_LOW , periods, 0));
Comment("Upper: ",upper,"\r\nLower: ",lower);
int openOrders=0;
int limitOrders=0;
for(int i=OrdersTotal()-1; i>=0; i--)
{
if(OrderSelect(i, SELECT_BY_POS))
{
if(OrderMagicNumber()!=MAGIC || OrderSymbol()!=Symbol()) continue;
if(OrderType()==OP_SELL || OrderType()==OP_BUY) openOrders++;
if(OrderType()==OP_SELLLIMIT || OrderType()==OP_BUYLIMIT) limitOrders++;
}
}
if(openOrders==0 && limitOrders==0)
{
int res;
res=OrderSend(Symbol(), OP_SELLLIMIT, 1, upper, 3, 0, 0, "Sub Sell", MAGIC, 0, clrBlue);
res=OrderSend(Symbol(), OP_BUYLIMIT, 1, lower+(Ask-Bid), 3, 0, 0, "Sub Buy", MAGIC, 0, clrBlue);
}
TimeBar=Time[0];
return(0);
}
You will also need to address the problem that the EA could potentially try to open an order within
MarketInfo(Symbol(), MODE_STOPLEVEL)
of the current price which would cause the order to be rejected by the trade server.
It is also good practice to check your
OrderSend
for errors.
Quote
Report
Post Reply
Checking account access…