#include <#_lib_news.mq4>
extern int ChasPoyas = 3; // часовой пояс. 0-GMT
extern int MinDo = 15;
extern int MinPosle = 15;
extern string BS = "---------------- Base settings---------------";
extern int MagicNumber = 8951;
extern int Slippage = 3;
extern int ProfitFactor = 600;
extern int StopLossFactor = 900;
extern int AccountOrders = 10;
extern int Distance = 30;
extern string MM = "---------------- Money Managment---------------";
extern bool FixedLot = false;
extern double Lots = 0.2;
extern double MaximumRisk = 2;
extern double DecreaseFactor = 0;
extern string TMS = "---------------- Time Settings ---------------";
extern bool TradeOnFriday = true;
extern bool TradeOnMonday = true;
extern int BaginTradeHour = 20;
extern int EndTradeHour = 1;
extern string RTS2 = "---------------- RSI Teacher Settings ---------------";
extern int RSI_Period = 13;
extern int RSI_Price = 1;
extern double RSI_Hi_Level = 70;
extern double RSI_Lo_Level = 30;
extern double RSI_Hi_LevelClos = 65;
extern double RSI_Lo_LevelClos = 30;
extern bool Vhigh = true;
extern bool Vmedium = false;
extern bool Vlow = false;
//***************
// Global variables
double ShortOrderPrevProfit, ShortOrderPrevLot, ShortOrderPrevSL, ShortOrderPrevTP;
double LongOrderPrevProfit, LongOrderPrevLot, LongOrderPrevSL, LongOrderPrevTP;
int LongOrderPrevTicket, ShortOrderPrevTicket;
int buyOrders = 0, sellOrders = 0, allOrders = 0;
string DivTrainStr = "; ";
// Long position ticket
int LongTicket = -1;
// Short position ticket
int ShortTicket = -1;
int CountBuyOrders = 0, CountSellOrders = 0;
double price, LastLot;
int ticket;
int prevstatus;
int prevtime;
string FileNamePrev;
int TimeNextNewsPrev;
//------------------------------------------------------------------
// initialization function
//------------------------------------------------------------------
int init ()
{
if (Digits == 5) {
Slippage = Slippage * 10;
Distance = Distance * 10;
}
return (0);
}
//------------------------------------------------------------------
// deinitialization function |
//------------------------------------------------------------------
int deinit ()
{
return (0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start ()
{
int i;
bool BuySignal = false;
bool SellSignal = false;
GetCurrentOrders(Symbol());
if (AccountFreeMargin() > 0)
CheckForOpen();
return (0);
}
//------------------------------------------------------------------
// Get open positions |
//------------------------------------------------------------------
int GetCurrentOrders(string symbol)
{
int i = 0;
buyOrders = 0;
sellOrders = 0;
allOrders = OrdersTotal();
for(i=0; i < allOrders; i++)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false) break;
if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
{
if(OrderType() == OP_BUY) buyOrders++;
if(OrderType() == OP_SELL) sellOrders++;
}
}
/*
if(buyOrders > 0) return( buyOrders );
else return(-sellOrders);
*/
return;
}
//------------------------------------------------------------------
//| Check for open order conditions |
//+------------------------------------------------------------------+
void CheckForOpen()
{
double Lot;
int i;
bool tradeAllow = false;
if (!TradeOnFriday)
if (DayOfWeek() == 5 && Hour() >= BaginTradeHour) return;
if (!TradeOnMonday)
if (DayOfWeek() == 1 && Hour() <= EndTradeHour) return;
if (BaginTradeHour > 0 && EndTradeHour > 0) {
if (Hour() >= BaginTradeHour || Hour() <= EndTradeHour) tradeAllow = true;
} else {
tradeAllow = true;
}
if (allOrders < AccountOrders || !AccountOrders && tradeAllow) {
if (DayOfWeek() == 5 && Hour() >= 23 && Minute()>=40) return;
{
if (DayOfWeek() == 1 && Hour() <= 0 && Minute()<=5) return;
{
Lot = GetLotSize();
double MaxLot = MarketInfo(Symbol(), MODE_MINLOT);
double MinLot = MarketInfo(Symbol(), MODE_MAXLOT);
double LotSize = MarketInfo(Symbol(), MODE_LOTSIZE);
double LockLot;
//---- buy
if(iRSI( Symbol(), 0, RSI_Period, RSI_Price, 0)<=RSI_Lo_Level&&buyOrders<1 )
{iCustom(Symbol(),0,"Urdala_News",MinDo,MinPosle, ChasPoyas,Vhigh,Vmedium,Vlow,0,0);{
LongTicket=OrderSend(Symbol(),OP_BUY,Lot,Ask,3,0,0,"",MagicNumber,0,Blue);
if (LongTicket > 0) {
OrderModify(LongTicket, Lot, NormalizeDouble(Ask-(StopLossFactor*Point), Digits), NormalizeDouble(Ask+(ProfitFactor*Point),Digits), 0);
//buyOrders++;
}
}}
//---- sell
if(iRSI( Symbol(), 0, RSI_Period, RSI_Price, 0)>=RSI_Hi_Level&&sellOrders<1)
{
ShortTicket=OrderSend(Symbol(),OP_SELL,Lot,Bid,3,0,0,"",MagicNumber,0,Red);
if (ShortTicket > 0) {
OrderModify(ShortTicket, Lot, NormalizeDouble(Bid+(StopLossFactor*Point),Digits), NormalizeDouble(Bid-(ProfitFactor*Point), Digits), 0);
//sellOrders++;
}
}
}
}
}
for (i=OrdersTotal()-1; i>=0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderType()==OP_BUY&& OrderMagicNumber() == MagicNumber ) // удаление открытой позиции
{
if(iRSI( Symbol(), 0, RSI_Period, RSI_Price, 0)>=RSI_Hi_LevelClos)
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Blue);
}}}
}
for (i=OrdersTotal()-1; i>=0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderType()==OP_SELL&& OrderMagicNumber() == MagicNumber ) // удаление открытой позиции
{
if(iRSI( Symbol(), 0, RSI_Period, RSI_Price, 0)<=RSI_Lo_LevelClos )
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,Red);
}}}
}
}
//------------------------------------------------------------------
// Calculate optimal lot size
//------------------------------------------------------------------
double GetLotSize()
{
if (!FixedLot) {
double lot = Lots;
int orders = HistoryTotal(); // history orders total
int losses = 0; // number of losses orders without a break
lot = NormalizeDouble(AccountFreeMargin()*MaximumRisk /(MarketInfo(Symbol(), MODE_LOTSIZE)/10), 2);
} else {
lot = Lots;
}
if(DecreaseFactor>0) {
for(int i = orders-1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS,MODE_HISTORY) == false) { Print("Error in history!"); break; }
if(OrderSymbol() != Symbol() || OrderType() > OP_SELL) continue;
//----
if(OrderProfit() > 0) break;
if(OrderProfit() < 0) losses++;
}
if(losses>1) lot = NormalizeDouble(lot-lot*losses/DecreaseFactor, 2);
}
double MinLot = MarketInfo(Symbol(), MODE_MINLOT);
double MaxLot = MarketInfo(Symbol(), MODE_MAXLOT);
if (MinLot == 0) MinLot = 0.01;
if (MaxLot == 0) MaxLot = 10000;
if (lot < MinLot) lot = MinLot;
if (lot > MaxLot) lot = MaxLot;
//---- return lot size
return(lot);
}
//+------------------------------------------------------------------+