Skip to content

Tutorial 001¤

This tutorial tend to help understand basic of lettrade

Main object¤

  • DataFeed: is data for your bot to running, this is a implement of pandas.DataFrame, so it have all pandas.DataFrame feature
  • Strategy: is base Strategy of your bot, implement your bot with this as base class
  • Exchange: is base Exchange of your bot, where control trading event and logic
  • Account: is base class, where your bot will control account balance, equity, risk, size, commission...
  • Commander: is base class, where your bot will communicate with you in realtime
  • Brain: is brain of your bot, it just internal object, so don't need to care about it

Init your bot¤

Init DataFeed¤

from lettrade import DataFeed
import yfinance as yf

# Your pandas.Dataframe
msft = yf.Ticker("MSFT")
df = msft.history(period="1mo")

print("pandas DataFrame:\n", df.tail())

data = DataFeed(data=df, name="MSFT")

print("LetTrade DataFeed:\n", data.tail())
pandas DataFrame:
                                  Open        High         Low       Close    Volume  Dividends  Stock Splits
Date                                                                                                        
2024-05-15 00:00:00-04:00  417.899994  423.809998  417.269989  423.079987  22239500       0.75           0.0
2024-05-16 00:00:00-04:00  421.799988  425.420013  420.350006  420.989990  17530100       0.00           0.0
2024-05-17 00:00:00-04:00  422.540009  422.920013  418.029999  420.209991  15352200       0.00           0.0
2024-05-20 00:00:00-04:00  420.209991  426.769989  419.989990  425.339996  16272100       0.00           0.0
2024-05-21 00:00:00-04:00  426.829987  432.970001  424.850006  429.040009  21428700       0.00           0.0
LetTrade DataFeed:
                         Date        Open        High         Low       Close    Volume  Dividends  Stock Splits
17 2024-05-15 00:00:00-04:00  417.899994  423.809998  417.269989  423.079987  22239500       0.75           0.0
18 2024-05-16 00:00:00-04:00  421.799988  425.420013  420.350006  420.989990  17530100       0.00           0.0
19 2024-05-17 00:00:00-04:00  422.540009  422.920013  418.029999  420.209991  15352200       0.00           0.0
20 2024-05-20 00:00:00-04:00  420.209991  426.769989  419.989990  425.339996  16272100       0.00           0.0
21 2024-05-21 00:00:00-04:00  426.829987  432.970001  424.850006  429.040009  21428700       0.00           0.0

Strategy implement¤

class SmaCross(Strategy):
    ema1_window = 9
    ema2_window = 21

Indicator/signal implement¤

import talib.abstract as ta
from lettrade import DataFeed, indicator as i

def indicators(self, df: DataFeed):
    df["ema1"] = ta.EMA(df, timeperiod=self.ema1_window)
    df["ema2"] = ta.EMA(df, timeperiod=self.ema2_window)

    df["signal_ema_crossover"] = i.crossover(df.ema1, df.ema2)
    df["signal_ema_crossunder"] = i.crossunder(df.ema1, df.ema2)