8
Follow
1361
Followers
Python版追涨杀跌策略
Created 2020-01-11 14:49:08 Updated 2024-12-12 20:57:43
6
7375
Python版追涨杀跌策略
趋势策略一般使用各种指标来判断行情方向,使用各个指标数值对比结果来作为交易信号。这样就避免不了使用参数,计算指标。既然使用了参数,就会有拟合的情况。在某些行情下策略表现非常好,但是如果运气不好,行情走势是对当前参数非常不友好的时候,可能策略表现就会非常差。所以,个人理解,对于策略设计应当是越简单越好,这样的策略健壮性会好一些。今天我们就来分享一个不使用指标的趋势策略。策略代码非常简单,只有40行。
策略代码:
pine
import time
basePrice = -1
ratio = 0.05
acc = _C(exchange.GetAccount)
lastCancelAll = 0
minStocks = 0.01
def CancelAll():
while True :
orders = _C(exchange.GetOrders)
for i in range(len(orders)) :
exchange.CancelOrder(orders[i]["Id"], orders[i])
if len(orders) == 0 :
break
Sleep(1000)
def main():
global basePrice, acc, lastCancelAll
exchange.SetPrecision(2, 3)
while True:
ticker = _C(exchange.GetTicker)
if basePrice == -1 :
basePrice = ticker.Last
if ticker.Last - basePrice > 0 and (ticker.Last - basePrice) / basePrice > ratio :
acc = _C(exchange.GetAccount)
if acc.Balance * ratio / ticker.Last > minStocks :
exchange.Buy(ticker.Last, acc.Balance * ratio / ticker.Last)
basePrice = ticker.Last
if ticker.Last - basePrice < 0 and (basePrice - ticker.Last) / basePrice > ratio :
acc = _C(exchange.GetAccount)
if acc.Stocks * ratio > minStocks :
exchange.Sell(ticker.Last, acc.Stocks * ratio)
basePrice = ticker.Last
ts = time.time()
if ts - lastCancelAll > 60 * 5 :
CancelAll()
lastCancelAll = ts
LogStatus(_D(), "\n", "行情信息:", ticker, "\n", "账户信息:", acc)
Sleep(500)
策略简单分析
策略原理非常简单,不使用任何指标,只是使用当前价格作为交易触发依据,并且主要参数只有一个ratio控制开仓触发。
做多触发:
pine
if ticker.Last - basePrice > 0 and (ticker.Last - basePrice) / basePrice > ratio
使用当前的价格,对比基础价格,当前价格大于基础价格时,并且价格超出ratio * 100 %时,触发挂单,挂出多单。
下单后,更新基础价格为当前价格。
空单触发:
pine
if ticker.Last - basePrice < 0 and (basePrice - ticker.Last) / basePrice > ratio
做空方向原理相同,使用当前价格,对比基础价格,当前价格小于基础价格时,并且价格超出ratio * 100 %时,触发挂单,挂出空单。
下单后,更新基础价格为当前价格。
每次下单的订单量为可用资金数值的ratio * 100 %。
除非计算出的下单量小于参数设置的最小交易量minStocks,否则就下单。
这样让策略,跟随价格变动追涨杀跌。
回测测试
最近有用户说Python策略比较少,后续就多分享一些Python语言编写的策略。策略代码也非常简单,非常适合发明者量化初学者学习。
策略地址:https://www.fmz.com/strategy/181185
策略仅供参考学习,回测测试,有兴趣可以优化升级。
Related Recommendations
Research on Binance Futures Multi-currency Hedging Strategy Part 3Research on Binance Futures Multi-currency Hedging Strategy Part 2Research on Binance Futures Multi-currency Hedging Strategy Part 1Crocodile line trading system Python versionJavaScript version SuperTrend strategyIntroducing FMZ Quant data science research environmentParabolic Steering SAR and Price High and Low Point StrategyTeach you how to let an old strategy docking the websocket quotes interfaceSimilarities and differences between commodity futures and cryptocurrency exchanges APIMulti-level percentage take profit strategy
Comment
All comments (0)
No data
- 1




