Advertisement
shjavohir

JDS BB 1

Sep 25th, 2022
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.22 KB | None | 0 0
  1. //+------------------------------------------------------------------+
  2. //| JDS_BB.mq4 |
  3. //| Copyright 2022.04, Developer @ShJavohir |
  4. //| https://www.t.me/shjavohir |
  5. //+------------------------------------------------------------------+
  6. #property copyright "Copyright 2022.04, Developer @ShJavohir"
  7. #property link "https://www.t.me/shjavohir"
  8. #property version "1.10"
  9. #property strict
  10.  
  11. enum BBProcess {
  12. CROSS_BB_UP,
  13. CROSS_CENTER,
  14. CROSS_BB_DOWN
  15. };
  16.  
  17.  
  18. extern const string DIVIDER_1 = ""; //<===== Asosiy sozlamalar =====>
  19. extern double LOT = 0.01; //Lot
  20. extern double MARTINGALE_FACTOR = 2; //Martingale factor
  21. extern double TARGET_PROFIT = 1; //Kerakli foyda ($)
  22. extern int MAGIC_NUMBER = 1202; //Magic number
  23. extern int SLIPPAGE = 15; //Slippage
  24.  
  25. extern const string DIVIDER_2 = ""; //<===== BB sozlamalari =====>
  26. extern int BB_PERIOD = 8; //BB period
  27. extern double BB_DEVIATION = 2; //BB deviation
  28. extern int BB_SHIFT = 0; //BB shift
  29. extern ENUM_APPLIED_PRICE BB_APPLIED_PRICE = PRICE_CLOSE; //BB ishlatadigan narx
  30.  
  31. const string EA_NAME = "JDS_BB by JDSystems";
  32.  
  33. bool isOrderOpenedBuy = false;
  34. double currentLotBuy;
  35. double lastOrderOpenPriceBuy = 0;
  36. ENUM_ORDER_TYPE processOrderTypeBuy;
  37. ENUM_ORDER_TYPE lookingSignalBuy = OP_BUYLIMIT; //Means no looking signal
  38. static datetime lastCandleTimeBuy;
  39. bool isBBpassedBuy = false;
  40.  
  41. bool isOrderOpenedSell = false;
  42. double currentLotSell;
  43. double lastOrderOpenPriceSell = 0;
  44. ENUM_ORDER_TYPE processOrderTypeSell;
  45. ENUM_ORDER_TYPE lookingSignalSell = OP_BUYLIMIT; //Means no looking signal
  46. static datetime lastCandleTimeSell;
  47. bool isBBpassedSell = false;
  48. BBProcess nextProcess;
  49. bool haveReverseCandle = false;
  50.  
  51. bool allowBuy = true;
  52. bool allowSell = true;
  53.  
  54. //+------------------------------------------------------------------+
  55. //| Expert initialization function |
  56. //+------------------------------------------------------------------+
  57. int OnInit() {
  58. //---
  59. currentLotBuy = LOT;
  60.  
  61. currentLotSell = LOT;
  62.  
  63. //---
  64. return(INIT_SUCCEEDED);
  65. }
  66. //+------------------------------------------------------------------+
  67. //| Expert deinitialization function |
  68. //+------------------------------------------------------------------+
  69. void OnDeinit(const int reason) {
  70. //---
  71.  
  72. }
  73.  
  74. //+------------------------------------------------------------------+
  75. //| |
  76. //+------------------------------------------------------------------+
  77. string getOrderTypeString(ENUM_ORDER_TYPE orderType) {
  78. if(orderType == 0) return "BUY";
  79. if(orderType == 1) return "SELL";
  80. return "NO";
  81. }
  82.  
  83. //+------------------------------------------------------------------+
  84. //| |
  85. //+------------------------------------------------------------------+
  86. string getNextProcess() {
  87. if(nextProcess == CROSS_BB_UP) return "BB UP LINE";
  88. if(nextProcess == CROSS_CENTER) return "BB CENTER LINE";
  89. if(nextProcess == CROSS_BB_DOWN) return "BB DOWN LINE";
  90.  
  91. return "NO";
  92. }
  93. //+------------------------------------------------------------------+
  94. //| Expert tick function |
  95. //+------------------------------------------------------------------+
  96. void OnTick() {
  97. Comment(
  98. "IsOrderOpened: ", isOrderOpenedBuy,
  99. "\nLookingSignal (BUY): ", getOrderTypeString(lookingSignalBuy),
  100.  
  101. "\nLookingSignal (SELL): ", getOrderTypeString(lookingSignalSell),
  102.  
  103. "\nProcessOrderType (BUY): ", getOrderTypeString(processOrderTypeBuy),
  104.  
  105. "\nProcessOrderType (SELL): ", getOrderTypeString(processOrderTypeSell),
  106. "\nCurrent lot: ", currentLotBuy,
  107. "\nLast order open price: ", lastOrderOpenPriceBuy,
  108. "\nIsBBCrossed: ", isBBpassedBuy,
  109. "\nAllow buy: ", allowBuy,
  110. "\nAllow sell; ", allowSell
  111. );
  112. //////////////////////////////
  113. //Comment(high, " ",low, " ",open, " ",close, open>close?"Bearish":"Bullish");
  114. if(isOrderOpenedBuy) {
  115. if(getProfit(OP_BUY) >= TARGET_PROFIT ) { // * ( getLotBuy() / (LOT * MARTINGALE_FACTOR))
  116. if(closeAllOrders(OP_BUY)) {
  117.  
  118. resetLotBuy();
  119. isOrderOpenedBuy = false;
  120. lastOrderOpenPriceBuy = Ask;
  121. processOrderTypeBuy = OP_BUYLIMIT;;
  122. lookingSignalBuy = OP_BUYLIMIT;// no signal;
  123. if(!isOrderOpenedSell) allowBuy = true;
  124.  
  125.  
  126. resetLotSell();
  127. isOrderOpenedSell = false;
  128. lastOrderOpenPriceSell = Bid;
  129. processOrderTypeSell = OP_SELLLIMIT;;
  130. lookingSignalSell = OP_SELLLIMIT;// no signal;
  131. if(!isOrderOpenedBuy) allowSell = true;
  132.  
  133.  
  134. }
  135.  
  136. }
  137.  
  138. if(true) { //lastCandleTimeBuy != Time[0]
  139. double open = Open[1]; //iCustom(Symbol(), 0, IndicatorName, 10, true, 2, 1);
  140. double close = Close[1]; // iCustom(Symbol(), 0, IndicatorName, 10, true, 3, 1);
  141.  
  142.  
  143.  
  144.  
  145. if(processOrderTypeBuy == OP_BUY && Ask < lastOrderOpenPriceBuy) {
  146. if(Close[0] > getBBHigher()) {
  147. isBBpassedBuy = true;
  148. }
  149.  
  150. if(true && Close[0] < getBBLower() && allowBuy) //
  151. if(buy()) {
  152. isBBpassedBuy = false;
  153. lastOrderOpenPriceBuy = Ask;
  154. allowBuy = false;
  155. allowSell = true;
  156. }
  157.  
  158.  
  159. }
  160. lastCandleTimeBuy = Time[0];
  161. }
  162.  
  163. } else if(allowBuy) {
  164. double high = High[0];//iCustom(Symbol(), 0, IndicatorName, 10, true, 0, 1);
  165. double low = Low[0]; //iCustom(Symbol(), 0, IndicatorName, 10, true, 1, 1);
  166. double open = Open[0];//iCustom(Symbol(), 0, IndicatorName, 10, true, 2, 1);
  167. double close = Close[0];//iCustom(Symbol(), 0, IndicatorName, 10, true, 3, 1);
  168.  
  169. if(lookingSignalBuy != OP_BUY && lookingSignalBuy != OP_SELL) {
  170. //look for initial signal
  171. if(close < getBBLower()) {
  172. //if(getMaSignal() == OP_BUY)
  173. lookingSignalBuy = OP_BUY;
  174. }
  175. } else if(lookingSignalBuy == OP_BUY) {
  176. if(lookingSignalBuy == OP_BUY) {
  177. if(close < getBBLower()) {
  178. //open order
  179. int ticket = openBuyOrder();
  180. if(ticket > 0) {
  181.  
  182. isOrderOpenedBuy = true;
  183. currentLotBuy = LOT;
  184. multiplyLotBuy(MARTINGALE_FACTOR);
  185. lastOrderOpenPriceBuy = Ask;
  186. processOrderTypeBuy = OP_BUY;
  187. lookingSignalBuy = OP_BUYLIMIT;// no signal;
  188. isBBpassedBuy = false;
  189. allowBuy = false;
  190. allowSell = true;
  191. } else {
  192. logError(__FUNCTION__, "Could not open order", GetLastError());
  193. }
  194. }
  195. }
  196. }
  197.  
  198. }
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217. if(isOrderOpenedSell) {
  218. if(getProfit(OP_SELL) >= TARGET_PROFIT) { // * (getLotSell() / (LOT * MARTINGALE_FACTOR))
  219. if(closeAllOrders(OP_SELL)) {
  220. resetLotSell();
  221. isOrderOpenedSell = false;
  222. lastOrderOpenPriceSell = Bid;
  223. processOrderTypeSell = OP_SELLLIMIT;;
  224. lookingSignalSell = OP_SELLLIMIT;// no signal;
  225. if(!isOrderOpenedBuy) allowSell = true;
  226.  
  227. resetLotBuy();
  228. isOrderOpenedBuy = false;
  229. lastOrderOpenPriceBuy = Ask;
  230. processOrderTypeBuy = OP_BUYLIMIT;;
  231. lookingSignalBuy = OP_BUYLIMIT;// no signal;
  232. if(!isOrderOpenedSell) allowBuy = true;
  233.  
  234. }
  235.  
  236. }
  237.  
  238. if(true) { //lastCandleTimeSell != Time[0]
  239. double open = Open[1];//iCustom(Symbol(), 0, IndicatorName, 10, true, 2, 1);
  240. double close = Close[1];//iCustom(Symbol(), 0, IndicatorName, 10, true, 3, 1);
  241.  
  242. if(processOrderTypeSell == OP_SELL && Bid > lastOrderOpenPriceSell) {
  243. if(Close[0] < getBBLower()) {
  244. isBBpassedSell = true;
  245. }
  246.  
  247. if(true && Close[0] > getBBHigher() && allowSell) //
  248. if(sell()) {
  249. isBBpassedSell = false;
  250. lastOrderOpenPriceSell = Bid;
  251. allowSell = false;
  252. allowBuy = true;
  253. }
  254.  
  255.  
  256. }
  257. lastCandleTimeSell = Time[0];
  258. }
  259.  
  260. } else if(allowSell) {
  261.  
  262. double high = High[0];//iCustom(Symbol(), 0, IndicatorName, 10, true, 0, 1);
  263. double low = Low[0]; //iCustom(Symbol(), 0, IndicatorName, 10, true, 1, 1);
  264. double open = Open[0];//iCustom(Symbol(), 0, IndicatorName, 10, true, 2, 1);
  265. double close = Close[0];//iCustom(Symbol(), 0, IndicatorName, 10, true, 3, 1);
  266.  
  267.  
  268. if(lookingSignalSell != OP_BUY && lookingSignalSell != OP_SELL) {
  269. //look for initial signal
  270. if(close > getBBHigher()) {
  271. //if(getMaSignal() == OP_BUY)
  272. lookingSignalSell = OP_SELL;
  273. }
  274. } else if(lookingSignalSell == OP_SELL) {
  275. if(lookingSignalSell == OP_SELL) {
  276. if(close > getBBHigher()) {
  277. //open order
  278. int ticket = openSellOrder();
  279. if(ticket > 0) {
  280. isOrderOpenedSell = true;
  281. currentLotSell = LOT;
  282. multiplyLotSell(MARTINGALE_FACTOR);
  283. lastOrderOpenPriceSell = Bid;
  284. processOrderTypeSell = OP_SELL;
  285. lookingSignalSell = OP_SELLLIMIT;// no signal;
  286. isBBpassedSell = false;
  287. allowSell = false;
  288. allowBuy = true;
  289. } else {
  290. logError(__FUNCTION__, "Could not open order", GetLastError());
  291. }
  292. }
  293. }
  294. }
  295.  
  296. }
  297.  
  298. //+------------------------------------------------------------------+
  299. }
  300.  
  301. /*
  302. if(lookingSignal == OP_SELL) {
  303. if(close < open && close < getBBHigher()) {
  304. //open order
  305. int ticket = openSellOrder();
  306. if(ticket > 0) {
  307. isOrderOpened = true;
  308. currentLot = LOT;
  309. multiplyLot(MARTINGALE_FACTOR);
  310. lastOrderOpenPrice = Bid;
  311. processOrderType = OP_SELL;
  312. lookingSignal = OP_BUYLIMIT;// no signal;
  313. nextProcess = CROSS_CENTER;
  314. isBBpassed = false;
  315. haveReverseCandle = false;
  316. } else {
  317. logError(__FUNCTION__, "Could not open order", GetLastError());
  318. }
  319. }
  320. }
  321. */
  322.  
  323. //+------------------------------------------------------------------+
  324. //| |
  325. //+------------------------------------------------------------------+
  326. double getBBLower() {
  327. return iBands(Symbol(), 0, BB_PERIOD, BB_DEVIATION, BB_SHIFT, PRICE_CLOSE, MODE_LOWER, 1);
  328. }
  329.  
  330. //+------------------------------------------------------------------+
  331. //| |
  332. //+------------------------------------------------------------------+
  333. double getBBHigher() {
  334. return iBands(Symbol(), 0, BB_PERIOD, BB_DEVIATION, BB_SHIFT, PRICE_CLOSE, MODE_UPPER, 1);
  335. }
  336.  
  337. //+------------------------------------------------------------------+
  338. //| |
  339. //+------------------------------------------------------------------+
  340. double getBBMid() {
  341. return iBands(Symbol(), 0, BB_PERIOD, BB_DEVIATION, BB_SHIFT, PRICE_CLOSE, MODE_MAIN, 1);
  342. }
  343. //+------------------------------------------------------------------+
  344.  
  345. //+------------------------------------------------------------------+
  346. //| |
  347. //+------------------------------------------------------------------+
  348. int openBuyOrder(bool isTpZero = false) {
  349. RefreshRates();
  350. return (OrderSend(Symbol(), OP_BUY, getLotBuy(), Ask, SLIPPAGE, 0, 0, EA_NAME, MAGIC_NUMBER));
  351. }
  352.  
  353. //+------------------------------------------------------------------+
  354. //| |
  355. //+------------------------------------------------------------------+
  356. int openSellOrder(bool isTpZero = false) {
  357. RefreshRates();
  358. return (OrderSend(Symbol(), OP_SELL, getLotSell(), Bid, SLIPPAGE, 0, 0, EA_NAME, MAGIC_NUMBER));
  359. }
  360.  
  361. //+------------------------------------------------------------------+
  362. //| |
  363. //+------------------------------------------------------------------+
  364. double getLotBuy() {
  365. return currentLotBuy;
  366. }
  367.  
  368. //+------------------------------------------------------------------+
  369. //| |
  370. //+------------------------------------------------------------------+
  371. double getLotSell() {
  372. return currentLotSell;
  373. }
  374.  
  375. //+------------------------------------------------------------------+
  376. //| |
  377. //+------------------------------------------------------------------+
  378.  
  379. //+------------------------------------------------------------------+
  380. //| |
  381. //+------------------------------------------------------------------+
  382. void multiplyLotBuy(double factor) {
  383. currentLotBuy = currentLotBuy * factor;
  384. Print("Lot multiplied BUY: ", currentLotBuy);
  385. }
  386.  
  387. //+------------------------------------------------------------------+
  388. //| |
  389. //+------------------------------------------------------------------+
  390. void multiplyLotSell(double factor) {
  391. currentLotSell = currentLotSell * factor;
  392. Print("Lot multiplied SELL: ", currentLotSell);
  393. }
  394.  
  395. //+------------------------------------------------------------------+
  396. //| |
  397. //+------------------------------------------------------------------+
  398. void resetLotBuy() {
  399. currentLotBuy = LOT;
  400. }
  401.  
  402. //+------------------------------------------------------------------+
  403. //| |
  404. //+------------------------------------------------------------------+
  405. void resetLotSell() {
  406. currentLotSell = LOT;
  407. }
  408. //+------------------------------------------------------------------+
  409.  
  410. //+------------------------------------------------------------------+
  411. //| |
  412. //+------------------------------------------------------------------+
  413. void logError(string functionName, string msg, int errCode, bool isNoisy = false) {
  414. if(isNoisy) {
  415.  
  416. Alert("Error in function ", functionName, ". Message: ", msg, " Error code: ", errCode);
  417.  
  418. } else {
  419.  
  420. Print("Error in function ", functionName, ". Message: ", msg, " Error code: ", errCode);
  421.  
  422. }
  423. }
  424. //+------------------------------------------------------------------+
  425.  
  426. //+------------------------------------------------------------------+
  427. //| |
  428. //+------------------------------------------------------------------+
  429.  
  430. //+------------------------------------------------------------------+
  431.  
  432. //+------------------------------------------------------------------+
  433.  
  434. //+------------------------------------------------------------------+
  435. //| |
  436. //+------------------------------------------------------------------+
  437. bool closeAllOrders(ENUM_ORDER_TYPE orderType) {
  438. bool didAllClosed = true;
  439. for (int i = (OrdersTotal() - 1); i >= 0; i--) {
  440. //If the order cannot be selected throw and log an error
  441. if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false) {
  442. didAllClosed = false;
  443. logError(__FUNCTION__, "Could not select order", GetLastError());
  444. break;
  445. } else {
  446. //OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), SLIPPAGE);
  447. if(OrderMagicNumber() == MAGIC_NUMBER && OrderSymbol() == Symbol()) {
  448. if (OrderType() == OP_BUY && orderType == OP_BUY) {
  449. if(!OrderClose(OrderTicket(), OrderLots(), Bid, SLIPPAGE)) {
  450. logError(__FUNCTION__, "Could not close order", GetLastError());
  451. didAllClosed = false;
  452. }
  453. } else if(OrderType() == OP_SELL && orderType == OP_SELL) {
  454. if(!OrderClose(OrderTicket(), OrderLots(), Ask, SLIPPAGE)) {
  455. logError(__FUNCTION__, "Could not close order", GetLastError());
  456. didAllClosed = false;
  457. }
  458. } else {
  459. if(!OrderDelete(OrderTicket())) logError(__FUNCTION__, "Could not delete order", GetLastError());
  460.  
  461. }
  462. }
  463. }
  464. }
  465. return didAllClosed;
  466. }
  467.  
  468. //+------------------------------------------------------------------+
  469. //| |
  470. //+------------------------------------------------------------------+
  471. double getProfit(ENUM_ORDER_TYPE orderType) {
  472. double profit = 0;
  473. for (int i = (OrdersTotal() - 1); i >= 0; i--) {
  474. //If the order cannot be selected throw and log an error
  475. if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false) {
  476. logError(__FUNCTION__, "Could not select order", GetLastError());
  477. } else {
  478. //OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), SLIPPAGE);
  479. if(OrderMagicNumber() == MAGIC_NUMBER && OrderSymbol() == _Symbol) {
  480. if (OrderType() == orderType) {
  481. profit += OrderProfit();
  482. }
  483. }
  484. }
  485. }
  486. return profit;
  487. }
  488. //+------------------------------------------------------------------+
  489. bool buy() {
  490. int ticket = openBuyOrder();
  491. if(ticket > 0) {
  492. multiplyLotBuy(MARTINGALE_FACTOR);
  493. lastOrderOpenPriceBuy = Ask;
  494. processOrderTypeBuy = OP_BUY;
  495. lookingSignalBuy = OP_BUYLIMIT;// no signal;
  496. return true;
  497. } else {
  498. logError(__FUNCTION__, "Could not open order in process", GetLastError());
  499. return false;
  500. }
  501. }
  502. //+------------------------------------------------------------------+
  503.  
  504. //+------------------------------------------------------------------+
  505. //| |
  506. //+------------------------------------------------------------------+
  507. bool sell() {
  508. int ticket = openSellOrder();
  509. if(ticket > 0) {
  510. multiplyLotSell(MARTINGALE_FACTOR);
  511. lastOrderOpenPriceSell = Bid;
  512. processOrderTypeSell = OP_SELL;
  513. lookingSignalSell = OP_BUYLIMIT;// no signal
  514. return true;
  515. } else {
  516. logError(__FUNCTION__, "Could not open order in process", GetLastError());
  517. return false;
  518. }
  519. }
  520. //+------------------------------------------------------------------+
  521.  
  522. //+------------------------------------------------------------------+
  523.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement