#property copyright "Copyright 2021, algobaba." #property link "http://www.algobaba.com" #property version "1.00" #property strict #include #include ///////////////////////////////////////////////////////////////// (MA Settings #1) input string Q1 = "(01) I N I T I A L S T R A T E G Y S E T T I N G ------------------------------------------------------------------------------------------------------------------------" ; input int MA_period_fast = 15; // MA Period input int MA_shift_fast = 0; // MA Shift // int MA_price_first = input int MA_period_slow = 70; // MA Period input int MA_shift_slow = 0; // MA Shift slow input string Strategy_Tag = "META1"; input string PostFix_buy = ""; input string PostFix_sell = ""; input string buy_condition = "LE"; // the condition of buy input string sell_condition = "SE"; // the condition of sell extern double LotSize =0.1; extern int TakeProfit = 600; // TakeProfit (Points) extern int StopLoss = 400; // StopLoss (Points) extern int MagicNumber = 123456; // trailing stop bool UseTrailingStop = false; int WhenToTrail = 10; int TrialAmount = 5; // break-even bool UseMoveToBreakEven = false; int WhenToMoveBe = 3; int PipsToLockIn = 1 ; int buy_ticket1 = 0; int sell_ticket1 =0; int buy_ticket = 0; int sell_ticket =0; double pips; int err; //// double opnl = 0.0; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int init() { //--- double ticksize = MarketInfo(Symbol(),MODE_TICKSIZE); if(ticksize == 0.00001 || ticksize == 0.001) pips = ticksize*10; else pips = ticksize; //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ int start() { ///////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// if(check_order_closed(buy_ticket))buy_ticket=0; if(check_order_closed(sell_ticket))sell_ticket=0; //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// //if(OpenOrdersOnThisPair(Symbol())==0) //if(Hour() >= from_hour && Hour() <= to_hour ) if(IsNewCandle()) { CheckForTrade(); } //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// return(0); } //+------------------------------------------------------------------+ bool IsNewCandle() { static int BarsOnChart =0; if(BarsOnChart == Bars)return(false); BarsOnChart = Bars; return (true); } bool IsNewDay() { static int NewDay = 0; if( Day() == NewDay)return (false); NewDay = Day(); return (true); } ////////////////////////////////////////////////////////////////////////////////////////////////////// void CheckForTrade() { //////////////////// // buy signal cross MA /////////////////////////// if (iMA(NULL,0,MA_period_fast,MA_shift_fast,0,0,1) > iMA(NULL,0,MA_period_slow,MA_shift_slow,0,0,1)&& iMA(NULL,0,MA_period_fast,MA_shift_fast,0,0,2) < iMA(NULL,0,MA_period_slow,MA_shift_slow,0,0,2) ) { if( buy_condition == "LE" ){ if( PostFix_buy!= "") { buy_ticket = OrderSend(Symbol()+" "+PostFix_buy,0,LotSize,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,"MoAtia_EA",MagicNumber,0,clrNONE); // IB_MappedOrderSimple(Symbol(),"LE",MarketInfo(Symbol(),MODE_ASK),Strategy_Tag); LongEntry(Symbol()+" "+PostFix_buy,LotSize,IntegerToString(TakeProfit),IntegerToString(StopLoss),Strategy_Tag); } else{ buy_ticket = OrderSend(Symbol(),0,LotSize,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,"MoAtia_EA",MagicNumber,0,clrNONE); // IB_MappedOrderSimple(Symbol(),"LE",MarketInfo(Symbol(),MODE_ASK),Strategy_Tag); LongEntry(Symbol(),LotSize,IntegerToString(TakeProfit),IntegerToString(StopLoss),Strategy_Tag); } if( PostFix_sell !="") { buy_ticket = OrderSend(Symbol()+" "+PostFix_sell,0,LotSize,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,"MoAtia_EA",MagicNumber,0,clrNONE); // IB_MappedOrderSimple(Symbol(),"LE",MarketInfo(Symbol(),MODE_ASK),Strategy_Tag); LongEntry(Symbol()+" "+PostFix_sell,LotSize,IntegerToString(TakeProfit),IntegerToString(StopLoss),Strategy_Tag); } else{ buy_ticket = OrderSend(Symbol(),0,LotSize,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,"MoAtia_EA",MagicNumber,0,clrNONE); // IB_MappedOrderSimple(Symbol(),"LE",MarketInfo(Symbol(),MODE_ASK),Strategy_Tag); LongEntry(Symbol(),LotSize,IntegerToString(TakeProfit),IntegerToString(StopLoss),Strategy_Tag); } }else { if( sell_condition == "SE" ) { if( PostFix_sell !="") { sell_ticket = OrderSend(Symbol()+" "+PostFix_sell,1,LotSize,Bid,3,Bid+StopLoss*Point,Bid-TakeProfit*Point,"MoAtia_EA",MagicNumber,0,clrNONE); // IB_MappedOrderSimple(Symbol(),"SE",MarketInfo(Symbol(),MODE_BID),Strategy_Tag); ShortEntry(Symbol()+" "+PostFix_sell,LotSize,IntegerToString(TakeProfit),IntegerToString(StopLoss),Strategy_Tag); } else{ sell_ticket = OrderSend(NULL,1,LotSize,Bid,3,Bid+StopLoss*Point,Bid-TakeProfit*Point,"MoAtia_EA",MagicNumber,0,clrNONE); // IB_MappedOrderSimple(Symbol(),"SE",MarketInfo(Symbol(),MODE_BID),Strategy_Tag); ShortEntry(Symbol(),LotSize,IntegerToString(TakeProfit),IntegerToString(StopLoss),Strategy_Tag); } if( PostFix_buy!= "") { sell_ticket = OrderSend(Symbol()+" "+PostFix_buy,1,LotSize,Bid,3,Bid+StopLoss*Point,Bid-TakeProfit*Point,"MoAtia_EA",MagicNumber,0,clrNONE); // IB_MappedOrderSimple(Symbol(),"SE",MarketInfo(Symbol(),MODE_BID),Strategy_Tag); ShortEntry(Symbol()+" "+PostFix_buy,LotSize,IntegerToString(TakeProfit),IntegerToString(StopLoss),Strategy_Tag); } else{ sell_ticket = OrderSend(NULL,1,LotSize,Bid,3,Bid+StopLoss*Point,Bid-TakeProfit*Point,"MoAtia_EA",MagicNumber,0,clrNONE); // IB_MappedOrderSimple(Symbol(),"SE",MarketInfo(Symbol(),MODE_BID),Strategy_Tag); ShortEntry(Symbol(),LotSize,IntegerToString(TakeProfit),IntegerToString(StopLoss),Strategy_Tag); } } } } if (iMA(NULL,0,MA_period_fast,MA_shift_fast,0,0,1) < iMA(NULL,0,MA_period_slow,MA_shift_slow,0,0,1)&& iMA(NULL,0,MA_period_fast,MA_shift_fast,0,0,2) > iMA(NULL,0,MA_period_slow,MA_shift_slow,0,0,2) ) { if( sell_condition == "SE" ) { if( PostFix_sell !="") { sell_ticket = OrderSend(Symbol()+" "+PostFix_sell,1,LotSize,Bid,3,Bid+StopLoss*Point,Bid-TakeProfit*Point,"MoAtia_EA",MagicNumber,0,clrNONE); // IB_MappedOrderSimple(Symbol(),"SE",MarketInfo(Symbol(),MODE_BID),Strategy_Tag); ShortEntry(Symbol()+" "+PostFix_sell,LotSize,IntegerToString(TakeProfit),IntegerToString(StopLoss),Strategy_Tag); } else{ sell_ticket = OrderSend(NULL,1,LotSize,Bid,3,Bid+StopLoss*Point,Bid-TakeProfit*Point,"MoAtia_EA",MagicNumber,0,clrNONE); // IB_MappedOrderSimple(Symbol(),"SE",MarketInfo(Symbol(),MODE_BID),Strategy_Tag); ShortEntry(Symbol(),LotSize,IntegerToString(TakeProfit),IntegerToString(StopLoss),Strategy_Tag); } if( PostFix_buy!= "") { sell_ticket = OrderSend(Symbol()+" "+PostFix_sell,1,LotSize,Bid,3,Bid+StopLoss*Point,Bid-TakeProfit*Point,"MoAtia_EA",MagicNumber,0,clrNONE); // IB_MappedOrderSimple(Symbol(),"SE",MarketInfo(Symbol(),MODE_BID),Strategy_Tag); ShortEntry(Symbol()+" "+PostFix_sell,LotSize,IntegerToString(TakeProfit),IntegerToString(StopLoss),Strategy_Tag); } else{ sell_ticket = OrderSend(NULL,1,LotSize,Bid,3,Bid+StopLoss*Point,Bid-TakeProfit*Point,"MoAtia_EA",MagicNumber,0,clrNONE); // IB_MappedOrderSimple(Symbol(),"SE",MarketInfo(Symbol(),MODE_BID),Strategy_Tag); ShortEntry(Symbol(),LotSize,IntegerToString(TakeProfit),IntegerToString(StopLoss),Strategy_Tag); } }else { if( PostFix_buy!= "") { buy_ticket = OrderSend(Symbol()+" "+PostFix_buy,0,LotSize,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,"MoAtia_EA",MagicNumber,0,clrNONE); // IB_MappedOrderSimple(Symbol(),"LE",MarketInfo(Symbol(),MODE_ASK),Strategy_Tag); LongEntry(Symbol()+" "+PostFix_buy,LotSize,IntegerToString(TakeProfit),IntegerToString(StopLoss),Strategy_Tag); } else{ buy_ticket = OrderSend(Symbol(),0,LotSize,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,"MoAtia_EA",MagicNumber,0,clrNONE); // IB_MappedOrderSimple(Symbol(),"LE",MarketInfo(Symbol(),MODE_ASK),Strategy_Tag); LongEntry(Symbol(),LotSize,IntegerToString(TakeProfit),IntegerToString(StopLoss),Strategy_Tag); } if( PostFix_sell !="") { buy_ticket = OrderSend(Symbol()+" "+PostFix_buy,0,LotSize,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,"MoAtia_EA",MagicNumber,0,clrNONE); // IB_MappedOrderSimple(Symbol(),"LE",MarketInfo(Symbol(),MODE_ASK),Strategy_Tag); LongEntry(Symbol()+" "+PostFix_buy,LotSize,IntegerToString(TakeProfit),IntegerToString(StopLoss),Strategy_Tag); } else{ buy_ticket = OrderSend(Symbol(),0,LotSize,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,"MoAtia_EA",MagicNumber,0,clrNONE); // IB_MappedOrderSimple(Symbol(),"LE",MarketInfo(Symbol(),MODE_ASK),Strategy_Tag); LongEntry(Symbol(),LotSize,IntegerToString(TakeProfit),IntegerToString(StopLoss),Strategy_Tag); } } } } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// bool check_order_closed(int ticket) { int OrderTicketNumber=ticket; // whatever your reference order ticket # is for(int trade=OrdersHistoryTotal()-1;trade>=0;trade--) { if(!OrderSelect(trade,SELECT_BY_POS,MODE_HISTORY))printf("there is a problem with selecting history orders check_order_closed method " + IntegerToString(GetLastError())); if(OrderTicket()==OrderTicketNumber) { return true; break; } } return false; } int OpenOrdersOnThisPair(string pair) { int total =0; for(int i=OrdersTotal()-1; i>=0; i--) { if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) printf("there is a problem with selecting history orders OpenOrdersOnThisPair method" + IntegerToString(GetLastError())); if(OrderSymbol() == pair) total++; } return (total); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void ExitBuys_normal() { for (int i=OrdersTotal()-1; i >=0; i--) { if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { if (OrderType() == OP_BUY && (OrderMagicNumber()==MagicNumber)) { if(OrderSymbol() == Symbol()) { while(true)//infinite loop must be escaped by break { bool result = OrderClose(OrderTicket(), OrderLots(), Bid, 3, Red);//actual order closing if (result != true)//if it did not close { err = GetLastError(); Print("LastError = ",err);//get the reason why it didn't close } else {err = 0;break;}//if it did close it breaks out of while early DOES NOT RUN SWITCH switch(err) { case 129://INVALID_PRICE //if it was 129 it will run every line until it gets to the break. case 135://ERR_PRICE_CHANGED//same for 135 case 136://ERR_OFF_QUOTES//and 136 case 137://ERR_BROKER_BUSY//and 137 case 138://ERR_REQUOTE//and 138 case 146:Sleep(1000);RefreshRates();i++;break;//Sleeps,Refreshes and increments.Then breaks out of switch. default:break;//if the err does not match any of the above. It does not increment. and runs next order in series. } break;//after breaking out of switch it breaks out of while loop. which order it runs next depends on i++ or not. } } } } else Print( "When selecting a trade, error ",GetLastError()," occurred"); } } //+------------------------------------------------------------------+ //| function to close sell orders | //+------------------------------------------------------------------+ void ExitSells_normal() { for (int i=OrdersTotal()-1; i >=0; i--) { if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { if (OrderType() == OP_SELL && (OrderMagicNumber()==MagicNumber)) { if(OrderSymbol() == Symbol()) { while(true)//infinite loop must be escaped by break { bool result = OrderClose(OrderTicket(), OrderLots(), Ask, 3, Red);//actual order closing if (result != true)//if it did not close { err = GetLastError(); Print("LastError = ",err);//get the reason why it didn't close } else {err = 0;break;}//if it did close it breaks out of while early DOES NOT RUN SWITCH switch(err) { case 129://INVALID_PRICE //if it was 129 it will run every line until it gets to the break. case 135://ERR_PRICE_CHANGED//same for 135 case 136://ERR_OFF_QUOTES//and 136 case 137://ERR_BROKER_BUSY//and 137 case 138://ERR_REQUOTE//and 138 case 146:Sleep(1000);RefreshRates();i++;break;//Sleeps,Refreshes and increments.Then breaks out of switch. default:break;//if the err does not match any of the above. It does not increment. and runs next order in series. } break;//after breaking out of switch it breaks out of while loop. which order it runs next depends on i++ or not. } } } } else Print( "When selecting a trade, error ",GetLastError()," occurred"); } } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void MoveToBreakeven() { for(int b=OrdersTotal()-1; b >= 0; b--) { if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES)) if(OrderMagicNumber()== MagicNumber) if(OrderSymbol()==Symbol()) if(OrderType()==OP_BUY) if(Bid-OrderOpenPrice()>WhenToMoveBe*pips) if(OrderOpenPrice()>OrderStopLoss()) if(!OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+(PipsToLockIn*pips),OrderTakeProfit(),0,clrNONE) )printf(IntegerToString(GetLastError()) + " with breakeven method "); } for (int s=OrdersTotal()-1; s >= 0; s--) { if(OrderSelect(s,SELECT_BY_POS,MODE_TRADES)) if(OrderMagicNumber()== MagicNumber) if(OrderSymbol()==Symbol()) if(OrderType()==OP_SELL) if(OrderOpenPrice()-Ask>WhenToMoveBe*pips) if(OrderOpenPrice()=0;b--) { if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES)) if(OrderMagicNumber()==MagicNumber) if(OrderSymbol()==Symbol()) if(OrderType()==OP_BUY) if(Bid-OrderOpenPrice()>WhenToTrail*pips) if(OrderStopLoss()=0;s--) { if(OrderSelect(s,SELECT_BY_POS,MODE_TRADES)) if(OrderMagicNumber()== MagicNumber) if(OrderSymbol()==Symbol()) if(OrderType()==OP_SELL) if(OrderOpenPrice()-Ask>WhenToTrail*pips) if(OrderStopLoss()>Ask+TrialAmount*pips) if(!OrderModify(OrderTicket(),OrderOpenPrice(),Ask+(TrialAmount*pips),OrderTakeProfit(),0,clrNONE))printf(IntegerToString(GetLastError()) + " with breakeven method "); } } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+