您現(xiàn)在的位置:程序化交易>> 期貨公式>> 金字塔等>> 金字塔模型>>正文內(nèi)容

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

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

?

?


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


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


# 你選擇的品種的數(shù)據(jù)更新將會觸發(fā)此段邏輯,例如日或分鐘歷史數(shù)據(jù)切片或者是實時數(shù)據(jù)切片更新。--(必須實現(xiàn))
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()
???????
??????? #建立頭寸,根據(jù)唐奇安通道創(chuàng)新高入場,關(guān)鍵點就是利用波動atr計算倉位數(shù)量,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)

 

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

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

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

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

 


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

相關(guān)文章

    沒有相關(guān)內(nèi)容
  主站蜘蛛池模板: 肉柳高嫁03集在线播放| 国产男女爽爽爽免费视频| 国产精品综合一区二区| 国产粗话肉麻对白在线播放| 国产亚洲精久久久久久无码77777| 午夜一级毛片免费视频| 免费午夜爽爽爽WWW视频十八禁| 亚洲欧美日韩中文综合v日本| 九九久久99综合一区二区| 中文字幕在线看视频一区二区三区 | 韩国激情3小时三级在线观看| 精精国产XXXX视频在线| 毛片大全免费看| 日韩人妻无码精品一专区| 妇乱子伦精品小说588| 国产精品91av| 古代np多夫h肉辣文| 亚洲欧洲日产国码av系列天堂 | 亚洲欧美成人一区二区在线电影| 九九精品视频在线观看| 中文字幕精品一区| 91精品欧美一区二区综合在线| 麻花传MD034苏蜜清歌| 男性gay黄免费网站| 极品videossex日本妇| 尤物久久99热国产综合| 国产精品9999久久久久仙踪林 | 国产精品一区二区久久精品涩爱| 国产99久久精品一区二区| 国产韩国精品一区二区三区 | 亚洲av福利天堂一区二区三 | 久久这里精品国产99丫e6| xxxxx做受大片视频| 黄色免费网站网址| 男人j进入女人p狂躁免费观看 | 理论秋霞在线看免费| 日本特黄特色aaa大片免费| 国产鲁鲁视频在线播放| 国产caowo13在线观看一女4男| 亚洲欧洲久久精品| 国产午夜精品久久久久免费视 |