Is Position Sizing based on last trade result possible?

How to create strategies and indicators
Message
Author
matrox
Posts: 3
Joined: Fri Aug 14, 2009 12:21 pm

Is Position Sizing based on last trade result possible?

#1 Postby matrox » Mon Aug 17, 2009 9:12 am

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!

philBell
Posts: 24
Joined: Thu Jul 23, 2009 7:20 am
Location: Brighton, UK

#2 Postby philBell » Tue Aug 18, 2009 3:44 am

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: 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

matrox
Posts: 3
Joined: Fri Aug 14, 2009 12:21 pm

#3 Postby matrox » Wed Aug 19, 2009 7:19 am

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.

philBell
Posts: 24
Joined: Thu Jul 23, 2009 7:20 am
Location: Brighton, UK

#4 Postby philBell » Wed Aug 19, 2009 12:20 pm

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


Return to “FT API”

Who is online

Users browsing this forum: No registered users and 4 guests