//+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ #property copyright "Box-Master (by Transcendreamer)" #property description "Stop-Reverse Auto-Trading Bot" #property strict input double box_value =10; input double target_ratio =1; input double reverse_mult =1; input int reverse_limit =2; input int lot_digits =2; input double lot_divider =1; input int order_attempts =20; input int order_pause =1000; input bool stop_reverse =false; input bool safe_closing =false; #ifndef __MQL4__ input ENUM_ORDER_TYPE_FILLING filling_type=ORDER_FILLING_FOK; #endif input ENUM_BASE_CORNER text_corner=CORNER_RIGHT_UPPER; input color text_color=clrRed; string FONTNAME="Verdana"; int FONTSIZE=10; color FONTCOLOR=text_color; int SPACING=18; int YOFFSET=18; int XOFFSET=5; datetime saved_time; int total_boxes,total_lines; double current=0,previous=0; double box_profit,box_volume,box_drawdown; int box_reversals; bool event_open_upper,event_open_lower; bool event_close_upper,event_close_lower; double border_upper[],border_lower[]; double target_upper[],target_lower[]; double lot_min[],lot_max[]; double off_level[]; bool box_active[],line_active[]; bool box_buy[],box_sell[],box_trend[],box_timing[]; string box_name[],line_name[]; string box_comment[],line_comment[]; int box_magic[],line_magic[]; datetime box_time[]; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int OnInit() { clean_old(); scan_lines(); scan_boxes(); return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void OnChartEvent(const int id,const long &lparam,const double &dparam,const string &sparam) { if(id==CHARTEVENT_OBJECT_CLICK) for(int i=0; iprice1)?price0:price1; double lower=(price0box_time[i]) off_box(i); } } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void check_lines() { for(int i=0; ioff_level[i] && previous>off_level[i]) continue; if(current=border_upper[i]) && (previousborder_lower[i]); event_close_upper = (current>=target_upper[i]); event_close_lower = (current<=target_lower[i]); check_box_close(i); check_box_open(i); check_box_reverse(i); check_box_safe_close(i); update_status(i); } } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void update_status(int i) { string name="STATUS_"+box_name[i]; string text_min=DoubleToString(lot_min[i],lot_digits); string text_max=DoubleToString(lot_max[i],lot_digits); string text_vol=DoubleToString(box_volume,lot_digits); string text_pnl=DoubleToString(box_profit,2); string text_rev=IntegerToString(box_reversals); string text=box_name[i]+ " LOTS:"+text_min+"-"+text_max+ " VOL:"+text_vol+ " P/L:"+text_pnl+ " REV:"+text_rev; ENUM_ANCHOR_POINT text_anchor=0; if(text_corner==CORNER_LEFT_LOWER) text_anchor=ANCHOR_LEFT_LOWER; if(text_corner==CORNER_LEFT_UPPER) text_anchor=ANCHOR_LEFT_UPPER; if(text_corner==CORNER_RIGHT_LOWER) text_anchor=ANCHOR_RIGHT_LOWER; if(text_corner==CORNER_RIGHT_UPPER) text_anchor=ANCHOR_RIGHT_UPPER; ObjectCreate(0,name,OBJ_LABEL,0,0,0); ObjectSetString(0,name,OBJPROP_TEXT,text); ObjectSetString(0,name,OBJPROP_FONT,FONTNAME); ObjectSetInteger(0,name,OBJPROP_FONTSIZE,FONTSIZE); ObjectSetInteger(0,name,OBJPROP_COLOR,FONTCOLOR); ObjectSetInteger(0,name,OBJPROP_CORNER,text_corner); ObjectSetInteger(0,name,OBJPROP_ANCHOR,text_anchor); ObjectSetInteger(0,name,OBJPROP_XDISTANCE,XOFFSET); ObjectSetInteger(0,name,OBJPROP_YDISTANCE,YOFFSET+i*SPACING); ObjectSetInteger(0,name,OBJPROP_SELECTABLE,false); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void check_box_close(int i) { if(event_close_upper) if(box_volume>0) { Print("UPPER LIMIT CLOSING FOR BOX ",box_comment[i]); close_all(box_magic[i]); off_box(i); return; } if(event_close_lower) if(box_volume<0) { Print("LOWER LIMIT CLOSING FOR BOX ",box_comment[i]); close_all(box_magic[i]); off_box(i); return; } } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void check_box_open(int i) { if(event_open_upper) if(!box_sell[i]) if(box_volume==0) { Print("OPENING UP FOR BOX ",box_comment[i]); Print("VOLUME: ",DoubleToString(lot_min[i],lot_digits)); do_open(lot_min[i],box_magic[i],box_comment[i]); return; } if(event_open_lower) if(!box_buy[i]) if(box_volume==0) { Print("OPENING DOWN FOR BOX ",box_comment[i]); Print("VOLUME: ",DoubleToString(lot_min[i],lot_digits)); do_open(-lot_min[i],box_magic[i],box_comment[i]); return; } } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void check_box_reverse(int i) { if(box_reversals>=reverse_limit) { Print("FULL STOP FOR BOX ",box_comment[i]); close_all(box_magic[i]); off_box(i); return; } if(event_open_upper) if(box_volume<0) { double new_volume=get_new_volume(i); Print("REVERSAL UP FOR BOX ",box_comment[i]); Print("VOLUME: ",new_volume); if(safe_closing) save_drawdown(i); if(stop_reverse) close_all(box_magic[i]); do_open(new_volume,box_magic[i],box_comment[i]);; GlobalVariableSet("REVERSALS_"+box_name[i],++box_reversals); return; } if(event_open_lower) if(box_volume>0) { double new_volume=get_new_volume(i); Print("REVERSAL DOWN FOR BOX ",box_comment[i]); Print("VOLUME: ",new_volume); if(safe_closing) save_drawdown(i); if(stop_reverse) close_all(box_magic[i]); do_open(new_volume,box_magic[i],box_comment[i]); GlobalVariableSet("REVERSALS_"+box_name[i],++box_reversals); return; } } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void check_box_safe_close(int i) { if(!safe_closing) return; if(box_drawdown<=0) return; if(box_profit0) do_open(lot_min[i],box_magic[i],box_comment[i]); if(box_volume<0) do_open(-lot_min[i],box_magic[i],box_comment[i]); return; } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ double get_new_volume(int i) { double new_volume=-box_volume*reverse_mult; if(!stop_reverse) new_volume=new_volume-box_volume; return(new_volume); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void save_drawdown(int i) { box_drawdown-=box_profit; GlobalVariableSet("DRAWDOWN_"+box_name[i],box_drawdown); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void off_box(int i) { Print("SWITCHING OFF BOX ",box_comment[i]); box_active[i]=false; GlobalVariableDel("DRAWDOWN_"+box_name[i]); GlobalVariableDel("REVERSALS_"+box_name[i]); string new_name="box"+box_comment[i]; if(box_buy[i]) new_name="buy_"+new_name; if(box_sell[i]) new_name="sell_"+new_name; if(box_trend[i]) new_name="trend_"+new_name; if(box_timing[i]) new_name="time_"+new_name; ObjectDelete(0,"STATUS_"+box_name[i]); ObjectSetString(0,box_name[i],OBJPROP_TEXT,""); ObjectSetString(0,box_name[i],OBJPROP_NAME,new_name); ObjectDelete(0,"TARGET_UPPER_"+box_comment[i]); ObjectDelete(0,"TARGET_LOWER_"+box_comment[i]); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void off_line(int i) { Print("SWITCHING OFF LINE ",line_comment[i]); line_active[i]=false; string new_name="off"+line_comment[i]; ObjectSetString(0,box_name[i],OBJPROP_NAME,new_name); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ double get_volume(long magic) { #ifdef __MQL4__ double volume=0; int orders=OrdersTotal(); for(int k=0; k0) ticket=OrderSend(_Symbol,OP_BUY,NormalizeDouble(volume,lot_digits),Ask,0,0,0,comment,magic,0,clrLime); else if(volume<0) ticket=OrderSend(_Symbol,OP_SELL,NormalizeDouble(-volume,lot_digits),Bid,0,0,0,comment,magic,0,clrRed); if(ticket!=-1) {check=true;break;} Print("TRADING ERROR CODE ",GetLastError()); Sleep(order_pause); RefreshRates(); } if(!check) Alert("OPENING FAILED FOR MAGIC "+string(magic)); return(check); #else bool check=false; for(int i=0; i0) { request.type=ORDER_TYPE_BUY; request.price=SymbolInfoDouble(_Symbol,SYMBOL_ASK); request.volume=NormalizeDouble(volume,lot_digits); } else if(volume<0) { request.type=ORDER_TYPE_SELL; request.price=SymbolInfoDouble(_Symbol,SYMBOL_BID); request.volume=NormalizeDouble(-volume,lot_digits); } else return(false); request.symbol=_Symbol; request.action=TRADE_ACTION_DEAL; request.type_filling=filling_type; request.magic=magic; request.comment=comment; check=OrderSend(request,result); if(check) break; Print("TRADING ERROR CODE "+(string)result.retcode); Sleep(order_pause); } if(!check) Alert("OPENING FAILED FOR MAGIC "+string(magic)); return(check); #endif } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ bool do_close(ulong ticket,int magic,double volume) { #ifdef __MQL4__ bool check=false; for(int i=0; i0?volume:OrderLots(); if(OrderType()==OP_BUY) check=OrderClose((int)ticket,lots,Bid,0,clrMagenta); if(OrderType()==OP_SELL) check=OrderClose((int)ticket,lots,Ask,0,clrMagenta); if(check) break; Print("TRADING ERROR CODE ",GetLastError()); Sleep(order_pause); RefreshRates(); } if(!check) Alert("CLOSING FAILED FOR MAGIC "+string(magic)); return(check); #else bool check=false; for(int i=0; i0?NormalizeDouble(volume,lot_digits):PositionGetDouble(POSITION_VOLUME); request.symbol=PositionGetString(POSITION_SYMBOL); if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY) { request.type=ORDER_TYPE_SELL; request.price=SymbolInfoDouble(Symbol(),SYMBOL_BID); } else { request.type=ORDER_TYPE_BUY; request.price=SymbolInfoDouble(Symbol(),SYMBOL_ASK); } check=OrderSend(request,result); if(check) break; Print("TRADING ERROR CODE "+(string)result.retcode); Sleep(order_pause); } if(!check) Alert("CLOSING FAILED FOR MAGIC "+string(magic)); return(check); #endif } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void close_all(int magic) { #ifdef __MQL4__ for(int k=OrdersTotal()-1; k>=0; k--) { bool select=OrderSelect(k,SELECT_BY_POS,MODE_TRADES); if(!select) continue; if(OrderMagicNumber()!=magic) continue; if(OrderSymbol()!=Symbol()) continue; do_close(OrderTicket(),magic,0); } #else for(int k=PositionsTotal()-1; k>=0; k--) { ulong ticket=PositionGetTicket(k); if(!PositionSelectByTicket(ticket)) continue; if(PositionGetInteger(POSITION_MAGIC)!=magic) continue; if(PositionGetString(POSITION_SYMBOL)!=Symbol()) continue; do_close(ticket,magic,0); } #endif } //+------------------------------------------------------------------+