Пример #1
0
    def close_full(self, ticket):
        position_id = int(ticket)
        pos = mt5.positions_get(ticket=position_id)[0]
        order = mt5.TradePosition(pos).type
        symbol = mt5.TradePosition(pos).symbol

        if order == 0:  # Buy
            price = mt5.symbol_info_tick(symbol).bid
            # TODO Check if deviation even needs to be in this request
            request = {
                "action": mt5.TRADE_ACTION_DEAL,
                "symbol": symbol,
                "position": position_id,
                "volume": mt5.TradePosition(pos).volume,
                "type": mt5.ORDER_TYPE_SELL,
                "price": price,
                "magic": self.magic,
                "comment": f"python close all {symbol}",
            }
        elif order == 1:  # Sell
            price = mt5.symbol_info_tick(symbol).ask
            request = {
                "action": mt5.TRADE_ACTION_DEAL,
                "symbol": symbol,
                "position": position_id,
                "volume": mt5.TradePosition(pos).volume,
                "type": mt5.ORDER_TYPE_BUY,
                "price": price,
                "magic": self.magic,
                "comment": f"python close all {symbol}",
            }

        result = mt5.order_send(request)
Пример #2
0
    def runner(self):
        """Returns an order request. Will automatically close 'pct_close' percent of all positions and set the stoploss
            to the opening price + 'sl_points'

            Will implement a ticket system to allow specification of a position"""
        for pos in mt5.positions_get():
            position_id = mt5.TradePosition(pos).ticket
            symbol = mt5.TradePosition(pos).symbol
            order = mt5.TradePosition(pos).type
            point = mt5.symbol_info(symbol).point
            price = mt5.TradePosition(pos).price_open
            bid = mt5.symbol_info_tick(symbol).bid
            ask = mt5.symbol_info_tick(symbol).ask
            current_price = mt5.TradePosition(pos).price_current
            pct_close = 80
            sl_points = 30
            tp_points = 500

            if order == 0:  # Buy
                new_sl = round(price + sl_points * point, 3)
                if current_price < new_sl:
                    print("Desired SL is under current price")
                else:
                    self.close_custom_pct(pct_close, ticket=position_id)
                    request = {
                        "action": mt5.TRADE_ACTION_SLTP,
                        "symbol": symbol,
                        "position": position_id,
                        "sl": new_sl,
                        "tp": round(ask + tp_points * point, 3),
                        "magic": self.magic,
                        "comment": f"python runner {symbol}"
                    }
            if order == 1:  # Sell
                new_sl = round(price - sl_points * point, 3)
                if current_price > new_sl:
                    print("Desired SL is over current price")
                else:
                    self.close_custom_pct(pct_close, ticket=position_id)
                    request = {
                        "action": mt5.TRADE_ACTION_SLTP,
                        "symbol": symbol,
                        "position": position_id,
                        "sl": new_sl,
                        "tp": round(bid - (tp_points * point), 3),
                        "magic": self.magic,
                        "comment": f"python runner {symbol}"
                    }

            try:
                result = mt5.order_send(request)
                self.order_error_check(result)
            except UnboundLocalError:
                print(f"Desired SL      : {new_sl}")
                print(
                    f"Current Price   : {mt5.TradePosition(pos).price_current}"
                )
Пример #3
0
 def get_positions(self):
     """Returns open positions"""
     for count, order in enumerate(mt5.positions_get(), 1):
         pip_diff = abs((mt5.TradePosition(order).price_open -
                         mt5.TradePosition(order).sl) * 100)
         max_loss = round(
             (pip_diff * (mt5.TradePosition(order).volume *
                          (0.01 / mt5.TradePosition(order).sl))) * 100000,
             2)
         current_risk = round((max_loss / self.balance) * 100, 2)
         msg = ""
         msg += f"   Position {count} - {self.Orders(mt5.TradePosition(order).type).name}"
         msg += f"Ticket      : {mt5.TradePosition(order).ticket}\n"
         msg += f"Volume      : {mt5.TradePosition(order).volume} @ {current_risk}% Risk\n"
         msg += f"Open Price  : {mt5.TradePosition(order).price_open:.3f}\n"
         msg += f"TP          : {mt5.TradePosition(order).tp:.3f}\n"
         msg += f"SL          : {mt5.TradePosition(order).sl:.3f}\n"
         msg += "----------------------------\n"
Пример #4
0
    def close_all(self):
        """Close all currently open positions, regardless of symbol and order type"""
        while mt5.positions_total():
            pos = mt5.positions_get(
            )[0]  # FIXME Im sure this could be better. Try not to use indexing
            position_id = mt5.TradePosition(pos).ticket
            symbol = mt5.TradePosition(pos).symbol

            order = mt5.TradePosition(pos).type
            if order == 0:  # Buy
                price = mt5.symbol_info_tick(symbol).bid
                # TODO Check if deviation even needs to be in this request
                request = {
                    "action": mt5.TRADE_ACTION_DEAL,
                    "symbol": symbol,
                    "position": position_id,
                    "volume": mt5.TradePosition(pos).volume,
                    "type": mt5.ORDER_TYPE_SELL,
                    "price": price,
                    "magic": self.magic,
                    "comment": f"python close all {symbol}",
                }
            elif order == 1:  # Sell
                price = mt5.symbol_info_tick(symbol).ask
                request = {
                    "action": mt5.TRADE_ACTION_DEAL,
                    "symbol": symbol,
                    "position": position_id,
                    "volume": mt5.TradePosition(pos).volume,
                    "type": mt5.ORDER_TYPE_BUY,
                    "price": price,
                    "magic": self.magic,
                    "comment": f"python close all {symbol}",
                }

            result = mt5.order_send(request)
        return "No Open Positions"
Пример #5
0
    def auto_be(self, ticket=""):
        """Returns an order request. Sets all open positions to break even + a few points to ensure no loss is taken"""
        if ticket == "":
            for pos in mt5.positions_get():
                position_id = mt5.TradePosition(pos).ticket
                symbol = mt5.TradePosition(pos).symbol
                tp = mt5.TradePosition(pos).tp
                price = mt5.TradePosition(pos).price_open
                point = mt5.symbol_info(symbol).point
                order = mt5.TradePosition(pos).type

                if order == 1:
                    request = {
                        "action": mt5.TRADE_ACTION_SLTP,
                        "symbol": symbol,
                        "position": position_id,
                        "sl": round(price - 5 * point, 3),
                        "tp": tp,
                        "magic": self.magic,
                        "comment": f"python auto BE {symbol}"
                    }

                elif order == 0:
                    request = {
                        "action": mt5.TRADE_ACTION_SLTP,
                        "symbol": symbol,
                        "position": position_id,
                        "sl": round(price + 5 * point, 3),
                        "tp": tp,
                        "magic": self.magic,
                        "comment": f"python auto BE {symbol}"
                    }

                result = mt5.order_send(request)
        else:
            position_id = int(ticket)
            pos = mt5.positions_get(ticket=position_id)[0]
            symbol = mt5.TradePosition(pos).symbol
            tp = mt5.TradePosition(pos).tp
            order = mt5.TradePosition(pos).type
            price = mt5.TradePosition(pos).price_open
            point = mt5.symbol_info(symbol).point

            if order == 1:
                request = {
                    "action": mt5.TRADE_ACTION_SLTP,
                    "symbol": symbol,
                    "position": position_id,
                    "sl": round(price - 5 * point, 3),
                    "tp": tp,
                    "magic": self.magic,
                    "comment": f"python auto BE {symbol}"
                }

            elif order == 0:
                request = {
                    "action": mt5.TRADE_ACTION_SLTP,
                    "symbol": symbol,
                    "position": position_id,
                    "sl": round(price + 5 * point, 3),
                    "tp": tp,
                    "magic": self.magic,
                    "comment": f"python auto BE {symbol}"
                }
            result = mt5.order_send(request)
        self.order_error_check(result)
Пример #6
0
    def half_risk(self, ticket=""):
        """Returns an order request that halves the initial risk of an open position
            Will have a ticket system implemented that will allow a position to be specified"""
        if ticket == "":
            for pos in mt5.positions_get():
                position_id = mt5.TradePosition(pos).ticket
                symbol = mt5.TradePosition(pos).symbol
                tp = mt5.TradePosition(pos).tp
                sl = mt5.TradePosition(pos).sl
                order = mt5.TradePosition(pos).type
                price = mt5.TradePosition(pos).price_open

                if order == 0:  # Buy
                    diff = round(price - sl, 3)
                    request = {
                        "action": mt5.TRADE_ACTION_SLTP,
                        "symbol": symbol,
                        "position": position_id,
                        "sl": round(sl + (diff / 2), 3),
                        "tp": tp,
                        "magic": self.magic,
                        "comment": f"python half risk {symbol}"
                    }
                if order == 1:  # Sell
                    diff = round(sl - price, 3)
                    request = {
                        "action": mt5.TRADE_ACTION_SLTP,
                        "symbol": symbol,
                        "position": position_id,
                        "sl": round(sl - (diff / 2), 3),
                        "tp": tp,
                        "magic": self.magic,
                        "comment": f"python half risk {symbol}"
                    }
                result = mt5.order_send(request)
        else:
            position_id = int(ticket)
            pos = mt5.positions_get(ticket=position_id)[0]
            symbol = mt5.TradePosition(pos).symbol
            tp = mt5.TradePosition(pos).tp
            sl = mt5.TradePosition(pos).sl
            order = mt5.TradePosition(pos).type
            price = mt5.TradePosition(pos).price_open

            if order == 0:  # Buy
                diff = round(price - sl, 3)
                request = {
                    "action": mt5.TRADE_ACTION_SLTP,
                    "symbol": symbol,
                    "position": position_id,
                    "sl": round(sl + (diff / 2), 3),
                    "tp": tp,
                    "magic": self.magic,
                    "comment": f"python half risk {symbol}"
                }
            if order == 1:  # Sell
                diff = round(sl - price, 3)
                request = {
                    "action": mt5.TRADE_ACTION_SLTP,
                    "symbol": symbol,
                    "position": position_id,
                    "sl": round(sl - (diff / 2), 3),
                    "tp": tp,
                    "magic": self.magic,
                    "comment": f"python half risk {symbol}"
                }

            result = mt5.order_send(request)
        self.order_error_check(result)
Пример #7
0
    def close_custom_volume(self, vol, ticket=1):
        """Returns an order request that closes a position by a specified volume
            Currently not implemented within the GUI"""
        if ticket == 1:
            # If no ticket specified, close half of all positions
            for pos in mt5.positions_get():
                position_id = mt5.TradePosition(pos).ticket
                symbol = mt5.TradePosition(pos).symbol
                sell_vol = round(vol, 2)
                order = mt5.TradePosition(pos).type

                if order == 0:
                    price = mt5.symbol_info_tick(symbol).bid
                    request = {
                        "action": mt5.TRADE_ACTION_DEAL,
                        "symbol": symbol,
                        "position": position_id,
                        "volume": sell_vol,
                        "type": mt5.ORDER_TYPE_SELL,
                        "price": price,
                        "magic": self.magic,
                        "comment": f"python close half {symbol}"
                    }
                if order == 1:
                    price = mt5.symbol_info_tick(symbol).ask
                    request = {
                        "action": mt5.TRADE_ACTION_DEAL,
                        "symbol": symbol,
                        "position": position_id,
                        "volume": sell_vol,
                        "type": mt5.ORDER_TYPE_BUY,
                        "price": price,
                        "magic": self.magic,
                        "comment": f"python close half {symbol}"
                    }
                result = mt5.order_send(request)
        else:
            for pos in mt5.positions_get(ticket=ticket):
                position_id = mt5.TradePosition(pos).ticket
                symbol = mt5.TradePosition(pos).symbol
                sell_vol = round(mt5.TradePosition(pos).volume / 2, 2)
                order = mt5.TradePosition(pos).type

                if order == 0:
                    price = mt5.symbol_info_tick(symbol).bid
                    request = {
                        "action": mt5.TRADE_ACTION_DEAL,
                        "symbol": symbol,
                        "position": position_id,
                        "volume": sell_vol,
                        "type": mt5.ORDER_TYPE_SELL,
                        "price": price,
                        "magic": self.magic,
                        "comment": f"python close half {symbol}"
                    }
                if order == 1:
                    price = mt5.symbol_info_tick(symbol).ask
                    request = {
                        "action": mt5.TRADE_ACTION_DEAL,
                        "symbol": symbol,
                        "position": position_id,
                        "volume": sell_vol,
                        "type": mt5.ORDER_TYPE_BUY,
                        "price": price,
                        "magic": self.magic,
                        "comment": f"python close half {symbol}"
                    }
                result = mt5.order_send(request)
Пример #8
0
    def close_custom_pct(self, percent_close, ticket=""):
        """Returns an order request that closes a specified percentage of a specific position
            ticket: position ID
            When ticket=1, closes specified percentage of ALL currently open positions"""
        if ticket == "":
            # If no ticket specified, close half of all positions
            for pos in mt5.positions_get():
                position_id = mt5.TradePosition(pos).ticket
                symbol = mt5.TradePosition(pos).symbol
                sell_vol = round(
                    ((mt5.TradePosition(pos).volume / 100) * percent_close), 2)
                order = mt5.TradePosition(pos).type

                if order == 0:
                    price = mt5.symbol_info_tick(symbol).bid
                    request = {
                        "action": mt5.TRADE_ACTION_DEAL,
                        "symbol": symbol,
                        "position": position_id,
                        "volume": sell_vol,
                        "type": mt5.ORDER_TYPE_SELL,
                        "price": price,
                        "magic": self.magic,
                        "comment": f"python close half {symbol}"
                    }
                if order == 1:
                    price = mt5.symbol_info_tick(symbol).ask
                    request = {
                        "action": mt5.TRADE_ACTION_DEAL,
                        "symbol": symbol,
                        "position": position_id,
                        "volume": sell_vol,
                        "type": mt5.ORDER_TYPE_BUY,
                        "price": price,
                        "magic": self.magic,
                        "comment": f"python close half {symbol}"
                    }
                result = mt5.order_send(request)

        else:
            for pos in mt5.positions_get(ticket=int(ticket)):
                position_id = mt5.TradePosition(pos).ticket
                symbol = mt5.TradePosition(pos).symbol
                sell_vol = round(
                    ((mt5.TradePosition(pos).volume / 100) * percent_close), 2)
                order = mt5.TradePosition(pos).type

                if order == 0:
                    price = mt5.symbol_info_tick(symbol).bid
                    request = {
                        "action": mt5.TRADE_ACTION_DEAL,
                        "symbol": symbol,
                        "position": position_id,
                        "volume": sell_vol,
                        "type": mt5.ORDER_TYPE_SELL,
                        "price": price,
                        "magic": self.magic,
                        "comment": f"python close half {symbol}"
                    }
                if order == 1:
                    price = mt5.symbol_info_tick(symbol).ask
                    request = {
                        "action": mt5.TRADE_ACTION_DEAL,
                        "symbol": symbol,
                        "position": position_id,
                        "volume": sell_vol,
                        "type": mt5.ORDER_TYPE_BUY,
                        "price": price,
                        "magic": self.magic,
                        "comment": f"python close half {symbol}"
                    }
                result = mt5.order_send(request)
                self.order_error_check(result)
Пример #9
0
    def close_half(self, ticket=""):
        """Returns a order request that closes 50% of a selected position by volume
            ticket: the position ID that will be modified. (Currently not implemented)
            When ticket=1, it will close half of ALL currently open positions"""
        if ticket == "":
            # If no ticket specified, close half of all positions
            for pos in mt5.positions_get():
                position_id = mt5.TradePosition(pos).ticket
                symbol = mt5.TradePosition(pos).symbol
                sell_vol = round(mt5.TradePosition(pos).volume / 2, 2)
                order = mt5.TradePosition(pos).type

                # TODO check if deviation needs to be in these requests
                if order == 0:
                    price = mt5.symbol_info_tick(symbol).bid
                    request = {
                        "action": mt5.TRADE_ACTION_DEAL,
                        "symbol": symbol,
                        "position": position_id,
                        "volume": sell_vol,
                        "type": mt5.ORDER_TYPE_SELL,
                        "price": price,
                        "magic": self.magic,
                        "comment": f"python close half {symbol}"
                    }
                if order == 1:
                    price = mt5.symbol_info_tick(symbol).ask
                    request = {
                        "action": mt5.TRADE_ACTION_DEAL,
                        "symbol": symbol,
                        "position": position_id,
                        "volume": sell_vol,
                        "type": mt5.ORDER_TYPE_BUY,
                        "price": price,
                        "magic": self.magic,
                        "comment": f"python close half {symbol}"
                    }
                result = mt5.order_send(request)
        else:
            for pos in mt5.positions_get(ticket=int(ticket)):
                position_id = mt5.TradePosition(pos).ticket
                symbol = mt5.TradePosition(pos).symbol
                sell_vol = round(mt5.TradePosition(pos).volume / 2, 2)
                order = mt5.TradePosition(pos).type

                if order == 0:
                    price = mt5.symbol_info_tick(symbol).bid
                    request = {
                        "action": mt5.TRADE_ACTION_DEAL,
                        "symbol": symbol,
                        "position": position_id,
                        "volume": sell_vol,
                        "type": mt5.ORDER_TYPE_SELL,
                        "price": price,
                        "magic": self.magic,
                        "comment": f"python close half {symbol}"
                    }
                if order == 1:
                    price = mt5.symbol_info_tick(symbol).ask
                    request = {
                        "action": mt5.TRADE_ACTION_DEAL,
                        "symbol": symbol,
                        "position": position_id,
                        "volume": sell_vol,
                        "type": mt5.ORDER_TYPE_BUY,
                        "price": price,
                        "magic": self.magic,
                        "comment": f"python close half {symbol}"
                    }
                result = mt5.order_send(request)