Posting in the forum after long time.
I've been working on a strategy and it has evolved gradually after a few months of experiment.
But here i am stuck at not being able to move ahead cause of lack of coding knowledge. Kindly assist me. TIA.
So the basic is that i want to open a set of trades as and when they happen based on certain conditions becoming true. This is a multi-pair EA.
1. There is a particular trade with a set trade comment ("Maxor_M") and magic number (415) that is checked initially. If it is open, the other conditions are checked, but if it is not open then nothing goes ahead. (for discussion, lets call this trade as X)
2. If X is open for the particular pair, for a set type, then i need to check another condition if another kind of trade is open or not. (call this another kind of trade as Y) For this, i need to compare the open time of X with that of Y. Here, there are two kinds of Y. One is an open Y trade and the other is a closed Y trade.
3. X's open time has to be greater than both the Y's, open Y (Yo) and closed Y (Yc). Thus XOOT (X's OrderOpenTime) needs to be greater than oYOOT and cYOCT (open Y's OrderOpenTime & closed Y's OrderCloseTime).
4. If either of the conditions are not met, no new trade is open. Therefore
if(XOOT < oYOOT || XOOT < cYOCT) DoNothing;
else(open new X trade).
I've been trying to make this code work and have done all know possiable permutation and combinations of it but to no avail. I even made two different approaches in code to get this thing going. I'm attaching below both the code snippets. Kindly look into it and suggest the best way to make it work.
Code Snip 1
Code: Select all
void CheckTrades()
{
string NUL = symbol;
if(MaxM_Stack) { //just a switch
if(CIO(1,NUL,CopyMN,8)) { //checking to see if condition is returned as true for Buy
OpenBuy(NUL,MagicNumber,TradeComment4); } //open Buy if true
if(CIO(0,NUL,CopyMN,8)) { //checking to see if condition is returned as true for Sell
OpenSell(NUL,MagicNumber,TradeComment4); } //open Sell if true
}
return (0);
}
bool CIO (int t, string symbol, int MN, int z)
{
int Tot = OrdersHistoryTotal();
int Tots = OrdersTotal();
int OOTMM,OCTSMH,OOTSMO; //even used data type as datetime and static datetime, not sure.
int type = OP_BUY;
if(t == 0) type = OP_SELL;
bool retrn = true; //default is true - i guess this is wrong, not sure!
for (int i = 0; i < Tots; i ++) { //checking open trades for X
if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
if(OrderComment() == "Maxor_M" && OrderType() == type && OrderMagicNumber() == MN && OrderSymbol() == symbol) {
OOTMM = OrderOpenTime(); //capturing X's open time for future comparisions
//Alert(symbol, " - ",OOTMM," - ", OrderComment(),Tot," - ",Tots); //just for checks
for (int x = Tot-1; x >= 0; x--) { //checking history for Y's close time
if(!OrderSelect(x, SELECT_BY_POS, MODE_HISTORY)) continue;
if(OrderType() == type && OrderMagicNumber() == MagicNumber && OrderSymbol() == symbol) {
OCTSMH = OrderCloseTime(); //capturing Y's close time for comparision
if(OCTSMH > OOTMM) retrn = false; //converts default bool into false if condition not met
break; } } //not sure about using break here. have tried without it as well
for (int a = Tots-1; a >= 0; a--) { //checking open trades for Y
if(!OrderSelect(a, SELECT_BY_POS, MODE_TRADES)) continue;
if(OrderType() == type && OrderMagicNumber() == MagicNumber && OrderSymbol() == symbol) {
OOTSMO = OrderOpenTime();
if(OOTSMO > OOTMM) retrn = false; //converts default bool into false if condition not met
break; } }
break;
}
}
return(retrn);
}COde Snip 2
Code: Select all
void CheckTrades()
{
string NUL = symbol;
int OOTMM,OCTSMH,OOTSMO;
if(MaxM_Stack) { //just a switch
OOTMM = CIO(1,NUL,CopyMN,0); //check open time of X - for buying conditions
if(OOTMM > 0) { //if open time of X is > 0, then X is open
OOTSMO = CIO(1,NUL,MagicNumber,1); //check open time of open Y
OCTSMH = CIOO(1,NUL,MagicNumber,2); //check close time of closed Y
if(OOTMM > OCTSMH || (OOTMM > OOTSMO || OOTSMO == 0)) { //check if both conditions are met
OpenBuy(NUL,MagicNumber,TradeComment4); } } // open a Buy trade
OOTMM = CIO(0,NUL,CopyMN,0);
if(OOTMM > 0) { //same as above - for sell conditions
OOTSMO = CIO(0,NUL,MagicNumber,1);
OCTSMH = CIOO(0,NUL,MagicNumber,2);
if(OOTMM > OCTSMH || (OOTMM > OOTSMO || OOTSMO == 0)) {
OpenSell(NUL,MagicNumber,TradeComment4); } } //open a sell trade
}
return (0);
}
int CIO (int t, string symbol, int MN, int z)
{
int Tots = OrdersTotal();
int OOTMMx;
int type = OP_BUY;
if(t == 0) type = OP_SELL;
for (int i = Tots -1; i >= 0; i --) { //checking all open trades
if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
if(OrderType() == type && OrderMagicNumber() == MN && OrderSymbol() == symbol) {
if(z == 0 && OrderComment() == "Maxor_M") OOTMMx = OrderOpenTime(); //checking for open X
else if(z == 1) OOTMMx = OrderOpenTime(); //checking for open Y
else(OOTMMx == 0); // returning 0 if either X is not open or Y is not open
} }
return(OOTMMx);
}
int CIOO (int t, string symbol, int MN, int z)
{
int Tots = OrdersHistoryTotal();
int OOTMMx;
int type = OP_BUY;
if(t == 0) type = OP_SELL;
for (int i = Tots - 1; i >= 0; i --) { //checking all open trades
if(!OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) continue;
if(OrderType() == type && OrderMagicNumber() == MN && OrderSymbol() == symbol) {
OOTMMx = OrderCloseTime(); break; } //checking for closed Y
}
return(OOTMMx);
}
I know what i am trying to achieve is not that difficult. It is just my lack of coding knowledge that is preventing me from executing this successfully.
Anyone willing to lend me a helping hand do kindly get back to me with queries that you might have.
Regards, Max.