== and not =
Hi Alain,
I have been extremely busy with work so haven't been able to spend much on my code last week. Even when I add what you recommended, the code still opens multiple trades per currency pair.
Do you maybe have an idea why it does this?
Thank you,
Anton
{
int total,ord,i;
string symbol;
total = OrdersTotal();
for(i=0;i<total;i++)
{
OrderSelect(i,SELECT_BY_POS);
if(OrderSymbol() == Symbol())ord++;
}
if(ord>0); //Abort! A Position For This Pair is Already Open
//the rest of my program code
}
If you don't want you code to do anything else if a position is already open (so you don't want it to manage any open positions etc) then change this to:
{
int total,ord,i;
string symbol;
total = OrdersTotal();
for(i=0;i<total;i++)
{
OrderSelect(i,SELECT_BY_POS);
if(OrderSymbol() == Symbol())ord++;
}
if(ord>0) return; //Abort! A Position For This Pair is Already Open
//the rest of my program code
}
This does not stop your code. It will just roll right on past it, whatever the outcome of the "ord" test.
{
int total,ord,i;
string symbol;
total = OrdersTotal();
for(i=0;i<total;i++)
{
OrderSelect(i,SELECT_BY_POS);
if(OrderSymbol() == Symbol())ord++;
}
if(ord>0); //Abort! A Position For This Pair is Already Open
//the rest of my program code
}
If you don't want you code to do anything else if a position is already open (so you don't want it to manage any open positions etc) then change this to:
{
int total,ord,i;
string symbol;
total = OrdersTotal();
for(i=0;i<total;i++)
{
OrderSelect(i,SELECT_BY_POS);
if(OrderSymbol() == Symbol())ord++;
}
if(ord>0) return; //Abort! A Position For This Pair is Already Open
//the rest of my program code
}
Thanks a ton Honest!!
If I add the return; and link my code to multiple charts, for example: EURUSD and GBPUSD, then will the code trade the charts completely independent of each other? provided of course there is enough in my account balance to fund both?
Thanks a ton Honest!!
If I add the return; and link my code to multiple charts, for example: EURUSD and GBPUSD, then will the code trade the charts completely independent of each other? provided of course there is enough in my account balance to fund both?
Yes, each instance of the EA will be working independently unless you code it otherwise.
If you have one EURUSD order open:
- ord==1 on EURUSD chart so no more trades allowed on that chart.
- ord==0 on GBPUSD chart so trade allowed on that chart.
This is because you are testing for Symbol.
The only caveat: if you have 2 charts of the same symbol open (e.g. 2 x EURUSD charts) then they will only trade 1 order combined... in theory. You may find some oddities through processing delays if they are both are trading the same logic at the same time. This is where Magic Numbers could be useful.
If you want to trade multiple symbols from the same chart e.g. have only the EURUSD chart open but trade EURUSD and GBPUSD, you would need to make some changes.Yes, each instance of the EA will be working independently unless you code it otherwise.
If you have one EURUSD order open:
- ord==1 on EURUSD chart so no more trades allowed on that chart.
- ord==0 on GBPUSD chart so trade allowed on that chart.
This is because you are testing for Symbol.
The only caveat: if you have 2 charts of the same symbol open (e.g. 2 x EURUSD charts) then they will only trade 1 order combined... in theory. You may find some oddities through processing delays if they are both are trading the same logic at the same time. This is where Magic Numbers could be useful.
If you want to trade multiple symbols from the same chart e.g. have only the EURUSD chart open but trade EURUSD and GBPUSD, you would need to make some changes.double Leverage = AccountInfoInteger(ACCOUNT_LEVERAGE);
double LotSize = ((((AccountBalance/Ask)*Leverage) - 1)/100000)/2;
- You can't use any predefined variables,
can't use the tester, must poll
(not OnTick,) and usually other problems, e.g A problem with
iBarShift - MQL4 and MetaTrader 4 - MQL4 programming forum - Page
2
- Code it to trade the chart pair only. Look at the others if you must. (Don't assume that Time[i] == iTime(otherPair,tf, i) always use iBarShift.)
- Then put it on other charts to trade the other pairs. Done.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi Guys,
I am trying to add some code to my EA so that it only has one trade open per currency pair at a time.
I found this code online and tried it but I'm now getting the following errors:
Could anyone possibly give me advice on how to resolve this issue?
{
int total,ord,i;
string symbol;
total = OrdersTotal();
for(i=0;i<total;i++)
{
OrderSelect(i,SELECT_BY_POS);
if(OrderSymbol() = Symbol())ord++;
}
if(ord>0); //Abort! A Position For This Pair is Already Open
//the rest of my program code
}
Thanks in advance!
Anton