Help with coading for spread

Forums ProRealTime English forum ProOrder support Help with coading for spread

Viewing 15 posts - 1 through 15 (of 15 total)
  • #243598

    I’m trying to write code to account for varying size in spread based upon the size of an older placed on IG. My system was placing lots of orders and when I added the code to account for variable spread it stopped placing orders. Could anyone look at the code and make a suggestion as to why it will no longer place any orders.

    this is the initial code that sets the betsize (BS) for the trades based on how much money the account has. it was working find until I added the code to deduct spread . Any suggestions where it is going wrong? it seems to not longer be producing the value BS so the system cannot place a trade.

    Original working code below that made the BS value and would place orders.

     

    #243610

    I’m trying to understand what this is doing. To begin with could you repost your “original code that worked” and add some comments explaining what the variables are. The var BSP hasn’t been defined.

    1 user thanked author for this post.
    #243614

    The purpose of the code was to determine the size of bet possible from the available funds (capital+ strategyprofit)

     

    Because the spread changes with bet size as does the margin I wanted to model this better in backtesting.

     

    The original code at the bottom of the post just modelled the increase in margin from 5% to 15%.

     

    The new code at the top was attempting to model the increasing spread on larger spreadbets, so it could be deducted from strategy profit after each trade closes a record of the true equity curve which can be displayed with Graph.

     

    However the code never places an order so it mustn’t be creating the BS (betsize) value.

     

    BSP is bet size proportion, a number between 0 and 1 that represents the amount of the equity you are willing to risk as margin in each bet.

    #243617

    When there’s an underlying  problem in code, it usually shows up as a symptom(s).

    Tracing backward from the symptom(s), can lead back to the underlying problem.

    Poetic Logic.

     

    If no order was taken, was there an order scenario to take!

    If yes, for an order not to be made, the order code may not have run!

    Usually the order statement fall within a codeblock like ‘IF condition’.

    The condition must have been false, not to activate the codeblock.

    The condition may be several variables, one or all could have been false.

    Finding and following back from the false variables, could lead to the underlying problem.

    That’s the easiest way, however not having the full code turns a 2minute job into 2hours.

     

    The hard way, systemically run through the code sequentially, and hope you spot something.

    You may find ‘A problem’ but there no way of testing if it is  ‘The Problem’ if you can’t run the code.

    This is the beginning of the ‘that’s not it!’ start cycle, reward for you efforts, HAHA!

     

    If no entry made,

    then line 2, Gain = 0

    and line 3, codeblock = false.

     

    Additionally, when barindex = 0, the first bar, ‘BS’ probably defaults to zero,

    making line 13 condition true and ‘spread’ = sp1 = 0.6

    However, line 29,  ‘Myspread = spread * BS’ …

    If BS = 0, then any number multiplied by zero equals zero,     0.6 * 0 = 0

    Line 34 is the first time ‘BS’ is assigned a value.

    So, a guess would be, is ‘MYspread’ being zero, affecting later code, if its used,  and is  ‘BS’ reset to zero later aswell.

     

    There could be another or simple solutions, but I’m not seeing it.

    Anyway, that’s my attempt.

    Regards

    1 user thanked author for this post.
    #243621

    With only this extract to work from, the only potential error I can see is at line 32. If BSP isn’t correctly initialised then the value of equity will collapse to zero and no orders are likely to be placed.

    Edit – Just noticed druby above has said the same thing, but in a lot more detail

    1 user thanked author for this post.
    #243623

    I’ve run the code graphing each variable and the code sets “myequity” to zero which is the problem. here is some fuller code to test for yourself with a market entry of open higher last last close to make it bet in a backtest.

     

    i anyone can figure out how to make it produce a value of “myequity” id be grateful

     

     

    #243629

    Have a look at this, it seems to be doing something, most of the numbers appear to add up.

    I noticed the value of BS, in the print window, doesn’t get high enough to trigger the bigger spread threshold conditions.

    Don’t know why there set at the thousand levels.

    The problems I had, to get this far, were mainly around some variables still being defined as undefined till they were assigned with a value.

    Maybe, also the sequence they appeared, especially at the start

    To get around this, I set some up in the barindex = 0 block, which will be the first bar of the preloadbars.

    One other thing I only tested with 1 trade a day, or at a time!

     

     

    1 user thanked author for this post.
    #243631

    Not sure that the spread is related to bet size…

    Spread varies with open/close hours, and volatility (which itself increase with announcements).

    https://www.ig.com/uk/help-and-support/spread-betting-and-cfds-5064504a/products-markets-and-trading-hours-008e7240/forex-spread-betting-product-details-3134d533

    “MyEquity = MyEquity + Gain – myspread”
    The best way should be to calculate accurate gain with the real enter and exit price, then you don’t need to add any spread in the equation…
    RealGain = RealExitPrice – RealEnterPrice (the spread is already included in the RealGain)

    Where did you see that spread varies with bet size ?

    1 user thanked author for this post.
    #243633

    Hi LucasBest, I was just trying to get the code working somehow, from I code point of view, didn’t think any further than that.

    After posting, I came across this, More accurate spread for back testing. : Forum ProOrder support – ProRealTime

    Which may have been the starting source.

    2 users thanked author for this post.
    #243634

    Hi Druby,

    Maybe i’m wrong… Was just asking. 🙂
    What i thought is that margin depend on bet size, while the spread depend on volatility and open/close hours.

    #243636

    I agree, but maybe if one is trading > £5,000,000+ per trade then IG may impose bigger spread?

    1 user thanked author for this post.
    #243642

    Thanks Druby this worked. IG does have variable spreads based on betsize, the values were taken from their spread table on the deal ticket and you are right GraHal that it needs big margin to trigger them. But my backtest on the SPY (S&P500) was turning  £10k into hundreds of millions so over the 2 years and 9 months of the 1min chart data so I needed to factor all variable including variable spread an variable margin for larger bets. This has worked and the actual back test results are more modest and realistic (see screenshot).

     

    Below is the final code that is behaving as expected taking the cost of spread off the profit of each trade and keeping track of the true equity curve which sets the amount of margin available to bet with.

     

     

    1 user thanked author for this post.
    #243703

    spread table on the deal ticket

    I tried to found the “spread table on the deal ticket”, but could not…
    Have you still got the link?
    All i have found is :
    https://www.ig.com/uk/help-and-support/spread-betting-and-cfds-5064504a/products-markets-and-trading-hours-008e7240/spread-bet-indices-product-details-1b3f8f8c
    https://a.c-dn.net/c/content/dam/publicsites/1721026084532/igcom/uk/files/account/240228_IGM_ENG_Indices_Tiered_Margins_CFDs_Retail.pdf

    This code is for indices or forex or crypto ? Really can’t found those bet size tiers and those spreads…

    #243704

    The spread table only appears in the IG app when you try to place a trade which is big enough to require variable spread I’ve include a screenshot to demonstrate this

     

     

    1 user thanked author for this post.
    #243709

    Thanks, and do anyone knows where to find “Indices CFD Margin Tiers (Retail)” for mini indices ?

Viewing 15 posts - 1 through 15 (of 15 total)

Create your free account now and post your request to benefit from the help of the community
Register or Login