Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Nowhere without you - 6. - page 1094
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Do you manually place the EA on the charts and want it to open MaxOpenOrders orders or TOTAL MaxOpenOrders orders on EVERY chart? And what orders: just the market ones or all of them including limit and stop?
What is the purpose of the words "in this strategy"? Magic is needed if there are manual trades in the account, or if another EA is running, so that these orders are not touched. I think so.
It needs to open for all symbols. Suppose MaxOpenOrders is 1, it should open only one market order. This is a bot for binary options, there are no pending orders. However, it works in series, if the position is closed in a loss, then the same position is opened, only with a larger lot, and until the series ends with a profitable trade, no trades should be opened for other instruments.
OK, I missed the binary options part. For some reason it seems to me that if I manually pin the same EA to different charts, it will still count as running different EAs. And for EVERY chart 1 option will be run (well, when it works properly). I could be wrong as I'm a beginner myself. I have considered multicurrency in my EA in this way:
input string TradeSymbols = "EURUSD, GBPUSD, AUDUSD, NZDUSD, USDCAD, USDCHF, GBPCAD"; // symbols for trading
string Symbols[50]; // 50 is the maximum number of symbols
int SymbolCount;
//--------------------------------------------------
int OnInit()
{
if (IsTesting() || !ExtractSymbols())
{
SymbolCount = 1;
Symbols[0] = Symbol();
}
return(INIT_SUCCEEDED);
}
//--------------------------------------------------
bool ExtractSymbols()
{
ushort Comma = StringGetCharacter(",", 0);
SymbolCount = StringSplit(TradeSymbols, Comma, Symbols);
for (int i = 0; i < SymbolCount; i++)
{
StringToUpper(Symbols[i]);
Symbols[i] = StringTrimRight(Symbols[i]); // protection from accidental spaces
Symbols[i] = StringTrimLeft(Symbols[i]);
}
if (SymbolCount > 0) return(true);
return(false);
}
//--------------------------------------------------
void OnTick()
{
for (int i = 0; i < SymbolCount; i++)
{
double LastAsk = SymbolInfoDouble(Symbols[i], SYMBOL_ASK);
double LastBid = SymbolInfoDouble(Symbols[i], SYMBOL_BID);
// and so on...
}
}
To apply the EA to any chart. This way, MaxOpenOrders will be exactly for all charts.
OK, I missed the binary options part. For some reason it seems to me that if I manually pin the same EA to different charts, it will still count as running different EAs. And for EVERY chart 1 option will be run (well, when it works properly). I could be wrong as I'm a beginner myself. I have considered multicurrency in my EA in this way:
input string TradeSymbols = "EURUSD, GBPUSD, AUDUSD, NZDUSD, USDCAD, USDCHF, GBPCAD"; // symbols for trading
string Symbols[50]; // 50 is the maximum number of symbols
int SymbolCount;
//--------------------------------------------------
int OnInit()
{
if (IsTesting() || !ExtractSymbols())
{
SymbolCount = 1;
Symbols[0] = Symbol();
}
return(INIT_SUCCEEDED);
}
//--------------------------------------------------
bool ExtractSymbols()
{
ushort Comma = StringGetCharacter(",", 0);
SymbolCount = StringSplit(TradeSymbols, Comma, Symbols);
for (int i = 0; i < SymbolCount; i++)
{
StringToUpper(Symbols[i]);
Symbols[i] = StringTrimRight(Symbols[i]); // protection from accidental spaces
Symbols[i] = StringTrimLeft(Symbols[i]);
}
if (SymbolCount > 0) return(true);
return(false);
}
//--------------------------------------------------
void OnTick()
{
for (int i = 0; i < SymbolCount; i++)
{
double LastAsk = SymbolInfoDouble(Symbols[i], SYMBOL_ASK);
double LastBid = SymbolInfoDouble(Symbols[i], SYMBOL_BID);
// and so on...
}
}
To apply the EA to any chart. This way the MaxOpenOrders will be accurate for all charts.
And if the next week has gone, and I still need to keep a report from the opening price of the bar of Tuesday of last week? Ie I need an EA to determine the opening price of the bar on Tuesday and started from it as long as necessary, for example - a week, two, a month, etc.
Thank you.
What if next week has started and I still need to report from Tuesday's open bar price of last week? i.e. I need the Expert Advisor to determine the open price of the bar on Tuesday and use it for as long as necessary, for example, a week, two weeks, a month, etc.
Thank you.
Then you set a specific date and the program takes the opening price of the day for that date, it's even easier.
Then you set a specific date and the program takes the opening price of the day for that date, it's even easier.
Can you help me with the code, I don't get it.
Do you think that the calculation of the opening price of the first bar on Tuesday can be implemented in OnInit() and it will calculate the price on startup and remember it until the EA is restarted?
i am sorry if i am writing nonsense, i am just learning)
Can you help with the code, I don't get it.
Do you think the price of the first bar on Tuesday should be calculated in OnInit() and it will calculate the price on startup and remember it until the EA is restarted?
I am sorry if I am writing nonsense, I am just learning)
The opening price of the first bar of the day = the opening price of the daily bar. You can do it in Inite, if you don't need to recalculate the value later.
The code is approximately as follows:
Is this code written in MQL5?
for (int i=0; i<SymbolCount; i++)
{
if (CountTrades() == 0) // The number of orders must be zero
{
if (((TypeLastHistOrder() == OP_BUY && PriceCloseLastHistOrder(OP_BUY) < PriceOpenLastHistOrder(OP_BUY)) ||
(TypeLastHistOrder() == OP_SELL && PriceCloseLastHistOrder(OP_SELL) > PriceOpenLastHistOrder(OP_SELL))&& MaxOpenOrders > OrdersTotal())
// If the last trade is losing, the same trade will be opened, but with a larger lot
{
Type = TypeLastHistOrder();
if (Type == OP_BUY) Price = LastAsk;
if (Type == OP_SELL) Price = LastBid;
Lot = NormalizeDouble(LotsLastHistOrder()*Multiplier, 2);
ticket = OrderSend(Symbols[i], Type, Lot, Price, Slippage, 0, 0, IntegerToString(Exp), Magic);
}
if (PriceCloseLastHistOrder() == PriceOpenLastHistOrder() && CountHistTrades() > 0 && MaxOpenOrders > OrdersTotal())
// if the last trade's profit is equal to zero, the same trade will be opened
{
Type = TypeLastHistOrder();
if (Type == OP_BUY) Price = LastAsk;
if (Type == OP_SELL) Price = LastBid;
Lot = NormalizeDouble(LotsLastHistOrder(), 2);
ticket = OrderSend(Symbols[i], Type, Lot, Price, Slippage, 0, 0, IntegerToString(Exp), Magic)
}
if (((TypeLastHistOrder() == OP_BUY && PriceCloseLastHistOrder(OP_BUY) > PriceOpenLastHistOrder(OP_BUY))
|| (TypeLastHistOrder() == OP_SELL && PriceCloseLastHistOrder(OP_SELL) < PriceOpenLastHistOrder(OP_SELL))
|| CountHistTrades() == 0)// If the last trade is profitable, the order is opened
{
if (SignalBuy(Symbols[i]) && MaxOpenOrders > OrdersTotal())
{
ticket = OrderSend(Symbols[i], OP_BUY, Lots, Ask, Slippage, 0, 0, IntegerToString(Exp), Magic)
}
if (SignalSell(Symbols[i]) && MaxOpenOrders > OrdersTotal())
{
ticket = OrderSend(Symbols[i], OP_SELL, Lots, Bid, Slippage, 0, 0, IntegerToString(Exp), Magic)
}
}
}
}
Code in OnTick after words and so on. Written in MQL4, do you need MQL5?
For each OrderSend, the MaxOpenOrders condition should be no greater than OrdersTotal().
Procedure code (removed OrderSymbol()==Symbol() and entered iClose()/iOpen() instead of Close/Open), I didn't check code correctness:
//+------------------------------------------------------------------+
int CountTrades(int type = -1)
{
int cnt = 0;
for (int i=OrdersTotal()-1; i>=0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if (OrderMagicNumber() == Magic && (OrderType() == type || type == -1))
cnt++;
}
}
return(cnt);
}
//+------------------------------------------------------------------+
int CountHistTrades(int type = -1)
{
int cnt = 0;
for (int i=OrdersHistoryTotal()-1; i>=0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
{
if (OrderMagicNumber() == Magic && (OrderType() == type || type == -1))
cnt++;
}
}
return(cnt);
}
//+------------------------------------------------------------------+
bool SignalBuy(string Sym)
{
for (int i=1; i<=Wait; i++)
{
double C = iClose(Sym, PERIOD_M5, i); // Specify the required timeframe here
double O = iOpen(Sym, PERIOD_M5, i);
if(C > O) return(false);
}
if ((iBarShift(Sym, 0, TimeLastHistOrder()+Timeout) >= Wait || (Wait == 0 && TimeCurrent() >= TimeLastHistOrder()+Timeout))
&& CountHistTrades() > 0) return(true);
if (CountHistTrades() == 0) return(true);
return(false);
}
//+------------------------------------------------------------------+
bool SignalSell(string Sym)
{
for (int i=1; i<=Wait; i++)
{
double C = iClose(Sym, PERIOD_M5, i); // Specify the required timeframe here
double O = iOpen(Sym, PERIOD_M5, i);
if(C < O) return(false);
}
if ((iBarShift(Sym, 0, TimeLastHistOrder()+Timeout) >= Wait || (Wait == 0 && TimeCurrent() >= TimeLastHistOrder()+Timeout))
&& CountHistTrades() > 0) return(true);
if (CountHistTrades() == 0) return(true);
return(false);
}
//+------------------------------------------------------------------+
datetime TimeLastHistOrder(int type = -1)
{
datetime lasttime = 0;
datetime opentime = 0;
for (int i=OrdersHistoryTotal()-1; i>=0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
{
if (OrderMagicNumber() == Magic && (OrderType() == type || type == -1))
{
if (OrderCloseTime() > lasttime)
{
lasttime = OrderCloseTime();
opentime = OrderOpenTime();
}
}
}
}
return(opentime);
}
//+------------------------------------------------------------------+
int TypeLastHistOrder()
{
datetime time = 0;
int type = -1;
for (int i=OrdersHistoryTotal()-1; i>=0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
{
if (OrderMagicNumber() == Magic)
{
if (OrderCloseTime() > time)
{
time = OrderCloseTime();
type = OrderType();
}
}
}
}
return(type);
}
//+------------------------------------------------------------------+
double LotsLastHistOrder(int type = -1)
{
datetime time = 0;
double lots = 0;
for (int i=OrdersHistoryTotal()-1; i>=0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
{
if (OrderMagicNumber() == Magic && (OrderType() == type || type == -1))
{
if (OrderOpenTime() > time)
{
time = OrderOpenTime();
time = OrderLots();
}
}
}
}
return(lots);
}
//+------------------------------------------------------------------+
double PriceCloseLastHistOrder(int type = -1)
{
datetime time = 0;
double price = 0;
for (int i=OrdersHistoryTotal()-1; i>=0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
{
if (OrderMagicNumber() == Magic && (OrderType() == type || type == -1))
{
if (OrderCloseTime() > time)
{
time = OrderCloseTime();
price = OrderClosePrice();
}
}
}
}
return(price);
}
//+------------------------------------------------------------------+
double PriceOpenLastHistOrder(int type = -1)
{
datetime time = 0;
double price = 0;
for (int i=OrdersHistoryTotal()-1; i>=0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
{
if (OrderMagicNumber() == Magic && (OrderType() == type || type == -1))
{
if (OrderCloseTime() > time)
{
time = OrderCloseTime();
price = OrderOpenPrice();
}
}
}
}
return(price);
}