Answers.Body: <blockquote>
  <p>how should the reward scheme be for a game like this? i.e., whether one action is good or not depends on other actions taken before it?</p>
  </blockquote>
  <p>The reward scheme should always be a &quot;natural&quot; representation of the goals of the agent. If the goal of your agent is to make a profit, then the reward should be the amount of profit*.</p>
  <p>It is important to RL, and becomes critical for rewards that may be delayed, that the state representation captures all the information that impacts those rewards. That means representing the direct effects of actions, so that they can be tracked. In the case of a trading bot, it will be important at a minimum that the state includes the current portfolio. If the agent can access data about the amount and value of stock it currently holds, it has a chance to predict likely profits later.</p>
  <blockquote>
  <p>can we set a scheme like this: set the reward to be zero at each step, no matter it is a <code>buy</code> or a <code>sell</code>. Only give the reward at the end of the game, say after 1000 steps, and the reward, in this case, is the total money made?</p>
  </blockquote>
  <p>You can, but that looks like it is making the problem harder than it needs to be.</p>
  <p>I would naturally set the reward to be the profit on each transaction (including negative reward for buying stock). The agent should be able to use that to figure out long term rewards, and will learn faster if it is given this more direct feedback. RL algorithms are well-suited to figuring out the need to invest at an earlier time in order to make profit later. If there is also a time horizon, that should be factored into the state - the agent should be made aware that the game will end in so many time steps otherwise it may end up holding stock instead of selling it in time to meet the final evaluation.</p>
  <p>One important caveat is that real trading problems are hard, because the real environment is highly variable and competitive. The competition includes intelligent people with advanced qualifications in statistics. The chances of making a trading bot that is successful in a real trading environment are low.</p>
  <hr />
  <p>* Sometimes this &quot;natural&quot; rerpresentation is too sparse, and you may want to look into reward shaping. That is adding interim rewards or spreading out an existing reward, to assist the learning agent. For a stock trading game for instance, it may help to allocate some reward for gaining ownership of a stock, perhaps the nominal resale value minus the fees for selling it.</p>
  <p>However, where it is possible to stick with the simplest direct interpretation of the agent's goals, then you should do that. Reward shaping comes with the potential risk that the changes you have made will impact what it means for the agent to perform optimally.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-10-29T07:40:46.027Z
Answers.LastActivityDate: 2021-10-31T09:49:00.547Z
Answers.LastEditDate: 2021-10-31T09:49:00.547Z
Answers.UserDisplayName: Neil Slater
Body: <h3>Problem setting</h3>
  <p>Consider a game like trading a stock</p>
  <ul>
  <li>At each step, the agent can <code>buy</code> / <code>sell</code> a stock.</li>
  <li>Trade is a pair of <code>buy</code> and <code>sell</code> actions.</li>
  <li>We can set the reward for the <code>sell</code> action as the profit made in this trade. Although this might be misleading, since whether a trade is good or not depends on both <code>buying</code> and <code>selling</code> timing.</li>
  <li>We don't know the reward for <code>buy</code>, whether a <code>buy</code> is good or not depends on when you <code>sell</code>.</li>
  </ul>
  <h3>Questions</h3>
  <ol>
  <li>How should the reward scheme be for a game like this? i.e., whether one action is good or not depends on other actions taken before it?</li>
  <li>Can we set a scheme like this: set the reward to be zero at each step, no matter it is a <code>buy</code> or a <code>sell</code>. Only give the reward at the end of the game, say after 1000 steps, and the reward, in this case, is the total money made?</li>
  </ol>
  
Tags: <reinforcement-learning><game-ai><reward-functions><reward-design><algorithmic-trading>
Title: How should I define the reward function for a stock trading-like game?
UserDisplayName: em1971
ThreadText: # How should I define the reward function for a stock trading-like game?
  
  (Asked by: username_0 on 2021-10-29)
  
  ### Problem setting
  
  Consider a game like trading a stock
  
  * At each step, the agent can `buy` / `sell` a stock.
  * Trade is a pair of `buy` and `sell` actions.
  * We can set the reward for the `sell` action as the profit made in this trade. Although this might be misleading, since whether a trade is good or not depends on both `buying` and `selling` timing.
  * We don't know the reward for `buy`, whether a `buy` is good or not depends on when you `sell`.
  
  ### Questions
  
  1. How should the reward scheme be for a game like this? i.e., whether one action is good or not depends on other actions taken before it?
  2. Can we set a scheme like this: set the reward to be zero at each step, no matter it is a `buy` or a `sell`. Only give the reward at the end of the game, say after 1000 steps, and the reward, in this case, is the total money made?
  
  **Tags:** reinforcement-learning, game-ai, reward-functions, reward-design, algorithmic-trading
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-10-29. Score: 1
  
  > how should the reward scheme be for a game like this? i.e., whether one action is good or not depends on other actions taken before it?
  
  The reward scheme should always be a "natural" representation of the goals of the agent. If the goal of your agent is to make a profit, then the reward should be the amount of profit\*.
  
  It is important to RL, and becomes critical for rewards that may be delayed, that the state representation captures all the information that impacts those rewards. That means representing the direct effects of actions, so that they can be tracked. In the case of a trading bot, it will be important at a minimum that the state includes the current portfolio. If the agent can access data about the amount and value of stock it currently holds, it has a chance to predict likely profits later.
  
  > can we set a scheme like this: set the reward to be zero at each step, no matter it is a `buy` or a `sell`. Only give the reward at the end of the game, say after 1000 steps, and the reward, in this case, is the total money made?
  
  You can, but that looks like it is making the problem harder than it needs to be.
  
  I would naturally set the reward to be the profit on each transaction (including negative reward for buying stock). The agent should be able to use that to figure out long term rewards, and will learn faster if it is given this more direct feedback. RL algorithms are well-suited to figuring out the need to invest at an earlier time in order to make profit later. If there is also a time horizon, that should be factored into the state - the agent should be made aware that the game will end in so many time steps otherwise it may end up holding stock instead of selling it in time to meet the final evaluation.
  
  One important caveat is that real trading problems are hard, because the real environment is highly variable and competitive. The competition includes intelligent people with advanced qualifications in statistics. The chances of making a trading bot that is successful in a real trading environment are low.
  
  ---
  
  \* Sometimes this "natural" rerpresentation is too sparse, and you may want to look into reward shaping. That is adding interim rewards or spreading out an existing reward, to assist the learning agent. For a stock trading game for instance, it may help to allocate some reward for gaining ownership of a stock, perhaps the nominal resale value minus the fees for selling it.
  
  However, where it is possible to stick with the simplest direct interpretation of the agent's goals, then you should do that. Reward shaping comes with the potential risk that the changes you have made will impact what it means for the agent to perform optimally.
  
  
  ---
  
  