Orders Execution
Miscellaneous
0
Views
0
Downloads
0
Favorites
Profitability Reports
AUD/USD
Oct 2024 - Jan 2025
0.00 %
Total Trades
0
Won Trades
0
Lost trades
0
Win Rate
0.0 %
Expected payoff
0.00
Gross Profit
0.00
Gross Loss
0.00
Total Net Profit
0.00
-100%
-50%
0%
50%
100%
NewsTrader_v2_usdchf_tp100
//+------------------------------------------------------------------+
//| NewsTrader_v2.mq4 |
//| Copyright © 2006, TrendLaboratory Ltd. |
//| http://finance.groups.yahoo.com/group/TrendLaboratory |
//| E-mail: igorad2003@yahoo.co.uk |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, TrendLaboratory Ltd."
#property link "http://finance.groups.yahoo.com/group/TrendLaboratory"
#include <stdlib.mqh>
//---- input parameters
extern string ExpertName = "NewsTrader_v2";
extern int Magic = 44445;
extern int Slippage = 6;
extern string Main_Parameters = " Trade Volume & Trade Method";
extern double Lots = 0.2; // Lot size
extern bool StopTrade = false; // Stop of Trade switch
extern string Data = " Input Data ";
extern int TimeZone = 0; // Difference between server time and local time
extern int TimeGap = 5; // Time Gap between News Time and Order Open Time in min
extern int OrderDuration = 15; // Order Duratiton Time in min
extern int ProcessTime = 2; // Order processing Time in min
extern int SessionEnd = 23; // Session End Time
extern string CalendarName = "CalendarNFP07ja.txt";
extern double TakeProfit = 100; // Take Profit in pips
extern double TrailingStop = 30; // Trailing Stop in pips
extern double InitialStop = 50; // Initial Stop in pips
extern double BreakEven = 0; // Breakeven in pips
extern double PendOrdGap = 20; // Gap for Pending Orders from current price in pips
extern bool DisplayLine = false; // Display Line Option (Visualization mode)
extern bool DisplayText = false; // Display Text Option (Visualization mode)
extern string MM_Parameters = " MoneyManagement by L.Williams ";
extern bool MM = false; // ÌÌ Switch
extern double MMRisk = 0.15; // Risk Factor
extern double LossMax = 1000; // Maximum Loss by 1 Lot
int cnt=0, ticket, mode=0, digit=0, numords;
double SellProfit=0,BuyProfit=0;
double BuyStop=0, SellStop=0, Lotsi=0;
bool BuyInTrade=false, SellInTrade=false;
datetime FinTime=0;
string sDate[100]; // Date
string sTime[100]; // Time
string sCurrency[100]; // Currency
string sDescription[100]; // Description
string sRating[100]; // Rating
datetime dt[100];
int NewsNum, TriesNum=5;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
ReadnPlotCalendar();
//----
return(0);
}
// ---- Money Management
double MoneyManagement ( bool flag, double Lots, double risk, double maxloss)
{
Lotsi=Lots;
if ( flag ) Lotsi=NormalizeDouble(Lots*AccountFreeMargin()*risk/maxloss,1);
if (Lotsi<0.1) Lotsi=0.1;
return(Lotsi);
}
// ---- Trailing Stops
void TrailStops()
{
bool result = false;
for (cnt=0;cnt<OrdersTotal();cnt++)
{
OrderSelect(cnt, SELECT_BY_POS);
int mode=OrderType();
if ( OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
if (mode==OP_BUY)
{
BuyStop = Bid - TrailingStop*Point;
if ( BreakEven > 0 && Bid-OrderOpenPrice()>Point*BreakEven ) BuyStop = OrderOpenPrice();
if( OrderOpenPrice() <= BuyStop || OrderStopLoss() == 0 )
{
if ( BuyStop > OrderStopLoss() )
{
result = OrderModify(OrderTicket(),OrderOpenPrice(),
NormalizeDouble(BuyStop, Digits),
OrderTakeProfit(),0,LightGreen);
if( !result )
{
Print("BUY: OrderModify failed with error #",GetLastError());
}
//return(0);
}
}
}
// - SELL Orders
if (mode==OP_SELL)
{
SellStop = Ask + Point * TrailingStop;
if ( BreakEven > 0 && OrderOpenPrice()-Ask>Point*BreakEven ) SellStop = OrderOpenPrice();
if( OrderOpenPrice() >= SellStop)
{
if( OrderStopLoss() > SellStop || OrderStopLoss() == 0 )
{
result = OrderModify(OrderTicket(), OrderOpenPrice(),
NormalizeDouble(SellStop, Digits),
OrderTakeProfit(),0,DarkOrange);
if( !result )
{
Print("SELL: OrderModify failed with error #",GetLastError());
}
//return(0);
}
}
}
}
}
}
// ---- Open Sell Orders
void SellOrdOpen()
{
double SellPrice= Bid - PendOrdGap*Point;
if (InitialStop > 0) SellStop=SellPrice + InitialStop*Point; else SellStop=0;
if (TakeProfit > 0) SellProfit=SellPrice - TakeProfit*Point; else SellProfit=0;
//Print(" SellPrice=", SellPrice);
ticket = OrderSend( Symbol(),OP_SELLSTOP,Lotsi,
NormalizeDouble(SellPrice, Digits),
Slippage,
NormalizeDouble(SellStop , Digits),
NormalizeDouble(SellProfit , Digits),
ExpertName+" SELL",Magic,0,Red);
if(ticket<0)
Print("SELLSTOP: OrderSend failed with error #",GetLastError());
else
if(ticket>0)
SellInTrade=false;
return(0);
}
// ---- Open Buy Orders
void BuyOrdOpen()
{
double BuyPrice =Ask + PendOrdGap*Point;
if (InitialStop > 0) BuyStop = BuyPrice - InitialStop*Point; else BuyStop=0;
if (TakeProfit>0) BuyProfit=BuyPrice + TakeProfit*Point; else BuyProfit=0;
ticket = OrderSend(Symbol(),OP_BUYSTOP, Lotsi,
NormalizeDouble(BuyPrice, Digits),
Slippage,
NormalizeDouble(BuyStop , Digits),
NormalizeDouble(BuyProfit , Digits),
ExpertName+" BUY",Magic,0,Blue);
if(ticket<0)
Print("BUYSTOP: OrderSend failed with error #",GetLastError());
else
if(ticket>0)
BuyInTrade=false;
return(0);
}
// ---- Delete Extra Orders
void ExtraOrdDel()
{
int total = OrdersTotal();
for (cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS);
mode=OrderType();
if ( OrderSymbol()==Symbol() && OrderMagicNumber()==Magic )
{
if (mode==OP_BUY && !BuyInTrade ) BuyInTrade =true;
if (mode==OP_SELL && !SellInTrade) SellInTrade=true;
if (mode > OP_SELL && (BuyInTrade || SellInTrade))
OrderDelete(OrderTicket());
}
}
}
// ---- Scan Trades
int ScanTrades()
{
int total = OrdersTotal();
numords = 0;
for(cnt=0; cnt<total; cnt++)
{
OrderSelect(cnt, SELECT_BY_POS);
if(OrderSymbol() == Symbol() && OrderType()<=OP_SELLSTOP && OrderMagicNumber() == Magic)
numords++;
}
return(numords);
}
datetime FinishTime(int Duration)
{
int total = OrdersTotal();
datetime ftime=0;
for(cnt=0; cnt<total; cnt++)
{
OrderSelect(cnt, SELECT_BY_POS);
if(OrderSymbol() == Symbol() && OrderType()<=OP_SELLSTOP && OrderMagicNumber() == Magic)
ftime=OrderOpenTime()+ Duration*60;
}
return(ftime);
}
// Closing of Pending Orders
void PendOrdDel(int mode)
{
for (int i=0; i<OrdersTotal(); i++)
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if ( Symbol()==OrderSymbol() && OrderMagicNumber()==Magic)
{
if((mode==0 || mode==1) && OrderType()==OP_BUYSTOP)
{
bool result = OrderDelete(OrderTicket());
if(!result)
{
Print("BUYSTOP: OrderDelete failed with error #",GetLastError());
//return(0);
}
//Print("total=",OrdersTotal()," i=",i);
}
else
if((mode==0 || mode==2) && OrderType()==OP_SELLSTOP)
{
result = OrderDelete( OrderTicket() );
if(!result)
{
Print("SELLSTOP: OrderDelete failed with error #",GetLastError());
//return(0);
}
}
}
}
return(0);
}
//-----
void ReadnPlotCalendar()
{
int i,handle;
handle=FileOpen(CalendarName,FILE_CSV|FILE_READ,';');
if(handle<1)
{
Print("File not found ", GetLastError());
return(false);
}
i=0;
while(!FileIsEnding(handle))
{
sDate[i]=FileReadString(handle); // Date
sTime[i]=FileReadString(handle); // Time
sCurrency[i]=FileReadString(handle); // Currency
sDescription[i]=FileReadString(handle); // Description
sRating[i]=FileReadString(handle); // Rating
FileReadString(handle); // null
dt[i] = StrToTime(sDate[i]+" "+sTime[i])+TimeZone*3600;
color c=Yellow ;
if (sCurrency[i] == "JPY") c = Yellow;
if (sCurrency[i] == "USD") c = Brown;
if (sCurrency[i] == "CHF") c = Green;
if (sCurrency[i] == "EUR") c = Blue;
if (sCurrency[i] == "GBP") c = Orange;
if (sCurrency[i] == "CAD") c = Gray;
if (sCurrency[i] == "AUD") c = DarkViolet;
if (sCurrency[i] == "NZD") c = Lime;
string info = i+"_"+sDate[i]+" "+sTime[i]+" "+sCurrency[i]+" "+" "+sDescription[i]+" ";
Print( info + "c=",c);
if (DisplayText)
{
ObjectCreate("NTT"+i, OBJ_TEXT, 0, dt[i], Close[0]);
ObjectSet("NTT"+i, OBJPROP_COLOR, c);
ObjectSetText("NTT"+i,sCurrency[i] + " " + sDescription[i] + " ",8);
ObjectSet("NTT"+i, OBJPROP_ANGLE, 90);
}
if (DisplayLine)
{
ObjectCreate("NTL"+i, OBJ_VLINE, 0, dt[i], Close[0]);
ObjectSet("NTL"+i, OBJPROP_COLOR, c);
ObjectSet("NTL"+i, OBJPROP_STYLE, STYLE_DOT);
ObjectSet("NTL"+i, OBJPROP_BACK, true);
ObjectSetText("NTL"+i,sDescription[i] + " · ",8);
}
i++;
}
NewsNum = i;
return(0);
}
//----
void ObjDel()
{
int _GetLastError;
if(DisplayLine && DisplayText) int obtotal = 0.5*ObjectsTotal(); else obtotal = ObjectsTotal();
for ( int i = 0; i < obtotal; i ++ )
{
if (DisplayLine)
if ( !ObjectDelete( StringConcatenate( "NTL", i ) ) )
{
_GetLastError = GetLastError();
Print( "ObjectDelete( \"", StringConcatenate( "NTL", i ),"\" ) - Error #", _GetLastError );
}
if (DisplayText)
if( !ObjectDelete( StringConcatenate( "NTT", i ) ) )
{
_GetLastError = GetLastError();
Print( "ObjectDelete( \"", StringConcatenate( "NTT", i ),"\" ) - Error #", _GetLastError );
}
}
}
//---- Close of Orders
void CloseOrder(int mode)
{
bool result=false;
int total=OrdersTotal();
for (int i=0; i<=total; i++)
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if (OrderMagicNumber() == Magic && OrderSymbol() == Symbol())
{
if ((mode == 0 || mode ==1) && OrderType()==OP_BUY ) result=CloseAtMarket(OrderTicket(),OrderLots(),Aqua);
if ((mode == 0 || mode ==2) && OrderType()==OP_SELL) result=CloseAtMarket(OrderTicket(),OrderLots(),Pink);
}
}
}
bool CloseAtMarket(int ticket,double lot,color clr)
{
bool result = false;
int ntr;
int tries=0;
while (!result && tries < TriesNum)
{
ntr=0;
while (ntr<5 && !IsTradeAllowed()) { ntr++; Sleep(5000); }
RefreshRates();
result=OrderClose(ticket,lot,OrderClosePrice(),Slippage,clr);
tries++;
}
if (!result) Print("Error closing order : ",ErrorDescription(GetLastError()));
return(result);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
ObjDel();
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
if(Bars < 1) {Print("Not enough bars for this strategy");return(0);}
if(AccountFreeMargin()<(1000*Lots)){
Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0);}
//----
if (!StopTrade && ScanTrades()<1)
{
for (int i=0; i<=NewsNum; i++)
{
datetime OpenTime = dt[i] - TimeGap*60;
if((TimeCurrent()>= OpenTime && TimeCurrent() <= OpenTime+ProcessTime*60))
{
Lotsi = MoneyManagement ( MM, Lots, MMRisk, LossMax);
BuyOrdOpen();
SellOrdOpen();
}
}
}
if (ScanTrades()>0)
{
ExtraOrdDel();
datetime FinTime = FinishTime(OrderDuration);
if (TimeCurrent()>=FinTime && TimeCurrent()<=FinTime+ProcessTime*60) PendOrdDel(0);
datetime EndTime = StrToTime(SessionEnd+":00");
bool EOD = false;
EOD = TimeCurrent()>= EndTime;
if(EOD) {CloseOrder(0);PendOrdDel(0);}
if (TrailingStop>0 || BreakEven>0) TrailStops();
}
return(0);
}//int start
//+------------------------------------------------------------------+
Comments
Markdown Formatting Guide
# H1
## H2
### H3
**bold text**
*italicized text*
[title](https://www.example.com)

`code`
```
code block
```
> blockquote
- Item 1
- Item 2
1. First item
2. Second item
---