Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Nowhere without you - 6. - page 1096

 
AlexeyVik:

In the order commentary look for [tp] if there is one...


Thank you.
 

Hello Profi.

help fix the error in the indicator.....

indicator is working.

The signal is triggered on bar 0.

But when other ticks are received on the current bar, it triggers again and gives new signals.

I need it to put this signal in a circle in the place of the current event price when Sell or Buy - event occurs, and no other events were processed on this open bar.

I want to avoid new signals and circles on this bar.

________________________

I understand that you will say it is nonsense, it is false signals, etc., but this is exactly what I need.

I know I should create a flag, a marker, etc. - but I have no time to study the language myself.

_________________________

I would be very grateful if you could add such a flag to the attached file and mark the added lines with the color.....

Thank you, and I hope someone will respond

Files:
 
alvlaf:

Makes a couple of errors, can't get it right. What's wrong here?

//+------------------------------------------------------------------+
//| 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++)

{

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, 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);

}

Alexander, it is even worse than before. Besides the fact that it opens two different symbols, it manages to open several positions simultaneously for one and the same symbol. Somewhere you and I have gone a bit overboard.
 

Please help me to trade on the news. I am in need of an EA which has two main functions.

The first function: placingpending orders with a given TP and SL, at a given time at a given level of price.
The second function: to keep the pending orders parallel to the price. It means to move them together with the price (it is so bothersome to do it manually). At a given time, at the moment of news publication, we have to stop this action and wait until the price touches one of the orders. The second order is immediately deleted after that.

Once again:
2 minutes before the news release, put two opposite stop orders at a distance of 10 points from the price, immediately activate the function of moving parallel to the price, 2 minutes move together with the price, one order runs away from the price, the second one catches up with the price. At the news release, you should disable the function of pending order movement and then wait for the breakdown of one of the orders. The second order is deleted immediately after the breakdown of the first one.

Variable variables that are changed in properties of an EA (approximately as I imagine, I do not know what the realization is in reality)

TP - 600 (by default)

Trall - 300 (default)

SL - 100 (default)

Pips - 100 (distance from price by default)

Slippage - 100 (slippage in pips by default)

Lot - 0.1

Risk - 10 //percent (well, it is not necessary, I will ask it later if necessary)

TimeNews1On - true (use cells below to enter time, false - advisor does not trade them)

TimeNewsHour - hour (news release time 1)

TimeNewsMin - minutes (news release time 1)

TimeNews2On - false (by default)

TimeNewsHour - hours (news release time 2)

TimeNewsMin - minutes (news release time 2)

TimeNews3On - false

TimeNewsHour - hours (news release time 3)

TimeNewsMin - minutes (news release time 3)

TimeNews4On - false

TimeNewsHour - hours (news release time 4)

TimeNewsMin - minutes (news release time 4)

TimeNews5On - false

TimeNewsHour - hours (news release time 5)

TimeNewsMin - minutes (news release time 5)


Insta, five digits.


I am doing all this manually, it is too much trouble, and now I have no time to work out the news, I want to place the Expert Advisor in the morning or at night on pairs where there will be news on that day, so they will all work out.
--------------------------------------

If you know such an expert, please share.

Thank you in advance!

 
alvlaf:

Let's go back to your source code. Try it like this:

int OnInit()

{

if (!GlobalVariableCheck("AllowNewOrders")) GlobalVariableSet("AllowNewOrders",1);

return(INIT_SUCCEED);

}

void OnDeInit()

{

GlobalVariableSet("AllowNewOrders",1);

}

void OnTick()

{

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))

// If the last trade is losing, the same one is opened, but with bigger lot

{

GlobalVariableSet("AllowNewOrders", 0);

Type = TypeLastHistOrder();

if (Type == OP_BUY) Price = Ask;

if (Type == OP_SELL) Price = Bid;

Lot = NormalizeDouble(LotsLastHistOrder()*Multiplier, 2);

ticket = OrderSend(Symbol(), Type, Lot, Price, Slippage, 0, 0, IntegerToString(Exp), Magic);

}

if (PriceCloseLastHistOrder() == PriceOpenLastHistOrder() && CountHistTrades() > 0)

// if last trade's profit is equal to zero, the same trade will be opened

{

GlobalVariableSet("AllowNewOrders", 0);

Type = TypeLastHistOrder();

if (Type == OP_BUY) Price = Ask;

if (Type == OP_SELL) Price = Bid;

Lot = NormalizeDouble(LotsLastHistOrder(), 2);

ticket = OrderSend(Symbol(), 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 (GlobalVariableGet("AllowNewOrders") > 0)return;

if (SignalBuy() && MaxOpenOrders > OrdersTotal())

{

ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, 0, 0, IntegerToString(Exp), Magic);

if (ticket>0) GlobalVariableSet("AllowNewOrders", 0);

}

if (SignalSell() && MaxOpenOrders > OrdersTotal())

{

ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, 0, 0, IntegerToString(Exp), Magic);

if (ticket>0) GlobalVariableSet("AllowNewOrders", 0);

}

}

}

}

I haven't checked it, because I don't understand how multiple copies of an EA with a total limit of 1 order will determine on which symbol (after a profitable series) it is better to start trading?

 
BooGUY:


Please help to trade on the news. Really need an EA that should have two main functions.

First function: placingpending orders with a given TP and SL, at a given time at a given level from the price.
The second function: to keep the pending orders parallel to the price. It means to move them together with the price (it is so bothersome to do it manually). At a given time, at the moment of news publication, we have to stop this action and wait until the price touches one of the orders. The second order is immediately deleted after that.

Once again:
2 minutes before the news release, put two opposite stop orders at a distance of 10 points from the price, immediately activate the function of moving parallel to the price, 2 minutes move together with the price, one order runs away from the price, the second one catches up with the price. At the news release, you should disable the function of pending order movement and then wait for the breakdown of one of the orders. The second one will be deleted immediately after the breakdown of the first one.

If you know or have met such advisors, please share


Thank you in advance!

I can send you the code for reading news from a file, it may come in handy. Once a week, you look at the economic calendar and enter the data into the file folder_with_terminal\MQL4\Files\novosti.txt of the news that you should pay attention to, the advisor can be activated even just before the news (you can use this advisor to track the beginning of the American session, for example - in case there is a strong movement). I have not finished the trading code because I was seriously "cheated" a couple of times by the news :)) The latest version placed six orders (3 buy stops + 3 sell stops) at different distances with different Take Profit. Anyway, my news code works, but I have to improve my trading code.

#property strict

input string NewsFilename = "novosti.txt"; // Name of the text file containing news

input int MinutesBefore = 5; // How many minutes prior to news publication should the orders be placed

input int MinutesAfter1 = 15; // how many minutes after the news release remove orders of the 1st group

input int MinutesAfter2 = 30; // how many minutes after the news release remove Group 2 orders

input int MinutesAfter3 = 30; // how many minutes after news release remove Group 3 orders

input int PriceIndent1 = 15; // Indent from the price in points

input int PriceIndent2 = 5; // Step back from the previous TakeProfit in points

input int TakeProfit1 = 40; // TakeProfit size for the 1st group in points

input int TakeProfit2 = 60; // TakeProfit size for the 2nd group in points

input int TakeProfit3 = 100; // TakeProfit size for the 3rd group in points

input int StopLoss = 20; // StopLoss size for all orders in points

input double Multiplier1 = 1.0; // Coefficient to the order lot of the 2nd group

input double Multiplier2 = 2.0; // coefficient to the order lots of the 3rd group

input int MaximalRisk = 10; // Maximum risk in percents

//====================================================================================================

datetime Times[200]; // LastTime;

string Symbols[200];

bool NewsPassed[200];

int NewsCount;

//====================================================================================================

int OnInit()

{

// Loading fresh news

if (!LoadNews()) return(INIT_FAILED);

return(INIT_SUCCEEDED);

}

//====================================================================================================

void OnTick()

{

if (!IsTradeAllowed()) return;

datetime TestTime = iTime(Symbol(), PERIOD_H1, 0);

// Updating of news at the beginning of each day

if (TimeDay(TestTime) != TimeDay(LastTime))

{

LastTime = TestTime;

if (!LoadNews())

{

Alert("Update news error!");

return;

}

}

//-------------------------------------------------------------------------------------------------

for (int i = 0; i < NewsCount; i++)

{

// Till news release put orders for breakthrough

if (Times[i] > TimeCurrent() && Times[i] - TimeCurrent() <= MinutesBefore * 60 && !NewsPassed[i])

{

PrintFormat("The current time is %s, it's time to set currency orders - %s", TimeToStr(TimeCurrent(), TIME_DATE|TIME_MINUTES), Symbols[i]);

SetupOrders(Symbols[i];

NewsPassed[i] = true;

}

}

//-------------------------------------------------------------------------------------------------

}

//====================================================================================================

bool LoadNews()

{

int Count, Tick = 0;

string Str, Data[40];

ResetLastError();

if (!FileIsExist(NewsFilename, 0))

{

PrintFormat("The news file %s does not exist.", NewsFilename);

Print("The file must be in the folder \\MQL4\\Files, in case of testing - in the folder \\Tester\\Files.");

return(false);

}

int Handle = FileOpen(NewsFilename, FILE_READ|FILE_SHARE_READ|FILE_TXT);

if (Handle != INVALID_HANDLE)

{

while (!FileIsEnding(Handle))

{

Str = FileReadString(Handle);

Count = StringSplit(Str, StringGetChar(" ", 0), Data);

for (int i = 1; i < Count; i = i + 2)

{

Times[Tick] = StringToTime(Data[0] + " + Data[i]);

Symbols[Tick] = Data[i + 1];

Tick++;

}

}

FileClose(Handle);

NewsCount = Tick;

for (int i = 0; i < NewsCount; i++)

{

if (Times[i] > TimeCurrent()) NewsPassed[i] = false;

else NewsPassed[i] = false;

}

PrintFormat("Number of news uploaded - %i.", NewsCount);

return(true);

}

PrintFormat("Can't open file %s. Error code %d.", NewsFilename, GetLastError());

return(false);

}

//====================================================================================================

void SetupOrders(string Sym)

{

int Ticket, Dig, Count;

double Price, TP, SL, Lot;

datetime KeepAlive = TimeCurrent() + (MinutesBefore + MinutesAfter1) * 60;

string SymList[7];

//-------------------------------------------------------------------------------------------------

if (Sym == "AUD")

{

SymList[0] = "AUDCAD"; SymList[1] = "AUDCHF"; SymList[2] = "AUDJPY"; SymList[3] = "AUDNZD";

SymList[4] = "AUDUSD"; SymList[5] = "EURAUD"; SymList[6] = "GBPAUD";

}

//-------------------------------------------------------------------------------------------------

if (Sym == "CAD")

{

SymList[0] = "AUDCAD"; SymList[1] = "CADCHF"; SymList[2] = "CADJPY"; SymList[3] = "EURCAD";

SymList[4] = "GBPCAD"; SymList[5] = "NZDCAD"; SymList[6] = "USDCAD";

}

//-------------------------------------------------------------------------------------------------

if (Sym == "CHF")

{

SymList[0] = "AUDCHF"; SymList[1] = "CADCHF"; SymList[2] = "EURCHF"; SymList[3] = "GBPCHF";

SymList[4] = "CHFJPY"; SymList[5] = "NZDCHF"; SymList[6] = "USDCHF";

}

//-------------------------------------------------------------------------------------------------

if (Sym == "EUR")

{

SymList[0] = "EURAUD"; SymList[1] = "EURCAD"; SymList[2] = "EURCHF"; SymList[3] = "EURGBP";

SymList[4] = "EURJPY"; SymList[5] = "EURNZD"; SymList[6] = "EURUSD";

}

//-------------------------------------------------------------------------------------------------

if (Sym == "GBP")

{

SymList[0] = "EURGBP"; SymList[1] = "GBPAUD"; SymList[2] = "GBPCAD"; SymList[3] = "GBPCHF";

SymList[4] = "GBPJPY"; SymList[5] = "GBPNZD"; SymList[6] = "GBPUSD";

}

//-------------------------------------------------------------------------------------------------

if (Sym == "JPY")

{

SymList[0] = "AUDJPY"; SymList[1] = "CADJPY"; SymList[2] = "CHFJPY"; SymList[3] = "EURJPY";

SymList[4] = "GBPJPY"; SymList[5] = "NZDJPY"; SymList[6] = "USDJPY";

}

//-------------------------------------------------------------------------------------------------

if (Sym == "NZD")

{

SymList[0] = "AUDNZD"; SymList[1] = "EURNZD"; SymList[2] = "GBPNZD"; SymList[3] = "NZDCAD";

SymList[4] = "NZDCHF"; SymList[5] = "NZDJPY"; SymList[6] = "NZDUSD";

}

//-------------------------------------------------------------------------------------------------

if (Sym == "USD")

{

SymList[0] = "AUSUSD"; SymList[1] = "EURUSD"; SymList[2] = "GBPUSD"; SymList[3] = "NZDUSD";

SymList[4] = "USDCAD"; SymList[5] = "USDCHF"; SymList[6] = "USDJPY";

}

//-------------------------------------------------------------------------------------------------

for (int i = 0; i < 7; i++)

{

Count = 0;

Dig = int(MarketInfo(SymList[i], MODE_DIGITS));

Lot = NormalizeDouble(0.01, 2);

//Buy

KeepAlive = TimeCurrent() + (MinutesBefore + MinutesAfter1) * 60;

Price = NormalizeDouble(MarketInfo(SymList[i], MODE_ASK) + PriceIndent1 / pow(10, Dig-1), Dig);

TP = NormalizeDouble(Price + TakeProfit1 / pow(10, Dig-1), Dig);

SL = NormalizeDouble(Price - StopLoss / pow(10, Dig-1), Dig);

Ticket = OrderSend(SymList[i], OP_BUYSTOP, Lot, Price, 5, SL, TP, NULL, 0, KeepAlive, clrLimeGreen);

if (Ticket > 0) Count++; else PrintFormat("Error creating the order %i, symbol %s, price %f, Take %f, Stop %f, Lot %f.", GetLastError(), SymList[i], Price, TP, SL, Lot);

KeepAlive = TimeCurrent() + (MinutesBefore + MinutesAfter2) * 60;

Price = NormalizeDouble(TP + PriceIndent2 / pow(10, Dig-1), Dig);

TP = NormalizeDouble(Price + TakeProfit2 / pow(10, Dig-1), Dig);

SL = NormalizeDouble(Price - StopLoss / pow(10, Dig-1), Dig);

Ticket = OrderSend(SymList[i], OP_BUYSTOP, Lot * Multiplier1, Price, 5, SL, TP, NULL, 0, KeepAlive, clrLimeGreen);

if (Ticket > 0) Count++; else PrintFormat("Error creating the order %i, symbol %s, price %f, Take %f, Stop %f, Lot %f.", GetLastError(), SymList[i], Price, TP, SL, Lot*Multiplier1);

KeepAlive = TimeCurrent() + (MinutesBefore + MinutesAfter3) * 60;

Price = NormalizeDouble(TP + PriceIndent2 / pow(10, Dig-1), Dig);

TP = NormalizeDouble(Price + TakeProfit2 / pow(10, Dig-1), Dig);

SL = NormalizeDouble(Price - StopLoss / pow(10, Dig-1), Dig);

Ticket = OrderSend(SymList[i], OP_BUYSTOP, Lot*Multiplier2, Price, 5, SL, TP, NULL, 0, KeepAlive, clrLimeGreen);

if (Ticket > 0) Count++; else PrintFormat("Error creating the order %i, symbol %s, price %f, take %f, stop %f, lot %f.", GetLastError(), SymList[i], Price, TP, SL, Lot*Multiplier2);

// Selling

KeepAlive = TimeCurrent() + (MinutesBefore + MinutesAfter1) * 60;

Price = NormalizeDouble(MarketInfo(SymList[i], MODE_BID) - PriceIndent1 / pow(10, Dig-1), Dig);

TP = NormalizeDouble(Price - TakeProfit1 / pow(10, Dig-1), Dig);

SL = NormalizeDouble(Price + StopLoss / pow(10, Dig-1), Dig);

Ticket = OrderSend(SymList[i], OP_SELLSTOP, Lot, Price, 5, SL, TP, NULL, 0, KeepAlive, clrOrangeRed);

if (Ticket > 0) Count++; else PrintFormat("Error creating the order %i, symbol %s, price %f, Take %f, Stop %f, Lot %f.", GetLastError(), SymList[i], Price, TP, SL, Lot);

KeepAlive = TimeCurrent() + (MinutesBefore + MinutesAfter2) * 60;

Price = NormalizeDouble(TP - PriceIndent2 / pow(10, Dig-1), Dig);

TP = NormalizeDouble(Price - TakeProfit2 / pow(10, Dig-1), Dig);

SL = NormalizeDouble(Price + StopLoss / pow(10, Dig-1), Dig);

Ticket = OrderSend(SymList[i], OP_SELLSTOP, Lot, Price, 5, SL, TP, NULL, 0, KeepAlive, clrOrangeRed);

if (Ticket > 0) Count++; else PrintFormat("Error creating the order %i, symbol %s, price %f, Take %f, Stop %f, Lot %f.", GetLastError(), SymList[i], Price, TP, SL, Lot*Multiplier1);

KeepAlive = TimeCurrent() + (MinutesBefore + MinutesAfter3) * 60;

Price = NormalizeDouble(TP - PriceIndent2 / pow(10, Dig-1), Dig);

TP = NormalizeDouble(Price - TakeProfit3 / pow(10, Dig-1), Dig);

SL = NormalizeDouble(Price + StopLoss / pow(10, Dig-1), Dig);

Ticket = OrderSend(SymList[i], OP_SELLSTOP, Lot, Price, 5, SL, TP, NULL, 0, KeepAlive, clrOrangeRed);

PrintFormat("There are %i orders placed by the symbol %s", SymList[i], Count);0 }

}

The news file looks like this:

15.09.2016 01:45 NZD 04:30 AUD 10:30 CHF 14:00 GBP 15:30 USD

16.09.2016 15:30 USD 17:00 USD

and so on...

 
A13ksandr:

Let's go back to your source code. Try it like this:

int OnInit()

{

if (!GlobalVariableCheck("AllowNewOrders")) GlobalVariableSet("AllowNewOrders",1);

return(INIT_SUCCEED);

}

void OnDeInit()

{

GlobalVariableSet("AllowNewOrders",1);

}

void OnTick()

{

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))

// If the last trade is losing, the same one is opened, but with bigger lot

{

GlobalVariableSet("AllowNewOrders", 0);

Type = TypeLastHistOrder();

if (Type == OP_BUY) Price = Ask;

if (Type == OP_SELL) Price = Bid;

Lot = NormalizeDouble(LotsLastHistOrder()*Multiplier, 2);

ticket = OrderSend(Symbol(), Type, Lot, Price, Slippage, 0, 0, IntegerToString(Exp), Magic);

}

if (PriceCloseLastHistOrder() == PriceOpenLastHistOrder() && CountHistTrades() > 0)

// if last trade's profit is equal to zero, the same trade will be opened

{

GlobalVariableSet("AllowNewOrders", 0);

Type = TypeLastHistOrder();

if (Type == OP_BUY) Price = Ask;

if (Type == OP_SELL) Price = Bid;

Lot = NormalizeDouble(LotsLastHistOrder(), 2);

ticket = OrderSend(Symbol(), 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 (GlobalVariableGet("AllowNewOrders") > 0)return;

if (SignalBuy() && MaxOpenOrders > OrdersTotal())

{

ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, 0, 0, IntegerToString(Exp), Magic);

if (ticket>0) GlobalVariableSet("AllowNewOrders", 0);

}

if (SignalSell() && MaxOpenOrders > OrdersTotal())

{

ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, 0, 0, IntegerToString(Exp), Magic);

if (ticket>0) GlobalVariableSet("AllowNewOrders", 0);

}

}

}

}

I haven't checked it, because I don't understand how several copies of an EA with a total limit of 1 order will detect at which symbol (after a profitable series) it is better to start trading?

Alexander, the bot has to wait for the specified number of candles in each symbol in one direction and then start a series, that is, at which symbol this condition is triggered first for that symbol and open the first trade. At the same time, you should prohibit opening a series for other symbols until the first series finishes with profit.

The last code you have given is a step forward - it does not open any deal at all (i.e., the ban is active). I have not understood why none at all. Attached is a screenshot of the log.

 

Maybe it's time to learn how to insert code!

int OnInit()

{

   if (!GlobalVariableCheck("AllowNewOrders")) GlobalVariableSet("AllowNewOrders",1);

   return(INIT_SUCCEEDED);

}

void  OnDeInit()

{

   GlobalVariableSet("AllowNewOrders",1); 

} 

void OnTick()

{

   if (CountTrades() == 0) // Количество ордеров должно равняться нулю

   {

      if ((TypeLastHistOrder() == OP_BUY && PriceCloseLastHistOrder(OP_BUY) < PriceOpenLastHistOrder(OP_BUY))

      || (TypeLastHistOrder() == OP_SELL && PriceCloseLastHistOrder(OP_SELL) > PriceOpenLastHistOrder(OP_SELL)))

      // Если последняя сделка убыточная, то открывается такая же, но с увеличенным лотом

      {

         GlobalVariableSet("AllowNewOrders", 0);

         Type = TypeLastHistOrder();

         if (Type == OP_BUY)  Price = Ask;

         if (Type == OP_SELL) Price = Bid;

         Lot = NormalizeDouble(LotsLastHistOrder()*Multiplier, 2);

         ticket = OrderSend(Symbol(), Type, Lot, Price, Slippage, 0, 0, IntegerToString(Exp), Magic);

      }

      if (PriceCloseLastHistOrder() == PriceOpenLastHistOrder() && CountHistTrades() > 0) 

      // Если прибыль последней сделки равняется нулю, то открывается такая же

      {

         GlobalVariableSet("AllowNewOrders", 0);

         Type = TypeLastHistOrder();

         if (Type == OP_BUY)  Price = Ask;

         if (Type == OP_SELL) Price = Bid;

         Lot = NormalizeDouble(LotsLastHistOrder(), 2);

         ticket = OrderSend(Symbol(), 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 (GlobalVariableGet("AllowNewOrders") > 0) return;

         if (SignalBuy() && MaxOpenOrders > OrdersTotal())

         {

            ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, 0, 0, IntegerToString(Exp), Magic);

            if (ticket>0) GlobalVariableSet("AllowNewOrders", 0);

         }

         if (SignalSell() && MaxOpenOrders > OrdersTotal())

         {

            ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, 0, 0, IntegerToString(Exp), Magic);

            if (ticket>0) GlobalVariableSet("AllowNewOrders", 0);

         }

      }

   }

} 
 
abeiks: Maybe it's time to learn how to insert code!
int OnInit()                     // ИХМО так нагляднее
{
   if (!GlobalVariableCheck("AllowNewOrders")) GlobalVariableSet("AllowNewOrders",1);
   return(INIT_SUCCEEDED);
}

void  OnDeInit()
{
   GlobalVariableSet("AllowNewOrders",1); 
} 

void OnTick()
{
   if (CountTrades() == 0) // Количество ордеров должно равняться нулю
   {
      if ((TypeLastHistOrder() == OP_BUY && PriceCloseLastHistOrder(OP_BUY) < PriceOpenLastHistOrder(OP_BUY))
      || (TypeLastHistOrder() == OP_SELL && PriceCloseLastHistOrder(OP_SELL) > PriceOpenLastHistOrder(OP_SELL)))
      // Если последняя сделка убыточная, то открывается такая же, но с увеличенным лотом
      {
         GlobalVariableSet("AllowNewOrders", 0);
         Type = TypeLastHistOrder();
         if (Type == OP_BUY)  Price = Ask;
         if (Type == OP_SELL) Price = Bid;
         Lot = NormalizeDouble(LotsLastHistOrder()*Multiplier, 2);
         ticket = OrderSend(Symbol(), Type, Lot, Price, Slippage, 0, 0, IntegerToString(Exp), Magic);
      }

      if (PriceCloseLastHistOrder() == PriceOpenLastHistOrder() && CountHistTrades() > 0) 

      // Если прибыль последней сделки равняется нулю, то открывается такая же
      {
         GlobalVariableSet("AllowNewOrders", 0);
         Type = TypeLastHistOrder();
         if (Type == OP_BUY)  Price = Ask;
         if (Type == OP_SELL) Price = Bid;
         Lot = NormalizeDouble(LotsLastHistOrder(), 2);
         ticket = OrderSend(Symbol(), 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 (GlobalVariableGet("AllowNewOrders") > 0) return;
         if (SignalBuy() && MaxOpenOrders > OrdersTotal())
         {
            ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, 0, 0, IntegerToString(Exp), Magic);
            if (ticket>0) GlobalVariableSet("AllowNewOrders", 0);
         }

         if (SignalSell() && MaxOpenOrders > OrdersTotal())
         {
            ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, 0, 0, IntegerToString(Exp), Magic);
            if (ticket>0) GlobalVariableSet("AllowNewOrders", 0);
         }
      }
   }
}
 
alvlaf:

The last code you gave is a step ahead - it doesn't open any trades at all (so the ban is in effect). I have not yet understood why none at all. Attached is a screenshot of the log.

At first, the variable was called ProfitableSerie and had opposite function. And I have something else to add.

int OnInit()                     // abeiks && LRA: замечания приняты)))
{
   if (!GlobalVariableCheck("AllowNewOrders")) GlobalVariableSet("AllowNewOrders",1);
   return(INIT_SUCCEEDED);
}

// OnDeinit больше не нужен

void OnTick()
{
   if (CountTrades() == 0) // Количество ордеров должно равняться нулю
   {
      if ((TypeLastHistOrder() == OP_BUY && PriceCloseLastHistOrder(OP_BUY) < PriceOpenLastHistOrder(OP_BUY))
      || (TypeLastHistOrder() == OP_SELL && PriceCloseLastHistOrder(OP_SELL) > PriceOpenLastHistOrder(OP_SELL)))
      // Если последняя сделка убыточная, то открывается такая же, но с увеличенным лотом
      {
         GlobalVariableSet("AllowNewOrders", 0);
         Type = TypeLastHistOrder();
         if (Type == OP_BUY)  Price = Ask;
         if (Type == OP_SELL) Price = Bid;
         Lot = NormalizeDouble(LotsLastHistOrder()*Multiplier, 2);
         ticket = OrderSend(Symbol(), Type, Lot, Price, Slippage, 0, 0, IntegerToString(Exp), Magic);
         return;
      }
      if (PriceCloseLastHistOrder() == PriceOpenLastHistOrder() && CountHistTrades() > 0) 
      // Если прибыль последней сделки равняется нулю, то открывается такая же
      {
         GlobalVariableSet("AllowNewOrders", 0);
         Type = TypeLastHistOrder();
         if (Type == OP_BUY)  Price = Ask;
         if (Type == OP_SELL) Price = Bid;
         Lot = NormalizeDouble(LotsLastHistOrder(), 2);
         ticket = OrderSend(Symbol(), Type, Lot, Price, Slippage, 0, 0, IntegerToString(Exp), Magic);
         return;
      }
      if (((TypeLastHistOrder() == OP_BUY && PriceCloseLastHistOrder(OP_BUY) > PriceOpenLastHistOrder(OP_BUY))
      || (TypeLastHistOrder() == OP_SELL && PriceCloseLastHistOrder(OP_SELL) < PriceOpenLastHistOrder(OP_SELL)))
      || CountHistTrades() == 0)// Если последняя сделка прибыльная, то открывается ордер
      {
         if (GlobalVariableGet("AllowNewOrders") > 0) return;
         if (SignalBuy() && MaxOpenOrders > OrdersTotal())
         {
            ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, 0, 0, IntegerToString(Exp), Magic);
            if (ticket>0) GlobalVariableSet("AllowNewOrders", 0);
            return;
         }
         if (SignalSell() && MaxOpenOrders > OrdersTotal())
         {
            ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, 0, 0, IntegerToString(Exp), Magic);
            if (ticket>0) GlobalVariableSet("AllowNewOrders", 0);
            return;
         }
      }
      GlobalVariableSet("AllowNewOrders", 1);
   }
}

Try it this way. I have a feeling something else will come up...

Reason: