Simple Coding Help - No Promise.

While backtesting in Amibroker, using the following block:

BuyPrice=ValueWhen(Buy,O);
ShortPrice=ValueWhen(Short,O);
SetTradeDelays( 1, 1, 1, 1 );


instead of:

BuyPrice=Open;
ShortPrice=Open;
SetTradeDelays( 1, 1, 1, 1 );


results in huge difference in profits.

Why so much difference? Is ValueWhen looking at future?
 
While backtesting in Amibroker, using the following block:

BuyPrice=ValueWhen(Buy,O);
ShortPrice=ValueWhen(Short,O);
SetTradeDelays( 1, 1, 1, 1 );


instead of:

BuyPrice=Open;
ShortPrice=Open;
SetTradeDelays( 1, 1, 1, 1 );


results in huge difference in profits.

Why so much difference? Is ValueWhen looking at future?
No, ValueWhen with only two parameters will not look into future. If you give 3rd parameter as negative it will look into future.

What is your Buy/Sell formula? In any case, first version is more likely to be correct
 
Hi Experts!

Is there a way to capture the net position in a stock in AMI?

If I am running an afl based ATS in crude oil and the afl has just shorted 1 lot, is there a way I can get this value of 1 into a variable, say np?
 
I needed an intraday cumulative volume scanner AFL - it should display cumulative volume bars on chart and same can be scanned with a Filter say - Filter Cumvol > 100000 or so.... Will be much grateful for help...
 

trash

Well-Known Member
While backtesting in Amibroker, using the following block:

BuyPrice=ValueWhen(Buy,O);
ShortPrice=ValueWhen(Short,O);
SetTradeDelays( 1, 1, 1, 1 );


instead of:

BuyPrice=Open;
ShortPrice=Open;
SetTradeDelays( 1, 1, 1, 1 );


results in huge difference in profits.

Why so much difference? Is ValueWhen looking at future?
Using BuyPrice = ValueWhen(Buy,O) etc. is nonsense. Just use BuyPrice = Open etc and you are done. ValueWhen reports last occurrence while it may not be the bar when actual buying took place.

If you wanna store price at buy and wanna sell at cross of buyprice then rather do this.

Code:
// basic priceatbuy code by Tomasz Janeczko, multiple modifications by trash

delay = 1;
SetTradeDelays( delay, delay, delay, delay );// used in analysis
SetPositionSize( 10, spsPercentOfEquity );

if ( delay > 0 )
{
    array  = Open;
}
else
{
    array  = Close;
}

Buy   = Sell = 0;
Short = Cover = 0;
BuyPrice = SellPrice = array;

_MA = MA( C, 20 );
BuyConditions = Cross( C, _MA );

priceatbuy = Null;
priceatsell = Null;
patba = Null; // PriceAtBuy Array (for plotting only)
patse = Null;

for ( i = delay; i < BarCount; i++ )
{
    SellSignal = array[ i ] > priceatbuy;

    if ( SellSignal AND NOT IsNull( priceatbuy ) )
    {
        Sell[ i - delay ] = 1;
        priceatbuy = Null;
        priceatsell = SellPrice[i];
    }

    BuySignal = BuyConditions[ i - delay ];

    if ( IsNull( priceatbuy ) AND BuySignal )
    {
        Buy[ i - delay ] = 1;
        priceatbuy = BuyPrice[ i ];
        priceatsell = Null;
    }

    // to plot price at buy and sell you need an ARRAY, not scalar
    patba[ i ] = priceatbuy; 
    patse[ i ] = priceatsell;    
}

SetChartOptions( 0, chartShowArrows | chartShowDates | chartWrapTitle );
_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%), Vol %g {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ), V ) );
Plot( C, "Close", ParamColor( "Color Price", colorDefault ), styleNoTitle | ParamStyle( "Style" ) | GetPriceStyle(), 0, 1, 0, 0);

Plot( patba, "priceatbuy", colorGreen, styleLine, Null, Null, 0, 1 );
Plot( patse, "priceatsell", colorRed, styleLine, Null, Null, 0, 1 );
Plot( _MA, "MA", ParamColor( "Color MA", colorCycle ), styleLine );

PlotShapes( Buy*shapeUpArrow, colorGreen, 0, H, -15 );
PlotShapes( Buy*shapeHollowUpArrow, colorWhite, 0, H, -15 );
PlotShapes( Ref( Buy, -delay )*shapeHollowSmallCircle, colorGreen, 0, BuyPrice, 0 );

PlotShapes( Sell*shapeDownArrow, colorRed, 0, H, -15 );
PlotShapes( Sell*shapeHollowDownArrow, colorWhite, 0, H, -15 );
PlotShapes( Ref( Sell, -delay )*shapeHollowSmallCircle, colorRed, 0, SellPrice, 0 );
BTW here is another quote by Tomasz

No. To buy always at open you should write:

BuyPrice = Open;

(isn't this obvious ?)

If you write

BuyPrice = ValueWhen(Buy, Open);

and you SET DELAY > 0
this code actually attempts to buy at OPEN price WHEN NON DELAYED buy
occurred. Since trading is delayed as per your setting the open of
N-days before may NOT fit into High-LOW range of the bar when
ACTUAL TRADE takes place so AmiBroker must readjust it to
fit into H-L range (what ever is nearer your desired price).


Hope this helps.

Best regards,
Tomasz Janeczko
amibroker.com
The quote is in regards to this user code

Code:
> //----------------------------------------------------
> // overide default settings, set buy delay at 1 bar
> SetTradeDelays(1,0,0,0);
> 
> // make a simple trading system
> Buy = Cross(RSI(),30);
> 
> // define the BuyPrice
> BuyPrice = ValueWhen( Buy, Open);
> 
> // define the sell condition
> SellCond1 = C > BuyPrice * 1.02; 
> SellCond2 = C < BuyPrice * 0.96; 
> SellCond3 = Cross( BarsSince(Buy), 5 ); 
> 
> Sell = SellCond1 OR SellCond2 OR SellCond3;
> 
> // define the SellPrice
> SellPrice = ValueWhen( Sell, Close);
> 
> Buy = ExRem(Buy,Sell);
> Sell = ExRem(Sell,Buy);
> //----------------------------------------------------
>
https://groups.yahoo.com/neo/groups/amibroker/conversations/messages/48785
 
Last edited:

pratapvb

Well-Known Member
Hi All,

Can any one post Volume Weighted Average AFl for Amibroker.

Thanks in advance
check my vwap thread post 511 and go to relevant post from there
 

xsis

Active Member
deal coders

from some another forum i found that its possible to code cumulative delta volume in amibroker and use it successfully on 1sec data? is anybody using it? can anybody post the same?

thks
 

pratapvb

Well-Known Member
Thanks Pratap. Wants to know is it relevent for trading Daily, 4-hrs and 30-mins charts
maybe hgher order vwap like vwap series could be....but I have not done it or tested it

also the values are more correct in the lowest TF used as avg price of bar and corresponding volume is used....so the lesser the TF lesser the calculation error....so even if using on higher TF the levels would have to be checked on 1min or lowest TF available
 

Similar threads