//+------------------------------------------------------------------+ //| OpenDaily.mq4 | //| Copyright © 2008, LEGRUPO | //| http://www.legrupo.com | //| Version: 1.0 | //| History: | //| 1.0 => Release version to the public | //+------------------------------------------------------------------+ #property copyright "Copyright © 2008, LEGRUPO" #property link "http://www.legrupo.com" //+------------------------------------------------------------------+ //| EX4 imports | //+------------------------------------------------------------------+ #include #include //---- input parameters extern int ExpertID = 488488; extern int TakeProfit=0; extern int TrailingStop = 0; extern double StopLoss=0; extern bool autoChooseTPandSL = true; extern double Lots=0.1; extern int Slippage = 3; extern int AtrPeriod = 2; extern double PercentageOfATR = 0.4; extern string Object_ID = "atrpeter"; int pipMult = 100; double buffer[]; extern bool UseMoneyManagement = false; extern bool AccountIsMicro = false; extern int Risk = 10; // risk in percentage % (10% of risk in this case) // Bar handling used to determine when a bar has moved datetime bartime=0; // broker point size double myPoint; // buffers double AtrBuffer[]; double TempBuffer[]; //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { //---- if (StringFind(Symbol(),"JPY",0) != -1) { pipMult = 1; } if (PercentageOfATR != 1.0) { int percentage = PercentageOfATR*100; } //---- return(0); } //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { //---- int MagicNumber = MakeMagicNumber()+Year()+Month()+Day();//+Hour(); if(UseMoneyManagement==true) { Lots = DefineLotSize(); //Adjust the lot size total if (Lots > MarketInfo(Symbol(), MODE_MAXLOT)) { Lots = MarketInfo(Symbol(), MODE_MAXLOT); } } if(TrailingStop>0) { for (int tr = 0; tr <= OrdersTotal(); tr++) { if (OrderSelect(tr, SELECT_BY_POS, MODE_TRADES) == true) { if(OrderMagicNumber() == MagicNumber) { if(OrderType()==OP_BUY) { if(Bid-OrderOpenPrice()>Point*TrailingStop) { if(OrderStopLoss()(Point*TrailingStop)) { if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0)) { OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red); } } } } } } } // do not trade until allowed: begin if(bartime==iTime(Symbol(), 0, 0) ) return(0); // do not trade until allowed: end // ATR calculation (By Ron Thompson): begin datetime t=iTime(Symbol(),0,0); double buy_limit = NormalizeDouble(ObjectGet(Object_ID + "BUY" + DoubleToStr(t, 0), OBJPROP_PRICE1), Digits); double sell_limit = NormalizeDouble(ObjectGet(Object_ID + "SELL" + DoubleToStr(t, 0), OBJPROP_PRICE1), Digits); double LimitEntry = StrToDouble(ObjectDescription(Object_ID + "R" + DoubleToStr(t, 0))); // risk Comment("BUY: ",buy_limit,"\nSELL: ",sell_limit,"\nRISK: ",LimitEntry); // ATR calculation: end double tp, sl, price = 0.0; string data = TimeYear(TimeCurrent())+"."+TimeMonth(TimeCurrent())+"."+TimeDay(TimeCurrent()); datetime expiretime = StrToTime(data+" "+TimeToStr(Hour()+3)+":59"); if (CountLongs(MagicNumber) == 0) { price = buy_limit;//Ask+LimitEntry*Point; if (autoChooseTPandSL) { tp = price+(LimitEntry*2)*Point; sl = price-LimitEntry*Point; } else { tp = Ask+TakeProfit*Point; sl = Ask-StopLoss*Point; } int ticket_buy = OrderSend(Symbol(),OP_BUYSTOP,Lots,price,Slippage,sl,tp,WindowExpertName(),MagicNumber,0,CLR_NONE); if(ticket_buy>0) { if(OrderSelect(ticket_buy,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice()); } else { //Print(WindowExpertName()," ",Symbol()," Error opening order : ",ErrorDescription(GetLastError())); Print("BUY Price: ",price," - SL: ",sl," - TP: ",tp); return(0); } // fim ticket_buy } if (CountShorts(MagicNumber) == 0) { price = Bid-LimitEntry*Point; if (autoChooseTPandSL) { tp = price-(LimitEntry*2)*Point; sl = price+LimitEntry*Point; } else { tp = Bid-TakeProfit*Point; sl = Bid+StopLoss*Point; } int ticket_sell = OrderSend(Symbol(),OP_SELLSTOP,Lots,price,Slippage,sl,tp,WindowExpertName(),MagicNumber,0,CLR_NONE); if(ticket_sell>0) { if(OrderSelect(ticket_sell,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice()); } else { //Print(WindowExpertName()," ",Symbol()," Error opening order : ",ErrorDescription(GetLastError())); Print("SELL Price: ",price," - SL: ",sl," - TP: ",tp); return(0); } // fim ticket_sell } //---- return(0); } //+------------------------------------------------------------------+ int MakeMagicNumber() { int SymbolCode = 0; int PeriodCode = 0; int MagicNumber = 0; //---- Symbol Code if( Symbol() == "AUDCAD" || Symbol() == "AUDCADm" ) { SymbolCode = 1000; } else if( Symbol() == "AUDJPY" || Symbol() == "AUDJPYm" ) { SymbolCode = 2000; } else if( Symbol() == "AUDNZD" || Symbol() == "AUDNZDm" ) { SymbolCode = 3000; } else if( Symbol() == "AUDUSD" || Symbol() == "AUDUSDm" ) { SymbolCode = 4000; } else if( Symbol() == "CHFJPY" || Symbol() == "CHFJPYm" ) { SymbolCode = 5000; } else if( Symbol() == "EURAUD" || Symbol() == "EURAUDm" ) { SymbolCode = 6000; } else if( Symbol() == "EURCAD" || Symbol() == "EURCADm" ) { SymbolCode = 7000; } else if( Symbol() == "EURCHF" || Symbol() == "EURCHFm" ) { SymbolCode = 8000; } else if( Symbol() == "EURGBP" || Symbol() == "EURGBPm" ) { SymbolCode = 9000; } else if( Symbol() == "EURJPY" || Symbol() == "EURJPYm" ) { SymbolCode = 1000; } else if( Symbol() == "EURUSD" || Symbol() == "EURUSDm" ) { SymbolCode = 1100; } else if( Symbol() == "GBPCHF" || Symbol() == "GBPCHFm" ) { SymbolCode = 1200; } else if( Symbol() == "GBPJPY" || Symbol() == "GBPJPYm" ) { SymbolCode = 1300; } else if( Symbol() == "GBPUSD" || Symbol() == "GBPUSDm" ) { SymbolCode = 1400; } else if( Symbol() == "NZDJPY" || Symbol() == "NZDJPYm" ) { SymbolCode = 1500; } else if( Symbol() == "NZDUSD" || Symbol() == "NZDUSDm" ) { SymbolCode = 1600; } else if( Symbol() == "USDCAD" || Symbol() == "USDCADm" ) { SymbolCode = 1700; } else if( Symbol() == "USDCHF" || Symbol() == "USDCHFm" ) { SymbolCode = 1800; } else if( Symbol() == "USDJPY" || Symbol() == "USDJPYm" ) { SymbolCode = 1900; } //---- Calculate MagicNumber MagicNumber = ExpertID+SymbolCode; return(MagicNumber); } double DefineLotSize() { double lotMM = MathCeil(AccountFreeMargin() * Risk / 1000) / 100; if(AccountIsMicro==false) { //normal account if (lotMM < 0.1) lotMM = Lots; if ((lotMM > 0.5) && (lotMM < 1)) lotMM=0.5; if (lotMM > 1.0) lotMM = MathCeil(lotMM); if (lotMM > 100) lotMM = 100; } else { //micro account if (lotMM < 0.01) lotMM = Lots; if (lotMM > 1.0) lotMM = MathCeil(lotMM); if (lotMM > 100) lotMM = 100; } return (lotMM); } //+------------------------------------------------------------------+ //| Calculate concurrent Long position | //+------------------------------------------------------------------+ int CountLongs(int MagicNumber) { int count=0; int trade; int trades=OrdersTotal(); for(trade=0;trade