Skip to content

Commit

Permalink
Added an individual lot size control for every instrument
Browse files Browse the repository at this point in the history
  • Loading branch information
ChallengerIL committed Sep 26, 2023
1 parent 668d4a3 commit 2529d56
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 52 deletions.
51 changes: 25 additions & 26 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
STRATEGIES = {
"example_strategy": {
"tfs": ["M1", "M5", "M15", "M30", "H1", "H4", "D1"],
"pairs": ['GBPUSD'],
"pairs": ['GBPUSD', 'EURUSD', 'AUDUSD', 'USDJPY', 'USDCAD', 'USDCHF'],
"indicators": {
"adx": {
"H1": {
"M15": {
"p": [14],
"plotting": {
"color": ["white"],
Expand Down Expand Up @@ -221,29 +221,28 @@
# },
# },
# },
# "hma": {
# "M15": {
# "p": [12, 80],
# "slope": [1, 1],
# "plotting": {
# "color": ["green", "red"],
# },
# },
# "H1": {
# "p": [12],
# "slope": [1],
# "plotting": {
# "color": ["yellow"],
# },
# },
# "H1": {
# "p": [12, 80],
# "slope": [1, 1],
# "plotting": {
# "color": ["orange", "pink"],
# },
# },
# },
"hma": {
"M15": {
"p": [80],
"slope": [1],
"plotting": {
"color": ["green", "red"],
},
},
"M1": {
"p": [12],
"plotting": {
"color": ["yellow"],
},
},
# "H1": {
# "p": [12, 80],
# "slope": [1, 1],
# "plotting": {
# "color": ["orange", "pink"],
# },
# },
},
# "tma": {
# "M1": {
# "p": [20],
Expand Down Expand Up @@ -280,7 +279,7 @@
"update_time": time(0, 0, 0, tzinfo=TIMEZONE),
"update_period": 25,
"plotting_rows": 2,
"training_period": 200,
"training_period": 1000,
},
}

Expand Down
2 changes: 1 addition & 1 deletion df_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,4 @@ def __init__(self, pair, strategy, start, end, **kw):
with h5py.File(f"{FILES_DIR}pairs_data.h5", 'a') as file:
[file.create_dataset(f"{tf}_{pair}", data=self.df[tf], dtype=self.df[tf].dtype) for tf in self.df]

# shutdown()
shutdown()
1 change: 1 addition & 0 deletions indicators.py
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,7 @@ def match_indexes(goal_index, source_index, arr, shift_to_match=True, arr_type=n

result = numpy_fill(idx_arr)
result = result.astype(arr_type)

if shift_to_match:
result = shift(result, step)

Expand Down
52 changes: 30 additions & 22 deletions test_trader.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def __init__(self, pair, strategy, verbose=False, cushion=2, **kw):
else:
self.multiplier = 0.0001

self.tp = 40
self.tp = 30
self.sl = 30
self.tp_multiplier = 1.5
self.slope_level = 0.0002
Expand All @@ -69,6 +69,7 @@ def __init__(self, pair, strategy, verbose=False, cushion=2, **kw):

self.indicators.append(indi)

self.lot_size = 0.01
self.take_profit = 1
self.active_longs = []
self.active_shorts = []
Expand Down Expand Up @@ -96,6 +97,9 @@ def __init__(self, pair, strategy, verbose=False, cushion=2, **kw):
self.closed_short_loss[:] = np.nan
self.closed_long_idx = []
self.closed_short_idx = []
self.win_series = 0
self.fast_growth = [3, 4, 5, 7, 9, 11, 14, 19, 24, 32, 41, 54, 70, 91, 118, 154, 200, 260, 337, 439, 570,
741, 964, 1253, 1628, 2117, 2752, 3578, 4651, 6046]
self.pips = 0
self.profit_trades = 0
self.loss_trades = 0
Expand Down Expand Up @@ -149,11 +153,11 @@ def tester(self, account, index):

# direction = random.randint(-1, 1)
# if direction == 1:
# self.open_long(index, account.lot_size)
# self.open_long(index)
# else:
# self.open_short(index, account.lot_size)
# self.open_short(index)

def open_long(self, index, lot_size, open_price=None, custom_tp=None, custom_sl=None, sl_multiplied_tp=None,
def open_long(self, index, open_price=None, custom_tp=None, custom_sl=None, sl_multiplied_tp=None,
previous_tp=False, previous_sl=False):
if not open_price:
open_price = self.open[index]
Expand Down Expand Up @@ -183,18 +187,18 @@ def open_long(self, index, lot_size, open_price=None, custom_tp=None, custom_sl=
self.long_tps.append(tp)
self.long_sls.append(sl)
self.active_longs.append(price)
self.long_lot_sizes.append(lot_size)
self.long_lot_sizes.append(self.lot_size)
self.went_long_price[index] = price

if self.verbose:
print(f"Going Long on {self.name}, {lot_size}")
print(f"Going Long on {self.name}, {self.lot_size}")
print(self.index[index])
print(f"Take Profit: {tp}")
print(f"Stop Loss: {sl}")
print("Active Longs: " + str(self.active_longs))
print("\n")

def open_short(self, index, lot_size, open_price=None, custom_tp=None, custom_sl=None, sl_multiplied_tp=None,
def open_short(self, index, open_price=None, custom_tp=None, custom_sl=None, sl_multiplied_tp=None,
previous_tp=False, previous_sl=False):
if not open_price:
open_price = self.open[index]
Expand Down Expand Up @@ -222,11 +226,11 @@ def open_short(self, index, lot_size, open_price=None, custom_tp=None, custom_sl
self.active_shorts.append(open_price)
self.short_tps.append(tp)
self.short_sls.append(sl)
self.short_lot_sizes.append(lot_size)
self.short_lot_sizes.append(self.lot_size)
self.went_short_price[index] = open_price

if self.verbose:
print(f"Going Short on {self.name}, {lot_size}")
print(f"Going Short on {self.name}, {self.lot_size}")
print(self.index[index])
print(f"Take Profit: {tp}")
print(f"Stop Loss: {sl}")
Expand All @@ -238,9 +242,13 @@ def check_exit(self, account, index, close_long=False, close_short=False, martin

# Add Break Even mode with a fixed tp of ~ 1 pip after price moved away 20+ pips

if not martin or not fast_growth:
if self.lot_size < account.lot_size:
self.lot_size = account.lot_size

if fast_growth:
self.adjust_lot_size = False
account.lot_size = account.fast_growth[account.win_series] / 100
self.lot_size = self.fast_growth[self.win_series] / 100

if len(self.active_longs) > 0:
for i, value in enumerate(self.active_longs):
Expand Down Expand Up @@ -284,12 +292,12 @@ def check_exit(self, account, index, close_long=False, close_short=False, martin
account.profit_trades += 1
self.closed_long_profit[index] = current_price
if martin:
account.lot_size = account.lot_size_start
self.lot_size = account.lot_size
self.direction_lost = 0
self.direction_won = 1
if fast_growth:
if account.win_series < len(account.fast_growth) - 1:
account.win_series += 1
if self.win_series < len(self.fast_growth) - 1:
self.win_series += 1
else:
if self.verbose:
print(f"Closing {self.long_lot_sizes[i]} {self.name} Long in loss")
Expand All @@ -305,12 +313,12 @@ def check_exit(self, account, index, close_long=False, close_short=False, martin
account.loss_trades += 1
self.closed_long_loss[index] = current_price
if martin:
account.lot_size *= 2
self.lot_size *= 2
self.direction_lost = 1
self.direction_won = 0
if fast_growth:
if account.win_series > 0:
account.win_series -= 1
if self.win_series > 0:
self.win_series -= 1

account.balance += result
self.pips += pips
Expand Down Expand Up @@ -380,12 +388,12 @@ def check_exit(self, account, index, close_long=False, close_short=False, martin
account.profit_trades += 1
self.closed_short_profit[index] = current_price
if martin:
account.lot_size = account.lot_size_start
self.lot_size = account.lot_size
self.direction_lost = 0
self.direction_won = -1
if fast_growth:
if account.win_series < len(account.fast_growth) - 1:
account.win_series += 1
if self.win_series < len(self.fast_growth) - 1:
self.win_series += 1
else:
if self.verbose:
print(f"Closing {self.short_lot_sizes[i]} {self.name} Short in loss")
Expand All @@ -401,12 +409,12 @@ def check_exit(self, account, index, close_long=False, close_short=False, martin
account.loss_trades += 1
self.closed_short_loss[index] = current_price
if martin:
account.lot_size *= 2
self.lot_size *= 2
self.direction_lost = -1
self.direction_won = 0
if fast_growth:
if account.win_series > 0:
account.win_series -= 1
if self.win_series > 0:
self.win_series -= 1

account.balance += result
self.pips += pips
Expand Down
3 changes: 0 additions & 3 deletions tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ def __init__(self, pairs_num, bars_len):
self.lot_size = self.lot_size_start
self.max_lot_size = 50
self.currency_converter = 10
self.win_series = 0
self.fast_growth = [3, 4, 5, 7, 9, 11, 14, 19, 24, 32, 41, 54, 70, 91, 118, 154, 200, 260, 337, 439, 570,
741, 964, 1253, 1628, 2117, 2752, 3578, 4651, 6046]
self.profits = []
self.losses = []
self.profit_pips = []
Expand Down

0 comments on commit 2529d56

Please sign in to comment.