extern double Long_Entry_pdr = 5; extern double Short_Entry_pdr = 5; extern double Long_stop_pdr = 3; extern double Short_stop_pdr = 3; extern double Lots_Acc_Equity = 1; extern double TrailingStop_pdr = 5; extern double TakeProfit = 200; extern double Slippage = 3; extern double TrailingStopMode = true; double TrailingStop; int Hour_between_Period = 24; int Current_day,Previous_day; int Previous_hour=0; double Lots=1.0; double yesterday_high=0; double yesterday_low=0; double yesterday_close=0; double today_open=0; double today_long_SL=0; double today_short_SL=0; double today_buy=0; double today_sell=0; double yesterday_range=0; double Acc_Start_Equity; string EAName = "FORXY"; // identifies the EA int uniqueMagic = 20060122; // Magic number of the trades int IsPositionOpen=false; //datetime Last_long_time=0, Last_short_time=0; void CheckOpenOrders() { int total = OrdersTotal(); { for(int i=total-1;i>=0;i--) OrderSelect(i, SELECT_BY_POS); int type = OrderType(); bool result = false; if ( OrderSymbol()==Symbol() && ( (OrderMagicNumber() == uniqueMagic) || (OrderComment() == EAName)) ) // only look if myEA and symbol... { //Close opened long positions if ( type == OP_BUY && OrderProfit()>0) { result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), Slippage, Green ); IsPositionOpen=false; } //Close opened short positions if ( type == OP_SELL && OrderProfit()>0) { result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), Slippage, Red ); IsPositionOpen=false; } } } return; } int init() { Acc_Start_Equity=AccountEquity()/3; Previous_day=Day(); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int start() { //Short_Entry_pdr=Long_Entry_pdr; Short_stop_pdr=Long_stop_pdr; int cnt, ticket, total; if (Previous_day!=Day() && DayOfWeek()>0 && DayOfWeek()<6) { yesterday_high= iHigh(NULL,PERIOD_H4,1); yesterday_low= iLow(NULL,PERIOD_H4,1); yesterday_close= iClose(NULL,PERIOD_H4,1); today_open= iOpen(NULL,PERIOD_H4,0); yesterday_range=(yesterday_high-yesterday_low); /* today_pivot= (yesterday_high+yesterday_low+yesterday_close)/3; today_S1= 2*today_pivot-yesterday_high; today_S2= today_R1= 2*today_pivot-yesterday_high; today_R2= */ today_buy= today_open-yesterday_range*(Long_Entry_pdr/22)-MarketInfo(Symbol(),MODE_SPREAD)*Point; today_sell= today_open-yesterday_range*(Short_Entry_pdr/22); TrailingStop=yesterday_range*(TrailingStop_pdr/100); today_buy=MathRound(today_buy/Point)*Point; today_sell=MathRound(today_sell/Point)*Point; today_long_SL= today_buy-yesterday_range*(Long_stop_pdr/100); today_short_SL= today_sell-yesterday_range*(Short_stop_pdr/100)-MarketInfo(Symbol(),MODE_SPREAD)*Point; Lots= 0.1*MathCeil( (Lots_Acc_Equity/100)/( ((today_buy-today_long_SL)/Point)/(AccountFreeMargin()-Acc_Start_Equity)) ); if (Lots<0.1) Lots=0.1; Print("Day: ",Day()," Order_Total : ",OrdersTotal()," Lots : ",Lots); if (IsPositionOpen==true) CheckOpenOrders(); if (OrdersTotal()>0 && IsPositionOpen==false) { int totalorders = OrdersTotal(); for(int j=totalorders-1;j>=0;j--) // scan all orders and positions... { OrderSelect(j, SELECT_BY_POS); if ( OrderSymbol()==Symbol() && ( (OrderMagicNumber() == uniqueMagic) || (OrderComment() == EAName)) ) { int type = OrderType(); if ( type > 1 ) bool result = OrderDelete( OrderTicket() ); } } } if (OrdersTotal()==0) { ticket=OrderSend(Symbol(),OP_BUYSTOP,Lots,today_buy,Slippage,today_long_SL,today_buy+Point*TakeProfit,EAName,uniqueMagic,0,Green); ticket=OrderSend(Symbol(),OP_SELLSTOP,Lots,today_sell,Slippage,today_short_SL,today_sell-Point*TakeProfit,EAName,uniqueMagic,0,Red); IsPositionOpen=false; } Previous_day= Day(); } else {Previous_day= Day();} total = OrdersTotal(); for(int i=total-1;i>=0;i--) { OrderSelect(i, SELECT_BY_POS); type = OrderType(); result = false; if ( OrderSymbol()==Symbol() && ( (OrderMagicNumber() == uniqueMagic) || (OrderComment() == EAName)) ) // only look if myEA and symbol... { if(OrderType()==OP_BUY) { if(TrailingStopMode==true && TrailingStop > 0) { if(Bid - OrderOpenPrice() > Point * TrailingStop) { if(OrderStopLoss() < Bid - Point * TrailingStop) { OrderModify(OrderTicket(), OrderOpenPrice(), Bid - Point * TrailingStop, OrderTakeProfit(), 0, MediumSeaGreen); return(0); } } } } if(OrderType()==OP_SELL) { if(TrailingStopMode==true && TrailingStop > 0) { if((OrderOpenPrice() - Ask) > (Point * TrailingStop)) { if((OrderStopLoss() > (Ask + Point * TrailingStop)) || (OrderStopLoss() == 0)) { OrderModify(OrderTicket(), OrderOpenPrice(), Ask + Point * TrailingStop, OrderTakeProfit(), 0, DarkOrange); return(0); } } } } if ( type == OP_BUY || type == OP_SELL && IsPositionOpen==false) IsPositionOpen= true; if (IsPositionOpen==true && type>1) result = OrderDelete( OrderTicket() ); } } return(0); } // the end.