Buying on the next Open price after a signal

Hello all, I started learning alf very recently and I am having a question about buying on the next open after a signal.

This is what have coded :

Buy = Cross(MA(C,20), MA(C,50));
Sell = Cross(MA(C,50),MA(C,20));

BuyPrice = Open;
SellPrice = Open;

SetTradeDelays(1,1,1,1);

The above code works well but I am wanting to code the same logic with another approach:

Buy = Cross(MA(C,20), MA(C,50));
Sell = Cross(MA(C,50),MA(C,20));

BuyPrice = ValueWhen(Buy,Open,1);
SellPrice = ValueWhen(Sell,Open,1);

Unfortunately it's not working and I don`t know why. Can somebody tell me which are my options to code a system which buys/sells on the open of next bar?

You haven't said WHY you want to use a different approach rather than your first example, which works. Your second example doesn't work as you expect because you only assigned new Buy and Sell prices... you didn't do anything to change when the entry/exit occurs relative to the signal.

Here's one possible alternative:

SetTradeDelays(0,0,0 0);

BuySig = Cross(MA(C,20), MA(C,50));
SellSig = Cross(MA(C,50),MA(C,20));

// Enter/Exit one bar after the signal occurs
Buy = Ref(BuySig,-1);
Sell = Ref(SellSig,-1);

BuyPrice = Open;
SellPrice = Open;
4 Likes

Thank you very much for your answer. I haven't said why I wanted to use a different approach because I was just experimenting and exploring how I can code the same logic in a different way.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.