75 KiB
75 KiB
In [172]:
import pandas as pd
import MetaTrader5 as mt
import time
import reIn [173]:
# login to your Trading Account - sign up in the description
mt.initialize()
login = 10730196
password = '#mDJu9O3'
server = 'VantageInternational-Demo'
mt.login(login, password, server)Out [173]:
True
In [174]:
symbol = 'XAUUSD'In [267]:
ohlc = mt.copy_rates_from_pos(symbol, mt.TIMEFRAME_D1, 0, 60)
ohlc_df = pd.DataFrame(ohlc)
ohlc_df['time']=pd.to_datetime(ohlc_df['time'], unit='s')
ohlc_dfOut [267]:
| time | open | high | low | close | tick_volume | spread | real_volume | |
|---|---|---|---|---|---|---|---|---|
| 0 | 2025-01-31 | 2796.26 | 2817.07 | 2791.03 | 2799.25 | 176088 | 18 | 0 |
| 1 | 2025-02-03 | 2800.67 | 2830.72 | 2772.09 | 2814.76 | 222780 | 18 | 0 |
| 2 | 2025-02-04 | 2814.23 | 2845.34 | 2807.33 | 2842.09 | 190207 | 18 | 0 |
| 3 | 2025-02-05 | 2842.57 | 2882.22 | 2839.76 | 2867.22 | 144079 | 18 | 0 |
| 4 | 2025-02-06 | 2865.36 | 2873.34 | 2834.23 | 2855.99 | 136634 | 18 | 0 |
| 5 | 2025-02-07 | 2855.96 | 2886.76 | 2852.55 | 2861.36 | 174549 | 18 | 0 |
| 6 | 2025-02-10 | 2858.80 | 2911.71 | 2854.73 | 2907.97 | 174533 | 18 | 0 |
| 7 | 2025-02-11 | 2907.84 | 2942.67 | 2881.61 | 2897.68 | 221876 | 18 | 0 |
| 8 | 2025-02-12 | 2898.02 | 2909.09 | 2864.20 | 2904.11 | 185547 | 18 | 0 |
| 9 | 2025-02-13 | 2904.02 | 2929.41 | 2900.54 | 2928.40 | 152063 | 18 | 0 |
| 10 | 2025-02-14 | 2927.85 | 2939.99 | 2876.94 | 2882.55 | 180610 | 18 | 0 |
| 11 | 2025-02-17 | 2884.84 | 2906.38 | 2878.83 | 2897.46 | 129712 | 18 | 0 |
| 12 | 2025-02-18 | 2898.40 | 2936.88 | 2891.99 | 2935.23 | 139030 | 18 | 0 |
| 13 | 2025-02-19 | 2935.71 | 2946.87 | 2918.54 | 2932.76 | 160944 | 18 | 0 |
| 14 | 2025-02-20 | 2933.47 | 2954.83 | 2924.08 | 2938.61 | 155405 | 18 | 0 |
| 15 | 2025-02-21 | 2938.70 | 2949.74 | 2916.75 | 2935.95 | 159274 | 18 | 0 |
| 16 | 2025-02-24 | 2935.19 | 2956.22 | 2921.30 | 2952.46 | 171870 | 18 | 0 |
| 17 | 2025-02-25 | 2952.42 | 2953.75 | 2888.18 | 2914.93 | 198402 | 18 | 0 |
| 18 | 2025-02-26 | 2915.06 | 2930.16 | 2890.77 | 2916.36 | 158179 | 18 | 0 |
| 19 | 2025-02-27 | 2916.73 | 2920.82 | 2867.77 | 2877.15 | 207221 | 18 | 0 |
| 20 | 2025-02-28 | 2877.31 | 2885.07 | 2832.62 | 2859.03 | 224045 | 18 | 0 |
| 21 | 2025-03-03 | 2856.18 | 2895.21 | 2855.57 | 2893.66 | 205740 | 18 | 0 |
| 22 | 2025-03-04 | 2892.77 | 2927.81 | 2881.92 | 2917.65 | 214258 | 18 | 0 |
| 23 | 2025-03-05 | 2917.84 | 2929.88 | 2894.34 | 2919.11 | 231043 | 18 | 0 |
| 24 | 2025-03-06 | 2919.52 | 2926.51 | 2891.20 | 2911.01 | 219058 | 18 | 0 |
| 25 | 2025-03-07 | 2911.39 | 2930.42 | 2896.78 | 2912.06 | 225312 | 18 | 0 |
| 26 | 2025-03-10 | 2911.25 | 2918.22 | 2880.22 | 2889.02 | 220535 | 18 | 0 |
| 27 | 2025-03-11 | 2888.82 | 2922.13 | 2880.30 | 2916.08 | 175258 | 18 | 0 |
| 28 | 2025-03-12 | 2915.66 | 2940.48 | 2906.11 | 2933.24 | 170540 | 18 | 0 |
| 29 | 2025-03-13 | 2934.10 | 2989.05 | 2932.89 | 2989.01 | 177969 | 18 | 0 |
| 30 | 2025-03-14 | 2989.31 | 3004.93 | 2978.46 | 2986.39 | 197059 | 18 | 0 |
| 31 | 2025-03-17 | 2984.83 | 3001.86 | 2982.13 | 3001.42 | 155946 | 18 | 0 |
| 32 | 2025-03-18 | 3001.16 | 3038.25 | 2999.39 | 3034.08 | 179165 | 18 | 0 |
| 33 | 2025-03-19 | 3033.89 | 3051.96 | 3022.79 | 3047.26 | 174636 | 18 | 0 |
| 34 | 2025-03-20 | 3047.96 | 3057.46 | 3025.69 | 3044.64 | 200333 | 18 | 0 |
| 35 | 2025-03-21 | 3044.55 | 3047.43 | 2999.46 | 3023.15 | 185542 | 18 | 0 |
| 36 | 2025-03-24 | 3021.99 | 3033.33 | 3002.46 | 3011.81 | 193224 | 18 | 0 |
| 37 | 2025-03-25 | 3011.62 | 3035.94 | 3007.55 | 3020.34 | 153347 | 18 | 0 |
| 38 | 2025-03-26 | 3019.39 | 3032.03 | 3012.34 | 3018.96 | 162718 | 18 | 0 |
| 39 | 2025-03-27 | 3019.37 | 3059.67 | 3017.60 | 3056.73 | 196828 | 18 | 0 |
| 40 | 2025-03-28 | 3056.57 | 3086.76 | 3054.19 | 3084.56 | 187584 | 18 | 0 |
| 41 | 2025-03-31 | 3086.34 | 3127.96 | 3076.77 | 3124.07 | 209942 | 18 | 0 |
| 42 | 2025-04-01 | 3122.98 | 3148.96 | 3100.79 | 3114.26 | 206537 | 18 | 0 |
| 43 | 2025-04-02 | 3113.80 | 3143.32 | 3105.20 | 3133.56 | 188846 | 18 | 0 |
| 44 | 2025-04-03 | 3142.25 | 3167.64 | 3054.21 | 3113.32 | 313074 | 0 | 0 |
| 45 | 2025-04-04 | 3114.54 | 3136.61 | 3015.79 | 3038.42 | 252778 | 18 | 0 |
| 46 | 2025-04-07 | 3008.62 | 3055.38 | 2956.52 | 2982.60 | 212668 | 0 | 0 |
| 47 | 2025-04-08 | 2983.16 | 3022.64 | 2974.72 | 2982.34 | 185173 | 18 | 0 |
| 48 | 2025-04-09 | 2983.48 | 3099.46 | 2969.98 | 3082.91 | 253727 | 18 | 0 |
| 49 | 2025-04-10 | 3083.53 | 3176.29 | 3071.39 | 3175.77 | 184496 | 18 | 0 |
| 50 | 2025-04-11 | 3175.72 | 3245.34 | 3175.71 | 3237.59 | 187568 | 18 | 0 |
| 51 | 2025-04-14 | 3226.90 | 3245.72 | 3193.72 | 3210.36 | 157467 | 18 | 0 |
| 52 | 2025-04-15 | 3211.18 | 3233.55 | 3210.02 | 3229.79 | 115741 | 18 | 0 |
| 53 | 2025-04-16 | 3231.04 | 3342.39 | 3229.80 | 3342.26 | 172558 | 18 | 0 |
| 54 | 2025-04-17 | 3343.13 | 3357.71 | 3283.98 | 3327.48 | 162159 | 18 | 0 |
| 55 | 2025-04-21 | 3332.68 | 3430.48 | 3328.90 | 3424.67 | 165503 | 18 | 0 |
| 56 | 2025-04-22 | 3424.07 | 3500.04 | 3366.85 | 3381.28 | 205173 | 18 | 0 |
| 57 | 2025-04-23 | 3353.28 | 3386.63 | 3260.21 | 3288.34 | 193560 | 18 | 0 |
| 58 | 2025-04-24 | 3288.51 | 3367.41 | 3288.45 | 3349.38 | 160771 | 18 | 0 |
| 59 | 2025-04-25 | 3350.42 | 3370.71 | 3265.04 | 3318.69 | 240555 | 18 | 0 |
In [268]:
# alltime high, low - last 60 days
ath_df = ohlc_df.iloc[ohlc_df['high'].idxmax()]
ath = round(ath_df['high'],0)
atl = round(ath_df['low'],0)
ath, atlOut [268]:
(3500.0, 3367.0)
In [269]:
#lastday high, low
ldh = ohlc_df.iloc[-2]['high']
ldl = ohlc_df.iloc[-2]['low']
ldh, ldl
Out [269]:
(3367.41, 3288.45)
In [270]:
#today high, low
tdh = ohlc_df.iloc[-1]['high']
tdl = ohlc_df.iloc[-1]['low']
tdh ,tdl
Out [270]:
(3370.71, 3265.04)
In [271]:
def tp_sl():
ohlc = mt.copy_rates_from_pos('XAUUSD', mt.TIMEFRAME_M5, 1, 75)
ohlc_df = pd.DataFrame(ohlc)
ohlc_df['time']=pd.to_datetime(ohlc_df['time'], unit='s')
# Create a column to mark the start of 5-minute intervals
ohlc_df['box_start'] = (ohlc_df['time'].dt.minute % 15 == 0).astype(int)
# Create columns for rolling maximum and minimum over the last 5 candles, excluding the current candle
ohlc_df['max_box'] = ohlc_df['high'].shift(1).rolling(window=15).max()
ohlc_df['min_box'] = ohlc_df['low'].shift(1).rolling(window=15).min()
# Breaking signal
#ohlc_df["Break_signal"] = (ohlc_df["close"] > ohlc_df["max_box"]).astype(int) * 2 + (ohlc_df["close"] < ohlc_df["min_box"]).astype(int)
#print(ohlc_df['max_box'][14].astype(int)*2)
return ohlc_df
In [272]:
lastchart = tp_sl()
lastchart = lastchart.drop(lastchart[lastchart.box_start != 1].index)
lastchartOut [272]:
| time | open | high | low | close | tick_volume | spread | real_volume | box_start | max_box | min_box | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 2025-04-25 17:45:00 | 3282.20 | 3282.66 | 3279.63 | 3280.86 | 1373 | 18 | 0 | 1 | NaN | NaN |
| 4 | 2025-04-25 18:00:00 | 3277.96 | 3280.87 | 3276.92 | 3280.03 | 1236 | 18 | 0 | 1 | NaN | NaN |
| 7 | 2025-04-25 18:15:00 | 3272.39 | 3275.33 | 3272.39 | 3273.67 | 1204 | 18 | 0 | 1 | NaN | NaN |
| 10 | 2025-04-25 18:30:00 | 3272.36 | 3275.36 | 3272.02 | 3274.25 | 1014 | 18 | 0 | 1 | NaN | NaN |
| 13 | 2025-04-25 18:45:00 | 3280.15 | 3281.25 | 3278.42 | 3280.74 | 771 | 18 | 0 | 1 | NaN | NaN |
| 16 | 2025-04-25 19:00:00 | 3282.96 | 3284.49 | 3281.25 | 3284.49 | 839 | 18 | 0 | 1 | 3285.59 | 3270.06 |
| 19 | 2025-04-25 19:15:00 | 3283.87 | 3284.03 | 3282.06 | 3283.46 | 503 | 18 | 0 | 1 | 3285.71 | 3270.06 |
| 22 | 2025-04-25 19:30:00 | 3283.25 | 3285.59 | 3283.12 | 3284.71 | 647 | 18 | 0 | 1 | 3285.71 | 3270.06 |
| 25 | 2025-04-25 19:45:00 | 3280.78 | 3283.15 | 3280.31 | 3283.07 | 696 | 18 | 0 | 1 | 3285.71 | 3272.02 |
| 28 | 2025-04-25 20:00:00 | 3285.31 | 3285.49 | 3281.97 | 3282.23 | 935 | 18 | 0 | 1 | 3287.48 | 3278.42 |
| 31 | 2025-04-25 20:15:00 | 3287.70 | 3290.24 | 3286.50 | 3289.91 | 888 | 18 | 0 | 1 | 3288.42 | 3280.31 |
| 34 | 2025-04-25 20:30:00 | 3286.99 | 3289.14 | 3286.15 | 3289.08 | 754 | 18 | 0 | 1 | 3291.22 | 3280.31 |
| 37 | 2025-04-25 20:45:00 | 3291.99 | 3292.61 | 3289.81 | 3291.86 | 746 | 18 | 0 | 1 | 3294.64 | 3280.31 |
| 40 | 2025-04-25 21:00:00 | 3293.22 | 3295.00 | 3291.48 | 3293.64 | 558 | 18 | 0 | 1 | 3295.10 | 3280.31 |
| 43 | 2025-04-25 21:15:00 | 3293.62 | 3294.76 | 3292.44 | 3294.53 | 424 | 18 | 0 | 1 | 3297.03 | 3281.78 |
| 46 | 2025-04-25 21:30:00 | 3293.21 | 3294.49 | 3292.38 | 3292.54 | 307 | 18 | 0 | 1 | 3297.03 | 3286.15 |
| 49 | 2025-04-25 21:45:00 | 3291.28 | 3291.98 | 3290.34 | 3290.69 | 350 | 18 | 0 | 1 | 3297.03 | 3286.15 |
| 52 | 2025-04-25 22:00:00 | 3288.37 | 3290.12 | 3288.15 | 3289.65 | 522 | 18 | 0 | 1 | 3297.03 | 3288.33 |
| 55 | 2025-04-25 22:15:00 | 3289.72 | 3292.76 | 3289.64 | 3291.62 | 402 | 18 | 0 | 1 | 3297.03 | 3287.78 |
| 58 | 2025-04-25 22:30:00 | 3294.65 | 3298.99 | 3294.65 | 3298.55 | 443 | 18 | 0 | 1 | 3295.21 | 3287.78 |
| 61 | 2025-04-25 22:45:00 | 3308.50 | 3308.56 | 3304.18 | 3308.00 | 699 | 18 | 0 | 1 | 3308.72 | 3287.78 |
| 64 | 2025-04-25 23:00:00 | 3304.77 | 3306.93 | 3304.77 | 3305.68 | 426 | 18 | 0 | 1 | 3312.45 | 3287.78 |
| 67 | 2025-04-25 23:15:00 | 3305.48 | 3307.01 | 3305.44 | 3306.68 | 231 | 18 | 0 | 1 | 3312.45 | 3287.78 |
| 70 | 2025-04-25 23:30:00 | 3308.53 | 3309.31 | 3308.05 | 3308.78 | 172 | 18 | 0 | 1 | 3312.45 | 3289.64 |
| 73 | 2025-04-25 23:45:00 | 3314.35 | 3315.61 | 3313.61 | 3314.07 | 353 | 28 | 0 | 1 | 3314.33 | 3294.65 |
In [285]:
current_high = lastchart['high'][73]
current_close = lastchart['close'][73]
#sl = lastchart['min_box'][23] - tp_sl_ratio * (current_high - current_close) # SL at the high of the current candle
#tp = lastchart['max_box'][23] + tp_sl_ratio * (current_high - current_close)
current_diff = lastchart.max_box.max() - lastchart.min_box.min()
lastchart.max_box.max() - lastchart.min_box.min()
#current_close, current_highOut [285]:
44.26999999999998
In [274]:
last_max = lastchart.max_box.mean()
last_min = lastchart.min_box.mean()
mean_high = round((ath + tdh + ldh + last_max) / 4, 2)
mean_low = round((atl + tdl + ldl + last_min) / 4, 2)
mean_high, mean_low, last_max, last_minOut [274]:
(3383.78, 3300.72, 3297.017, 3282.3729999999996)
In [275]:
res = ath - ldl
res = ldh - tdl
res = mean_high - tdl
fb1 = round(mean_high - (res * 0.236),2)
fb2 = round(mean_high - (res * 0.382),2)
fb3 = round(mean_high - (res * 0.681),2)
fb4 = round(mean_high - (res * 0.786),2)
fb1, fb2, fb3, fb4Out [275]:
(3355.76, 3338.42, 3302.92, 3290.45)
In [276]:
tp = ath - ldh + fb1
sl = fb1 - (tp - ldh)
tp, slOut [276]:
(3488.3500000000004, 3234.8199999999997)
In [277]:
current_price = mt.symbol_info_tick(symbol)
fb = [fb1, fb2, fb3, fb4]
trading_type = []
x = 1
for i in fb:
#print(i)
if current_price.bid < i:
print(f"Aktueller Preis {current_price.bid} ist kleiner als FB{x} {i}, trading ist BuyStop")
trading_type.append('BuyStop')
elif current_price.bid > i:
print(f"Aktueller Preis {current_price.bid} ist größer als FB{x} {i}, trading type ist BuyLimit")
trading_type.append("BuyLimit")
x += 1
trading_type
Out [277]:
Aktueller Preis 3318.69 ist kleiner als FB1 3355.76, trading ist BuyStop Aktueller Preis 3318.69 ist kleiner als FB2 3338.42, trading ist BuyStop Aktueller Preis 3318.69 ist größer als FB3 3302.92, trading type ist BuyLimit Aktueller Preis 3318.69 ist größer als FB4 3290.45, trading type ist BuyLimit
['BuyStop', 'BuyStop', 'BuyLimit', 'BuyLimit']
In [278]:
ldh - tdl
mean_high - mean_lowOut [278]:
83.0600000000004
In [286]:
tp_factor = ath-ldh
tp_factor = ath - tdh
#tp_factor = ath - mean_low
tp_factor = mean_high - mean_low - current_diff
sl_factor = [tp - fb1, tp - fb2, tp - fb3, tp - fb4]
data = {
'trade_type' : [trading_type[0] + '01', trading_type[1] + '02', trading_type[2] + '03', trading_type[3] + '04'],
'trade_price': [fb1, fb2, fb3, fb4],
'trade_volume': [0.1, 0.2, 0.3, 0.4],
#'trade_sl': [fb1 - sl_factor, fb2 - sl_factor, fb3 - sl_factor, fb4 - sl_factor],
#'trade_sl': [fb1 - sl_factor[0], fb2 - sl_factor[1], fb3 - sl_factor[2], fb4 - sl_factor[3]],
'trade_sl': [fb1 - tp_factor, fb2 - tp_factor, fb3 - tp_factor, fb4 - tp_factor],
'trade_tp': [tp_factor + fb1, tp_factor + fb2, tp_factor + fb3, tp_factor + fb4]
}In [280]:
# adj = 15
# factor = 5
# adj2 = adj - 5
# data = {
# 'trade_type' : ['BuyLimit01', 'BuyLimit02', 'BuyLimit03', 'BuyLimit04', 'BuyLimit05', 'BuyLimit06', 'BuyLimit07', 'BuyLimit08', 'BuyLimit09', 'BuyStop01', 'BuyStop02'],
# 'trade_price': [ath - adj, ath - adj - 25, ath - adj - 50, ath - adj - 75, ath - adj - 100, ath - adj - 125, ath - adj - 150, ath - adj - 175, ath - adj - 200, ath - 66, ath - adj],
# 'trade_volume': [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.1, 0.1],
# 'trade_sl': [ath - adj - factor, ath - adj - 25 - factor, ath - adj - 50 - factor, ath - adj - 75 - factor, ath - adj - 100 - factor, ath - adj - 125 - factor, ath - adj - 150 - factor, ath - adj - 175 - factor, ath - adj - 200 - factor, ath - 66 - 20, ath - adj - 20],
# 'trade_tp': [ath - adj2, ath - adj2, ath - adj2, ath - adj2, ath - adj2, ath - adj2, ath - adj2, ath - adj2, ath - adj2, ath, ath]
# }In [288]:
df = pd.DataFrame(data)
dfOut [288]:
| trade_type | trade_price | trade_volume | trade_sl | trade_tp | |
|---|---|---|---|---|---|
| 0 | BuyStop01 | 3355.76 | 0.1 | 3316.97 | 3394.55 |
| 1 | BuyStop02 | 3338.42 | 0.2 | 3299.63 | 3377.21 |
| 2 | BuyLimit03 | 3302.92 | 0.3 | 3264.13 | 3341.71 |
| 3 | BuyLimit04 | 3290.45 | 0.4 | 3251.66 | 3329.24 |
In [282]:
for index, row in df.iterrows():
if bool(re.search('^BuyStop[0-9]{2}', row.trade_type)):
df.loc[index, ['trade_volume']] = 0.1
print(row.trade_type, row.trade_volume, index)
BuyStop01 0.1 0 BuyStop02 0.2 1 BuyLimit03 0.3 2 BuyLimit04 0.4 3
In [283]:
dfOut [283]:
| trade_type | trade_price | trade_volume | trade_sl | trade_tp | |
|---|---|---|---|---|---|
| 0 | BuyStop01 | 3355.76 | 0.1 | 3272.70 | 3438.82 |
| 1 | BuyStop02 | 3338.42 | 0.1 | 3255.36 | 3421.48 |
| 2 | BuyLimit03 | 3302.92 | 0.3 | 3219.86 | 3385.98 |
| 3 | BuyLimit04 | 3290.45 | 0.4 | 3207.39 | 3373.51 |
In [284]:
#if bool(re.search('^BuyLimit[0-9]{2}', df.trade_type[1])) and df.trade_volume[0] == 0.1 and df.trade_volume[1] != 0.1:
# df.at[2, 'trade_volume'] = df.trade_volume[1] + 0.1
#if df.trade_volume[2] != 0.1 and df.trade_volume[2] != 0.1:
# df.at[2, 'trade_volume'] = df.trade_volume[1] + 0.1
#dfIn [168]:
if bool(re.search('^StopLimit[0-9]{2}', 'BuyLimit01')):
print('Wahr')
else:
print('Falsch')Falsch
In [169]:
def buyOrder(trade_type: str, trade_price: float, trade_volume: float, trade_sl: float, trade_tp: float, symbol: str):
if bool(re.search('^BuyLimit[0-9]{2}', trade_type)):
# Buy Stop
request = {
"action": mt.TRADE_ACTION_PENDING,
"symbol": symbol,
"volume": trade_volume, # FLOAT
"type": mt.ORDER_TYPE_BUY_LIMIT,
"price": trade_price,
"sl": trade_sl, # FLOAT
"tp": trade_tp, # FLOAT
"deviation": 20, # INTERGER
"magic": 0, # INTERGER
"comment": trade_type,
"type_time": mt.ORDER_TIME_GTC,
"type_filling": mt.ORDER_FILLING_IOC,
}
else:
#Sell Stop
request = {
"action": mt.TRADE_ACTION_PENDING,
"symbol": symbol,
"volume": trade_volume, # FLOAT
"type": mt.ORDER_TYPE_BUY_STOP,
"price": trade_price,
"sl": trade_sl, # FLOAT
"tp": trade_tp, # FLOAT
"deviation": 20, # INTERGER
"magic": 0, # INTERGER
"comment": trade_type,
"type_time": mt.ORDER_TIME_GTC,
"type_filling": mt.ORDER_FILLING_IOC,
}
order = mt.order_send(request)In [292]:
for index, row in df.iterrows():
#print(index, row)
print(row['trade_type'], row['trade_price'], row['trade_volume'], row['trade_sl'], row['trade_tp'], symbol)
buyOrder(row['trade_type'], row['trade_price'], row['trade_volume'], row['trade_sl'], row['trade_tp'], symbol)
BuyStop01 3355.76 0.1 3316.97 3394.5500000000006 XAUUSD BuyStop02 3338.42 0.2 3299.6299999999997 3377.2100000000005 XAUUSD BuyLimit03 3302.92 0.3 3264.1299999999997 3341.7100000000005 XAUUSD BuyLimit04 3290.45 0.4 3251.6599999999994 3329.2400000000002 XAUUSD
In [289]:
# Remove Pending Order
def rm_pending_orders():
while mt.orders_total() > 0:
pending_orders = mt.orders_get()
order1 = pending_orders[0]
request = {
'action': mt.TRADE_ACTION_REMOVE,
'order': order1.ticket
}
mt.order_send(request)
In [291]:
rm_pending_orders()[1;31m---------------------------------------------------------------------------[0m [1;31mKeyboardInterrupt[0m Traceback (most recent call last) Cell [1;32mIn[291], line 1[0m [1;32m----> 1[0m rm_pending_orders() Cell [1;32mIn[289], line 5[0m, in [0;36mrm_pending_orders[1;34m()[0m [0;32m 3[0m [38;5;28;01mdef[39;00m [38;5;21mrm_pending_orders[39m(): [1;32m----> 5[0m [38;5;28;01mwhile[39;00m mt[38;5;241m.[39morders_total() [38;5;241m>[39m [38;5;241m0[39m: [0;32m 6[0m pending_orders [38;5;241m=[39m mt[38;5;241m.[39morders_get() [0;32m 7[0m order1 [38;5;241m=[39m pending_orders[[38;5;241m0[39m] [1;31mKeyboardInterrupt[0m:
In [ ]:
pos = mt.orders_get()
for i in pos:
print(i.comment)
df.loc[df['trade_type'] == i.comment, 'pending_trades'] = 1
df['pending_trades'] = df['pending_trades']. fillna(0)In [ ]:
df.loc[df['trade_type'].isin(['BuyLimit02', 'BuyLimit03'])]In [ ]:
row = df.loc[df['trade_type'] == 'BuyLimit02']
df.loc[df['trade_type'] == 'BuyLimit02', 'pending_trades'] = 1In [ ]:
dfIn [ ]:
tp_price = 30
pos = mt.positions_get()
pos[0].profit
pos[0].price_open + tp_price3366.26
In [48]:
pos = mt.positions_get()
for i in pos:
print(i)
pos_comment = i.comment
pos_profit = i.profit
print(pos_comment, pos_profit)
df.loc[df['trade_type'] == i.comment, 'open_trades'] = 1
df['open_trades'] = df['open_trades']. fillna(0)TradePosition(ticket=238422628, time=1745410731, time_msc=1745410731060, time_update=1745410731, time_update_msc=1745410731060, type=0, magic=0, identifier=238422628, reason=3, volume=0.1, price_open=3336.26, sl=3190.17, tp=3482.35, price_current=3303.56, swap=-18.48, profit=-327.0, symbol='XAUUSD', comment='BuyStop04', external_id='') BuyStop04 -327.0 TradePosition(ticket=239454126, time=1745465427, time_msc=1745465427533, time_update=1745465427, time_update_msc=1745465427533, type=0, magic=0, identifier=239454126, reason=3, volume=0.1, price_open=3336.7, sl=3169.39, tp=3504.01, price_current=3303.56, swap=-4.62, profit=-331.4, symbol='XAUUSD', comment='BuyStop03', external_id='') BuyStop03 -331.4 TradePosition(ticket=239454136, time=1745456864, time_msc=1745456864432, time_update=1745456864, time_update_msc=1745456864432, type=0, magic=0, identifier=239454136, reason=3, volume=0.1, price_open=3311.53, sl=3144.22, tp=3478.84, price_current=3303.56, swap=-4.62, profit=-79.7, symbol='XAUUSD', comment='BuyStop04', external_id='') BuyStop04 -79.7
In [49]:
dfOut [49]:
| trade_type | trade_price | trade_volume | trade_sl | trade_tp | open_trades | |
|---|---|---|---|---|---|---|
| 0 | BuyStop01 | 3450.07 | 0.1 | 3331.67 | 3568.47 | 0.0 |
| 1 | BuyStop02 | 3419.19 | 0.1 | 3300.79 | 3537.59 | 0.0 |
| 2 | BuyStop03 | 3355.93 | 0.1 | 3237.53 | 3474.33 | 1.0 |
| 3 | BuyLimit04 | 3333.72 | 0.4 | 3215.32 | 3452.12 | 0.0 |
In [ ]:
open_trades = df.loc[df['open_trades'] == 1]
open_tradesIn [ ]:
for index, row in open_trades.iterrows():
print(row.trade_type)In [ ]: