Giter VIP home page Giter VIP logo

Comments (21)

happydasch avatar happydasch commented on June 14, 2024

for the stop trail order, this should basically work, but you need to provide a tradeID as a parameter when creating the order. The tradeID needs to be the oanda tradeID to close when the stop trail order executes.

For the closing, what exactly do you mean with that? Do you want to close an open position? Then you do either a market or a limit order to close the position, for closing open orders, you could use order_cancel method of the store.

from btoandav20.

einnairo avatar einnairo commented on June 14, 2024

Thanks for the tips on trailing stop.

For my 'Closing' question, I mean the call in backtrader self.close(). This call in backtrader will not work right? Oanda api will not be able to do that right?

I like to ask for your advise too, for forex how do you setcommision? Is this correct for oanda assuming margin 5%, 20 times leverage?
cerebro.broker.setcommission(leverage=20, mult=20.0, stocklike=False, automargin=0.05)
I am trying to put fixed commision in too which is beleive is 5$ under 1mil size. But cannot get it to work.
Possible to share what your setcommission line is (for backtesting) ?

from btoandav20.

happydasch avatar happydasch commented on June 14, 2024

For my 'Closing' question, I mean the call in backtrader self.close(). This call in backtrader will not work right? Oanda api will not be able to do that right?

Actually it may work, (but I did not test it) Since the close method will create a order in the other direction (when you have a long position, it should create a sell position with the same amount).

from btoandav20.

happydasch avatar happydasch commented on June 14, 2024

I like to ask for your advise too, for forex how do you setcommision? Is this correct for oanda assuming margin 5%, 20 times leverage?
cerebro.broker.setcommission(leverage=20, mult=20.0, stocklike=False, automargin=0.05)
I am trying to put fixed commision in too which is beleive is 5$ under 1mil size. But cannot get it to work.
Possible to share what your setcommission line is (for backtesting) ?

I use just a fixed value for that. Maybe this may help you:

https://community.backtrader.com/topic/525/forex-commission-scheme/2

from btoandav20.

happydasch avatar happydasch commented on June 14, 2024

I added the forex commission scheme with small changes to btoandav20

from btoandav20.

einnairo avatar einnairo commented on June 14, 2024

Thanks again.

Can I ask again, this is my really simple code. but getting a error:
print('Timezone from ContractDetails: {}'.format(self.data0.contractdetails.m_timeZoneId))
AttributeError: 'dict' object has no attribute 'm_timeZoneId'

I tried with # tz='US/Eastern' and without. Need your expert help! Thanks alot
Can I ask if runonce=False should work right?

def main():
    StoreCls = btoandav20.stores.OandaV20Store
    BrokerCls = btoandav20.brokers.OandaV20Broker
    DataCls = btoandav20.feeds.OandaV20Data

    cerebro = bt.Cerebro()

    storekwargs = dict(token='abcetcetcetc', account='111-113-1111111-111', practice=True)
    store = StoreCls(**storekwargs)

    broker = BrokerCls(**storekwargs)
    cerebro.setbroker(broker)

    data0 = store.getdata(dataname='EUR_USD', timeframe=bt.TimeFrame.Minutes, compression=5, qcheck=1, backfill_start=True, backfill=False)  # tz='US/Eastern'
    cerebro.adddata(data0)
    cerebro.resampledata(data0, timeframe=bt.TimeFrame.Days, compression=1)


    cerebro.addstrategy(BaseStrategy)

    strat = cerebro.run(maxcpus=2, exactbars=-1, runonce=False)
    print(cerebro.broker.getvalue())

from btoandav20.

happydasch avatar happydasch commented on June 14, 2024

this should help you:


    import pytz

    data0 = store.getdata(dataname='EUR_USD', timeframe=bt.TimeFrame.Minutes, compression=5, qcheck=1, backfill_start=True, backfill=False, tz=pytz.timezone('US/Eastern'))  # tz='US/Eastern'
    

from btoandav20.

happydasch avatar happydasch commented on June 14, 2024

print('Timezone from ContractDetails: {}'.format(self.data0.contractdetails.m_timeZoneId))
AttributeError: 'dict' object has no attribute 'm_timeZoneId'

This will not work ...

Print out the contract details to see what values are available.

for EURUSD:

{'minimumTradeSize': '1.0', 'displayName': u'EUR/USD', 'name': u'EUR_USD', 'displayPrecision': 5, 'maximumTrailingStopDistance': '1.0', 'minimumTrailingStopDistance': '0.0005', 'marginRate': '0.025', 'tradeUnitsPrecision': 0, 'pipLocation': -4, 'maximumOrderUnits': '100000000.0', 'maximumPositionSize': '0.0', 'type': u'CURRENCY'}

from btoandav20.

einnairo avatar einnairo commented on June 14, 2024

Hi! Thanks for your help. Again I really appreciate how responsive you are to questions.
its been difficult working with backtrader although a really good package.

I have changed this line to just the below instead and it works now.
print('Timezone from ContractDetails: {}'.format(self.data0.contractdetails))

my printout is pretty similar to yours except I have a higher margin required.
Timezone from ContractDetails: {'name': 'EUR_USD', 'type': 'CURRENCY', 'displayName': 'EUR/USD', 'pipLocation': -4, 'displayPrecision': 5, 'tradeUnitsPrecision': 0, 'minimumTradeSize': '1.0', 'maximumTrailingStopDistance': '1.0', 'minimumTrailingStopDistance': '0.0005', 'maximumPositionSize': '0.0', 'maximumOrderUnits': '100000000.0', 'marginRate': '0.05'}

from btoandav20.

einnairo avatar einnairo commented on June 14, 2024

can I check as well if order_target_percent() and oco works with this package?

from btoandav20.

einnairo avatar einnairo commented on June 14, 2024

Ok I am trying to test the order_target_percent() out but my live data is in ticks it seems.
How can I get 5 minute candles for data0 and for resampled data at 1 day for data1?
I have tried timeframe=bt.TimeFrame.Minutes, compression=5 but they still come in ticks.

data0 = store.getdata(dataname='EUR_USD', timeframe=bt.TimeFrame.TFrame(TIMEFRAMES[1]), compression=5, qcheck=300, backfill_start=True, backfill=False, tz='America/New_York') 

cerebro.adddata(data0)

cerebro.resampledata(data0, timeframe=bt.TimeFrame.TFrame(TIMEFRAMES[2]), compression=1)

any help will be appreciated. Thanks!

from btoandav20.

einnairo avatar einnairo commented on June 14, 2024

Ok managed to get it to work.

dataX = store.getdata(dataname='EUR_USD', timeframe=bt.TimeFrame.Minutes, compression=1, qcheck=0.5, backfill_start=True, backfill=False,  reconnect=True, reconntimeout=10, tz='America/New_York', bidask=True, bar2edge=True, adjbartime=True, rightedge=True)  

cerebro.resampledata(dataX, timeframe=bt.TimeFrame.Minutes, compression=1)
cerebro.resampledata(dataX, timeframe=bt.TimeFrame.Minutes, compression=3)

Thanks! Appreciate the great job on this package.

from btoandav20.

einnairo avatar einnairo commented on June 14, 2024

Pls help me out with this:
https://community.backtrader.com/topic/2603/btoandav20-sizers

Thanks alot in advance.

from btoandav20.

einnairo avatar einnairo commented on June 14, 2024

Hi happydasch, I also like to ask how do you modify your bracket order after a position has been opened? I would like to change stop price and limit price or even change the order type of stop to stop trail. I feel this is quite critical.

I can created singular orders like a buy to open the position, then add a stoploss order.But with oanda the margin required is alot more as the stoploss is not linked to the position.

Using parent order parameter to link both orders does not work on oanda too.

Hope to hear from you, many thanks as always.

from btoandav20.

happydasch avatar happydasch commented on June 14, 2024

Hi happydasch, I also like to ask how do you modify your bracket order after a position has been opened? I would like to change stop price and limit price or even change the order type of stop to stop trail. I feel this is quite critical.

This is not possible, you would need to cancel the open orders and create new ones

from btoandav20.

happydasch avatar happydasch commented on June 14, 2024

you can check out the latest commits with comminfo refactoring

from btoandav20.

einnairo avatar einnairo commented on June 14, 2024

Thanks once again.

Can I also ask another question. You mentioned above the oanda trade id required for a non bracket stoptrail order. Can I check how do you set the trade id or get the trade id? Actually on my app it shows the ticket number and its always increasing.

So you mentioned I have to pass in the id parameter, I tried the below, after refering to the syntax of id in oanda api, seem to be just id of type string, link here
https://developer.oanda.com/rest-live/orders/#createNewOrder:

self.long_trailstop = self.sell(price=trail_stop_px, exectype=bt.Order.StopTrail, size=stop_size, trailamount=self.ATR_at_exe * 1.5, id='888')
but the above does not work.

I got an error which bascially is an error when issuing stoptrail orders without brackets.

okwargs['type'] = self._ORDEREXECS[order.exectype]
KeyError: 5

Any ideas?

from btoandav20.

happydasch avatar happydasch commented on June 14, 2024

Thanks once again.

Can I also ask another question. You mentioned above the oanda trade id required for a non bracket stoptrail order. Can I check how do you set the trade id or get the trade id? Actually on my app it shows the ticket number and its always increasing.

So you mentioned I have to pass in the id parameter, I tried the below, after refering to the syntax of id in oanda api, seem to be just id of type string, link here
https://developer.oanda.com/rest-live/orders/#createNewOrder:

self.long_trailstop = self.sell(price=trail_stop_px, exectype=bt.Order.StopTrail, size=stop_size, trailamount=self.ATR_at_exe * 1.5, id='888')
but the above does not work.

I got an error which bascially is an error when issuing stoptrail orders without brackets.

okwargs['type'] = self._ORDEREXECS[order.exectype]
KeyError: 5

Any ideas?

open a new issue with this, add some example code, so I can see what is causing this error

from btoandav20.

happydasch avatar happydasch commented on June 14, 2024

at the moment I am working on backtesting and sizer, I will write some description on how to use commission, sizer, margin and leverage once it is done.

from btoandav20.

einnairo avatar einnairo commented on June 14, 2024

Thanks ! Appreciate what you are doing for the community. Hope single order trailing stop or perhaps syntax to modify existing bracket orders will be extremely helpful. I think many strats out there will work with trailing stops that are added or modified from initial basic stops after meeting certain conditions. Thanks alot!

from btoandav20.

einnairo avatar einnairo commented on June 14, 2024

Also I have tried the Risk sizer now using risk_percents=2. 1 thing I find interesting and pls correct me if I am wrong, is no matter what, it will use units available due to margin required. I am printing out the calculations from your sizer source code.

source code cash: 5032.7468 (cash)
source code stop pips: 1.2 (self.p.stoploss)
source code position size: 0 (position)
source code cash_to_use SGD: 100.654936 (cash_to_use - this is in account currency)
source code price in SGD price: 1.4, SGD_USD (price - custom for me at 1.4 exchange rate)
source code convert cash to use from sgd to usd: 71.89638285714287 (cash_to_use - in usd)
source code price_per_pip: 59.91365238095239 (price_per_pip)

source code price of pair: {'type': 'PRICE', 'instrument': 'EUR_USD', 'time': '1591674898.818356540', 'status': 'tradeable', 'tradeable': True, 'bids': [{'price': 1.12865, 'liquidity': 1000000}, {'price': 1.12864, 'liquidity': 2000000}, {'price': 1.12863, 'liquidity': 2000000}, {'price': 1.12861, 'liquidity': 5000000}], 'asks': [{'price': 1.12872, 'liquidity': 1000000}, {'price': 1.12874, 'liquidity': 2000000}, {'price': 1.12875, 'liquidity': 2000000}, {'price': 1.12876, 'liquidity': 5000000}], 'closeoutBid': 1.12861, 'closeoutAsk': 1.12876, 'quoteHomeConversionFactors': {'positiveUnits': '1.38984', 'negativeUnits': '1.39007'}, 'unitsAvailable': {'default': {'long': '64159.0', 'short': '64159.0'}, 'reduceFirst': {'long': '64159.0', 'short': '64159.0'}, 'reduceOnly': {'long': '0.0', 'short': '0.0'}, 'openOnly': {'long': '64159.0', 'short': '64159.0'}}}
(the above is your call on get_pricing(name))

source code size: 599136.5238095239 (size - this is calculated size)
source code disect: 64159.0 (this is the min of earlier calculated size and units available)
close price is self.data0.close[0]: 1.12865 (just for ref)

The size goes to units available. I dont think there is a time when it will use the larger size due to margin. On my app, it shows margin SGD1500+ when i have cash of SGD5K. So i am puzzled why I cannot use more margin. Next I removed the bracket stop, and create a single stop order and even so, margin required is SGD2516.
In essence it means I cannot even risk 2% of my account. My margin requirement rules are 5% for this eur usd pair, and leverage at 20:1.

from btoandav20.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.