//+------------------------------------------------------------------+ //| PERSONAL_ASSISTANT.mq4 | //| INFINITE LOOP | //| | //+------------------------------------------------------------------+ #property copyright "INFINITE LOOP" #property link "" #property version "1.00" #property strict #property description "Personal assistant is there to provide you with crucial information for making investment decisions and to execute your orders!" //+------------------------------------------------------------------+ //| User input variables | //+------------------------------------------------------------------+ input int ID=3900; input bool Allow_pen_orders=true; // Allows placement of pending orders input bool Set_SL = false; // Allows input of StopLoss for active orders before execution input bool Set_TP = false; // Allows input of TakeProfit for active orders before execution input bool Display_legend=false; // Allows display of action commands input double LotSize=0.01; input int slippage=2; input int text_size=10; input color text_color=clrBlack; input int right_edge_shift = 380; input int upper_edge_shift = 15; //+------------------------------------------------------------------+ //| Global variables | //+------------------------------------------------------------------+ string EA_name="PERSONAL_ASSISTANT"; string global_Volume = "VOLUME_" + IntegerToString(ID); string global_Record = "RECORD_" + IntegerToString(ID); string global_Price="PRICE_"+IntegerToString(ID); string global_StopLoss="SL_"+IntegerToString(ID); string global_TakeProfit="TP_"+IntegerToString(ID); //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { if(AccountInfoDouble(ACCOUNT_BALANCE)<5) return(INIT_FAILED); GlobalVariableSet(global_Volume,LotSize); GlobalVariableSet(global_Record,-1.0); GlobalVariableSet(global_Price,0.0); GlobalVariableSet(global_StopLoss,0.0); GlobalVariableSet(global_TakeProfit,0.0); return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { for(int z=1; z<=24; z++) ObjectDelete(ChartID(),EA_name+"_"+(string)z); GlobalVariableDel(global_Volume); GlobalVariableDel(global_Record); GlobalVariableDel(global_Price); GlobalVariableDel(global_StopLoss); GlobalVariableDel(global_TakeProfit); } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { int x=0, y=0, open_position_counter=0, sl_counter = 0, tp_counter = 0; double EA_profit=0, EA_takeProfit=0, EA_StopLoss=0, EA_volume=0; string text=""; //******************************************************************************************************************************** // Control over opened positions for(int pos=0; pos open BUY order manually button 2 -> open SELL order manually button 3 -> CLOSE BUY or SELL order manually button 4 -> increase current Lot volume (Lots in increments of 0.01) button 5 -> decrease current Lot volume (Lots in increments of 0.01) button 6 -> set PENDING BUY STOP order manually button 7 -> set PENDING SELL STOP order manually button 8 -> set PENDING BUY LIMIT order manually button 9 -> set PENDING SELL LIMIT order manually */ if(id==CHARTEVENT_KEYDOWN && (GlobalVariableGet(global_Record)==-1.0)) { // ********************************************************************************************************************* // button 1 if(lparam==49 || lparam==97) { if(!Set_SL && !Set_TP) if(OrderSend(_Symbol,OP_BUY,GlobalVariableGet(global_Volume),MarketInfo(_Symbol,MODE_ASK),slippage,0,0,EA_name+" "+IntegerToString(ID),ID,0,clrNONE)>0) Print("Order BUY successfully opened for ",EA_name,"_",ID); else Print("Order BUY unsuccessfully opened for ",EA_name,"_",ID); else { GlobalVariableSet(global_Record,lparam); if(Set_SL) createObjectPT(24,OBJ_TEXT,0,Time[0]+_Period*60*20,Bid,"Input SL"); else createObjectPT(24,OBJ_TEXT,0,Time[0]+_Period*60*20,Bid,"Input TP"); } } // ********************************************************************************************************************* // button 2 if(lparam==50 || lparam==98) { if(!Set_SL && !Set_TP) if(OrderSend(_Symbol,OP_SELL,GlobalVariableGet(global_Volume),MarketInfo(_Symbol,MODE_BID),slippage,0,0,EA_name+" "+IntegerToString(ID),ID,0,clrNONE)>0) Print("Order SELL successfully opened for ",EA_name,"_",ID); else Print("Order SELL unsuccessfully opened for ",EA_name,"_",ID); else { GlobalVariableSet(global_Record,lparam); if(Set_SL) createObjectPT(24,OBJ_TEXT,0,Time[0]+_Period*60*20,Bid,"Input SL"); else createObjectPT(24,OBJ_TEXT,0,Time[0]+_Period*60*20,Bid,"Input TP"); } } // ********************************************************************************************************************* // button 3 if(lparam==51 || lparam==99) { for(int pos=0; pos=MarketInfo(_Symbol,MODE_MAXLOT)) Alert("Top limit for order volume reached!"); else { GlobalVariableSet(global_Volume,GlobalVariableGet(global_Volume)+MarketInfo(_Symbol,MODE_LOTSTEP)); text="Lot amount = "+DoubleToString(GlobalVariableGet(global_Volume),2); createObject(4,OBJ_LABEL,0,x,y+upper_edge_shift*3,text); text="Tick value = "+DoubleToString(MarketInfo(_Symbol,MODE_TICKVALUE)*GlobalVariableGet(global_Volume),3)+" "+AccountInfoString(ACCOUNT_CURRENCY); createObject(5,OBJ_LABEL,0,x,y+upper_edge_shift*4,text); text="Margin required = "+DoubleToString(MarketInfo(_Symbol,MODE_MARGINREQUIRED)*GlobalVariableGet(global_Volume),3); createObject(6,OBJ_LABEL,0,x,y+upper_edge_shift*5,text); } } // ********************************************************************************************************************* // button 5 if(lparam==53 || lparam==101) { if(GlobalVariableGet(global_Volume)<=MarketInfo(_Symbol,MODE_MINLOT)) Alert("Volume is at minimum, it can not be decreased!"); else { GlobalVariableSet(global_Volume,GlobalVariableGet(global_Volume)-MarketInfo(_Symbol,MODE_LOTSTEP)); text="Lot amount = "+DoubleToString(GlobalVariableGet(global_Volume),2); createObject(4,OBJ_LABEL,0,x,y+upper_edge_shift*3,text); text="Tick value = "+DoubleToString(MarketInfo(_Symbol,MODE_TICKVALUE)*GlobalVariableGet(global_Volume),3)+" "+AccountInfoString(ACCOUNT_CURRENCY); createObject(5,OBJ_LABEL,0,x,y+upper_edge_shift*4,text); text="Margin required = "+DoubleToString(MarketInfo(_Symbol,MODE_MARGINREQUIRED)*GlobalVariableGet(global_Volume),3); createObject(6,OBJ_LABEL,0,x,y+upper_edge_shift*5,text); } } // ********************************************************************************************************************* // button 6, button 7, button 8, button 9 if((lparam>=54 && lparam<=57) || (lparam>=102 && lparam<=105)) if(Allow_pen_orders) { GlobalVariableSet(global_Record,lparam); createObjectPT(24,OBJ_TEXT,0,Time[0]+_Period*60*20,Bid,"Input PRICE"); } else Alert("Pending orders not allowed!"); } // RESET SELECTED ACTION (button 0) if(id==CHARTEVENT_KEYDOWN) if(GlobalVariableGet(global_Record)!=-1.0) if(lparam==48 || lparam==96) { GlobalVariableSet(global_Record,-1.0); glDelete(); ObjectDelete(ChartID(),EA_name+"_"+(string)24); } // MOUSE ON CHART EVENT -> LOCATION of MOUSE CLICK if(id==CHARTEVENT_CLICK && (GlobalVariableGet(global_Record)!=-1.0)) { double action=GlobalVariableGet(global_Record); int window=0; double price=0, SL = 0, TP = 0; datetime time=0; ChartXYToTimePrice(ChartID(),(int)lparam,(int)dparam,window,time,price); price=NormalizeDouble(price,_Digits); // determine what inputs are required for ACTIVE or PENDING TRADES // if active trade, and Set_SL TRUE, first set SL, then if Set_TP TRUE, set TP if(action==49.0 || action==50 || action==97.0 || action==98.0) { if(Set_SL && Set_TP) { if(GlobalVariableGet(global_TakeProfit)==0 && GlobalVariableGet(global_StopLoss)!=0) { GlobalVariableSet(global_TakeProfit,price); GlobalVariableSet(global_Record,-1.0); } if(GlobalVariableGet(global_StopLoss)==0) { GlobalVariableSet(global_StopLoss,price); createObjectPT(24,OBJ_TEXT,0,Time[0]+_Period*60*20,Bid,"Input TP"); } } if(!Set_TP && Set_SL) if(GlobalVariableGet(global_StopLoss)==0) GlobalVariableSet(global_StopLoss,price); if(Set_TP && !Set_SL) if(GlobalVariableGet(global_TakeProfit)==0) GlobalVariableSet(global_TakeProfit,price); } if((action>=54 && action<=57) || (action>=102 && action<=105)) if(GlobalVariableGet(global_Price)==0) { GlobalVariableSet(global_Price,price); createObjectPT(24,OBJ_TEXT,0,Time[0]+_Period*60*20,Bid,"Input SL"); } else if(GlobalVariableGet(global_StopLoss)==0) { GlobalVariableSet(global_StopLoss,price); createObjectPT(24,OBJ_TEXT,0,Time[0]+_Period*60*20,Bid,"Input TP"); } else if(GlobalVariableGet(global_TakeProfit)==0) { GlobalVariableSet(global_TakeProfit,price); GlobalVariableSet(global_Record,-1.0); } if(action==49.0 || action==50 || action==97.0 || action==98.0) { if(Set_SL && !Set_TP) GlobalVariableSet(global_Record,-1.0); if(!Set_SL && Set_TP) GlobalVariableSet(global_Record,-1.0); } SL = GlobalVariableGet(global_StopLoss); TP = GlobalVariableGet(global_TakeProfit); price=GlobalVariableGet(global_Price); // ********************************************************************************************************************* // BUY (button 1) if((action==49 || action==97) && GlobalVariableGet(global_Record)==-1.0) { ObjectDelete(ChartID(),EA_name+"_"+(string)24); if(((Set_SL && SLMarketInfo(_Symbol,MODE_ASK)) || !Set_TP)) if(OrderSend(_Symbol,OP_BUY,GlobalVariableGet(global_Volume),MarketInfo(_Symbol,MODE_ASK),slippage,SL,TP,EA_name+" "+IntegerToString(ID),ID,0,clrNONE)>0) Print("Order BUY successfully opened for ",EA_name,"_",ID); else Print("Order BUY unsuccessfully opened for ",EA_name,"_",ID); else Alert("StopLoss or TakeProfit for BUY order is incorrect!"); glDelete(); } // ********************************************************************************************************************* // SELL (button 2) if((action==50 || action==98) && GlobalVariableGet(global_Record)==-1.0) { ObjectDelete(ChartID(),EA_name+"_"+(string)24); if(((Set_SL && SL>MarketInfo(_Symbol,MODE_BID)) || !Set_SL) && ((Set_TP && TP0) Print("Order SELL successfully opened for ",EA_name,"_",ID); else Print("Order SELL unsuccessfully opened for ",EA_name,"_",ID); else Alert("StopLoss or TakeProfit for SELL order is incorrect!"); glDelete(); } // ********************************************************************************************************************* // BUY STOP (button 6) if((action==54.0 || action==102.0) && GlobalVariableGet(global_Record)==-1.0) { ObjectDelete(ChartID(),EA_name+"_"+(string)24); if(price>MarketInfo(_Symbol,MODE_ASK)) if(SLprice) if(OrderSend(_Symbol,OP_BUYSTOP,GlobalVariableGet(global_Volume),price,slippage,SL,TP,EA_name+" "+IntegerToString(ID),ID,0,clrNONE)>0) { Print("Order BUY STOP successfully opened for ",EA_name,"_",ID); Alert("Pending order BUY STOP set at price = ",price," with SL = ",SL," and TP = ",TP); } else { Print("Order BUY STOP unsuccessfully opened for ",EA_name,"_",ID); Alert("ERROR at setting Pending order BUY STOP!"); } else Alert("StopLoss or TakeProfit for BUY STOP order is incorrect!"); else Alert("The Price level set for BUY STOP order is outside allowed range!"); glDelete(); } // ********************************************************************************************************************* // SELL STOP (button 7) if((action==55.0 || action==103.0) && GlobalVariableGet(global_Record)==-1.0) { ObjectDelete(ChartID(),EA_name+"_"+(string)24); if(priceprice && TP0) { Print("Order SELL STOP successfully opened for ",EA_name,"_",ID); Alert("Pending order SELL STOP set at price = ",price," with SL = ",SL," and TP = ",TP); } else { Print("Order SELL STOP unsuccessfully opened for ",EA_name,"_",ID); Alert("ERROR at setting Pending order SELL STOP!"); } else Alert("StopLoss or TakeProfit for SELL STOP order is incorrect!"); else Alert("The Price level set for SELL STOP order is outside allowed range!"); glDelete(); } // ********************************************************************************************************************* // BUY LIMIT (button 8) if((action==56.0 || action==104.0) && GlobalVariableGet(global_Record)==-1.0) { ObjectDelete(ChartID(),EA_name+"_"+(string)24); if(priceprice) if(OrderSend(_Symbol,OP_BUYLIMIT,GlobalVariableGet(global_Volume),price,slippage,SL,TP,EA_name+" "+IntegerToString(ID),ID,0,clrNONE)>0) { Print("Order BUY LIMIT successfully opened for ",EA_name,"_",ID); Alert("Pending order BUY LIMIT set at price = ",price," with SL = ",SL," and TP = ",TP); } else { Print("Order BUY LIMIT unsuccessfully opened for ",EA_name,"_",ID); Alert("ERROR at setting Pending order BUY LIMIT!"); } else Alert("StopLoss or TakeProfit for BUY LIMIT order is incorrect!"); else Alert("The Price level set for BUY LIMIT order is outside allowed range!"); glDelete(); } // ********************************************************************************************************************* // SELL LIMIT (button 9) if((action==57.0 || action==105.0) && GlobalVariableGet(global_Record)==-1.0) { ObjectDelete(ChartID(),EA_name+"_"+(string)24); if(price>MarketInfo(_Symbol,MODE_BID)) if(SL>price && TP0) { Print("Order SELL LIMIT successfully opened for ",EA_name,"_",ID); Alert("Pending order SELL LIMIT set at price = ",price," with SL = ",SL," and TP = ",TP); } else { Print("Order SELL LIMIT unsuccessfully opened for ",EA_name,"_",ID); Alert("ERROR at setting Pending order SELL LIMIT!"); } else Alert("StopLoss or TakeProfit for SELL LIMIT order is incorrect!"); else Alert("The Price level set for SELL LIMIT order is outside allowed range!"); glDelete(); } } } //+------------------------------------------------------------------+ //| Custom delete global varables function | //+------------------------------------------------------------------+ void glDelete() { GlobalVariableSet(global_Price,0.0); GlobalVariableSet(global_StopLoss,0.0); GlobalVariableSet(global_TakeProfit,0.0); } //+------------------------------------------------------------------+