Hi everyone
I try coding for backtesting as the code below. The main point in code is how to apply PositionScore. I need to apply PositionScore variable to choose which trade are prioritize on logic
PositionScore RSI(15)>60 .
I study how to use position score as this link. but not met as my requirement, I prefer the trade that PositionScore RSI(15)>60
Portfolio-level backtesting IMPORTANT: Please read first Tutorial:
In sample code
PositionScore = 100-RSI(); // prefer stocks that have low RSI;
My code is below.
PosQty = 5; // You can define here how many open positions you want
SetOption ("InitialEquity", 10000000);
SetOption ("MaxOpenPositions", PosQty);
SetOption ("MinShares", 100);
RoundLotSize=100;
SetPositionSize(100/PosQty,spsPercentOfEquity);
SetOption("CommissionMode",1);
SetOption("CommissionAmount",0.1);
//SetTradeDelays(1,1,0,0);
//SetOption( "ActivateStopsImmediately", True );
//Test MA compare to EMA
//***********************************************************
BuyPrice=Close;
SellPrice=close;
sEMa=10;
lEMa=50;
buyX=EMA(C, sEMa)>EMA(C, lEMa);
sellX=EMA(C,sEMa)<EMA(C,lEMa);
Buy=buyX;
Sell=sellX;
//Sell=0;
Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);
//***********************************************************
PositionScore = RSI(15)>60;
PositionScore = 100 - valuewhen(RSI(15) > 60, RSI(15));
I don't yet understand completely in ValueWhen function.
however I will try it out as your post.
Primarily, my logic for ranking to choose prefered stock using RSI is below.
assuming MaxOpenPosition=2
The following stock occurs buy signal simultaneously.
stock A ,rsi=50
stock B,rsi=62
stock C rsi= 65
stock D rsi=70
stock E rsi=55
stock E rsi=61
so C (rsi=65) and D=(rsi70) are selected to trade order by RSI descending.
@technqvi, based on your original description, I'm not sure that you need ValueWhen()
, although perhaps @Anthony is seeing something that I am not.
If you only want to take trades when the RSI is above 60, then just add that to your Buy logic, like this:
Buy=buyX AND RSI(15) > 60;
To rank your entries by the highest RSI value, use something like this:
PositionScore = RSI(15);
@mradtke
your answer is right as my specification.
no need to use valueWhen.
PositionScore = RSI(15); it is enought for me.
I want to take the trade if only buyx ; thus at the day, many stocks may meet as my criteria buyx (ema10>ema50 ) but RSI is still less than 60.
however, we prefer to choose the first two stocks have RSI value the most.
The following figure is shown the test result , given that MaxOpenPositions=2
Not set position score
set position score
don't set Set MaxOpenPositions
@technqvi, I can't tell from your post whether your problem is solved or if you still have a question. Since you did not mark any of the responses as the solution, I'm guessing that something still isn't working as you hoped?
Your post was resolved to my issue absolutely.
PositionScore = RSI(15)