Conditional Commission Rate on SetOption


I'm trying to set a condition where the commission rate will change on whether the trade is a Buy or a Sell. Buy rate = 0.295 and Sell rate = 0.895

I came up with this:

Plot( Close, "Close", colorDefault, styleCandle );

buyFees = 0.295;
sellFees = 0.895;

BuySignal = IIf(Ref(H,-1) == 9.58, 1, 0);
SellSignal = IIf(Ref(C,-1) > C, 1, 0);
SetPositionSize(1000, spsShares);

Buy = Ref(O, 0) && BuySignal;
Sell = Ref(C, 0) && SellSignal;

Buy = ExRem(Buy, Sell);
Sell = ExRem(Sell, Buy);

useBuyFees = ExRem(Buy, Sell);
useSellFees = ExRem(Sell, Buy);

tradingFees = IIf(useBuyFees, buyFees, sellFees);
tf = NumToStr(tradingFees);
fees = StrToNum(tf);
printf("fees used = %s\n", tf);

SetOption("CommissionMode", 1);
SetOption("CommissionAmount", fees);

The Interpretation does show the fees are changing depending on the trade. However in the Backtest it's just using the Sell rate of 0.895 regardless of the trade.

Am I missing something? TIA

Edit: My apologies for not having my account verified before posting. :grin:

@arcee, welcome to this Community.

Your request is similar to the one that was discussed in this past thread, but unfortunately, in that case, no proper solution was provided.
I fear that "Commissions" (like FX Cash conversion settings), due to the many differences between user needs, is one one the areas where AmiBroker (and probably almost any other applications in this field) is yet not totally customizable.

But I genuinely hope that @Tomasz (or other experienced users) will contradict me and provide you a solution!

Otherwise, you can always use the Feedback Center to submit a feature request.

1 Like

Thanks for the welcome @beppe. You’re correct. Also one of the reasons Why I posted this was because of the lack of solution in that similar thread.

Was really hoping I could atleast simulate an accurate backtesting result if possible.

Well, before I do use the Feedback center I hope @Tomasz and/or other advanced users here could help us find a way to come up with a workaround.

Thanks again for the help @beppe.

1 Like

In addition to very wide range of build-in commission settings (multi-tier commission table, fixed value, fixed percent of trade value), you can also:

  1. add commission to the price (BuyPrice/SellPrice/CoverPrice/ShortPrice)
  2. use custom backtester and subtract commissions directly from bo.Cash

There are plenty of options to address every need.


Thank you for these @Tomasz. :smiley: I will try and find my way from here. Will post some updates in this thread.

Sometimes the simplest code is the simplest solution. No need to complicate things. Thanks @Tomasz

BuyPrice = C*1.00295;
SellPrice = C*(1-0.00895);

Caveat: this solution is not absolute and will still depend on what you want to achieve.