Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //+------------------------------------------------------------------+
- //| JDS_BB.mq4 |
- //| Copyright 2022.04, Developer @ShJavohir |
- //| https://www.t.me/shjavohir |
- //+------------------------------------------------------------------+
- #property copyright "Copyright 2022.04, Developer @ShJavohir"
- #property link "https://www.t.me/shjavohir"
- #property version "1.10"
- #property strict
- enum BBProcess {
- CROSS_BB_UP,
- CROSS_CENTER,
- CROSS_BB_DOWN
- };
- extern const string DIVIDER_1 = ""; //<===== Asosiy sozlamalar =====>
- extern double LOT = 0.01; //Lot
- extern double MARTINGALE_FACTOR = 2; //Martingale factor
- extern double TARGET_PROFIT = 1; //Kerakli foyda ($)
- extern int MAGIC_NUMBER = 1202; //Magic number
- extern int SLIPPAGE = 15; //Slippage
- extern const string DIVIDER_2 = ""; //<===== BB sozlamalari =====>
- extern int BB_PERIOD = 8; //BB period
- extern double BB_DEVIATION = 2; //BB deviation
- extern int BB_SHIFT = 0; //BB shift
- extern ENUM_APPLIED_PRICE BB_APPLIED_PRICE = PRICE_CLOSE; //BB ishlatadigan narx
- const string EA_NAME = "JDS_BB by JDSystems";
- bool isOrderOpenedBuy = false;
- double currentLotBuy;
- double lastOrderOpenPriceBuy = 0;
- ENUM_ORDER_TYPE processOrderTypeBuy;
- ENUM_ORDER_TYPE lookingSignalBuy = OP_BUYLIMIT; //Means no looking signal
- static datetime lastCandleTimeBuy;
- bool isBBpassedBuy = false;
- bool isOrderOpenedSell = false;
- double currentLotSell;
- double lastOrderOpenPriceSell = 0;
- ENUM_ORDER_TYPE processOrderTypeSell;
- ENUM_ORDER_TYPE lookingSignalSell = OP_BUYLIMIT; //Means no looking signal
- static datetime lastCandleTimeSell;
- bool isBBpassedSell = false;
- BBProcess nextProcess;
- bool haveReverseCandle = false;
- bool allowBuy = true;
- bool allowSell = true;
- //+------------------------------------------------------------------+
- //| Expert initialization function |
- //+------------------------------------------------------------------+
- int OnInit() {
- //---
- currentLotBuy = LOT;
- currentLotSell = LOT;
- //---
- return(INIT_SUCCEEDED);
- }
- //+------------------------------------------------------------------+
- //| Expert deinitialization function |
- //+------------------------------------------------------------------+
- void OnDeinit(const int reason) {
- //---
- }
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- string getOrderTypeString(ENUM_ORDER_TYPE orderType) {
- if(orderType == 0) return "BUY";
- if(orderType == 1) return "SELL";
- return "NO";
- }
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- string getNextProcess() {
- if(nextProcess == CROSS_BB_UP) return "BB UP LINE";
- if(nextProcess == CROSS_CENTER) return "BB CENTER LINE";
- if(nextProcess == CROSS_BB_DOWN) return "BB DOWN LINE";
- return "NO";
- }
- //+------------------------------------------------------------------+
- //| Expert tick function |
- //+------------------------------------------------------------------+
- void OnTick() {
- Comment(
- "IsOrderOpened: ", isOrderOpenedBuy,
- "\nLookingSignal (BUY): ", getOrderTypeString(lookingSignalBuy),
- "\nLookingSignal (SELL): ", getOrderTypeString(lookingSignalSell),
- "\nProcessOrderType (BUY): ", getOrderTypeString(processOrderTypeBuy),
- "\nProcessOrderType (SELL): ", getOrderTypeString(processOrderTypeSell),
- "\nCurrent lot: ", currentLotBuy,
- "\nLast order open price: ", lastOrderOpenPriceBuy,
- "\nIsBBCrossed: ", isBBpassedBuy,
- "\nAllow buy: ", allowBuy,
- "\nAllow sell; ", allowSell
- );
- //////////////////////////////
- //Comment(high, " ",low, " ",open, " ",close, open>close?"Bearish":"Bullish");
- if(isOrderOpenedBuy) {
- if(getProfit(OP_BUY) >= TARGET_PROFIT ) { // * ( getLotBuy() / (LOT * MARTINGALE_FACTOR))
- if(closeAllOrders(OP_BUY)) {
- resetLotBuy();
- isOrderOpenedBuy = false;
- lastOrderOpenPriceBuy = Ask;
- processOrderTypeBuy = OP_BUYLIMIT;;
- lookingSignalBuy = OP_BUYLIMIT;// no signal;
- if(!isOrderOpenedSell) allowBuy = true;
- resetLotSell();
- isOrderOpenedSell = false;
- lastOrderOpenPriceSell = Bid;
- processOrderTypeSell = OP_SELLLIMIT;;
- lookingSignalSell = OP_SELLLIMIT;// no signal;
- if(!isOrderOpenedBuy) allowSell = true;
- }
- }
- if(true) { //lastCandleTimeBuy != Time[0]
- double open = Open[1]; //iCustom(Symbol(), 0, IndicatorName, 10, true, 2, 1);
- double close = Close[1]; // iCustom(Symbol(), 0, IndicatorName, 10, true, 3, 1);
- if(processOrderTypeBuy == OP_BUY && Ask < lastOrderOpenPriceBuy) {
- if(Close[0] > getBBHigher()) {
- isBBpassedBuy = true;
- }
- if(true && Close[0] < getBBLower() && allowBuy) //
- if(buy()) {
- isBBpassedBuy = false;
- lastOrderOpenPriceBuy = Ask;
- allowBuy = false;
- allowSell = true;
- }
- }
- lastCandleTimeBuy = Time[0];
- }
- } else if(allowBuy) {
- double high = High[0];//iCustom(Symbol(), 0, IndicatorName, 10, true, 0, 1);
- double low = Low[0]; //iCustom(Symbol(), 0, IndicatorName, 10, true, 1, 1);
- double open = Open[0];//iCustom(Symbol(), 0, IndicatorName, 10, true, 2, 1);
- double close = Close[0];//iCustom(Symbol(), 0, IndicatorName, 10, true, 3, 1);
- if(lookingSignalBuy != OP_BUY && lookingSignalBuy != OP_SELL) {
- //look for initial signal
- if(close < getBBLower()) {
- //if(getMaSignal() == OP_BUY)
- lookingSignalBuy = OP_BUY;
- }
- } else if(lookingSignalBuy == OP_BUY) {
- if(lookingSignalBuy == OP_BUY) {
- if(close < getBBLower()) {
- //open order
- int ticket = openBuyOrder();
- if(ticket > 0) {
- isOrderOpenedBuy = true;
- currentLotBuy = LOT;
- multiplyLotBuy(MARTINGALE_FACTOR);
- lastOrderOpenPriceBuy = Ask;
- processOrderTypeBuy = OP_BUY;
- lookingSignalBuy = OP_BUYLIMIT;// no signal;
- isBBpassedBuy = false;
- allowBuy = false;
- allowSell = true;
- } else {
- logError(__FUNCTION__, "Could not open order", GetLastError());
- }
- }
- }
- }
- }
- if(isOrderOpenedSell) {
- if(getProfit(OP_SELL) >= TARGET_PROFIT) { // * (getLotSell() / (LOT * MARTINGALE_FACTOR))
- if(closeAllOrders(OP_SELL)) {
- resetLotSell();
- isOrderOpenedSell = false;
- lastOrderOpenPriceSell = Bid;
- processOrderTypeSell = OP_SELLLIMIT;;
- lookingSignalSell = OP_SELLLIMIT;// no signal;
- if(!isOrderOpenedBuy) allowSell = true;
- resetLotBuy();
- isOrderOpenedBuy = false;
- lastOrderOpenPriceBuy = Ask;
- processOrderTypeBuy = OP_BUYLIMIT;;
- lookingSignalBuy = OP_BUYLIMIT;// no signal;
- if(!isOrderOpenedSell) allowBuy = true;
- }
- }
- if(true) { //lastCandleTimeSell != Time[0]
- double open = Open[1];//iCustom(Symbol(), 0, IndicatorName, 10, true, 2, 1);
- double close = Close[1];//iCustom(Symbol(), 0, IndicatorName, 10, true, 3, 1);
- if(processOrderTypeSell == OP_SELL && Bid > lastOrderOpenPriceSell) {
- if(Close[0] < getBBLower()) {
- isBBpassedSell = true;
- }
- if(true && Close[0] > getBBHigher() && allowSell) //
- if(sell()) {
- isBBpassedSell = false;
- lastOrderOpenPriceSell = Bid;
- allowSell = false;
- allowBuy = true;
- }
- }
- lastCandleTimeSell = Time[0];
- }
- } else if(allowSell) {
- double high = High[0];//iCustom(Symbol(), 0, IndicatorName, 10, true, 0, 1);
- double low = Low[0]; //iCustom(Symbol(), 0, IndicatorName, 10, true, 1, 1);
- double open = Open[0];//iCustom(Symbol(), 0, IndicatorName, 10, true, 2, 1);
- double close = Close[0];//iCustom(Symbol(), 0, IndicatorName, 10, true, 3, 1);
- if(lookingSignalSell != OP_BUY && lookingSignalSell != OP_SELL) {
- //look for initial signal
- if(close > getBBHigher()) {
- //if(getMaSignal() == OP_BUY)
- lookingSignalSell = OP_SELL;
- }
- } else if(lookingSignalSell == OP_SELL) {
- if(lookingSignalSell == OP_SELL) {
- if(close > getBBHigher()) {
- //open order
- int ticket = openSellOrder();
- if(ticket > 0) {
- isOrderOpenedSell = true;
- currentLotSell = LOT;
- multiplyLotSell(MARTINGALE_FACTOR);
- lastOrderOpenPriceSell = Bid;
- processOrderTypeSell = OP_SELL;
- lookingSignalSell = OP_SELLLIMIT;// no signal;
- isBBpassedSell = false;
- allowSell = false;
- allowBuy = true;
- } else {
- logError(__FUNCTION__, "Could not open order", GetLastError());
- }
- }
- }
- }
- }
- //+------------------------------------------------------------------+
- }
- /*
- if(lookingSignal == OP_SELL) {
- if(close < open && close < getBBHigher()) {
- //open order
- int ticket = openSellOrder();
- if(ticket > 0) {
- isOrderOpened = true;
- currentLot = LOT;
- multiplyLot(MARTINGALE_FACTOR);
- lastOrderOpenPrice = Bid;
- processOrderType = OP_SELL;
- lookingSignal = OP_BUYLIMIT;// no signal;
- nextProcess = CROSS_CENTER;
- isBBpassed = false;
- haveReverseCandle = false;
- } else {
- logError(__FUNCTION__, "Could not open order", GetLastError());
- }
- }
- }
- */
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- double getBBLower() {
- return iBands(Symbol(), 0, BB_PERIOD, BB_DEVIATION, BB_SHIFT, PRICE_CLOSE, MODE_LOWER, 1);
- }
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- double getBBHigher() {
- return iBands(Symbol(), 0, BB_PERIOD, BB_DEVIATION, BB_SHIFT, PRICE_CLOSE, MODE_UPPER, 1);
- }
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- double getBBMid() {
- return iBands(Symbol(), 0, BB_PERIOD, BB_DEVIATION, BB_SHIFT, PRICE_CLOSE, MODE_MAIN, 1);
- }
- //+------------------------------------------------------------------+
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- int openBuyOrder(bool isTpZero = false) {
- RefreshRates();
- return (OrderSend(Symbol(), OP_BUY, getLotBuy(), Ask, SLIPPAGE, 0, 0, EA_NAME, MAGIC_NUMBER));
- }
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- int openSellOrder(bool isTpZero = false) {
- RefreshRates();
- return (OrderSend(Symbol(), OP_SELL, getLotSell(), Bid, SLIPPAGE, 0, 0, EA_NAME, MAGIC_NUMBER));
- }
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- double getLotBuy() {
- return currentLotBuy;
- }
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- double getLotSell() {
- return currentLotSell;
- }
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- void multiplyLotBuy(double factor) {
- currentLotBuy = currentLotBuy * factor;
- Print("Lot multiplied BUY: ", currentLotBuy);
- }
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- void multiplyLotSell(double factor) {
- currentLotSell = currentLotSell * factor;
- Print("Lot multiplied SELL: ", currentLotSell);
- }
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- void resetLotBuy() {
- currentLotBuy = LOT;
- }
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- void resetLotSell() {
- currentLotSell = LOT;
- }
- //+------------------------------------------------------------------+
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- void logError(string functionName, string msg, int errCode, bool isNoisy = false) {
- if(isNoisy) {
- Alert("Error in function ", functionName, ". Message: ", msg, " Error code: ", errCode);
- } else {
- Print("Error in function ", functionName, ". Message: ", msg, " Error code: ", errCode);
- }
- }
- //+------------------------------------------------------------------+
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- //+------------------------------------------------------------------+
- //+------------------------------------------------------------------+
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- bool closeAllOrders(ENUM_ORDER_TYPE orderType) {
- bool didAllClosed = true;
- for (int i = (OrdersTotal() - 1); i >= 0; i--) {
- //If the order cannot be selected throw and log an error
- if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false) {
- didAllClosed = false;
- logError(__FUNCTION__, "Could not select order", GetLastError());
- break;
- } else {
- //OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), SLIPPAGE);
- if(OrderMagicNumber() == MAGIC_NUMBER && OrderSymbol() == Symbol()) {
- if (OrderType() == OP_BUY && orderType == OP_BUY) {
- if(!OrderClose(OrderTicket(), OrderLots(), Bid, SLIPPAGE)) {
- logError(__FUNCTION__, "Could not close order", GetLastError());
- didAllClosed = false;
- }
- } else if(OrderType() == OP_SELL && orderType == OP_SELL) {
- if(!OrderClose(OrderTicket(), OrderLots(), Ask, SLIPPAGE)) {
- logError(__FUNCTION__, "Could not close order", GetLastError());
- didAllClosed = false;
- }
- } else {
- if(!OrderDelete(OrderTicket())) logError(__FUNCTION__, "Could not delete order", GetLastError());
- }
- }
- }
- }
- return didAllClosed;
- }
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- double getProfit(ENUM_ORDER_TYPE orderType) {
- double profit = 0;
- for (int i = (OrdersTotal() - 1); i >= 0; i--) {
- //If the order cannot be selected throw and log an error
- if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false) {
- logError(__FUNCTION__, "Could not select order", GetLastError());
- } else {
- //OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), SLIPPAGE);
- if(OrderMagicNumber() == MAGIC_NUMBER && OrderSymbol() == _Symbol) {
- if (OrderType() == orderType) {
- profit += OrderProfit();
- }
- }
- }
- }
- return profit;
- }
- //+------------------------------------------------------------------+
- bool buy() {
- int ticket = openBuyOrder();
- if(ticket > 0) {
- multiplyLotBuy(MARTINGALE_FACTOR);
- lastOrderOpenPriceBuy = Ask;
- processOrderTypeBuy = OP_BUY;
- lookingSignalBuy = OP_BUYLIMIT;// no signal;
- return true;
- } else {
- logError(__FUNCTION__, "Could not open order in process", GetLastError());
- return false;
- }
- }
- //+------------------------------------------------------------------+
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- bool sell() {
- int ticket = openSellOrder();
- if(ticket > 0) {
- multiplyLotSell(MARTINGALE_FACTOR);
- lastOrderOpenPriceSell = Bid;
- processOrderTypeSell = OP_SELL;
- lookingSignalSell = OP_BUYLIMIT;// no signal
- return true;
- } else {
- logError(__FUNCTION__, "Could not open order in process", GetLastError());
- return false;
- }
- }
- //+------------------------------------------------------------------+
- //+------------------------------------------------------------------+
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement