I downloaded Delphi and am now able to create basic strategies but I am running into the following challenge when attempting to implement a money management system in the strategy.
How do I control lot size of a future trade based on the previous trade's result? Say if it wins I would like to increase lot size for the next order or vise versa? Can this be done?
Thanks!
Is Position Sizing based on last trade result possible?
-
- Posts: 3
- Joined: Fri Aug 14, 2009 12:21 pm
-
- Posts: 24
- Joined: Thu Jul 23, 2009 7:20 am
- Location: Brighton, UK
In your ForexTester installation, there should be a folder Examples, inside which is a folder Strategies, and inside that is a folder Delpi holding a project SMAStrategy.dpr.
In that project, the variables declared in lines 10 to 20 keep their values from one tick to the next, so you could declare a similar variable in your project and use it to hold "profit from the last trade", and use it to change the value of the Lotsize parameter when you create a new order, during a single session. You'd set LastProfit := 0 in either ResetStrategy (if you wanted to apply your rules within a single test run) or InitStrategy (to apply the rules over a single ForexTester session).
If you want to carry the value across from one ForexTester session to another, you'd need to write this to a disk file, and read it back from the file into the new ForexTester session in InitStrategy (obeyed once per session) or ResetStrategy (obeyed once per test run).
If you are closing your trades yourself, you'd calculate the profit from that trade yourself at the closure, and save it in your LastProfit variable. If you're relying on ForexTester to close the trade based on a stop loss, you'd need to put code into the GetSingleTick procedure that
1. checked if the order still existed
2. if it does, set LastProfit to the current profit of that order
For a single order at a time, something like
Note that orders are numbered from 0, not 1, so if there is only one current order it is number 0.
If you have more than one order active concurrently, it would become a bit more complex, as when an order closed you'd need to identify which one it was, and add its profit to a ProfitFromAllTrades variable?
I think? At least it may be a place to start defining the question.
Phil Bell
In that project, the variables declared in lines 10 to 20 keep their values from one tick to the next, so you could declare a similar variable in your project and use it to hold "profit from the last trade", and use it to change the value of the Lotsize parameter when you create a new order, during a single session. You'd set LastProfit := 0 in either ResetStrategy (if you wanted to apply your rules within a single test run) or InitStrategy (to apply the rules over a single ForexTester session).
If you want to carry the value across from one ForexTester session to another, you'd need to write this to a disk file, and read it back from the file into the new ForexTester session in InitStrategy (obeyed once per session) or ResetStrategy (obeyed once per test run).
If you are closing your trades yourself, you'd calculate the profit from that trade yourself at the closure, and save it in your LastProfit variable. If you're relying on ForexTester to close the trade based on a stop loss, you'd need to put code into the GetSingleTick procedure that
1. checked if the order still existed
2. if it does, set LastProfit to the current profit of that order
For a single order at a time, something like
Code: Select all
if OrderCount = 1 then
if OrderSelect( [b]0[/b], SELECT_BY_POS, MODE_TRADES ) then
LastProfit := OrderProfitPips; // or := OrderProfit for $
Note that orders are numbered from 0, not 1, so if there is only one current order it is number 0.
If you have more than one order active concurrently, it would become a bit more complex, as when an order closed you'd need to identify which one it was, and add its profit to a ProfitFromAllTrades variable?
I think? At least it may be a place to start defining the question.
Phil Bell
Who is online
Users browsing this forum: No registered users and 4 guests