人人爽天天爽夜夜爽qc-人人爽天天爽夜夜爽曰-人人天天爱天天做天天摸-人人天天夜夜-色网站在线-色网站在线看

您現在的位置:程序化交易>> 期貨公式>> 金字塔等>> 金字塔模型>>正文內容

金字塔PYTHON海龜交易策略源碼[金字塔模型]

# 本Python代碼主要用于策略交易

?

?


# 可以自己import我們平臺支持的第三方python模塊,比如pandas、numpy等。
from PythonApi import *
import numpy as np
import talib
import math


#? 在這個方法中編寫任何的初始化邏輯。context對象將會在你的算法策略的任何方法之間做傳遞。--(必須實現)
def init(context):
??? #入場周期
??? context.X = 20
??? #出場周期
??? context.Y = 10
??? #記錄建倉的atr
??? context.entry = 0
??? #記錄交易次數
??? context.num = 0
??? #交易標的
??? context.s = context.run_info.base_book_id
??? #記錄上次開倉價
??? context.enterprice = 0


# 你選擇的品種的數據更新將會觸發此段邏輯,例如日或分鐘歷史數據切片或者是實時數據切片更新。--(必須實現)
def handle_bar(context):
??? close = history_bars(context.s,context.X+2,\'self\',\'close\',include_now=True)
??? high = history_bars(context.s,context.X+2,\'self\',\'high\',include_now=True)?
??? low = history_bars(context.s,context.X+2,\'self\',\'low\',include_now=True)??
??? if len(close) == context.X+2:
??????? #atr的計算參考這個帖子http://www.weistock.com/bbs/dispbbs.asp?boardid=10&Id=173300
??????? tr = talib.TRANGE(high,low,close)
??????? atr = talib.SMA(tr[1:],context.X)
??????? unit = int((get_account(6)*0.01) / (atr[-2] * get_dynainf(context.s,209)))
??????? #X天的高低點(不包含當天)
??????? X周期高點 = high[:-1].max()
??????? X周期低點 = low[:-1].min()
???????
??????? #建立頭寸,根據唐奇安通道創新高入場,關鍵點就是利用波動atr計算倉位數量,portfolio用來進行倉位的控制
??????? portfolio=get_portfolio (context.s, 2)
??????? if high[-1]>=X周期高點 and portfolio.buy_quantity==0 and portfolio.sell_quantity==0:
??????????? buy_open(context.s, "Market",0 ,unit,serial_id = 1)
??????????? context.entry = atr[-2]
??????????? context.num = 1
??????????? context.enterprice = close[-1]
??????? if low[-1]<=X周期低點 and portfolio.sell_quantity==0 and portfolio.buy_quantity==0:
??????????? sell_open(context.s, "Market",0 ,unit,serial_id = 2)
??????????? context.entry = atr[-2]
??????????? context.num = 1
??????????? context.enterprice = close[-1]
???????????
??????? #加倉,最高價比上次開倉價多0.5個atr(盈利加倉)
??????? if portfolio.sell_quantity ==0 and portfolio.buy_quantity>0 and high[-1]>context.enterprice + 0.5*context.entry and context.num<4:
??????????? buy_open(context.s, "Market",0 ,unit,serial_id = 3)
??????????? context.num+=1
??????????? context.enterprice = close[-1]
??????? if portfolio.buy_quantity==0 and portfolio.sell_quantity>0 and low[-1]<context.enterprice - 0.5*context.entry and context.num<4:
??????????? sell_open(context.s, "Market",0 ,unit,serial_id = 4)
??????????? context.num+=1
??????????? context.enterprice = close[-1]
???????????
??????? #出場,跌破短周期低點平多
??????? Y周期高點 = high[-context.Y-1:-1].max()
??????? Y周期低點 = low[-context.Y-1:-1].min()
??????? if portfolio.buy_quantity>0 and low[-1] < Y周期低點:
??????????? sell_close(context.s,"Market",0,portfolio.buy_quantity,serial_id = 5)
??????? if portfolio.sell_quantity>0 and high[-1] > Y周期高點:
??????????? buy_close(context.s,"Market",0,portfolio.sell_quantity,serial_id = 6)
???????????
??????? #止損,虧損幅度超過開倉2個atr幅度止損
??????? if portfolio.buy_quantity>0 and low[-1] < context.enterprice - 2*context.entry:
??????????? sell_close(context.s,"Market",0,portfolio.buy_quantity,serial_id = 7)
??????? if portfolio.sell_quantity>0 and high[-1] > context.enterprice + 2*context.entry:
??????????? buy_close(context.s,"Market",0,portfolio.sell_quantity,serial_id = 8)

 

有思路,想編寫各種指標公式,交易模型,選股公式,還原公式的朋友

可聯系技術人員 QQ: 262069696  點擊在線交流或微信:cxhjy888 進行 有償收費 編寫!(注:由于人數限制,QQ或微信請選擇方便的一個聯系我們就行,加好友時請簡單備注下您的需求,否則無法通過。謝謝您!)

怎么收費,代編流程等詳情請點擊查閱!

(注:由于人數限制,QQ或微信請選擇方便的一個聯系我們就行,加好友時請簡單備注下您的需求,否則無法通過。謝謝您!)

 


【字體: 】【打印文章】【查看評論

相關文章

    沒有相關內容
  主站蜘蛛池模板: 小明成人永久视频在线观看 | 91蝌蚪国产| 好吊色这里只有精品 | 国产大片中文字幕 | 天天干天天操天天射 | 一区二区三区中文字幕 | 黄色片视频在线观看 | 性欧美v | 亚洲精品日韩专区在线观看 | 一二三四视频社区在线1 | 国产丝袜精品丝袜久久 | 亚洲国产成人久久午夜 | 久久黄色免费 | 亚洲欧美第一 | 日韩专区中文字幕 | 99精品在线播放 | 视频三区精品中文字幕 | 成年视频免费 | 欧美视频免费一区二区三区 | 免费色视频 | 印度一级毛片免费的 | 阿v精品一区二区三区 | 国产黄色免费 | 香蕉在线视频网站 | 看全色黄大色黄大片 视 | 精品国产91乱码一区二区三区 | 中文字幕在线成人免费看 | 免费特黄级夫费生活片 | 精品中文字幕一区二区三区四区 | 国产精品成人观看视频国产 | 一级女性黄 色生活片 | 男女在线视频 | 无限资源日本好片 | 亚洲第一区视频 | 青草91| 国产精品久久久久久久久鸭 | 国产视频99 | 亚洲人成一区二区不卡 | 日韩小片| 国内一区二区三区精品视频 | 欧美3p在线观看一区二区三区 |