//+------------------------------------------------------------------+ //| baiso1.mq4 | //| Copyright 2016, MetaQuotes Software Corp. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2016, MetaQuotes Software Corp." #property link "https://www.mql5.com" #property version "1.00" #property strict //+------------------------------------------------------------------+ //| Expert initialization function | // NOI KHAI BAO BIEN double khoiLuong= 0.01;// 0 string Com ; color maucualenh = clrRed; int loaiLenh = OP_BUY; double giavaolenh=0; datetime thoigiangiaodich; bool chophepGD = true; int Magic =999; double lastLotsize ; int lasttype; double balance;// acc extern double risk = 5; extern double stoploss = 50; extern double takeprofit = 100; extern double Trailling_pip = 10; string sym; bool active = false; //+------------------------------------------------------------------+ int OnInit() { //--- //double number = 1; ////int nguyen =MathFloor(number); //Comment(nguyen); sym=Symbol(); Com = "#"+"1"+ "("+"1.1050" + "*"+"1.0245"+")"; // "#1(1.1050*1.0245)" string ketqua = catchuoi(Com,"(","*"); double giaslcualenh = StrToDouble(ketqua); Comment(ketqua); //checkLisicen(); // SendNotification("hello thinh"); //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { // cho chạy code ben dưới dòng này khi qua nến mới if(thoigiangiaodich == iTime(Symbol(),0,0) ){return;}thoigiangiaodich =iTime(Symbol(),0,0) ; // if(chophepGD == false){return;} //if(OrdersTotal()>0){return;} // chi cho vao 1 lenh // if(active ==false){return;} //Trailling(Trailling_pip) ; //if(demsolenh( sym ) >0 ){return;} //===================================================================== checkonline(); double tenkan1 = iIchimoku(Symbol(),0,9,26,52,MODE_TENKANSEN,1); double tenkan2 = iIchimoku(Symbol(),0,9,26,52,MODE_TENKANSEN,2); double kijun1 = iIchimoku(Symbol(),0,9,26,52,MODE_KIJUNSEN,1); double kijun2 = iIchimoku(Symbol(),0,9,26,52,MODE_KIJUNSEN,2); // duong do cat xanh do tu duoi len ==> buy if(tenkan2 < kijun2 && tenkan1 > kijun1 ) { loaiLenh == OP_BUY; } if(tenkan2 > kijun2 && tenkan1 < kijun1 ) { loaiLenh == OP_SELL; } //=========tinh khoi luong =========== balance = AccountEquity(); double slPoint= stoploss*10* MarketInfo(sym,MODE_POINT); khoiLuong = get_lot_from_money_point(sym,balance,risk,slPoint); // dieu kien vao lenh-------------- loaiLenh = MathRand()%2;// loai lenh ngau nhien //iWPR // TH1 --> chua co lenh nao het if(demlenh[OP_BUY] ==0 ) {//vao lenh dau tien // Com = index + sl + tp ; Com = "#"+"1"+ "("+"1.1050" + "*"+"1.0245"+")"; // "#1(1.1050*1.0245)" // vaoLenh(........ , Com); } //TH2 -- > da co lenh if(demlenh[OP_BUY]>0 ) { Com = lastdex[OP_BUY]+1;// 6 // 5 } // sell //--------------------------------VAO LENH------------------ // vaoLenh(sym,loaiLenh,khoiLuong,0,stoploss,takeprofit,Magic,Com); // SendNotification("co lenh moi ne "+ sym+ " "+ khoiLuong ); }// end ontick //===================================== ////////////////// string catchuoi(string text,string sig1,string sig2) { int vitri1 = StringFind(text,sig1,0); int vitri2 = StringFind(text,sig2,0); string res = StringSubstr(text,vitri1+1,vitri2-vitri1-1); return(res); } //=========== double get_lot_from_money_point(string symm,double balancee,double risk,double slPoint) { double lotsize ; double sotien = MathAbs(risk/100*balancee); // 10000 * 5 /100 double tickval = MarketInfo(symm , MODE_TICKVALUE); double ticksize = MarketInfo(symm , MODE_TICKSIZE); //========cong thuc chinh if(slPoint!=0 && ticksize !=0 && tickval !=0 ) { lotsize = sotien /(slPoint*tickval/ticksize); } // =========== 0.2568746655 double lotstep = MarketInfo(symm,MODE_LOTSTEP);//Comment(lotstep); int step; if(lotstep==1){step = 0;} if(lotstep == 0.01){step =2;} if(lotstep == 0.001){step =3;} if(lotstep == 0.1 ){step =1;} lotsize = NormalizeDouble(lotsize,step); if (lotsize < MarketInfo(symm, MODE_MINLOT)) lotsize = MarketInfo(symm, MODE_MINLOT); if (lotsize > MarketInfo(symm, MODE_MAXLOT)) lotsize = MarketInfo(symm, MODE_MAXLOT); return(lotsize); } //+------------------------------------------------------------------+ double lenhcuoicung(string symm) { double lastprofit; datetime lasttime ; for(int i =0; i <= OrdersHistoryTotal()-1; i++) { if(OrderSelect(i, SELECT_BY_POS,MODE_HISTORY)==False) {continue;} if(OrderSymbol() != symm){continue;} if(OrderMagicNumber()!= Magic){continue;} if(OrderType()>1 ){continue;} if(OrderCloseTime()> lasttime ) { lasttime = OrderCloseTime(); // 6h lastprofit = OrderProfit(); lastLotsize = OrderLots(); lasttype = OrderType(); } } return(lastprofit); } //====================================================================== void vaoLenh(string symm, int typee, double lott, double pricee, double slpip,double tppip,int Magicc, string comm ) { if(lott ==0){return;} 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); slprice = pricee - slpip*10*MarketInfo(symm ,MODE_POINT); tpprice = pricee + tppip*10*MarketInfo(symm ,MODE_POINT); mau = clrBlue; } if(typee== OP_SELL) { pricee = MarketInfo(symm,MODE_BID); slprice = pricee + slpip*10*MarketInfo(symm ,MODE_POINT); tpprice = pricee - tppip*10*MarketInfo(symm ,MODE_POINT); mau = clrRed; } pricee = NormalizeDouble(pricee,MarketInfo(symm , MODE_DIGITS)); slprice = NormalizeDouble(slprice,MarketInfo(symm , MODE_DIGITS)); tpprice = NormalizeDouble(tpprice,MarketInfo(symm , MODE_DIGITS)); //-----gui lenh double thanhcong = OrderSend(symm,typee,lott,pricee,20,0,0,comm,Magicc,0,mau); // ----- 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("bi loi: "+ error);} } } //==================================================================== /* void laygiatrinen() { // tim mo hinh enguffing for(int n =500 ; n >=0; n--) { double highttruoc, lowtruoc , hightsau, lowsau ; highttruoc = iHigh(Symbol(),0,n+1); lowtruoc = iLow(Symbol(),0,n+1); hightsau = iHigh(Symbol(),0,n); lowsau = iLow(Symbol(),0,n); if(highttruoc lowsau) { Comment(n);// gan nhat cay nen so 0 //break; } } */ /*int caynencaonhat,caynenthapnhat; caynencaonhat= iHighest(Symbol(),0,MODE_HIGH,100,0);// tra ve cay nen cao nhat caynenthapnhat = iLowest(Symbol(),0,MODE_LOW,100,0);// tra ve cay nen thap nhat double giacaonhat = iHigh(Symbol(),0,caynencaonhat);// lay gias hight cua .... double giathapnhat = iLow(Symbol(),0,caynenthapnhat);// lay low .... // Comment(caynencaonhat +" / "+ caynenthapnhat); Comment(giacaonhat +" / "+ giathapnhat);*/ //+==================================================================+ int Order_Open(int ordType,string sym_,double lots,double price,double sl,double tp,int mag,string com,double bidask) {// int ticket; if(lots==0){return(0);} color col; double Stoploss,TakeProfit; double unit=1; if(bidask!=0){unit= bidask;} if(ordType==OP_BUY) {price=MarketInfo(sym_,MODE_ASK);Stoploss=price-sl*unit; TakeProfit=price+tp*unit; col=Blue;} if(ordType==OP_SELL) {price=MarketInfo(sym_,MODE_BID);Stoploss=price+sl*unit; TakeProfit=price-tp*unit; col=Red;} price=NormalizeDouble(price,MarketInfo(sym_,MODE_DIGITS)); int NormalizeLot; if(MarketInfo(sym_,MODE_MINLOT)==0.1) {NormalizeLot=1;} else {NormalizeLot=2;} lots=NormalizeDouble(lots,NormalizeLot); int sucess=-1; int ross=0;int demm; sucess=OrderSend(sym_,ordType,lots,price,3,0,0,com,mag,0,col);Sleep(100); if(sucess>0 && (sl!=0 || tp!=0)) { while(ross<=0 && demm<20){ ross=OrderModify(sucess,price,Stoploss,TakeProfit,0,clrNONE); demm++;Sleep(100); } } int loi = GetLastError(); if(loi!=0 && loi !=1 ){ Print("eror"+loi); Print(sym_+ "/price "+ price+ " /op "+ordType+"/lot "+lots); } return(sucess); } //+------------------------------------------ //====================================================== void checkLisicen() { /*datetime x = iTime(Symbol(),PERIOD_D1,0);// thoi gian dau ngay // x= TimeCurrent();// thoi gian hien tai //TimeHour(x); // TimeMinute() // dau ngay + 3 hour()? x= x +3*60*60; Comment(x); */ if( TimeYear( TimeCurrent())<=2020 && TimeMonth( TimeCurrent())<=4 ){active= true;} // --> file ex4 */ //ham chuyen doi /*datetime x = StrToTime(giobatdau); string a = "06"; string b = "30"; string c= a+ ":"+b; datetime d = StrToTime(c)+60; Comment(d); StrToInteger*/ /* string commentt = "| 5 (6)lenhdautien"; int vitri= StringFind(commentt,"(",0); string cat = StringSubstr(commentt,vitri+1,1); Comment(cat);*/ //Str } //========================================== int demsolenh(string captiencandem) { int dem; for(int i = OrdersTotal()-1 ; i>=0; i--) { if(OrderSelect(i, SELECT_BY_POS,MODE_TRADES)==False) {continue;} if(OrderSymbol() != captiencandem){continue;} if(OrderMagicNumber()!= Magic){continue;} dem ++;// dem dc 1 lenh } return(dem); } //==================================== //==================================== double dinhdangLot(double khoiLuong) { if(khoiLuong==0){ khoiLuong = MarketInfo(Symbol(),MODE_MINLOT) ; } if(khoiLuong> MarketInfo(Symbol(),MODE_MAXLOT)){ khoiLuong = MarketInfo(Symbol(),MODE_MAXLOT) ; } khoiLuong = NormalizeDouble(khoiLuong,2); return(khoiLuong);// tra ve khoi da duoc dinh dang } //+==================================================================+ void Trailling(double trail) { double NewSL; trail*= 10*Point; // trail = trail * 10*10*Point 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 - trail;} else if (OrderType() == OP_SELL) {NewSL = Bid + trail;} if (OrderType() == OP_BUY && OrderStopLoss()>=NewSL ) {continue;} if (OrderType() == OP_SELL && OrderStopLoss()<=NewSL) {continue;} Comment(NewSL); OrderModify(OrderTicket(),OrderOpenPrice(),NewSL,OrderTakeProfit(),0,CLR_NONE); if(GetLastError()!=0 && GetLastError()!=1) {Print("Loi Trailling: " + GetLastError());} } } ///////////////////////// void ontick() { checkonline(); } // khai bao bien chung double giabuycaonhat,giabuythapnhat,giasellcaonhat,giasellthapnhat; int demlenh[6];datetime thoigiancuoicung[2]; int lastdex[2]; void checkonline() { giabuythapnhat = 1000000;giasellthapnhat = 1000000; ArrayInitialize(lastdex,0);// reset het ve 0 ArrayInitialize(thoigiancuoicung,0);// reset het ve 0 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;} demlenh[OrderType()] ++; int dex = StrToInteger(OrderComment()); // "0" // StrToDouble("0.01"); // demlenh[0]++; /// demlenh[1]++; ///////////buy if(OrderType() == OP_BUY ) { //////////////coment if(OrderOpenTime()> thoigiancuoicung[OP_BUY]) { thoigiancuoicung[OP_BUY]= OrderOpenTime(); lastdex[OP_BUY] = dex; } ////////////////////// //----------------------------------- if(OrderOpenPrice() > giabuycaonhat) { giabuycaonhat = OrderOpenPrice(); } if(OrderOpenPrice() < giabuythapnhat) { giabuythapnhat = OrderOpenPrice(); } //-------------------------------------- } //////////// sell if(OrderType() == OP_SELL ) { if(OrderOpenTime()> thoigiancuoicung[OP_SELL]) { thoigiancuoicung[OP_SELL]= OrderOpenTime(); lastdex[OP_SELL] = dex; } ////////////////////////////////// if(OrderOpenPrice() > giasellcaonhat) { giasellcaonhat = OrderOpenPrice(); } if(OrderOpenPrice() < giasellthapnhat) { giasellthapnhat = OrderOpenPrice(); } } }// end for Comment(giabuycaonhat + " "+ giabuythapnhat ); // Comment(giabuycaonhat + giabuythapnhat ); sai }// end void