def capital_gain(self, account: Account): """Returns the $ amount gained if TP is filled. If no TP is set, returns account equity.""" if self.trades == 1: #For one trades/asset if self.open_positions[0].type == 0: #For long positions orderType = mt5.ORDER_TYPE_BUY else: #For short positions orderType = mt5.ORDER_TYPE_SELL #________Calculating capital gain_________# # try: # gain = mt5.order_calc_profit(orderType, self.asset, self.open_positions[0].volume, self.open_positions[0].price_open, self.open_positions[0].tp) # except TypeError: # gain = account.equity gain = mt5.order_calc_profit(orderType, self.asset, self.open_positions[0].volume, self.open_positions[0].price_open, self.open_positions[0].tp) if gain == None: return account.equity else: return gain else: #For > one trades/asset gain = 0 for i in self.open_positions: if i.type == 0: orderType = mt5.ORDER_TYPE_BUY else: orderType = mt5.ORDER_TYPE_SELL #________Calculating capital gain_________# try: gain += mt5.order_calc_profit(orderType, self.asset, i.volume, i.price_open, i.tp) except TypeError: gain = account.equity # gain += mt5.order_calc_profit(orderType, self.asset, i.volume, i.price_open, i.tp) # if gain == None: # return account.equity # else: return gain return gain
def capital_target(self, account: Account) -> None: """Returns the $ amount that will be received if TP is hit""" target = mt5.order_calc_profit(self._type(), self.symbol, self.volume, self.entry, self.take_profit) if target is None: return account.equity else: return target
def capital_risk(self, account: Account) -> None: """Returns the $ amount lost if SL is hit""" risk = mt5.order_calc_profit(self._type(), self.symbol, self.volume, self.entry, self.stop_loss) if risk is None: return -(account.equity) else: return risk
def profit(position): if position[0].type == 0: #For long positions type_ = mt5.ORDER_TYPE_BUY else: type_ = mt5.ORDER_TYPE_SELL profit = mt5.order_calc_profit(type_, position[0].symbol, position[0].volume, position[0].price_open, position[0].sl) return profit
def capital_risk(self, account: Account): """Returns the $ amount that will be lost if SL is filled. If no SL is placed it returns account equity as entire equity is @ risk. Requires an account object for equity""" if self.trades == 1: #For one trades/asset if self.open_positions[0].type == 0: #For long positions type_ = mt5.ORDER_TYPE_BUY else: type_ = mt5.ORDER_TYPE_SELL # try: # risk = mt5.order_calc_profit(type_, self.asset, self.open_positions[0].volume, self.open_positions[0].price_open, self.open_positions[0].sl) # except TypeError: # risk = account.equity risk = mt5.order_calc_profit(type_, self.asset, self.open_positions[0].volume, self.open_positions[0].price_open, self.open_positions[0].sl) if risk == None: return -(account.equity) else: return risk else: #For > one trades/asset risk = 0 for i in self.open_positions: if i.type == 0: orderType = mt5.ORDER_TYPE_BUY else: orderType = mt5.ORDER_TYPE_SELL try: risk += mt5.order_calc_profit(orderType, self.asset, i.volume, i.price_open, i.sl) except TypeError: risk = -(account.equity) # risk += mt5.order_calc_profit(orderType, self.asset, i.volume, i.price_open, i.sl) # if risk == None: # return account.equity # else: return risk
import MetaTrader5 as mt5 #____________Initializing Metatrader5____________# mt5.initialize() print(mt5.version(), '\n') print(mt5.terminal_info(), '\n') print(mt5.account_info(), '\n') order = mt5.orders_get() #getting all pending orders #____________Calculating required Margin_________________# margin = mt5.order_calc_margin( mt5.ORDER_TYPE_BUY, order[0].symbol, order[0].volume_initial, order[0].price_open) #returns required margin in account currency print('\nMargin requirement for buying {0} @ {1} is ${2}'.format( order[0].symbol, order[0].price_open, margin)) #____________Calculating Profit/loss_____________# volatility = float(input('Enter the desired volatility in percentage: ')) percentage = volatility * (order[0].price_open / 100) profit = mt5.order_calc_profit( mt5.ORDER_TYPE_BUY, order[0].symbol, order[0].volume_initial, order[0].price_open, (order[0].price_open + percentage)) #returns potential P/L in account currency print('\n Profit on {0} at {1} with {2}% volatility is ${3}'.format( order[0].symbol, order[0].price_open, volatility, profit))
distance = 300 for symbol in symbols: symbol_info = mt5.symbol_info(symbol) if symbol_info is None: print(symbol, "not found, skipped") continue if not symbol_info.visible: print(symbol, "is not visible, trying to switch on") if not mt5.symbol_select(symbol, True): print("symbol_select({}}) failed, skipped", symbol) continue point = mt5.symbol_info(symbol).point symbol_tick = mt5.symbol_info_tick(symbol) ask = symbol_tick.ask bid = symbol_tick.bid buy_profit = mt5.order_calc_profit(mt5.ORDER_TYPE_BUY, symbol, lot, ask, ask + distance * point) if buy_profit != None: print(" buy {} {} lot: profit on {} points => {} {}".format( symbol, lot, distance, buy_profit, account_currency)) else: print("order_calc_profit(ORDER_TYPE_BUY) failed, error code =", mt5.last_error()) sell_profit = mt5.order_calc_profit(mt5.ORDER_TYPE_SELL, symbol, lot, bid, bid - distance * point) if sell_profit != None: print(" sell {} {} lots: profit on {} points => {} {}".format( symbol, lot, distance, sell_profit, account_currency)) else: print("order_calc_profit(ORDER_TYPE_SELL) failed, error code =", mt5.last_error()) print()