forked from 51bitquant/course_codes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
lin
committed
Dec 18, 2020
1 parent
8fd626b
commit 9ed8236
Showing
7 changed files
with
300 additions
and
7 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# 第十五课: 为什么合约(期货)危险?现货和合约(期货)的交易你要了解的东西 | ||
|
||
|
||
## 现货和合约 | ||
1. 现货就是简单的买卖。但是首先你得有(买), 然后才能卖(无) | ||
|
||
2. 现货有什么局限性?(1) | ||
只能买,然后在卖,只能在上涨过程中赚钱,下跌过程是不能赚钱的(类似A股), | ||
要想赚钱,只能等牛市, (2) | ||
你有多少钱,就只能买多少钱的东西,没法扩大你的资金利用率。 | ||
|
||
3. 那么合约 Contract,或者叫期货Future, 解决了从无到有,和从有到无的过程。 | ||
开仓和平仓。 | ||
|
||
4. 合约自带杠杆, 最高125倍杠杆。那么就容易导致梭哈亏光, | ||
也可能暴富。但是这一刻可能不会到来。 | ||
|
||
5. 有杠杆就是涉及到借贷,或者说信用问题。那么就是保证金问题。你最多亏的就是你的保证金(破产,爆仓价格也叫破产价), | ||
如果你把全部本金作为保证金,那么你最多就是亏光你的所有本金。看看中行的原油宝事件,亏光本金还倒贴欠债,就是很不合理啊。想想下!!! | ||
|
||
|
||
## 合约需要保证金保证金交易 | ||
|
||
1. 合约介绍参考文档: | ||
[https://www.binancezh.pro/cn/support/faq/c-4?navId=4](https://www.binancezh.pro/cn/support/faq/c-4?navId=4) | ||
|
||
2. 强平文档: [https://www.binancezh.pro/cn/support/faq/360033525271](https://www.binancezh.pro/cn/support/faq/360033525271) | ||
|
||
|
||
|
||
3. 标记价格和市场价格的区别。 | ||
1. 标记价格是多个市场的均价。 | ||
2. 市场价格是当前交易的市场的价格。 | ||
|
||
4. 维持保证金: [https://www.binancezh.pro/cn/support/faq/360033162192](https://www.binancezh.pro/cn/support/faq/360033162192) | ||
|
||
5. 资金费率: | ||
永续合约为了让它的价格回归现货,跟现货价格一致。所以设置一个收费制度。就是价格高于现货,多头给这个手续费。价格低于这个现货,空头给这个钱。每8小时计算一次。 | ||
想象为啥? | ||
|
||
6. 爆仓清算 | ||
**用户爆仓时,保险金基金会收取一定比例的清算金,交易历史记录中标记为“爆仓清算”。但建议用户严格控制风险,避免爆仓。 | ||
用户的强平价格并不会因此发生变化。** | ||
|
||
1. 清算费率: BTC 0.5%, ETH等75倍的0.75%, 最高50倍的交易对1% | ||
|
||
6. 如何计算爆仓价格: | ||
|
||
 | ||
|
||
1. 开仓价 22988.00, 爆仓价格: 23079.59, 仓位 | ||
2. 按理说你是要亏掉0.18的时候,你才爆仓,但是真实是你什么时候爆仓呢? | ||
3. 什么时候亏掉0.18呢? (x-22988) * 0.001 = 0.18 => 0.18/0.001+22988 = | ||
23168 | ||
4. 但是真实是23079.59 就爆仓了。为啥呢? => 它有个千分之5的清算手续费。 0.18 | ||
\- 22988.00 * 0.001 * 0.005 = 0.06506, 所以你的爆仓价格是: | ||
0.06506/0.001+22988 = 23053, 这个是市场价格 | ||
|
||
通过维持保证金来计算: 初始保证金 - 维持保证金 = 你的亏损的金额. | ||
维持保证金和初始保证金。 125倍的初始保证金是1/125 = 0.8% | ||
|
||
1. (x-22988) * 0.001 = 22988 * 0.001* 0.8% - 22988 * 0.001 * 0.004 | ||
2. x = (22988 * 0.8% * 0.001 - 22988 * 0.001 * 0.004)/0.001 + | ||
22988 | ||
3. 维持保证金是0.4% | ||
## 那么我们交易用多少倍杠杆呢? | ||
|
||
1. 要在爆仓价格前设置止损。这样你的钱能少亏很多。少亏你开仓仓位价值的0.5%。 | ||
2. 如果你做到上面第一点。那么你开仓倍数没有太大的关系。 | ||
3. 计算看看 | ||
|
||
1. 假设你有10,000元。一倍杠杆,也就是不加杠杆,只用自己的本金。全仓梭哈 | ||
2. 我用10倍杠杆, 买入10000元的资产,这时保证金是1000元。 | ||
3. 我100倍杠杆, 买入10000元的资产,保证金是100元。 | ||
|
||
以上三种情况,它的爆仓价是多少呢? | ||
1. 一倍杠杆的,你的10000块钱亏光就会爆仓。涨到价格翻倍就会爆仓。 | ||
2. 如果你全仓跟1倍杠杆是一样的,但是如果逐仓就不一样 | ||
|
||
3. 1% - 0.4% = 0.6% | ||
4. 10% - 0.4% = 9.6% | ||
|
||
## 注意 | ||
1. 合约交易一定要在爆仓前设置止损,能帮助节省很多钱。 100万, -> 5000 1W * 0.005 | ||
10000* 0.0005 =50 | ||
|
||
2. 如果你是一个严格设置止损的交易者,杠杆倍数多少没什么关系。但是杠杆越大,月容易爆仓。 | ||
3. 一般设置倍数在20倍以内就够了。 | ||
4. 合理利用逐仓能保证你的资金安全。 | ||
|
||
|
||
## 币安邀请链接 | ||
|
||
1. 币安邀请链接: https://www.binancezh.pro/cn/futures/ref/51bitquant | ||
2. 合约邀请码:51bitquant | ||
|
||
|
||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from howtrader.app.cta_strategy.backtesting import BacktestingEngine | ||
from howtrader.trader.object import Interval | ||
from datetime import datetime | ||
from strategies.my_dual import MyDualWithNDays | ||
from strategies.dual_kdj import MyDualKDJ | ||
|
||
from howtrader.app.cta_strategy.backtesting import OptimizationSetting | ||
from strategies.grid_balance_strategy import GridBalanceStrategy | ||
|
||
if __name__ == '__main__': | ||
engine = BacktestingEngine() | ||
|
||
engine.set_parameters( | ||
vt_symbol="btcusdt.BINANCE", # 现货的数据 | ||
interval=Interval.MINUTE, | ||
start=datetime(2020,3,12), | ||
end=datetime(2020,12,1), | ||
rate=7.5/10000, # 币安手续费千分之1, BNB 万7.5 7.5/10000 | ||
slippage=0, | ||
size=1, # 币本位合约 100 | ||
pricetick=0.01, # 价格精度. | ||
capital=300000) | ||
|
||
|
||
engine.load_data() | ||
|
||
engine.add_strategy(GridBalanceStrategy, {"balance_diff_pct": 0.005}) | ||
engine.run_backtesting() | ||
|
||
engine.calculate_result() # 计算回测的结果 | ||
engine.calculate_statistics() # 计算一些统计指标 | ||
|
||
engine.show_chart() # 绘制图表 | ||
|
||
# 一个参数没法进行优化. | ||
# setttings = OptimizationSetting() | ||
# setttings.add_parameter("balance_diff_pct", start=0.001, end=0.10, step=0.001) | ||
# setttings.set_target("total_return") | ||
# result = engine.run_ga_optimization(setttings) | ||
# print(result) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# 第十五课: 均仓策略并回测 | ||
|
||
均仓策略是网格策略的一种变种。它原理就是通过不断平衡现金和持有的资产使其动态平衡。 | ||
|
||
假设你现在有20,000美金,btc的价格是18,000美金。 你如果全部梭哈了,只能买到 | ||
|
||
20,000/18,000 = 1.11个btc(不计算手续费), 但是你可能担心买在高位,不买又怕继续涨 | ||
这时候,你可以半仓操作买10000美金的btc,留一办的现金 | ||
|
||
如果你的网格或者动态调整仓位的比例是5%, | ||
那么如果下跌的5%的时候回加点仓位,涨5%的时候会减少仓位 一直循环操作。 | ||
这个就是动态网格。 | ||
|
||
## 策略实现过程分析 | ||
1. 首先得知道你有多少本金,有多少币 | ||
2. 然后比较他们所持仓的价值,如果他们的价值超过一定的误差,我们就调整仓位 | ||
|
||
## howtrader里面的实现过程 | ||
1. 需要订阅我们的资产 | ||
|
||
|
||
|
||
|
||
## 均仓策略特点 | ||
1. 相比其他网格来说逻辑简单,算法也很简单. | ||
|
||
|
||
|
||
|
||
|
||
|
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
from howtrader.app.cta_strategy import ( | ||
CtaTemplate, | ||
StopOrder, | ||
TickData, | ||
BarData, | ||
TradeData, | ||
OrderData, | ||
BarGenerator, | ||
ArrayManager | ||
) | ||
|
||
from howtrader.trader.event import ( | ||
EVENT_TICK, | ||
EVENT_BAR, | ||
EVENT_ORDER, | ||
EVENT_TRADE, | ||
EVENT_POSITION, | ||
EVENT_ACCOUNT | ||
) | ||
|
||
from howtrader.event import Event | ||
|
||
|
||
from howtrader.trader.constant import Interval | ||
from datetime import datetime | ||
from howtrader.app.cta_strategy.engine import CtaEngine, EngineType | ||
import pandas_ta as ta | ||
import pandas as pd | ||
|
||
|
||
class GridBalanceStrategy(CtaTemplate): | ||
|
||
author = "51bitquant" | ||
|
||
balance_diff_pct = 0.01 | ||
|
||
parameters = ["balance_diff_pct"] | ||
|
||
def __init__(self, cta_engine: CtaEngine, strategy_name, vt_symbol, setting): | ||
"""""" | ||
super().__init__(cta_engine, strategy_name, vt_symbol, setting) | ||
|
||
if cta_engine.engine_type == EngineType.LIVE: | ||
self.cta_engine.event_engine.register(EVENT_ACCOUNT, self.process_account) | ||
|
||
self.my_balance = 300000 | ||
def on_init(self): | ||
""" | ||
Callback when strategy is inited. | ||
""" | ||
self.write_log("策略初始化") | ||
self.load_bar(1) | ||
|
||
|
||
def on_start(self): | ||
""" | ||
Callback when strategy is started. | ||
""" | ||
self.write_log(f"我的策略启动, {self.trading}") | ||
self.put_event() | ||
|
||
|
||
def on_stop(self): | ||
""" | ||
Callback when strategy is stopped. | ||
""" | ||
self.write_log("策略停止") | ||
|
||
self.put_event() | ||
|
||
def on_tick(self, tick: TickData): | ||
pass | ||
|
||
def on_bar(self, bar: BarData): | ||
""" | ||
Callback of new bar data update. | ||
""" | ||
# print("1分钟的K线数据", bar) | ||
|
||
price = bar.close_price | ||
self.cancel_all() # 撤单. | ||
|
||
if self.my_balance <= 0: | ||
return | ||
|
||
if (abs(self.my_balance - self.pos * price) / self.my_balance) >= self.balance_diff_pct: | ||
|
||
balance_diff = abs(self.my_balance - self.pos * price) / 2 | ||
# print('需要进行资金平衡.', balance_diff, self.my_balance, self.pos, price) | ||
if self.my_balance > self.pos * price: | ||
self.buy(price*1.001, balance_diff/price) | ||
else: | ||
self.sell(price * 0.999, balance_diff / price) | ||
|
||
self.put_event() | ||
|
||
def process_account(self, event: Event): | ||
print("event account", event.data) | ||
|
||
def on_order(self, order: OrderData): | ||
""" | ||
订单的回调方法: 订单状态更新的时候,会调用这个方法。 | ||
""" | ||
|
||
self.put_event() | ||
|
||
|
||
def on_trade(self, trade: TradeData): | ||
""" | ||
订单成交的推送,比如你下10个BTC,那么可能不会一下子成交,会不断慢慢的成交, | ||
这时有成交它就会推送给你,告诉你成交了多少,还有多少没有成交 | ||
系统通过里面处理这个方法,知道你当前的仓位数量 | ||
""" | ||
from howtrader.trader.object import Offset | ||
if trade.offset == Offset.OPEN: | ||
self.my_balance -= trade.price * trade.volume | ||
elif trade.offset == Offset.CLOSE: | ||
self.my_balance += trade.price * trade.volume | ||
|
||
self.put_event() # 更新UI界面方法。 | ||
|
||
|
||
def on_stop_order(self, stop_order: StopOrder): | ||
""" | ||
这个是一个停止单的方法,用来监听你止损单的方法。 | ||
""" | ||
pass | ||
|