//Initializing the Variables
STP_Sc = 2*ticksize; //Size of the ScaleIn point (2 ticks below entryprice)
STP_L = 2*Ticksize; //Size of the StopLoss point (2 ticks below ScaleIn Price)
STP_G = 10*Ticksize; // Size of the TakeProfit point (10 tics above entryprice)
Bought = 0; // Just a variable to tell if there is an OpenPosition
Price_of_buy = 0; // The EntryPrice
Profit = 0; // The actual StopGain Price
ScaleIn = 0; // The actual ScaleIn EntryPrice
Price_of_stoploss = 0; // The actual StopLoss Price
Stage = 0; // 0 = didnt touch the ScaleIn, 1 = touched the ScaleIn
//Initializing the loop
for( i = 0; i < BarCount; i++ )
{
if( Price_of_buy == 0 AND Buy[ i ] ) //If no openpositions and buy is true
{
//Filling the Variables
Bought = 1 ;
Price_of_buy = BuyPrice[ i ];
ScaleIn = BuyPrice[ i ] - STP_Sc;
Price_of_stoploss = BuyPrice[ i ] - STP_Sc - STP_L;
Profit = BuyPrice[i] + STP_G;
// Here we start with the conditions at the ENTRY BAR (THE SAME BAR)!
if(Low[ i ] <= ScaleIn) //If the low touches the ScaleIn price, I want it to Buy another contract
{
Buy[ i ] = sigScaleIn;
Sell[ i ] = 0; // mark appropriate exit code (I think this is redundant here, right? Yes, I got it from the Amibroker Examples)
buyPrice[i] = ScaleIn;
Stage = 1;
/*
So the backtest would buy at this price. And thats what I want. But... I also want it to make another test at the same bar AFTER the ScaleIn Buy. I want to see if the low touched the Stoploss price! If it touched, I want it to exit the 2 contracts. Lets continue the code.
*/
if( Low[i] <= Price_of_stoploss ) //Notice that this next if is NESTED (an IF inside an IF)
{
Buy[ i ] = 0;
Sell[ i ] = 1; // mark appropriate exit code
SellPrice[i] = Price_of_stoploss ;
Stage = 0; //reset
Price_of_buy = 0; // reset
Price_of_stoploss = 0;
Bought = 0;
Profit = 0;
ScaleIn = 0;
}
}
Ok. Here is how I THINK the Array table would look like to the Amibroker Backtest AFTER THE FIRST IF (The scalein buy):
Buy = 1
Sell = 0
This is how I THINK the ARRAY table after the second IF executed would be:
Buy = 0
Sell = 1
So, at the same bar, both of the conditions would be executed? In my tests its Always the second array table that is execute: it makes a sell at the StopLoss Price, escaping the ScaleIn Part. But, it there is no StopLoss touch, the ScaleIn is executed perfectly.
So my theory is that the second IF subscribe the first and only the “last condition met” is executed by the backtester. Please, correct me if im wrong. Im just a noob.
And my question is: how do i code it so both actions (ScaleIn and StopLoss) are executed by the backtester if they happen at the same bar?
Thanks!!!