//+------------------------------------------------------------------+ //| Lars Fiverr EA.mq4 | //| Copyright 2022, MetaQuotes Software Corp. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2022, MetaQuotes Software Corp." #property link "https://www.mql5.com" #property version "1.00" #property strict extern string EA_Settings="<<<<>>>>"; extern double Risk=2;//Risk % extern double Takeprofit=4;//Takeprofit % extern color TPCol=clrGreen;//TP Line Colour extern color SLCol=clrRed;//SL Line Colour int Slipage=5;//Slipage double StoplossLevel=0; double TakeprofitLevel=0; int PrevCount=0; double pipValue = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE); double TPPointsByLots=AccountBalance()*Takeprofit*0.01/pipValue; double SLPointsByLots=AccountBalance()*Risk*0.01/pipValue; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- int CountNow=CountOrders(OP_BUY)+CountOrders(OP_SELL); if(CountNow>0 && GetTotalProfit()!=0 && GetTotalLot()!=0) { pipValue = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE); TPPointsByLots=AccountBalance()*Takeprofit*0.01/pipValue; SLPointsByLots=AccountBalance()*Risk*0.01/pipValue; ObjectDelete(0,"TPLevel"); ObjectDelete(0,"SLLevel"); if(GetTotalLotPerType(OP_BUY)>0 && GetTotalLotPerType(OP_SELL)==0) { TakeprofitLevel=Ask+(TPPointsByLots*_Point-GetTotalLotByPoint())/GetTotalLot(); StoplossLevel=Bid-(SLPointsByLots*_Point+GetTotalLotByPoint())/GetTotalLot(); ObjectCreate("TPLevel",OBJ_HLINE,0,0,NormalizeDouble(TakeprofitLevel,_Digits)); ObjectSetInteger(0,"TPLevel",OBJPROP_COLOR,TPCol); ObjectSetInteger(0,"TPLevel",OBJPROP_STYLE,STYLE_SOLID); ObjectSetInteger(0,"TPLevel",OBJPROP_WIDTH,1); ObjectSetInteger(0,"TPLevel",OBJPROP_BACK,false); ObjectSetInteger(0,"TPLevel",OBJPROP_SELECTABLE,false); ObjectCreate("SLLevel",OBJ_HLINE,0,0,NormalizeDouble(StoplossLevel,_Digits)); ObjectSetInteger(0,"SLLevel",OBJPROP_COLOR,SLCol); ObjectSetInteger(0,"SLLevel",OBJPROP_STYLE,STYLE_SOLID); ObjectSetInteger(0,"SLLevel",OBJPROP_WIDTH,1); ObjectSetInteger(0,"SLLevel",OBJPROP_BACK,false); ObjectSetInteger(0,"SLLevel",OBJPROP_SELECTABLE,false); } else if(GetTotalLotPerType(OP_BUY)==0 && GetTotalLotPerType(OP_SELL)>0) { TakeprofitLevel=Bid-(TPPointsByLots*_Point+GetTotalLotByPoint())/GetTotalLot(); StoplossLevel=Ask+(SLPointsByLots*_Point-GetTotalLotByPoint())/GetTotalLot(); ObjectCreate("TPLevel",OBJ_HLINE,0,0,NormalizeDouble(TakeprofitLevel,_Digits)); ObjectSetInteger(0,"TPLevel",OBJPROP_COLOR,TPCol); ObjectSetInteger(0,"TPLevel",OBJPROP_STYLE,STYLE_SOLID); ObjectSetInteger(0,"TPLevel",OBJPROP_WIDTH,1); ObjectSetInteger(0,"TPLevel",OBJPROP_BACK,false); ObjectSetInteger(0,"TPLevel",OBJPROP_SELECTABLE,false); ObjectCreate("SLLevel",OBJ_HLINE,0,0,NormalizeDouble(StoplossLevel,_Digits)); ObjectSetInteger(0,"SLLevel",OBJPROP_COLOR,SLCol); ObjectSetInteger(0,"SLLevel",OBJPROP_STYLE,STYLE_SOLID); ObjectSetInteger(0,"SLLevel",OBJPROP_WIDTH,1); ObjectSetInteger(0,"SLLevel",OBJPROP_BACK,false); ObjectSetInteger(0,"SLLevel",OBJPROP_SELECTABLE,false); } else if(GetTotalLotPerType(OP_BUY)>GetTotalLotPerType(OP_SELL) && GetTotalLotPerType(OP_BUY)>0 && GetTotalLotPerType(OP_SELL)>0) { TakeprofitLevel=Ask+(TPPointsByLots*_Point-GetTotalLotByPointPlus())/GetTotalLot(); StoplossLevel=Bid-(SLPointsByLots*_Point-GetTotalLotByPointPlus())/GetTotalLot(); ObjectCreate("TPLevel",OBJ_HLINE,0,0,NormalizeDouble(TakeprofitLevel,_Digits)); ObjectSetInteger(0,"TPLevel",OBJPROP_COLOR,TPCol); ObjectSetInteger(0,"TPLevel",OBJPROP_STYLE,STYLE_SOLID); ObjectSetInteger(0,"TPLevel",OBJPROP_WIDTH,1); ObjectSetInteger(0,"TPLevel",OBJPROP_BACK,false); ObjectSetInteger(0,"TPLevel",OBJPROP_SELECTABLE,false); ObjectCreate("SLLevel",OBJ_HLINE,0,0,NormalizeDouble(StoplossLevel,_Digits)); ObjectSetInteger(0,"SLLevel",OBJPROP_COLOR,SLCol); ObjectSetInteger(0,"SLLevel",OBJPROP_STYLE,STYLE_SOLID); ObjectSetInteger(0,"SLLevel",OBJPROP_WIDTH,1); ObjectSetInteger(0,"SLLevel",OBJPROP_BACK,false); ObjectSetInteger(0,"SLLevel",OBJPROP_SELECTABLE,false); } else if(GetTotalLotPerType(OP_BUY)0 && GetTotalLotPerType(OP_SELL)>0) { TakeprofitLevel=Bid-(TPPointsByLots*_Point-GetTotalLotByPointPlus())/GetTotalLot(); StoplossLevel=Ask+(SLPointsByLots*_Point-GetTotalLotByPointPlus())/GetTotalLot(); ObjectCreate("TPLevel",OBJ_HLINE,0,0,NormalizeDouble(TakeprofitLevel,_Digits)); ObjectSetInteger(0,"TPLevel",OBJPROP_COLOR,TPCol); ObjectSetInteger(0,"TPLevel",OBJPROP_STYLE,STYLE_SOLID); ObjectSetInteger(0,"TPLevel",OBJPROP_WIDTH,1); ObjectSetInteger(0,"TPLevel",OBJPROP_BACK,false); ObjectSetInteger(0,"TPLevel",OBJPROP_SELECTABLE,false); ObjectCreate("SLLevel",OBJ_HLINE,0,0,NormalizeDouble(StoplossLevel,_Digits)); ObjectSetInteger(0,"SLLevel",OBJPROP_COLOR,SLCol); ObjectSetInteger(0,"SLLevel",OBJPROP_STYLE,STYLE_SOLID); ObjectSetInteger(0,"SLLevel",OBJPROP_WIDTH,1); ObjectSetInteger(0,"SLLevel",OBJPROP_BACK,false); ObjectSetInteger(0,"SLLevel",OBJPROP_SELECTABLE,false); } } else if(CountNow==0) { ObjectDelete(0,"TPLevel"); ObjectDelete(0,"SLLevel"); } PrevCount=CountNow; //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- ObjectDelete(0,"TPLevel"); ObjectDelete(0,"SLLevel"); } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- int CountNow=CountOrders(OP_BUY)+CountOrders(OP_SELL); if(CountNow>0&& GetTotalProfit()!=0 && GetTotalLot()!=0) { ObjectDelete(0,"TPLevel"); ObjectDelete(0,"SLLevel"); pipValue = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE); TPPointsByLots=AccountBalance()*Takeprofit*0.01/pipValue; SLPointsByLots=AccountBalance()*Risk*0.01/pipValue; if(GetTotalLotPerType(OP_BUY)>0 && GetTotalLotPerType(OP_SELL)==0) { TakeprofitLevel=Ask+(TPPointsByLots*_Point-GetTotalLotByPoint())/GetTotalLot(); StoplossLevel=Bid-(SLPointsByLots*_Point+GetTotalLotByPoint())/GetTotalLot(); ObjectCreate("TPLevel",OBJ_HLINE,0,0,NormalizeDouble(TakeprofitLevel,_Digits)); ObjectSetInteger(0,"TPLevel",OBJPROP_COLOR,TPCol); ObjectSetInteger(0,"TPLevel",OBJPROP_STYLE,STYLE_SOLID); ObjectSetInteger(0,"TPLevel",OBJPROP_WIDTH,1); ObjectSetInteger(0,"TPLevel",OBJPROP_BACK,false); ObjectSetInteger(0,"TPLevel",OBJPROP_SELECTABLE,false); ObjectCreate("SLLevel",OBJ_HLINE,0,0,NormalizeDouble(StoplossLevel,_Digits)); ObjectSetInteger(0,"SLLevel",OBJPROP_COLOR,SLCol); ObjectSetInteger(0,"SLLevel",OBJPROP_STYLE,STYLE_SOLID); ObjectSetInteger(0,"SLLevel",OBJPROP_WIDTH,1); ObjectSetInteger(0,"SLLevel",OBJPROP_BACK,false); ObjectSetInteger(0,"SLLevel",OBJPROP_SELECTABLE,false); } else if(GetTotalLotPerType(OP_BUY)==0 && GetTotalLotPerType(OP_SELL)>0) { TakeprofitLevel=Bid-(TPPointsByLots*_Point+GetTotalLotByPoint())/GetTotalLot(); StoplossLevel=Ask+(SLPointsByLots*_Point-GetTotalLotByPoint())/GetTotalLot(); ObjectCreate("TPLevel",OBJ_HLINE,0,0,NormalizeDouble(TakeprofitLevel,_Digits)); ObjectSetInteger(0,"TPLevel",OBJPROP_COLOR,TPCol); ObjectSetInteger(0,"TPLevel",OBJPROP_STYLE,STYLE_SOLID); ObjectSetInteger(0,"TPLevel",OBJPROP_WIDTH,1); ObjectSetInteger(0,"TPLevel",OBJPROP_BACK,false); ObjectSetInteger(0,"TPLevel",OBJPROP_SELECTABLE,false); ObjectCreate("SLLevel",OBJ_HLINE,0,0,NormalizeDouble(StoplossLevel,_Digits)); ObjectSetInteger(0,"SLLevel",OBJPROP_COLOR,SLCol); ObjectSetInteger(0,"SLLevel",OBJPROP_STYLE,STYLE_SOLID); ObjectSetInteger(0,"SLLevel",OBJPROP_WIDTH,1); ObjectSetInteger(0,"SLLevel",OBJPROP_BACK,false); ObjectSetInteger(0,"SLLevel",OBJPROP_SELECTABLE,false); } else if(GetTotalLotPerType(OP_BUY)>GetTotalLotPerType(OP_SELL) && GetTotalLotPerType(OP_BUY)>0 && GetTotalLotPerType(OP_SELL)>0) { TakeprofitLevel=Ask+(TPPointsByLots*_Point-GetTotalLotByPointPlus())/GetTotalLot(); StoplossLevel=Bid-(SLPointsByLots*_Point-GetTotalLotByPointPlus())/GetTotalLot(); ObjectCreate("TPLevel",OBJ_HLINE,0,0,NormalizeDouble(TakeprofitLevel,_Digits)); ObjectSetInteger(0,"TPLevel",OBJPROP_COLOR,TPCol); ObjectSetInteger(0,"TPLevel",OBJPROP_STYLE,STYLE_SOLID); ObjectSetInteger(0,"TPLevel",OBJPROP_WIDTH,1); ObjectSetInteger(0,"TPLevel",OBJPROP_BACK,false); ObjectSetInteger(0,"TPLevel",OBJPROP_SELECTABLE,false); ObjectCreate("SLLevel",OBJ_HLINE,0,0,NormalizeDouble(StoplossLevel,_Digits)); ObjectSetInteger(0,"SLLevel",OBJPROP_COLOR,SLCol); ObjectSetInteger(0,"SLLevel",OBJPROP_STYLE,STYLE_SOLID); ObjectSetInteger(0,"SLLevel",OBJPROP_WIDTH,1); ObjectSetInteger(0,"SLLevel",OBJPROP_BACK,false); ObjectSetInteger(0,"SLLevel",OBJPROP_SELECTABLE,false); } else if(GetTotalLotPerType(OP_BUY)0 && GetTotalLotPerType(OP_SELL)>0) { TakeprofitLevel=Bid-(TPPointsByLots*_Point-GetTotalLotByPointPlus())/GetTotalLot(); StoplossLevel=Ask+(SLPointsByLots*_Point-GetTotalLotByPointPlus())/GetTotalLot(); ObjectCreate("TPLevel",OBJ_HLINE,0,0,NormalizeDouble(TakeprofitLevel,_Digits)); ObjectSetInteger(0,"TPLevel",OBJPROP_COLOR,TPCol); ObjectSetInteger(0,"TPLevel",OBJPROP_STYLE,STYLE_SOLID); ObjectSetInteger(0,"TPLevel",OBJPROP_WIDTH,1); ObjectSetInteger(0,"TPLevel",OBJPROP_BACK,false); ObjectSetInteger(0,"TPLevel",OBJPROP_SELECTABLE,false); ObjectCreate("SLLevel",OBJ_HLINE,0,0,NormalizeDouble(StoplossLevel,_Digits)); ObjectSetInteger(0,"SLLevel",OBJPROP_COLOR,SLCol); ObjectSetInteger(0,"SLLevel",OBJPROP_STYLE,STYLE_SOLID); ObjectSetInteger(0,"SLLevel",OBJPROP_WIDTH,1); ObjectSetInteger(0,"SLLevel",OBJPROP_BACK,false); ObjectSetInteger(0,"SLLevel",OBJPROP_SELECTABLE,false); } } else if(CountNow==0) { ObjectDelete(0,"TPLevel"); ObjectDelete(0,"SLLevel"); } PrevCount=CountNow; } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ int CountOrders(int OType) { int C=0; for(int i=OrdersTotal();i>=0;i--) { bool SelectSells= OrderSelect(i,SELECT_BY_POS); if(SelectSells) { if(OrderSymbol()==Symbol() && OrderType()==OType) { C++; } } } return(C); } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ double GetTotalValuePerPoint() { double BuyValuePerPoint=0; double SellValuePerPoint=0; for(int i=OrdersTotal();i>=0;i--) { bool SelectBuys= OrderSelect(i,SELECT_BY_POS); if(SelectBuys) { if( OrderSymbol()==Symbol()) { if(OrderType()==OP_BUY && OrderProfit()!=0) { BuyValuePerPoint=BuyValuePerPoint+(OrderProfit()/(Bid-OrderOpenPrice())); } if(OrderType()==OP_SELL && OrderProfit()!=0) { SellValuePerPoint=SellValuePerPoint+(OrderProfit()/(OrderOpenPrice()-Ask)); } } } } return(BuyValuePerPoint+SellValuePerPoint); } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ double GetTotalLotByPoint() { double BuyLotByPoint=0; double SellLotByPoint=0; for(int i=OrdersTotal();i>=0;i--) { bool SelectBuys= OrderSelect(i,SELECT_BY_POS); if(SelectBuys) { if( OrderSymbol()==Symbol()) { if(OrderType()==OP_BUY) { BuyLotByPoint=BuyLotByPoint+(Bid-OrderOpenPrice())*OrderLots(); } if(OrderType()==OP_SELL) { SellLotByPoint=SellLotByPoint+(OrderOpenPrice()-Ask)*OrderLots(); } } } } return(BuyLotByPoint-SellLotByPoint); } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ double GetTotalLotByPointPlus() { double BuyLotByPoint=0; double SellLotByPoint=0; for(int i=OrdersTotal();i>=0;i--) { bool SelectBuys= OrderSelect(i,SELECT_BY_POS); if(SelectBuys) { if( OrderSymbol()==Symbol()) { if(OrderType()==OP_BUY) { BuyLotByPoint=BuyLotByPoint+(Bid-OrderOpenPrice())*OrderLots(); } if(OrderType()==OP_SELL) { SellLotByPoint=SellLotByPoint+(OrderOpenPrice()-Ask)*OrderLots(); } } } } return(BuyLotByPoint+SellLotByPoint); } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ double GetTotalLot() { double BuyLot=0; double SellLot=0; for(int i=OrdersTotal();i>=0;i--) { bool SelectBuys= OrderSelect(i,SELECT_BY_POS); if(SelectBuys) { if( OrderSymbol()==Symbol()) { if(OrderType()==OP_BUY) { BuyLot=BuyLot+OrderLots(); } if(OrderType()==OP_SELL) { SellLot=SellLot+OrderLots(); } } } } return(MathAbs(BuyLot-SellLot)); } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ double GetTotalLotPerType(int Otype) { double Lot=0; for(int i=OrdersTotal();i>=0;i--) { bool SelectBuys= OrderSelect(i,SELECT_BY_POS); if(SelectBuys) { if( OrderSymbol()==Symbol()) { if(OrderType()==Otype) { Lot=Lot+OrderLots(); } } } } return(Lot); } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ double GetTotalProfit() { double Profit=0; for(int i=OrdersTotal();i>=0;i--) { bool SelectSells= OrderSelect(i,SELECT_BY_POS); if(SelectSells) { if(OrderSymbol()==Symbol()) { Profit=Profit+OrderProfit(); } } } return(Profit); } //+------------------------------------------------------------------+