0% found this document useful (0 votes)
97 views

First - Second v2.Mq4

Uploaded by

Zain Lamour
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views

First - Second v2.Mq4

Uploaded by

Zain Lamour
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

//+------------------------------------------------------------------+

//| Copyright © 2015, Õëûñòîâ Âëàäèìèð |


//| [email protected] |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2015, http://cmillion.ru"
#property link "[email protected]"
#property version "2.00"
#property description "Modify by extraw on 03/12/2017"
#property description "Disclaimer: I will not responsible for any of you losses,
use at your own risk."
//--------------------------------------------------------------------*/
// TargetBalance: if AccountEquity >= TargetBalance then EA stop trading
// LossToRunInsurer: EA trade Martingale style until the profit < LossToRunInsurer,
// then it will run Insurer to cover the losses (hopefully)
//--------------------------------------------------------------------*/
input double TargetBalance = 11000;
extern int Takeprofit = 20; //òåéêïðîôèò
extern double LotBuy = 0.10; //
extern double LotSell = 0.10; //
extern int MaxOrders = 100; //ìàêñèìàëüíîå êîë-âî äîëèâî÷íûõ
îðäåðîâ
extern string Comment_order = "";
extern int Magic = 777888;
extern bool DrawInfo = true; //âûâîä èíôîðìàöèè íà ýêðàí
extern int slippage = 30; //Ìàêñèìàëüíî äîïóñòèìîå
îòêëîíåíèå öåíû äëÿ ðûíî÷íûõ îðäåðîâ (îðäåðîâ íà ïîêóïêó èëè ïðîäàæó).
input string _a = "Insurer input";
input double LossToRunInsurer = -500;
extern double PercentProfitClose = 0.1; //çàêđûâạ̀ü âñå îđäåđà ïđè ïîëó÷åíèè
ïđîôẹ̀à â ïđîöåị́å ị̂ äåïîçẹ̀à
extern double Lot = 5; //ëị̂ ṇ̃đàơó₫ùåăî ṇ̃îï îđäåđà
extern int delta = 25; //đàñṇ̃îÿíèå äî ṇ̃îï îđäåđà ị̂ êđàéíåé
ïîçèöèè
//--------------------------------------------------------------------
string val;
int N;
bool Run_Insurer = false;
bool Stop_Trading = false;
//--------------------------------------------------------------------
int init()
{
val = " "+AccountCurrency();
return(0);
}
//-------------------------------------------------------------------
int deinit()
{
if (!IsTesting()) ObjectsDeleteAll(0);
return(0);
}
//--------------------------------------------------------------------
int start()
{
if (!IsTradeAllowed())
{
DrawLABEL("IsTradeAllowed","Òîðãîâëÿ çàïðåùåíà",5,15,Red);
return(0);
}
else DrawLABEL("IsTradeAllowed","Òîðãîâëÿ ðàçðåøåíà",5,15,Lime);
if (Stop_Trading == true) return 0;

//---
if (Run_Insurer == false)
{
double OSL,OTP,OOP,TP,Profit,ProfitB,ProfitS,FM;
int n=0,tip;
for (int i=0; i<OrdersTotal(); i++)
{
if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if (OrderSymbol()==Symbol() && Magic==OrderMagicNumber())
{
tip = OrderType();
OSL = NormalizeDouble(OrderStopLoss(),Digits);
OTP = NormalizeDouble(OrderTakeProfit(),Digits);
OOP = NormalizeDouble(OrderOpenPrice(),Digits);
TP=OTP;
if (tip==OP_BUY)
{
ProfitB+=OrderProfit()+OrderSwap()+OrderCommission();
n++;
if (OTP==0 && Takeprofit!=0)
{
TP = NormalizeDouble(OOP + Takeprofit * Point,Digits);
}
if (TP != OTP) if (!
OrderModify(OrderTicket(),OOP,OSL,TP,0,White)) Print("Error OrderModify
",GetLastError());
}
if (tip==OP_SELL)
{
ProfitS+=OrderProfit()+OrderSwap()+OrderCommission();
n++;
if (OTP==0 && Takeprofit!=0)
{
TP = NormalizeDouble(OOP - Takeprofit * Point,Digits);
}
if (TP != OTP) if (!
OrderModify(OrderTicket(),OOP,OSL,TP,0,White)) Print("Error OrderModify
",GetLastError());
}
}
}
}
Profit = ProfitB + ProfitS;
FM=AccountFreeMargin();
DrawLABEL("Balance",StringConcatenate("Balance
",DoubleToStr(AccountBalance(),2),val),5,35,Lime);
DrawLABEL("Equity",StringConcatenate("Equity
",DoubleToStr(AccountEquity(),2),val),5,55,Lime);
DrawLABEL("FreeMargin",StringConcatenate("FreeMargin
",DoubleToStr(FM,2),val),5,75,Color(FM<0,Red,Lime));
DrawLABEL("Profit",StringConcatenate("Profit
",DoubleToStr(Profit,2),val),5,95,Color(Profit<0,Red,Lime));
DrawLABEL("Orders",StringConcatenate("Orders
",n),5,115,Color(Profit<0,Red,Lime));
DrawLABEL("Control","Running Martingale",5,135,clrWhite);
//----------------------------------------------------------------

if (n>=N && n!=0) return(0);

if (Profit < LossToRunInsurer)


{
Run_Insurer = true;
return(0); // escape Martingale here to run Insurer in next tick
}

if (n<MaxOrders)
{
if (OrderSend(Symbol(),OP_BUY,
LotBuy,NormalizeDouble(Ask,Digits),slippage,0,0,Comment_order,Magic,0,CLR_NONE)==-1
||
OrderSend(Symbol(),OP_SELL,LotSell,NormalizeDouble(Bid,Digits),slippage,0,0,Comment
_order,Magic,0,CLR_NONE)==-1)
//Print("Îøèáêà ",GetLastError()," îòêðûòèÿ îðäåðà ");
Print("Error OrderSend: Martingale");
}

N=0;
for (i=0; i<OrdersTotal(); i++)
{
if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if (OrderSymbol()==Symbol() && Magic==OrderMagicNumber())
{
N++;
}
}
}
}
else // Run_Insurer == true
{
double LB=0,LS=0,OL=0,MaxPrice=0,MinPrice=0;
int b=0,s=0,bs=0,ss=0,OT;

Profit=0;
for (i=OrdersTotal()-1; i>=0; i--)
{
if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if (OrderSymbol() == Symbol())
{
OT = OrderType();
OL = OrderLots();
OOP = OrderOpenPrice();
if (MaxPrice<OOP) MaxPrice=OOP;
if (MinPrice>OOP || MinPrice==0) MinPrice=OOP;
if (OT==OP_BUY)
{
b++;LB+= OL;
Profit+=OrderProfit();
}
if (OT==OP_SELL)
{
s++;LS+= OL;
Profit+=OrderProfit();
}
if (OT==OP_BUYSTOP) bs++;
if (OT==OP_SELLSTOP) ss++;
}
}
}

FM=AccountFreeMargin();
DrawLABEL("Balance",StringConcatenate("Balance
",DoubleToStr(AccountBalance(),2),val),5,35,Lime);
DrawLABEL("Equity",StringConcatenate("Equity
",DoubleToStr(AccountEquity(),2),val),5,55,Lime);
DrawLABEL("FreeMargin",StringConcatenate("FreeMargin
",DoubleToStr(FM,2),val),5,75,Color(FM<0,Red,Lime));
DrawLABEL("Profit",StringConcatenate("Profit
",DoubleToStr(Profit,2),val),5,95,Color(Profit<0,Red,Lime));
DrawLABEL("Orders",StringConcatenate("Orders
",b+s),5,115,Color(Profit<0,Red,Lime));
DrawLABEL("Control","Running Insurer",5,135,clrYellow);
//---
if (Profit>=AccountBalance()/100*PercentProfitClose)
{
Alert("Äîṇ̃èăíọ́ óđîâåíü ïđîôẹ̀à = "+DoubleToStr(Profit,2));
CloseAll();
Run_Insurer = false;
}
//---
int err;
if (bs==0)
{
err=OrderSend(Symbol(),OP_BUYSTOP,
Lot,NormalizeDouble(MaxPrice+delta*Point,Digits),30,0,0,NULL,0,0,Blue);
if (err < 0) Print("Error BuyStop OrderSend: Insurer");
}
if (ss==0)
{
err=OrderSend(Symbol(),OP_SELLSTOP,Lot,NormalizeDouble(MinPrice-
delta*Point,Digits),30,0,0,NULL,0,0,Red);
if (err < 0) Print("Error SellStop OrderSend: Insurer");
}
}

if (TargetBalance > 0 && AccountEquity() > TargetBalance)


{
CloseAll();
Stop_Trading = true;
}

return(0);
}
//------------------------------------------------------------------
color Color(bool P,color a,color b)
{
if (P) return(a);
else return(b);
}
//------------------------------------------------------------------
void DrawLABEL(string name, string Name, int X, int Y, color clr)
{
if (ObjectFind(name)==-1)
{
ObjectCreate(name, OBJ_LABEL, 0, 0, 0);
ObjectSet(name, OBJPROP_CORNER, 1);
ObjectSet(name, OBJPROP_XDISTANCE, X);
ObjectSet(name, OBJPROP_YDISTANCE, Y);
}
ObjectSetText(name,Name,12,"Arial",clr);
}
//--------------------------------------------------------------------

bool CloseAll()
{
bool error=true;
int j,err,nn,OT;
while(true)
{
for (j = OrdersTotal()-1; j >= 0; j--)
{
if (OrderSelect(j, SELECT_BY_POS))
{
if (OrderSymbol() == Symbol())
{
OT = OrderType();
if (OT>1)
{
error=OrderDelete(OrderTicket());
}
if (OT==OP_BUY)
{

error=OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits),3,Blue);
if (error) Alert(Symbol()," Çàêđụ̂ îđäåđ N ",OrderTicket(),"
ïđèáûëü ",OrderProfit(),
" ",TimeToStr(TimeCurrent(),TIME_SECONDS));
}
if (OT==OP_SELL)
{

error=OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits),3,Red);
if (error) Alert(Symbol()," Çàêđụ̂ îđäåđ N ",OrderTicket(),"
ïđèáûëü ",OrderProfit(),
" ",TimeToStr(TimeCurrent(),TIME_SECONDS));
}
if (!error)
{
err = GetLastError();
if (err<2) continue;
if (err==129)
{ Comment("Íåïđàâèëüíàÿ öåíà
",TimeToStr(TimeCurrent(),TIME_MINUTES));
Sleep(5000);
RefreshRates();
continue;
}
if (err==146)
{
if (IsTradeContextBusy()) Sleep(2000);
continue;
}
Comment("Îøèáêà ",err," çàêđụ̂èÿ îđäåđà N ",OrderTicket(),"
",TimeToStr(TimeCurrent(),TIME_MINUTES));
}
}
}
}
int k=0;
for (j = 0; j < OrdersTotal(); j++)
{
if (OrderSelect(j, SELECT_BY_POS))
{
if (OrderSymbol() == Symbol())
{
OT = OrderType();
if (OT==OP_BUY || OT==OP_SELL) k++;
}
}
}
if (k==0) break;
nn++;
if (nn>10) {Alert(Symbol()," Íå óäàëîñü çàêđụ̂ü âñå ñäåëêè, îṇ̃àëîñü åùå
",k);return(0);}
Sleep(1000);
RefreshRates();
}
return(1);
}

You might also like