| View previous topic :: View next topic |
| Author |
Message |
matrox
Joined: 14 Aug 2009 Posts: 3
|
Posted: Mon Aug 17, 2009 2:12 pm Post subject: Is Position Sizing based on last trade result possible? |
|
|
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! |
|
| Back to top |
|
 |
philBell
Joined: 23 Jul 2009 Posts: 25 Location: Brighton, UK
|
Posted: Tue Aug 18, 2009 8:44 am Post subject: |
|
|
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
| Code: | 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 |
|
| Back to top |
|
 |
matrox
Joined: 14 Aug 2009 Posts: 3
|
Posted: Wed Aug 19, 2009 12:19 pm Post subject: |
|
|
| Thank you! That worked perfectly. I have a good MQL4 programming background but delphi is new to me. Your answer just took care of a lot of my questions. |
|
| Back to top |
|
 |
philBell
Joined: 23 Jul 2009 Posts: 25 Location: Brighton, UK
|
Posted: Wed Aug 19, 2009 5:20 pm Post subject: |
|
|
Pleased to help; there aren't many books about Delphi, and its a pig to try and learn it from the "Help" text.
Phil Bell |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You can attach files in this forum You can download files in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|