Comments (5)
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.
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.
Thanks @AminHP much appreciated.
from gym-mtsim.
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.
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)
- how can I pass through the observation_space as Dict into Keras DQN model HOT 3
- Symbol Data Download HOT 2
- A Complete Example using stable-baselines HOT 5
- Run on MT5 HOT 1
- Possible to train the bot on 1H or 4H instead of D1? HOT 4
- How can I add technical indicators ? HOT 1
- example: environment with custom parameters HOT 3
- is it possible to use this code to live trading via MT5? HOT 2
- pyfolio HOT 2
- Should use open price instead of close price HOT 2
- AssertionError: Continuous action space must have a finite lower and upper bound HOT 4
- Check if market is closed HOT 3
- Incompatibility with Linux systems HOT 2
- Feature Request: update from gym to gymnasium HOT 3
- Set lots(volume) HOT 3
- Fee is not the same as spread HOT 1
- The new models and the issues caused by signal_features. HOT 1
- Error running using the environment HOT 4
- A question about calculate balance HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gym-mtsim.