วิธี “PVSRA” มักใช้โดยเทรดเดอร์ที่มีเลเวอเรจสูงและก้าวร้าวในกรอบเวลาที่ต่ำกว่า เพื่อระบุความตั้งใจของ “ผู้ดูแลสภาพคล่อง” วิธีการนี้รวมเอาปริมาณและค่าเฉลี่ยเคลื่อนที่แบบเอกซ์โปเนนเชียลไว้ในแท่งเทียน OHLC ซึ่งเพิ่มโอกาสในการจับสถานะซื้อที่มีกำไรจากการเคลื่อนตัวเป็นขาขึ้นหรือสั้นลงที่ขาลง
การซื้อขายแบบอัลกอริทึมได้รับความนิยมเพิ่มมากขึ้น และ Python เป็นหนึ่งในตัวเลือกอันดับต้น ๆ สำหรับการเขียนโปรแกรมบอทการซื้อขายเพื่อดำเนินการหรือทดสอบกลยุทธ์ มาดำดิ่งกัน
นำเข้าข้อมูล
ฉันใช้แพ็คเกจ API ของ Binance และแพ็คเกจ python-binance เพื่อดึงข้อมูลเข้าสู่ Python
ขั้นแรก รับคีย์ API ของคุณ จากนั้นคัดลอก API และคีย์ลับ
วิธีสร้าง API | Binance,api,สร้าง
จากนั้นติดตั้งไลบรารี่
pip install python-binance
จากนั้นนำเข้าและสร้างอินสแตนซ์:
from binance.client import Client, enums user_key = '' secret_key = '' client = Client(user_key, secret_key)
ในการรับข้อมูล เราสามารถใช้ปลายทาง get_historical_klines
columns = ['Kline Open Time', 'Open Price', 'High Price', 'Low Price', 'Close Price', 'Volume', 'Kline Close Time', 'Quote Asset Volume', 'Number of Trades', 'Taker Buy Base Asset Volume','Taker Buy Quote Asset Volume', 'Unused'] df = pd.DataFrame( client.get_historical_klines("ETHUSDT", Client.KLINE_INTERVAL_15MINUTE, "23 Sept, 2022", klines_type=enums.HistoricalKlinesType.SPOT), columns=columns)
สิ่งนี้จะสร้าง DataFrame พร้อมข้อมูลทั้งหมดที่เราต้องการ และอื่นๆ อีกมากมาย โดยเฉพาะอย่างยิ่ง สิ่งนี้จะส่งคืนข้อมูล OHLC พร้อมด้วยปริมาณและข้อมูลเพิ่มเติม
ฟังก์ชันอำนวยความสะดวกนี้จะตัดคอลัมน์ที่ไม่จำเป็นออก จัดเรียงข้อมูล และตั้งค่าวันที่เป็นดัชนี
def fix_df(df): # make them numbers df = df.apply(pd.to_numeric, errors='coerce') # get the date df['Date'] = pd.to_datetime(df['Kline Open Time'], unit='ms', errors='coerce') df = df.sort_values(by='Date', ascending=True) df.rename(columns={'Open Price': 'Open', 'High Price': 'High', 'Low Price': 'Low', 'Close Price': 'Close'}, inplace=True) df.set_index('Date', inplace=True) df.drop(df.columns[[0, 6, 7, 8, 9, 10, 11]], axis=1, inplace=True) return df df = fix_df(df)
ตอนนี้เรามีข้อมูลทั้งหมดที่เราต้องการแล้ว มีเพียงบางสิ่งที่ต้องระบุที่นี่ อย่างแรกคือเมื่อเราหาค่าเฉลี่ย เราจะหาค่าเฉลี่ยเคลื่อนที่อย่างง่ายของแท่งเทียน 10 แท่งก่อนหน้า นอกจากนี้ การแพร่กระจายของแท่งเทียนของแท่งเทียนคือค่าสัมบูรณ์ของความแตกต่างระหว่างจุดสูงสุดและจุดต่ำสุดของแท่งเทียน
วิธี PVSRA แบ่งประเภทของเทียนออกเป็น 2-3 หมวดหมู่ ประเภทแรกคือ จุดไคลแม็กซ์/ปริมาณสูง:
- ถ้าปริมาณปัจจุบัน≥ปริมาณเฉลี่ย * 2 หรือ
- การแพร่กระจายของแท่งเทียน * ปริมาณ ≥ สูงสุด (การกระจายของแท่งเทียน * ปริมาณ) ในช่วง 10 แท่งเทียนที่ผ่านมา
หมวดหมู่ที่สองคือ ปริมาณที่เพิ่มขึ้น:
- ถ้าปริมาณปัจจุบัน ≥ ปริมาณเฉลี่ย * 1.5
หมวดหมู่สุดท้ายคือ ปกติ ซึ่งเป็นเพียงแท่งเทียนปกติเมื่อไม่ตรงตามเงื่อนไขเหล่านี้ มาเริ่มเขียนโค้ดกันเถอะ
รหัส
ที่นี่เราเพียงคำนวณค่าเฉลี่ยเคลื่อนที่อย่างง่ายของปริมาณในช่วง 10 แท่งเทียนที่ผ่านมา
import numpy as np import pandas as pd def calculate_average_vol(df): df['Av'] = df['Volume'].rolling(window=10).mean() return df df = calculate_average_vol(df)
ต่อไป เราจะคำนวณการแพร่กระจายของแท่งเทียนและเงื่อนไขที่สองสำหรับหมวดหมู่ "จุดไคลแม็กซ์/ปริมาณสูง"
def calculate_climax(df): df['Spread * Volume'] = df['Volume'] * abs((df['High'] - df['Low'])) df['HiValue2'] = df['Spread * Volume'].rolling(window=10).max() df['Volume > 2 * Average'] = df['Volume'] >= (df['Av'] * 2) df['Spread > HiValue2'] = df['Spread * Volume'] >= df['HiValue2'] return df df = calculate_climax(df)
EMA ยังเป็นตัวเลือกยอดนิยมสำหรับกลยุทธ์ PVSRA ดังนั้นจึงมีการเขียนฟังก์ชันอำนวยความสะดวกเพื่อคำนวณ EMA ที่มีความยาวตามต้องการ
def calculate_emas(df, emas=(5, 13, 50, 200)): # calculate emas for ema in emas: df[str(ema) + ' Day EMA'] = df['Close'].ewm(span=ema, adjust=False, ignore_na=False).mean() return df df = calculate_emas(df)
สุดท้ายนี้ เราสามารถคำนวณแท่งเทียน PVSRA ให้เป็นประเภทที่หนึ่ง สอง หรือสามได้ ด้วยการใช้ np.select
เราสามารถกำหนดเงื่อนไขที่เป็นไปได้หลายรายการในแถวของ DataFrame
def calculate_pvsra(df: pd.DataFrame): df['isBull'] = (df['Close'] > df['Open']).astype(int) df['isBull'] = df['isBull'].map({0: 'no', 1: 'yes'}) climax = (df['Volume'] >= df['Av'] * 2) | \ (df['Spread * Volume'] >= df['HiValue2']) rising = df['Volume'] >= (df['Av'] * 1.5) condlist = [climax, rising] choicelist = [2, 1] df['VA'] = np.select(condlist=condlist, choicelist=choicelist, default=0) return df df = calculate_pvsra(df)
ตอนนี้เราสามารถจำแนกและดูจำนวนเทียนที่พิมพ์เพื่อตรวจสอบซ้ำได้ หากแท่งเทียนเป็นขาขึ้นและอยู่ในหมวดแรก เราจะใช้สีเขียว หากเป็นภาวะหมี เราจะให้สีแดง เทียนประเภทที่สองจะมีสีฟ้าและชมพูตามลำดับ เทียนในหมวดหมู่สุดท้ายจะถูกทิ้งไว้ตามลำพัง
bull = df[df['isBull'] == 'yes'] bear = df[df['isBull'] != 'yes'] df_green = bull[(bull['VA'] == 2)] df_blue = bull[(bull['VA'] == 1)] df_red = bear[(bear['VA'] == 2)] df_pink = bear[(bear['VA'] == 1)] print('Number of red', len(df_red)) print('Number of blue', len(df_blue)) print('Number of green', len(df_green)) print('Number of pink', len(df_pink))
พล็อตผลลัพธ์โดยใช้ Plotly
เทียนที่รั้น (ปิด › เปิด)
import plotly.graph_objects as go def create_candlestick_chart(dataframe: pd.DataFrame): return go.Candlestick(x=dataframe.index, open=dataframe['Open'], high=dataframe['High'], low=dataframe['Low'], close=dataframe['Close']) fig = go.Figure(create_candlestick_chart(df)) fig.add_traces(create_candlestick_chart(df_red)) fig.add_traces(create_candlestick_chart(df_blue)) fig.add_traces(create_candlestick_chart(df_green)) fig.add_traces(create_candlestick_chart(df_pink)) # red fig.data[1].increasing.fillcolor = 'rgb(255,0,0)' fig.data[1].increasing.line.color = 'rgb(255,0,0)' fig.data[1].decreasing.fillcolor = 'rgb(255,0,0)' fig.data[1].decreasing.line.color = 'rgb(255,0,0)' # blue fig.data[2].increasing.fillcolor = 'rgba(14, 0, 172, 1)' fig.data[2].increasing.line.color = 'rgba(14, 0, 172, 1)' fig.data[2].decreasing.fillcolor = 'rgba(14, 0, 172, 1)' fig.data[2].decreasing.line.color = 'rgba(14, 0, 172, 1)' # green fig.data[3].increasing.fillcolor = 'rgb(0, 255, 0)' fig.data[3].increasing.line.color = 'rgb(0, 255, 0)' fig.data[3].decreasing.fillcolor = 'rgb(0, 255, 0)' fig.data[3].decreasing.line.color = 'rgb(0, 255, 0)' # pink fig.data[4].increasing.fillcolor = 'rgba(234, 0, 255, 1)' fig.data[4].increasing.line.color = 'rgba(234, 0, 255, 1)' fig.data[4].decreasing.fillcolor = 'rgba(234, 0, 255, 1)' fig.data[4].decreasing.line.color = 'rgba(234, 0, 255, 1)' fig.show()