Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Nowhere without you - 6. - page 1095
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
extern int Exp = 1;
ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, 0, 0, IntegerToString(Exp), Magic);
Does this code work for you? The date must be here, and greater than the current date by 10 minutes (i.e. TimeCurrent() + 600 minimum).
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);
}
Gives out a couple of errors, couldn't fix it. What's wrong here?
//+------------------------------------------------------------------+Guys, help. I am trading on MT4 platform from Stforex. When I open an order, it does not show me the entry level on the chart (no dotted line), and therefore not convenient to put a stop-loss. Maybe who knows what's wrong and how to fix it?
http://prntscr.com/chfa36
Settings - Charts - Show trading levels
Guys, what function could return error 65 ?
http://prntscr.com/chfa36
Settings - Charts - Show trading levels
This function was originally switched on, but the levels still do not show on the graph
Makes a couple of errors, can't get it right. What's wrong here?
You forgot to add a closing parenthesis in OnTick and LastAsk and LastBid variables. Here's code that compiles without errors, I haven't checked its functionality:
//+------------------------------------------------------------------+
//| BB1.mq4 |
//| Copyright 2016, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
extern double Lots = 1; // Lots
extern int Exp = 1; // Expiration
extern int Wait = 2; // Number of candlesticks of one direction
extern int Timeout = 1; // Time slot
extern double Multiplier = 3; // Multiplier
extern int Slippage = 5; // Slippage
extern int Magic = 774274; // Magic
extern int MaxOpenOrders = 1; // Maximum number of orders
int ticket, Type, SymbolCount;
double Price, Lot;
input string TradeSymbols = "EURUSD_OP, GBPUSD_OP, AUDUSD_OP, NZDUSD_OP, USDCAD_OP, USDCHF_OP, GBPCAD_OP, AUDNZD_OP, CHFJPY_OP, GBPCHF_OP"; // symbols for trading
string Symbols[50]; // 50 is maximum possible number of symbols
//--------------------------------------------------
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);
if (CountTrades() == 0) // number of orders must be equal to 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, open order
{
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)
}
}
}
}
}
//+------------------------------------------------------------------+
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);
}
Hello!
How can you determine if a position has closed on TP? I don't think OrderClosePrice()==OrderTakeProfit() would be correct to use.
Hello!
How can you determine if a position is closed on TP? I don't think OrderClosePrice()==OrderTakeProfit() would be correct to use.
In the order comment look for [tp] if there is...