Trading Systems Leaders in this forum (automated trading systems) are winning more than 3000 pips in a month (30000$ investing one lot every time). Click here to register and get more information
i would like to write a code for a hedge strategy in one currency and stop open when, for example, buy OP are more then sell OP, so i need to write something like, "if totalorder of OP_BUY > totalorders of OP_SELL" any idea?
Code:
int BuyCnt = 0;
int SellCnt = 0;
int BuyStopCnt = 0;
int SellStopCnt = 0;
int BuyLimitCnt = 0;
int SellLimitCnt = 0;
int cnt = OrdersTotal();
for (int i=0; i < cnt; i++)
{
if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
//if (OrderSymbol() != Symbol()) continue;
//if (OrderMagicNumber() != Magic) continue;
int type = OrderType();
if (type == OP_BUY) BuyCnt++;
if (type == OP_SELL) SellCnt++;
if (type == OP_BUYSTOP) BuyStopCnt++;
if (type == OP_SELLSTOP) SellStopCnt++;
if (type == OP_BUYLIMIT) BuyLimitCnt++;
if (type == OP_SELLLIMIT) SellLimitCnt++;
}
IF NUMBER OF OPEN TRADES = 8 THEN CLOSE THE OLDEST OPEN TRADE
I want to close the oldest market order if there are >=8 market orders, but to ignore all the pending orders
thanks
Code:
extern int Magic = ...
//-----
int BuyCnt = 0;
int SellCnt = 0;
int cnt = OrdersTotal();
for (int i=0; i < cnt; i++)
{
if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
if (OrderSymbol() != Symbol()) continue;
if (OrderMagicNumber() != Magic) continue;
int type = OrderType();
if (type == OP_BUY) BuyCnt++;
if (type == OP_SELL) SellCnt++;
}
if (BuyCnt+SellCnt < 8) return;
//-----
datetime tm = 0;
int ticket = -1;
cnt = OrdersTotal();
for (i=0; i < cnt; i++)
{
if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
if (OrderSymbol() != Symbol()) continue;
if (OrderMagicNumber() != Magic) continue;
type = OrderType();
if (type == OP_BUY || type == OP_SELL)
{
if (OrderOpenTime() > tm)
{
tm = OrderOpenTime();
ticket = OrderTicket();
}
}
}
//-----
if (ticket != -1) OrderClose(ticket, ...
Lets say I am in a trade based on a stochastic setting. I want to exit based on another stochastic cross totally different from my entry. No problem yet. But lets say at my exit the market continues to go up. I want to change the exit based on 95% of the current stochastic signal line to the next level to keep me in the trade. So once I have my current stochastic setting at 95% and it gets hit, my new exit is 95% of the new setting and once this gets hit my new exit is 95% of that setting. This setup can keep you in a trade until the market peaks and turns down crossing the signal and the trade is exited on the cross. You know when it seems overbought or oversold. By changing the stochastic settings, as the price increases you are able to stay in the market till the trend breaks. Once trade is closed, you start next order with 1st level of settings. Currently I use a main signal on one setting crossing a signal signal on another setting. I only use 1 of the lines each stoch and black the other one out. I want to be able to move at least 4 levels up if the market continues to trend. Of course the opposite would be for a sell.
Could someone please help me with some language to make my Expert Advisor trade a percentage of my balance? For example, I would like each trade, instead of being x number of lots, to be 10% of my current balance. Does anyone know how to do this? The more explicit the details the more helpful it would be.
sell:
when price have 70pip range
exampel : price open day : 208.55
low price(low price day) :208.30
and price going up (high price day)to 209.00
OP sell 209.00.....
buy: when price have 70pip range
exampel : price open day : 208.55
high price(high price day) :208.70
and price going down (low price day)to 207.00
OP buy 207.00.....
sell:
when price have 70pip range
exampel : price open day : 208.55
low price(low price day) :208.30
and price going up (high price day)to 209.00
OP sell 209.00.....
buy: when price have 70pip range
exampel : price open day : 208.55
high price(high price day) :208.70
and price going down (low price day)to 207.00
OP buy 207.00.....
thks RickD
Hi,
Do you mean that I should write the EA for you?
Please send a PM me.
many thanks for being so helpful here. You couldnīt be so kind to introduce the solution of the following problem to me?
My EA sometimes gets stopped out in a winning trade and a few seconds later it sets up the same trade once again, which is not only senseless but very often results in losses. How can I tell the EA to wait with a new order 1 hour, or any other time?
Thanks in advance!
-wonderkey
Hi,
Select the last order and don't trade some time.
Code:
int MinTimeSec = 60; //no trade 60 seconds after last close
int Magic = ...
void start()
{
...
if (LastOrderSelect(MODE_HISTORY, OP_BUY, OP_SELL))
{
if (TimeCurrent() - OrderCloseTime() < MinTimeSec) return;
}
...
}
bool LastOrderSelect(int pool, int type1, int type2 = -1)
{
datetime tm = -1;
int ticket = -1;
if (pool == MODE_TRADES)
{
int cnt = OrdersTotal();
for (int i=0; i < cnt; i++)
{
if (!OrderSelect(i, SELECT_BY_POS, pool)) continue;
if (OrderSymbol() != Symbol()) continue;
if (OrderMagicNumber() != Magic) continue;
int type = OrderType();
if (type == type1 || type == type2)
{
if (OrderOpenTime() > tm)
{
tm = OrderOpenTime();
ticket = OrderTicket();
}
}
}
return (OrderSelect(ticket, SELECT_BY_TICKET));
}
if (pool == MODE_HISTORY)
{
cnt = OrdersHistoryTotal();
for (i=0; i < cnt; i++)
{
if (!OrderSelect(i, SELECT_BY_POS, pool)) continue;
if (OrderSymbol() != Symbol()) continue;
if (OrderMagicNumber() != Magic) continue;
type = OrderType();
if (type == type1 || type == type2)
{
if (OrderCloseTime() > tm)
{
tm = OrderCloseTime();
ticket = OrderTicket();
}
}
}
return (OrderSelect(ticket, SELECT_BY_TICKET));
}
return (false);
}
I wrote Alert but it only works when I open window with my indicator. It works only also when I jump beetwen charts windows, in other words when I jump between charts time levels. It seems that indicator can not count when whorks. How can I solve my problem ? Need help. Is it conected with Indicatorcounted ? Should I do something in parameter of Alert. How to do it ?
Should I use something like:
double
? = ObjectGetValueByShift(string name, int shift));
Hey Rick, I'm glad you're doing this.
What's the code for crossing of two 2-line indicators? I'm sure it isn't really difficult. Say, the 1st indicator's lines cross each other 1 bar before [MAIN > SIGNAL] = BUY order, 2nd indicator's lines cross one another [MAIN < SIGNAL] = SELL order.
What I came up with is this, but it just doesn't cut it.
Code:
if (Time[0] == prevtime)
return(0);
prevtime = Time[0];
//IND1 = iIND1 (NULL, 0, 0, MODE_MAIN, 0); mentioned later
//IND1S = iIND1 (NULL, 0, 0, MODE_SIGNAL, 0); mentioned later
IND2 = iIND2 (NULL, 0, 0, MODE_MAIN, 0); // how to go about these two?
IND2S = iIND2 (NULL, 0, 0, MODE_SIGNAL, 0); // how to go about these two?
Taking into consideration Guru's approach, [with some minor changes and fixed direction b/c it's misspelled all over the lesson ]
Code:
int Crossed (double line1 , double line2)
{
static int last_direction = 0;
static int current_direction = 0;
if(line1>line2)current_direction = 1; //up
if(line1<line2)current_direction = 2; //down
if(current_direction != last_direction) //changed
{
last_direction = current_direction;
return (last_direction);
}
else
{
return (0);
}
}
IND1 = iIND1 (NULL, 0, 0, MODE_MAIN, 0);
IND1S = iIND1 (NULL, 0, 0, MODE_SIGNAL, 0);
int isCrossed = Crossed (IND1 ,IND2);
if(isCrossed == 1)
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"My EA",12345,0,Green);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
}
else Print("Yikes! You're screwed! Get back to the manual!!! Ha! ",GetLastError());
return(0);
}
if(isCrossed == 2)
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,"My EA",12345,0,Red);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
}
else Print(""Yikes! You're screwed! Get back to the manual!!! Ha! "",GetLastError());
return(0);
}
return(0);
}