//+---------------+ //|Daily HiLo FIB | //+---------------+ #property copyright "Ron Thompson" #property link "http://www.ForexMT4.com/" // This EA is NEVER to be SOLD individually // This EA is NEVER to be INCLUDED as part of a collection that is SOLD /* ---Martingale--- If enabled (MartingaleStyle greater than 1) the system will increase lot sizes when there is a loss. This is an attempt to recover the previous lossby doubling the current trade. The failure of Martingale will come when you don't have enough money to support the steps anymore, and you lose everything, AND MORE, because FOREX trades on MARGIN!!!. ---MartingaleStyle--- Allows you to pick the type of Martingale you are comfortible with. 0 == NO LOSS RECOVERY 1 == pure Martingale 2 == Modified Martingale - starts after 2 losses 3 == Fibonacci - growth by Fibo numbers, requires 2 wins to recover the total loss 4 == Martingale partial, starts after 5 losses NOTE! The largest lot is controlled by MODE_MAXLOT and will NOT be exceeded ---Starting Balance--- The formula for Lots is AccountBalance() / StartingBalance Lets say your AccountBalance() is currently 550$ if you want to trade 10.0 lots, put 55 into StartingBalance 550 / 55 = 10 if you want to trade 1.0 lots, put 550 into StartingBalance 550 / 550 = 1 if you want to trade 0.5 lots, put 1100 into StartingBalance 550 / 1100 = 0.5 if you want to trade 0.1 lots, put 5500 into StartingBalance 550 / 5500 = 0.1 if you want to trade 0.01 lots, put 55000 into StartingBalance 550 / 55000 = 0.01 ---ProfitMade--- The amount of pips you expect to make on EACH TRADE ---LossLimit--- The amount of pips you can afford to lose on EACH TRADE ---Basket--- In DOLLARS, this will close all trades after so much profit or loss ---BreakEven--- Move the stoploss to break-even once the trade is in profit by 'breakeven' pips ---TrailStop--- Follow price action with a stoploss, keeping the stoploss within 'TrailStop' pips ---Lot Increase--- Lots must be equal to ZERO and StartingBalance>ZERO for lot increasement to work LotIncreasement grows the lot size based on your starting balance, risking more money when you have more money. It is built in, based on Starting Balance and LotResolution, and is independent of the Martingale process. ---LotResolution--- This controls the Lot size of step your broker/account can support If set to 1, then your lot size is increased in 0.1 steps as your account grows If set to 2, then your lot size is increased in 0.01 steps as your account grows IBFX supports LotResolution of 2 ---General--- Added Robert Hill's routine to manage 4 or 5 decimal place brokers Updated the lotsi calculations to support martingale AND increasement */ // EA SPECIFIC extern double ATRPeriod = 0 ; extern double ATRMultiplier = 1.0 ; // user input extern int MartingaleStyle = 0 ; extern double Lots = 5 ; extern double StartingBalance = 0 ; extern double ProfitMade = 0 ; extern double BasketProfit = 0 ; extern double LossLimit = 69 ; extern double BasketLoss = 0 ; extern double BreakEven = 0 ; extern double TrailStop = 34 ; extern int LotResolution = 1 ; extern bool KillLogging = true ; // Trade control double Lotz; double lotsi; // used for Martingale loss recovery double myPoint; // support for 3/5 decimal places int Slippage=2; // how many pips of slippage can you tolorate bool TradeAllowed=true; // used to manage trades int loopcount; // count of order attempts int maxloop=25; // maximum number of attempts to handle errors int LL2SL=10; // LossLimit to StopLoss server spread int maxOrders; // statistic for maximum numbers or orders open at one time double maxLots; // Largest lot size ever opened int MagicNumber = 252537; // allows multiple experts to trade on same account string TradeComment = "_dhlF1a.txt"; // where to log information // Bar handling datetime bartime=0; // used to determine when a bar has moved // Loss Recovery int lotptr=0; // array pointer for Loss Recovery int maxptr=9; // maximum Loss Recovery array element int Step[10]; // how to step the IStep pointer int dayShort=1; //1 short per day int dayLong=1; //1 long per day // used for verbose error logging #include <stdlib.mqh> //+-------------+ //| Custom init | //|-------------+ // Called ONCE when EA is added to chart or recompiled int init() { // get normalized Point based on Broker decimal places myPoint = SetPoint(); // start with the right sized lots //lotsi=Lots; // grow Lots if using StartingBalance Lotz=Lots; if(StartingBalance>0 && Lots==0) { Lotz=NormalizeDouble(AccountBalance()/StartingBalance,LotResolution); } lotsi=Lotz; // NO LOSS RECOVERY if (MartingaleStyle==0) { logwrite(TradeComment,"Loss Recovery Style = 0 - NONE"); Step[0]=1; Step[1]=1; Step[2]=1; Step[3]=1; Step[4]=1; Step[5]=1; Step[6]=1; Step[7]=1; Step[8]=1; Step[9]=1; } // Martingale if (MartingaleStyle==1) { logwrite(TradeComment,"Loss Recovery Style = 1 - MART"); Step[0]=1; Step[1]=2; Step[2]=4; Step[3]=8; Step[4]=16; Step[5]=32; Step[6]=64; Step[7]=128; Step[8]=256; Step[9]=512; } // Martingale off by 1 if (MartingaleStyle==2) { logwrite(TradeComment,"Loss Recovery Style = 1 - MART-MOD"); Step[0]=1; Step[1]=1; Step[2]=2; Step[3]=4; Step[4]=8; Step[5]=16; Step[6]=32; Step[7]=64; Step[8]=128; Step[9]=256; } // Fibonacci if (MartingaleStyle==3) { logwrite(TradeComment,"Loss Recovery Style = 3 - FIBO"); Step[0]=1; Step[1]=1; Step[2]=2; Step[3]=3; Step[4]=5; Step[5]=8; Step[6]=13; Step[7]=21; Step[8]=34; Step[9]=55; } // Martingale Partial if (MartingaleStyle==4) { logwrite(TradeComment,"Loss Recovery Style = 4 - MART-PART"); Step[0]=1; Step[1]=1; Step[2]=1; Step[3]=1; Step[4]=2; Step[5]=4; Step[6]=8; Step[7]=16; Step[8]=32; Step[9]=64; } if(IsTesting()) LL2SL=100; if(IsTesting()) maxloop=2 ; logwrite(TradeComment,"Init Complete"); Comment(" "); } //+----------------+ //| Custom DE-init | //+----------------+ // Called ONCE when EA is removed from chart int deinit() { // always indicate deinit statistics logwrite(TradeComment,"MAX number of orders "+maxOrders); logwrite(TradeComment,"Max Lots is "+maxLots); logwrite(TradeComment,"DE-Init Complete"); Comment(" "); } //+-----------+ //| Main | //+-----------+ // Called EACH TICK and each Bar[] int start() { int cnt=0; int gle=0; int ticket=0; int OrdersPerSymbol=0; int fib50; // stoploss and takeprofit and close control double SL=0; double TP=0; double CurrentProfit=0; double CurrentBasket=0; // direction control bool BUYme=false; bool SELLme=false; //safety counter int loopcount=0; //Sunday bar int backbar; // bar counting if(bartime!=Time[0]) { bartime=Time[0]; dayLong=0; dayShort=0; } // grow Lots if using StartingBalance Lotz=Lots; if(StartingBalance>0 && Lots==0) { Lotz=NormalizeDouble(AccountBalance()/StartingBalance,LotResolution); } OrdersPerSymbol=0; for(cnt=OrdersTotal();cnt>=0;cnt--) { OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); if( OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber) OrdersPerSymbol++; } if(OrdersPerSymbol==0) TradeAllowed=true; // keep some statistics if(OrdersPerSymbol>maxOrders) maxOrders=OrdersPerSymbol; //uncomment for MULTIPLE trades per bar if(OrdersPerSymbol==0) TradeAllowed=true; //+-----------------------------+ //| Insert your indicator here | //| And set either BUYme or | //| SELLme true to place orders | //+-----------------------------+ if(DayOfWeek() == 0) backbar=2; else backbar=1; fib50=High[backbar]-((High[backbar]-Low[backbar])/2); if (Close[0]>fib50) BUYme=true; if (Close[0]<fib50) SELLme=true; //+------------+ //| End Insert | //+------------+ //ENTRY LONG (buy, Ask) if( dayLong==0 && TradeAllowed && BUYme) { dayLong++; OpenBuy(); } //ENTRY SHORT (sell, Bid) if( dayShort==0 && TradeAllowed && SELLme) { dayShort++; OpenSell(); } // // Order Management // // Basket profit or loss - count the profit/loss // from this EA & Symbol()& magicnumber only // calculation can be done in PIPS or DOLLARS CurrentBasket=0; for(cnt=OrdersTotal();cnt>=0;cnt--) { OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); if( OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber) { // add up pips //if(OrderType()==OP_BUY ) CurrentBasket=CurrentBasket+( Close[0]-OrderOpenPrice() ); //if(OrderType()==OP_SELL ) CurrentBasket=CurrentBasket+( (Close[0]-OrderOpenPrice()) * (-1) ); //add up dollars if(OrderType()==OP_BUY ) CurrentBasket=CurrentBasket+( OrderProfit() ); if(OrderType()==OP_SELL ) CurrentBasket=CurrentBasket+( OrderProfit() ); } } if( BasketProfit>0 && CurrentBasket>=BasketProfit ) CloseEverything(); if( BasketLoss >0 && CurrentBasket<=BasketLoss*(-1) ) CloseEverything(); for(cnt=OrdersTotal();cnt>=0;cnt--) { OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); if( OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber ) { if(OrderType()==OP_BUY) { CurrentProfit=(Bid-OrderOpenPrice()) ; // // Modify for break even //======================= // // OrderStopLoss will be equal to OrderOpenPrice if this event happens // thus it will only ever get executed one time per ticket if( BreakEven>0 ) { if (CurrentProfit >= BreakEven*myPoint && OrderOpenPrice()>OrderStopLoss()) { SL=OrderOpenPrice()+(Ask-Bid); TP=OrderTakeProfit(); OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP, White); gle=GetLastError(); if(gle==0) { logwrite(TradeComment,"MODIFY BUY BE Ticket="+OrderTicket()+" SL="+SL+" TP="+TP); } else { logwrite(TradeComment,"-----ERROR----- MODIFY BUY BE Bid="+Bid+" error="+gle+" "+ErrorDescription(gle)); } } } // // check for trailing stop //========================= // // This starts trailing after 'TrailStop' pips of profit if( TrailStop>0 && Close[0]>OrderOpenPrice()+(TrailStop*myPoint) ) { if( OrderStopLoss() < Bid-(TrailStop*myPoint) ) { SL=Bid-(TrailStop*myPoint); TP=OrderTakeProfit(); OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,0,White); gle=GetLastError(); if(gle==0) { logwrite(TradeComment,"MODIFY BUY TS Ticket="+OrderTicket()+" SL="+SL+" TP="+TP); lotsi=Lotz; lotptr=0; } else { logwrite(TradeComment,"-----ERROR----- MODIFY BUY TS Bid="+Bid+" error="+gle+" "+ErrorDescription(gle)+" "); } } } // Did we make a profit //====================== if(ProfitMade>0 && CurrentProfit>=(ProfitMade*myPoint)) { CloseBuy("PROFIT"); // PROFIT, so reset the lot size per user inputs lotsi=Lotz; lotptr=0; } // Did we take a loss //==================== if(LossLimit>0 && CurrentProfit<=(LossLimit*(-1)*myPoint)) { CloseBuy("LOSS"); lotptr++; if(lotptr>maxptr) lotptr=0; if(MartingaleStyle>0) { lotsi=NormalizeDouble(Lotz*Step[lotptr],LotResolution); logwrite(TradeComment,"BUY lotptr="+lotptr+" Step="+Step[lotptr]); } } } // if BUY if(OrderType()==OP_SELL) { CurrentProfit=(OrderOpenPrice()-Ask); //logwrite(TradeComment,"SELL CurrentProfit="+CurrentProfit/myPoint+" CurrentBasket="+CurrentBasket/myPoint); // // Modify for break even //======================= // // OrderStopLoss will be equal to OrderOpenPrice if this event happens // thus it will only ever get executed one time per ticket if( BreakEven>0 ) { if (CurrentProfit >= BreakEven*myPoint && OrderOpenPrice()<OrderStopLoss()) { SL=OrderOpenPrice()-(Ask-Bid); TP=OrderTakeProfit(); OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP, Red); gle=GetLastError(); if(gle==0) { logwrite(TradeComment,"MODIFY SELL BE Ticket="+OrderTicket()+" SL="+SL+" TP="+TP); } else { logwrite(TradeComment,"-----ERROR----- MODIFY SELL BE Ask="+Ask+" error="+gle+" "+ErrorDescription(gle)); } } } // // check for trailing stop //========================= // // This starts trailing after 'TrailStop' pips of profit if( TrailStop>0 && Close[0]<OrderOpenPrice()-(TrailStop*myPoint) ) { if( OrderStopLoss() > Ask+(TrailStop*myPoint) ) { SL=Ask+(TrailStop*myPoint); TP=OrderTakeProfit(); OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,0,Red); gle=GetLastError(); if(gle==0) { logwrite(TradeComment,"MODIFY SELL TS Ticket="+OrderTicket()+" SL="+SL+" TP="+TP); lotsi=Lotz; lotptr=0; } else { logwrite(TradeComment,"-----ERROR----- MODIFY SELL TS Ask="+Ask+" error="+gle+" "+ErrorDescription(gle)); } } } // Did we make a profit //====================== if( ProfitMade>0 && CurrentProfit>=(ProfitMade*myPoint) ) { CloseSell("PROFIT"); // PROFIT, so reset the lot size per user inputs lotsi=Lotz; lotptr=0; } // Did we take a loss //==================== if( LossLimit>0 && CurrentProfit<=(LossLimit*(-1)*myPoint) ) { CloseSell("LOSS"); lotptr++; if(lotptr>maxptr) lotptr=0; if(MartingaleStyle>0) { lotsi=NormalizeDouble(Lotz*Step[lotptr],LotResolution); logwrite(TradeComment,"SELL lotptr="+lotptr+" Step="+Step[lotptr]); } } } //if SELL } // if(OrderSymbol) } // for } // start() //+-----------------+ //| CloseEverything | //+-----------------+ // Closes all OPEN and PENDING orders int CloseEverything() { int i; for(i=OrdersTotal();i>=0;i--) { OrderSelect(i, SELECT_BY_POS); if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber) { if(OrderType()==OP_BUY) CloseBuy ("BASKET"); if(OrderType()==OP_SELL) CloseSell("BASKET"); if(OrderType()==OP_BUYLIMIT) OrderDelete( OrderTicket() ); if(OrderType()==OP_SELLLIMIT) OrderDelete( OrderTicket() ); if(OrderType()==OP_BUYSTOP) OrderDelete( OrderTicket() ); if(OrderType()==OP_SELLSTOP) OrderDelete( OrderTicket() ); } Sleep(1000); } //for } // closeeverything // log data to a file name passed in // print everything regardless of log setting void logwrite (string filename, string mydata) { int myhandle; string gregorian=TimeToStr(CurTime(),TIME_DATE|TIME_SECONDS); Print(mydata+" "+gregorian); // don't log anything if testing or if user doesn't want it if(IsTesting()) return(0); if(KillLogging) return(0); myhandle=FileOpen(Symbol()+"_"+filename, FILE_CSV|FILE_WRITE|FILE_READ, ";"); if(myhandle>0) { FileSeek(myhandle,0,SEEK_END); FileWrite(myhandle, mydata+" "+gregorian); FileClose(myhandle); } } //ENTRY LONG (buy, Ask) void OpenBuy() { int gle=0; int ticket=0; double SL=0; double TP=0; int loopcount; // PLACE order is independent of MODIFY order. // This is mandatory for ECNs and acceptable for retail brokers loopcount=0; while(true) { // place order - NO TP OR SL ticket=OrderSend(Symbol(),OP_BUY,lotsi,Ask,Slippage,0,0,TradeComment,MagicNumber,White); gle=GetLastError(); if(gle==0) { logwrite(TradeComment,"BUY PLACED Ticket="+ticket+" Ask="+Ask+" Lots="+lotsi); TradeAllowed=false; break; } else { logwrite(TradeComment,"-----ERROR----- Placing BUY order: Lots="+lotsi+" Bid="+Bid+" Ask="+Ask+" ticket="+ticket+" Err="+gle+" "+ErrorDescription(gle)); RefreshRates(); Sleep(500); // give up after loopcount tries loopcount++; if(loopcount>maxloop) { logwrite(TradeComment,"-----ERROR----- Giving up on placing BUY order"); return(gle); } } }//while - place order // modify the order for users TP & SL loopcount=0; while(true) { // don't set TP and SL both to zero, they're already there if(LossLimit==0 && ProfitMade==0) break; if(LossLimit ==0) SL=0; if(ProfitMade ==0) TP=0; if(LossLimit >0) SL=Ask-((LossLimit+LL2SL)*myPoint ); if(ProfitMade >0) TP=Ask+((ProfitMade+LL2SL)*myPoint ); OrderModify(ticket,OrderOpenPrice(),SL,TP,0,White); gle=GetLastError(); if(gle==0) { logwrite(TradeComment,"BUY MODIFIED Ticket="+ticket+" Ask="+Ask+" Lots="+lotsi+" SL="+SL+" TP="+TP); break; } else { logwrite(TradeComment,"-----ERROR----- Modifying BUY order: Lots="+lotsi+" SL="+SL+" TP="+TP+" Bid="+Bid+" Ask="+Ask+" ticket="+ticket+" Err="+gle+" "+ErrorDescription(gle)); RefreshRates(); Sleep(500); loopcount++; if(loopcount>maxloop) { logwrite(TradeComment,"-----ERROR----- Giving up on modifying BUY order"); return(gle); } } }//while - modify order }//BUYme //ENTRY SHORT (sell, Bid) void OpenSell() { int gle=0; int ticket=0; double SL=0; double TP=0; int loopcount; // PLACE order is independent of MODIFY order. // This is mandatory for ECNs and acceptable for retail brokers loopcount=0; while(true) { ticket=OrderSend(Symbol(),OP_SELL,lotsi,Bid,Slippage,0,0,TradeComment,MagicNumber,Red); gle=GetLastError(); if(gle==0) { logwrite(TradeComment,"SELL PLACED Ticket="+ticket+" Bid="+Bid+" Lots="+lotsi); TradeAllowed=false; break; } else { logwrite(TradeComment,"-----ERROR----- placing SELL order: Lots="+lotsi+" SL="+SL+" TP="+TP+" Bid="+Bid+" Ask="+Ask+" ticket="+ticket+" Err="+gle+" "+ErrorDescription(gle)); RefreshRates(); Sleep(500); loopcount++; if(loopcount>maxloop) { logwrite(TradeComment,"-----ERROR----- Giving up on placing SELL order"); return(gle); } } }//while // modify the order for users TP & SL loopcount=0; while(true) { // don't set TP and SL both to zero, they're already there if(LossLimit==0 && ProfitMade==0) break; if(LossLimit ==0) SL=0; if(ProfitMade ==0) TP=0; if(LossLimit >0) SL=Bid+((LossLimit+LL2SL)*myPoint ); if(ProfitMade >0) TP=Bid-((ProfitMade+LL2SL)*myPoint ); OrderModify(ticket,OrderOpenPrice(),SL,TP,0,Red); gle=GetLastError(); if(gle==0) { logwrite(TradeComment,"SELL MODIFIED Ticket="+ticket+" Bid="+Bid+" Lots="+lotsi+" SL="+SL+" TP="+TP); TradeAllowed=false; break; } else { logwrite(TradeComment,"-----ERROR----- modifying SELL order: Lots="+lotsi+" SL="+SL+" TP="+TP+" Bid="+Bid+" Ask="+Ask+" ticket="+ticket+" Err="+gle+" "+ErrorDescription(gle)); RefreshRates(); Sleep(500); loopcount++; if(loopcount>maxloop) { logwrite(TradeComment,"-----ERROR----- Giving up on placing SELL order"); return(gle); } } }//while }//SELLme void CloseBuy (string myInfo) { int gle; int cnt; int OrdersPerSymbol; int loopcount=0; string bTK=" Ticket="+OrderTicket(); string bSL=" SL="+OrderStopLoss(); string bTP=" TP="+OrderTakeProfit(); string bPM; string bLL; string bER; bPM=" PM="+ProfitMade; bLL=" LL="+LossLimit; while(true) { OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,White); gle=GetLastError(); bER=" error="+gle+" "+ErrorDescription(gle); if(gle==0) { logwrite(TradeComment,"CLOSE BUY "+myInfo+ bTK + bSL + bTP + bPM + bLL); break; } else { logwrite(TradeComment,"-----ERROR----- CLOSE BUY "+myInfo+ bER +" Bid="+Bid+ bTK + bSL + bTP + bPM + bLL); RefreshRates(); Sleep(500); } loopcount++; if(loopcount>maxloop) { logwrite(TradeComment,"-----ERROR----- Giving up on closing SELL order"); return(gle); } }//while } void CloseSell (string myInfo) { int gle; int cnt; int OrdersPerSymbol; int loopcount=0; string sTK=" Ticket="+OrderTicket(); string sSL=" SL="+OrderStopLoss(); string sTP=" TP="+OrderTakeProfit(); string sPM; string sLL; string sER; sPM=" PM="+ProfitMade; sLL=" LL="+LossLimit; while(true) { OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Red); gle=GetLastError(); sER=" error="+gle+" "+ErrorDescription(gle); if(gle==0) { logwrite(TradeComment,"CLOSE SELL "+myInfo + sTK + sSL + sTP + sPM + sLL); break; } else { logwrite(TradeComment,"-----ERROR----- CLOSE SELL "+myInfo+ sER +" Ask="+Ask+ sTK + sSL + sTP + sPM + sLL); RefreshRates(); Sleep(500); } loopcount++; if(loopcount>maxloop) { logwrite(TradeComment,"-----ERROR----- Giving up on closing SELL order"); return(gle); } }//while } // Function to correct the value of Point // for brokers that add an extra digit to price // Courtesy of Robert Hill double SetPoint() { double mPoint; if (Digits < 4) mPoint = 0.01; else mPoint = 0.0001; return(mPoint); }
Sample
Analysis
Market Information Used:
Series array that contains open time of each bar
Series array that contains the highest prices of each bar
Series array that contains the lowest prices of each bar
Series array that contains close prices for each bar
Indicator Curves created:
Indicators Used:
Custom Indicators Used:
Order Management characteristics:
Checks for the total of open orders
It can change open orders parameters, due to possible stepping strategy
It automatically opens orders when conditions are reached
It Closes Orders by itself
Other Features:
Uses files from the file system
It writes information to file