Insufficient Capital in Strategy Backtesting when Buying Futures and Forex Asset

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #243386 quote
    jogtt
    Participant
    New

    I am backtesting a trading strategy with different asset classes. For the time being, it has worked properly with indices and stocks. Nonetheless, when analysing Futures or Forex , the position is opened at the opening price of a candle and then closed at the close price of the very same candle, although the sell conditions are not met.

    I believe the error is related to how shares are bought and the leverage that might be involved, as the message I am getting In the ‘Equity Curve’ chart panel is Insufficient capital, but since I am still a beginner, I do not actually know how to go about this . I am using the following code:

    // Initial capital
    InitialCapital = 100000000
    CumulativeProfit = StrategyProfit + InitialCapital 
    
    // Calculate the number of shares (lots) to buy
    LotsToBuy = FLOOR(CumulativeProfit / Close)
    
    // Execute Buy Trade
    IF Close>Close[1] THEN
       IF NOT OnMarket THEN
          BUY LotsToBuy LOTS AT MARKET
       ENDIF
    ENDIF
    
     // Execute Sale Trade
    IF Close<Close[3] THEN // SellCondition1 AND SellCondition2 OR SellCondition4
       IF OnMarket THEN
          SELL AT MARKET
       ENDIF
    ENDIF

    It would be highly appreciated if you could let me know how to fix the code.

    Many thanks in advance for your help!

    #243395 quote
    robertogozzi
    Moderator
    Master

    As to your exiting condition, simply append this line to your code, so you can see when it’s TRUE:

    graph (Close<Close[3]) AND OnMarket

    As to the Capital, input the value you want as shown in the highlighed box (see the attached pic).

    But you should also reduce your InitialCapital, as the larger it is the higher number of assets/lots will be traded, without solving the problem.

    jogtt and Iván González thanked this post
    #243403 quote
    jogtt
    Participant
    New

    Thanks for your reply, robertogozzi.

    I have been modifying the code as you suggested, and the sell condition is not triggered, as you can see in the image attached.

    Concerning the initial capital, I have tried different values: 100, 1k, 10k, 100k, 1M. Although 1 future contract was worth 35-40USD at the entry time step, the ‘insufficient capital’ issue is not solved…any ideas?

    #243418 quote
    robertogozzi
    Moderator
    Master

    I set InitialCapital = 10000 and 20K+ as the Capital for the backtest.

    I tested it on Apple (AAPL), Daily TF, and it seems to be working as expected.

    jogtt thanked this post
    #243424 quote
    jogtt
    Participant
    New

    Exactly. Because the code works for assets classified as ‘stocks’ or ‘index’. However, if you try the same code with ‘futures’ or ‘forex’, it is when it does not work

    #243426 quote
    robertogozzi
    Moderator
    Master

    Use a 10K InitialCapital and 100LK+ as Capital to backtest, then replace line 6 by:

    LotsToBuy = max(1,FLOOR(CumulativeProfit / (Close / PipSize)))
    jogtt thanked this post
    #243427 quote
    jogtt
    Participant
    New

    Fixed! Grazie mille, @robertogozzi 

    robertogozzi thanked this post
Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.

Insufficient Capital in Strategy Backtesting when Buying Futures and Forex Asset


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
jogtt @jogtt Participant
Summary

This topic contains 6 replies,
has 2 voices, and was last updated by jogtt
1 year ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 02/03/2025
Status: Active
Attachments: 2 files
Logo Logo
Loading...