In a previous blog post A Comprehensive Guide to Funding Rates in FX and Crypto Markets, it highlighted the significance of the funding rate in developing an effective trading strategy. This article provides a practical example to demonstrate how to set up customized funding curves on ALGOGENE platform
Data Preparation
The steps are similar to this article Guideline to backtest with custom datasets. Suppose we have below CSV file test.csv containing the swap fee history of EURUSD. In this example, we assume the funding rate is updated on a weekly basis.
Date,Long,Short 2025-01-01,-0.0026,0.00146 2025-01-08,-0.00225,0.00153 2025-01-15,-0.002255,0.00151 2025-01-22,-0.00231,0.0012 2025-01-29,-0.00236,0.00098 2025-02-05,-0.002349,0.00103 2025-02-12,-0.00235,0.001 2025-02-19,-0.00237,0.00057 2025-02-26,-0.00234,0.00088 2025-03-05,-0.00234,0.00086 2025-03-12,-0.002353,0.00074 2025-03-19,-0.002346,0.00079 2025-03-26,-0.00232,0.00076
This file contains 3 columns:
- Date: represent the date when there are updates on funding rate figures. During backtest, the last observed funding rates will be used for calculation.
- Long: the rate used for a net long position to calculate the funding amount
- Short: the rate used for a net short position to calculate the funding amount
Data Import
Now, let's import our data files as follows:
- After login the portal, go to [My History] > [Custom File Viewer]
- Select '/data' directory, then upload data files
- We can then 'Edit' to view the uploaded content
- Now, we need to create a meta file '_meta_.json' to instruct the system how to process the data files.
- '_meta_.json' is in JSON format where we can specify the curve name in the first key. In this example, we label it as 'EURUSDswap'
- the second key of '_meta_.json' should contain the following
- 'file': the file name on the cloud directory
- 'file_delimiter': the file delimiter used in our data file
- 'fmt_time': the date format used in our data file, in Python date encoding
- %Y: the year in four-digit format, eg. "2018"
- %y: the year in two-digit format, that is, without the century. For example, "18" instead of "2018"
- %m: the month in 2-digit format, from 01 to 12
- %b: the first three characters of the month name. eg. "Sep"
- %d: day of the month, from 1 to 31
- %H: the hour, from 0 to 23
- %M: the minute, from 00 to 59
- %S: the second, from 00 to 59
- %f: the microsecond from 000000 to 999999
- %Z: the timezone
- %z: UTC offset
- %j: the number of the day in the year, from 001 to 366
- %W: the week number of the year, from 00 to 53, with Monday being counted as the first day of the week
- %U: the week number of the year, from 00 to 53, with Sunday counted as the first day of each week
- %a: the first three characters of the weekday, e.g. Wed
- %A: the full name of the weekday, e.g. Wednesday
- %B: the full name of the month, e.g. September
- %w: the weekday as a number, from 0 to 6, with Sunday being 0
- %p: AM/PM for time
- 'col_time': the column index of date (first column index = 0)
- 'col_long': the column index of long side (first column index = 0)
- 'col_short': the column index of short side (first column index = 0)


The sample meta file used in the example can be copied here:
{
"EURUSDswap":{
"file": "test.csv",
"file_delimiter": ",",
"fmt_time": "%Y-%m-%d",
"col_time": 0,
"col_long": 1,
"col_short": 2
}
}
Backtest
After we properly setup the '_meta_.json', we can then add our funding curves for backtest.
- Go to [Backtest] > [Setting]
- Select 'EURUSD' in the instrument panel
- 'Start Period' and 'End Period' set to '2025-01' and '2025-03' respectively
- 'Initial Capital' set to 100,000
- 'Base Currency' set to 'USD'

Then, in backtest script, we can use self.evt.setFundingCurve() function to assign the funding curve (see line #12 below). As an example, we just open a buy order at the beginning of the backtest without any close order logic.
The full source code of our example as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | from AlgoAPI import AlgoAPIUtil, AlgoAPI_Backtest from datetime import datetime, timedelta class AlgoEvent: def __init__(self): self.isfirsttrade = False def start(self, mEvt): self.evt = AlgoAPI_Backtest.AlgoEvtHandler(self,mEvt) self.evt.setFundingCurve(instrument="EURUSD", curve_name="EURUSDswap") self.evt.start() def on_marketdatafeed(self, md, ab): if not self.isfirsttrade: self.isfirsttrade = True order = AlgoAPIUtil.OrderObject( instrument = md.instrument, volume = 0.01, openclose = 'open', buysell = 1, ordertype = 0 #market order ) self.evt.sendOrder(order) |
After running the backtest, we can see that daily funding fee is calculated and reflected in account balance.

Now, you learnt how to include funding rate curves on the platform. Try backtest with a custom dataset today! Happy Trading!
