Code Monkey home page Code Monkey logo

Comments (5)

AminHP avatar AminHP commented on May 30, 2024 1

Here is a simple code for SL and TP:

sim = MtSimulator(balance=10000)
sim.load_symbols('...')
sim_index = sim.symbols_data['EURUSD'].index
minute_count = 20 * 24 * 60
end_offset = 3 * 24 * 60
start_index = 10 * 12 * (20 * 24 * 60)
sim.current_time = sim_index[start_index]

tp_threshold = 0.0030
sl_threshold = 0.0030
order_volume = 0.01

for i in range(minute_count + end_offset):
    for order in sim.orders.copy():
        current_price = sim.price_at('EURUSD', sim.current_time)
        close_order = False
        # print(order.id, order.type, order.entry_price, current_price['High'], current_price['Low'])

        if order.type == OrderType.Buy:
            if current_price['Low'] <= order.entry_price - sl_threshold:  # SL
                close_order = True
                # print(order.id, 'buy SL')
            elif current_price['High'] >= order.entry_price + tp_threshold:  # TP
                close_order = True
                # print(order.id, 'buy TP')

        if order.type == OrderType.Sell:
            if current_price['High'] >= order.entry_price + sl_threshold:  # SL
                close_order = True
                # print(order.id, 'sell SL')
            elif current_price['Low'] <= order.entry_price - tp_threshold:  # TP
                close_order = True
                # print(order.id, 'sell TP')

        if close_order:
            sim.close_order(order)

    if i <= minute_count:
        if np.random.uniform() < 0.05:
            sim.create_order(order_type=OrderType.Buy, symbol='EURUSD', volume=order_volume, fee=0.0003)

        if np.random.uniform() < 0.05:
            sim.create_order(order_type=OrderType.Sell, symbol='EURUSD', volume=order_volume, fee=0.0003)

    sim.tick(sim_index[start_index + i + 1] - sim_index[start_index + i])

Note that you can create/close orders using the env.step method instead of sim.create_order and sim.close_order in case you need the visualization and other features.

from gym-mtsim.

AminHP avatar AminHP commented on May 30, 2024

Hi @philreyp, I actually was thinking about adding this feature to the simulator a few months ago. But I haven't had enough time to make it yet. In the next few days, I will try to give you a quick implementation that brings some good intuitions.

from gym-mtsim.

philreyp avatar philreyp commented on May 30, 2024

Thanks @AminHP much appreciated.

from gym-mtsim.

philreyp avatar philreyp commented on May 30, 2024

Thank you very much @AminHP I'd like to clarify when we use sim.close_order(order), the reward is calculated based on close price of that tick and not the SL/TP?

from gym-mtsim.

AminHP avatar AminHP commented on May 30, 2024

That's right. It is calculated based on the close price. If you want to be more precise, I suggest you use minutely data, but change the tick value to one day (for daily trading).

from gym-mtsim.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.