Margin math

Discussion in 'Forex' started by Rocko Bonaparte, Sep 30, 2012.

  1. funnyguy, I see you did a lot there. I got the email notice and just kind of assumed somebody just did a one-liner in the thread and that would be it for the night. You kind of caught me by surprise! It's pretty late here so I can't verify everything this evening but I'll be working on it here and there over the next few days. Assuming I pick it all up, then I'll have to incorporate into my backtester code.

    Edit: And just to keep things on track, I'm asking a lot of this without factoring in a margin call whipping my ass in the middle of holding everything. Ummm that's next on my list to get right. ;)
     
    #11     Oct 8, 2012
  2. funnyguy_2nd, I assume one of the major things I have wrong is assuming the base currency is USD, not EUR. That should explain why I am fixated on 99 lots instead of 82 lots. I wanted to understand this first.

    When I was casually browsing around with how currencies generally work, the first term in the pair is considered the base currency. So for EURUSD the base currency would be the euro. So when buying one lot I would be thinking in terms of 100 euros instead of 100 dollars. But then I read that for some of the pairs, this is different, and one of those pairs was EURUSD. So for EURUSD the dollar is the base currency. Is that totally wrong?

    So my 99 comes from the idea figuring out how many $100 units I could get, assuming at a minimum one penny's worth of commission or more without going into those exact details. It would be 100 lots if there weren't any, so I went with 99. I was assuming that if USD was the base currency, my contracts would be based on how many $100 blocks I could get. But it looks like instead it's based on how many blocks of euros I can get. I can see that as (100/1.2181).
     
    #12     Oct 8, 2012
  3. If I understand that right, then is there an asymmetry in the number of contracts I can open between going long USDEUR and going short EURUSD?
     
    #13     Oct 9, 2012
  4. Can I... can I... have the AFL? :D
     
    #14     Oct 9, 2012
  5. Throwing in the risk-adjusted position size added some numbers to the whole thing that messed me up. I was following most of it from the position size, but that number was smashing into the room like Kool-Aid Man until I figured out that was related to your stop loss and portfolio risk criteria. But I'll go doodle this all out on some paper and see if I'm following at all.

    Is that AFL something you came up with? In one of the screen caps, the name comes up, but I can't find that anywhere. So I assume that's some proprietary thing.
     
    #15     Oct 10, 2012
  6. so if I understand that right if my base currency is in USD, the conversion factor in effect nullifies the entry price, and it becomes risk / absolute stop.

    I at least can reproduce your numbers now. Something I'm trying to wrestle with is why another formula I found didn't appear to work. Or rather, the order of magnitudes made no sense. I assume it was talking about different lot sizes (standard, mini, whatever).

    http://www.fxstreet.com/education/learning-center/unit-3/chapter-3/the-position-size/

    This site has arranged the formulas differently but I managed to reproduce your results with it:

    http://www.babypips.com/school/calculating-position-sizes.html

    Gotta love the Internet. We'll based of that little exercise I can start to rethink my contract sizes. If I have it correctly, in the code I have run, my contract sizes will come up differently, but the profits and losses should come up as the same.
     
    #16     Oct 10, 2012
  7. It's true the numbers were a little bit different and I wondered about that, but I was dismissing it as floating point errors and things like that. In my coding it's a lot more obvious with currencies since the numbers tend to go to larger extremes more quickly. I had to use a big decimal type to keep things accurate; I'd be off by a nontrivial amount of money just in one transaction between the computer and my hand calculation. I would have been more concerned if I was orders of magnitude wrong, or nothing at all was similar.

    Now going back to finding out how many contracts I can get, I presume to use a formula like this:

    contracts = account size * conversion factor / unit size

    So back to my numbers way up top:
    contracts = 100000 * 1/1.21800 / 1 = 82101 units

    One question: How is leverage then factored into the contracts? If I could, say, get one contract per unit, with 100:1 leverage, does that mean it's 82,101 units of 100:1 leveraged currency? Or would the contract size be multiplied by the amount of leverage?
     
    #17     Oct 11, 2012
  8. I had started to doodle all over my last post to add in a concrete example, and it went past 30 minutes.

    Edit: I'll pencil some more stuff in here based on some more reading and make it a little more concrete. So I'll just post again!

    Let's use micro contracts, and 100:1 leverage. Again 100:1 is just for the cleanliness of the math, not because I actually plan to use such dramatic leverage. Blablabla. I have $100,000 of fictitious USD to get margin called on this baby. EURUSD is 1.21800.

    It sounds like leverage reduces the cost of an individual contract, which implies the use of leverage means being able to acquire more contracts, if one were so inclined.

    So I have:
    unleveraged contracts = $100,000 * 1/1.21800 / 1000 = 82.1018. I see a lot of places cut that down to two decimal places so it would be 82.10 for starters if I just left it like this.

    So leveraged contracts = 100 * unleveraged contracts = 8210.18.

    How am I doing?

    One reason I'm taking this approach is to write some guard code to check if position sizes I'm entering make sense in the backtest. This should trim back anything superfluous.
     
    #18     Oct 11, 2012
  9. So I think I see how this sets my other math of fire now. So with 100:1 margin, if I had $10,000 to blow on some xxxUSD that's 1.01, I could get 9899.99 micro lots. I think. If it went up to 1.02, the profit would be: 9899.99 * (1.02 - 1.01) * 1000 units in micro = $98999.90. Does that sound right?
     
    #19     Oct 12, 2012
  10. Hmm okay I see it in my own formula above for unleveraged contracts. I had created a formula for the price per lot that looked like this:

    price per lot = unit size / (conversion * margin deposit)

    when I guess it's

    price per lot = unit size / (conversion * leverage ratio)

    Then taking $10,000 and dividing by that.
     
    #20     Oct 12, 2012