After the previous lesson, we started to explain the strategy framework systematically. You need to register a Vitu account to use the Start Research feature
(1) Start with a very simple trading strategy
This trading strategy is very simple:
Buy 0.1 bitcoins a day
As shown in the figure, paste the code into the edit area and click "Run this policy so that the computer can execute, first make the policy conform to the Initialization + Cycle framework, like this:
Initialization: Select the Bitcoin as the subject of the transaction, set the exchange to binance Cycle daily: Buy 0.1 Bitcoins
(2) What is the Initialization + Cycle framework?
In order to effectively transform investment inspiration into a computer-executable trading strategy, you must write based on a model, which is the framework.The vitu framework has two parts, initialization and cycle:
def initialize(context) is what a policy does before it starts to run, such as preparing the subject matter for a transaction
Defhandle_data (context) refers to what happens in each cycle as the cycle of time elapses after the policy has started running.For example, the cycle is day, and the cycle is to buy 0.1 bitcoins per day.
In fact, the "Initialization + Cycle" framework is easy to understand, people do transactions every day is in line with this framework, initialization is to set how much cash, cycle is to view the market every day or minute, judge, order and so on.
(3) How to code the strategy?
Let's learn how to write the Initialization + Cycle framework code:
def initialize(context)
This is where you write the initialization code, for example, by selecting the bitcoin that you want to trade
def handle_data(context)
Here's where you write the cycle code, for example, by buying 0.1 bitcoins
What does def,context mean?
It's actually a call to a function provided by vitu. If you don't understand it, remember that later learning will make you understand it
Where should the code be written?
After opening Vitu.AI, create a new policy by Starting Research -> New ->:

Enter the Policy Editing page. On the right is the Policy Code Editing Area. The code template is initially provided to you by default. Delete it completely and write our code.

What about a complete example?
First set up the account, we set the exchange as binance, the virtual account is named myaccount, and the initial capital is USDT of 10,000
from vitu import ai ai.create_account(name='myaccount', exchange='binance', account_type='digital.spot', position_base=[{'asset': 'USDT', 'qty': 10000}])
Select the bitcoin that you want to trade and get the previously initialized account:
context.security = 'BTC/USDT.binance' context.myaccount = context.get_account('myaccount')
Buy 0.1 bitcoins and use the account you just obtained:
current_price = context.get_price(context.security) context.myaccount.buy(context.security, current_price, 0.1)
After completing the code into the Initialization + Cycle framework:
from vitu import ai ai.create_account(name='myaccount', exchange='binance', account_type='digital.spot', position_base=[{'asset': 'USDT', 'qty': 10000}]) def initialize(context): context.security = 'BTC/USDT.binance' context.myaccount = context.get_account('myaccount') def handle_data(context): current_price = context.get_price(context.security) context.myaccount.buy(context.security, current_price, 0.1)
So is this code ready to run now?
Wait a moment, let's configure the parameters of the strategy, such as baseline, frequency, start time of the test, handling fee, etc.
universe = ai.create_universe(['BTC/USDT.binance']) my_strategy = ai.create_strategy(initialize,handle_data, universe=universe, benchmark='csi5', freq='d', refresh_rate=1) ai.backtest(strategy=my_strategy, start='2018-12-10', end='2019-08-10', commission={'taker': 0.0002, 'maker': 0.0002} )
OK! Great success!The completion code is as follows:
from vitu import ai ai.create_account(name='myaccount', exchange='binance', account_type='digital.spot', position_base=[{'asset': 'USDT', 'qty': 10000}]) def initialize(context): context.security = 'BTC/USDT.binance' context.myaccount = context.get_account('myaccount') def handle_data(context): current_price = context.get_price(context.security) context.myaccount.buy(context.security, current_price, 0.1) universe = ai.create_universe(['BTC/USDT.binance']) my_strategy = ai.create_strategy(initialize,handle_data, universe=universe, benchmark='csi5', freq='d', refresh_rate=1) ai.backtest(strategy=my_strategy, start='2018-12-10', end='2019-08-10', commission={'taker': 0.0002, 'maker': 0.0002} )
As shown, paste the code into the edit area and click Run.

You will see the results when you finish running

You can see that if you have an initial capital of $10,000 from December 10, 2018, and try to buy 0.1 bitcoins per transaction day, by 2019-08-10, your earnings curve will grow as yellow line in the chart, your capital will be $31,982, and the gray line in the chart is the base earnings (default is csi5, which represents the growth level of the first five currencies in the market value)
Next, click on "Test Back Details" to see more detailed results, including the record of placing an order, the record of holding a position, etc.

What do you mean by the details of the test?
As just happened, using historical and true market data over a period of time to verify how a certain trading strategy behaves over that period is called a retest.
Details of the retrospective test will tell you how the strategy behaves during this period, such as rates of return, annualized rates of return, maximum withdrawal, Sharp ratio and other indicators, and will generally include the record of placing an order, the record of holding a position, etc.
When exactly does the cycle begin?
If the strategy frequency is daily, it starts at zero UTC on each trading day (that is, 8:00 a.m. Beijing Time), so in the example, the opening cycle starts at 8:00 a.m. on each trading day, and the buying of bitcoins is repeated once a day.
If the policy frequency is minute, it is executed at the beginning of each minute, so the Bitcoin Buy operation in the example starts at 8:00:00 per transaction day, then 8:01:00, so it loops once a minute.
I'm here today.Next time, let's talk about orders, functions and API s.
Vitu.ai - Data never panics - The world of block chains is no exception