New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question: Is there an 'easy' way to set Order properties? #4
Comments
env is equivalent to:
Using the following right after above
sets the values (verified in code right after) but doesn't actually change the order volume (the volume is all over the place). Can you expand on '... might work in some cases...'? Why some cases and not others? |
For example, this code works. But inside an A2C model, you can't apply it. sim = MtEnv(...)
env.simulator.symbols_info[symbol1].volume_max = a1
env.simulator.symbols_info[symbol2].volume_max = a2
env.step(...)
env.simulator.symbols_info[symbol1].volume_max = b1
env.simulator.symbols_info[symbol2].volume_max = b2
env.step(...)
env.simulator.symbols_info[symbol1].volume_max = c1
env.simulator.symbols_info[symbol2].volume_max = c2
env.step(...) |
I am using AC2. The _get_modified_volume() in MTEnv class limits the volume to between volume_max and volume_min before the order is sent to MtSimulator for execution. Can you please explain your statement above "...But inside an A2C model, you can't apply it. ..."? Also, why would the model being used restrict the use of the volume_ values? Thanks |
About the statement "...But inside an A2C ...": I mean this code works because you can change the About the "why would the model ..." question: This is something related to the MetaTrader. They restrict these values and we cannot set any volume we want. In case you don't want these restrictions to be applied, just remove or modify the class MyMtEnv(MtEnv):
def _check_volume(self, symbol: str, volume: float) -> None:
pass
def _get_modified_volume(self, symbol: str, volume: float) -> float:
si = self.simulator.symbols_info[symbol]
v = abs(volume)
return v |
If it is not possible to limit the volume (lot size in forex), how else can the risk be mitigated within this framework? |
Shouldn't the RL model itself learn to manage the risk? Since stable-baselines does not support such a thing, a simple way to do it is to call a function at the beginning of the step method: class MyMtEnv(MtEnv):
def step(self, action: np.ndarray) -> Tuple[Dict[str, np.ndarray], float, bool, Dict[str, Any]]:
action = self._modify_action(action)
return super().step(action)
def _modify_action(self, action: np.ndarray) -> np.ndarray:
k = self.symbol_max_orders + 2
for i, symbol in enumerate(self.trading_symbols):
symbol_action = action[k*i:k*(i+1)]
volume = symbol_action[-1]
if self._current_tick > 20: # or some other conditions according to your risk management algorithm
volume = np.clip(volume, -1.2, 1.2)
symbol_action[-1] = volume
return action |
I will give the code a try. Thanks. You bring up a good point w.r.t. the RL model learning to manage the risk (that's what a proper reward is supposed to control, right??). However, I trade with a proprietary firm that restricts certain aspects of my account during a trading session (for example, if my balance goes more than x% below it's daily starting amount, all my trades will be closed automatically). This means that I have to watch my intra-day drawdown. One of the ways I control this is through a restriction of the volume/lot-size. This is why the control I've asked about in this thread is critical for me. |
Yes, an ideal RL agent should be able to control everything, but it is difficult to make such an agent. By the way, I like your attitude. Managing some stuff besides the RL model is an excellent way to combine the knowledge of a human expert and an AI agent. It helps to achieve better results in less time. Let me know if my last piece of code was proper for your requirement. |
I will try and debug asap but maybe you can quickly see the problem:
I get errors below when:
is run.
|
It seems there is a problem with pathos and multiprocessing. Please try |
FYI: results in
|
I updated this code |
The new code works (thanks) but it does not appear to accomplish the original goal. The volume is not affected by its inclusion.
The volume should be fixed at 0.01. |
My code had a problem and I fixed it. Try the new code and send me your complete code if the problem still exists. Make sure you are using |
You caught my mistake ('Make sure you are using MyMtEnv not MtEnv'). I wasn't actually testing your updated code. New code appears work for restricting volume!! ... but only if
When set to 1, 4 get:
|
As I said earlier, it seems to be a problem with class MyCustomEnv(MtEnv):
def __init__(self, *args, **kwargs):
multiprocessing_processes = kwargs.pop('multiprocessing_processes', None)
super().__init__(*args, **kwargs)
self.multiprocessing_pool = Pool(multiprocessing_processes) if multiprocessing_processes else None
def step(self, action: np.ndarray):
action = self._modify_action(action)
return super().step(action)
def _modify_action(self, action: np.ndarray) -> np.ndarray:
k = self.symbol_max_orders + 2
for i, symbol in enumerate(self.trading_symbols):
symbol_action = action[k*i:k*(i+1)]
volume = symbol_action[-1]
volume = np.clip(volume, -0.01, 0.01)
symbol_action[-1] = volume
return action |
Specifically, how do I set 'volume', 'volume_step', 'volume_min' and 'volume_max' without creating a child of the Order class? I need to be able to set these values potentially for each order.
The text was updated successfully, but these errors were encountered: