Opublikowano:
Aktualizacja:
W celu pełniej kontroli handlu oraz ograniczeniu ryzyka stosuje się stop_loss oraz take_profit. W tym rozdziale stworzymy taką funkcję.
from random import seed
from random import random
import math
import tensorflow as tf
from tensorflow import keras
import os
import tempfile
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
import plotly.graph_objects as go
import sklearn
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from plotly.subplots import make_subplots
from scipy import stats
mpl.rcParams['figure.figsize'] = (12, 10)
colors = plt.rcParams['axes.prop_cycle'].by_key()['color']
Z danych wyszukujemy sobie “ciekawe” miejsca.
data=[]
df=[]
data=pd.read_csv('./05_04_2022.csv',decimal='.')
df1=data[1000:1250] #luka na wzrost
df2=data[3320:3570] #luka na spadek
df_S = df2.copy()
df_W = df1.copy()
l_usrednien_H=25
l_usrednien_L=5
def my_function1(tablica,l_usrednien_H_f,l_usrednien_L_f):
tablica['sr1'] =tablica['close'].ewm(span=l_usrednien_H_f, adjust=False).mean()
tablica['sr2'] =tablica['close'].ewm(span=l_usrednien_L_f, adjust=False).mean()
tablica['v1'] =tablica['sr2']>tablica['sr1']
#usuwa początkowe dane z tablicy poza uśrenianiem
train0z=[]
train0z=tablica[l_usrednien_H_f:]
train0z= train0z.reset_index(drop=True)
#usuwa początkowe dane z tablicy które są 'true'
iind=0
for txr in range(len(train0z)):
if train0z['v1'].iloc[txr]==1:
iind=txr
break
train0zxL=[]
train0zxL=train0z[iind:]
train0zxL= train0zxL.reset_index(drop=True)
#dodaje kolumnę 'nr'
sq=[]
tr=[]
for aw in range(len(train0zxL)):
sq.insert(0,len(train0zxL)-aw-1)
tr=pd.DataFrame(sq)
train0zxL =train0zxL.assign(nr=tr.values)
return train0zxL
train0zW=my_function1(df_W,l_usrednien_H,l_usrednien_L)
train0zS=my_function1(df_S,l_usrednien_H,l_usrednien_L)
W celu zamknięcia wcześniej otworzonej pozycji o wolumenie 0.02 EURUSD uruchamiamy funkcję zamknij()*.
fig = go.Figure(data=[go.Candlestick(x=train0zW['nr'],
open=train0zW['open'],
high=train0zW['high'],
low=train0zW['low'],
close=train0zW['close'])])
fig.update_layout(xaxis_rangeslider_visible=False)
fig.show()
fig = go.Figure(data=[go.Candlestick(x=train0zW['nr'],
open=train0zS['open'],
high=train0zS['high'],
low=train0zS['low'],
close=train0zS['close'])])
fig.update_layout(xaxis_rangeslider_visible=False)
fig.show()
Analogiczne postępujemy aby zakupić parę walutową.
def my_function2(tablica,nr_linii,cena_otwarcia,stopL,takeP,spread,typ_f,dat_trading,deb):
#tablica - macierz do analizy
#nr_linii - numer linii gdzie otwarta jest pozycja
#cena_otwarcia - cena otwarcia pozycji
#stopL -stop loss
#takeP -takie profit
#spread -spread
#typ - 1-buy, -0- sell
#dat_trading 1- jeden dzień, 0-więcej dni
#deb - opcja debugowania
(...dalszy kod źródłowy funkcji w strefie premium...)
def my_function_W(tablica,nr_linii_f,stopL_f,takeP_f,typ_f,deb):
day_trading=0
spread=0
tablica_in=tablica
p_open=tablica_in['open'][nr_linii_f]
if typ_f==0:
stopL_f2=p_open+stopL_f
takeP_f2=p_open-takeP_f
else:
stopL_f2=p_open-stopL_f
takeP_f2=p_open+takeP_f
l_end,l_val=my_function2(tablica_in,nr_linii_f,p_open,stopL_f,takeP_f,spread,typ_f,day_trading,deb)
if deb==1:
fig = go.Figure(data=[go.Candlestick(x=tablica_in['nr'],
open=tablica_in['open'],
high=tablica_in['high'],
low=tablica_in['low'],
close=tablica_in['close'])])
fig.add_trace(go.Scatter(x=tablica_in['nr'], y=tablica_in['sr1'],name='sr1'))
fig.add_trace(go.Scatter(x=tablica_in['nr'], y=tablica_in['sr2'],name='sr2'))
fig.add_shape(type="line",x0=nr_linii_f, y0=0, x1=nr_linii_f, y1=1,xref='x', yref='paper',line=dict(color="Red",width=0.5,dash="dot"))
fig.add_shape(type="line",x0=l_end, y0=0, x1=l_end, y1=1,xref='x', yref='paper',line=dict(color="Orange",width=0.5,dash="dot"))
fig.add_shape(type="line",x0=0, y0=p_open, x1=1, y1=p_open,yref='y', xref='paper',line=dict(color="Blue",width=0.5,dash="dot"))
fig.add_shape(type="line",x0=0, y0=takeP_f2, x1=1, y1=takeP_f2,yref='y', xref='paper',line=dict(color="Green",width=0.5,dash="dot"))
fig.add_shape(type="line",x0=0, y0=stopL_f2, x1=1, y1=stopL_f2,yref='y', xref='paper',line=dict(color="Green",width=0.5,dash="dot"))
fig.update_layout(xaxis_rangeslider_visible=False)
fig.show()
print(round(l_val,1),l_end)
return l_end,round(l_val,1)
nr_linii=60
stopL=40
takeP=30
tab=train0zW
typ_w="buy"
deb=1
if typ_w=="buy":
typ=1
l_end,l_val=my_function_W(tab,nr_linii,stopL,takeP,typ,deb)
elif typ_w=="sell":
typ=0
l_end,l_val=my_function_W(tab,nr_linii,stopL,takeP,typ,deb)
else:
print("error")
print(l_end,l_val)
l_end,l_val=my_function_W(train0zS,10,20,30,0,2)
l_end,l_val=my_function_W(train0zS,10,20,200,0,2)
l_end,l_val=my_function_W(train0zS,10,20,300,0,2)
l_end,l_val=my_function_W(train0zS,63,10,30,0,2)
l_end,l_val=my_function_W(train0zS,63,13,30,0,2)
l_end,l_val=my_function_W(train0zS,63,40,170,0,2)
l_end,l_val=my_function_W(train0zS,10,40,20,1,2)
l_end,l_val=my_function_W(train0zS,10,150,10,1,2)
l_end,l_val=my_function_W(train0zS,10,300,10,1,2)
l_end,l_val=my_function_W(train0zS,63,30,30,1,2)
l_end,l_val=my_function_W(train0zW,20,40,40,0,2)
l_end,l_val=my_function_W(train0zW,11,40,40,0,2)
l_end,l_val=my_function_W(train0zW,50,40,40,0,2)
l_end,l_val=my_function_W(train0zW,50,50,40,0,2)
l_end,l_val=my_function_W(train0zW,50,200,40,0,2)
l_end,l_val=my_function_W(train0zW,12,20,30,1,2)
l_end,l_val=my_function_W(train0zW,12,20,34,1,2)
l_end,l_val=my_function_W(train0zW,12,100,34,1,2)
l_end,l_val=my_function_W(train0zW,12,100,100,1,2)
l_end,l_val=my_function_W(train0zW,4,110,100,1,2)
l_end,l_val=my_function_W(train0zW,60,40,30,1,2)
* Kod źródłowy funkcji znaleźć w strefie PREMIUM.
© 2025 All Rights Reserved.
Cookie | Duration | Description |
---|---|---|
cookielawinfo-checkbox-analytics | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics". |
cookielawinfo-checkbox-functional | 11 months | The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional". |
cookielawinfo-checkbox-necessary | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary". |
cookielawinfo-checkbox-others | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other. |
cookielawinfo-checkbox-performance | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance". |
viewed_cookie_policy | 11 months | The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data. |
Kopiowanie wszystkich kodów tylko dla użytkowników PREMIUM.