Live Trading Bot Strategy In Python

CodeTrading
1 Feb 202414:57

Summary

TLDRこの動画スクリプトでは、Pythonを使用して市場でライブテストを行う自動売買ボットのテスト結果を共有しています。正面のトレンドが見られ、日次での集約結果は主にプラスの取引日を示しています。説明された戦略をライブ市場でテストし、スプレッド、手数料を考慮に入れた実際の取引を行ったボットの構築方法を紹介します。最適化されたストラテジーの選択、実装、数日間の運用、結果の評価が行われます。ストラテジーは、指数移動平均に基づいて価格トレンドを検出し、エントリーポイントはボリンジャーバンドを使用して選択します。バックテストでは、約35日間のデータで61%のリーチリターンが得られましたが、20日間ほどの利益がなかったこともありました。ライブテストの結果は、期待を管理し、トレーディングの性質を受け入れることを示しています。

Takeaways

  • 📈 現在テストしている自動売買ボットはPythonを使用して市場でライブ取引を行っています。
  • 🌟 テストしている戦略は、前回の動画で最適化とバックテスト済みで、約35日間の取引データに基づいて61%のリーチリターンを達成しています。
  • 🛠️ 戦略は指数移動平均を利用して価格トレンドを検出し、エントリーポイントにはボリンジャーバンドを選んでいます。
  • 🔧 停止損失はATR(平均トレンド範囲)に関連し、利益確定値も停止損失距離に関連しているため、間接的にATRにも関連しています。
  • 🔄 テスト期間の約20日間は利益がなく、最終的にテスト期間の終盤でポジティブなリターンが得られました。
  • 📊 ライブテストでは、スプレッド、手数料、取引手数料などの市場要因を考慮しています。
  • 💡 動画の説明リンクから当日のコードをダウンロードできます。
  • 🔍 ライブテスト中は、WandasのAPIを使用して市場に接続し、取引を実行しています。
  • 📈 取引ロットは固定しており、取引条件によってロットサイズを変更する機能や公式は使用していません。
  • 🔄 5分足の時間枠戦略を使用しているため、5分ごとに市場をチェックする必要があります。
  • 📊 少数の取引データに基づく初期分析では、総資産は正のトレンドにあり、利益を上げています。

Q & A

  • この動画で紹介されている取引ボットの主な機能は何ですか?

    -この取引ボットは、Pythonを使用して市場でリアルタイムに取引を行います。また、ストラテジーを導入して市場でライブテストを行い、スプレッド、手数料を考慮して実際の取引環境で自動化された取引戦略を実行します。

  • 使用されているストラテジーは何ですか?

    -使用されているストラテジーは、指数移動平均を利用して価格のトレンドを検出し、エボリューションズ・バンドを用いてエントリーポイントを選定します。

  • エポック(EP)とスプレッドに対する損切りと利益確定の設定はどのようになされていますか?

    -損切りはATR(平均トレンド範囲)に基づいて設定されており、利益確定は損切り距離に関連して間接的にATRにも関連しています。

  • バックテストの結果はどのようでしたか?

    -バックテストでは、35日間にわたる取引データに基づいて61%のリーチリターンが達成されました。しかし、約20日間は利益が得られず、テスト期間の終盤でのみプラスのリターンが得られました。

  • 取引ボットを実行するために必要な情報は何ですか?

    -取引ボットを実行するためには、アクセストークンと取引アカウントIDが必要です。これらの情報は、Wandaのウェブサイトやプラットフォームで生成することができます。

  • 取引ボットの実行間隔は何ですか?

    -取引ボットは、スケジューラを使用して5分ごとに市場をチェックし、取引を行います。

  • 取引ボットが現在開いている取引をカウントする方法は何ですか?

    -取引ボットは、APIを使用して現在開いている取引の数をカウントし、一度に一つの取引のみを行います。

  • 取引のスプレッドが大きい場合、取引ボットはどのように動作しますか?

    -取引のスプレッドが定義された最大スプレッドを超える場合、取引ボットは取引を行わずに待機します。

  • 取引信号が発生したとき、取引ボットはどのように取引を開始しますか?

    -取引信号が発生し、現在市場に開いている取引が存在し、かつスプレッドが最大スプレッド以下の場合、取引ボットは指定されたロットサイズで取引を開始します。

  • この取引ボットのパフォーマンスを評価するために、どの期間のデータを収集する予定ですか?

    -この取引ボットのパフォーマンスを評価するために、少なくとも1ヶ月間のデータを収集し、より正確な分析を行う予定です。

  • この動画の最後に提供されているコードをダウンロードするにはどうすればいいですか?

    -この動画の最後に提供されているコードをダウンロードするには、動画の説明欄にあるリンクをクリックしてダウンロードすることができます。

Outlines

00:00

🤖 ライブ取引ボットの紹介と戦略の実施

この段落では、Pythonを使用して市場でライブテストしている取引ボット「Equity」について紹介されています。現在のトレンドはプラスで、ほとんどの取引日がプラスとなっています。今日の目標は、以前の動画で紹介した戦略を実装し、市場でのライブテストを行うことです。スプレッド、手数料を考慮に入れた上で、Pythonでライブ取引ボットを構築する方法を学びます。最適化された戦略を選んで、市場に接続されたライブ取引ボットに実装し、数日間実行して結果を評価します。以前の動画で最適化とバックテストが行われた戦略を簡単に復習し、その結果についても触れています。

05:02

🔍 取引戦略の詳細とWandaへの接続

この段落では、使用する取引戦略の詳細と、Wandaプラットフォームへの接続方法について説明されています。戦略は、指数移動平均を用いて価格のトレンドを検出し、エボリューションズ・バンドを用いてエントリーポイントを選びます。WandaのAPIを使用し、アクセストークンとアカウントIDが必要です。さらに、获取したキャンドルデータをPandasデータフレームに変換し、技術指標を計算するための関数を定義しています。

10:03

📈 ライブ取引の実施と結果の分析

最後の段落では、ライブ取引を実施し、その結果を分析しています。取引ボットは、定義された条件に従ってポジションをオープンし、スプレッドやATRに基づいてストップロスとテイクプロフィットを計算します。CSVファイルをダウンロードして取引結果を分析し、权益のトレンドと日々の取引結果を確認します。この戦略をさらにテストし、1か月以上実行してより正確な分析を行う予定です。

Mindmap

Keywords

💡live trading bot

「live trading bot」とは、市場で実際の取引を行う自動化されたプログラムのことです。この動画では、Pythonを使用してテストされているlive trading botが紹介されており、そのbotは市場の動向をリアルタイムで追跡し、自動的に取引を行うことができます。

💡positive trend

「positive trend」とは、株価や通貨などの価格が上昇しているトレンドを指します。この動画では、live trading botが市場のpositive trendを認識し、それを基に取引を行うことが示されています。

💡exponential moving averages (EMA)

「exponential moving averages (EMA)」とは、価格データに基づく移動平均線の一つで、最近の価格変化に比较大的重みを置く技術的な分析ツールです。この動画では、EMAを用いて価格のトレンドを判断し、取引のエントリーポイントを決定する戦略が説明されています。

💡Bollinger bands

「Bollinger bands」とは、株価の変動範囲を示すための技術的な分析ツールで、中心線(移動平均線)を基準に上下にバンドを設置します。この動画では、Bollinger bandsを使用して、取引のエントリーポイントを決定する戦略が紹介されています。

💡ATR (Average True Range)

「ATR (Average True Range)」とは、市場の変動性を示す指標で、過去14日間の平均的な価格範囲から計算されます。この動画では、ATRを用いて、stop-loss(損切り)およびtake-profit(利益確定)の基準を設定する方法が説明されています。

💡backtest

「backtest」とは、過去のデータに基づいて取引戦略をテストすることを指します。この動画では、最適なバックテスト戦略を選んで、それをlive trading botに適用する方法が説明されています。

💡spread

「spread」とは、金融市場において、買いと売りの価格の差を指します。この動画では、spreadが大きい場合、取引を行わないという条件が設定されていることが示されています。

💡API (Application Programming Interface)

「API (Application Programming Interface)」とは、ソフトウェア間で通信を行うためのインターフェースです。この動画では、APIを使用して、取引データの取得や取引の実行を行います。

💡optimization

「optimization」とは、取引戦略やシステムを最適化することです。この動画では、最適なパラメーターを選択して、バックテスト戦略を改善する方法が説明されています。

💡stop loss

「stop loss」とは、損切りを行うための取引の指令です。価格が一定のレベルまで下がった場合に、自動的に売却されることで損失を制限します。この動画では、ATRを用いてstop lossを設定する方法が説明されています。

💡take profit

「take profit」とは、利益確定を行うための取引の指令です。価格が一定のレベルに達した場合に、自動的に売却され利益を確定します。この動画では、stop loss distanceに基づいてtake profitを設定する方法が説明されています。

Highlights

The video presents a live trading bot using Python, showcasing a positive trend in the market.

The trading bot is tested live on the market, taking into account spread, commissions, and trading fees.

The strategy used is based on exponential moving averages and Bolinger bands, optimized and back-tested in a previous video.

The back-test results showed a 61% return on investment over 35 days of trading.

The stop-loss and take-profit values are defined in relation to the ATR (Average True Range).

The trading bot is implemented to run on the market, with the plan to assess results after a few days.

The video encourages viewers to share insights on testing the strategy on different assets.

The Python code is available for download, allowing viewers to run the automated trading strategies themselves.

The trading bot uses the Wanda API for market connection, requiring an access token and account ID.

Functions are defined for EMA signal detection, total signal computation, and trade execution.

The bot includes a mechanism to count open trades, ensuring only one trade is open at a time.

The bot accounts for spread in its trading calculations, avoiding trades when the spread is extremely large.

The trading bot uses a fixed lot size for simplicity, not adjusting based on trading conditions.

The bot is scheduled to run once every 5 minutes, suitable for a 5-minute time frame strategy.

Initial live test results show a general positive trend, with some days of profit and others of loss.

The video creator plans to run the strategy for a full month to gather enough data for a more comprehensive analysis.

Viewers are encouraged to share their findings when testing the code on different assets.

Transcripts

play00:00

hi and welcome back this is the equity

play00:03

curve of a live trading bot that I'm

play00:05

testing live on the market using python

play00:08

as we can see it's a positive trend so

play00:10

far and the daily aggregated results are

play00:12

showing mostly positive trading days

play00:15

today I will show you a live trading bot

play00:18

that I'm running using the strategy we

play00:19

introduced in the previous video so we

play00:22

can test the strategy live on the market

play00:24

considering the spread commissions and

play00:26

all the trading fees we will see how to

play00:28

build a live Trading B in Python so you

play00:31

can run your automated trading

play00:32

strategies live on the market if this is

play00:34

your first time on this channel I

play00:36

usually share the python codes so you

play00:38

can download today's code from the link

play00:40

in the description of this video and

play00:42

here's our plan for this session first

play00:44

we will pick an optimized and back

play00:46

tested strategy second we will implement

play00:48

this strategy into our live trading bot

play00:50

that's connected to the market we will

play00:52

let this strategy run for a few days and

play00:54

finally we will assess the results so as

play00:57

you can see this is a good way to test

play00:58

any strategy you have on real conditions

play01:01

and on new data the strategy we will use

play01:04

today is the same we optimized in the

play01:06

previous video so just a quick recap we

play01:08

will detect the price trend relying on

play01:10

the exponential moving averages and we

play01:12

will choose our entry points using

play01:14

Bolinger bands for more details please

play01:17

check my previous video it shows how we

play01:19

Define the functions related to this

play01:21

strategy and how we optimized it and

play01:23

back tested it for the best set of

play01:25

parameters so in the back test the

play01:27

results reached 61% in reach Returns on

play01:30

data covering almost 35 days of trading

play01:33

so it's almost 6 weeks period the stop

play01:35

loss is defined in relation to the ATR

play01:37

the average through range and the take

play01:39

profit value is also related to the

play01:41

stop-loss distance so indirectly to the

play01:43

ATR as well again if you missed the

play01:46

previous video with the details and the

play01:48

optimization of the strategy I will

play01:50

leave a link in the description so you

play01:52

can go back and watch the video and make

play01:54

sure that you didn't miss any

play01:55

information going back to the

play01:57

optimization back test we can notice

play01:59

that for almost 20 days we didn't have

play02:01

any profits it's only by the end of the

play02:03

testing period that the strategy picked

play02:05

up a positive return the reason I'm

play02:07

showing this is to manage expectations

play02:09

we need to be aware that some weeks and

play02:11

some days might be negative and this is

play02:13

the nature of trading however sometimes

play02:15

we can get lucky and by the beginning of

play02:17

the live test we would see immediately

play02:19

some positive results without having to

play02:21

wait for a few weeks or few days now

play02:23

let's see the python code go through the

play02:25

functions and run our live test but just

play02:27

before we do this just before we start

play02:29

part with the code walk through it would

play02:31

be great if you will be testing the

play02:33

strategy on different assets please

play02:35

share some insights on your findings in

play02:38

the comment section I'm curious how far

play02:40

we can take this one so in the first

play02:42

cell we're importing pandas pandas

play02:44

technical analysis the uh scheduler

play02:47

because this is what we'll be using to

play02:48

launch this function or this bot once

play02:51

every five minutes or once per day

play02:53

depending on which time frame you

play02:55

trading and we're using Wanda so we're

play02:57

using the onas apis version 2.0 and so

play03:00

on so we are importing all of this then

play03:04

I'm defining the first function which is

play03:06

the EMA signal and this function is

play03:08

going to test if the fast EMA is below

play03:11

the uh slow EMA then it's going to

play03:14

return one which means we are in a

play03:16

downtrend in the opposite case where the

play03:19

fast EMA is above the slow EMA we return

play03:22

two which is an uptrend in any other

play03:25

case we return zero so that's the

play03:26

function that's going to take the data

play03:29

frame small data frame take the current

play03:31

candles index and the number of back

play03:33

candles to consider for the uh slow and

play03:36

fast EMA so we need a slice of the data

play03:40

frame including some back candles to be

play03:42

able to compute the uh moving averages

play03:44

the second function is the total signal

play03:47

so it's going to compute the EMA signal

play03:50

it's going to call the EMA signal in

play03:52

case we have an uptrend it's going to

play03:54

test if we are below or if a candle

play03:57

closes below the lower edge of of the uh

play04:00

Ballinger bands so if the current close

play04:03

price of the current candle is less or

play04:06

equal than the Ballinger band in the

play04:08

current candle then this case we return

play04:10

two which is our entry signal for a long

play04:13

position in the opposite case we need a

play04:15

downtrend and a closing or a candle

play04:18

closing above the upper edge of the

play04:21

Ballinger Bend and in this case we

play04:23

return one which means it's a cell

play04:24

signal or a short signal in any other

play04:26

case we return zero so for this one just

play04:29

based on the back test of the previous

play04:30

video on this channel I'm using the

play04:32

Binger bands with length 15 and standard

play04:35

deviation of 1.5 you're wondering why we

play04:39

use this please visit the previous video

play04:41

we have some optimization parameters

play04:43

that we are showing how it works and

play04:45

what was the best result we obtained for

play04:47

this set of

play04:49

parameters okay now since we're using

play04:51

Wanda to connect to the market you will

play04:53

need two things the first one is an

play04:55

access token and this one you can

play04:57

usually generate it on the website or on

play04:59

the platform of Wanda when you log into

play05:02

your account and then you need your

play05:03

account ID as well the account you're

play05:05

trading from and now we can Define

play05:08

couple more functions that we're going

play05:09

to use for our training the first one is

play05:11

get candles n so all this function does

play05:15

it's going to query the platform of Enda

play05:18

or your broker and it's going to

play05:19

download the number of candles that

play05:22

you're putting here as a parameter so I

play05:24

can get the last 100 candles the last 50

play05:27

candles or the last 20 candles depending

play05:29

on the need of my trading system in this

play05:32

case we're testing it just to query the

play05:35

last three candles just to see if it's

play05:37

working properly and in case we have

play05:40

true here so for candle in candles print

play05:44

is the string candle. bid. open. o

play05:48

meaning open position greater than one

play05:51

in this case um returning true so we

play05:54

have true three times because we queried

play05:56

three candles this is how I make sure

play05:59

that I'm I'm connected through my access

play06:01

token and account ID and everything is

play06:03

working properly because these are used

play06:05

here to grab the candles from Wanda's

play06:09

platform so for now I know it's working

play06:12

my token is working my account ID is

play06:14

working and I can carry on with my code

play06:17

then another function is countor opened

play06:21

trades we're defining this function to

play06:23

count the number of open trades because

play06:25

we need to open one trade at a time so

play06:27

we will need to count how many open

play06:28

trades are coming Curr ly on the market

play06:30

relating related to the account we are

play06:32

using so far so this is why we've

play06:35

defined this function it uses the API it

play06:37

uses the access token the account ID and

play06:40

it's going to request using client.

play06:42

request R meaning trades. openen trades

play06:46

for this particular account so the

play06:48

account ID we're using here and it's

play06:50

going to return the length of the

play06:53

response so the length of these

play06:55

trades now this function get candles

play06:59

frame is going to get the last candles

play07:02

so it's going to use the candle get

play07:04

candles function the one that we've

play07:06

predefined here it's going to transform

play07:08

the data of the candles into a data

play07:10

frame into pandas data frame with the

play07:12

open close high and low prices so we're

play07:16

just reformatting the data in in a way

play07:19

in a shape and in a way that would be

play07:21

easy to use just as a pandas data frame

play07:24

and then we're adding the ATR as a new

play07:26

column we're using technical analysis

play07:28

the T ta pandascore ta we're also

play07:32

Computing the fast moving average so of

play07:34

length 30 the uh slow moving average of

play07:37

length 50 the RSI in case needed of

play07:41

length 10 and the Ballinger bands length

play07:44

15 and standard deviation 1.5 so this is

play07:48

where we pre-compute pretty much

play07:49

everything we need for the candles as

play07:51

classic technical indicators now we can

play07:54

Define the trading job function so in

play07:57

this function we're going to aggregate

play07:58

everything together and execute our

play08:01

trades so first of all we're going to

play08:03

use the get candles frame I'm querying

play08:05

70 candles so now we have a data frame

play08:09

it's called DF Stream So a data frame

play08:11

with candles open low high closing price

play08:14

the RSI the ATR fast and slow moving

play08:17

averages so everything is done using

play08:19

this function and it's stored in the DF

play08:22

stream variable then we can compute the

play08:24

signal so we call the total signal we

play08:27

feed the data frame as the parameter so

play08:30

the current data frame with the last 70

play08:32

candles then we indicate the index of

play08:35

the current candle which is the length

play08:36

of data frame Stream So DF stream minus

play08:39

one and then this number seven is the

play08:43

number of back candles to consider for

play08:45

the signal so uh again it's more

play08:49

detailed in the previous video but in

play08:51

brief we're using a number of back

play08:52

candles to test for a certain condition

play08:55

so we're looking for the EMA fast to

play08:58

detect the trend if it's below for

play09:00

example the slow EMA we are in a

play09:02

downtrend but it has to be below it for

play09:06

a number of back candles like here it

play09:08

has to be for seven consecutive back

play09:11

candles this condition should be true

play09:13

this is why we use the keyword all only

play09:15

in this case we are detecting a

play09:17

downtrend this is a way to avoid noisy

play09:20

signals and now we can Define the stop

play09:22

loss related to the ATR so it's equal to

play09:25

1.1 time the current ATR value

play09:30

and the take profit stop- loss ratio is

play09:32

1.5 and I'm defining a maximum spread

play09:35

which is 16 10 to Minus 5 this is only

play09:38

related to the EUR US dollar and

play09:40

depending on the account and the

play09:41

Precision you're using for your trading

play09:43

account but the reason I'm defining a

play09:45

maximum spread is because I don't want

play09:48

my bot to be trading when the spread is

play09:51

extremely large so Above This maximum

play09:54

spread limit the bot will not be trading

play09:56

and this is a condition I included here

play09:59

so for both short and long

play10:02

positions and then to to compute the

play10:05

spread the current spread live um

play10:08

querying the open bid price and the open

play10:12

ask price and I'm Computing the

play10:14

difference between these two so the ask

play10:16

and the bid price and this is what we

play10:18

consider the spread then for long

play10:20

positions so for buying positions we

play10:22

need a stop-loss uh value so it's going

play10:25

to be equal to the candle open bid price

play10:28

minus the stop plus ATR distance minus

play10:31

the spread this is where things become

play10:33

different from the back test we've

play10:34

introduced in the previous video we

play10:36

didn't consider the spread and here we

play10:39

are accounting for the spread we are

play10:40

increasing The stop-loss Distance by the

play10:43

amount of the spread same thing for the

play10:45

short positions we need a stop-loss

play10:47

value which is equal to the open uh

play10:49

asking price and because it's the

play10:52

highest value between the bid and ask

play10:54

plus the stop- loss related to the ATR

play10:57

so the stop loss distance Plus the

play10:59

spread value now same thing for the take

play11:01

profit the take profit is equal to the

play11:04

open ask Price Plus the stop-loss ATR

play11:08

value distance times the takeprofit

play11:10

stoploss ratio plus the spread again and

play11:13

for the short positions take profit is

play11:16

the candle open price but it's the

play11:18

bidding price minus the stop loss ATR um

play11:21

distance times the takeprofit stoploss

play11:23

ratio minus the spread and this is it

play11:27

basically so once we have defined all of

play11:29

these parameters now we just have to

play11:31

call the client API using the access

play11:33

token and if the signal is equal to one

play11:36

and the count of currently open trades

play11:38

is equal to zero meaning we don't have

play11:40

any open trades on the market and at the

play11:43

same time the spread is less than the

play11:45

maximum spread limit we can open a short

play11:48

position with the um minus 3,000 units

play11:51

we're using the Euro US doll using the

play11:54

take profit um value for the short

play11:57

positions and the stop stop loss value

play11:59

for the short positions as well and

play12:01

whenever we have a Buy Signal we use

play12:04

positive units so plus $3,000 us using

play12:07

the take profit the buy take profit or

play12:09

the long take profit and the uh buy stop

play12:13

loss or the long stop loss for the sake

play12:16

of Simplicity we're using fixed lot

play12:19

sizing so we're not using any function

play12:22

or any formulas to change the lot sizes

play12:24

here depending on the trading conditions

play12:26

just for the sake of this video but it's

play12:28

working well so far we're going to let

play12:29

it run on the market and see what it

play12:32

gives then we can run a Chron scheduler

play12:35

function adore job launching the trading

play12:38

job function which is defined right here

play12:42

and U it's from Monday until Friday from

play12:45

Midnight until midnight and it's going

play12:47

to run once probing the market once

play12:49

every 5 minutes so at minute 1 minute 6

play12:52

and so on why because we're using 5

play12:55

minutes time frame strategy so there's

play12:56

no need to run more than one time every

play12:59

5 minutes nothing's going to change on

play13:01

the market on the 5 minutes time frame

play13:03

so I managed to download a CSV file with

play13:07

the uh trades done so far it wasn't

play13:09

running for very long time now so it's

play13:12

just few days but before that I've been

play13:14

trying to um tune some parameters

play13:16

especially regarding the U experimenting

play13:19

on the spread but now it seems to be

play13:21

running in a stable way so I downloaded

play13:24

the data I did some quick analysis and

play13:27

this is the equity we can see that we

play13:29

have some drow Downs some ups and downs

play13:31

but in general things are climbing in

play13:34

the positive trend now these are in

play13:36

dollars here so we can see how it's

play13:39

climbing from $144 as an equity to 152

play13:43

dropping down to 144 and so on today was

play13:45

a good day it's

play13:48

$158 I think we've made $10 in one day

play13:51

if I'm not mistaken and this is the

play13:53

equity by trade so these are the trades

play13:57

these are not the days this is the a

play13:58

number of Trades and this one right here

play14:02

is um the aggregated results of Trades

play14:06

per day so this is positive in dollars

play14:09

again so this is around $7 that day this

play14:12

is- $6 this is plus2 and a half or plus

play14:16

three uh plus $1

play14:19

$2 and uh today was around a bit above

play14:23

$8 now I'm intending to let this

play14:25

strategy run for a full month at least

play14:28

so we can get gather enough data for a

play14:30

better analysis and I will get back to

play14:31

you in another video about it and show

play14:33

you where this strategy landed so that's

play14:36

all for today I hope you guys are happy

play14:38

with this one it looks fine from my side

play14:40

I don't know if you are enjoying this as

play14:42

much as I am but if you decide to

play14:44

venture and try this code live on

play14:46

different assets again please share your

play14:49

findings it would be nice to know how

play14:51

this strategy performs on different

play14:53

markets until our next one trade safe

play14:55

and see you next time

Rate This

5.0 / 5 (0 votes)

Related Tags
自動売買Python戦略最適化ライブテストバックテストExponential移動平均Bollinger Bands取引手数料市場分析株式投資技術解析
Do you need a summary in English?