// extern string ___ = "==================================="; extern double lotsize = 0.01; extern int dperiod =14; extern int kperiod =3; extern int slowing =3; extern double tp_point =3000; extern double sl_point =500; extern double start_tralling_point =700; extern double Trailling_point =600; /* extern int stoplossDay = 0; extern int targetDay =0; */ int Magic=9; string Com = "Winmyself"; int LastType,solenhpair; bool active = true; //datetime ngayHetHan =D'2020.07.01 00:00:00'; bool testing; string mode,note; double covertp,tonglenh; datetime TradeTime; int init() { //if(TimeCurrent()ngayHetHan ) { ObjectText( "hello", "lien he de co duoc ban quyen - thinhle.free@gmail.com / t.me/Thinhlefree", 10, 50, 15, clrRed); active = false; }*/ if(IsTesting()==true || IsOptimization()==true) {testing=true; } // if(!IsTradeAllowed()){Alert("bam F7 va chon tab Common/Allow live trading hoac bam Auto trading");} return(0); } //================== void deinit(){} void start() { if(active==false){return;} //if(OrdersTotal()==tongsolenh&& OrdersTotal()!=0){return;}tongsolenh = OrdersTotal(); Trailling(); // if((stopLossEquity!=0 && AccountEquity()takeProfitEquity)) // {DONGLENH_ToanBo();return;} if(TradeTime==iTime(Symbol(),0,0)) {return;}TradeTime=iTime(Symbol(),0,0);//Sleep(10000); //////////////////////////////////////////////////////////////// checkOffline();// lay thong tin cac lenh history checkOnline(); //lay thong tin cac lenh dang trade ///////////////////////////////////////////////// // phan khai bao bien va goi indicator double op = -1; double pinsig = check_pinbar(1); double stochmain =iStochastic(Symbol(),0,kperiod,dperiod,slowing,MODE_EMA,PRICE_CLOSE,MODE_MAIN,1); double stochsignal =iStochastic(Symbol(),0,kperiod,dperiod,slowing,MODE_EMA,PRICE_CLOSE,MODE_SIGNAL,1); /////////////////////////////////// // vao lenh if(demlenh[OP_BUY] == 0 && Open[1] Close[1] ) { vaoLenh(OP_SELL,Symbol(),lotsize,0,sl_point ,tp_point , Magic, Com,POINT) ;//chedo_trade=3;LastType=OP_BUY; } } //=============== double check_pinbar(int candle) { double kq = -1; if(iCustom(Symbol(),0,"PinBar",1,candle) != EMPTY_VALUE){kq = OP_SELL;} if(iCustom(Symbol(),0,"PinBar",0,candle) != EMPTY_VALUE){kq = OP_BUY;} return(kq); } //+------------------------------------------------------------------+ void Trailling() { double NewSL; for(int i = OrdersTotal()-1; i >=0 ; i--) //OrdersHistoryTotal() { if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) {continue;} //Neu khong tim duoc lenh thi tiep tuc lap if (OrderSymbol() != Symbol()) {continue;} if (OrderMagicNumber() != Magic) {continue;} if (OrderType() == OP_BUY) {NewSL = Ask - Trailling_point*Point;} else if (OrderType() == OP_SELL) {NewSL = Bid + Trailling_point*Point;} if (OrderType() == OP_BUY && OrderStopLoss() OrderOpenPrice()+start_tralling_point*Point) { OrderModify(OrderTicket(),OrderOpenPrice(),NewSL,OrderTakeProfit(),0,CLR_NONE); } if (OrderType() == OP_SELL && OrderStopLoss()>NewSL && Bid< OrderOpenPrice()-start_tralling_point*Point ) { OrderModify(OrderTicket(),OrderOpenPrice(),NewSL,OrderTakeProfit(),0,CLR_NONE); } if(GetLastError()!=0 && GetLastError()!=1) {Print("Loi Trailling: " + GetLastError());} } } //////////////// double profitToday; void checkOffline() { //=====================DOAN NAY CHECK HISTORY================ // histime=0; // ArrayInitialize(histime,0); profitToday=0; /* ArrayInitialize(hisprofit,0); ArrayInitialize(hisop,0); ArrayInitialize(hisop,0); ArrayInitialize(hisOpen,0); ArrayInitialize(hislot,0); */ //symlist =""; // int group;hisrecordProfit=0; for( int i=0; i<=OrdersHistoryTotal()-1; i++) { if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) {continue;} // if (StringFind(sym,StringSubstr( OrderSymbol(),0,6),0)<0) {continue;} if(OrderType()>=2) {continue;} if(OrderMagicNumber()!=Magic){continue;} if(OrderOpenTime() > iTime(Symbol(),PERIOD_D1,0) ) {profitToday+= OrderProfit()+ OrderSwap()+ OrderCommission();} /*if(OrderCloseTime()>lasttime ) { lasttime = OrderCloseTime(); hisloss = StrToDouble(catchuoi(OrderComment(),"[","]")); hisprofitall = OrderProfit()+OrderSwap()+OrderCommission(); hisTylechia = StrToDouble(catchuoi(OrderComment(),"(",")")); histarget = StrToDouble(catchuoi(OrderComment(),"{","}")); }*/ // if(StringFind(OrderComment(),"record")>0 ){hisrecordProfit= hisprofit;} } } /////////////////////// //------------ //0 1 2 3 4 5 6 7 8 9 10 11 int indexNow; double tonglot;double totalProfit;//double lastprice; double giacaonhat[2];double giathapnhat[2]; int ticketcaonhat[2];int ticketthapnhat[2]; int ticketwinbuy[10];int ticketwinsell[10]; double profitlenhcaonhat[2];double profitlenhthapnhat[2]; double tongwin[2];double tonglose[2]; int demlenh[6]; bool allowtrade[2]; void checkOnline() { ArrayInitialize(demlenh,0); ArrayInitialize(giathapnhat,100000); ArrayInitialize(giacaonhat,0); ArrayInitialize(ticketwinbuy,0); ArrayInitialize(ticketwinbuy,0); ArrayInitialize(tongwin,0); ArrayInitialize(tonglose,0); ArrayInitialize(profitlenhcaonhat,0); ArrayInitialize(profitlenhthapnhat,0); //profitToDay =0; allowtrade[OP_BUY] = true; allowtrade[OP_SELL] =true; double profit; for( int i=OrdersTotal(); i>=0; i--) { if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) {continue;} if(OrderMagicNumber()!= Magic){continue;} demlenh[OrderType()]++; profit = OrderProfit()+OrderSwap()+OrderCommission(); if(OrderType()==OP_SELL) { /*if(Check_inside(OrderOpenPrice(),Ask+khoangcach*10*Point,Bid- khoangcach*10*Point)==true) { allowtrade[OP_SELL] =false; }*/ if(OrderOpenPrice()>giacaonhat[OP_SELL]) { giacaonhat[OP_SELL]= OrderOpenPrice(); ticketcaonhat[OP_SELL] = OrderTicket(); profitlenhcaonhat[OP_SELL] = profit; } if(OrderOpenPrice()0) { if(i>= ArrayRange(ticketwinsell,0)){ArrayResize(ticketwinsell,i+1);} ticketwinsell[i]=OrderTicket(); tongwin[OP_SELL]+=profit; } if(profit <0) { // ticketlose[OP_SELL][i]=OrderTicket(); tonglose[OP_SELL]+=profit; } }// end sell //////////////////////// if(OrderType()==OP_BUY) { /* if(Check_inside(OrderOpenPrice(),Ask+khoangcach*10*Point,Bid- khoangcach*10*Point)==true) { allowtrade[OP_BUY] =false; }*/ if(OrderOpenPrice()>giacaonhat[OP_BUY]) { giacaonhat[OP_BUY]= OrderOpenPrice(); //thuanhieunhat[OP_BUY] = profit; ticketcaonhat[OP_BUY] = OrderTicket(); profitlenhcaonhat[OP_BUY] = profit; } if(OrderOpenPrice()0) { if(i>= ArrayRange(ticketwinbuy,0)){ArrayResize(ticketwinbuy,i+1);} ticketwinbuy[i]=OrderTicket(); tongwin[OP_BUY]+=profit; //Comment(tongwin[OP_BUY]); } if(profit <0) { tonglose[OP_BUY]+=profit; } }// end buy //////////////// TP SL /* if(Check_inside(OrderOpenPrice(),Ask+3*khoangcach*10*Point,Bid- 3*khoangcach*10*Point)==false) {continue;} if(kieudanh== GO_LENH) { if(OrderStopLoss()!=0 || OrderTakeProfit()!=0) { OrderModify(OrderTicket(),OrderOpenPrice(),0,0,0,clrNONE); } } if(kieudanh== TRADE) { double tp; if(OrderType()==OP_BUY )//&& tpmode !=OP_SELL) { tp = OrderOpenPrice()+ takerate*khoangcach*10*Point; tp = NormalizeDouble(tp,Digits); // if(tp Ask+MarketInfo(OrderSymbol(),MODE_STOPLEVEL)*Point) { OrderModify(OrderTicket(),OrderOpenPrice(),0,tp,0,clrNONE); } } if(OrderType()==OP_SELL)//&& tpmode !=OP_BUY) { tp = OrderOpenPrice()- takerate*khoangcach*10*Point; tp = NormalizeDouble(tp,Digits); // if(tp>Ask){DongLenh_Ticket(OrderTicket(),OrderLots());} if( OrderTakeProfit()!=tp && tp < Bid-MarketInfo(OrderSymbol(),MODE_STOPLEVEL)*Point) { OrderModify(OrderTicket(),OrderOpenPrice(),0,tp,0,clrNONE); } } } */ }//end for } ///////// //------------ham nhac nho void ObjectText(string ObjName,string ObjText,int X,int Y,int fontSize,color clr_) { ObjectDelete(0,ObjName); int Check=ObjectFind(0,ObjName); if(Check<0) {ObjectCreate(ObjName,OBJ_LABEL,0,0,0);} //--> Hàm chính để vẽ ObjectSetText(ObjName,ObjText,fontSize,"Verdana",clr_); ObjectSet(ObjName,OBJPROP_CORNER,0); ObjectSet(ObjName,OBJPROP_BGCOLOR,clrBlack); ObjectSet(ObjName,OBJPROP_XDISTANCE,X); ObjectSet(ObjName,OBJPROP_YDISTANCE,Y); } //+------------------------------------------------------------------+ /////////////////////// /* void check_lenhdangtrade() { int count;string cm; for(int j = OrdersTotal();j>=0;j--) { if (!OrderSelect(j, SELECT_BY_POS, MODE_TRADES)) {continue;} if (OrderSymbol()!=Symbol()) {continue;} // if (OrderMagicNumber()!=Magic) {continue;} // if(khoangcachbase==0 ){continue;} if(OrderType()==OP_BUY && OrderStopLoss()>OrderOpenPrice()) {continue;} if(OrderType()==OP_SELL && OrderStopLoss() OrderOpenPrice()+breakEven_level*MathAbs(OrderOpenPrice()-OrderStopLoss())) {OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+spr,OrderTakeProfit(),0,0);reset();} if(OrderType()==OP_SELL&& Ask< OrderOpenPrice()-breakEven_level*MathAbs(OrderOpenPrice()-OrderStopLoss())) {OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-spr,OrderTakeProfit(),0,0);reset();} if(OrderType()==OP_BUY){price[1]= OrderOpenPrice();price[2] = price[1]-MathAbs(OrderOpenPrice()-OrderStopLoss());} if(OrderType()==OP_SELL){price[2]= OrderOpenPrice();price[1] = price[2]+MathAbs(OrderOpenPrice()-OrderStopLoss());} } } solenhpair =count; }*/ /* void reset() { khoangcachbase=0;price[1]=0;price[2]=0;ObjectsDeleteAll();been_inside=false;total=0; chedo_trade =0;lineup = 0;linedn = 100000; ovuong = 0; }*/ /////////////////////////// void RemoveObjects(string s8) { int t1=ObjectsTotal(); while(t1>=0) {if (StringFind(ObjectName(t1),s8,0)>-1) {ObjectDelete(ObjectName(t1));} t1--;}} //-------------- /* string thisrecname; void check_obj() { //----get che do trade /* if(khoangcachbase!=0){ if(StringFind(cm,"cover",0)>=0){chedo_trade=4;return;} if(count>0){chedo_trade=3;return;} } if(ObjectsTotal() >15){RemoveObjects("#");return;} double bid = MarketInfo(Symbol(),MODE_BID); double ask = MarketInfo(Symbol(),MODE_ASK); if(lineup !=0&&been_inside == false &&Check_inside((bid+ask)/2,linedn,lineup)==true){been_inside = true;} else if( been_inside==true &&(ask > lineup||bid < linedn ) ) { chedo_trade=2;} // if(khoangcachbase!=0 ){return;} //----------------------- namerec=""; int obj_total=ObjectsTotal(); if(obj_total ==0){return;} int demrec;//chedo_trade =0; string lastname_rec; linedn = 0;lineup =0; for(int i=0;i=0) { lastname_rec = namerec; demrec++;namerec = name; lineup = ObjectGet(name,1); linedn = ObjectGet(name,3); lineup = NormalizeDouble(lineup,MarketInfo(Symbol(),MODE_DIGITS)); linedn = NormalizeDouble(linedn,MarketInfo(Symbol(),MODE_DIGITS)); Magic = StringSubstr(namerec,9,StringLen(namerec)); // Print(Magic); } } if(MathAbs(linedn-lineup)>0&&MathAbs(linedn-lineup) < (lotGapThep)*MarketInfo(Symbol(),MODE_STOPLEVEL)*Point) { Print("o vuong qua nho: "+MathAbs(linedn-lineup)+"/"+MarketInfo(Symbol(),MODE_STOPLEVEL)*Point); ObjectsDeleteAll();reset(); return;} ovuong = demrec;thisrecname = namerec; if(demrec>1){ ObjectDelete(0,lastname_rec); return;} //------------ khoangcachbase =MathAbs(linedn-lineup);//*MathPow(10,MarketInfo(Symbol(),MODE_DIGITS)-1);//pip price[1]= lineup;price[2]= linedn; slpoint =khoangcachbase;//point tppoint = tp_rate*slpoint; } */ //------------ //======================= double GET_SD(string sym, int timefamre,int index1)// index 1 = highup.2= highdn,3 = lowup, 4 = lowdn { double level1,level2,level3,level4; int caynen_sup,caynen_dem; string text; //----- for( int i =1;i< Bars;i++) { double value = iCustom(sym,timefamre,"SupplyDemand_chinhsua",2,i);//cay nen tren if(value > 0 ) { level1 = iHigh(sym,timefamre,i); level2 = MathMin(iClose(sym,timefamre,i),iOpen(sym,timefamre,i)); level2 = MathMax(level2,MathMax(iLow(sym,timefamre,i-1),iLow(sym,timefamre,i+1))); level2 = MathMax(level2,MathMin(iOpen(sym,timefamre,i-1),iClose(sym,timefamre,i-1))); level2 = MathMax(level2,MathMin(iOpen(sym,timefamre,i+1),iClose(sym,timefamre,i+1))); // if(khoang_cach == false){break;} //Comment(value +" i "+ i); text += "cay nen up " +i; caynen_sup = i; bool next =false; for(int j = i-1;j>=0;j--) { if(iHigh(sym,timefamre,j)> level1){next = true;} } if(next == false){ break;} // break; // if(MathAbs(MarketInfo(sym,MODE_BID)-level2)> 2*MathAbs(level2-level1) ){ text += " cay nen up " +i; break;} } } for( i =1;i< Bars;i++) { value = iCustom(sym,timefamre,"SupplyDemand_chinhsua",5,i);//cay nen duoi if(value > 0 ) {//text += "cay nen down " +i; level4 = iLow(sym,timefamre,i); level3 = MathMax(iClose(sym,timefamre,i),iOpen(sym,timefamre,i)); if(i>0) level3 = MathMin(level3,MathMin(iHigh(sym,timefamre,i+1),iHigh(sym,timefamre,i-1))); if(i>0) level3 = MathMin(level3,MathMax(iOpen(sym,timefamre,i-1),iClose(sym,timefamre,i-1))); level3 = MathMin(level3,MathMax(iOpen(sym,timefamre,i+1),iClose(sym,timefamre,i+1))); caynen_dem = i; text += " cay nen dn " +i; next =false; for(j = i-1;j>=0;j--) { if(iLow(sym,timefamre,j) < level4){next = true;} } if(next == false){ break;} // break; // if(khoang_cach == false){break;} // if(MathAbs(MarketInfo(sym,MODE_BID)-level3)>2* MathAbs(level3-level4) ){ text += "cay nen dn " +i; break;} } } //Comment(text+" --- "+level1 +" " +level2 +" " +level3 +" " +level4 ); // Comment(text); if( index1 == 1){return(level1);} if( index1 == 2){return(level2);} if( index1 == 3){return(level3);} if( index1 == 4){return(level4);} if( index1 == 5){return(caynen_sup);} if( index1 == 6){return(caynen_dem);} } //------------- bool Check_inside(double gia1,double pllineduoi,double pllinetren) { double llineduoi = MathMin(pllineduoi,pllinetren); double llinetren = MathMax(pllineduoi,pllinetren); if( llineduoi <= gia1 && gia1 <= llinetren ){return (true);} else {return (false);} } //===================== /* void Tinh_price() { // int LastOrder=0; lastLotcore=Lot_Start; ArrayInitialize(price,0); for (int j=OrdersTotal()-1; j >= 0; j--) { if (!OrderSelect(j, SELECT_BY_POS, MODE_TRADES)) {continue;} if (OrderSymbol()!=Symbol()) {continue;} if (OrderMagicNumber()!=Magic) {continue;} LastOrder = MathMax(LastOrder,OrderTicket()); lastLotcore = MathMax(lastLotcore,OrderLots()); } int rec = OrderSelect(LastOrder, SELECT_BY_TICKET, MODE_TRADES); LastType = OrderType(); price[1]= lineup;price[2]= linedn; //Tinh level price[0] = price[1] + tppoint; //TP price[3] = price[2] - tppoint ; } */ /* double ConvertPoint(string Sym6) { double Point_ = MarketInfo(Sym6,MODE_POINT); double Dig = MarketInfo(Sym6,MODE_DIGITS); double Digits_; if (StringFind(Sym6,"GOL",0) >-1 || StringFind(Sym6,"XAU",0) >-1) {Digits_ = 10;} else if (StringFind(Sym6,"SIL",0) >-1 || StringFind(Sym6,"XGU",0) >-1) {Digits_ = MathPow(10,Dig-2);} else if(Dig ==3 || Dig==5) {Digits_ = 10;} else {Digits_=1;} double myPoint = Digits_*Point_; double minSL = MarketInfo(Symbol(),MODE_STOPLEVEL)*Point_; //--> Phai nhan voi point // if(slpoint==0) {slpoint=0;} else {slpoint *= myPoint; slpoint = MathMax(minSL,slpoint);} if(tppoint==0) {tppoint=0;} else {tppoint *= myPoint; tppoint = MathMax(tppoint,minSL);} slpoint = NormalizeDouble(slpoint,Dig); tppoint = NormalizeDouble(tppoint,Dig); //BuySell_Gap *= myPoint; return(myPoint); }*/ /* void Cat_Chuoi(string Chuoi, string PhanCach) { int sta=0, to=0; //int sta=-1, to=StringFind(Chuoi,PhanCach,0); string txtLot; Chuoi+=PhanCach; for (int i = 0; i<=1000; i++) { sta = StringFind(Chuoi,PhanCach,to); to = StringFind(Chuoi,PhanCach,sta+1); if (sta+1 >= StringLen(Chuoi)) {break;} ArrayResize(LotArray,i+1); LotArray[i] = StringToDouble(StringSubstr(Chuoi,sta+1,to-sta-1)); LotArray[i] *=Lot_Start; txtLot += LotArray[i]+","; } Print("Lot: " +txtLot); }*/ /* void DAT_LENH_0(int op) { // Lot_Start = if(fist_lot==0){ khoangcachbase = MathAbs(linedn-lineup)*MathPow(10,MarketInfo(Symbol(),MODE_DIGITS)-1);//pip Lot_Start= GET_LOT( Symbol(),khoangcachbase, Risk_Percent_, AccountBalance());//Comment("fist lot"+a); Cat_Chuoi(Lot_Multiple,","); } if(KtraLenh(3)!=0) {return;} double price; // LastType=op;//last_lot = Lot_Start; if (op==true) {LastType=0; price=Ask;} else {LastType=1;price=Bid;} balance_nguyenmau = AccountBalance(); DatLenh(op,Symbol(),Lot_Start,price,0,0,Magic,Com); chedo_trade=2; lenh_moi_nhat = op;lastLotcore = Lot_Start; Sleep(500);door=true; }*/ /* void DAT_LENH_Next(int type) { if(KtraLenh(type)>0){return;} double price;double TradeLot; if (type==true) {LastType=1; price=Ask;} else {LastType=0;price=Bid;} if(use_martingale_lot==true){ TradeLot = Tinh_Lot();} if(use_martingale_lot==false){ TradeLot = lastLotcore;} DatLenh(type,Symbol(),TradeLot,price,0,0,Magic,Com); lenh_moi_nhat = type;lastLotcore = TradeLot; Sleep(500); }*/ /* double Tinh_Lot() { int k; int Count = ArraySize(LotArray); // Print("lastLotcore_0: " + lastLotcore); for (int i=0; i= level_bien){tp_reward_percent =0;} if(k< level_bien){tp_reward_percent =tp_rate*Risk_Percent_;} //Com = i; // Comment ("tp_reward_percent "+tp_reward_percent+"/"+tp_percent+"/"+k+"/"+lastLotcore); return(lastLotcore); } */ enum modedatlenh { PRICE=1, POINT=2, PIP =3, DISTANCE = 4, }; //====================================================================== int vaoLenh( int typee,string symm, double lott, double pricee, double slllll,double tppppp,int Magicc, string comm,modedatlenh mode ) { int kq = 0; if(lott ==0){return(kq);} int normallotunit ; if(MarketInfo(symm, MODE_MINLOT)== 0.01){normallotunit = 2;} if(MarketInfo(symm, MODE_MINLOT)== 0.1){normallotunit = 1;} if(MarketInfo(symm, MODE_MINLOT)== 0.001){normallotunit = 3;} lott = NormalizeDouble(lott, normallotunit ); //--------------------------- double slprice, tpprice; color mau; if(typee== OP_BUY) { pricee = MarketInfo(symm,MODE_ASK); } if(typee== OP_SELL) { pricee = MarketInfo(symm,MODE_BID); } if(mode == DISTANCE) { if((typee+2)%2== OP_BUY) { if(slllll!=0)slprice = pricee - slllll; if(tppppp!=0)tpprice = pricee + tppppp; mau = clrBlue; } if((typee+2)%2== OP_SELL) { if(slllll!=0) slprice = pricee + slllll; if(tppppp!=0) tpprice = pricee - tppppp; mau = clrRed; } } if(mode == POINT) { if((typee+2)%2== OP_BUY) { if(slllll!=0)slprice = pricee - slllll*MarketInfo(symm ,MODE_POINT); if(tppppp!=0)tpprice = pricee + tppppp*MarketInfo(symm ,MODE_POINT); mau = clrBlue; } if((typee+2)%2== OP_SELL) { if(slllll!=0) slprice = pricee + slllll*MarketInfo(symm ,MODE_POINT); if(tppppp!=0) tpprice = pricee - tppppp*MarketInfo(symm ,MODE_POINT); mau = clrRed; } } if(mode == PIP) { if((typee+2)%2== OP_BUY) { if(slllll!=0)slprice = pricee - slllll*10*MarketInfo(symm ,MODE_POINT); if(tppppp!=0)tpprice = pricee + tppppp*10*MarketInfo(symm ,MODE_POINT); mau = clrBlue; } if((typee+2)%2== OP_SELL) { if(slllll!=0) slprice = pricee + slllll*10*MarketInfo(symm ,MODE_POINT); if(tppppp!=0) tpprice = pricee - tppppp*10*MarketInfo(symm ,MODE_POINT); mau = clrRed; } } if(mode==PRICE) { slprice = slllll;tpprice = tppppp; } if(typee %2 == OP_BUY) { tpprice = MathMax(tpprice,Bid + MarketInfo(Symbol(),MODE_STOPLEVEL)*Point); slprice = MathMin(slprice,Ask - MarketInfo(Symbol(),MODE_STOPLEVEL)*Point); } if(typee %2 == OP_SELL) { slprice = MathMax(slprice,Bid + MarketInfo(Symbol(),MODE_STOPLEVEL)*Point); tpprice = MathMin(tpprice,Ask - MarketInfo(Symbol(),MODE_STOPLEVEL)*Point); } pricee = NormalizeDouble(pricee,MarketInfo(symm , MODE_DIGITS)); slprice = NormalizeDouble(slprice,MarketInfo(symm , MODE_DIGITS)); tpprice = NormalizeDouble(tpprice,MarketInfo(symm , MODE_DIGITS)); //============= AccountFreeMarginCheck(Symbol(),typee%2,lott); if ( ((AccountStopoutMode() == 1) && (AccountFreeMarginCheck(Symbol(), typee%2, lott) > AccountStopoutLevel())) || ((AccountStopoutMode() == 0) && (((AccountFreeMarginCheck(Symbol(), typee%2, lott)/AccountEquity()) * 100) > AccountStopoutLevel())) ){ //-----gui lenh double thanhcong = OrderSend(symm,typee,lott,pricee,20,0,0,comm,Magicc,0,mau); if(thanhcong >0 ){kq = thanhcong;} } else {Alert("khong du freemargin de vao "+lott+" lot");return(kq);} // ----- CHINH SL TP bool sucess =false; int dem; if(thanhcong >0 && slprice !=0 && tpprice!=0 ) { while ( sucess == false && dem<20) { sucess = OrderModify(thanhcong,pricee,slprice,tpprice,0,clrNONE); dem++; Sleep(50); } int error = GetLastError(); if(error !=0 && error !=1){ Print(thanhcong +" bi loi modify: "+ error+"/price: "+pricee+"/sl: "+slprice+"/tp: "+tpprice);} } return (kq); } //==================================================================== void DONGLENH_ToanBo() { for (int i=OrdersTotal()-1; i >= 0; i--) { if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {continue;} if (OrderSymbol()!=Symbol()) {continue;} // if (OrderMagicNumber()!=Magic) {continue;} if (OrderType()<2) {DongLenh_Ticket(OrderTicket(), OrderLots());} else {int rec = OrderDelete(OrderTicket(),CLR_NONE);} } } void DONGLENH_theo_type(int op1,int op2) { for (int i=OrdersTotal()-1; i >= 0; i--) { if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {continue;} if (OrderSymbol()!=Symbol()) {continue;} //if (OrderMagicNumber()!=Magic) {continue;} if ((OrderType()==op1||OrderType()==op2)&& OrderType()<2) {DongLenh_Ticket(OrderTicket(), OrderLots());} if ((OrderType()==op1||OrderType()==op2)&& OrderType()>2) {int rec = OrderDelete(OrderTicket(),CLR_NONE);} } } void DONGLENH_motphan(int op1,double lot) { for (int i=OrdersTotal()-1; i >= 0; i--) { if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {continue;} if (OrderSymbol()!=Symbol()) {continue;} // if (OrderMagicNumber()!=Magic) {continue;} if (OrderType()==op1) {DongLenh_Ticket(OrderTicket(), lot);} //if ((OrderType()==op1||OrderType()==op2)&& OrderType()>2) {int rec = OrderDelete(OrderTicket(),CLR_NONE);} } } void DongLenh_Ticket(int ticket, double lott) { int Slippage = 3; if (!OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES)) {return;} if(lott==0){lott=OrderLots();} if(OrderType()>1){ OrderDelete(OrderTicket(),CLR_NONE);} if(OrderType()<=1) { double priice; RefreshRates(); if (MathMod(OrderType(),2) == 0) {priice = MarketInfo(OrderSymbol(),MODE_BID);} else {priice = MarketInfo(OrderSymbol(),MODE_ASK);} OrderClose(ticket, lott, priice, Slippage, Gold); // Sleep(100); } return; } //========= void ObjectLine(string ObjName, double Price, color Col) { ObjectDelete(ObjName); ObjectCreate(ObjName, OBJ_HLINE, 0, TimeCurrent(),Price); ObjectSetInteger(0,ObjName,OBJPROP_COLOR,Col); ObjectSetInteger(0,ObjName,OBJPROP_WIDTH,1); } int KtraLenh(int oop) { int count; for (int i=OrdersTotal()-1; i >= 0; i--) { if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {continue;} if (OrderSymbol()!=Symbol()) {continue;} // if (OrderMagicNumber()!=Magic) {continue;} if(OrderType()!= oop || oop==3) {continue;} count++; } return(count); } //========= //============================= double GET_LOT(string sym1,int iSL,double dRisk,double dBalance) { int iPipMult[]={1,10,1,10,1,10,100}; double dRiskCapital=dRisk/100*dBalance; double tickval = MarketInfo(sym1,MODE_TICKVALUE);if(tickval==0){tickval=1;} // Comment(iSL); double res = dRiskCapital/(iSL*tickval*iPipMult[Digits]); double gd_MinLot = MarketInfo(sym1, MODE_MINLOT); double gd_MaxLot = MarketInfo(sym1, MODE_MAXLOT); if (res < gd_MinLot) res = gd_MinLot; if (res > gd_MaxLot) res = gd_MaxLot; res = NormalizeDouble(res, 2); return(res); } //+------------------------------------------------------------------+ double get_acc_equity() { double eq; for (int i=OrdersTotal()-1; i >= 0; i--) { if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {continue;} if (OrderSymbol()!=Symbol()) {continue;} //if (OrderMagicNumber()!=Magic) {continue;} eq+= OrderProfit()+OrderSwap()+OrderCommission(); } eq+= AccountBalance(); return(eq); } //============================= double get_lot_from_money_point(string sym1,double iSL_poit,double riskpc,double dBalance) { double dRiskmoney = MathAbs(riskpc/100*dBalance); double tickval = MarketInfo(sym1,MODE_TICKVALUE); double ticksize = MarketInfo(sym1,MODE_TICKSIZE); // double comission = get_commision_point(sym); // double bidask =( MarketInfo(sym1,MODE_ASK)-MarketInfo(sym1,MODE_BID) ); double khoangcachbaseee =(iSL_poit); if(tickval!=0 && iSL_poit!=0&&ticksize!=0) { double res = dRiskmoney/(khoangcachbaseee*tickval/ticksize); } double lot_step =MarketInfo(sym1,MODE_LOTSTEP);//int ss = res/lot_step;res = ss*lot_step; int step;if(lot_step==1){step = 0;} if(lot_step==0.1){step = 1;} if(lot_step==0.01){step = 2;} if(lot_step==0.001){step = 3;} res = NormalizeDouble(res, step); if (res < MarketInfo(sym1, MODE_MINLOT)) res = MarketInfo(sym1, MODE_MINLOT); if (res > MarketInfo(sym1, MODE_MAXLOT)) res = MarketInfo(sym1, MODE_MAXLOT); return(res); } //+------------------------------------------------------------------+ //+==================================================================+ /* void get_news() {if(IsTesting()||IsOptimization()){return;} int a = +1 + 10*MathRand()/32768;Sleep(a*1000); iCustom(Symbol(),0,"ffcal_net_thinh_panel",high,medium,low,speak,minBeforeNews,0,0);//Sleep(1000); for ( int i=OrdersTotal()-1; i >= 0; i--) { if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { continue;} if(OrderMagicNumber()!= tinTuc){continue;} // Print((TimeCurrent()-OrderOpenTime() )/60); if((TimeCurrent()-OrderOpenTime() )/60>minAfterNews+minBeforeNews){DongLenh_Ticket(OrderTicket(),0);continue;} } for(int n = ObjectsTotal();n>=0;n--) { string name = ObjectName(n); if(StringFind(name,"#",0)>=0){ObjectDelete(name);} if(StringFind(name,"UpCommingNews",0)<0){continue;} string text= ObjectDescription(ObjectName(n)); if(StringFind(text,"[",0)<0){continue;} string countryNews="abc";countryNews = StringSubstr( text,1,3); int ngoactruoc=StringFind(text,"(",0)+1;//if(ngoactruoc<0){continue;} int ngoacsau =StringFind(text,")",0)-1; int mintilnews = StrToInteger(StringSubstr(text,ngoactruoc,ngoacsau ) ); if(mintilnews> minBeforeNews ){continue;} bool coroi = false; for ( i=OrdersTotal()-1; i >= 0; i--) { if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { continue;} if(OrderMagicNumber()!= tinTuc){continue;} // if((TimeCurrent()-OrderOpenTime() )/60>minAfterNews+minBeforeNews){DongLenh_Ticket(OrderTicket(),0);continue;} if((TimeCurrent()-OrderOpenTime() )/60>minBeforeNews && StringFind(OrderComment(),countryNews,0)>=0){DongLenh_Ticket(OrderTicket(),0);coroi = false;continue;} if(StringFind(OrderComment(),countryNews,0)>=0){coroi=true;} } if(mintilnews < minBeforeNews&&coroi == false&& countryNews !="abc") { OrderOpen_sl_tp_point(OP_BUYLIMIT,Symbol(),MarketInfo(Symbol(),MODE_MINLOT),0.6*MarketInfo(Symbol(),MODE_ASK),0,0,tinTuc,countryNews+"<<"+mintilnews+">>"); } } } */ //---------------- //+------------------------------------------------------------------+*/ //================ double get_point_from_money_lot(string sym1,double phantramrisk,double dBalance,double lots) { double dRiskCapital=phantramrisk/100*dBalance; double tickval = MarketInfo(sym1,MODE_TICKVALUE); double iSL; if(lots !=0){ iSL = (dRiskCapital/(lots*tickval))* MarketInfo(sym1,MODE_POINT);} iSL = NormalizeDouble(iSL,MarketInfo(sym1,MODE_DIGITS)); return(iSL); } //================ /* double total; //----------------------------- void getlenhcu() {datetime thoigiancore,thoigiancover; lastProfitcore =0;lastsonocover=0;lastProfitcover=0; total =0; for (int i=OrdersHistoryTotal()-1; i >= 0; i--) { if (!OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) {continue;} if (OrderSymbol()!=Symbol()) {continue;} // if (OrderMagicNumber()!=Magic) {continue;} // if(StringFind(OrderComment(),thisrecname)<0){continue;} // Print(OrderTicket()+"/"+OrderComment()); total+= OrderProfit()+OrderSwap()+OrderCommission(); if(OrderCloseTime()>thoigiancore &&StringFind(OrderComment(),"core",0)>=0) {thoigiancore = OrderCloseTime(); lastProfitcore= OrderProfit()+OrderSwap()+OrderCommission();lastLotcore = OrderLots(); lasttypecore = OrderType(); lastmagiccore = OrderMagicNumber(); } if(OrderCloseTime()>thoigiancover &&( StringFind(OrderComment(),"cover",0)>=0||StringFind(OrderComment(),"fist",0)>=0)) {thoigiancover = OrderCloseTime(); lastProfitcover= OrderProfit()+OrderSwap()+OrderCommission(); lastLotcover = OrderLots(); lasttypecover= OrderType(); lastmagiccover = OrderMagicNumber(); int ngoactruoc=StringFind(OrderComment(),"[",0)+1;//if(ngoactruoc<0){continue;} int ngoacsau =StringFind(OrderComment(),"]",0)-1; lastsonocover = StrToDouble(StringSubstr(OrderComment(),ngoactruoc,ngoacsau ) ); } } }*/ //+------------------------------------------------------------------+ double get_commision_point(string symmm) { double ress; for(int i = OrdersHistoryTotal()-1; i >=0 ; i--) //OrdersHistoryTotal() { if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) {continue;} //Tuong duong if (OrderSymbol() != symmm) {continue;} if (OrderType() >1) {continue;} if(OrderCommission()!=0) { ress = OrderCommission()/OrderLots()*MarketInfo(symmm,MODE_POINT); break; } } // Print("commisson = "+ress); return(MathAbs(ress)); } //================ double get_bidask(string syml) { double coom=get_commision_point(syml); double sp =coom+MathAbs(MarketInfo(syml,MODE_BID)-MarketInfo(syml,MODE_ASK) ); if(sp==0){Alert("Please Show this Symbol : "+syml);} // double kkq = MathMax(coom+sp,10*MarketInfo(syml,MODE_POINT)); return(sp); } //------------ham nhac nho //------------------