Live Trading Bot Strategy In Python
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
🤖 ライブ取引ボットの紹介と戦略の実施
この段落では、Pythonを使用して市場でライブテストしている取引ボット「Equity」について紹介されています。現在のトレンドはプラスで、ほとんどの取引日がプラスとなっています。今日の目標は、以前の動画で紹介した戦略を実装し、市場でのライブテストを行うことです。スプレッド、手数料を考慮に入れた上で、Pythonでライブ取引ボットを構築する方法を学びます。最適化された戦略を選んで、市場に接続されたライブ取引ボットに実装し、数日間実行して結果を評価します。以前の動画で最適化とバックテストが行われた戦略を簡単に復習し、その結果についても触れています。
🔍 取引戦略の詳細とWandaへの接続
この段落では、使用する取引戦略の詳細と、Wandaプラットフォームへの接続方法について説明されています。戦略は、指数移動平均を用いて価格のトレンドを検出し、エボリューションズ・バンドを用いてエントリーポイントを選びます。WandaのAPIを使用し、アクセストークンとアカウントIDが必要です。さらに、获取したキャンドルデータをPandasデータフレームに変換し、技術指標を計算するための関数を定義しています。
📈 ライブ取引の実施と結果の分析
最後の段落では、ライブ取引を実施し、その結果を分析しています。取引ボットは、定義された条件に従ってポジションをオープンし、スプレッドやATRに基づいてストップロスとテイクプロフィットを計算します。CSVファイルをダウンロードして取引結果を分析し、权益のトレンドと日々の取引結果を確認します。この戦略をさらにテストし、1か月以上実行してより正確な分析を行う予定です。
Mindmap
Keywords
💡live trading bot
💡positive trend
💡exponential moving averages (EMA)
💡Bollinger bands
💡ATR (Average True Range)
💡backtest
💡spread
💡API (Application Programming Interface)
💡optimization
💡stop loss
💡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
hi and welcome back this is the equity
curve of a live trading bot that I'm
testing live on the market using python
as we can see it's a positive trend so
far and the daily aggregated results are
showing mostly positive trading days
today I will show you a live trading bot
that I'm running using the strategy we
introduced in the previous video so we
can test the strategy live on the market
considering the spread commissions and
all the trading fees we will see how to
build a live Trading B in Python so you
can run your automated trading
strategies live on the market if this is
your first time on this channel I
usually share the python codes so you
can download today's code from the link
in the description of this video and
here's our plan for this session first
we will pick an optimized and back
tested strategy second we will implement
this strategy into our live trading bot
that's connected to the market we will
let this strategy run for a few days and
finally we will assess the results so as
you can see this is a good way to test
any strategy you have on real conditions
and on new data the strategy we will use
today is the same we optimized in the
previous video so just a quick recap we
will detect the price trend relying on
the exponential moving averages and we
will choose our entry points using
Bolinger bands for more details please
check my previous video it shows how we
Define the functions related to this
strategy and how we optimized it and
back tested it for the best set of
parameters so in the back test the
results reached 61% in reach Returns on
data covering almost 35 days of trading
so it's almost 6 weeks period the stop
loss is defined in relation to the ATR
the average through range and the take
profit value is also related to the
stop-loss distance so indirectly to the
ATR as well again if you missed the
previous video with the details and the
optimization of the strategy I will
leave a link in the description so you
can go back and watch the video and make
sure that you didn't miss any
information going back to the
optimization back test we can notice
that for almost 20 days we didn't have
any profits it's only by the end of the
testing period that the strategy picked
up a positive return the reason I'm
showing this is to manage expectations
we need to be aware that some weeks and
some days might be negative and this is
the nature of trading however sometimes
we can get lucky and by the beginning of
the live test we would see immediately
some positive results without having to
wait for a few weeks or few days now
let's see the python code go through the
functions and run our live test but just
before we do this just before we start
part with the code walk through it would
be great if you will be testing the
strategy on different assets please
share some insights on your findings in
the comment section I'm curious how far
we can take this one so in the first
cell we're importing pandas pandas
technical analysis the uh scheduler
because this is what we'll be using to
launch this function or this bot once
every five minutes or once per day
depending on which time frame you
trading and we're using Wanda so we're
using the onas apis version 2.0 and so
on so we are importing all of this then
I'm defining the first function which is
the EMA signal and this function is
going to test if the fast EMA is below
the uh slow EMA then it's going to
return one which means we are in a
downtrend in the opposite case where the
fast EMA is above the slow EMA we return
two which is an uptrend in any other
case we return zero so that's the
function that's going to take the data
frame small data frame take the current
candles index and the number of back
candles to consider for the uh slow and
fast EMA so we need a slice of the data
frame including some back candles to be
able to compute the uh moving averages
the second function is the total signal
so it's going to compute the EMA signal
it's going to call the EMA signal in
case we have an uptrend it's going to
test if we are below or if a candle
closes below the lower edge of of the uh
Ballinger bands so if the current close
price of the current candle is less or
equal than the Ballinger band in the
current candle then this case we return
two which is our entry signal for a long
position in the opposite case we need a
downtrend and a closing or a candle
closing above the upper edge of the
Ballinger Bend and in this case we
return one which means it's a cell
signal or a short signal in any other
case we return zero so for this one just
based on the back test of the previous
video on this channel I'm using the
Binger bands with length 15 and standard
deviation of 1.5 you're wondering why we
use this please visit the previous video
we have some optimization parameters
that we are showing how it works and
what was the best result we obtained for
this set of
parameters okay now since we're using
Wanda to connect to the market you will
need two things the first one is an
access token and this one you can
usually generate it on the website or on
the platform of Wanda when you log into
your account and then you need your
account ID as well the account you're
trading from and now we can Define
couple more functions that we're going
to use for our training the first one is
get candles n so all this function does
it's going to query the platform of Enda
or your broker and it's going to
download the number of candles that
you're putting here as a parameter so I
can get the last 100 candles the last 50
candles or the last 20 candles depending
on the need of my trading system in this
case we're testing it just to query the
last three candles just to see if it's
working properly and in case we have
true here so for candle in candles print
is the string candle. bid. open. o
meaning open position greater than one
in this case um returning true so we
have true three times because we queried
three candles this is how I make sure
that I'm I'm connected through my access
token and account ID and everything is
working properly because these are used
here to grab the candles from Wanda's
platform so for now I know it's working
my token is working my account ID is
working and I can carry on with my code
then another function is countor opened
trades we're defining this function to
count the number of open trades because
we need to open one trade at a time so
we will need to count how many open
trades are coming Curr ly on the market
relating related to the account we are
using so far so this is why we've
defined this function it uses the API it
uses the access token the account ID and
it's going to request using client.
request R meaning trades. openen trades
for this particular account so the
account ID we're using here and it's
going to return the length of the
response so the length of these
trades now this function get candles
frame is going to get the last candles
so it's going to use the candle get
candles function the one that we've
predefined here it's going to transform
the data of the candles into a data
frame into pandas data frame with the
open close high and low prices so we're
just reformatting the data in in a way
in a shape and in a way that would be
easy to use just as a pandas data frame
and then we're adding the ATR as a new
column we're using technical analysis
the T ta pandascore ta we're also
Computing the fast moving average so of
length 30 the uh slow moving average of
length 50 the RSI in case needed of
length 10 and the Ballinger bands length
15 and standard deviation 1.5 so this is
where we pre-compute pretty much
everything we need for the candles as
classic technical indicators now we can
Define the trading job function so in
this function we're going to aggregate
everything together and execute our
trades so first of all we're going to
use the get candles frame I'm querying
70 candles so now we have a data frame
it's called DF Stream So a data frame
with candles open low high closing price
the RSI the ATR fast and slow moving
averages so everything is done using
this function and it's stored in the DF
stream variable then we can compute the
signal so we call the total signal we
feed the data frame as the parameter so
the current data frame with the last 70
candles then we indicate the index of
the current candle which is the length
of data frame Stream So DF stream minus
one and then this number seven is the
number of back candles to consider for
the signal so uh again it's more
detailed in the previous video but in
brief we're using a number of back
candles to test for a certain condition
so we're looking for the EMA fast to
detect the trend if it's below for
example the slow EMA we are in a
downtrend but it has to be below it for
a number of back candles like here it
has to be for seven consecutive back
candles this condition should be true
this is why we use the keyword all only
in this case we are detecting a
downtrend this is a way to avoid noisy
signals and now we can Define the stop
loss related to the ATR so it's equal to
1.1 time the current ATR value
and the take profit stop- loss ratio is
1.5 and I'm defining a maximum spread
which is 16 10 to Minus 5 this is only
related to the EUR US dollar and
depending on the account and the
Precision you're using for your trading
account but the reason I'm defining a
maximum spread is because I don't want
my bot to be trading when the spread is
extremely large so Above This maximum
spread limit the bot will not be trading
and this is a condition I included here
so for both short and long
positions and then to to compute the
spread the current spread live um
querying the open bid price and the open
ask price and I'm Computing the
difference between these two so the ask
and the bid price and this is what we
consider the spread then for long
positions so for buying positions we
need a stop-loss uh value so it's going
to be equal to the candle open bid price
minus the stop plus ATR distance minus
the spread this is where things become
different from the back test we've
introduced in the previous video we
didn't consider the spread and here we
are accounting for the spread we are
increasing The stop-loss Distance by the
amount of the spread same thing for the
short positions we need a stop-loss
value which is equal to the open uh
asking price and because it's the
highest value between the bid and ask
plus the stop- loss related to the ATR
so the stop loss distance Plus the
spread value now same thing for the take
profit the take profit is equal to the
open ask Price Plus the stop-loss ATR
value distance times the takeprofit
stoploss ratio plus the spread again and
for the short positions take profit is
the candle open price but it's the
bidding price minus the stop loss ATR um
distance times the takeprofit stoploss
ratio minus the spread and this is it
basically so once we have defined all of
these parameters now we just have to
call the client API using the access
token and if the signal is equal to one
and the count of currently open trades
is equal to zero meaning we don't have
any open trades on the market and at the
same time the spread is less than the
maximum spread limit we can open a short
position with the um minus 3,000 units
we're using the Euro US doll using the
take profit um value for the short
positions and the stop stop loss value
for the short positions as well and
whenever we have a Buy Signal we use
positive units so plus $3,000 us using
the take profit the buy take profit or
the long take profit and the uh buy stop
loss or the long stop loss for the sake
of Simplicity we're using fixed lot
sizing so we're not using any function
or any formulas to change the lot sizes
here depending on the trading conditions
just for the sake of this video but it's
working well so far we're going to let
it run on the market and see what it
gives then we can run a Chron scheduler
function adore job launching the trading
job function which is defined right here
and U it's from Monday until Friday from
Midnight until midnight and it's going
to run once probing the market once
every 5 minutes so at minute 1 minute 6
and so on why because we're using 5
minutes time frame strategy so there's
no need to run more than one time every
5 minutes nothing's going to change on
the market on the 5 minutes time frame
so I managed to download a CSV file with
the uh trades done so far it wasn't
running for very long time now so it's
just few days but before that I've been
trying to um tune some parameters
especially regarding the U experimenting
on the spread but now it seems to be
running in a stable way so I downloaded
the data I did some quick analysis and
this is the equity we can see that we
have some drow Downs some ups and downs
but in general things are climbing in
the positive trend now these are in
dollars here so we can see how it's
climbing from $144 as an equity to 152
dropping down to 144 and so on today was
a good day it's
$158 I think we've made $10 in one day
if I'm not mistaken and this is the
equity by trade so these are the trades
these are not the days this is the a
number of Trades and this one right here
is um the aggregated results of Trades
per day so this is positive in dollars
again so this is around $7 that day this
is- $6 this is plus2 and a half or plus
three uh plus $1
$2 and uh today was around a bit above
$8 now I'm intending to let this
strategy run for a full month at least
so we can get gather enough data for a
better analysis and I will get back to
you in another video about it and show
you where this strategy landed so that's
all for today I hope you guys are happy
with this one it looks fine from my side
I don't know if you are enjoying this as
much as I am but if you decide to
venture and try this code live on
different assets again please share your
findings it would be nice to know how
this strategy performs on different
markets until our next one trade safe
and see you next time
Browse More Related Video
5.0 / 5 (0 votes)